KiCad PCB EDA Suite
edit_points.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2014-2017 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
26 #include "edit_points.h"
27 
28 bool EDIT_POINT::WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const
29 {
30  // Corners of the EDIT_POINT square
31  VECTOR2I topLeft = GetPosition() - aSize;
32  VECTOR2I bottomRight = GetPosition() + aSize;
33 
34  return ( aPoint.x > topLeft.x && aPoint.y > topLeft.y &&
35  aPoint.x < bottomRight.x && aPoint.y < bottomRight.y );
36 }
37 
38 
40  EDA_ITEM( NOT_USED ), m_parent( aParent )
41 {
42 }
43 
44 
45 EDIT_POINT* EDIT_POINTS::FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView ) // fixme: ugly
46 {
47  float size = aView->ToWorld( EDIT_POINT::POINT_SIZE );
48 
49  std::deque<EDIT_POINT>::iterator pit, pitEnd;
50  for( pit = m_points.begin(), pitEnd = m_points.end(); pit != pitEnd; ++pit )
51  {
52  EDIT_POINT& point = *pit;
53 
54  if( point.WithinPoint( aLocation, size ) )
55  return &point;
56  }
57 
58  std::deque<EDIT_LINE>::iterator lit, litEnd;
59  for( lit = m_lines.begin(), litEnd = m_lines.end(); lit != litEnd; ++lit )
60  {
61  EDIT_LINE& line = *lit;
62 
63  if( line.WithinPoint( aLocation, size ) )
64  return &line;
65  }
66 
67  return NULL;
68 }
69 
70 
71 int EDIT_POINTS::GetContourStartIdx( int aPointIdx ) const
72 {
73  int lastIdx = 0;
74 
75  for( int idx : m_contours )
76  {
77  if( idx >= aPointIdx )
78  return lastIdx;
79 
80  lastIdx = idx + 1;
81  }
82 
83  return lastIdx;
84 }
85 
86 
87 int EDIT_POINTS::GetContourEndIdx( int aPointIdx ) const
88 {
89  for( int idx : m_contours )
90  {
91  if( idx >= aPointIdx )
92  return idx;
93  }
94 
95  return m_points.size() - 1;
96 }
97 
98 
99 bool EDIT_POINTS::IsContourStart( int aPointIdx ) const
100 {
101  for( int idx : m_contours )
102  {
103  if( idx + 1 == aPointIdx )
104  return true;
105 
106  // the list is sorted, so we cannot expect it any further
107  if( idx > aPointIdx )
108  break;
109  }
110 
111  return ( aPointIdx == 0 );
112 }
113 
114 
115 bool EDIT_POINTS::IsContourEnd( int aPointIdx ) const
116 {
117  for( int idx : m_contours )
118  {
119  if( idx == aPointIdx )
120  return true;
121 
122  // the list is sorted, so we cannot expect it any further
123  if( idx > aPointIdx )
124  break;
125  }
126 
127  // the end of the list surely is the end of a contour
128  return ( aPointIdx == (int) m_points.size() - 1 );
129 }
130 
131 
132 EDIT_POINT* EDIT_POINTS::Previous( const EDIT_POINT& aPoint, bool aTraverseContours )
133 {
134  for( unsigned int i = 0; i < m_points.size(); ++i )
135  {
136  if( m_points[i] == aPoint )
137  {
138  if( !aTraverseContours && IsContourStart( i ) )
139  return &m_points[GetContourEndIdx( i )];
140 
141  if( i == 0 )
142  return &m_points[m_points.size() - 1];
143  else
144  return &m_points[i - 1];
145  }
146  }
147 
148  return NULL;
149 }
150 
151 
153 {
154  for( unsigned int i = 0; i < m_lines.size(); ++i )
155  {
156  if( m_lines[i] == aLine )
157  {
158  if( i == 0 )
159  return &m_lines[m_lines.size() - 1];
160  else
161  return &m_lines[i - 1];
162  }
163  }
164 
165  return NULL;
166 }
167 
168 
169 EDIT_POINT* EDIT_POINTS::Next( const EDIT_POINT& aPoint, bool aTraverseContours )
170 {
171  for( unsigned int i = 0; i < m_points.size(); ++i )
172  {
173  if( m_points[i] == aPoint )
174  {
175  if( !aTraverseContours && IsContourEnd( i ) )
176  return &m_points[GetContourStartIdx( i )];
177 
178  if( i == m_points.size() - 1 )
179  return &m_points[0];
180  else
181  return &m_points[i + 1];
182  }
183  }
184 
185  return NULL;
186 }
187 
188 
190 {
191  for( unsigned int i = 0; i < m_lines.size(); ++i )
192  {
193  if( m_lines[i] == aLine )
194  {
195  if( i == m_lines.size() - 1 )
196  return &m_lines[0];
197  else
198  return &m_lines[i + 1];
199  }
200  }
201 
202  return NULL;
203 }
204 
205 
207 {
208  BOX2I box;
209  bool empty = true;
210 
211  for( const auto& point : m_points )
212  {
213  if( empty )
214  {
215  box.SetOrigin( point.GetPosition() );
216  empty = false;
217  }
218  else
219  {
220  box.Merge( point.GetPosition() );
221  }
222  }
223 
224  for( const auto& line : m_lines )
225  {
226  if( empty )
227  {
228  box.SetOrigin( line.GetOrigin().GetPosition() );
229  box.SetEnd( line.GetEnd().GetPosition() );
230  empty = false;
231  }
232  else
233  {
234  box.Merge( line.GetOrigin().GetPosition() );
235  box.Merge( line.GetEnd().GetPosition() );
236  }
237  }
238 
239  return box;
240 }
241 
242 
243 void EDIT_POINTS::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
244 {
245  auto gal = aView->GetGAL();
246 
247  gal->SetFillColor( KIGFX::COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
248  gal->SetIsFill( true );
249  gal->SetIsStroke( false );
250  gal->PushDepth();
251  gal->SetLayerDepth( gal->GetMinDepth() );
252 
253  float size = aView->ToWorld( EDIT_POINT::POINT_SIZE );
254 
255  for( const EDIT_POINT& point : m_points )
256  gal->DrawRectangle( point.GetPosition() - size / 2, point.GetPosition() + size / 2 );
257 
258  for( const EDIT_LINE& line : m_lines )
259  {
260  gal->DrawCircle( line.GetPosition(), size / 2 );
261  }
262 
263  gal->PopDepth();
264 }
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
std::list< int > m_contours
Indices of end contour points.
Definition: edit_points.h:526
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
Definition: edit_points.h:524
bool WithinPoint(const VECTOR2I &aPoint, unsigned int aSize) const
Function WithinPoint()
Definition: edit_points.cpp:28
std::deque< EDIT_LINE > m_lines
EDIT_LINEs for modifying m_parent.
Definition: edit_points.h:525
the 3d code uses this value
Definition: typeinfo.h:80
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Function ToWorld() Converts a screen space point/vector to a point/vector in world space coordinates...
Definition: view.cpp:441
Class EDIT_LINE.
Definition: edit_points.h:189
virtual const BOX2I ViewBBox() const override
>
EDA_ITEM * Next() const
Definition: base_struct.h:220
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
>
int GetContourEndIdx(int aPointIdx) const
Function GetContourEndIdx()
Definition: edit_points.cpp:87
EDIT_POINT * FindPoint(const VECTOR2I &aLocation, KIGFX::VIEW *aView)
Function FindPoint()
Definition: edit_points.cpp:45
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:177
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect...
Definition: box2.h:350
void SetEnd(coord_type x, coord_type y)
Definition: box2.h:213
virtual VECTOR2I GetPosition() const
Function GetPosition()
Definition: edit_points.h:65
bool IsContourStart(int aPointIdx) const
Function IsContourStart()
Definition: edit_points.cpp:99
bool IsContourEnd(int aPointIdx) const
Function IsContourEnd()
void SetOrigin(const Vec &pos)
Definition: box2.h:198
static bool empty(const wxTextEntryBase *aCtrl)
EDIT_POINTS(EDA_ITEM *aParent)
Constructor.
Definition: edit_points.cpp:39
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:165
static const int POINT_SIZE
Single point size in pixels
Definition: edit_points.h:171
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Function Previous()
Class EDIT_POINT.
Definition: edit_points.h:46
Class VIEW.
Definition: view.h:58
int GetContourStartIdx(int aPointIdx) const
Function GetContourStartIdx()
Definition: edit_points.cpp:71
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39