KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <core/mirror.h> // for FLIP_DIRECTION
25#include <geometry/shape.h>
26#include <base_units.h>
27#include <math/vector2d.h> // for VECTOR2I
28#include <geometry/eda_angle.h>
29
30class CIRCLE;
31class SHAPE_CIRCLE;
33class SHAPE_RECT;
34
35class SHAPE_ARC : public SHAPE
36{
37public:
38
40 SHAPE( SH_ARC ),
41 m_width( 0 ),
42 m_radius( 0 )
43 {};
44
56 SHAPE_ARC( const VECTOR2I& aArcCenter, const VECTOR2I& aArcStartPoint,
57 const EDA_ANGLE& aCenterAngle, int aWidth = 0 );
58
65 SHAPE_ARC( const VECTOR2I& aArcStart, const VECTOR2I& aArcMid, const VECTOR2I& aArcEnd,
66 int aWidth );
67
76 SHAPE_ARC( const SEG& aSegmentA, const SEG& aSegmentB, int aRadius, int aWidth = 0 );
77
78 SHAPE_ARC( const SHAPE_ARC& aOther );
79
80 SHAPE_ARC( const SHAPE_ARC& aOther, int aWidth );
81
82 virtual ~SHAPE_ARC() {}
83
84 SHAPE* Clone() const override
85 {
86 return new SHAPE_ARC( *this );
87 }
88
98 SHAPE_ARC& ConstructFromStartEndAngle( const VECTOR2I& aStart, const VECTOR2I& aEnd,
99 const EDA_ANGLE& aAngle, double aWidth = 0 );
100
110 SHAPE_ARC& ConstructFromStartEndCenter( const VECTOR2I& aStart, const VECTOR2I& aEnd,
111 const VECTOR2I& aCenter, bool aClockwise = false,
112 double aWidth = 0 );
113
114 const VECTOR2I& GetP0() const { return m_start; }
115 const VECTOR2I& GetP1() const { return m_end; }
116 const VECTOR2I& GetArcMid() const { return m_mid; }
117 const VECTOR2I& GetCenter() const;
118
119 const BOX2I BBox( int aClearance = 0 ) const override;
120
121 VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
122
131 bool NearestPoints( const SHAPE_ARC& aArc, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const;
132
141 bool NearestPoints( const SHAPE_CIRCLE& aCircle, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const;
142
151 bool NearestPoints( const SEG& aSeg, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const;
152
161 bool NearestPoints( const SHAPE_RECT& aRect, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const;
162
163 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
164 VECTOR2I* aLocation = nullptr ) const override;
165 bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
166 VECTOR2I* aLocation = nullptr ) const override;
167
168
169 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
170 VECTOR2I* aLocation = nullptr ) const override
171 {
172 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
173 }
174
183 int IntersectLine( const SEG& aSeg, std::vector<VECTOR2I>* aIpsBuffer ) const;
184
192 int Intersect( const CIRCLE& aArc, std::vector<VECTOR2I>* aIpsBuffer ) const;
193
201 int Intersect( const SHAPE_ARC& aArc, std::vector<VECTOR2I>* aIpsBuffer ) const;
202
203 VECTOR2I GetStart() const override { return m_start; }
204 VECTOR2I GetEnd() const override { return m_end; }
205
206 void SetWidth( int aWidth ) override
207 {
208 m_width = aWidth;
209 }
210
211 int GetWidth() const override
212 {
213 return m_width;
214 }
215
216 bool IsSolid() const override
217 {
218 return true;
219 }
220
221 bool IsEffectiveLine() const;
222
223 void Move( const VECTOR2I& aVector ) override;
224
231 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter ) override;
232
233 void Mirror( const VECTOR2I& aRef, FLIP_DIRECTION aFlipDirection );
234
235 void Mirror( const SEG& axis );
236
237 void Reverse();
238
239 SHAPE_ARC Reversed() const;
240
241 double GetRadius() const;
242
243 SEG GetChord() const
244 {
245 return SEG( m_start, m_end );
246 }
247
254
258 EDA_ANGLE GetStartAngle() const;
259
263 EDA_ANGLE GetEndAngle() const;
264
268 double GetLength() const;
269
279 static int DefaultAccuracyForPCB() { return ARC_HIGH_DEF; }
280
297 int* aActualError = nullptr ) const;
298
299 bool operator==( SHAPE_ARC const& aArc ) const
300 {
301 return ( aArc.m_start == m_start ) && ( aArc.m_end == m_end ) && ( aArc.m_mid == m_mid )
302 && ( aArc.m_width == m_width );
303 }
304
305 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aMaxError, ERROR_LOC aErrorLoc ) const override;
306
310 bool IsCCW() const
311 {
312 VECTOR2L mid = m_mid;
313 VECTOR2L v1 = m_end - mid;
314 VECTOR2L v2 = m_start - mid;
315
316 return v1.Cross( v2 ) > 0;
317 }
318
319 bool IsClockwise() const { return !IsCCW(); }
320
321private:
322 void update_values();
323
324 bool sliceContainsPoint( const VECTOR2I& p ) const;
325
326private:
331
332 BOX2I m_bbox; // Calculated value
333 VECTOR2I m_center; // Calculated value
334 double m_radius; // Calculated value
335};
336
337// Required for Boost Test BOOST_CHECK_EQUAL:
338std::ostream& operator<<( std::ostream& aStream, const SHAPE_ARC& aArc );
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
constexpr int ARC_HIGH_DEF
Definition base_units.h:137
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
Represent basic circle geometry with utility geometry functions.
Definition circle.h:33
Definition seg.h:38
EDA_ANGLE GetCentralAngle() const
Get the "central angle" of the arc - this is the angle at the point of the "pie slice".
double m_radius
Definition shape_arc.h:334
const VECTOR2I & GetArcMid() const
Definition shape_arc.h:116
void update_values()
VECTOR2I GetEnd() const override
Definition shape_arc.h:204
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aMaxError, ERROR_LOC aErrorLoc) const override
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
SEG GetChord() const
Definition shape_arc.h:243
bool IsClockwise() const
Definition shape_arc.h:319
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition shape_arc.h:169
void Move(const VECTOR2I &aVector) override
SHAPE_ARC & ConstructFromStartEndAngle(const VECTOR2I &aStart, const VECTOR2I &aEnd, const EDA_ANGLE &aAngle, double aWidth=0)
Construct this arc from the given start, end and angle.
virtual ~SHAPE_ARC()
Definition shape_arc.h:82
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
int GetWidth() const override
Definition shape_arc.h:211
EDA_ANGLE GetEndAngle() const
bool IsCCW() const
Definition shape_arc.h:310
double GetLength() const
BOX2I m_bbox
Definition shape_arc.h:332
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter) override
Rotate the arc by a given angle about a point.
bool sliceContainsPoint(const VECTOR2I &p) const
const SHAPE_LINE_CHAIN ConvertToPolyline(int aMaxError=DefaultAccuracyForPCB(), int *aActualError=nullptr) const
Construct a SHAPE_LINE_CHAIN of segments from a given arc.
VECTOR2I NearestPoint(const VECTOR2I &aP) const
int Intersect(const CIRCLE &aArc, std::vector< VECTOR2I > *aIpsBuffer) const
Find intersection points between this arc and a CIRCLE.
VECTOR2I m_mid
Definition shape_arc.h:328
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.
void Mirror(const VECTOR2I &aRef, FLIP_DIRECTION aFlipDirection)
SHAPE_ARC Reversed() const
VECTOR2I m_center
Definition shape_arc.h:333
int m_width
Definition shape_arc.h:330
const VECTOR2I & GetP1() const
Definition shape_arc.h:115
int IntersectLine(const SEG &aSeg, std::vector< VECTOR2I > *aIpsBuffer) const
Find intersection points between this arc and aSeg, treating aSeg as an infinite line.
VECTOR2I m_end
Definition shape_arc.h:329
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,...
static int DefaultAccuracyForPCB()
Definition shape_arc.h:279
double GetRadius() const
EDA_ANGLE GetStartAngle() const
bool operator==(SHAPE_ARC const &aArc) const
Definition shape_arc.h:299
bool IsEffectiveLine() const
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition shape_arc.h:84
void Reverse()
bool NearestPoints(const SHAPE_ARC &aArc, VECTOR2I &aPtA, VECTOR2I &aPtB, int64_t &aDistSq) const
Compute closest points between this arc and aArc.
const VECTOR2I & GetP0() const
Definition shape_arc.h:114
VECTOR2I m_start
Definition shape_arc.h:327
bool IsSolid() const override
Definition shape_arc.h:216
void SetWidth(int aWidth) override
Definition shape_arc.h:206
VECTOR2I GetStart() const override
Definition shape_arc.h:203
const VECTOR2I & GetCenter() const
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Represent a set of closed polygons.
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:179
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:134
FLIP_DIRECTION
Definition mirror.h:23
@ SH_ARC
circular arc
Definition shape.h:50
std::ostream & operator<<(std::ostream &aStream, const SHAPE_ARC &aArc)
Definition shape_arc.cpp:33
VECTOR3I v1(5, 5, 5)
VECTOR2I v2(1, 0)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< int64_t > VECTOR2L
Definition vector2d.h:684