KiCad PCB EDA Suite
shape_line_chain.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  * Copyright (C) 2013 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * Copyright (C) 2013-2020 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef __SHAPE_LINE_CHAIN
27 #define __SHAPE_LINE_CHAIN
28 
29 
30 #include <clipper.hpp>
31 #include <geometry/seg.h>
32 #include <geometry/shape.h>
33 #include <geometry/shape_arc.h>
34 #include <math/vector2d.h>
35 
36 
46 class SHAPE_LINE_CHAIN : public SHAPE
47 {
48 private:
49  typedef std::vector<VECTOR2I>::iterator point_iter;
50  typedef std::vector<VECTOR2I>::const_iterator point_citer;
51 
52 public:
58  struct INTERSECTION
59  {
66  };
67 
68 
76  {
77  public:
78  POINT_INSIDE_TRACKER( const VECTOR2I& aPoint );
79 
80  void AddPolyline( const SHAPE_LINE_CHAIN& aPolyline );
81  bool IsInside();
82 
83  private:
84 
85  bool processVertex ( const VECTOR2I& ip, const VECTOR2I& ipNext );
86 
90  bool m_finished;
91  int m_state;
92  int m_count;
93  };
94 
95  typedef std::vector<INTERSECTION> INTERSECTIONS;
96 
97 
103  {}
104 
110  : SHAPE( SH_LINE_CHAIN ),
111  m_points( aShape.m_points ),
112  m_shapes( aShape.m_shapes ),
113  m_arcs( aShape.m_arcs ),
114  m_closed( aShape.m_closed ),
115  m_width( aShape.m_width ),
116  m_bbox( aShape.m_bbox )
117  {}
118 
119  SHAPE_LINE_CHAIN( const std::vector<int>& aV);
120 
121  SHAPE_LINE_CHAIN( const std::vector<wxPoint>& aV, bool aClosed = false )
122  : SHAPE( SH_LINE_CHAIN ), m_closed( aClosed ), m_width( 0 )
123  {
124  m_points.reserve( aV.size() );
125 
126  for( auto pt : aV )
127  m_points.emplace_back( pt.x, pt.y );
128 
129  m_shapes = std::vector<ssize_t>( aV.size(), ssize_t( SHAPE_IS_PT ) );
130  }
131 
132  SHAPE_LINE_CHAIN( const std::vector<VECTOR2I>& aV, bool aClosed = false )
133  : SHAPE( SH_LINE_CHAIN ), m_closed( aClosed ), m_width( 0 )
134  {
135  m_points = aV;
136  m_shapes = std::vector<ssize_t>( aV.size(), ssize_t( SHAPE_IS_PT ) );
137  }
138 
139  SHAPE_LINE_CHAIN( const SHAPE_ARC& aArc, bool aClosed = false )
140  : SHAPE( SH_LINE_CHAIN ),
141  m_closed( aClosed ),
142  m_width( 0 )
143  {
145  m_arcs.emplace_back( aArc );
146  m_shapes = std::vector<ssize_t>( m_points.size(), 0 );
147  }
148 
149  SHAPE_LINE_CHAIN( const ClipperLib::Path& aPath ) :
150  SHAPE( SH_LINE_CHAIN ),
151  m_closed( true ),
152  m_width( 0 )
153  {
154  m_points.reserve( aPath.size() );
155  m_shapes = std::vector<ssize_t>( aPath.size(), ssize_t( SHAPE_IS_PT ) );
156 
157  for( const auto& point : aPath )
158  m_points.emplace_back( point.X, point.Y );
159  }
160 
162  {}
163 
164  SHAPE_LINE_CHAIN& operator=(const SHAPE_LINE_CHAIN&) = default;
165 
166  SHAPE* Clone() const override;
167 
172  void Clear()
173  {
174  m_points.clear();
175  m_arcs.clear();
176  m_shapes.clear();
177  m_closed = false;
178  }
179 
187  void SetClosed( bool aClosed )
188  {
189  m_closed = aClosed;
190  }
191 
197  bool IsClosed() const
198  {
199  return m_closed;
200  }
201 
206  void SetWidth( int aWidth )
207  {
208  m_width = aWidth;
209  }
210 
215  int Width() const
216  {
217  return m_width;
218  }
219 
226  int SegmentCount() const
227  {
228  int c = m_points.size() - 1;
229  if( m_closed )
230  c++;
231 
232  return std::max( 0, c );
233  }
234 
241  int PointCount() const
242  {
243  return m_points.size();
244  }
245 
254  SEG Segment( int aIndex )
255  {
256  if( aIndex < 0 )
257  aIndex += SegmentCount();
258 
259  if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
260  return SEG( m_points[aIndex], m_points[0], aIndex );
261  else
262  return SEG( m_points[aIndex], m_points[aIndex + 1], aIndex );
263  }
264 
273  const SEG CSegment( int aIndex ) const
274  {
275  if( aIndex < 0 )
276  aIndex += SegmentCount();
277 
278  if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
279  return SEG( const_cast<VECTOR2I&>( m_points[aIndex] ),
280  const_cast<VECTOR2I&>( m_points[0] ), aIndex );
281  else
282  return SEG( const_cast<VECTOR2I&>( m_points[aIndex] ),
283  const_cast<VECTOR2I&>( m_points[aIndex + 1] ), aIndex );
284  }
285 
291  void SetPoint( int aIndex, const VECTOR2I& aPos )
292  {
293  if( aIndex < 0 )
294  aIndex += PointCount();
295  else if( aIndex >= PointCount() )
296  aIndex -= PointCount();
297 
298  m_points[aIndex] = aPos;
299 
300  if( m_shapes[aIndex] != SHAPE_IS_PT )
301  convertArc( m_shapes[aIndex] );
302  }
303 
311  const VECTOR2I& CPoint( int aIndex ) const
312  {
313  if( aIndex < 0 )
314  aIndex += PointCount();
315  else if( aIndex >= PointCount() )
316  aIndex -= PointCount();
317 
318  return m_points[aIndex];
319  }
320 
321  const std::vector<VECTOR2I>& CPoints() const
322  {
323  return m_points;
324  }
325 
329  const VECTOR2I& CLastPoint() const
330  {
331  return m_points[PointCount() - 1];
332  }
333 
337  const std::vector<SHAPE_ARC>& CArcs() const
338  {
339  return m_arcs;
340  }
341 
345  const std::vector<ssize_t>& CShapes() const
346  {
347  return m_shapes;
348  }
349 
351  const BOX2I BBox( int aClearance = 0 ) const override
352  {
353  BOX2I bbox;
354  bbox.Compute( m_points );
355 
356  if( aClearance != 0 || m_width != 0 )
357  bbox.Inflate( aClearance + m_width );
358 
359  return bbox;
360  }
361 
363  {
365 
366  if( m_width != 0 )
368  }
369 
370  const BOX2I BBoxFromCache() const
371  {
372  return m_bbox;
373  }
374 
385  bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr ) const override;
386 
397  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr ) const override;
398 
406  int Distance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
407  SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
408 
415  const SHAPE_LINE_CHAIN Reverse() const;
416 
423  long long int Length() const;
424 
435  void Append( int aX, int aY, bool aAllowDuplication = false )
436  {
437  VECTOR2I v( aX, aY );
438  Append( v, aAllowDuplication );
439  }
440 
450  void Append( const VECTOR2I& aP, bool aAllowDuplication = false )
451  {
452  if( m_points.size() == 0 )
453  m_bbox = BOX2I( aP, VECTOR2I( 0, 0 ) );
454 
455  if( m_points.size() == 0 || aAllowDuplication || CPoint( -1 ) != aP )
456  {
457  m_points.push_back( aP );
458  m_shapes.push_back( ssize_t( SHAPE_IS_PT ) );
459  m_bbox.Merge( aP );
460  }
461  }
462 
469  void Append( const SHAPE_LINE_CHAIN& aOtherLine );
470 
471  void Append( const SHAPE_ARC& aArc );
472 
473  void Insert( size_t aVertex, const VECTOR2I& aP );
474 
475  void Insert( size_t aVertex, const SHAPE_ARC& aArc );
476 
485  void Replace( int aStartIndex, int aEndIndex, const VECTOR2I& aP );
486 
496  void Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE_CHAIN& aLine );
497 
505  void Remove( int aStartIndex, int aEndIndex );
506 
512  void Remove( int aIndex )
513  {
514  Remove( aIndex, aIndex );
515  }
516 
526  int Split( const VECTOR2I& aP );
527 
535  int Find( const VECTOR2I& aP ) const;
536 
544  int FindSegment( const VECTOR2I& aP ) const;
545 
554  const SHAPE_LINE_CHAIN Slice( int aStartIndex, int aEndIndex = -1 ) const;
555 
557  {
558  compareOriginDistance( const VECTOR2I& aOrigin ):
559  m_origin( aOrigin )
560  {}
561 
562  bool operator()( const INTERSECTION& aA, const INTERSECTION& aB )
563  {
564  return ( m_origin - aA.p ).EuclideanNorm() < ( m_origin - aB.p ).EuclideanNorm();
565  }
566 
568  };
569 
570  bool Intersects( const SHAPE_LINE_CHAIN& aChain ) const;
571 
581  int Intersect( const SEG& aSeg, INTERSECTIONS& aIp ) const;
582 
592  int Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& aIp ) const;
593 
601  int PathLength( const VECTOR2I& aP ) const;
602 
613  bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
614 
615 
623  bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
624 
632  int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
633 
642  bool CheckClearance( const VECTOR2I& aP, const int aDist) const;
643 
650  const OPT<INTERSECTION> SelfIntersecting() const;
651 
659 
665  void convertArc( ssize_t aArcIndex );
666 
670  ClipperLib::Path convertToClipper( bool aRequiredOrientation ) const;
671 
678  int NearestSegment( const VECTOR2I& aP ) const;
679 
686  const VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
687 
697  const VECTOR2I NearestPoint( const SEG& aSeg, int& dist ) const;
698 
700  const std::string Format() const override;
701 
703  bool Parse( std::stringstream& aStream ) override;
704 
705  bool operator!=( const SHAPE_LINE_CHAIN& aRhs ) const
706  {
707  if( PointCount() != aRhs.PointCount() )
708  return true;
709 
710  for( int i = 0; i < PointCount(); i++ )
711  {
712  if( CPoint( i ) != aRhs.CPoint( i ) )
713  return true;
714  }
715 
716  return false;
717  }
718 
719  bool CompareGeometry( const SHAPE_LINE_CHAIN& aOther ) const;
720 
721  void Move( const VECTOR2I& aVector ) override
722  {
723  for( auto& pt : m_points )
724  pt += aVector;
725 
726  for( auto& arc : m_arcs )
727  arc.Move( aVector );
728  }
729 
736  void Mirror( bool aX = true, bool aY = false, const VECTOR2I& aRef = { 0, 0 } );
737 
744  void Rotate( double aAngle, const VECTOR2I& aCenter = VECTOR2I( 0, 0 ) ) override;
745 
746  bool IsSolid() const override
747  {
748  return false;
749  }
750 
751  const VECTOR2I PointAlong( int aPathLength ) const;
752 
753  double Area() const;
754 
755  size_t ArcCount() const
756  {
757  return m_arcs.size();
758  }
759 
760  ssize_t ArcIndex( size_t aSegment ) const
761  {
762  if( aSegment >= m_shapes.size() )
763  return SHAPE_IS_PT;
764 
765  return m_shapes[aSegment];
766  }
767 
768  const SHAPE_ARC& Arc( size_t aArc ) const
769  {
770  return m_arcs[aArc];
771  }
772 
773  bool isArc( size_t aSegment ) const
774  {
775  return aSegment < m_shapes.size() && m_shapes[aSegment] != SHAPE_IS_PT;
776  }
777 
778 private:
779 
780  constexpr static ssize_t SHAPE_IS_PT = -1;
781 
783  std::vector<VECTOR2I> m_points;
784 
790  std::vector<ssize_t> m_shapes;
791 
792  std::vector<SHAPE_ARC> m_arcs;
793 
795  bool m_closed;
796 
802  int m_width;
803 
806 };
807 
808 
809 #endif // __SHAPE_LINE_CHAIN
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:128
int Find(const VECTOR2I &aP) const
Function Find()
BOX2I m_bbox
cached bounding box
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
compareOriginDistance(const VECTOR2I &aOrigin)
void Append(const VECTOR2I &aP, bool aAllowDuplication=false)
Function Append()
int FindSegment(const VECTOR2I &aP) const
Function FindSegment()
const SHAPE_ARC & Arc(size_t aArc) const
long long int Length() const
Function Length()
int Split(const VECTOR2I &aP)
Function Split()
int EdgeContainingPoint(const VECTOR2I &aP, int aAccuracy=0) const
Function EdgeContainingPoint()
BOX2< VECTOR2I > BOX2I
Definition: box2.h:521
bool isArc(size_t aSegment) const
bool operator!=(const SHAPE_LINE_CHAIN &aRhs) const
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Function Intersect()
std::vector< INTERSECTION > INTERSECTIONS
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Function PointOnEdge()
void SetPoint(int aIndex, const VECTOR2I &aPos)
Accessor Function to move a point to a specific location.
const SHAPE_LINE_CHAIN Slice(int aStartIndex, int aEndIndex=-1) const
Function Slice()
SHAPE_LINE_CHAIN(const SHAPE_LINE_CHAIN &aShape)
Copy Constructor.
VECTOR2I p
point of intersection between our and their.
void Move(const VECTOR2I &aVector) override
int Width() const
Gets the current width of the segments in the chain.
VECTOR2I::extended_type ecoord
Definition: seg.h:42
SHAPE_LINE_CHAIN(const ClipperLib::Path &aPath)
std::vector< VECTOR2I >::const_iterator point_citer
void Compute(const Container &aPointList)
Compute the bounding box from a given list of points.
Definition: box2.h:90
SHAPE_LINE_CHAIN(const SHAPE_ARC &aArc, bool aClosed=false)
int Distance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Function Distance()
virtual ~SHAPE_LINE_CHAIN()
size_t ArcCount() const
void AddPolyline(const SHAPE_LINE_CHAIN &aPolyline)
const SHAPE_LINE_CHAIN Reverse() const
Function Reverse()
const std::vector< SHAPE_ARC > & CArcs() const
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
void convertArc(ssize_t aArcIndex)
Converts an arc to only a point chain by removing the arc and references.
int PointCount() const
Function PointCount()
const OPT< INTERSECTION > SelfIntersecting() const
Function SelfIntersecting()
SHAPE_LINE_CHAIN(const std::vector< VECTOR2I > &aV, bool aClosed=false)
bool operator()(const INTERSECTION &aA, const INTERSECTION &aB)
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
std::vector< SHAPE_ARC > m_arcs
int PathLength(const VECTOR2I &aP) const
Function PathLength()
ClipperLib::Path convertToClipper(bool aRequiredOrientation) const
Creates a new Clipper path from the SHAPE_LINE_CHAIN in a given orientation.
bool Parse(std::stringstream &aStream) override
void Insert(size_t aVertex, const VECTOR2I &aP)
const std::vector< ssize_t > & CShapes() const
ssize_t ArcIndex(size_t aSegment) const
double dist(const double ax, const double ay, const double bx, const double by)
Definition: delauney.h:168
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
const VECTOR2I & CPoint(int aIndex) const
Function Point()
void SetWidth(int aWidth)
Sets the width of all segments in the chain.
void SetClosed(bool aClosed)
Function SetClosed()
bool m_closed
is the line chain closed?
SHAPE_LINE_CHAIN()
Constructor Initializes an empty line chain.
SEG their
segment belonging from the aOther argument of Intersect()
SHAPE_LINE_CHAIN & operator=(const SHAPE_LINE_CHAIN &)=default
const BOX2I BBox(int aClearance=0) const override
Function BBox()
const std::vector< VECTOR2I > & CPoints() const
std::vector< VECTOR2I > m_points
array of vertices
bool CheckClearance(const VECTOR2I &aP, const int aDist) const
Function CheckClearance()
bool Intersects(const SHAPE_LINE_CHAIN &aChain) const
bool IsSolid() const override
SHAPE.
Definition: shape.h:74
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const
void Remove(int aStartIndex, int aEndIndex)
Function Remove()
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:385
const std::string Format() const override
void Remove(int aIndex)
Function Remove() removes the aIndex-th point from the line chain.
int SegmentCount() const
Function SegmentCount()
SHAPE_LINE_CHAIN(const std::vector< wxPoint > &aV, bool aClosed=false)
void Rotate(double aAngle, const VECTOR2I &aCenter=VECTOR2I(0, 0)) override
Function Rotate rotates all vertices by a given angle.
Definition: seg.h:39
int NearestSegment(const VECTOR2I &aP) const
Find the segment nearest the given point.
bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr) const override
Function Collide()
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:301
SEG Segment(int aIndex)
Function Segment()
std::vector< ssize_t > m_shapes
Array of indices that refer to the index of the shape if the point is part of a larger shape,...
const SEG CSegment(int aIndex) const
Function CSegment()
static constexpr ssize_t SHAPE_IS_PT
SHAPE_LINE_CHAIN.
int m_width
Width of the segments (for BBox calculations in RTree) TODO Adjust usage of SHAPE_LINE_CHAIN to accou...
line segment
Definition: shape.h:43
const VECTOR2I & CLastPoint() const
Returns the last point in the line chain.
boost::optional< T > OPT
Definition: optional.h:7
void Clear()
Function Clear() Removes all points from the line chain.
SHAPE * Clone() const override
Function Clone()
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Function PointInside()
std::vector< VECTOR2I >::iterator point_iter
bool IsClosed() const
Function IsClosed()
void Replace(int aStartIndex, int aEndIndex, const VECTOR2I &aP)
Function Replace()
SEG our
segment belonging from the (this) argument of Intersect()
const SHAPE_LINE_CHAIN ConvertToPolyline(double aAccuracy=500.0) const
Constructs a SHAPE_LINE_CHAIN of segments from a given arc.
Definition: shape_arc.cpp:231
const BOX2I BBoxFromCache() const
const VECTOR2I PointAlong(int aPathLength) const
bool processVertex(const VECTOR2I &ip, const VECTOR2I &ipNext)
void Mirror(bool aX=true, bool aY=false, const VECTOR2I &aRef={ 0, 0 })
Mirrors the line points about y or x (or both)
bool CompareGeometry(const SHAPE_LINE_CHAIN &aOther) const