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-2019
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 #include <vector>
30 #include <sstream>
31 
32 #include <core/optional.h>
33 
34 #include <math/vector2d.h>
35 #include <geometry/shape.h>
36 #include <geometry/seg.h>
37 
38 #include <clipper.hpp>
39 
49 class SHAPE_LINE_CHAIN : public SHAPE
50 {
51 private:
52  typedef std::vector<VECTOR2I>::iterator point_iter;
53  typedef std::vector<VECTOR2I>::const_iterator point_citer;
54 
55 public:
61  struct INTERSECTION
62  {
69  };
70 
71  typedef std::vector<INTERSECTION> INTERSECTIONS;
72 
78  SHAPE( SH_LINE_CHAIN ), m_closed( false )
79  {}
80 
85  SHAPE( SH_LINE_CHAIN ), m_points( aShape.m_points ), m_closed( aShape.m_closed )
86  {}
87 
92  SHAPE_LINE_CHAIN( const VECTOR2I& aA, const VECTOR2I& aB ) :
93  SHAPE( SH_LINE_CHAIN ), m_closed( false )
94  {
95  m_points.resize( 2 );
96  m_points[0] = aA;
97  m_points[1] = aB;
98  }
99 
100  SHAPE_LINE_CHAIN( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) :
101  SHAPE( SH_LINE_CHAIN ), m_closed( false )
102  {
103  m_points.resize( 3 );
104  m_points[0] = aA;
105  m_points[1] = aB;
106  m_points[2] = aC;
107  }
108 
109  SHAPE_LINE_CHAIN( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC, const VECTOR2I& aD ) :
110  SHAPE( SH_LINE_CHAIN ), m_closed( false )
111  {
112  m_points.resize( 4 );
113  m_points[0] = aA;
114  m_points[1] = aB;
115  m_points[2] = aC;
116  m_points[3] = aD;
117  }
118 
119 
120  SHAPE_LINE_CHAIN( const VECTOR2I* aV, int aCount ) :
121  SHAPE( SH_LINE_CHAIN ),
122  m_closed( false )
123  {
124  m_points.resize( aCount );
125 
126  for( int i = 0; i < aCount; i++ )
127  m_points[i] = *aV++;
128  }
129 
130 
131  SHAPE_LINE_CHAIN( const std::vector<wxPoint>& aV ) :
132  SHAPE( SH_LINE_CHAIN ),
133  m_closed( false )
134  {
135  m_points.reserve( aV.size() );
136 
137  for( auto pt : aV )
138  m_points.emplace_back( pt.x, pt.y );
139  }
140 
141  SHAPE_LINE_CHAIN( const ClipperLib::Path& aPath ) :
142  SHAPE( SH_LINE_CHAIN ),
143  m_closed( true )
144  {
145  m_points.reserve( aPath.size() );
146 
147  for( const auto& point : aPath )
148  m_points.emplace_back( point.X, point.Y );
149  }
150 
152  {}
153 
154  SHAPE* Clone() const override;
155 
160  void Clear()
161  {
162  m_points.clear();
163  m_closed = false;
164  }
165 
173  void SetClosed( bool aClosed )
174  {
175  m_closed = aClosed;
176  }
177 
183  bool IsClosed() const
184  {
185  return m_closed;
186  }
187 
194  int SegmentCount() const
195  {
196  int c = m_points.size() - 1;
197  if( m_closed )
198  c++;
199 
200  return std::max( 0, c );
201  }
202 
209  int PointCount() const
210  {
211  return m_points.size();
212  }
213 
222  SEG Segment( int aIndex )
223  {
224  if( aIndex < 0 )
225  aIndex += SegmentCount();
226 
227  if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
228  return SEG( m_points[aIndex], m_points[0], aIndex );
229  else
230  return SEG( m_points[aIndex], m_points[aIndex + 1], aIndex );
231  }
232 
241  const SEG CSegment( int aIndex ) const
242  {
243  if( aIndex < 0 )
244  aIndex += SegmentCount();
245 
246  if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
247  return SEG( const_cast<VECTOR2I&>( m_points[aIndex] ),
248  const_cast<VECTOR2I&>( m_points[0] ), aIndex );
249  else
250  return SEG( const_cast<VECTOR2I&>( m_points[aIndex] ),
251  const_cast<VECTOR2I&>( m_points[aIndex + 1] ), aIndex );
252  }
253 
261  VECTOR2I& Point( int aIndex )
262  {
263  if( aIndex < 0 )
264  aIndex += PointCount();
265 
266  return m_points[aIndex];
267  }
268 
276  const VECTOR2I& CPoint( int aIndex ) const
277  {
278  if( aIndex < 0 )
279  aIndex += PointCount();
280  else if( aIndex >= PointCount() )
281  aIndex -= PointCount();
282 
283  return m_points[aIndex];
284  }
285 
286  const std::vector<VECTOR2I>& CPoints() const
287  {
288  return m_points;
289  }
290 
295  {
296  return m_points[PointCount() - 1];
297  }
298 
302  const VECTOR2I& CLastPoint() const
303  {
304  return m_points[PointCount() - 1];
305  }
306 
308  const BOX2I BBox( int aClearance = 0 ) const override
309  {
310  BOX2I bbox;
311  bbox.Compute( m_points );
312 
313  if( aClearance != 0 )
314  bbox.Inflate( aClearance );
315 
316  return bbox;
317  }
318 
320  {
322  }
323 
332  bool Collide( const VECTOR2I& aP, int aClearance = 0 ) const override;
333 
342  bool Collide( const SEG& aSeg, int aClearance = 0 ) const override;
343 
351  int Distance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
352 
359  const SHAPE_LINE_CHAIN Reverse() const;
360 
367  long long int Length() const;
368 
379  void Append( int aX, int aY, bool aAllowDuplication = false )
380  {
381  VECTOR2I v( aX, aY );
382  Append( v, aAllowDuplication );
383  }
384 
394  void Append( const VECTOR2I& aP, bool aAllowDuplication = false )
395  {
396  if( m_points.size() == 0 )
397  m_bbox = BOX2I( aP, VECTOR2I( 0, 0 ) );
398 
399  if( m_points.size() == 0 || aAllowDuplication || CPoint( -1 ) != aP )
400  {
401  m_points.push_back( aP );
402  m_bbox.Merge( aP );
403  }
404  }
405 
412  void Append( const SHAPE_LINE_CHAIN& aOtherLine )
413  {
414  if( aOtherLine.PointCount() == 0 )
415  return;
416 
417  else if( PointCount() == 0 || aOtherLine.CPoint( 0 ) != CPoint( -1 ) )
418  {
419  const VECTOR2I p = aOtherLine.CPoint( 0 );
420  m_points.push_back( p );
421  m_bbox.Merge( p );
422  }
423 
424  for( int i = 1; i < aOtherLine.PointCount(); i++ )
425  {
426  const VECTOR2I p = aOtherLine.CPoint( i );
427  m_points.push_back( p );
428  m_bbox.Merge( p );
429  }
430  }
431 
432  void Insert( int aVertex, const VECTOR2I& aP )
433  {
434  m_points.insert( m_points.begin() + aVertex, aP );
435  }
436 
446  void Replace( int aStartIndex, int aEndIndex, const VECTOR2I& aP );
447 
457  void Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE_CHAIN& aLine );
458 
466  void Remove( int aStartIndex, int aEndIndex );
467 
473  void Remove( int aIndex )
474  {
475  Remove( aIndex, aIndex );
476  }
477 
487  int Split( const VECTOR2I& aP );
488 
496  int Find( const VECTOR2I& aP ) const;
497 
505  int FindSegment( const VECTOR2I& aP ) const;
506 
515  const SHAPE_LINE_CHAIN Slice( int aStartIndex, int aEndIndex = -1 ) const;
516 
518  {
519  compareOriginDistance( const VECTOR2I& aOrigin ):
520  m_origin( aOrigin )
521  {}
522 
523  bool operator()( const INTERSECTION& aA, const INTERSECTION& aB )
524  {
525  return ( m_origin - aA.p ).EuclideanNorm() < ( m_origin - aB.p ).EuclideanNorm();
526  }
527 
529  };
530 
531  bool Intersects( const SHAPE_LINE_CHAIN& aChain ) const;
532 
542  int Intersect( const SEG& aSeg, INTERSECTIONS& aIp ) const;
543 
553  int Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& aIp ) const;
554 
563  int PathLength( const VECTOR2I& aP ) const;
564 
575  bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
576 
584  bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
585 
593  int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
594 
603  bool CheckClearance( const VECTOR2I& aP, const int aDist) const;
604 
611  const OPT<INTERSECTION> SelfIntersecting() const;
612 
620 
626  void convertFromClipper( const ClipperLib::Path& aPath );
627 
632  ClipperLib::Path convertToClipper( bool aRequiredOrientation ) const;
633 
640  const VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
641 
651  const VECTOR2I NearestPoint( const SEG& aSeg, int& dist ) const;
652 
654  const std::string Format() const override;
655 
657  bool Parse( std::stringstream& aStream ) override;
658 
659  bool operator!=( const SHAPE_LINE_CHAIN& aRhs ) const
660  {
661  if( PointCount() != aRhs.PointCount() )
662  return true;
663 
664  for( int i = 0; i < PointCount(); i++ )
665  {
666  if( CPoint( i ) != aRhs.CPoint( i ) )
667  return true;
668  }
669 
670  return false;
671  }
672 
673  bool CompareGeometry( const SHAPE_LINE_CHAIN& aOther ) const;
674 
675  void Move( const VECTOR2I& aVector ) override
676  {
677  for( std::vector<VECTOR2I>::iterator i = m_points.begin(); i != m_points.end(); ++i )
678  (*i) += aVector;
679  }
680 
687  void Rotate( double aAngle, const VECTOR2I& aCenter );
688 
689  bool IsSolid() const override
690  {
691  return false;
692  }
693 
694  const VECTOR2I PointAlong( int aPathLength ) const;
695 
696  double Area() const;
697 
698 private:
700  std::vector<VECTOR2I> m_points;
701 
703  bool m_closed;
704 
707 };
708 
709 #endif // __SHAPE_LINE_CHAIN
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:123
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()
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:520
bool operator!=(const SHAPE_LINE_CHAIN &aRhs) const
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Function Intersect()
std::vector< INTERSECTION > INTERSECTIONS
void Insert(int aVertex, const VECTOR2I &aP)
static const int dist[10][10]
Definition: ar_matrix.cpp:320
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Function PointOnEdge()
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
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:89
SHAPE_LINE_CHAIN(const VECTOR2I *aV, int aCount)
int Distance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Function Distance()
void convertFromClipper(const ClipperLib::Path &aPath)
Function convertFromClipper() Appends the Clipper path to the current SHAPE_LINE_CHAIN.
const SHAPE_LINE_CHAIN Reverse() const
Function Reverse()
SHAPE_LINE_CHAIN(const VECTOR2I &aA, const VECTOR2I &aB)
Constructor Initializes a 2-point line chain (a single segment)
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
int PointCount() const
Function PointCount()
const OPT< INTERSECTION > SelfIntersecting() const
Function SelfIntersecting()
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()
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
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
void SetClosed(bool aClosed)
Function SetClosed()
bool m_closed
is the line chain closed?
SHAPE_LINE_CHAIN()
Constructor Initializes an empty line chain.
void Rotate(double aAngle, const VECTOR2I &aCenter)
Function Rotate rotates all vertices by a given angle.
SEG their
segment belonging from the aOther argument of Intersect()
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()
SHAPE_LINE_CHAIN(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC)
bool Intersects(const SHAPE_LINE_CHAIN &aChain) const
bool IsSolid() const override
Class SHAPE.
Definition: shape.h:58
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:384
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
Definition: seg.h:36
VECTOR2I & LastPoint()
Returns the last point in the line chain.
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:300
SEG Segment(int aIndex)
Function Segment()
const SEG CSegment(int aIndex) const
Function CSegment()
#define max(a, b)
Definition: auxiliary.h:86
Class SHAPE_LINE_CHAIN.
size_t i
Definition: json11.cpp:649
SHAPE_LINE_CHAIN(const std::vector< wxPoint > &aV)
SHAPE_LINE_CHAIN(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC, const VECTOR2I &aD)
line segment
Definition: shape.h:45
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()
VECTOR2I & Point(int aIndex)
Function Point()
void Append(const SHAPE_LINE_CHAIN &aOtherLine)
Function Append()
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 VECTOR2I PointAlong(int aPathLength) const
bool CompareGeometry(const SHAPE_LINE_CHAIN &aOther) const