KiCad PCB EDA Suite
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  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #ifndef __SHAPE_COMPOUND_H
25 #define __SHAPE_COMPOUND_H
26 
27 #include <geometry/shape.h>
28 #include <math/vector2d.h>
29 #include <math/box2.h>
30 #include <list>
31 #include <vector>
32 
33 class SHAPE_SIMPLE;
34 
35 class SHAPE_COMPOUND : public SHAPE
36 {
37 public:
39  SHAPE( SH_COMPOUND ),
40  m_dirty( true )
41  {
42  }
43 
44 
45  SHAPE_COMPOUND( const std::vector<SHAPE*>& aShapes );
46 
47  SHAPE_COMPOUND( const SHAPE_COMPOUND& aOther );
49 
50  SHAPE_COMPOUND* Clone() const override;
51  const std::string Format() const override;
52 
53  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
54  VECTOR2I* aLocation = nullptr ) const override;
55 
56  bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
57  {
58  return SHAPE::Collide( aShape, aClearance, aMTV );
59  }
60 
61  bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
62  VECTOR2I* aLocation = nullptr ) const override
63  {
64  return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
65  }
66 
67  const std::vector<SHAPE*>& Shapes() const
68  {
69  return m_shapes;
70  }
71 
72  const BOX2I BBox( int aClearance = 0 ) const override;
73 
74  int Distance( const SEG& aSeg ) const;
75 
76  void Move( const VECTOR2I& aVector ) override;
77 
78  void AddShape( SHAPE* aShape )
79  {
80  // Don't make clients deal with nested SHAPE_COMPOUNDs
81  if( aShape->HasIndexableSubshapes() )
82  {
83  std::vector<SHAPE*> subshapes;
84  aShape->GetIndexableSubshapes( subshapes );
85 
86  for( SHAPE* subshape : subshapes )
87  m_shapes.push_back( subshape->Clone() );
88 
89  delete aShape;
90  }
91  else
92  {
93  m_shapes.push_back( aShape );
94  }
95 
96  m_dirty = true;
97  }
98 
99  bool Empty() const
100  {
101  return m_shapes.empty();
102  }
103 
104  int Size() const
105  {
106  return m_shapes.size();
107  }
108 
109  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override;
110 
111  bool IsSolid() const override;
112 
114  {
115  return m_shapes.size() != 1 ? nullptr : m_shapes[0];
116  }
117 
118  virtual bool HasIndexableSubshapes() const override
119  {
120  return true;
121  }
122 
123  virtual size_t GetIndexableSubshapeCount() const override
124  {
125  return m_shapes.size();
126  }
127 
128  virtual void GetIndexableSubshapes( std::vector<SHAPE*>& aSubshapes ) override
129  {
130  aSubshapes = m_shapes;
131  }
132 
133  bool ConvertToSimplePolygon( SHAPE_SIMPLE* aOut ) const;
134 
135 private:
137  bool m_dirty;
138  std::vector<SHAPE*> m_shapes;
139 };
140 
141 #endif // __SHAPE_COMPOUND_H
compound shape, consisting of multiple simple shapes
Definition: shape.h:49
SHAPE_SIMPLE.
Definition: shape_simple.h:43
void Move(const VECTOR2I &aVector) override
int Size() const
virtual size_t GetIndexableSubshapeCount() 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,...
Define a general 2D-vector/point.
Definition: vector2d.h:61
bool Empty() const
bool IsSolid() const override
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
BOX2 handles a 2-D bounding box, built on top of an origin point and size vector, both of templated c...
Definition: box2.h:43
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:165
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
virtual void GetIndexableSubshapes(std::vector< SHAPE * > &aSubshapes) override
int Distance(const SEG &aSeg) const
SHAPE_COMPOUND * Clone() const override
Return a dynamically allocated copy of the shape.
An abstract shape on 2D plane.
Definition: shape.h:116
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,...
Definition: seg.h:41
virtual void GetIndexableSubshapes(std::vector< SHAPE * > &aSubshapes)
Definition: shape.h:106
const std::vector< SHAPE * > & Shapes() const
std::vector< SHAPE * > m_shapes
SHAPE * UniqueSubshape() const
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
const std::string Format() const override
virtual bool HasIndexableSubshapes() const
Definition: shape.h:99
virtual bool HasIndexableSubshapes() const override
bool ConvertToSimplePolygon(SHAPE_SIMPLE *aOut) const
void AddShape(SHAPE *aShape)