KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_rect.cpp
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) 2015 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
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
27#include <geometry/shape_rect.h>
29#include <geometry/roundrect.h>
30
31bool SHAPE_RECT::Collide( const SEG& aSeg, int aClearance, int* aActual, VECTOR2I* aLocation ) const
32{
33 BOX2I bbox( BBox() );
34
35 if( bbox.Contains( aSeg.A ) )
36 {
37 if( aLocation )
38 *aLocation = aSeg.A;
39
40 if( aActual )
41 *aActual = 0;
42
43 return true;
44 }
45
46 if( bbox.Contains( aSeg.B ) )
47 {
48 if( aLocation )
49 *aLocation = aSeg.B;
50
51 if( aActual )
52 *aActual = 0;
53
54 return true;
55 }
56
57 VECTOR2I corners[] = { VECTOR2I( m_p0.x, m_p0.y ),
58 VECTOR2I( m_p0.x, m_p0.y + m_h ),
59 VECTOR2I( m_p0.x + m_w, m_p0.y + m_h ),
60 VECTOR2I( m_p0.x + m_w, m_p0.y ),
61 VECTOR2I( m_p0.x, m_p0.y ) };
62
63 SEG::ecoord closest_dist_sq = VECTOR2I::ECOORD_MAX;
64 VECTOR2I nearest;
65
66 for( int i = 0; i < 4; i++ )
67 {
68 SEG side( corners[i], corners[ i + 1] );
69 SEG::ecoord dist_sq = side.SquaredDistance( aSeg );
70
71 if( dist_sq < closest_dist_sq )
72 {
73 if ( aLocation )
74 {
75 nearest = side.NearestPoint( aSeg );
76 }
77
78 closest_dist_sq = dist_sq;
79 }
80 else if( aLocation && dist_sq == closest_dist_sq )
81 {
82 VECTOR2I near = side.NearestPoint( aSeg );
83
84 if( ( near - aSeg.A ).SquaredEuclideanNorm()
85 < ( nearest - aSeg.A ).SquaredEuclideanNorm() )
86 {
87 nearest = near;
88 }
89 }
90 }
91
92 if( closest_dist_sq == 0 || closest_dist_sq < SEG::Square( aClearance ) )
93 {
94 if( aActual )
95 *aActual = sqrt( closest_dist_sq );
96
97 if( aLocation )
98 *aLocation = nearest;
99
100 return true;
101 }
102
103 return false;
104}
105
106const std::string SHAPE_RECT::Format( bool aCplusPlus ) const
107{
108 std::stringstream ss;
109
110 ss << "SHAPE_RECT( ";
111 ss << m_p0.x;
112 ss << ", ";
113 ss << m_p0.y;
114 ss << ", ";
115 ss << m_w;
116 ss << ", ";
117 ss << m_h;
118 ss << ", ";
119 ss << m_radius;
120 ss << ");";
121
122 return ss.str();
123}
124
125
127 ERROR_LOC aErrorLoc ) const
128{
129 if( m_radius > 0 )
130 {
131 ROUNDRECT rr( *this, m_radius );
132 rr.TransformToPolygon( aBuffer );
133 return;
134 }
135
136 int idx = aBuffer.NewOutline();
137 SHAPE_LINE_CHAIN& outline = aBuffer.Outline( idx );
138
139 outline.Append( m_p0 );
140 outline.Append( { m_p0.x + m_w, m_p0.y } );
141 outline.Append( { m_p0.x + m_w, m_p0.y + m_h } );
142 outline.Append( { m_p0.x, m_p0.y + m_h } );
143 outline.SetClosed( true );
144}
145
147{
148 if( m_radius > 0 )
149 {
150 SHAPE_POLY_SET poly;
151 ROUNDRECT rr( *this, m_radius );
152 rr.TransformToPolygon( poly );
153 return poly.Outline( 0 );
154 }
155
157 rv.Append( m_p0 );
158 rv.Append( m_p0.x, m_p0.y + m_h );
159 rv.Append( m_p0.x + m_w, m_p0.y + m_h );
160 rv.Append( m_p0.x + m_w, m_p0.y );
161 rv.Append( m_p0 );
162 rv.SetClosed( true );
163 return rv;
164}
165
166
168{
169 // Ensure that the height and width are positive.
170 if( m_w < 0 )
171 {
172 m_w = -m_w;
173 m_p0.x -= m_w;
174 }
175
176 if( m_h < 0 )
177 {
178 m_h = -m_h;
179 m_p0.y -= m_h;
180 }
181}
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
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:168
A round rectangle shape, based on a rectangle and a radius.
Definition roundrect.h:36
void TransformToPolygon(SHAPE_POLY_SET &aBuffer) const
Get the polygonal representation of the roundrect.
Definition roundrect.cpp:81
Definition seg.h:42
VECTOR2I A
Definition seg.h:49
ecoord SquaredDistance(const SEG &aSeg) const
Definition seg.cpp:80
VECTOR2I::extended_type ecoord
Definition seg.h:44
VECTOR2I B
Definition seg.h:50
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition seg.cpp:604
static SEG::ecoord Square(int a)
Definition seg.h:123
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.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
int m_h
Height.
Definition shape_rect.h:250
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
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 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
int m_radius
Corner radius.
Definition shape_rect.h:251
static constexpr extended_type ECOORD_MAX
Definition vector2d.h:76
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695