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 if( m_radius > 0 )
34 {
35 SHAPE_LINE_CHAIN lineChain( Outline() );
36 return lineChain.Collide( aSeg, aClearance, aActual, aLocation );
37 }
38
39 BOX2I bbox( BBox() );
40
41 if( bbox.Contains( aSeg.A ) )
42 {
43 if( aLocation )
44 *aLocation = aSeg.A;
45
46 if( aActual )
47 *aActual = 0;
48
49 return true;
50 }
51
52 if( bbox.Contains( aSeg.B ) )
53 {
54 if( aLocation )
55 *aLocation = aSeg.B;
56
57 if( aActual )
58 *aActual = 0;
59
60 return true;
61 }
62
63 VECTOR2I corners[] = { VECTOR2I( m_p0.x, m_p0.y ),
64 VECTOR2I( m_p0.x, m_p0.y + m_h ),
65 VECTOR2I( m_p0.x + m_w, m_p0.y + m_h ),
66 VECTOR2I( m_p0.x + m_w, m_p0.y ),
67 VECTOR2I( m_p0.x, m_p0.y ) };
68
69 SEG::ecoord closest_dist_sq = VECTOR2I::ECOORD_MAX;
70 VECTOR2I nearest;
71
72 for( int i = 0; i < 4; i++ )
73 {
74 SEG side( corners[i], corners[ i + 1] );
75 SEG::ecoord dist_sq = side.SquaredDistance( aSeg );
76
77 if( dist_sq < closest_dist_sq )
78 {
79 if ( aLocation )
80 {
81 nearest = side.NearestPoint( aSeg );
82 }
83
84 closest_dist_sq = dist_sq;
85 }
86 else if( aLocation && dist_sq == closest_dist_sq )
87 {
88 VECTOR2I near = side.NearestPoint( aSeg );
89
90 if( ( near - aSeg.A ).SquaredEuclideanNorm()
91 < ( nearest - aSeg.A ).SquaredEuclideanNorm() )
92 {
93 nearest = near;
94 }
95 }
96 }
97
98 if( closest_dist_sq == 0 || closest_dist_sq < SEG::Square( aClearance ) )
99 {
100 if( aActual )
101 *aActual = sqrt( closest_dist_sq );
102
103 if( aLocation )
104 *aLocation = nearest;
105
106 return true;
107 }
108
109 return false;
110}
111
112const std::string SHAPE_RECT::Format( bool aCplusPlus ) const
113{
114 std::stringstream ss;
115
116 ss << "SHAPE_RECT( ";
117 ss << m_p0.x;
118 ss << ", ";
119 ss << m_p0.y;
120 ss << ", ";
121 ss << m_w;
122 ss << ", ";
123 ss << m_h;
124 ss << ", ";
125 ss << m_radius;
126 ss << ");";
127
128 return ss.str();
129}
130
131
132void SHAPE_RECT::TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError, ERROR_LOC aErrorLoc ) const
133{
134 if( m_radius > 0 )
135 {
136 ROUNDRECT rr( *this, m_radius );
137 rr.TransformToPolygon( aBuffer, aError );
138 return;
139 }
140
141 int idx = aBuffer.NewOutline();
142 SHAPE_LINE_CHAIN& outline = aBuffer.Outline( idx );
143
144 outline.Append( m_p0 );
145 outline.Append( { m_p0.x + m_w, m_p0.y } );
146 outline.Append( { m_p0.x + m_w, m_p0.y + m_h } );
147 outline.Append( { m_p0.x, m_p0.y + m_h } );
148 outline.SetClosed( true );
149}
150
151
153{
154 // TODO: we're DEPENDING on clients of this routine to use the actual arcs (if any)
155 // inserted into the SHAPE_LINE_CHAIN. They must NOT use the approximated segments
156 // because we don't know what IUScale to generate them in.
157
158 SHAPE_POLY_SET buffer;
160 return std::move( buffer.Outline( 0 ) );
161}
162
163
165{
166 // Ensure that the height and width are positive.
167 if( m_w < 0 )
168 {
169 m_w = -m_w;
170 m_p0.x -= m_w;
171 }
172
173 if( m_h < 0 )
174 {
175 m_h = -m_h;
176 m_p0.y -= m_h;
177 }
178}
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
@ ERROR_INSIDE
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, int aMaxError) 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
static int DefaultAccuracyForPCB()
Definition shape_arc.h:283
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.
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if point aP lies closer to us than aClearance.
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