Horizon
Loading...
Searching...
No Matches
shape.h
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2013 CERN
5 * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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#ifndef __SHAPE_H
27#define __SHAPE_H
28
29#include <sstream>
30#include <vector>
31#include <geometry/seg.h>
32#include <math/vector2d.h>
33#include <math/box2.h>
34
36
40
41enum SHAPE_TYPE
42{
43 SH_RECT = 0,
44 SH_SEGMENT,
45 SH_LINE_CHAIN,
46 SH_CIRCLE,
47 SH_SIMPLE,
48 SH_POLY_SET,
49 SH_COMPOUND,
50 SH_ARC,
51 SH_NULL,
52 SH_POLY_SET_TRIANGLE
53};
54
55static inline wxString SHAPE_TYPE_asString( SHAPE_TYPE a )
56{
57 switch( a )
58 {
59 case SH_RECT: return wxT( "SH_RECT" );
60 case SH_SEGMENT: return wxT( "SH_SEGMENT" );
61 case SH_LINE_CHAIN: return wxT( "SH_LINE_CHAIN" );
62 case SH_CIRCLE: return wxT( "SH_CIRCLE" );
63 case SH_SIMPLE: return wxT( "SH_SIMPLE" );
64 case SH_POLY_SET: return wxT( "SH_POLY_SET" );
65 case SH_COMPOUND: return wxT( "SH_COMPOUND" );
66 case SH_ARC: return wxT( "SH_ARC" );
67 case SH_NULL: return wxT( "SH_NULL" );
68 case SH_POLY_SET_TRIANGLE: return wxT( "SH_POLY_SET_TRIANGLE" );
69 }
70
71 return wxEmptyString; // Just to quiet GCC.
72}
73
74class SHAPE;
75
77{
78public:
82 SHAPE_BASE( SHAPE_TYPE aType ) :
83 m_type( aType )
84 {}
85
86 virtual ~SHAPE_BASE()
87 {}
88
94 SHAPE_TYPE Type() const
95 {
96 return m_type;
97 }
98
99 wxString TypeName() const
100 {
101 return SHAPE_TYPE_asString( m_type );
102 }
103
104 virtual bool HasIndexableSubshapes() const
105 {
106 return false;
107 }
108
109 virtual size_t GetIndexableSubshapeCount() const { return 0; }
110
111 virtual void GetIndexableSubshapes( std::vector<SHAPE*>& aSubshapes ) { }
112
113protected:
115 SHAPE_TYPE m_type;
116};
117
121class SHAPE : public SHAPE_BASE
122{
123public:
127 static const int MIN_PRECISION_IU = 4;
128
132 SHAPE( SHAPE_TYPE aType ) :
133 SHAPE_BASE( aType )
134 {}
135
136 virtual ~SHAPE()
137 {}
138
144 virtual SHAPE* Clone() const
145 {
146 assert( false );
147 return nullptr;
148 };
149
155 bool IsNull() const
156 {
157 return m_type == SH_NULL;
158 }
159
170 virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
171 VECTOR2I* aLocation = nullptr ) const
172 {
173 return Collide( SEG( aP, aP ), aClearance, aActual, aLocation );
174 }
175
189 virtual bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const;
190
191 virtual bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
192 VECTOR2I* aLocation = nullptr ) const;
193
204 virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
205 VECTOR2I* aLocation = nullptr ) const = 0;
206
214 virtual const BOX2I BBox( int aClearance = 0 ) const = 0;
215
221 virtual VECTOR2I Centre() const
222 {
223 return BBox( 0 ).Centre(); // if nothing better is available....
224 }
225
230 virtual void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) = 0;
231
232 virtual void Move( const VECTOR2I& aVector ) = 0;
233
234 virtual bool IsSolid() const = 0;
235
236 virtual bool Parse( std::stringstream& aStream );
237
238 virtual const std::string Format( ) const;
239
240protected:
241 typedef VECTOR2I::extended_type ecoord;
242};
243
244
245class SHAPE_LINE_CHAIN_BASE : public SHAPE
246{
247public:
248 SHAPE_LINE_CHAIN_BASE( SHAPE_TYPE aType ) :
249 SHAPE( aType )
250 {
251 }
252
253 virtual ~SHAPE_LINE_CHAIN_BASE()
254 {
255 }
256
266 virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
267 VECTOR2I* aLocation = nullptr ) const override;
268
278
279 virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
280 VECTOR2I* aLocation = nullptr ) const override;
281
282 SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
283
293 bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
294
301 bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
302
309 int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
310
311 virtual const VECTOR2I GetPoint( int aIndex ) const = 0;
312 virtual const SEG GetSegment( int aIndex ) const = 0;
313 virtual size_t GetPointCount() const = 0;
314 virtual size_t GetSegmentCount() const = 0;
315 virtual bool IsClosed() const = 0;
316
317 virtual BOX2I* GetCachedBBox() const { return nullptr; }
318};
319
320#endif // __SHAPE_H
Definition seg.h:41
Definition shape.h:77
SHAPE_BASE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:82
SHAPE_TYPE m_type
< type of our shape
Definition shape.h:115
SHAPE_TYPE Type() const
Return the type of the shape.
Definition shape.h:94
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Check if point aP lies inside a polygon (any type) defined by the line chain.
Definition shape_line_chain.cpp:1535
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if point aP lies closer to us than aClearance.
Definition shape_line_chain.cpp:301
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
Definition shape_line_chain.cpp:1587
int EdgeContainingPoint(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
Definition shape_line_chain.cpp:1593
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Definition shape_line_chain.h:81
An abstract shape on 2D plane.
Definition shape.h:122
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition shape.h:170
virtual VECTOR2I Centre() const
Compute a center-of-mass of the shape.
Definition shape.h:221
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:132
static const int MIN_PRECISION_IU
This is the minimum precision for all the points in a shape.
Definition shape.h:127
bool IsNull() const
Return true if the shape is a null shape.
Definition shape.h:155
virtual SHAPE * Clone() const
Return a dynamically allocated copy of the shape.
Definition shape.h:144
virtual bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const =0
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
virtual const BOX2I BBox(int aClearance=0) const =0
Compute a bounding box of the shape, with a margin of aClearance a collision.
virtual void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 })=0
Definition wx_compat.h:13