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-2017
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 #include "clipper.hpp"
38 
48 class SHAPE_LINE_CHAIN : public SHAPE
49 {
50 private:
51  typedef std::vector<VECTOR2I>::iterator point_iter;
52  typedef std::vector<VECTOR2I>::const_iterator point_citer;
53 
54 public:
60  struct INTERSECTION
61  {
68  };
69 
70  typedef std::vector<INTERSECTION> INTERSECTIONS;
71 
77  SHAPE( SH_LINE_CHAIN ), m_closed( false )
78  {}
79 
84  SHAPE( SH_LINE_CHAIN ), m_points( aShape.m_points ), m_closed( aShape.m_closed )
85  {}
86 
91  SHAPE_LINE_CHAIN( const VECTOR2I& aA, const VECTOR2I& aB ) :
92  SHAPE( SH_LINE_CHAIN ), m_closed( false )
93  {
94  m_points.resize( 2 );
95  m_points[0] = aA;
96  m_points[1] = aB;
97  }
98 
99  SHAPE_LINE_CHAIN( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) :
100  SHAPE( SH_LINE_CHAIN ), m_closed( false )
101  {
102  m_points.resize( 3 );
103  m_points[0] = aA;
104  m_points[1] = aB;
105  m_points[2] = aC;
106  }
107 
108  SHAPE_LINE_CHAIN( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC, const VECTOR2I& aD ) :
109  SHAPE( SH_LINE_CHAIN ), m_closed( false )
110  {
111  m_points.resize( 4 );
112  m_points[0] = aA;
113  m_points[1] = aB;
114  m_points[2] = aC;
115  m_points[3] = aD;
116  }
117 
118 
119  SHAPE_LINE_CHAIN( const VECTOR2I* aV, int aCount ) :
120  SHAPE( SH_LINE_CHAIN ),
121  m_closed( false )
122  {
123  m_points.resize( aCount );
124 
125  for( int i = 0; i < aCount; i++ )
126  m_points[i] = *aV++;
127  }
128 
129  SHAPE_LINE_CHAIN( const ClipperLib::Path& aPath ) :
130  SHAPE( SH_LINE_CHAIN ),
131  m_closed( true )
132  {
133  m_points.reserve( aPath.size() );
134 
135  for( auto point : aPath )
136  m_points.emplace_back( point.X, point.Y );
137  }
138 
140  {}
141 
142  SHAPE* Clone() const override;
143 
148  void Clear()
149  {
150  m_points.clear();
151  m_closed = false;
152  }
153 
161  void SetClosed( bool aClosed )
162  {
163  m_closed = aClosed;
164  }
165 
171  bool IsClosed() const
172  {
173  return m_closed;
174  }
175 
182  int SegmentCount() const
183  {
184  int c = m_points.size() - 1;
185  if( m_closed )
186  c++;
187 
188  return std::max( 0, c );
189  }
190 
197  int PointCount() const
198  {
199  return m_points.size();
200  }
201 
210  SEG Segment( int aIndex )
211  {
212  if( aIndex < 0 )
213  aIndex += SegmentCount();
214 
215  if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
216  return SEG( m_points[aIndex], m_points[0], aIndex );
217  else
218  return SEG( m_points[aIndex], m_points[aIndex + 1], aIndex );
219  }
220 
229  const SEG CSegment( int aIndex ) const
230  {
231  if( aIndex < 0 )
232  aIndex += SegmentCount();
233 
234  if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
235  return SEG( const_cast<VECTOR2I&>( m_points[aIndex] ),
236  const_cast<VECTOR2I&>( m_points[0] ), aIndex );
237  else
238  return SEG( const_cast<VECTOR2I&>( m_points[aIndex] ),
239  const_cast<VECTOR2I&>( m_points[aIndex + 1] ), aIndex );
240  }
241 
249  VECTOR2I& Point( int aIndex )
250  {
251  if( aIndex < 0 )
252  aIndex += PointCount();
253 
254  return m_points[aIndex];
255  }
256 
264  const VECTOR2I& CPoint( int aIndex ) const
265  {
266  if( aIndex < 0 )
267  aIndex += PointCount();
268  else if( aIndex >= PointCount() )
269  aIndex -= PointCount();
270 
271  return m_points[aIndex];
272  }
273 
274  const std::vector<VECTOR2I>& CPoints() const
275  {
276  return m_points;
277  }
278 
283  {
284  return m_points[PointCount() - 1];
285  }
286 
290  const VECTOR2I& CLastPoint() const
291  {
292  return m_points[PointCount() - 1];
293  }
294 
296  const BOX2I BBox( int aClearance = 0 ) const override
297  {
298  BOX2I bbox;
299  bbox.Compute( m_points );
300 
301  if( aClearance != 0 )
302  bbox.Inflate( aClearance );
303 
304  return bbox;
305  }
306 
315  bool Collide( const VECTOR2I& aP, int aClearance = 0 ) const override;
316 
325  bool Collide( const SEG& aSeg, int aClearance = 0 ) const override;
326 
334  int Distance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
335 
342  const SHAPE_LINE_CHAIN Reverse() const;
343 
350  int Length() const;
351 
362  void Append( int aX, int aY, bool aAllowDuplication = false )
363  {
364  VECTOR2I v( aX, aY );
365  Append( v, aAllowDuplication );
366  }
367 
377  void Append( const VECTOR2I& aP, bool aAllowDuplication = false )
378  {
379  if( m_points.size() == 0 )
380  m_bbox = BOX2I( aP, VECTOR2I( 0, 0 ) );
381 
382  if( m_points.size() == 0 || aAllowDuplication || CPoint( -1 ) != aP )
383  {
384  m_points.push_back( aP );
385  m_bbox.Merge( aP );
386  }
387  }
388 
395  void Append( const SHAPE_LINE_CHAIN& aOtherLine )
396  {
397  if( aOtherLine.PointCount() == 0 )
398  return;
399 
400  else if( PointCount() == 0 || aOtherLine.CPoint( 0 ) != CPoint( -1 ) )
401  {
402  const VECTOR2I p = aOtherLine.CPoint( 0 );
403  m_points.push_back( p );
404  m_bbox.Merge( p );
405  }
406 
407  for( int i = 1; i < aOtherLine.PointCount(); i++ )
408  {
409  const VECTOR2I p = aOtherLine.CPoint( i );
410  m_points.push_back( p );
411  m_bbox.Merge( p );
412  }
413  }
414 
415  void Insert( int aVertex, const VECTOR2I& aP )
416  {
417  m_points.insert( m_points.begin() + aVertex, aP );
418  }
419 
429  void Replace( int aStartIndex, int aEndIndex, const VECTOR2I& aP );
430 
440  void Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE_CHAIN& aLine );
441 
449  void Remove( int aStartIndex, int aEndIndex );
450 
456  void Remove( int aIndex )
457  {
458  Remove( aIndex, aIndex );
459  }
460 
470  int Split( const VECTOR2I& aP );
471 
479  int Find( const VECTOR2I& aP ) const;
480 
488  int FindSegment( const VECTOR2I& aP ) const;
489 
498  const SHAPE_LINE_CHAIN Slice( int aStartIndex, int aEndIndex = -1 ) const;
499 
501  {
502  compareOriginDistance( const VECTOR2I& aOrigin ):
503  m_origin( aOrigin )
504  {}
505 
506  bool operator()( const INTERSECTION& aA, const INTERSECTION& aB )
507  {
508  return ( m_origin - aA.p ).EuclideanNorm() < ( m_origin - aB.p ).EuclideanNorm();
509  }
510 
512  };
513 
514  bool Intersects( const SHAPE_LINE_CHAIN& aChain ) const;
515 
525  int Intersect( const SEG& aSeg, INTERSECTIONS& aIp ) const;
526 
536  int Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& aIp ) const;
537 
546  int PathLength( const VECTOR2I& aP ) const;
547 
556  bool PointInside( const VECTOR2I& aP ) const;
557 
565  bool PointOnEdge( const VECTOR2I& aP ) const;
566 
575  bool CheckClearance( const VECTOR2I& aP, const int aDist) const;
576 
583  const OPT<INTERSECTION> SelfIntersecting() const;
584 
592 
598  void convertFromClipper( const ClipperLib::Path& aPath );
599 
604  ClipperLib::Path convertToClipper( bool aRequiredOrientation ) const;
605 
612  const VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
613 
623  const VECTOR2I NearestPoint( const SEG& aSeg, int& dist ) const;
624 
626  const std::string Format() const override;
627 
629  bool Parse( std::stringstream& aStream ) override;
630 
631  bool operator!=( const SHAPE_LINE_CHAIN& aRhs ) const
632  {
633  if( PointCount() != aRhs.PointCount() )
634  return true;
635 
636  for( int i = 0; i < PointCount(); i++ )
637  {
638  if( CPoint( i ) != aRhs.CPoint( i ) )
639  return true;
640  }
641 
642  return false;
643  }
644 
645  bool CompareGeometry( const SHAPE_LINE_CHAIN& aOther ) const;
646 
647  void Move( const VECTOR2I& aVector ) override
648  {
649  for( std::vector<VECTOR2I>::iterator i = m_points.begin(); i != m_points.end(); ++i )
650  (*i) += aVector;
651  }
652 
659  void Rotate( double aAngle, const VECTOR2I& aCenter );
660 
661  bool IsSolid() const override
662  {
663  return false;
664  }
665 
666  const VECTOR2I PointAlong( int aPathLength ) const;
667 
668  double Area() const;
669 
670 private:
672  std::vector<VECTOR2I> m_points;
673 
675  bool m_closed;
676 
679 };
680 
681 #endif // __SHAPE_LINE_CHAIN
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
bool PointOnEdge(const VECTOR2I &aP) const
Function PointOnEdge()
BOX2I m_bbox
cached bounding box
bool PointInside(const VECTOR2I &aP) const
Function PointInside()
compareOriginDistance(const VECTOR2I &aOrigin)
void Append(const VECTOR2I &aP, bool aAllowDuplication=false)
Function Append()
int Split(const VECTOR2I &aP)
Function Split()
BOX2< VECTOR2I > BOX2I
Definition: box2.h:520
std::vector< INTERSECTION > INTERSECTIONS
void Insert(int aVertex, const VECTOR2I &aP)
static const int dist[10][10]
Definition: ar_matrix.cpp:320
const SHAPE_LINE_CHAIN Reverse() const
Function Reverse()
bool operator!=(const SHAPE_LINE_CHAIN &aRhs) const
ClipperLib::Path convertToClipper(bool aRequiredOrientation) const
Creates a new Clipper path from the SHAPE_LINE_CHAIN in a given orientation.
int PointCount() const
Function PointCount()
const VECTOR2I & CLastPoint() const
Returns the last point in the line chain.
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
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
void Compute(const Container &aPointList)
Compute the bounding box from a given list of points.
Definition: box2.h:89
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Function Intersect()
bool CheckClearance(const VECTOR2I &aP, const int aDist) const
Function CheckClearance()
SHAPE_LINE_CHAIN(const VECTOR2I *aV, int aCount)
void convertFromClipper(const ClipperLib::Path &aPath)
Function convertFromClipper() Appends the Clipper path to the current SHAPE_LINE_CHAIN.
const SHAPE_LINE_CHAIN Slice(int aStartIndex, int aEndIndex=-1) const
Function Slice()
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
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()
bool Parse(std::stringstream &aStream) override
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
const SEG CSegment(int aIndex) const
Function CSegment()
void SetClosed(bool aClosed)
Function SetClosed()
int Find(const VECTOR2I &aP) const
Function Find()
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 VECTOR2I PointAlong(int aPathLength) const
const BOX2I BBox(int aClearance=0) const override
Function BBox()
bool Intersects(const SHAPE_LINE_CHAIN &aChain) const
std::vector< VECTOR2I > m_points
array of vertices
SHAPE_LINE_CHAIN(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC)
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 FindSegment(const VECTOR2I &aP) const
Function FindSegment()
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
int PathLength(const VECTOR2I &aP) const
Function PathLength()
SEG Segment(int aIndex)
Function Segment()
#define max(a, b)
Definition: auxiliary.h:86
Class SHAPE_LINE_CHAIN.
double Area() const
size_t i
Definition: json11.cpp:597
SHAPE_LINE_CHAIN(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC, const VECTOR2I &aD)
line segment
Definition: shape.h:45
boost::optional< T > OPT
Definition: optional.h:7
bool IsClosed() const
Function IsClosed()
void Clear()
Function Clear() Removes all points from the line chain.
SHAPE * Clone() const override
Function Clone()
VECTOR2I & Point(int aIndex)
Function Point()
void Append(const SHAPE_LINE_CHAIN &aOtherLine)
Function Append()
const std::vector< VECTOR2I > & CPoints() const
std::vector< VECTOR2I >::iterator point_iter
bool CompareGeometry(const SHAPE_LINE_CHAIN &aOther) const
void Replace(int aStartIndex, int aEndIndex, const VECTOR2I &aP)
Function Replace()
int Distance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Function Distance()
SEG our
segment belonging from the (this) argument of Intersect()
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
int SegmentCount() const
Function SegmentCount()
int Length() const
Function Length()