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 
47 class SHAPE_LINE_CHAIN : public SHAPE
48 {
49 private:
50  typedef std::vector<VECTOR2I>::iterator point_iter;
51  typedef std::vector<VECTOR2I>::const_iterator point_citer;
52 
53 public:
59  struct INTERSECTION
60  {
67  };
68 
69  typedef std::vector<INTERSECTION> INTERSECTIONS;
70 
76  SHAPE( SH_LINE_CHAIN ), m_closed( false )
77  {}
78 
83  SHAPE( SH_LINE_CHAIN ), m_points( aShape.m_points ), m_closed( aShape.m_closed )
84  {}
85 
90  SHAPE_LINE_CHAIN( const VECTOR2I& aA, const VECTOR2I& aB ) :
91  SHAPE( SH_LINE_CHAIN ), m_closed( false )
92  {
93  m_points.resize( 2 );
94  m_points[0] = aA;
95  m_points[1] = aB;
96  }
97 
98  SHAPE_LINE_CHAIN( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) :
99  SHAPE( SH_LINE_CHAIN ), m_closed( false )
100  {
101  m_points.resize( 3 );
102  m_points[0] = aA;
103  m_points[1] = aB;
104  m_points[2] = aC;
105  }
106 
107  SHAPE_LINE_CHAIN( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC, const VECTOR2I& aD ) :
108  SHAPE( SH_LINE_CHAIN ), m_closed( false )
109  {
110  m_points.resize( 4 );
111  m_points[0] = aA;
112  m_points[1] = aB;
113  m_points[2] = aC;
114  m_points[3] = aD;
115  }
116 
117 
118  SHAPE_LINE_CHAIN( const VECTOR2I* aV, int aCount ) :
119  SHAPE( SH_LINE_CHAIN ),
120  m_closed( false )
121  {
122  m_points.resize( aCount );
123 
124  for( int i = 0; i < aCount; i++ )
125  m_points[i] = *aV++;
126  }
127 
129  {}
130 
131  SHAPE* Clone() const override;
132 
137  void Clear()
138  {
139  m_points.clear();
140  m_closed = false;
141  }
142 
150  void SetClosed( bool aClosed )
151  {
152  m_closed = aClosed;
153  }
154 
160  bool IsClosed() const
161  {
162  return m_closed;
163  }
164 
171  int SegmentCount() const
172  {
173  int c = m_points.size() - 1;
174  if( m_closed )
175  c++;
176 
177  return std::max( 0, c );
178  }
179 
186  int PointCount() const
187  {
188  return m_points.size();
189  }
190 
199  SEG Segment( int aIndex )
200  {
201  if( aIndex < 0 )
202  aIndex += SegmentCount();
203 
204  if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
205  return SEG( m_points[aIndex], m_points[0], aIndex );
206  else
207  return SEG( m_points[aIndex], m_points[aIndex + 1], aIndex );
208  }
209 
218  const SEG CSegment( int aIndex ) const
219  {
220  if( aIndex < 0 )
221  aIndex += SegmentCount();
222 
223  if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
224  return SEG( const_cast<VECTOR2I&>( m_points[aIndex] ),
225  const_cast<VECTOR2I&>( m_points[0] ), aIndex );
226  else
227  return SEG( const_cast<VECTOR2I&>( m_points[aIndex] ),
228  const_cast<VECTOR2I&>( m_points[aIndex + 1] ), aIndex );
229  }
230 
238  VECTOR2I& Point( int aIndex )
239  {
240  if( aIndex < 0 )
241  aIndex += PointCount();
242 
243  return m_points[aIndex];
244  }
245 
253  const VECTOR2I& CPoint( int aIndex ) const
254  {
255  if( aIndex < 0 )
256  aIndex += PointCount();
257  else if( aIndex >= PointCount() )
258  aIndex -= PointCount();
259 
260  return m_points[aIndex];
261  }
262 
264  const BOX2I BBox( int aClearance = 0 ) const override
265  {
266  BOX2I bbox;
267  bbox.Compute( m_points );
268 
269  if( aClearance != 0 )
270  bbox.Inflate( aClearance );
271 
272  return bbox;
273  }
274 
283  bool Collide( const VECTOR2I& aP, int aClearance = 0 ) const override;
284 
293  bool Collide( const SEG& aSeg, int aClearance = 0 ) const override;
294 
302  int Distance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
303 
310  const SHAPE_LINE_CHAIN Reverse() const;
311 
318  int Length() const;
319 
330  void Append( int aX, int aY, bool aAllowDuplication = false )
331  {
332  VECTOR2I v( aX, aY );
333  Append( v, aAllowDuplication );
334  }
335 
345  void Append( const VECTOR2I& aP, bool aAllowDuplication = false )
346  {
347  if( m_points.size() == 0 )
348  m_bbox = BOX2I( aP, VECTOR2I( 0, 0 ) );
349 
350  if( m_points.size() == 0 || aAllowDuplication || CPoint( -1 ) != aP )
351  {
352  m_points.push_back( aP );
353  m_bbox.Merge( aP );
354  }
355  }
356 
363  void Append( const SHAPE_LINE_CHAIN& aOtherLine )
364  {
365  if( aOtherLine.PointCount() == 0 )
366  return;
367 
368  else if( PointCount() == 0 || aOtherLine.CPoint( 0 ) != CPoint( -1 ) )
369  {
370  const VECTOR2I p = aOtherLine.CPoint( 0 );
371  m_points.push_back( p );
372  m_bbox.Merge( p );
373  }
374 
375  for( int i = 1; i < aOtherLine.PointCount(); i++ )
376  {
377  const VECTOR2I p = aOtherLine.CPoint( i );
378  m_points.push_back( p );
379  m_bbox.Merge( p );
380  }
381  }
382 
383  void Insert( int aVertex, const VECTOR2I& aP )
384  {
385  m_points.insert( m_points.begin() + aVertex, aP );
386  }
387 
397  void Replace( int aStartIndex, int aEndIndex, const VECTOR2I& aP );
398 
408  void Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE_CHAIN& aLine );
409 
417  void Remove( int aStartIndex, int aEndIndex );
418 
424  void Remove( int aIndex )
425  {
426  Remove( aIndex, aIndex );
427  }
428 
438  int Split( const VECTOR2I& aP );
439 
447  int Find( const VECTOR2I& aP ) const;
448 
456  int FindSegment( const VECTOR2I& aP ) const;
457 
466  const SHAPE_LINE_CHAIN Slice( int aStartIndex, int aEndIndex = -1 ) const;
467 
469  {
470  compareOriginDistance( const VECTOR2I& aOrigin ):
471  m_origin( aOrigin )
472  {}
473 
474  bool operator()( const INTERSECTION& aA, const INTERSECTION& aB )
475  {
476  return ( m_origin - aA.p ).EuclideanNorm() < ( m_origin - aB.p ).EuclideanNorm();
477  }
478 
480  };
481 
482  bool Intersects( const SHAPE_LINE_CHAIN& aChain ) const;
483 
493  int Intersect( const SEG& aSeg, INTERSECTIONS& aIp ) const;
494 
504  int Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& aIp ) const;
505 
514  int PathLength( const VECTOR2I& aP ) const;
515 
524  bool PointInside( const VECTOR2I& aP ) const;
525 
533  bool PointOnEdge( const VECTOR2I& aP ) const;
534 
541  const OPT<INTERSECTION> SelfIntersecting() const;
542 
550 
557  const VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
558 
568  const VECTOR2I NearestPoint( const SEG& aSeg, int& dist ) const;
569 
571  const std::string Format() const override;
572 
574  bool Parse( std::stringstream& aStream ) override;
575 
576  bool operator!=( const SHAPE_LINE_CHAIN& aRhs ) const
577  {
578  if( PointCount() != aRhs.PointCount() )
579  return true;
580 
581  for( int i = 0; i < PointCount(); i++ )
582  {
583  if( CPoint( i ) != aRhs.CPoint( i ) )
584  return true;
585  }
586 
587  return false;
588  }
589 
590  bool CompareGeometry( const SHAPE_LINE_CHAIN& aOther ) const;
591 
592  void Move( const VECTOR2I& aVector ) override
593  {
594  for( std::vector<VECTOR2I>::iterator i = m_points.begin(); i != m_points.end(); ++i )
595  (*i) += aVector;
596  }
597 
598  bool IsSolid() const override
599  {
600  return false;
601  }
602 
603  const VECTOR2I PointAlong( int aPathLength ) const;
604 
605  double Area() const;
606 
607 private:
609  std::vector<VECTOR2I> m_points;
610 
612  bool m_closed;
613 
616 };
617 
618 #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:468
std::vector< INTERSECTION > INTERSECTIONS
void Insert(int aVertex, const VECTOR2I &aP)
const SHAPE_LINE_CHAIN Reverse() const
Function Reverse()
bool operator!=(const SHAPE_LINE_CHAIN &aRhs) const
int PointCount() const
Function PointCount()
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
static const int dist[10][10]
Definition: dist.cpp:57
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:79
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Function Intersect()
SHAPE_LINE_CHAIN(const VECTOR2I *aV, int aCount)
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:589
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.
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:57
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:350
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
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:266
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
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()
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()