KiCad PCB EDA Suite
shape_arc.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) 2018 CERN
5  * Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Tomasz Wlostowski <[email protected]>
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_ARC_H
27 #define __SHAPE_ARC_H
28 
29 #include <geometry/shape.h>
30 #include <convert_to_biu.h>
31 #include <math/vector2d.h> // for VECTOR2I
32 
33 class SHAPE_LINE_CHAIN;
34 
35 class SHAPE_ARC : public SHAPE
36 {
37 public:
38 
40  SHAPE( SH_ARC ),
41  m_width( 0 )
42  {};
43 
55  SHAPE_ARC( const VECTOR2I& aArcCenter, const VECTOR2I& aArcStartPoint, double aCenterAngle,
56  int aWidth = 0 );
57 
64  SHAPE_ARC( const VECTOR2I& aArcStart, const VECTOR2I& aArcMid, const VECTOR2I& aArcEnd,
65  int aWidth );
66 
75  SHAPE_ARC( const SEG& aSegmentA, const SEG& aSegmentB, int aRadius, int aWidth = 0 );
76 
77  SHAPE_ARC( const SHAPE_ARC& aOther );
78 
79  virtual ~SHAPE_ARC() {}
80 
81  SHAPE* Clone() const override
82  {
83  return new SHAPE_ARC( *this );
84  }
85 
95  SHAPE_ARC& ConstructFromStartEndAngle( const VECTOR2I& aStart, const VECTOR2I& aEnd,
96  double aAngle, double aWidth = 0 );
97 
107  SHAPE_ARC& ConstructFromStartEndCenter( const VECTOR2I& aStart, const VECTOR2I& aEnd,
108  const VECTOR2I& aCenter, bool aClockwise = false,
109  double aWidth = 0 );
110 
111  const VECTOR2I& GetP0() const { return m_start; }
112  const VECTOR2I& GetP1() const { return m_end; }
113  const VECTOR2I& GetArcMid() const { return m_mid; }
114  VECTOR2I GetCenter() const;
115 
116  const BOX2I BBox( int aClearance = 0 ) const override;
117 
118  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
119  VECTOR2I* aLocation = nullptr ) const override;
120  bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
121  VECTOR2I* aLocation = nullptr ) const override;
122 
123 
124  bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
125  VECTOR2I* aLocation = nullptr ) const override
126  {
127  return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
128  }
129 
138  int IntersectLine( const SEG& aSeg, std::vector<VECTOR2I>* aIpsBuffer ) const;
139 
147  int Intersect( const SHAPE_ARC& aArc, std::vector<VECTOR2I>* aIpsBuffer ) const;
148 
149  bool IsClockwise() const;
150 
151  void SetWidth( int aWidth )
152  {
153  m_width = aWidth;
154  }
155 
156  int GetWidth() const
157  {
158  return m_width;
159  }
160 
161  bool IsSolid() const override
162  {
163  return true;
164  }
165 
166  void Move( const VECTOR2I& aVector ) override;
167 
174  void Rotate( double aAngle, const VECTOR2I& aCenter ) override;
175 
176  void Mirror( bool aX = true, bool aY = false, const VECTOR2I& aVector = { 0, 0 } );
177 
178  void Mirror( const SEG& axis );
179 
180  void Reverse();
181 
182  SHAPE_ARC Reversed() const;
183 
184  double GetRadius() const;
185 
186  SEG GetChord() const
187  {
188  return SEG( m_start, m_end );
189  }
190 
194  double GetCentralAngle() const;
195 
199  double GetStartAngle() const;
200 
204  double GetEndAngle() const;
205 
209  double GetLength() const;
210 
220  static double DefaultAccuracyForPCB(){ return 0.005 * PCB_IU_PER_MM; }
221 
237  const SHAPE_LINE_CHAIN ConvertToPolyline( double aAccuracy = DefaultAccuracyForPCB(),
238  double* aEffectiveAccuracy = nullptr ) const;
239 
240  bool operator==( SHAPE_ARC const& aArc ) const
241  {
242  return ( aArc.m_start == m_start ) && ( aArc.m_end == m_end ) && ( aArc.m_mid == m_mid )
243  && ( aArc.m_width == m_width );
244  }
245 
246 private:
247  bool ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) const
248  {
249  return ( ecoord{ aC.y } - aA.y ) * ( ecoord{ aB.x } - aA.x ) >
250  ( ecoord{ aB.y } - aA.y ) * ( ecoord{ aC.x } - aA.x );
251  }
252 
253  void update_bbox();
254 
255  bool sliceContainsPoint( const VECTOR2I& p ) const;
256 
257 private:
261 
262  int m_width;
264 };
265 
266 // Required for Boost Test BOOST_CHECK_EQUAL:
267 std::ostream& operator<<( std::ostream& aStream, const SHAPE_ARC& aArc );
268 
269 #endif
void Mirror(bool aX=true, bool aY=false, const VECTOR2I &aVector={ 0, 0 })
Definition: shape_arc.cpp:588
void Rotate(double aAngle, const VECTOR2I &aCenter) override
Rotate the arc by a given angle about a point.
Definition: shape_arc.cpp:570
bool IsClockwise() const
Definition: shape_arc.cpp:391
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
Definition: shape_arc.cpp:229
double GetRadius() const
Definition: shape_arc.cpp:492
VECTOR2I m_end
Definition: shape_arc.h:260
Define a general 2D-vector/point.
Definition: vector2d.h:61
SHAPE_ARC & ConstructFromStartEndAngle(const VECTOR2I &aStart, const VECTOR2I &aEnd, double aAngle, double aWidth=0)
Construct this arc from the given start, end and angle.
Definition: shape_arc.cpp:183
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: shape_arc.h:247
double GetStartAngle() const
Definition: shape_arc.cpp:444
bool sliceContainsPoint(const VECTOR2I &p) const
Definition: shape_arc.cpp:630
static double DefaultAccuracyForPCB()
Definition: shape_arc.h:220
int Intersect(const SHAPE_ARC &aArc, std::vector< VECTOR2I > *aIpsBuffer) const
Find intersection points between this arc and aArc.
Definition: shape_arc.cpp:319
A 2D bounding box built on top of an origin point and size vector.
Definition: box2.h:41
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition: shape.h:165
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition: shape_arc.h:124
VECTOR2I m_mid
Definition: shape_arc.h:259
constexpr double PCB_IU_PER_MM
const VECTOR2I & GetP0() const
Definition: shape_arc.h:111
virtual ~SHAPE_ARC()
Definition: shape_arc.h:79
const VECTOR2I & GetArcMid() const
Definition: shape_arc.h:113
SEG GetChord() const
Definition: shape_arc.h:186
circular arc
Definition: shape.h:50
An abstract shape on 2D plane.
Definition: shape.h:116
bool operator==(SHAPE_ARC const &aArc) const
Definition: shape_arc.h:240
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition: shape_arc.h:81
double GetEndAngle() const
Definition: shape_arc.cpp:454
void update_bbox()
Definition: shape_arc.cpp:338
void Reverse()
Definition: shape_arc.cpp:618
int IntersectLine(const SEG &aSeg, std::vector< VECTOR2I > *aIpsBuffer) const
Find intersection points between this arc and aSeg, treating aSeg as an infinite line.
Definition: shape_arc.cpp:301
Definition: seg.h:40
void Move(const VECTOR2I &aVector) override
Definition: shape_arc.cpp:561
int GetWidth() const
Definition: shape_arc.h:156
bool IsSolid() const override
Definition: shape_arc.h:161
const SHAPE_LINE_CHAIN ConvertToPolyline(double aAccuracy=DefaultAccuracyForPCB(), double *aEffectiveAccuracy=nullptr) const
Construct a SHAPE_LINE_CHAIN of segments from a given arc.
Definition: shape_arc.cpp:498
std::ostream & operator<<(std::ostream &aStream, const SHAPE_ARC &aArc)
Definition: shape_arc.cpp:35
SHAPE_ARC & ConstructFromStartEndCenter(const VECTOR2I &aStart, const VECTOR2I &aEnd, const VECTOR2I &aCenter, bool aClockwise=false, double aWidth=0)
Constructs this arc from the given start, end and center.
Definition: shape_arc.cpp:201
VECTOR2I::extended_type ecoord
Definition: shape.h:236
Represent a polyline (an zero-thickness chain of connected line segments).
VECTOR2I m_start
Definition: shape_arc.h:258
double GetCentralAngle() const
Definition: shape_arc.cpp:479
SHAPE_ARC()
Definition: shape_arc.h:39
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_arc.cpp:380
SHAPE_ARC Reversed() const
Definition: shape_arc.cpp:624
void SetWidth(int aWidth)
Definition: shape_arc.h:151
double GetLength() const
Definition: shape_arc.cpp:470
int m_width
Definition: shape_arc.h:262
BOX2I m_bbox
Definition: shape_arc.h:263
const VECTOR2I & GetP1() const
Definition: shape_arc.h:112
VECTOR2I GetCenter() const
Definition: shape_arc.cpp:464