KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_rect.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 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef __SHAPE_RECT_H
28#define __SHAPE_RECT_H
29
30#include <geometry/seg.h>
31#include <geometry/shape.h>
34#include <math/box2.h>
35#include <math/vector2d.h>
36#include <trigo.h>
37
38class SHAPE_RECT : public SHAPE
39{
40public:
45 SHAPE( SH_RECT ),
46 m_w( 0 ),
47 m_h( 0 ),
48 m_radius( 0 )
49 {}
50
54 SHAPE_RECT( const BOX2I& aBox ) :
55 SHAPE( SH_RECT ),
56 m_p0( aBox.GetPosition() ),
57 m_w( aBox.GetWidth() ),
58 m_h( aBox.GetHeight() ),
59 m_radius( 0 )
60 {}
61
65 SHAPE_RECT( int aX0, int aY0, int aW, int aH ) :
66 SHAPE( SH_RECT ),
67 m_p0( aX0, aY0 ),
68 m_w( aW ),
69 m_h( aH ),
70 m_radius( 0 )
71 {}
72
76 SHAPE_RECT( const VECTOR2I& aP0, int aW, int aH ) :
77 SHAPE( SH_RECT ),
78 m_p0( aP0 ),
79 m_w( aW ),
80 m_h( aH ),
81 m_radius( 0 )
82 {}
83
87 SHAPE_RECT( const VECTOR2I& aP0, const VECTOR2I& aP1 ) :
88 SHAPE( SH_RECT ),
89 m_p0( aP0 ),
90 m_w( aP1.x - aP0.x ),
91 m_h( aP1.y - aP0.y ),
92 m_radius( 0 )
93 {}
94
95 SHAPE_RECT( const SHAPE_RECT& aOther ) :
96 SHAPE( SH_RECT ),
97 m_p0( aOther.m_p0 ),
98 m_w( aOther.m_w ),
99 m_h( aOther.m_h ),
100 m_radius( aOther.m_radius )
101 {};
102
103 SHAPE* Clone() const override
104 {
105 return new SHAPE_RECT( *this );
106 }
107
109 const BOX2I BBox( int aClearance = 0 ) const override
110 {
111 BOX2I bbox( VECTOR2I( m_p0.x - aClearance, m_p0.y - aClearance ),
112 VECTOR2I( m_w + 2 * aClearance, m_h + 2 * aClearance ) );
113 return bbox;
114 }
115
120 SHAPE_RECT GetInflated( int aOffset ) const
121 {
122 SHAPE_RECT r{
123 m_p0 - VECTOR2I( aOffset, aOffset ),
124 m_w + 2 * aOffset,
125 m_h + 2 * aOffset,
126 };
127 r.SetRadius( m_radius + aOffset );
128 return r;
129 }
130
136 int Diagonal() const
137 {
138 return VECTOR2I( m_w, m_h ).EuclideanNorm();
139 }
140
141 int MajorDimension() const
142 {
143 return std::max( m_w, m_h );
144 }
145
146 int MinorDimension() const
147 {
148 return std::min( m_w, m_h );
149 }
150
151 bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
152 {
153 return SHAPE::Collide( aShape, aClearance, aMTV );
154 }
155
156 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
157 VECTOR2I* aLocation = nullptr ) const override
158 {
159 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
160 }
161
163 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
164 VECTOR2I* aLocation = nullptr ) const override;
165
169 const VECTOR2I& GetPosition() const
170 {
171 return m_p0;
172 }
173
177 const VECTOR2I GetSize() const
178 {
179 return VECTOR2I( m_w, m_h );
180 }
181
185 int GetWidth() const override
186 {
187 return m_w;
188 }
189
193 int GetHeight() const
194 {
195 return m_h;
196 }
197
201 int GetRadius() const
202 {
203 return m_radius;
204 }
205
206 void SetRadius( int aRadius )
207 {
208 m_radius = aRadius;
209 }
210
211 void Move( const VECTOR2I& aVector ) override
212 {
213 m_p0 += aVector;
214 }
215
221 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
222 {
223 RotatePoint( m_p0, aCenter, aAngle );
224
225 if( abs( aAngle.Sin() ) == 1 )
226 std::swap( m_h, m_w );
227 }
228
229 bool IsSolid() const override
230 {
231 return true;
232 }
233
234 const SHAPE_LINE_CHAIN Outline() const;
235
236 virtual const std::string Format( bool aCplusPlus = true ) const override;
237
238 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
239 ERROR_LOC aErrorLoc ) const override;
240
244 void Normalize();
245
246
247private:
249 int m_w;
250 int m_h;
252};
253
254#endif // __SHAPE_RECT_H
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
double Sin() const
Definition eda_angle.h:178
Definition seg.h:42
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.
void Move(const VECTOR2I &aVector) override
Definition shape_rect.h:211
int m_h
Height.
Definition shape_rect.h:250
int GetWidth() const override
Definition shape_rect.h:185
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.
virtual const std::string Format(bool aCplusPlus=true) const override
SHAPE_RECT(const VECTOR2I &aP0, const VECTOR2I &aP1)
Create by two corners.
Definition shape_rect.h:87
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
Definition shape_rect.h:151
VECTOR2I m_p0
Top-left corner.
Definition shape_rect.h:248
const SHAPE_LINE_CHAIN Outline() const
void Normalize()
Ensure that the height and width are positive.
int Diagonal() const
Return length of the diagonal of the rectangle.
Definition shape_rect.h:136
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition shape_rect.h:103
int m_w
Width.
Definition shape_rect.h:249
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition shape_rect.h:109
SHAPE_RECT(const VECTOR2I &aP0, int aW, int aH)
Create a rectangle defined by top-left corner aP0, width aW and height aH.
Definition shape_rect.h:76
int MinorDimension() const
Definition shape_rect.h:146
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
This function has limited utility for SHAPE_RECT as non-cartesian rotations will distort the rectangl...
Definition shape_rect.h:221
SHAPE_RECT GetInflated(int aOffset) const
Return a rectangle that is larger by aOffset in all directions, but still centered on the original re...
Definition shape_rect.h:120
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition shape_rect.h:156
SHAPE_RECT(const BOX2I &aBox)
Create a rectangle defined by a BOX2.
Definition shape_rect.h:54
const VECTOR2I & GetPosition() const
Definition shape_rect.h:169
int m_radius
Corner radius.
Definition shape_rect.h:251
const VECTOR2I GetSize() const
Definition shape_rect.h:177
int GetRadius() const
Definition shape_rect.h:201
void SetRadius(int aRadius)
Definition shape_rect.h:206
int GetHeight() const
Definition shape_rect.h:193
int MajorDimension() const
Definition shape_rect.h:141
SHAPE_RECT(int aX0, int aY0, int aW, int aH)
Create a rectangle defined by top-left corner (aX0, aY0), width aW and height aH.
Definition shape_rect.h:65
SHAPE_RECT()
Create an empty (0-sized) rectangle.
Definition shape_rect.h:44
SHAPE_RECT(const SHAPE_RECT &aOther)
Definition shape_rect.h:95
bool IsSolid() const override
Definition shape_rect.h:229
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
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:283
@ SH_RECT
axis-aligned rectangle
Definition shape.h:47
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:229
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695