KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_compound.h
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) 2016-2020 CERN
5 * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
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#ifndef __SHAPE_COMPOUND_H
26#define __SHAPE_COMPOUND_H
27
28#include <geometry/shape.h>
29#include <math/vector2d.h>
30#include <math/box2.h>
31#include <list>
32#include <vector>
33#include <memory>
34#include "eda_angle.h"
35
36class SHAPE_SIMPLE;
37
38class SHAPE_COMPOUND : public SHAPE
39{
40public:
43 m_dirty( true )
44 {
45 }
46
47
48 SHAPE_COMPOUND( const std::vector<SHAPE*>& aShapes );
49
50 SHAPE_COMPOUND( const SHAPE_COMPOUND& aOther );
52
53 SHAPE_COMPOUND* Clone() const override;
54 const std::string Format( bool aCplusPlus = true ) const override;
55
56 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
57 VECTOR2I* aLocation = nullptr ) const override;
58
59 bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
60 {
61 return SHAPE::Collide( aShape, aClearance, aMTV );
62 }
63
64 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
65 VECTOR2I* aLocation = nullptr ) const override
66 {
67 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
68 }
69
70 const std::vector<SHAPE*>& Shapes() const
71 {
72 return m_shapes;
73 }
74
75 const BOX2I BBox( int aClearance = 0 ) const override;
76
77 using SHAPE::Distance;
78
79 int Distance( const SEG& aSeg ) const;
80
81 void Move( const VECTOR2I& aVector ) override;
82
83 void AddShape( SHAPE* aShape )
84 {
85 // Don't make clients deal with nested SHAPE_COMPOUNDs
86 if( dynamic_cast<SHAPE_COMPOUND*>( aShape ) )
87 {
88 std::vector<const SHAPE*> subshapes;
89 aShape->GetIndexableSubshapes( subshapes );
90
91 for( const SHAPE* subshape : subshapes )
92 m_shapes.push_back( subshape->Clone() );
93
94 delete aShape;
95 }
96 else
97 {
98 m_shapes.push_back( aShape );
99 }
100
101 m_dirty = true;
102 }
103
104 void AddShape( std::shared_ptr<SHAPE> aShape )
105 {
106 // Don't make clients deal with nested SHAPE_COMPOUNDs
107 if( dynamic_cast<SHAPE_COMPOUND*>( aShape.get() ) )
108 {
109 std::vector<const SHAPE*> subshapes;
110 aShape->GetIndexableSubshapes( subshapes );
111
112 for( const SHAPE* subshape : subshapes )
113 m_shapes.push_back( subshape->Clone() );
114 }
115 else
116 {
117 m_shapes.push_back( aShape->Clone() );
118 }
119
120 m_dirty = true;
121 }
122
123 bool Empty() const
124 {
125 return m_shapes.empty();
126 }
127
128 int Size() const
129 {
130 return (int) m_shapes.size();
131 }
132
133 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override;
134
135 bool IsSolid() const override;
136
138 {
139 return m_shapes.size() != 1 ? nullptr : m_shapes[0];
140 }
141
142 virtual bool HasIndexableSubshapes() const override
143 {
144 return true;
145 }
146
147 virtual size_t GetIndexableSubshapeCount() const override
148 {
149 return m_shapes.size();
150 }
151
152 virtual void GetIndexableSubshapes( std::vector<const SHAPE*>& aSubshapes ) const override
153 {
154 aSubshapes.clear();
155 aSubshapes.reserve( m_shapes.size() );
156 std::copy( m_shapes.begin(), m_shapes.end(), std::back_inserter( aSubshapes ) );
157 }
158
159 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
160 ERROR_LOC aErrorLoc ) const override;
161
162private:
165 std::vector<SHAPE*> m_shapes;
166};
167
168#endif // __SHAPE_COMPOUND_H
Definition: seg.h:42
virtual void GetIndexableSubshapes(std::vector< const SHAPE * > &aSubshapes) const
Definition: shape.h:115
bool IsSolid() const override
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
virtual void GetIndexableSubshapes(std::vector< const SHAPE * > &aSubshapes) const override
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
const std::vector< SHAPE * > & Shapes() const
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.
virtual bool HasIndexableSubshapes() const override
virtual size_t GetIndexableSubshapeCount() const override
std::vector< SHAPE * > m_shapes
const std::string Format(bool aCplusPlus=true) const override
SHAPE_COMPOUND * Clone() const override
Return a dynamically allocated copy of the shape.
bool Empty() const
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
SHAPE * UniqueSubshape() const
void AddShape(std::shared_ptr< SHAPE > aShape)
int Distance(const SEG &aSeg) const
void Move(const VECTOR2I &aVector) override
int Size() const
void AddShape(SHAPE *aShape)
Represent a set of closed polygons.
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
Definition: shape_simple.h:42
An abstract shape on 2D plane.
Definition: shape.h:126
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:181
virtual int Distance(const VECTOR2I &aP) const
Returns the minimum distance from a given point to this shape.
Definition: shape.cpp:96
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
Definition: shape.h:53