KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape.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 * @author Tomasz Wlostowski <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25
26#include <geometry/shape.h>
27#include <geometry/shape_arc.h>
30#include <geometry/shape_rect.h>
35
37
38
39bool SHAPE::Parse( std::stringstream& aStream )
40{
41 assert( false );
42 return false;
43}
44
45
46const std::string SHAPE::Format( bool aCplusPlus ) const
47{
48 std::stringstream ss;
49 ss << "shape " << m_type;
50 return ss.str();
51}
52
53
54int SHAPE::GetClearance( const SHAPE* aOther ) const
55{
56 int actual_clearance = std::numeric_limits<int>::max();
57 std::vector<const SHAPE*> a_shapes;
58 std::vector<const SHAPE*> b_shapes;
59
64 if( Type() == SHAPE_TYPE::SH_POLY_SET )
65 {
66 const SHAPE_POLY_SET* polySet = static_cast<const SHAPE_POLY_SET*>( this );
67 if( polySet->OutlineCount() > 0 )
68 a_shapes.push_back( &polySet->COutline( 0 ) );
69 }
70 else
71 {
72 GetIndexableSubshapes( a_shapes );
73 }
74
75 if( aOther->Type() == SHAPE_TYPE::SH_POLY_SET )
76 {
77 const SHAPE_POLY_SET* polySet = static_cast<const SHAPE_POLY_SET*>( aOther );
78 if( polySet->OutlineCount() > 0 )
79 b_shapes.push_back( &polySet->COutline( 0 ) );
80 }
81 else
82 {
83 aOther->GetIndexableSubshapes( b_shapes );
84 }
85
86 if( GetIndexableSubshapeCount() == 0 )
87 a_shapes.push_back( this );
88
89 if( aOther->GetIndexableSubshapeCount() == 0 )
90 b_shapes.push_back( aOther );
91
92 for( const SHAPE* a : a_shapes )
93 {
94 for( const SHAPE* b : b_shapes )
95 {
96 int temp_dist = 0;
97 a->Collide( b, std::numeric_limits<int>::max() / 2, &temp_dist );
98
99 if( temp_dist < actual_clearance )
100 actual_clearance = temp_dist;
101 }
102 }
103
104 return actual_clearance;
105}
106
107
108int SHAPE::Distance( const VECTOR2I& aP ) const
109{
110 return sqrt( SquaredDistance( aP, false ) );
111}
112
113
114SEG::ecoord SHAPE::SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly ) const
115{
116 SHAPE_POLY_SET buffer;
117 TransformToPolygon( buffer, 0, ERROR_INSIDE );
118
119 if( buffer.OutlineCount() < 1 )
121
122 return buffer.COutline( 0 ).SquaredDistance( aP, aOutlineOnly );
123}
124
125
126bool SHAPE::PointInside( const VECTOR2I& aPt, int aAccuracy, bool aUseBBoxCache ) const
127{
128 SHAPE_POLY_SET buffer;
129 TransformToPolygon( buffer, aAccuracy, ERROR_INSIDE );
130
131 if( buffer.OutlineCount() < 1 )
132 return false;
133
134 return buffer.COutline( 0 ).PointInside( aPt, aAccuracy, aUseBBoxCache );
135}
136
137
139 ERROR_LOC aErrorLoc ) const
140{
141 aBuffer.AddOutline( m_points );
142}
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
@ ERROR_INSIDE
Definition: approximation.h:34
VECTOR2I::extended_type ecoord
Definition: seg.h:44
virtual size_t GetIndexableSubshapeCount() const
Definition: shape.h:113
SHAPE_TYPE m_type
< type of our shape
Definition: shape.h:119
virtual void GetIndexableSubshapes(std::vector< const SHAPE * > &aSubshapes) const
Definition: shape.h:115
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:98
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const override
Represent a set of closed polygons.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
int OutlineCount() const
Return the number of outlines in the set.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
SHAPE_LINE_CHAIN m_points
Definition: shape_simple.h:188
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.cpp:138
An abstract shape on 2D plane.
Definition: shape.h:126
int GetClearance(const SHAPE *aOther) const
Return the actual minimum distance between two shapes.
Definition: shape.cpp:54
virtual void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const =0
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
virtual bool Parse(std::stringstream &aStream)
Definition: shape.cpp:39
virtual bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Check if point aP lies inside a closed shape.
Definition: shape.cpp:126
virtual const std::string Format(bool aCplusPlus=true) const
Definition: shape.cpp:46
virtual int Distance(const VECTOR2I &aP) const
Returns the minimum distance from a given point to this shape.
Definition: shape.cpp:108
virtual SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Definition: shape.cpp:114
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:76