KiCad PCB EDA Suite
pns_solid.cpp
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2014 CERN
5  * Copyright (C) 2016-2020 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 modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <math/vector2d.h>
23 
24 #include <geometry/shape.h>
26 #include <geometry/shape_rect.h>
27 #include <geometry/shape_circle.h>
28 #include <geometry/shape_simple.h>
30 
31 #include "pns_router.h"
32 #include "pns_solid.h"
33 #include "pns_utils.h"
34 
35 namespace PNS {
36 
37 static const SHAPE_LINE_CHAIN buildHullForPrimitiveShape( const SHAPE* aShape, int aClearance,
38  int aWalkaroundThickness )
39 {
40  int cl = aClearance + ( aWalkaroundThickness + 1 )/ 2;
41 
42  switch( aShape->Type() )
43  {
44  case SH_RECT:
45  {
46  const SHAPE_RECT* rect = static_cast<const SHAPE_RECT*>( aShape );
47  return OctagonalHull( rect->GetPosition(),
48  rect->GetSize(),
49  cl + 1,
50  0.2 * cl );
51  }
52 
53  case SH_CIRCLE:
54  {
55  const SHAPE_CIRCLE* circle = static_cast<const SHAPE_CIRCLE*>( aShape );
56  int r = circle->GetRadius();
57  return OctagonalHull( circle->GetCenter() - VECTOR2I( r, r ),
58  VECTOR2I( 2 * r, 2 * r ),
59  cl + 1,
60  0.52 * ( r + cl ) );
61  }
62 
63  case SH_SEGMENT:
64  {
65  const SHAPE_SEGMENT* seg = static_cast<const SHAPE_SEGMENT*>( aShape );
66  return SegmentHull( *seg, aClearance, aWalkaroundThickness );
67  }
68 
69  case SH_ARC:
70  {
71  const SHAPE_ARC* arc = static_cast<const SHAPE_ARC*>( aShape );
72  return ArcHull( *arc, aClearance, aWalkaroundThickness );
73  }
74 
75  case SH_SIMPLE:
76  {
77  const SHAPE_SIMPLE* convex = static_cast<const SHAPE_SIMPLE*>( aShape );
78 
79  return ConvexHull( *convex, cl );
80  }
81  default:
82  {
83  wxLogError( "Unsupported hull shape: %d (%s)", aShape->Type(),
84  SHAPE_TYPE_asString( aShape->Type() ) );
85  break;
86  }
87  }
88 
89  return SHAPE_LINE_CHAIN();
90 }
91 
92 
93 const SHAPE_LINE_CHAIN SOLID::Hull( int aClearance, int aWalkaroundThickness, int aLayer ) const
94 {
95  if( !ROUTER::GetInstance()->GetInterface()->IsFlashedOnLayer( this, aLayer ) )
96  return HoleHull( aClearance, aWalkaroundThickness, aLayer );
97 
98  if( !m_shape )
99  return SHAPE_LINE_CHAIN();
100 
101  if( m_shape->Type() == SH_COMPOUND )
102  {
103  SHAPE_COMPOUND* cmpnd = static_cast<SHAPE_COMPOUND*>( m_shape );
104 
105  if ( cmpnd->Shapes().size() == 1 )
106  {
107  return buildHullForPrimitiveShape( cmpnd->Shapes()[0], aClearance,
108  aWalkaroundThickness );
109  }
110  else
111  {
112  // fixme - shouldn't happen but one day we should move
113  // TransformShapeWithClearanceToPolygon() to the Geometry Library
114  return SHAPE_LINE_CHAIN();
115  }
116  }
117  else
118  {
119  return buildHullForPrimitiveShape( m_shape, aClearance, aWalkaroundThickness );
120  }
121 }
122 
123 
124 const SHAPE_LINE_CHAIN SOLID::HoleHull( int aClearance, int aWalkaroundThickness, int aLayer ) const
125 {
126  if( !m_hole )
127  return SHAPE_LINE_CHAIN();
128 
129  if( m_hole->Type() == SH_COMPOUND )
130  {
131  SHAPE_COMPOUND* cmpnd = static_cast<SHAPE_COMPOUND*>( m_hole );
132 
133  if ( cmpnd->Shapes().size() == 1 )
134  {
135  return buildHullForPrimitiveShape( cmpnd->Shapes()[0], aClearance,
136  aWalkaroundThickness );
137  }
138  else
139  {
140  // fixme - shouldn't happen but one day we should move
141  // TransformShapeWithClearanceToPolygon() to the Geometry Library
142  return SHAPE_LINE_CHAIN();
143  }
144  }
145  else
146  {
147  return buildHullForPrimitiveShape( m_hole, aClearance, aWalkaroundThickness );
148  }
149 }
150 
151 
153 {
154  ITEM* solid = new SOLID( *this );
155  return solid;
156 }
157 
158 void SOLID::SetPos( const VECTOR2I& aCenter )
159 {
160  VECTOR2I delta = aCenter - m_pos;
161 
162  if( m_shape )
163  m_shape->Move( delta );
164 
165  m_pos = aCenter;
166 }
167 
168 
169 }
compound shape, consisting of multiple simple shapes
Definition: shape.h:49
Base class for PNS router board items.
Definition: pns_item.h:55
const SHAPE_LINE_CHAIN ConvexHull(const SHAPE_SIMPLE &aConvex, int aClearance)
Function ConvexHull()
Definition: pns_utils.cpp:169
SHAPE_SIMPLE.
Definition: shape_simple.h:43
int GetRadius() const
Definition: shape_circle.h:107
static const SHAPE_LINE_CHAIN buildHullForPrimitiveShape(const SHAPE *aShape, int aClearance, int aWalkaroundThickness)
Definition: pns_solid.cpp:37
void SetPos(const VECTOR2I &aCenter)
Definition: pns_solid.cpp:158
const SHAPE_LINE_CHAIN HoleHull(int aClearance, int aWalkaroundThickness, int aLayer) const override
Definition: pns_solid.cpp:124
const VECTOR2I GetCenter() const
Definition: shape_circle.h:112
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
const SHAPE_LINE_CHAIN SegmentHull(const SHAPE_SEGMENT &aSeg, int aClearance, int aWalkaroundThickness)
Definition: pns_utils.cpp:112
SHAPE * m_shape
Definition: pns_solid.h:125
const VECTOR2I GetSize() const
Definition: shape_rect.h:124
ITEM * Clone() const override
Return a deep copy of the item.
Definition: pns_solid.cpp:152
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:116
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:55
circular arc
Definition: shape.h:50
const SHAPE_LINE_CHAIN OctagonalHull(const VECTOR2I &aP0, const VECTOR2I &aSize, int aClearance, int aChamfer)
Definition: pns_utils.cpp:35
An abstract shape on 2D plane.
Definition: shape.h:116
circle
Definition: shape.h:46
SHAPE * m_hole
Definition: pns_solid.h:126
const SHAPE_LINE_CHAIN ArcHull(const SHAPE_ARC &aSeg, int aClearance, int aWalkaroundThickness)
Various utility functions.
Definition: pns_utils.cpp:55
const std::vector< SHAPE * > & Shapes() const
SHAPE_LINE_CHAIN.
axis-aligned rectangle
Definition: shape.h:43
virtual void Move(const VECTOR2I &aVector)=0
Push and Shove diff pair dimensions (gap) settings dialog.
simple polygon
Definition: shape.h:47
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:94
static ROUTER * GetInstance()
Definition: pns_router.cpp:79
const SHAPE_LINE_CHAIN Hull(int aClearance=0, int aWalkaroundThickness=0, int aLayer=-1) const override
Definition: pns_solid.cpp:93
line segment
Definition: shape.h:44
VECTOR2I m_pos
Definition: pns_solid.h:124