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 <wx/log.h>
32 
33 #include "pns_router.h"
34 #include "pns_solid.h"
35 #include "pns_utils.h"
36 
37 namespace PNS {
38 
39 static const SHAPE_LINE_CHAIN buildHullForPrimitiveShape( const SHAPE* aShape, int aClearance,
40  int aWalkaroundThickness )
41 {
42  int cl = aClearance + ( aWalkaroundThickness + 1 )/ 2;
43 
44  switch( aShape->Type() )
45  {
46  case SH_RECT:
47  {
48  const SHAPE_RECT* rect = static_cast<const SHAPE_RECT*>( aShape );
49  return OctagonalHull( rect->GetPosition(),
50  rect->GetSize(),
51  cl + 1,
52  0 );
53  }
54 
55  case SH_CIRCLE:
56  {
57  const SHAPE_CIRCLE* circle = static_cast<const SHAPE_CIRCLE*>( aShape );
58  int r = circle->GetRadius();
59  return OctagonalHull( circle->GetCenter() - VECTOR2I( r, r ),
60  VECTOR2I( 2 * r, 2 * r ),
61  cl + 1,
62  2.0 * ( 1.0 - M_SQRT1_2 ) * ( r + cl ) );
63  }
64 
65  case SH_SEGMENT:
66  {
67  const SHAPE_SEGMENT* seg = static_cast<const SHAPE_SEGMENT*>( aShape );
68  return SegmentHull( *seg, aClearance, aWalkaroundThickness );
69  }
70 
71  case SH_ARC:
72  {
73  const SHAPE_ARC* arc = static_cast<const SHAPE_ARC*>( aShape );
74  return ArcHull( *arc, aClearance, aWalkaroundThickness );
75  }
76 
77  case SH_SIMPLE:
78  {
79  const SHAPE_SIMPLE* convex = static_cast<const SHAPE_SIMPLE*>( aShape );
80 
81  return ConvexHull( *convex, cl );
82  }
83  default:
84  {
85  wxFAIL_MSG( wxString::Format( "Unsupported hull shape: %d (%s).",
86  aShape->Type(),
87  SHAPE_TYPE_asString( aShape->Type() ) ) );
88  break;
89  }
90  }
91 
92  return SHAPE_LINE_CHAIN();
93 }
94 
95 
96 const SHAPE_LINE_CHAIN SOLID::Hull( int aClearance, int aWalkaroundThickness, int aLayer ) const
97 {
98  if( !ROUTER::GetInstance()->GetInterface()->IsFlashedOnLayer( this, aLayer ) )
99  return HoleHull( aClearance, aWalkaroundThickness, aLayer );
100 
101  if( !m_shape )
102  return SHAPE_LINE_CHAIN();
103 
104  if( m_shape->Type() == SH_COMPOUND )
105  {
106  SHAPE_COMPOUND* cmpnd = static_cast<SHAPE_COMPOUND*>( m_shape );
107 
108  if ( cmpnd->Shapes().size() == 1 )
109  {
110  return buildHullForPrimitiveShape( cmpnd->Shapes()[0], aClearance,
111  aWalkaroundThickness );
112  }
113  else
114  {
115  // fixme - shouldn't happen but one day we should move
116  // TransformShapeWithClearanceToPolygon() to the Geometry Library
117  return SHAPE_LINE_CHAIN();
118  }
119  }
120  else
121  {
122  return buildHullForPrimitiveShape( m_shape, aClearance, aWalkaroundThickness );
123  }
124 }
125 
126 
127 const SHAPE_LINE_CHAIN SOLID::HoleHull( int aClearance, int aWalkaroundThickness, int aLayer ) const
128 {
129  if( !m_hole )
130  return SHAPE_LINE_CHAIN();
131 
132  if( m_hole->Type() == SH_COMPOUND )
133  {
134  SHAPE_COMPOUND* cmpnd = static_cast<SHAPE_COMPOUND*>( m_hole );
135 
136  if ( cmpnd->Shapes().size() == 1 )
137  {
138  return buildHullForPrimitiveShape( cmpnd->Shapes()[0], aClearance,
139  aWalkaroundThickness );
140  }
141  else
142  {
143  // fixme - shouldn't happen but one day we should move
144  // TransformShapeWithClearanceToPolygon() to the Geometry Library
145  return SHAPE_LINE_CHAIN();
146  }
147  }
148  else
149  {
150  return buildHullForPrimitiveShape( m_hole, aClearance, aWalkaroundThickness );
151  }
152 }
153 
154 
156 {
157  ITEM* solid = new SOLID( *this );
158  return solid;
159 }
160 
161 void SOLID::SetPos( const VECTOR2I& aCenter )
162 {
163  VECTOR2I delta = aCenter - m_pos;
164 
165  if( m_shape )
166  m_shape->Move( delta );
167 
168  m_pos = aCenter;
169 }
170 
171 
172 }
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:180
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
Definition: shape_simple.h:41
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:39
void SetPos(const VECTOR2I &aCenter)
Definition: pns_solid.cpp:161
const SHAPE_LINE_CHAIN HoleHull(int aClearance, int aWalkaroundThickness, int aLayer) const override
Definition: pns_solid.cpp:127
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:123
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:155
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
E_SERIE r
Definition: eserie.cpp:41
circle
Definition: shape.h:46
SHAPE * m_hole
Definition: pns_solid.h:126
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
const SHAPE_LINE_CHAIN ArcHull(const SHAPE_ARC &aSeg, int aClearance, int aWalkaroundThickness)
Various utility functions.
Definition: pns_utils.cpp:66
const std::vector< SHAPE * > & Shapes() const
Represent a polyline (an zero-thickness chain of connected line segments).
constexpr int delta
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:78
const SHAPE_LINE_CHAIN Hull(int aClearance=0, int aWalkaroundThickness=0, int aLayer=-1) const override
Definition: pns_solid.cpp:96
line segment
Definition: shape.h:44
VECTOR2I m_pos
Definition: pns_solid.h:124