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 a_shapes.push_back( &static_cast<const SHAPE_POLY_SET*>( this )->COutline( 0 ) );
66 else
67 GetIndexableSubshapes( a_shapes );
68
69 if( aOther->Type() == SHAPE_TYPE::SH_POLY_SET )
70 b_shapes.push_back( &static_cast<const SHAPE_POLY_SET*>( aOther )->COutline( 0 ) );
71 else
72 aOther->GetIndexableSubshapes( b_shapes );
73
74 if( GetIndexableSubshapeCount() == 0 )
75 a_shapes.push_back( this );
76
77 if( aOther->GetIndexableSubshapeCount() == 0 )
78 b_shapes.push_back( aOther );
79
80 for( const SHAPE* a : a_shapes )
81 {
82 for( const SHAPE* b : b_shapes )
83 {
84 int temp_dist = 0;
85 a->Collide( b, std::numeric_limits<int>::max() / 2, &temp_dist );
86
87 if( temp_dist < actual_clearance )
88 actual_clearance = temp_dist;
89 }
90 }
91
92 return actual_clearance;
93}
94
95
96int SHAPE::Distance( const VECTOR2I& aP ) const
97{
98 return sqrt( SquaredDistance( aP, false ) );
99}
100
101
102SEG::ecoord SHAPE::SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly ) const
103{
104 SHAPE_POLY_SET buffer;
105 TransformToPolygon( buffer, 0, ERROR_INSIDE );
106
107 if( buffer.OutlineCount() < 1 )
109
110 return buffer.COutline( 0 ).SquaredDistance( aP, aOutlineOnly );
111}
112
113
114bool SHAPE::PointInside( const VECTOR2I& aPt, int aAccuracy, bool aUseBBoxCache ) const
115{
116 SHAPE_POLY_SET buffer;
117 TransformToPolygon( buffer, aAccuracy, ERROR_INSIDE );
118
119 if( buffer.OutlineCount() < 1 )
120 return false;
121
122 return buffer.COutline( 0 ).PointInside( aPt, aAccuracy, aUseBBoxCache );
123}
124
125
127 ERROR_LOC aErrorLoc ) const
128{
129 aBuffer.AddOutline( m_points );
130}
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:126
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:114
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:96
virtual SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Definition: shape.cpp:102
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...
@ ERROR_INSIDE