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