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 (C) 2015-2022 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
30bool SHAPE_RECT::Collide( const SEG& aSeg, int aClearance, int* aActual, VECTOR2I* aLocation ) const
31{
32 BOX2I bbox( BBox() );
33
34 if( bbox.Contains( aSeg.A ) )
35 {
36 if( aLocation )
37 *aLocation = aSeg.A;
38
39 if( aActual )
40 *aActual = 0;
41
42 return true;
43 }
44
45 if( bbox.Contains( aSeg.B ) )
46 {
47 if( aLocation )
48 *aLocation = aSeg.B;
49
50 if( aActual )
51 *aActual = 0;
52
53 return true;
54 }
55
56 VECTOR2I corners[] = { VECTOR2I( m_p0.x, m_p0.y ),
57 VECTOR2I( m_p0.x, m_p0.y + m_h ),
58 VECTOR2I( m_p0.x + m_w, m_p0.y + m_h ),
59 VECTOR2I( m_p0.x + m_w, m_p0.y ),
60 VECTOR2I( m_p0.x, m_p0.y ) };
61
62 SEG::ecoord closest_dist_sq = VECTOR2I::ECOORD_MAX;
63 VECTOR2I nearest;
64
65 for( int i = 0; i < 4; i++ )
66 {
67 SEG side( corners[i], corners[ i + 1] );
68 SEG::ecoord dist_sq = side.SquaredDistance( aSeg );
69
70 if( dist_sq < closest_dist_sq )
71 {
72 if ( aLocation )
73 {
74 nearest = side.NearestPoint( aSeg );
75 }
76
77 closest_dist_sq = dist_sq;
78 }
79 else if( aLocation && dist_sq == closest_dist_sq )
80 {
81 VECTOR2I near = side.NearestPoint( aSeg );
82
83 if( ( near - aSeg.A ).SquaredEuclideanNorm()
84 < ( nearest - aSeg.A ).SquaredEuclideanNorm() )
85 {
86 nearest = near;
87 }
88 }
89 }
90
91 if( closest_dist_sq == 0 || closest_dist_sq < SEG::Square( aClearance ) )
92 {
93 if( aActual )
94 *aActual = sqrt( closest_dist_sq );
95
96 if( aLocation )
97 *aLocation = nearest;
98
99 return true;
100 }
101
102 return false;
103}
104
105const std::string SHAPE_RECT::Format( bool aCplusPlus ) const
106{
107 std::stringstream ss;
108
109 ss << "SHAPE_RECT( ";
110 ss << m_p0.x;
111 ss << ", ";
112 ss << m_p0.y;
113 ss << ", ";
114 ss << m_w;
115 ss << ", ";
116 ss << m_h;
117 ss << ");";
118
119 return ss.str();
120}
121
122
124 ERROR_LOC aErrorLoc ) const
125{
126 int idx = aBuffer.NewOutline();
127 SHAPE_LINE_CHAIN& outline = aBuffer.Outline( idx );
128
129 outline.Append( m_p0 );
130 outline.Append( { m_p0.x + m_w, m_p0.y } );
131 outline.Append( { m_p0.x + m_w, m_p0.y + m_h } );
132 outline.Append( { m_p0.x, m_p0.y + m_h } );
133 outline.SetClosed( true );
134}
bool Contains(const Vec &aPoint) const
Definition: box2.h:158
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:75
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:269
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:199
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
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:109
VECTOR2I m_p0
Top-left corner.
Definition: shape_rect.h:197
int m_w
Width.
Definition: shape_rect.h:198
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:92
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:75
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588