KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_ellipse.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <core/mirror.h>
23#include <geometry/shape.h>
24#include <geometry/ellipse.h>
25#include <geometry/eda_angle.h>
26#include <math/box2.h>
27#include <math/vector2d.h>
28
30class SHAPE_ARC;
31class CIRCLE;
32
33class SHAPE_ELLIPSE : public SHAPE
34{
35public:
37
38 SHAPE_ELLIPSE( const VECTOR2I& aCenter, int aMajorRadius, int aMinorRadius, const EDA_ANGLE& aRotation );
39
40 SHAPE_ELLIPSE( const VECTOR2I& aCenter, int aMajorRadius, int aMinorRadius, const EDA_ANGLE& aRotation,
41 const EDA_ANGLE& aStartAngle, const EDA_ANGLE& aEndAngle );
42
43 SHAPE_ELLIPSE( const VECTOR2I& aCenter, const VECTOR2I& aMajorEndpoint, double aRatio );
44
45 SHAPE_ELLIPSE( const VECTOR2I& aCenter, const VECTOR2I& aMajorEndpoint, double aRatio, const EDA_ANGLE& aStartAngle,
46 const EDA_ANGLE& aEndAngle );
47
48 SHAPE_ELLIPSE( const SHAPE_ELLIPSE& aOther ) = default;
50 ~SHAPE_ELLIPSE() override = default;
51
53 bool operator==( const SHAPE_ELLIPSE& aOther ) const
54 {
55 return m_isArc == aOther.m_isArc && m_ellipse.Center == aOther.m_ellipse.Center
56 && m_ellipse.MajorRadius == aOther.m_ellipse.MajorRadius
57 && m_ellipse.MinorRadius == aOther.m_ellipse.MinorRadius
58 && m_ellipse.Rotation == aOther.m_ellipse.Rotation && m_ellipse.StartAngle == aOther.m_ellipse.StartAngle
59 && m_ellipse.EndAngle == aOther.m_ellipse.EndAngle;
60 }
61
62 SHAPE* Clone() const override { return new SHAPE_ELLIPSE( *this ); }
63
64 const VECTOR2I& GetCenter() const { return m_ellipse.Center; }
65 int GetMajorRadius() const { return m_ellipse.MajorRadius; }
66 int GetMinorRadius() const { return m_ellipse.MinorRadius; }
67 const EDA_ANGLE& GetRotation() const { return m_ellipse.Rotation; }
68 const EDA_ANGLE& GetStartAngle() const { return m_ellipse.StartAngle; }
69 const EDA_ANGLE& GetEndAngle() const { return m_ellipse.EndAngle; }
70
71 bool IsArc() const { return m_isArc; }
72
73 void SetCenter( const VECTOR2I& aCenter );
74 void SetMajorRadius( int aRadius );
75 void SetMinorRadius( int aRadius );
76 void SetRotation( const EDA_ANGLE& aAngle );
77 void SetStartAngle( const EDA_ANGLE& aAngle );
78 void SetEndAngle( const EDA_ANGLE& aAngle );
79
80 const BOX2I BBox( int aClearance = 0 ) const override;
81
82 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
83 VECTOR2I* aLocation = nullptr ) const override;
84
85 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
86 VECTOR2I* aLocation = nullptr ) const override
87 {
88 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
89 }
90
91 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError, ERROR_LOC aErrorLoc ) const override;
92
93 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override;
94 void Move( const VECTOR2I& aVector ) override;
95
96 bool IsSolid() const override { return !m_isArc; }
97
101 void Mirror( const VECTOR2I& aRef, FLIP_DIRECTION aFlipDirection );
102
106 const std::string Format( bool aCplusPlus = true ) const override;
107
108 double GetLength() const;
109
116 SHAPE_LINE_CHAIN ConvertToPolyline( int aMaxError ) const;
117
118 bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const override;
119
120 SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const override;
121
126 VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
127
134 std::vector<VECTOR2I> Intersect( const SHAPE_ELLIPSE& aOther ) const;
135 std::vector<VECTOR2I> Intersect( const CIRCLE& aCircle ) const;
136 std::vector<VECTOR2I> Intersect( const SHAPE_ARC& aArc ) const;
137
143 std::vector<VECTOR2I> Intersect( const SEG& aSeg, bool aTreatAsLine = false ) const;
144
145private:
148 struct CONIC
149 {
150 double Axx;
151 double Axy;
152 double Ayy;
153 double Bx;
154 double By;
155 double C;
156 };
157
162 void normalize();
163
165 void updateCache();
166
171 bool isAngleInSweep( double aAngleRad ) const;
172
174 void sweepRange( double& aStart, double& aEnd ) const;
175
176 VECTOR2D toLocal( const VECTOR2I& aP ) const;
177 VECTOR2I toWorld( const VECTOR2D& aP ) const;
178
180 VECTOR2D pointAtParam( double aTheta ) const;
181
183 VECTOR2D closestLocalPoint( const VECTOR2D& aLocal ) const;
184
186 CONIC conicOf( const VECTOR2I& aCenter, double aMajorR, double aMinorR, const EDA_ANGLE& aRotation ) const;
187
189 std::vector<double> conicRoots( const CONIC& aConic ) const;
190
192 std::vector<VECTOR2I> intersectCircle( const VECTOR2I& aCenter, double aRadius ) const;
193
195 bool m_isArc;
196
197 double m_sinRot;
198 double m_cosRot;
201};
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
BOX2< VECTOR2I > BOX2I
Definition box2.h:927
Represent basic circle geometry with utility geometry functions.
Definition circle.h:33
Plain ellipse / elliptical-arc data.
Definition ellipse.h:32
NumericType MinorRadius
Definition ellipse.h:103
EDA_ANGLE Rotation
Definition ellipse.h:104
EDA_ANGLE StartAngle
Definition ellipse.h:105
NumericType MajorRadius
Definition ellipse.h:102
EDA_ANGLE EndAngle
Definition ellipse.h:106
VECTOR2< NumericType > Center
Definition ellipse.h:101
Definition seg.h:38
VECTOR2I::extended_type ecoord
Definition seg.h:40
void SetRotation(const EDA_ANGLE &aAngle)
int GetMajorRadius() const
void updateCache()
Recompute cached sin/cos and inverse-radius-squared values.
SHAPE_LINE_CHAIN ConvertToPolyline(int aMaxError) const
Build a polyline approximation of the ellipse or arc.
const VECTOR2I & GetCenter() const
void SetMajorRadius(int aRadius)
ELLIPSE< int > m_ellipse
Wrapped geometric data (from geometry/ellipse.h)
bool isAngleInSweep(double aAngleRad) const
Return true if aAngleRad falls between StartAngle and EndAngle (counter-clockwise sweep).
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const override
double m_invMinorRSq
1 / MinorRadius ^ 2
void SetStartAngle(const EDA_ANGLE &aAngle)
VECTOR2D toLocal(const VECTOR2I &aP) const
CONIC conicOf(const VECTOR2I &aCenter, double aMajorR, double aMinorR, const EDA_ANGLE &aRotation) const
Write an ellipse with the given world placement as a conic in this local frame.
const EDA_ANGLE & GetStartAngle() const
double m_cosRot
cos(Rotation)
const EDA_ANGLE & GetEndAngle() const
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const override
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
const std::string Format(bool aCplusPlus=true) const override
Serialize the ellipse.
double GetLength() const
VECTOR2I toWorld(const VECTOR2D &aP) const
bool operator==(const SHAPE_ELLIPSE &aOther) const
The cached trigonometry is derived from these fields, so it is not compared.
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
std::vector< double > conicRoots(const CONIC &aConic) const
Parameter angles of this curve where aConic is zero, already limited to its sweep.
void Mirror(const VECTOR2I &aRef, FLIP_DIRECTION aFlipDirection)
Mirror the ellipse across a horizontal or vertical axis passing through aRef.
VECTOR2D pointAtParam(double aTheta) const
Point on the full ellipse at parameter angle aTheta, in the local frame.
void sweepRange(double &aStart, double &aEnd) const
Canonical CCW sweep in radians; aEnd >= aStart. Used by all sweep-aware paths.
void normalize()
If major < minor, swap them and add 90 degrees to rotation.
VECTOR2I NearestPoint(const VECTOR2I &aP) const
Find the point on the curve closest to aP.
const EDA_ANGLE & GetRotation() const
std::vector< VECTOR2I > intersectCircle(const VECTOR2I &aCenter, double aRadius) const
Points where this curve crosses a full circle, before any sweep of that circle applies.
~SHAPE_ELLIPSE() override=default
void SetCenter(const VECTOR2I &aCenter)
bool IsArc() const
std::vector< VECTOR2I > Intersect(const SHAPE_ELLIPSE &aOther) const
Find the points where this curve crosses another one.
void SetMinorRadius(int aRadius)
double m_invMajorRSq
1 / MajorRadius ^ 2
double m_sinRot
sin(Rotation)
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
VECTOR2D closestLocalPoint(const VECTOR2D &aLocal) const
Point of the curve closest to aLocal, both in the local frame.
void Move(const VECTOR2I &aVector) override
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,...
SHAPE_ELLIPSE & operator=(const SHAPE_ELLIPSE &)=default
int GetMinorRadius() const
bool IsSolid() const override
bool m_isArc
true if open elliptical arc, false if closed ellipse
void SetEndAngle(const EDA_ANGLE &aAngle)
SHAPE_ELLIPSE(const SHAPE_ELLIPSE &aOther)=default
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
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
A conic curve Axx x^2 + Axy xy + Ayy y^2 + Bx x + By y + C = 0, written in this ellipse's local frame...
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682