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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef __SHAPE_RECT_H
24#define __SHAPE_RECT_H
25
26#include <geometry/seg.h>
27#include <geometry/shape.h>
30#include <math/box2.h>
31#include <math/vector2d.h>
32#include <trigo.h>
33
34class SHAPE_RECT : public SHAPE
35{
36public:
41 SHAPE( SH_RECT ),
42 m_w( 0 ),
43 m_h( 0 ),
44 m_radius( 0 )
45 {}
46
50 SHAPE_RECT( const BOX2I& aBox ) :
51 SHAPE( SH_RECT ),
52 m_p0( aBox.GetPosition() ),
53 m_w( aBox.GetWidth() ),
54 m_h( aBox.GetHeight() ),
55 m_radius( 0 )
56 {}
57
61 SHAPE_RECT( int aX0, int aY0, int aW, int aH ) :
62 SHAPE( SH_RECT ),
63 m_p0( aX0, aY0 ),
64 m_w( aW ),
65 m_h( aH ),
66 m_radius( 0 )
67 {}
68
72 SHAPE_RECT( const VECTOR2I& aP0, int aW, int aH ) :
73 SHAPE( SH_RECT ),
74 m_p0( aP0 ),
75 m_w( aW ),
76 m_h( aH ),
77 m_radius( 0 )
78 {}
79
83 SHAPE_RECT( const VECTOR2I& aP0, const VECTOR2I& aP1 ) :
84 SHAPE( SH_RECT ),
85 m_p0( aP0 ),
86 m_w( aP1.x - aP0.x ),
87 m_h( aP1.y - aP0.y ),
88 m_radius( 0 )
89 {}
90
91 SHAPE_RECT( const SHAPE_RECT& aOther ) :
92 SHAPE( SH_RECT ),
93 m_p0( aOther.m_p0 ),
94 m_w( aOther.m_w ),
95 m_h( aOther.m_h ),
96 m_radius( aOther.m_radius )
97 {};
98
99 SHAPE* Clone() const override
100 {
101 return new SHAPE_RECT( *this );
102 }
103
105 const BOX2I BBox( int aClearance = 0 ) const override
106 {
107 BOX2I bbox( VECTOR2I( m_p0.x - aClearance, m_p0.y - aClearance ),
108 VECTOR2I( m_w + 2 * aClearance, m_h + 2 * aClearance ) );
109 return bbox;
110 }
111
116 SHAPE_RECT GetInflated( int aOffset ) const
117 {
118 SHAPE_RECT r{
119 m_p0 - VECTOR2I( aOffset, aOffset ),
120 m_w + 2 * aOffset,
121 m_h + 2 * aOffset,
122 };
123 r.SetRadius( m_radius + aOffset );
124 return r;
125 }
126
132 int Diagonal() const
133 {
134 return VECTOR2I( m_w, m_h ).EuclideanNorm();
135 }
136
137 int MajorDimension() const
138 {
139 return std::max( m_w, m_h );
140 }
141
142 int MinorDimension() const
143 {
144 return std::min( m_w, m_h );
145 }
146
147 bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
148 {
149 return SHAPE::Collide( aShape, aClearance, aMTV );
150 }
151
152 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
153 VECTOR2I* aLocation = nullptr ) const override
154 {
155 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
156 }
157
159 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
160 VECTOR2I* aLocation = nullptr ) const override;
161
165 const VECTOR2I& GetPosition() const
166 {
167 return m_p0;
168 }
169
173 const VECTOR2I GetSize() const
174 {
175 return VECTOR2I( m_w, m_h );
176 }
177
181 int GetWidth() const override
182 {
183 return m_w;
184 }
185
189 int GetHeight() const
190 {
191 return m_h;
192 }
193
197 int GetRadius() const
198 {
199 return m_radius;
200 }
201
202 void SetRadius( int aRadius )
203 {
204 m_radius = aRadius;
205 }
206
207 void Move( const VECTOR2I& aVector ) override
208 {
209 m_p0 += aVector;
210 }
211
217 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
218 {
219 VECTOR2I c1 = m_p0;
220 VECTOR2I c2 = m_p0 + VECTOR2I( m_w, m_h );
221
222 RotatePoint( c1, aCenter, aAngle );
223 RotatePoint( c2, aCenter, aAngle );
224
225 m_p0 = VECTOR2I( std::min( c1.x, c2.x ), std::min( c1.y, c2.y ) );
226 m_w = std::abs( c2.x - c1.x );
227 m_h = std::abs( c2.y - c1.y );
228 }
229
230 bool IsSolid() const override
231 {
232 return true;
233 }
234
235 const SHAPE_LINE_CHAIN Outline() const;
236
237 virtual const std::string Format( bool aCplusPlus = true ) const override;
238
239 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
240 ERROR_LOC aErrorLoc ) const override;
241
245 void Normalize();
246
247
248private:
250 int m_w;
251 int m_h;
253};
254
255#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:918
Definition seg.h:38
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:207
int m_h
Height.
Definition shape_rect.h:251
int GetWidth() const override
Definition shape_rect.h:181
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:83
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:147
VECTOR2I m_p0
Top-left corner.
Definition shape_rect.h:249
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:132
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition shape_rect.h:99
int m_w
Width.
Definition shape_rect.h:250
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:105
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:72
int MinorDimension() const
Definition shape_rect.h:142
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:217
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:116
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition shape_rect.h:152
SHAPE_RECT(const BOX2I &aBox)
Create a rectangle defined by a BOX2.
Definition shape_rect.h:50
const VECTOR2I & GetPosition() const
Definition shape_rect.h:165
int m_radius
Corner radius.
Definition shape_rect.h:252
const VECTOR2I GetSize() const
Definition shape_rect.h:173
int GetRadius() const
Definition shape_rect.h:197
void SetRadius(int aRadius)
Definition shape_rect.h:202
int GetHeight() const
Definition shape_rect.h:189
int MajorDimension() const
Definition shape_rect.h:137
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:61
SHAPE_RECT()
Create an empty (0-sized) rectangle.
Definition shape_rect.h:40
SHAPE_RECT(const SHAPE_RECT &aOther)
Definition shape_rect.h:91
bool IsSolid() const override
Definition shape_rect.h:230
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
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:279
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
@ SH_RECT
axis-aligned rectangle
Definition shape.h:43
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:225
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683