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 * 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.h>
28#include <geometry/shape_arc.h>
31#include <geometry/shape_rect.h>
36
38
39
40bool SHAPE::Parse( std::stringstream& aStream )
41{
42 assert( false );
43 return false;
44}
45
46
47const std::string SHAPE::Format( bool aCplusPlus ) const
48{
49 std::stringstream ss;
50 ss << "shape " << m_type;
51 return ss.str();
52}
53
54
55int SHAPE::GetClearance( const SHAPE* aOther ) const
56{
57 int actual_clearance = std::numeric_limits<int>::max();
58 std::vector<const SHAPE*> a_shapes;
59 std::vector<const SHAPE*> b_shapes;
60
65 if( Type() == SHAPE_TYPE::SH_POLY_SET )
66 {
67 const SHAPE_POLY_SET* polySet = static_cast<const SHAPE_POLY_SET*>( this );
68 if( polySet->OutlineCount() > 0 )
69 a_shapes.push_back( &polySet->COutline( 0 ) );
70 }
71 else
72 {
73 GetIndexableSubshapes( a_shapes );
74 }
75
76 if( aOther->Type() == SHAPE_TYPE::SH_POLY_SET )
77 {
78 const SHAPE_POLY_SET* polySet = static_cast<const SHAPE_POLY_SET*>( aOther );
79 if( polySet->OutlineCount() > 0 )
80 b_shapes.push_back( &polySet->COutline( 0 ) );
81 }
82 else
83 {
84 aOther->GetIndexableSubshapes( b_shapes );
85 }
86
87 if( GetIndexableSubshapeCount() == 0 )
88 a_shapes.push_back( this );
89
90 if( aOther->GetIndexableSubshapeCount() == 0 )
91 b_shapes.push_back( aOther );
92
93 for( const SHAPE* a : a_shapes )
94 {
95 for( const SHAPE* b : b_shapes )
96 {
97 int temp_dist = 0;
98 a->Collide( b, std::numeric_limits<int>::max() / 2, &temp_dist );
99
100 if( temp_dist < actual_clearance )
101 actual_clearance = temp_dist;
102 }
103 }
104
105 return actual_clearance;
106}
107
108
109int SHAPE::Distance( const VECTOR2I& aP ) const
110{
111 return sqrt( SquaredDistance( aP, false ) );
112}
113
114
115SEG::ecoord SHAPE::SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly ) const
116{
117 SHAPE_POLY_SET buffer;
118 TransformToPolygon( buffer, 0, ERROR_INSIDE );
119
120 if( buffer.OutlineCount() < 1 )
122
123 return buffer.COutline( 0 ).SquaredDistance( aP, aOutlineOnly );
124}
125
126
127bool SHAPE::PointInside( const VECTOR2I& aPt, int aAccuracy, bool aUseBBoxCache ) const
128{
129 SHAPE_POLY_SET buffer;
130 TransformToPolygon( buffer, aAccuracy, ERROR_INSIDE );
131
132 if( buffer.OutlineCount() < 1 )
133 return false;
134
135 return buffer.COutline( 0 ).PointInside( aPt, aAccuracy, aUseBBoxCache );
136}
137
138
140 ERROR_LOC aErrorLoc ) const
141{
142 aBuffer.AddOutline( m_points );
143}
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:139
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:55
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:40
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:127
virtual const std::string Format(bool aCplusPlus=true) const
Definition: shape.cpp:47
virtual int Distance(const VECTOR2I &aP) const
Returns the minimum distance from a given point to this shape.
Definition: shape.cpp:109
virtual SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Definition: shape.cpp:115
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:76