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, see <https://www.gnu.org/licenses/>.
20 */
21
22
23#include <geometry/shape.h>
24#include <geometry/shape_arc.h>
27#include <geometry/shape_rect.h>
32
34
35
36bool SHAPE::Parse( std::stringstream& aStream )
37{
38 assert( false );
39 return false;
40}
41
42
43const std::string SHAPE::Format( bool aCplusPlus ) const
44{
45 std::stringstream ss;
46 ss << "shape " << m_type;
47 return ss.str();
48}
49
50
51int SHAPE::GetClearance( const SHAPE* aOther ) const
52{
53 int actual_clearance = std::numeric_limits<int>::max();
54 std::vector<const SHAPE*> a_shapes;
55 std::vector<const SHAPE*> b_shapes;
56
62 {
63 const SHAPE_POLY_SET* polySet = static_cast<const SHAPE_POLY_SET*>( this );
64 if( polySet->OutlineCount() > 0 )
65 a_shapes.push_back( &polySet->COutline( 0 ) );
66 }
67 else
68 {
69 GetIndexableSubshapes( a_shapes );
70 }
71
72 if( aOther->Type() == SHAPE_TYPE::SH_POLY_SET )
73 {
74 const SHAPE_POLY_SET* polySet = static_cast<const SHAPE_POLY_SET*>( aOther );
75 if( polySet->OutlineCount() > 0 )
76 b_shapes.push_back( &polySet->COutline( 0 ) );
77 }
78 else
79 {
80 aOther->GetIndexableSubshapes( b_shapes );
81 }
82
83 if( GetIndexableSubshapeCount() == 0 )
84 a_shapes.push_back( this );
85
86 if( aOther->GetIndexableSubshapeCount() == 0 )
87 b_shapes.push_back( aOther );
88
89 for( const SHAPE* a : a_shapes )
90 {
91 for( const SHAPE* b : b_shapes )
92 {
93 int temp_dist = 0;
94 a->Collide( b, std::numeric_limits<int>::max() / 2, &temp_dist );
95
96 if( temp_dist < actual_clearance )
97 actual_clearance = temp_dist;
98 }
99 }
100
101 return actual_clearance;
102}
103
104
105int SHAPE::Distance( const VECTOR2I& aP ) const
106{
107 return sqrt( SquaredDistance( aP, false ) );
108}
109
110
111SEG::ecoord SHAPE::SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly ) const
112{
113 SHAPE_POLY_SET buffer;
114 TransformToPolygon( buffer, 0, ERROR_INSIDE );
115
116 if( buffer.OutlineCount() < 1 )
118
119 return buffer.COutline( 0 ).SquaredDistance( aP, aOutlineOnly );
120}
121
122
123bool SHAPE::PointInside( const VECTOR2I& aPt, int aAccuracy, bool aUseBBoxCache ) const
124{
125 SHAPE_POLY_SET buffer;
126 TransformToPolygon( buffer, aAccuracy, ERROR_INSIDE );
127
128 if( buffer.OutlineCount() < 1 )
129 return false;
130
131 return buffer.COutline( 0 ).PointInside( aPt, aAccuracy, aUseBBoxCache );
132}
133
134
136 ERROR_LOC aErrorLoc ) const
137{
138 aBuffer.AddOutline( m_points );
139}
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
@ ERROR_INSIDE
VECTOR2I::extended_type ecoord
Definition seg.h:40
virtual size_t GetIndexableSubshapeCount() const
Definition shape.h:111
SHAPE_TYPE m_type
< type of our shape
Definition shape.h:117
virtual void GetIndexableSubshapes(std::vector< const SHAPE * > &aSubshapes) const
Definition shape.h:113
SHAPE_TYPE Type() const
Return the type of the shape.
Definition shape.h:96
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const override
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
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
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:135
int GetClearance(const SHAPE *aOther) const
Return the actual minimum distance between two shapes.
Definition shape.cpp:51
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:36
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:123
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:134
virtual const std::string Format(bool aCplusPlus=true) const
Definition shape.cpp:43
virtual int Distance(const VECTOR2I &aP) const
Returns the minimum distance from a given point to this shape.
Definition shape.cpp:105
virtual SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Definition shape.cpp:111
static constexpr extended_type ECOORD_MAX
Definition vector2d.h:72
@ SH_POLY_SET
set of polygons (with holes, etc.)
Definition shape.h:48
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683