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
30
31class SHAPE_ELLIPSE : public SHAPE
32{
33public:
35
36 SHAPE_ELLIPSE( const VECTOR2I& aCenter, int aMajorRadius, int aMinorRadius, const EDA_ANGLE& aRotation );
37
38 SHAPE_ELLIPSE( const VECTOR2I& aCenter, int aMajorRadius, int aMinorRadius, const EDA_ANGLE& aRotation,
39 const EDA_ANGLE& aStartAngle, const EDA_ANGLE& aEndAngle );
40
41 SHAPE_ELLIPSE( const VECTOR2I& aCenter, const VECTOR2I& aMajorEndpoint, double aRatio );
42
43 SHAPE_ELLIPSE( const VECTOR2I& aCenter, const VECTOR2I& aMajorEndpoint, double aRatio, const EDA_ANGLE& aStartAngle,
44 const EDA_ANGLE& aEndAngle );
45
46 SHAPE_ELLIPSE( const SHAPE_ELLIPSE& aOther ) = default;
48 ~SHAPE_ELLIPSE() override = default;
49
50 SHAPE* Clone() const override { return new SHAPE_ELLIPSE( *this ); }
51
52 const VECTOR2I& GetCenter() const { return m_ellipse.Center; }
53 int GetMajorRadius() const { return m_ellipse.MajorRadius; }
54 int GetMinorRadius() const { return m_ellipse.MinorRadius; }
55 const EDA_ANGLE& GetRotation() const { return m_ellipse.Rotation; }
56 const EDA_ANGLE& GetStartAngle() const { return m_ellipse.StartAngle; }
57 const EDA_ANGLE& GetEndAngle() const { return m_ellipse.EndAngle; }
58
59 bool IsArc() const { return m_isArc; }
60
61 void SetCenter( const VECTOR2I& aCenter );
62 void SetMajorRadius( int aRadius );
63 void SetMinorRadius( int aRadius );
64 void SetRotation( const EDA_ANGLE& aAngle );
65 void SetStartAngle( const EDA_ANGLE& aAngle );
66 void SetEndAngle( const EDA_ANGLE& aAngle );
67
68 const BOX2I BBox( int aClearance = 0 ) const override;
69
70 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
71 VECTOR2I* aLocation = nullptr ) const override;
72
73 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
74 VECTOR2I* aLocation = nullptr ) const override
75 {
76 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
77 }
78
79 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError, ERROR_LOC aErrorLoc ) const override;
80
81 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override;
82 void Move( const VECTOR2I& aVector ) override;
83
84 bool IsSolid() const override { return !m_isArc; }
85
89 void Mirror( const VECTOR2I& aRef, FLIP_DIRECTION aFlipDirection );
90
94 const std::string Format( bool aCplusPlus = true ) const override;
95
96 double GetLength() const;
97
104 SHAPE_LINE_CHAIN ConvertToPolyline( int aMaxError ) const;
105
106 bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const override;
107
108 SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const override;
109
110private:
115 void normalize();
116
118 void updateCache();
119
124 bool isAngleInSweep( double aAngleRad ) const;
125
127 void sweepRange( double& aStart, double& aEnd ) const;
128
130 bool m_isArc;
131
132 double m_sinRot;
133 double m_cosRot;
136};
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:918
Plain ellipse / elliptical-arc data.
Definition ellipse.h:32
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)
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:179
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:134
FLIP_DIRECTION
Definition mirror.h:23
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683