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 int Distance( const SEG& aSeg ) const;
78
79 void Move( const VECTOR2I& aVector ) override;
80
81 void AddShape( SHAPE* aShape )
82 {
83 // Don't make clients deal with nested SHAPE_COMPOUNDs
84 if( aShape->HasIndexableSubshapes() )
85 {
86 std::vector<const SHAPE*> subshapes;
87 aShape->GetIndexableSubshapes( subshapes );
88
89 for( const SHAPE* subshape : subshapes )
90 m_shapes.push_back( subshape->Clone() );
91
92 delete aShape;
93 }
94 else
95 {
96 m_shapes.push_back( aShape );
97 }
98
99 m_dirty = true;
100 }
101
102 void AddShape( std::shared_ptr<SHAPE> aShape )
103 {
104 // Don't make clients deal with nested SHAPE_COMPOUNDs
105 if( aShape->HasIndexableSubshapes() )
106 {
107 std::vector<const SHAPE*> subshapes;
108 aShape->GetIndexableSubshapes( subshapes );
109
110 for( const SHAPE* subshape : subshapes )
111 m_shapes.push_back( subshape->Clone() );
112 }
113 else
114 {
115 m_shapes.push_back( aShape->Clone() );
116 }
117
118 m_dirty = true;
119 }
120
121 bool Empty() const
122 {
123 return m_shapes.empty();
124 }
125
126 int Size() const
127 {
128 return m_shapes.size();
129 }
130
131 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override;
132
133 bool IsSolid() const override;
134
136 {
137 return m_shapes.size() != 1 ? nullptr : m_shapes[0];
138 }
139
140 virtual bool HasIndexableSubshapes() const override
141 {
142 return true;
143 }
144
145 virtual size_t GetIndexableSubshapeCount() const override
146 {
147 return m_shapes.size();
148 }
149
150 virtual void GetIndexableSubshapes( std::vector<const SHAPE*>& aSubshapes ) const override
151 {
152 aSubshapes.clear();
153 aSubshapes.reserve( m_shapes.size() );
154 std::copy( m_shapes.begin(), m_shapes.end(), std::back_inserter( aSubshapes ) );
155 }
156
157 bool ConvertToSimplePolygon( SHAPE_SIMPLE* aOut ) const;
158
159private:
162 std::vector<SHAPE*> m_shapes;
163};
164
165#endif // __SHAPE_COMPOUND_H
Definition: seg.h:42
virtual void GetIndexableSubshapes(std::vector< const SHAPE * > &aSubshapes) const
Definition: shape.h:113
virtual bool HasIndexableSubshapes() const
Definition: shape.h:106
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
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
bool ConvertToSimplePolygon(SHAPE_SIMPLE *aOut) const
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 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:124
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:179
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
Definition: shape.h:51