KiCad PCB EDA Suite
PolyLine.h
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  * Few parts of this code come from FreePCB, released under the GNU General Public License V2.
5  * (see http://www.freepcb.com/ )
6  *
7  * Copyright (C) 2012-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
8  * Copyright (C) 2008-2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
9  * Copyright (C) 2008-2013 Wayne Stambaugh <stambaughw@verizon.net>
10  * Copyright (C) 2012-2015 KiCad Developers, see CHANGELOG.TXT for contributors.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 
30 
36 //
37 // A polyline contains one or more contours, where each contour
38 // is defined by a list of corners and side-styles
39 // There may be multiple contours in a polyline.
40 // The last contour may be open or closed, any others must be closed.
41 // All of the corners and side-styles are concatenated into 2 arrays,
42 // separated by setting the end_contour flag of the last corner of
43 // each contour.
44 //
45 // When used for copper (or technical layers) areas, the first contour is the outer edge
46 // of the area, subsequent ones are "holes" in the copper.
47 
48 #ifndef POLYLINE_H
49 #define POLYLINE_H
50 
51 #include <vector>
52 
53 #include <wx/gdicmn.h> // for wxPoint definition
54 #include <layers_id_colors_and_visibility.h> // for LAYER_NUM definition
55 #include <class_eda_rect.h> // for EDA_RECT definition
56 
57 #include <geometry/shape_poly_set.h> // fixme
58 
59 class CSegment
60 {
61 public:
64 
65  CSegment() { };
66  CSegment( const wxPoint& aStart, const wxPoint& aEnd )
67  {
68  m_Start = aStart;
69  m_End = aEnd;
70  }
71 
72  CSegment( int x0, int y0, int x1, int y1 )
73  {
74  m_Start.x = x0; m_Start.y = y0;
75  m_End.x = x1; m_End.y = y1;
76  }
77 };
78 
79 class CPolyPt : public wxPoint
80 {
81 public:
82  CPolyPt( int aX = 0, int aY = 0, bool aEnd = false, int aUtility = 0 ) :
83  wxPoint( aX, aY ), end_contour( aEnd ), m_flags( aUtility )
84  {}
85 
86  // / Pure copy constructor is here to dis-ambiguate from the
87  // / specialized CPolyPt( const wxPoint& ) constructor version below.
88  CPolyPt( const CPolyPt& aPt ) :
89  wxPoint( aPt.x, aPt.y ), end_contour( aPt.end_contour ), m_flags( aPt.m_flags )
90  {}
91 
92  CPolyPt( const wxPoint& aPoint ) :
93  wxPoint( aPoint ), end_contour( false ), m_flags( 0 )
94  {}
95 
96 
98  int m_flags;
99 
100  bool operator ==( const CPolyPt& cpt2 ) const
101  { return (x == cpt2.x) && (y == cpt2.y) && (end_contour == cpt2.end_contour); }
102 
103  bool operator !=( CPolyPt& cpt2 ) const
104  { return (x != cpt2.x) || (y != cpt2.y) || (end_contour != cpt2.end_contour); }
105 };
106 
107 
108 #ifndef SWIG
109 
115 {
116 private:
117  std::vector <CPolyPt> m_cornersList; // array of points for corners
118 public:
120 
121  CPolyPt& operator [](int aIdx) { return m_cornersList[aIdx]; }
122 
123  // Accessor:
124  const std::vector <CPolyPt>& GetList() const {return m_cornersList;}
125 
126  int GetX( int ic ) const { return m_cornersList[ic].x; }
127  void SetX( int ic, int aValue ) { m_cornersList[ic].x = aValue; }
128  int GetY( int ic ) const { return m_cornersList[ic].y; }
129  void SetY( int ic, int aValue ) { m_cornersList[ic].y = aValue; }
130 
131  bool IsEndContour( int ic ) const
132  {
133  return m_cornersList[ic].end_contour;
134  }
135 
136  const wxPoint& GetPos( int ic ) const { return m_cornersList[ic]; }
137  const CPolyPt& GetCorner( int ic ) const { return m_cornersList[ic]; }
138 
139  // vector <> methods
140  void reserve( int aSize ) { m_cornersList.reserve( aSize ); }
141 
142 
143  void RemoveAllContours( void ) { m_cornersList.clear(); }
144  CPolyPt& GetLastCorner() { return m_cornersList.back(); }
145 
146  unsigned GetCornersCount() const { return m_cornersList.size(); }
147 
148  void DeleteCorner( int aIdx )
149  {
150  m_cornersList.erase( m_cornersList.begin() + aIdx );
151  }
152 
153  // used only to erase an entire polygon
154  void DeleteCorners( int aIdFirstCorner, int aIdLastCorner )
155  {
156  m_cornersList.erase( m_cornersList.begin() + aIdFirstCorner,
157  m_cornersList.begin() + aIdLastCorner + 1 );
158  }
159 
160  void Append( const CPOLYGONS_LIST& aList )
161  {
162  m_cornersList.insert( m_cornersList.end(),
163  aList.m_cornersList.begin(),
164  aList.m_cornersList.end() );
165  }
166 
167  void Append( const CPolyPt& aItem )
168  {
169  m_cornersList.push_back( aItem );
170  }
171 
172  void Append( const wxPoint& aItem )
173  {
174  CPolyPt item( aItem );
175 
176  m_cornersList.push_back( aItem );
177  }
178 
179  void InsertCorner( int aPosition, const CPolyPt& aItem )
180  {
181  m_cornersList.insert( m_cornersList.begin() + aPosition + 1, aItem );
182  }
183 
188  void AddCorner( const CPolyPt& aCorner )
189  {
190  m_cornersList.push_back( aCorner );
191  }
192 
198  {
199  if( m_cornersList.size() > 0 )
200  m_cornersList.back().end_contour = true;
201  }
202 
208  int GetContoursCount() const;
209 };
210 #endif
211 
212 
214 {
215 public:
216  enum HATCH_STYLE { NO_HATCH, DIAGONAL_FULL, DIAGONAL_EDGE }; // hatch styles
217 
218  // constructors/destructor
219  CPolyLine();
220  CPolyLine( const CPolyLine& aCPolyLine);
221  ~CPolyLine();
222 
228  void ImportSettings( const CPolyLine* aPoly );
229 
230  // functions for modifying the CPolyLine contours
231 
232  /* initialize a contour
233  * set layer, hatch style, and starting point
234  */
235  void Start( LAYER_NUM layer, int x, int y, int hatch );
236 
237  void AppendCorner( int x, int y );
238  void InsertCorner( int ic, int x, int y );
239 
246  void DeleteCorner( int ic );
247  void MoveCorner( int ic, int x, int y );
248 
255  {
257  }
258 
259  void RemoveContour( int icont );
260 
272 
279  CPolyLine* Chamfer( unsigned int aDistance );
280 
288  CPolyLine* Fillet( unsigned int aRadius, unsigned int aSegments );
289 
296  int RemoveNullSegments();
297 
298  void RemoveAllContours( void );
299 
300  // Remove or create hatch
301  void UnHatch();
302  void Hatch();
303 
304  // Transform functions
305  void MoveOrigin( int x_off, int y_off );
306 
307  // misc. functions
311  const EDA_RECT GetBoundingBox();
312 
318  const EDA_RECT GetBoundingBox( int icont );
319 
320  void Copy( const CPolyLine* src );
321  bool TestPointInside( int x, int y );
322 
327  bool IsCutoutContour( int aCornerIdx );
328 
333  void AppendArc( int xi, int yi, int xf, int yf, int xc, int yc, int num );
334 
335  // access functions
336  void SetLayer( LAYER_NUM aLayer ) { m_layer = aLayer; }
337  LAYER_NUM GetLayer() const { return m_layer; }
338 
339  int GetCornersCount() const
340  {
342  }
343 
347  bool GetClosed();
348 
354  int GetContoursCount() const;
355 
361  int GetContour( int ic );
362 
368  int GetContourStart( int icont );
369 
375  int GetContourEnd( int icont );
376 
382  int GetContourSize( int icont );
383 
384  int GetX( int ic ) const { return m_CornersList.GetX( ic ); }
385  int GetY( int ic ) const { return m_CornersList.GetY( ic ); }
386 
392  bool IsEndContour( int ic ) const { return m_CornersList.IsEndContour( ic ); }
393 
394  const wxPoint& GetPos( int ic ) const { return m_CornersList.GetPos( ic ); }
395 
396  int GetHatchPitch() const { return m_hatchPitch; }
397  static int GetDefaultHatchPitchMils() { return 20; } // default hatch pitch value in mils
398 
399  enum HATCH_STYLE GetHatchStyle() const { return m_hatchStyle; }
400  void SetHatch( int aHatchStyle, int aHatchPitch, bool aRebuildHatch )
401  {
402  SetHatchPitch( aHatchPitch );
403  m_hatchStyle = (enum HATCH_STYLE) aHatchStyle;
404 
405  if( aRebuildHatch )
406  Hatch();
407  }
408 
409  void SetX( int ic, int x )
410  {
411  m_CornersList.SetX( ic, x );
412  }
413 
414  void SetY( int ic, int y )
415  {
416  m_CornersList.SetY( ic, y );
417  }
418 
419  void SetHatchStyle( enum HATCH_STYLE style )
420  {
421  m_hatchStyle = style;
422  }
423 
424  void SetHatchPitch( int pitch ) { m_hatchPitch = pitch; }
425 
435  int NormalizeAreaOutlines( std::vector<CPolyLine*>* aNewPolygonList );
436 
437  // Bezier Support
438  void AppendBezier( int x1, int y1, int x2, int y2, int x3, int y3 );
439  void AppendBezier( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 );
440 
448  int Distance( const wxPoint& aPoint );
449 
459  int Distance( wxPoint aStart, wxPoint aEnd, int aWidth );
460 
468  int HitTestForEdge( const wxPoint& aPos, int aDistMax ) const;
469 
477  int HitTestForCorner( const wxPoint& aPos, int aDistMax ) const;
478 
479 private:
480  LAYER_NUM m_layer; // layer to draw on
481  enum HATCH_STYLE m_hatchStyle; // hatch style, see enum above
482  int m_hatchPitch; // for DIAGONAL_EDGE hatched outlines, basic distance between 2 hatch lines
483  // and the len of eacvh segment
484  // for DIAGONAL_FULL, the pitch is twice this value
485  int m_flags; // a flag used in some calculations
486 public:
487  CPOLYGONS_LIST m_CornersList; // array of points for corners
488  std::vector <CSegment> m_HatchLines; // hatch lines showing the polygon area
489 };
490 
493 
494 #endif // #ifndef POLYLINE_H
bool operator!=(CPolyPt &cpt2) const
Definition: PolyLine.h:103
int m_flags
Definition: PolyLine.h:98
void SetHatchPitch(int pitch)
Definition: PolyLine.h:424
bool IsEndContour(int ic) const
Definition: PolyLine.h:131
void AppendArc(int xi, int yi, int xf, int yf, int xc, int yc, int num)
Function AppendArc.
Definition: PolyLine.cpp:987
~CPolyLine()
Definition: PolyLine.cpp:68
CSegment(const wxPoint &aStart, const wxPoint &aEnd)
Definition: PolyLine.h:66
const wxPoint & GetPos(int ic) const
Definition: PolyLine.h:394
const EDA_RECT GetBoundingBox()
Definition: PolyLine.cpp:565
void Copy(const CPolyLine *src)
Definition: PolyLine.cpp:943
void UnHatch()
Definition: PolyLine.cpp:559
int GetY(int ic) const
Definition: PolyLine.h:385
void Start(LAYER_NUM layer, int x, int y, int hatch)
Definition: PolyLine.cpp:186
bool operator==(const CPolyPt &cpt2) const
Definition: PolyLine.h:100
void reserve(int aSize)
Definition: PolyLine.h:140
enum HATCH_STYLE GetHatchStyle() const
Definition: PolyLine.h:399
void SetX(int ic, int x)
Definition: PolyLine.h:409
const SHAPE_POLY_SET ConvertPolyListToPolySet(const CPOLYGONS_LIST &aList)
Definition: PolyLine.cpp:1316
const CPolyPt & GetCorner(int ic) const
Definition: PolyLine.h:137
void AppendCorner(int x, int y)
Definition: PolyLine.cpp:199
void AppendBezier(int x1, int y1, int x2, int y2, int x3, int y3)
Definition: PolyLine.cpp:1011
void Hatch()
Definition: PolyLine.cpp:728
int GetContourEnd(int icont)
Function GetContourEnd.
Definition: PolyLine.cpp:678
void RemoveContour(int icont)
Function RemoveContour.
Definition: PolyLine.cpp:252
void RemoveAllContours(void)
function RemoveAllContours removes all corners from the list.
Definition: PolyLine.cpp:513
void DeleteCorners(int aIdFirstCorner, int aIdLastCorner)
Definition: PolyLine.h:154
CPOLYGONS_LIST m_CornersList
Definition: PolyLine.h:487
unsigned GetCornersCount() const
Definition: PolyLine.h:146
void SetHatch(int aHatchStyle, int aHatchPitch, bool aRebuildHatch)
Definition: PolyLine.h:400
CPolyLine * Fillet(unsigned int aRadius, unsigned int aSegments)
Function Fillet returns a filleted version of a polygon.
Definition: PolyLine.cpp:370
CSegment()
Definition: PolyLine.h:65
LAYER_NUM m_layer
Definition: PolyLine.h:480
int Distance(const wxPoint &aPoint)
Function Distance Calculates the distance between a point and the zone:
Definition: PolyLine.cpp:1104
int HitTestForCorner(const wxPoint &aPos, int aDistMax) const
Function HitTestForCorner test is the point aPos is near (< aDistMax ) a corner.
Definition: PolyLine.cpp:1197
void Append(const wxPoint &aItem)
Definition: PolyLine.h:172
int RemoveNullSegments()
Function RemoveNullSegments Removes corners which create a null segment edge (i.e.
Definition: PolyLine.cpp:77
CPolyPt & operator[](int aIdx)
Definition: PolyLine.h:121
CSegment(int x0, int y0, int x1, int y1)
Definition: PolyLine.h:72
void Append(const CPolyPt &aItem)
Definition: PolyLine.h:167
std::vector< CSegment > m_HatchLines
Definition: PolyLine.h:488
CPolyPt(int aX=0, int aY=0, bool aEnd=false, int aUtility=0)
Definition: PolyLine.h:82
int GetContourSize(int icont)
Function GetContourSize.
Definition: PolyLine.cpp:704
CPolyLine * Chamfer(unsigned int aDistance)
Function Chamfer returns a chamfered version of a polygon.
Definition: PolyLine.cpp:285
enum HATCH_STYLE m_hatchStyle
Definition: PolyLine.h:481
CPolyPt & GetLastCorner()
Definition: PolyLine.h:144
Class SHAPE_POLY_SET.
void MoveOrigin(int x_off, int y_off)
Definition: PolyLine.cpp:970
int GetContoursCount() const
Function GetContoursCount.
Definition: PolyLine.cpp:620
CPolyPt(const CPolyPt &aPt)
Definition: PolyLine.h:88
void SetY(int ic, int y)
Definition: PolyLine.h:414
int GetX(int ic) const
Definition: PolyLine.h:384
wxPoint m_Start
Definition: PolyLine.h:62
const std::vector< CPolyPt > & GetList() const
Definition: PolyLine.h:124
const wxPoint & GetPos(int ic) const
Definition: PolyLine.h:136
bool IsCutoutContour(int aCornerIdx)
Definition: PolyLine.cpp:959
int GetHatchPitch() const
Definition: PolyLine.h:396
int GetContoursCount() const
Function GetContoursCount.
Definition: PolyLine.cpp:613
void DeleteCorner(int aIdx)
Definition: PolyLine.h:148
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
LAYER_NUM GetLayer() const
Definition: PolyLine.h:337
int HitTestForEdge(const wxPoint &aPos, int aDistMax) const
Function HitTestForEdge test is the point aPos is near (< aDistMax ) a vertex.
Definition: PolyLine.cpp:1155
int m_flags
Definition: PolyLine.h:485
void SetHatchStyle(enum HATCH_STYLE style)
Definition: PolyLine.h:419
Board layer functions and definitions.
int GetCornersCount() const
Definition: PolyLine.h:339
void CloseLastContour()
function CloseLastContour Set the .end_contour member of the last corner of the last contour to true ...
Definition: PolyLine.h:254
bool IsEndContour(int ic) const
Function IsEndContour.
Definition: PolyLine.h:392
bool GetClosed()
Definition: PolyLine.cpp:710
wxPoint m_End
Definition: PolyLine.h:63
static int GetDefaultHatchPitchMils()
Definition: PolyLine.h:397
void RemoveAllContours(void)
Definition: PolyLine.h:143
void DeleteCorner(int ic)
Function DeleteCorner remove the given corner.
Definition: PolyLine.cpp:222
Class EDA_RECT handles the component boundary box.
void AddCorner(const CPolyPt &aCorner)
function AddCorner add a corner to the list
Definition: PolyLine.h:188
bool end_contour
Definition: PolyLine.h:97
void Append(const CPOLYGONS_LIST &aList)
Definition: PolyLine.h:160
void SetY(int ic, int aValue)
Definition: PolyLine.h:129
void SetX(int ic, int aValue)
Definition: PolyLine.h:127
int GetContourStart(int icont)
Function GetContourStart.
Definition: PolyLine.cpp:655
void CloseLastContour()
function CloseLastContour Set the .end_contour member of the last corner in list to true ...
Definition: PolyLine.h:197
int GetY(int ic) const
Definition: PolyLine.h:128
int GetX(int ic) const
Definition: PolyLine.h:126
const CPOLYGONS_LIST ConvertPolySetToPolyList(const SHAPE_POLY_SET &aPolyset)
Definition: PolyLine.cpp:1362
bool IsPolygonSelfIntersecting()
Function IsPolygonSelfIntersecting Test a CPolyLine for self-intersection of vertex (all contours)...
Definition: PolyLine.cpp:1232
int GetContour(int ic)
Function GetContour.
Definition: PolyLine.cpp:641
void MoveCorner(int ic, int x, int y)
Definition: PolyLine.cpp:211
CPolyPt(const wxPoint &aPoint)
Definition: PolyLine.h:92
CPOLYGONS_LIST handle a list of contours (polygons corners).
Definition: PolyLine.h:114
void InsertCorner(int aPosition, const CPolyPt &aItem)
Definition: PolyLine.h:179
int NormalizeAreaOutlines(std::vector< CPolyLine * > *aNewPolygonList)
Function NormalizeAreaOutlines Convert a self-intersecting polygon to one (or more) non self-intersec...
Definition: PolyLine.cpp:117
bool TestPointInside(int x, int y)
Definition: PolyLine.cpp:913
std::vector< CPolyPt > m_cornersList
Definition: PolyLine.h:117
void InsertCorner(int ic, int x, int y)
Function InsertCorner insert a new corner between two existing corners.
Definition: PolyLine.cpp:532
int m_hatchPitch
Definition: PolyLine.h:482
void SetLayer(LAYER_NUM aLayer)
Definition: PolyLine.h:336
void ImportSettings(const CPolyLine *aPoly)
Function ImportSettings Copy settings (layer, hatch styles) from aPoly.
Definition: PolyLine.cpp:176