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 
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 
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_LINE_CHAIN_BASE( 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_LINE_CHAIN_BASE( 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 )
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 ) :
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 override
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 
382  int Distance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
383 
390  const SHAPE_LINE_CHAIN Reverse() const;
391 
398  long long int Length() const;
399 
410  void Append( int aX, int aY, bool aAllowDuplication = false )
411  {
412  VECTOR2I v( aX, aY );
413  Append( v, aAllowDuplication );
414  }
415 
425  void Append( const VECTOR2I& aP, bool aAllowDuplication = false )
426  {
427  if( m_points.size() == 0 )
428  m_bbox = BOX2I( aP, VECTOR2I( 0, 0 ) );
429 
430  if( m_points.size() == 0 || aAllowDuplication || CPoint( -1 ) != aP )
431  {
432  m_points.push_back( aP );
433  m_shapes.push_back( ssize_t( SHAPE_IS_PT ) );
434  m_bbox.Merge( aP );
435  }
436  }
437 
444  void Append( const SHAPE_LINE_CHAIN& aOtherLine );
445 
446  void Append( const SHAPE_ARC& aArc );
447 
448  void Insert( size_t aVertex, const VECTOR2I& aP );
449 
450  void Insert( size_t aVertex, const SHAPE_ARC& aArc );
451 
460  void Replace( int aStartIndex, int aEndIndex, const VECTOR2I& aP );
461 
471  void Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE_CHAIN& aLine );
472 
480  void Remove( int aStartIndex, int aEndIndex );
481 
487  void Remove( int aIndex )
488  {
489  Remove( aIndex, aIndex );
490  }
491 
501  int Split( const VECTOR2I& aP );
502 
510  int Find( const VECTOR2I& aP ) const;
511 
519  int FindSegment( const VECTOR2I& aP ) const;
520 
529  const SHAPE_LINE_CHAIN Slice( int aStartIndex, int aEndIndex = -1 ) const;
530 
532  {
533  compareOriginDistance( const VECTOR2I& aOrigin ):
534  m_origin( aOrigin )
535  {}
536 
537  bool operator()( const INTERSECTION& aA, const INTERSECTION& aB )
538  {
539  return ( m_origin - aA.p ).EuclideanNorm() < ( m_origin - aB.p ).EuclideanNorm();
540  }
541 
543  };
544 
545  bool Intersects( const SHAPE_LINE_CHAIN& aChain ) const;
546 
556  int Intersect( const SEG& aSeg, INTERSECTIONS& aIp ) const;
557 
567  int Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& aIp ) const;
568 
576  int PathLength( const VECTOR2I& aP ) const;
577 
586  bool CheckClearance( const VECTOR2I& aP, const int aDist) const;
587 
594  const OPT<INTERSECTION> SelfIntersecting() const;
595 
603 
609  void convertArc( ssize_t aArcIndex );
610 
614  ClipperLib::Path convertToClipper( bool aRequiredOrientation ) const;
615 
622  int NearestSegment( const VECTOR2I& aP ) const;
623 
630  const VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
631 
641  const VECTOR2I NearestPoint( const SEG& aSeg, int& dist ) const;
642 
644  const std::string Format() const override;
645 
647  bool Parse( std::stringstream& aStream ) override;
648 
649  bool operator!=( const SHAPE_LINE_CHAIN& aRhs ) const
650  {
651  if( PointCount() != aRhs.PointCount() )
652  return true;
653 
654  for( int i = 0; i < PointCount(); i++ )
655  {
656  if( CPoint( i ) != aRhs.CPoint( i ) )
657  return true;
658  }
659 
660  return false;
661  }
662 
663  bool CompareGeometry( const SHAPE_LINE_CHAIN& aOther ) const;
664 
665  void Move( const VECTOR2I& aVector ) override
666  {
667  for( auto& pt : m_points )
668  pt += aVector;
669 
670  for( auto& arc : m_arcs )
671  arc.Move( aVector );
672  }
673 
680  void Mirror( bool aX = true, bool aY = false, const VECTOR2I& aRef = { 0, 0 } );
681 
688  void Rotate( double aAngle, const VECTOR2I& aCenter = VECTOR2I( 0, 0 ) ) override;
689 
690  bool IsSolid() const override
691  {
692  return false;
693  }
694 
695  const VECTOR2I PointAlong( int aPathLength ) const;
696 
697  double Area() const;
698 
699  size_t ArcCount() const
700  {
701  return m_arcs.size();
702  }
703 
704  ssize_t ArcIndex( size_t aSegment ) const
705  {
706  if( aSegment >= m_shapes.size() )
707  return SHAPE_IS_PT;
708 
709  return m_shapes[aSegment];
710  }
711 
712  const SHAPE_ARC& Arc( size_t aArc ) const
713  {
714  return m_arcs[aArc];
715  }
716 
717  bool isArc( size_t aSegment ) const
718  {
719  return aSegment < m_shapes.size() && m_shapes[aSegment] != SHAPE_IS_PT;
720  }
721 
722  virtual const VECTOR2I GetPoint( int aIndex ) const override { return CPoint(aIndex); }
723  virtual const SEG GetSegment( int aIndex ) const override { return CSegment(aIndex); }
724  virtual size_t GetPointCount() const override { return PointCount(); }
725  virtual size_t GetSegmentCount() const override { return SegmentCount(); }
726 
727 private:
728 
729  constexpr static ssize_t SHAPE_IS_PT = -1;
730 
732  std::vector<VECTOR2I> m_points;
733 
739  std::vector<ssize_t> m_shapes;
740 
741  std::vector<SHAPE_ARC> m_arcs;
742 
744  bool m_closed;
745 
751  int m_width;
752 
755 };
756 
757 
758 #endif // __SHAPE_LINE_CHAIN
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:133
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()
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
bool isArc(size_t aSegment) const
bool operator!=(const SHAPE_LINE_CHAIN &aRhs) const
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Function Intersect()
virtual size_t GetSegmentCount() const override
std::vector< INTERSECTION > INTERSECTIONS
virtual size_t GetPointCount() const override
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.
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:91
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
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()
bool IsClosed() const override
Function IsClosed()
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()
virtual const SEG GetSegment(int aIndex) const override
bool Intersects(const SHAPE_LINE_CHAIN &aChain) const
bool IsSolid() const override
SHAPE.
Definition: shape.h:120
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:386
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()
virtual const VECTOR2I GetPoint(int aIndex) const override
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.
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:302
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:44
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()
std::vector< VECTOR2I >::iterator point_iter
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