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, 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#pragma once
27
28#include <core/mirror.h> // for FLIP_DIRECTION
29#include <geometry/shape.h>
30#include <base_units.h>
31#include <math/vector2d.h> // for VECTOR2I
32#include <geometry/eda_angle.h>
33
34class CIRCLE;
35class SHAPE_CIRCLE;
37class SHAPE_RECT;
38
39class SHAPE_ARC : public SHAPE
40{
41public:
42
44 SHAPE( SH_ARC ),
45 m_width( 0 ),
46 m_radius( 0 )
47 {};
48
60 SHAPE_ARC( const VECTOR2I& aArcCenter, const VECTOR2I& aArcStartPoint,
61 const EDA_ANGLE& aCenterAngle, int aWidth = 0 );
62
69 SHAPE_ARC( const VECTOR2I& aArcStart, const VECTOR2I& aArcMid, const VECTOR2I& aArcEnd,
70 int aWidth );
71
80 SHAPE_ARC( const SEG& aSegmentA, const SEG& aSegmentB, int aRadius, int aWidth = 0 );
81
82 SHAPE_ARC( const SHAPE_ARC& aOther );
83
84 SHAPE_ARC( const SHAPE_ARC& aOther, int aWidth );
85
86 virtual ~SHAPE_ARC() {}
87
88 SHAPE* Clone() const override
89 {
90 return new SHAPE_ARC( *this );
91 }
92
102 SHAPE_ARC& ConstructFromStartEndAngle( const VECTOR2I& aStart, const VECTOR2I& aEnd,
103 const EDA_ANGLE& aAngle, double aWidth = 0 );
104
114 SHAPE_ARC& ConstructFromStartEndCenter( const VECTOR2I& aStart, const VECTOR2I& aEnd,
115 const VECTOR2I& aCenter, bool aClockwise = false,
116 double aWidth = 0 );
117
118 const VECTOR2I& GetP0() const { return m_start; }
119 const VECTOR2I& GetP1() const { return m_end; }
120 const VECTOR2I& GetArcMid() const { return m_mid; }
121 const VECTOR2I& GetCenter() const;
122
123 const BOX2I BBox( int aClearance = 0 ) const override;
124
125 VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
126
135 bool NearestPoints( const SHAPE_ARC& aArc, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const;
136
145 bool NearestPoints( const SHAPE_CIRCLE& aCircle, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const;
146
155 bool NearestPoints( const SEG& aSeg, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const;
156
165 bool NearestPoints( const SHAPE_RECT& aRect, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const;
166
167 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
168 VECTOR2I* aLocation = nullptr ) const override;
169 bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
170 VECTOR2I* aLocation = nullptr ) const override;
171
172
173 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
174 VECTOR2I* aLocation = nullptr ) const override
175 {
176 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
177 }
178
187 int IntersectLine( const SEG& aSeg, std::vector<VECTOR2I>* aIpsBuffer ) const;
188
196 int Intersect( const CIRCLE& aArc, std::vector<VECTOR2I>* aIpsBuffer ) const;
197
205 int Intersect( const SHAPE_ARC& aArc, std::vector<VECTOR2I>* aIpsBuffer ) const;
206
207 VECTOR2I GetStart() const override { return m_start; }
208 VECTOR2I GetEnd() const override { return m_end; }
209
210 void SetWidth( int aWidth ) override
211 {
212 m_width = aWidth;
213 }
214
215 int GetWidth() const override
216 {
217 return m_width;
218 }
219
220 bool IsSolid() const override
221 {
222 return true;
223 }
224
225 bool IsEffectiveLine() const;
226
227 void Move( const VECTOR2I& aVector ) override;
228
235 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter ) override;
236
237 void Mirror( const VECTOR2I& aRef, FLIP_DIRECTION aFlipDirection );
238
239 void Mirror( const SEG& axis );
240
241 void Reverse();
242
243 SHAPE_ARC Reversed() const;
244
245 double GetRadius() const;
246
247 SEG GetChord() const
248 {
249 return SEG( m_start, m_end );
250 }
251
258
262 EDA_ANGLE GetStartAngle() const;
263
267 EDA_ANGLE GetEndAngle() const;
268
272 double GetLength() const;
273
283 static int DefaultAccuracyForPCB() { return ARC_HIGH_DEF; }
284
301 int* aActualError = nullptr ) const;
302
303 bool operator==( SHAPE_ARC const& aArc ) const
304 {
305 return ( aArc.m_start == m_start ) && ( aArc.m_end == m_end ) && ( aArc.m_mid == m_mid )
306 && ( aArc.m_width == m_width );
307 }
308
309 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aMaxError, ERROR_LOC aErrorLoc ) const override;
310
314 bool IsCCW() const
315 {
316 VECTOR2L mid = m_mid;
317 VECTOR2L v1 = m_end - mid;
318 VECTOR2L v2 = m_start - mid;
319
320 return v1.Cross( v2 ) > 0;
321 }
322
323 bool IsClockwise() const { return !IsCCW(); }
324
325private:
326 void update_values();
327
328 bool sliceContainsPoint( const VECTOR2I& p ) const;
329
330private:
335
336 BOX2I m_bbox; // Calculated value
337 VECTOR2I m_center; // Calculated value
338 double m_radius; // Calculated value
339};
340
341// Required for Boost Test BOOST_CHECK_EQUAL:
342std::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:129
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
Represent basic circle geometry with utility geometry functions.
Definition circle.h:33
Definition seg.h:42
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:338
const VECTOR2I & GetArcMid() const
Definition shape_arc.h:120
void update_values()
VECTOR2I GetEnd() const override
Definition shape_arc.h:208
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:247
bool IsClockwise() const
Definition shape_arc.h:323
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition shape_arc.h:173
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:86
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:215
EDA_ANGLE GetEndAngle() const
bool IsCCW() const
Definition shape_arc.h:314
double GetLength() const
BOX2I m_bbox
Definition shape_arc.h:336
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:332
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:337
int m_width
Definition shape_arc.h:334
const VECTOR2I & GetP1() const
Definition shape_arc.h:119
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:333
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:283
double GetRadius() const
EDA_ANGLE GetStartAngle() const
bool operator==(SHAPE_ARC const &aArc) const
Definition shape_arc.h:303
bool IsEffectiveLine() const
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition shape_arc.h:88
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:118
VECTOR2I m_start
Definition shape_arc.h:331
bool IsSolid() const override
Definition shape_arc.h:220
void SetWidth(int aWidth) override
Definition shape_arc.h:210
VECTOR2I GetStart() const override
Definition shape_arc.h:207
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:181
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:136
FLIP_DIRECTION
Definition mirror.h:27
@ SH_ARC
circular arc
Definition shape.h:54
std::ostream & operator<<(std::ostream &aStream, const SHAPE_ARC &aArc)
Definition shape_arc.cpp:37
VECTOR3I v1(5, 5, 5)
VECTOR2I v2(1, 0)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< int64_t > VECTOR2L
Definition vector2d.h:696