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 (C) 2021-2022 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>
33#include <math/box2.h>
34#include <math/vector2d.h>
35#include <trigo.h>
36
37class SHAPE_RECT : public SHAPE
38{
39public:
44 SHAPE( SH_RECT ),
45 m_w( 0 ),
46 m_h( 0 )
47 {}
48
52 SHAPE_RECT( const BOX2I& aBox ) :
53 SHAPE( SH_RECT ),
54 m_p0( aBox.GetPosition() ),
55 m_w( aBox.GetWidth() ),
56 m_h( aBox.GetHeight() )
57 {}
58
62 SHAPE_RECT( int aX0, int aY0, int aW, int aH ) :
63 SHAPE( SH_RECT ),
64 m_p0( aX0, aY0 ),
65 m_w( aW ),
66 m_h( aH )
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 {}
78
82 SHAPE_RECT( const VECTOR2I& aP0, const VECTOR2I& aP1 ) :
83 SHAPE( SH_RECT ),
84 m_p0( aP0 ),
85 m_w( aP1.x - aP0.x ),
86 m_h( aP1.y - aP0.y )
87 {}
88
89 SHAPE_RECT( const SHAPE_RECT& aOther ) :
90 SHAPE( SH_RECT ),
91 m_p0( aOther.m_p0 ),
92 m_w( aOther.m_w ),
93 m_h( aOther.m_h )
94 {};
95
96 SHAPE* Clone() const override
97 {
98 return new SHAPE_RECT( *this );
99 }
100
102 const BOX2I BBox( int aClearance = 0 ) const override
103 {
104 BOX2I bbox( VECTOR2I( m_p0.x - aClearance, m_p0.y - aClearance ),
105 VECTOR2I( m_w + 2 * aClearance, m_h + 2 * aClearance ) );
106 return bbox;
107 }
108
113 SHAPE_RECT GetInflated( int aOffset ) const
114 {
115 return SHAPE_RECT{
116 m_p0 - VECTOR2I( aOffset, aOffset ),
117 m_w + 2 * aOffset,
118 m_h + 2 * aOffset,
119 };
120 }
121
127 int Diagonal() const
128 {
129 return VECTOR2I( m_w, m_h ).EuclideanNorm();
130 }
131
132 int MajorDimension() const
133 {
134 return std::max( m_w, m_h );
135 }
136
137 int MinorDimension() const
138 {
139 return std::min( m_w, m_h );
140 }
141
142 bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
143 {
144 return SHAPE::Collide( aShape, aClearance, aMTV );
145 }
146
147 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
148 VECTOR2I* aLocation = nullptr ) const override
149 {
150 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
151 }
152
154 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
155 VECTOR2I* aLocation = nullptr ) const override;
156
160 const VECTOR2I& GetPosition() const
161 {
162 return m_p0;
163 }
164
168 const VECTOR2I GetSize() const
169 {
170 return VECTOR2I( m_w, m_h );
171 }
172
176 int GetWidth() const
177 {
178 return m_w;
179 }
180
184 int GetHeight() const
185 {
186 return m_h;
187 }
188
189 void Move( const VECTOR2I& aVector ) override
190 {
191 m_p0 += aVector;
192 }
193
199 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
200 {
201 RotatePoint( m_p0, aCenter, aAngle );
202
203 if( abs( aAngle.Sin() ) == 1 )
204 std::swap( m_h, m_w );
205 }
206
207 bool IsSolid() const override
208 {
209 return true;
210 }
211
213 {
215 rv.Append( m_p0 );
216 rv.Append( m_p0.x, m_p0.y + m_h );
217 rv.Append( m_p0.x + m_w, m_p0.y + m_h );
218 rv.Append( m_p0.x + m_w, m_p0.y );
219 rv.Append( m_p0 );
220 rv.SetClosed( true );
221 return rv;
222 }
223
224 virtual const std::string Format( bool aCplusPlus = true ) const override;
225
226 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
227 ERROR_LOC aErrorLoc ) const override;
228
229private:
231 int m_w;
232 int m_h;
233};
234
235#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...
Definition: approximation.h:32
double Sin() const
Definition: eda_angle.h:170
Definition: seg.h:42
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
Represent a set of closed polygons.
void Move(const VECTOR2I &aVector) override
Definition: shape_rect.h:189
int m_h
Height.
Definition: shape_rect.h:232
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.
Definition: shape_rect.cpp:123
virtual const std::string Format(bool aCplusPlus=true) const override
Definition: shape_rect.cpp:105
SHAPE_RECT(const VECTOR2I &aP0, const VECTOR2I &aP1)
Create by two corners.
Definition: shape_rect.h:82
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:142
VECTOR2I m_p0
Top-left corner.
Definition: shape_rect.h:230
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:212
int Diagonal() const
Return length of the diagonal of the rectangle.
Definition: shape_rect.h:127
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition: shape_rect.h:96
int m_w
Width.
Definition: shape_rect.h:231
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:102
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:137
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:199
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:113
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition: shape_rect.h:147
SHAPE_RECT(const BOX2I &aBox)
Create a rectangle defined by a BOX2.
Definition: shape_rect.h:52
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:160
const VECTOR2I GetSize() const
Definition: shape_rect.h:168
int GetWidth() const
Definition: shape_rect.h:176
int GetHeight() const
Definition: shape_rect.h:184
int MajorDimension() const
Definition: shape_rect.h:132
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:62
SHAPE_RECT()
Create an empty (0-sized) rectangle.
Definition: shape_rect.h:43
SHAPE_RECT(const SHAPE_RECT &aOther)
Definition: shape_rect.h:89
bool IsSolid() const override
Definition: shape_rect.h:207
An abstract shape on 2D plane.
Definition: shape.h:126
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
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:691