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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#pragma once
25
26#include <core/mirror.h>
27#include <geometry/shape.h>
28#include <geometry/ellipse.h>
29#include <geometry/eda_angle.h>
30#include <math/box2.h>
31#include <math/vector2d.h>
32
34
35class SHAPE_ELLIPSE : public SHAPE
36{
37public:
39
40 SHAPE_ELLIPSE( const VECTOR2I& aCenter, int aMajorRadius, int aMinorRadius, const EDA_ANGLE& aRotation );
41
42 SHAPE_ELLIPSE( const VECTOR2I& aCenter, int aMajorRadius, int aMinorRadius, const EDA_ANGLE& aRotation,
43 const EDA_ANGLE& aStartAngle, const EDA_ANGLE& aEndAngle );
44
45 SHAPE_ELLIPSE( const VECTOR2I& aCenter, const VECTOR2I& aMajorEndpoint, double aRatio );
46
47 SHAPE_ELLIPSE( const VECTOR2I& aCenter, const VECTOR2I& aMajorEndpoint, double aRatio, const EDA_ANGLE& aStartAngle,
48 const EDA_ANGLE& aEndAngle );
49
50 SHAPE_ELLIPSE( const SHAPE_ELLIPSE& aOther ) = default;
52 ~SHAPE_ELLIPSE() override = default;
53
54 SHAPE* Clone() const override { return new SHAPE_ELLIPSE( *this ); }
55
56 const VECTOR2I& GetCenter() const { return m_ellipse.Center; }
57 int GetMajorRadius() const { return m_ellipse.MajorRadius; }
58 int GetMinorRadius() const { return m_ellipse.MinorRadius; }
59 const EDA_ANGLE& GetRotation() const { return m_ellipse.Rotation; }
60 const EDA_ANGLE& GetStartAngle() const { return m_ellipse.StartAngle; }
61 const EDA_ANGLE& GetEndAngle() const { return m_ellipse.EndAngle; }
62
63 bool IsArc() const { return m_isArc; }
64
65 void SetCenter( const VECTOR2I& aCenter );
66 void SetMajorRadius( int aRadius );
67 void SetMinorRadius( int aRadius );
68 void SetRotation( const EDA_ANGLE& aAngle );
69 void SetStartAngle( const EDA_ANGLE& aAngle );
70 void SetEndAngle( const EDA_ANGLE& aAngle );
71
72 const BOX2I BBox( int aClearance = 0 ) const override;
73
74 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
75 VECTOR2I* aLocation = nullptr ) const override;
76
77 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
78 VECTOR2I* aLocation = nullptr ) const override
79 {
80 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
81 }
82
83 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError, ERROR_LOC aErrorLoc ) const override;
84
85 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override;
86 void Move( const VECTOR2I& aVector ) override;
87
88 bool IsSolid() const override { return !m_isArc; }
89
93 void Mirror( const VECTOR2I& aRef, FLIP_DIRECTION aFlipDirection );
94
98 const std::string Format( bool aCplusPlus = true ) const override;
99
100 double GetLength() const;
101
108 SHAPE_LINE_CHAIN ConvertToPolyline( int aMaxError ) const;
109
110 bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const override;
111
112 SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const override;
113
114private:
119 void normalize();
120
122 void updateCache();
123
128 bool isAngleInSweep( double aAngleRad ) const;
129
131 void sweepRange( double& aStart, double& aEnd ) const;
132
134 bool m_isArc;
135
136 double m_sinRot;
137 double m_cosRot;
140};
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:922
Plain ellipse / elliptical-arc data.
Definition ellipse.h:32
Definition seg.h:42
VECTOR2I::extended_type ecoord
Definition seg.h:44
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)
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
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.
void Mirror(const VECTOR2I &aRef, FLIP_DIRECTION aFlipDirection)
Mirror the ellipse across a horizontal or vertical axis passing through aRef.
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.
const EDA_ANGLE & GetRotation() const
~SHAPE_ELLIPSE() override=default
void SetCenter(const VECTOR2I &aCenter)
bool IsArc() const
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
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:183
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:138
FLIP_DIRECTION
Definition mirror.h:27
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687