KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_circle.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) 2013 CERN
5 * @author Tomasz Wlostowski <[email protected]>
6 * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
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#ifndef __SHAPE_CIRCLE_H
27#define __SHAPE_CIRCLE_H
28
29#include <geometry/shape.h>
30#include <geometry/circle.h>
31#include <math/box2.h>
32#include <math/vector2d.h>
33#include <trigo.h>
34
35#include <algorithm>
36
37class SHAPE_CIRCLE : public SHAPE
38{
39public:
42 m_circle()
43 {}
44
45 SHAPE_CIRCLE( const VECTOR2I& aCenter, int aRadius ) :
47 m_circle( aCenter, aRadius )
48 {}
49
50 SHAPE_CIRCLE( const CIRCLE& aCircle ) :
52 m_circle( aCircle )
53 {}
54
55 SHAPE_CIRCLE( const SHAPE_CIRCLE& aOther ) :
57 m_circle( aOther.m_circle )
58 {};
59
61 {}
62
63 SHAPE* Clone() const override
64 {
65 return new SHAPE_CIRCLE( *this );
66 }
67
68 SHAPE_CIRCLE& operator=( const SHAPE_CIRCLE& ) = default;
69
70 const BOX2I BBox( int aClearance = 0 ) const override
71 {
72 const VECTOR2I rc( m_circle.Radius + aClearance, m_circle.Radius + aClearance );
73
74 return BOX2I( m_circle.Center - rc, rc * 2 );
75 }
76
77 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
78 VECTOR2I* aLocation = nullptr ) const override
79 {
80 int minDist = aClearance + m_circle.Radius;
82 ecoord dist_sq = ( pn - m_circle.Center ).SquaredEuclideanNorm();
83
84 if( dist_sq == 0 || dist_sq < SEG::Square( minDist ) )
85 {
86 if( aLocation )
87 {
88 if( std::vector<VECTOR2I> pts = m_circle.Intersect( aSeg );
89 !pts.empty() && dist_sq == 0 )
90 {
91 *aLocation = m_circle.Intersect( aSeg )[0];
92 }
93 else
94 {
95 *aLocation = pn;
96 }
97 }
98
99 if( aActual )
100 *aActual = std::max( 0, (int) sqrt( dist_sq ) - m_circle.Radius );
101
102 return true;
103 }
104
105 return false;
106 }
107
108 void SetRadius( int aRadius )
109 {
110 m_circle.Radius = aRadius;
111 }
112
113 void SetCenter( const VECTOR2I& aCenter )
114 {
115 m_circle.Center = aCenter;
116 }
117
118 int GetRadius() const
119 {
120 return m_circle.Radius;
121 }
122
123 const VECTOR2I GetCenter() const
124 {
125 return m_circle.Center;
126 }
127
128 const CIRCLE GetCircle() const
129 {
130 return m_circle;
131 }
132
133 void Move( const VECTOR2I& aVector ) override
134 {
135 m_circle.Center += aVector;
136 }
137
138 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
139 {
140 RotatePoint( m_circle.Center, aCenter, aAngle );
141 }
142
143 bool IsSolid() const override
144 {
145 return true;
146 }
147
148 virtual const std::string Format( bool aCplusPlus = true ) const override;
149
150 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
151 ERROR_LOC aErrorLoc ) const override;
152
153private:
155};
156
157#endif
BOX2< VECTOR2I > BOX2I
Definition: box2.h:877
Represent basic circle geometry with utility geometry functions.
Definition: circle.h:33
VECTOR2I Center
Public to make access simpler.
Definition: circle.h:128
int Radius
Public to make access simpler.
Definition: circle.h:127
std::vector< VECTOR2I > Intersect(const CIRCLE &aCircle) const
Compute the intersection points between this circle and aCircle.
Definition: circle.cpp:221
Definition: seg.h:42
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:269
static SEG::ecoord Square(int a)
Definition: seg.h:123
CIRCLE m_circle
Definition: shape_circle.h:154
bool IsSolid() const override
Definition: shape_circle.h:143
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Definition: shape_circle.h:138
const CIRCLE GetCircle() const
Definition: shape_circle.h:128
SHAPE_CIRCLE(const SHAPE_CIRCLE &aOther)
Definition: shape_circle.h:55
SHAPE_CIRCLE(const VECTOR2I &aCenter, int aRadius)
Definition: shape_circle.h:45
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,...
Definition: shape_circle.h:77
int GetRadius() const
Definition: shape_circle.h:118
const VECTOR2I GetCenter() const
Definition: shape_circle.h:123
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.
SHAPE_CIRCLE(const CIRCLE &aCircle)
Definition: shape_circle.h:50
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_circle.h:70
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition: shape_circle.h:63
void SetRadius(int aRadius)
Definition: shape_circle.h:108
virtual const std::string Format(bool aCplusPlus=true) const override
SHAPE_CIRCLE & operator=(const SHAPE_CIRCLE &)=default
void Move(const VECTOR2I &aVector) override
Definition: shape_circle.h:133
void SetCenter(const VECTOR2I &aCenter)
Definition: shape_circle.h:113
Represent a set of closed polygons.
An abstract shape on 2D plane.
Definition: shape.h:126
VECTOR2I::extended_type ecoord
Definition: shape.h:284
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
@ SH_CIRCLE
circle
Definition: shape.h:50
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:228