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-2019 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 <gal/color4d.h>
27 #include "tool/edit_points.h"
28 
29 bool EDIT_POINT::WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const
30 {
31  // Corners of the EDIT_POINT square
32  VECTOR2I topLeft = GetPosition() - aSize;
33  VECTOR2I bottomRight = GetPosition() + aSize;
34 
35  return ( aPoint.x > topLeft.x && aPoint.y > topLeft.y &&
36  aPoint.x < bottomRight.x && aPoint.y < bottomRight.y );
37 }
38 
39 
41  EDA_ITEM( NOT_USED ), m_parent( aParent )
42 {
43 }
44 
45 
46 EDIT_POINT* EDIT_POINTS::FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView ) // fixme: ugly
47 {
48  float size = aView->ToWorld( EDIT_POINT::POINT_SIZE );
49 
50  std::deque<EDIT_POINT>::iterator pit, pitEnd;
51  for( pit = m_points.begin(), pitEnd = m_points.end(); pit != pitEnd; ++pit )
52  {
53  EDIT_POINT& point = *pit;
54 
55  if( point.WithinPoint( aLocation, size ) )
56  return &point;
57  }
58 
59  std::deque<EDIT_LINE>::iterator lit, litEnd;
60  for( lit = m_lines.begin(), litEnd = m_lines.end(); lit != litEnd; ++lit )
61  {
62  EDIT_LINE& line = *lit;
63 
64  if( line.WithinPoint( aLocation, size ) )
65  return &line;
66  }
67 
68  return NULL;
69 }
70 
71 
72 int EDIT_POINTS::GetContourStartIdx( int aPointIdx ) const
73 {
74  int lastIdx = 0;
75 
76  for( int idx : m_contours )
77  {
78  if( idx >= aPointIdx )
79  return lastIdx;
80 
81  lastIdx = idx + 1;
82  }
83 
84  return lastIdx;
85 }
86 
87 
88 int EDIT_POINTS::GetContourEndIdx( int aPointIdx ) const
89 {
90  for( int idx : m_contours )
91  {
92  if( idx >= aPointIdx )
93  return idx;
94  }
95 
96  return m_points.size() - 1;
97 }
98 
99 
100 bool EDIT_POINTS::IsContourStart( int aPointIdx ) const
101 {
102  for( int idx : m_contours )
103  {
104  if( idx + 1 == aPointIdx )
105  return true;
106 
107  // the list is sorted, so we cannot expect it any further
108  if( idx > aPointIdx )
109  break;
110  }
111 
112  return ( aPointIdx == 0 );
113 }
114 
115 
116 bool EDIT_POINTS::IsContourEnd( int aPointIdx ) const
117 {
118  for( int idx : m_contours )
119  {
120  if( idx == aPointIdx )
121  return true;
122 
123  // the list is sorted, so we cannot expect it any further
124  if( idx > aPointIdx )
125  break;
126  }
127 
128  // the end of the list surely is the end of a contour
129  return ( aPointIdx == (int) m_points.size() - 1 );
130 }
131 
132 
133 EDIT_POINT* EDIT_POINTS::Previous( const EDIT_POINT& aPoint, bool aTraverseContours )
134 {
135  for( unsigned int i = 0; i < m_points.size(); ++i )
136  {
137  if( m_points[i] == aPoint )
138  {
139  if( !aTraverseContours && IsContourStart( i ) )
140  return &m_points[GetContourEndIdx( i )];
141 
142  if( i == 0 )
143  return &m_points[m_points.size() - 1];
144  else
145  return &m_points[i - 1];
146  }
147  }
148 
149  return NULL;
150 }
151 
152 
154 {
155  for( unsigned int i = 0; i < m_lines.size(); ++i )
156  {
157  if( m_lines[i] == aLine )
158  {
159  if( i == 0 )
160  return &m_lines[m_lines.size() - 1];
161  else
162  return &m_lines[i - 1];
163  }
164  }
165 
166  return NULL;
167 }
168 
169 
170 EDIT_POINT* EDIT_POINTS::Next( const EDIT_POINT& aPoint, bool aTraverseContours )
171 {
172  for( unsigned int i = 0; i < m_points.size(); ++i )
173  {
174  if( m_points[i] == aPoint )
175  {
176  if( !aTraverseContours && IsContourEnd( i ) )
177  return &m_points[GetContourStartIdx( i )];
178 
179  if( i == m_points.size() - 1 )
180  return &m_points[0];
181  else
182  return &m_points[i + 1];
183  }
184  }
185 
186  return NULL;
187 }
188 
189 
191 {
192  for( unsigned int i = 0; i < m_lines.size(); ++i )
193  {
194  if( m_lines[i] == aLine )
195  {
196  if( i == m_lines.size() - 1 )
197  return &m_lines[0];
198  else
199  return &m_lines[i + 1];
200  }
201  }
202 
203  return NULL;
204 }
205 
206 
208 {
209  BOX2I box;
210  bool empty = true;
211 
212  for( const auto& point : m_points )
213  {
214  if( empty )
215  {
216  box.SetOrigin( point.GetPosition() );
217  empty = false;
218  }
219  else
220  {
221  box.Merge( point.GetPosition() );
222  }
223  }
224 
225  for( const auto& line : m_lines )
226  {
227  if( empty )
228  {
229  box.SetOrigin( line.GetOrigin().GetPosition() );
230  box.SetEnd( line.GetEnd().GetPosition() );
231  empty = false;
232  }
233  else
234  {
235  box.Merge( line.GetOrigin().GetPosition() );
236  box.Merge( line.GetEnd().GetPosition() );
237  }
238  }
239 
240  return box;
241 }
242 
243 
244 void EDIT_POINTS::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
245 {
246  auto gal = aView->GetGAL();
247 
248  if( aView->GetGAL()->GetClearColor().GetBrightness() > 0.5 )
249  gal->SetFillColor( KIGFX::COLOR4D( 0, 0, 0, 1.0 ) );
250  else
251  gal->SetFillColor( KIGFX::COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
252  gal->SetIsFill( true );
253  gal->SetIsStroke( false );
254  gal->PushDepth();
255  gal->SetLayerDepth( gal->GetMinDepth() );
256 
257  float size = aView->ToWorld( EDIT_POINT::POINT_SIZE );
258 
259  for( const EDIT_POINT& point : m_points )
260  gal->DrawRectangle( point.GetPosition() - size / 2, point.GetPosition() + size / 2 );
261 
262  for( const EDIT_LINE& line : m_lines )
263  {
264  gal->DrawCircle( line.GetPosition(), size / 2 );
265  }
266 
267  gal->PopDepth();
268 }
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
double GetBrightness() const
Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1....
Definition: color4d.h:269
int GetContourEndIdx(int aPointIdx) const
Function GetContourEndIdx()
Definition: edit_points.cpp:88
std::list< int > m_contours
Indices of end contour points.
Definition: edit_points.h:544
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
Definition: edit_points.h:542
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:475
std::deque< EDIT_LINE > m_lines
EDIT_LINEs for modifying m_parent.
Definition: edit_points.h:543
the 3d code uses this value
Definition: typeinfo.h:80
Class EDIT_LINE.
Definition: edit_points.h:207
virtual const BOX2I ViewBBox() const override
bool IsContourStart(int aPointIdx) const
Function IsContourStart()
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
bool IsContourEnd(int aPointIdx) const
Function IsContourEnd()
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
EDA_ITEM * Next() const
Definition: base_struct.h:218
EDIT_POINT * FindPoint(const VECTOR2I &aLocation, KIGFX::VIEW *aView)
Function FindPoint()
Definition: edit_points.cpp:46
virtual VECTOR2I GetPosition() const
Function GetPosition()
Definition: edit_points.h:68
int GetContourStartIdx(int aPointIdx) const
Function GetContourStartIdx()
Definition: edit_points.cpp:72
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:384
const COLOR4D & GetClearColor() const
void SetEnd(coord_type x, coord_type y)
Definition: box2.h:223
size_t i
Definition: json11.cpp:597
void SetOrigin(const Vec &pos)
Definition: box2.h:208
static bool empty(const wxTextEntryBase *aCtrl)
EDIT_POINTS(EDA_ITEM *aParent)
Constructor.
Definition: edit_points.cpp:40
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
static const int POINT_SIZE
Single point size in pixels
Definition: edit_points.h:185
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:61
bool WithinPoint(const VECTOR2I &aPoint, unsigned int aSize) const
Function WithinPoint()
Definition: edit_points.cpp:29
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39