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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef __SHAPE_COMPOUND_H
22#define __SHAPE_COMPOUND_H
23
24#include <geometry/shape.h>
25#include <math/vector2d.h>
26#include <math/box2.h>
27#include <list>
28#include <vector>
29#include <memory>
30#include "eda_angle.h"
31
32class SHAPE_SIMPLE;
33
34class SHAPE_COMPOUND : public SHAPE
35{
36public:
39 m_dirty( true )
40 {
41 }
42
43
44 SHAPE_COMPOUND( const std::vector<SHAPE*>& aShapes );
45
46 SHAPE_COMPOUND( const SHAPE_COMPOUND& aOther );
48
49 SHAPE_COMPOUND* Clone() const override;
50 const std::string Format( bool aCplusPlus = true ) const override;
51
52 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
53 VECTOR2I* aLocation = nullptr ) const override;
54
55 bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
56 {
57 return SHAPE::Collide( aShape, aClearance, aMTV );
58 }
59
60 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
61 VECTOR2I* aLocation = nullptr ) const override
62 {
63 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
64 }
65
66 const std::vector<SHAPE*>& Shapes() const
67 {
68 return m_shapes;
69 }
70
71 const BOX2I BBox( int aClearance = 0 ) const override;
72
73 using SHAPE::Distance;
74
75 int Distance( const SEG& aSeg ) const;
76
77 void Move( const VECTOR2I& aVector ) override;
78
79 void AddShape( SHAPE* aShape )
80 {
81 // Don't make clients deal with nested SHAPE_COMPOUNDs
82 if( dynamic_cast<SHAPE_COMPOUND*>( aShape ) )
83 {
84 std::vector<const SHAPE*> subshapes;
85 aShape->GetIndexableSubshapes( subshapes );
86
87 for( const SHAPE* subshape : subshapes )
88 m_shapes.push_back( subshape->Clone() );
89
90 delete aShape;
91 }
92 else
93 {
94 m_shapes.push_back( aShape );
95 }
96
97 m_dirty = true;
98 }
99
100 void AddShape( std::shared_ptr<SHAPE> aShape )
101 {
102 // Don't make clients deal with nested SHAPE_COMPOUNDs
103 if( dynamic_cast<SHAPE_COMPOUND*>( aShape.get() ) )
104 {
105 std::vector<const SHAPE*> subshapes;
106 aShape->GetIndexableSubshapes( subshapes );
107
108 for( const SHAPE* subshape : subshapes )
109 m_shapes.push_back( subshape->Clone() );
110 }
111 else
112 {
113 m_shapes.push_back( aShape->Clone() );
114 }
115
116 m_dirty = true;
117 }
118
119 bool Empty() const
120 {
121 return m_shapes.empty();
122 }
123
124 int Size() const
125 {
126 return (int) m_shapes.size();
127 }
128
129 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override;
130
131 bool IsSolid() const override;
132
134 {
135 return m_shapes.size() != 1 ? nullptr : m_shapes[0];
136 }
137
138 virtual bool HasIndexableSubshapes() const override
139 {
140 return true;
141 }
142
143 virtual size_t GetIndexableSubshapeCount() const override
144 {
145 return m_shapes.size();
146 }
147
148 virtual void GetIndexableSubshapes( std::vector<const SHAPE*>& aSubshapes ) const override
149 {
150 aSubshapes.clear();
151 aSubshapes.reserve( m_shapes.size() );
152 std::copy( m_shapes.begin(), m_shapes.end(), std::back_inserter( aSubshapes ) );
153 }
154
155 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
156 ERROR_LOC aErrorLoc ) const override;
157
158private:
161 std::vector<SHAPE*> m_shapes;
162};
163
164#endif // __SHAPE_COMPOUND_H
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
Definition seg.h:38
virtual void GetIndexableSubshapes(std::vector< const SHAPE * > &aSubshapes) const
Definition shape.h:113
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.
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
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:134
virtual int Distance(const VECTOR2I &aP) const
Returns the minimum distance from a given point to this shape.
Definition shape.cpp:105
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
Definition shape.h:49
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683