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 <tomasz.wlostowski@cern.ch>
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  bool IsClockwise() const;
124 
125  void SetWidth( int aWidth )
126  {
127  m_width = aWidth;
128  }
129 
130  int GetWidth() const
131  {
132  return m_width;
133  }
134 
135  bool IsSolid() const override
136  {
137  return true;
138  }
139 
140  void Move( const VECTOR2I& aVector ) override;
141 
148  void Rotate( double aAngle, const VECTOR2I& aCenter ) override;
149 
150  void Mirror( bool aX = true, bool aY = false, const VECTOR2I& aVector = { 0, 0 } );
151 
152  void Mirror( const SEG& axis );
153 
154  void Reverse();
155 
156  SHAPE_ARC Reversed() const;
157 
158  double GetRadius() const;
159 
160  SEG GetChord() const
161  {
162  return SEG( m_start, m_end );
163  }
164 
168  double GetCentralAngle() const;
169 
173  double GetStartAngle() const;
174 
178  double GetEndAngle() const;
179 
183  double GetLength() const;
184 
194  static double DefaultAccuracyForPCB(){ return 0.005 * PCB_IU_PER_MM; }
195 
211  const SHAPE_LINE_CHAIN ConvertToPolyline( double aAccuracy = DefaultAccuracyForPCB(),
212  double* aEffectiveAccuracy = nullptr ) const;
213 
214  bool operator==( SHAPE_ARC const& aArc ) const
215  {
216  return ( aArc.m_start == m_start ) && ( aArc.m_end == m_end ) && ( aArc.m_mid == m_mid )
217  && ( aArc.m_width == m_width );
218  }
219 
220 private:
221  bool ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) const
222  {
223  return ( ecoord{ aC.y } - aA.y ) * ( ecoord{ aB.x } - aA.x ) >
224  ( ecoord{ aB.y } - aA.y ) * ( ecoord{ aC.x } - aA.x );
225  }
226 
227  void update_bbox();
228 
229 private:
233 
234  int m_width;
236 };
237 
238 // Required for Boost Test BOOST_CHECK_EQUAL:
239 std::ostream& operator<<( std::ostream& aStream, const SHAPE_ARC& aArc );
240 
241 #endif
void Mirror(bool aX=true, bool aY=false, const VECTOR2I &aVector={ 0, 0 })
Definition: shape_arc.cpp:549
void Rotate(double aAngle, const VECTOR2I &aCenter) override
Rotate the arc by a given angle about a point.
Definition: shape_arc.cpp:531
bool IsClockwise() const
Definition: shape_arc.cpp:352
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:227
double GetRadius() const
Definition: shape_arc.cpp:453
VECTOR2I m_end
Definition: shape_arc.h:232
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:181
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: shape_arc.h:221
double GetStartAngle() const
Definition: shape_arc.cpp:405
static double DefaultAccuracyForPCB()
Definition: shape_arc.h:194
A 2D bounding box built on top of an origin point and size vector.
Definition: box2.h:41
VECTOR2I m_mid
Definition: shape_arc.h:231
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:160
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:214
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition: shape_arc.h:81
double GetEndAngle() const
Definition: shape_arc.cpp:415
void update_bbox()
Definition: shape_arc.cpp:299
void Reverse()
Definition: shape_arc.cpp:579
Definition: seg.h:40
void Move(const VECTOR2I &aVector) override
Definition: shape_arc.cpp:522
int GetWidth() const
Definition: shape_arc.h:130
bool IsSolid() const override
Definition: shape_arc.h:135
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:459
std::ostream & operator<<(std::ostream &aStream, const SHAPE_ARC &aArc)
Definition: shape_arc.cpp:33
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:199
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:230
double GetCentralAngle() const
Definition: shape_arc.cpp:440
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:341
SHAPE_ARC Reversed() const
Definition: shape_arc.cpp:585
void SetWidth(int aWidth)
Definition: shape_arc.h:125
double GetLength() const
Definition: shape_arc.cpp:431
int m_width
Definition: shape_arc.h:234
BOX2I m_bbox
Definition: shape_arc.h:235
const VECTOR2I & GetP1() const
Definition: shape_arc.h:112
VECTOR2I GetCenter() const
Definition: shape_arc.cpp:425