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, see <https://www.gnu.org/licenses/>.
20 */
21
22
23#include <geometry/shape_rect.h>
25#include <geometry/roundrect.h>
26
27bool SHAPE_RECT::Collide( const SEG& aSeg, int aClearance, int* aActual, VECTOR2I* aLocation ) const
28{
29 if( m_radius > 0 )
30 {
31 SHAPE_LINE_CHAIN lineChain( Outline() );
32 return lineChain.Collide( aSeg, aClearance, aActual, aLocation );
33 }
34
35 BOX2I bbox( BBox() );
36
37 if( bbox.Contains( aSeg.A ) )
38 {
39 if( aLocation )
40 *aLocation = aSeg.A;
41
42 if( aActual )
43 *aActual = 0;
44
45 return true;
46 }
47
48 if( bbox.Contains( aSeg.B ) )
49 {
50 if( aLocation )
51 *aLocation = aSeg.B;
52
53 if( aActual )
54 *aActual = 0;
55
56 return true;
57 }
58
59 VECTOR2I corners[] = { VECTOR2I( m_p0.x, m_p0.y ),
60 VECTOR2I( m_p0.x, m_p0.y + m_h ),
61 VECTOR2I( m_p0.x + m_w, m_p0.y + m_h ),
62 VECTOR2I( m_p0.x + m_w, m_p0.y ),
63 VECTOR2I( m_p0.x, m_p0.y ) };
64
65 SEG::ecoord closest_dist_sq = VECTOR2I::ECOORD_MAX;
66 VECTOR2I nearest;
67
68 for( int i = 0; i < 4; i++ )
69 {
70 SEG side( corners[i], corners[ i + 1] );
71 SEG::ecoord dist_sq = side.SquaredDistance( aSeg );
72
73 if( dist_sq < closest_dist_sq )
74 {
75 if ( aLocation )
76 {
77 nearest = side.NearestPoint( aSeg );
78 }
79
80 closest_dist_sq = dist_sq;
81 }
82 else if( aLocation && dist_sq == closest_dist_sq )
83 {
84 VECTOR2I near = side.NearestPoint( aSeg );
85
86 if( ( near - aSeg.A ).SquaredEuclideanNorm()
87 < ( nearest - aSeg.A ).SquaredEuclideanNorm() )
88 {
89 nearest = near;
90 }
91 }
92 }
93
94 if( closest_dist_sq == 0 || closest_dist_sq < SEG::Square( aClearance ) )
95 {
96 if( aActual )
97 *aActual = sqrt( closest_dist_sq );
98
99 if( aLocation )
100 *aLocation = nearest;
101
102 return true;
103 }
104
105 return false;
106}
107
108const std::string SHAPE_RECT::Format( bool aCplusPlus ) const
109{
110 std::stringstream ss;
111
112 ss << "SHAPE_RECT( ";
113 ss << m_p0.x;
114 ss << ", ";
115 ss << m_p0.y;
116 ss << ", ";
117 ss << m_w;
118 ss << ", ";
119 ss << m_h;
120 ss << ", ";
121 ss << m_radius;
122 ss << ");";
123
124 return ss.str();
125}
126
127
128void SHAPE_RECT::TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError, ERROR_LOC aErrorLoc ) const
129{
130 if( m_radius > 0 )
131 {
132 ROUNDRECT rr( *this, m_radius );
133 rr.TransformToPolygon( aBuffer, aError );
134 return;
135 }
136
137 int idx = aBuffer.NewOutline();
138 SHAPE_LINE_CHAIN& outline = aBuffer.Outline( idx );
139
140 outline.Append( m_p0 );
141 outline.Append( { m_p0.x + m_w, m_p0.y } );
142 outline.Append( { m_p0.x + m_w, m_p0.y + m_h } );
143 outline.Append( { m_p0.x, m_p0.y + m_h } );
144 outline.SetClosed( true );
145}
146
147
149{
150 // TODO: we're DEPENDING on clients of this routine to use the actual arcs (if any)
151 // inserted into the SHAPE_LINE_CHAIN. They must NOT use the approximated segments
152 // because we don't know what IUScale to generate them in.
153
154 SHAPE_POLY_SET buffer;
156 return std::move( buffer.Outline( 0 ) );
157}
158
159
161{
162 // Ensure that the height and width are positive.
163 if( m_w < 0 )
164 {
165 m_w = -m_w;
166 m_p0.x -= m_w;
167 }
168
169 if( m_h < 0 )
170 {
171 m_h = -m_h;
172 m_p0.y -= m_h;
173 }
174}
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:918
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:164
A round rectangle shape, based on a rectangle and a radius.
Definition roundrect.h:32
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aMaxError) const
Get the polygonal representation of the roundrect.
Definition roundrect.cpp:79
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
ecoord SquaredDistance(const SEG &aSeg) const
Definition seg.cpp:76
VECTOR2I::extended_type ecoord
Definition seg.h:40
VECTOR2I B
Definition seg.h:46
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition seg.cpp:629
static SEG::ecoord Square(int a)
Definition seg.h:119
static int DefaultAccuracyForPCB()
Definition shape_arc.h:279
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:251
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: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 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
int m_radius
Corner radius.
Definition shape_rect.h:252
static constexpr extended_type ECOORD_MAX
Definition vector2d.h:72
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683