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 <algorithm> // for max
31 #include <sstream>
32 #include <vector>
33 #include <wx/gdicmn.h> // for wxPoint
34 
35 #include <core/optional.h>
36 
37 #include <clipper.hpp>
38 #include <geometry/seg.h>
39 #include <geometry/shape.h>
40 #include <geometry/shape_arc.h>
41 #include <math/box2.h> // for BOX2I
42 #include <math/vector2d.h>
43 
44 
54 class SHAPE_LINE_CHAIN : public SHAPE
55 {
56 private:
57  typedef std::vector<VECTOR2I>::iterator point_iter;
58  typedef std::vector<VECTOR2I>::const_iterator point_citer;
59 
60 public:
66  struct INTERSECTION
67  {
74  };
75 
76  typedef std::vector<INTERSECTION> INTERSECTIONS;
77 
78 
84  {}
85 
91  : SHAPE( SH_LINE_CHAIN ),
92  m_points( aShape.m_points ),
93  m_shapes( aShape.m_shapes ),
94  m_arcs( aShape.m_arcs ),
95  m_closed( aShape.m_closed ),
96  m_width( aShape.m_width ),
97  m_bbox( aShape.m_bbox )
98  {}
99 
100  SHAPE_LINE_CHAIN( const std::vector<int>& aV);
101 
102  SHAPE_LINE_CHAIN( const std::vector<wxPoint>& aV, bool aClosed = false )
103  : SHAPE( SH_LINE_CHAIN ), m_closed( aClosed ), m_width( 0 )
104  {
105  m_points.reserve( aV.size() );
106 
107  for( auto pt : aV )
108  m_points.emplace_back( pt.x, pt.y );
109 
110  m_shapes = std::vector<ssize_t>( aV.size(), ssize_t( SHAPE_IS_PT ) );
111  }
112 
113  SHAPE_LINE_CHAIN( const std::vector<VECTOR2I>& aV, bool aClosed = false )
114  : SHAPE( SH_LINE_CHAIN ), m_closed( aClosed ), m_width( 0 )
115  {
116  m_points = aV;
117  m_shapes = std::vector<ssize_t>( aV.size(), ssize_t( SHAPE_IS_PT ) );
118  }
119 
120  SHAPE_LINE_CHAIN( const SHAPE_ARC& aArc, bool aClosed = false )
121  : SHAPE( SH_LINE_CHAIN ),
122  m_closed( aClosed ),
123  m_width( 0 )
124  {
126  m_arcs.emplace_back( aArc );
127  m_shapes = std::vector<ssize_t>( m_points.size(), 0 );
128  }
129 
130  SHAPE_LINE_CHAIN( const ClipperLib::Path& aPath ) :
131  SHAPE( SH_LINE_CHAIN ),
132  m_closed( true ),
133  m_width( 0 )
134  {
135  m_points.reserve( aPath.size() );
136  m_shapes = std::vector<ssize_t>( aPath.size(), ssize_t( SHAPE_IS_PT ) );
137 
138  for( const auto& point : aPath )
139  m_points.emplace_back( point.X, point.Y );
140  }
141 
143  {}
144 
145  SHAPE_LINE_CHAIN& operator=(const SHAPE_LINE_CHAIN&) = default;
146 
147  SHAPE* Clone() const override;
148 
153  void Clear()
154  {
155  m_points.clear();
156  m_arcs.clear();
157  m_shapes.clear();
158  m_closed = false;
159  }
160 
168  void SetClosed( bool aClosed )
169  {
170  m_closed = aClosed;
171  }
172 
178  bool IsClosed() const
179  {
180  return m_closed;
181  }
182 
187  void SetWidth( int aWidth )
188  {
189  m_width = aWidth;
190  }
191 
196  int Width() const
197  {
198  return m_width;
199  }
200 
207  int SegmentCount() const
208  {
209  int c = m_points.size() - 1;
210  if( m_closed )
211  c++;
212 
213  return std::max( 0, c );
214  }
215 
222  int PointCount() const
223  {
224  return m_points.size();
225  }
226 
235  SEG Segment( int aIndex )
236  {
237  if( aIndex < 0 )
238  aIndex += SegmentCount();
239 
240  if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
241  return SEG( m_points[aIndex], m_points[0], aIndex );
242  else
243  return SEG( m_points[aIndex], m_points[aIndex + 1], aIndex );
244  }
245 
254  const SEG CSegment( int aIndex ) const
255  {
256  if( aIndex < 0 )
257  aIndex += SegmentCount();
258 
259  if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
260  return SEG( const_cast<VECTOR2I&>( m_points[aIndex] ),
261  const_cast<VECTOR2I&>( m_points[0] ), aIndex );
262  else
263  return SEG( const_cast<VECTOR2I&>( m_points[aIndex] ),
264  const_cast<VECTOR2I&>( m_points[aIndex + 1] ), aIndex );
265  }
266 
272  void SetPoint( int aIndex, const VECTOR2I& aPos )
273  {
274  if( aIndex < 0 )
275  aIndex += PointCount();
276  else if( aIndex >= PointCount() )
277  aIndex -= PointCount();
278 
279  m_points[aIndex] = aPos;
280 
281  if( m_shapes[aIndex] != SHAPE_IS_PT )
282  convertArc( m_shapes[aIndex] );
283  }
284 
292  const VECTOR2I& CPoint( int aIndex ) const
293  {
294  if( aIndex < 0 )
295  aIndex += PointCount();
296  else if( aIndex >= PointCount() )
297  aIndex -= PointCount();
298 
299  return m_points[aIndex];
300  }
301 
302  const std::vector<VECTOR2I>& CPoints() const
303  {
304  return m_points;
305  }
306 
310  const VECTOR2I& CLastPoint() const
311  {
312  return m_points[PointCount() - 1];
313  }
314 
318  const std::vector<SHAPE_ARC>& CArcs() const
319  {
320  return m_arcs;
321  }
322 
326  const std::vector<ssize_t>& CShapes() const
327  {
328  return m_shapes;
329  }
330 
332  const BOX2I BBox( int aClearance = 0 ) const override
333  {
334  BOX2I bbox;
335  bbox.Compute( m_points );
336 
337  if( aClearance != 0 || m_width != 0 )
338  bbox.Inflate( aClearance + m_width );
339 
340  return bbox;
341  }
342 
344  {
346 
347  if( m_width != 0 )
349  }
350 
359  bool Collide( const VECTOR2I& aP, int aClearance = 0 ) const override;
360 
369  bool Collide( const SEG& aSeg, int aClearance = 0 ) const override;
370 
378  int Distance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
379 
386  const SHAPE_LINE_CHAIN Reverse() const;
387 
394  long long int Length() const;
395 
406  void Append( int aX, int aY, bool aAllowDuplication = false )
407  {
408  VECTOR2I v( aX, aY );
409  Append( v, aAllowDuplication );
410  }
411 
421  void Append( const VECTOR2I& aP, bool aAllowDuplication = false )
422  {
423  if( m_points.size() == 0 )
424  m_bbox = BOX2I( aP, VECTOR2I( 0, 0 ) );
425 
426  if( m_points.size() == 0 || aAllowDuplication || CPoint( -1 ) != aP )
427  {
428  m_points.push_back( aP );
429  m_shapes.push_back( ssize_t( SHAPE_IS_PT ) );
430  m_bbox.Merge( aP );
431  }
432  }
433 
440  void Append( const SHAPE_LINE_CHAIN& aOtherLine );
441 
442  void Append( const SHAPE_ARC& aArc );
443 
444  void Insert( size_t aVertex, const VECTOR2I& aP );
445 
446  void Insert( size_t aVertex, const SHAPE_ARC& aArc );
447 
457  void Replace( int aStartIndex, int aEndIndex, const VECTOR2I& aP );
458 
468  void Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE_CHAIN& aLine );
469 
477  void Remove( int aStartIndex, int aEndIndex );
478 
484  void Remove( int aIndex )
485  {
486  Remove( aIndex, aIndex );
487  }
488 
498  int Split( const VECTOR2I& aP );
499 
507  int Find( const VECTOR2I& aP ) const;
508 
516  int FindSegment( const VECTOR2I& aP ) const;
517 
526  const SHAPE_LINE_CHAIN Slice( int aStartIndex, int aEndIndex = -1 ) const;
527 
529  {
530  compareOriginDistance( const VECTOR2I& aOrigin ):
531  m_origin( aOrigin )
532  {}
533 
534  bool operator()( const INTERSECTION& aA, const INTERSECTION& aB )
535  {
536  return ( m_origin - aA.p ).EuclideanNorm() < ( m_origin - aB.p ).EuclideanNorm();
537  }
538 
540  };
541 
542  bool Intersects( const SHAPE_LINE_CHAIN& aChain ) const;
543 
553  int Intersect( const SEG& aSeg, INTERSECTIONS& aIp ) const;
554 
564  int Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& aIp ) const;
565 
574  int PathLength( const VECTOR2I& aP ) const;
575 
586  bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
587 
588 
596  bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
597 
605  int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
606 
615  bool CheckClearance( const VECTOR2I& aP, const int aDist) const;
616 
623  const OPT<INTERSECTION> SelfIntersecting() const;
624 
632 
638  void convertArc( ssize_t aArcIndex );
639 
644  ClipperLib::Path convertToClipper( bool aRequiredOrientation ) const;
645 
652  int NearestSegment( const VECTOR2I& aP ) const;
653 
660  const VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
661 
671  const VECTOR2I NearestPoint( const SEG& aSeg, int& dist ) const;
672 
674  const std::string Format() const override;
675 
677  bool Parse( std::stringstream& aStream ) override;
678 
679  bool operator!=( const SHAPE_LINE_CHAIN& aRhs ) const
680  {
681  if( PointCount() != aRhs.PointCount() )
682  return true;
683 
684  for( int i = 0; i < PointCount(); i++ )
685  {
686  if( CPoint( i ) != aRhs.CPoint( i ) )
687  return true;
688  }
689 
690  return false;
691  }
692 
693  bool CompareGeometry( const SHAPE_LINE_CHAIN& aOther ) const;
694 
695  void Move( const VECTOR2I& aVector ) override
696  {
697  for( auto& pt : m_points )
698  pt += aVector;
699 
700  for( auto& arc : m_arcs )
701  arc.Move( aVector );
702  }
703 
710  void Mirror( bool aX = true, bool aY = false, const VECTOR2I& aRef = { 0, 0 } );
711 
718  void Rotate( double aAngle, const VECTOR2I& aCenter = VECTOR2I( 0, 0 ) );
719 
720  bool IsSolid() const override
721  {
722  return false;
723  }
724 
725  const VECTOR2I PointAlong( int aPathLength ) const;
726 
727  double Area() const;
728 
729  size_t ArcCount() const
730  {
731  return m_arcs.size();
732  }
733 
734  ssize_t ArcIndex( size_t aSegment ) const
735  {
736  if( aSegment >= m_shapes.size() )
737  return SHAPE_IS_PT;
738 
739  return m_shapes[aSegment];
740  }
741 
742  const SHAPE_ARC& Arc( size_t aArc ) const
743  {
744  return m_arcs[aArc];
745  }
746 
747  bool isArc( size_t aSegment ) const
748  {
749  return aSegment < m_shapes.size() && m_shapes[aSegment] != SHAPE_IS_PT;
750  }
751 
752 private:
753 
754  constexpr static ssize_t SHAPE_IS_PT = -1;
755 
757  std::vector<VECTOR2I> m_points;
758 
764  std::vector<ssize_t> m_shapes;
765 
766  std::vector<SHAPE_ARC> m_arcs;
767 
769  bool m_closed;
770 
775  int m_width;
776 
779 };
780 
781 
782 #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
static const int dist[10][10]
Definition: ar_matrix.cpp:326
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.
void Rotate(double aAngle, const VECTOR2I &aCenter=VECTOR2I(0, 0))
Function Rotate rotates all vertices by a given angle.
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: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
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)
bool Collide(const VECTOR2I &aP, int aClearance=0) const override
Function Collide()
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()
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:60
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()
double Area() const
SHAPE_LINE_CHAIN(const std::vector< wxPoint > &aV, bool aClosed=false)
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: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:47
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:274
const VECTOR2I PointAlong(int aPathLength) const
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