KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_via.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-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * Author: Tomasz Wlostowski <[email protected]>
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 "pns_via.h"
23#include "pns_node.h"
24#include "pns_utils.h"
25#include "pns_router.h"
26#include "pns_debug_decorator.h"
27
28#include <geometry/shape_rect.h>
29#include <math/box2.h>
30
31namespace PNS {
32
33int VIA::EffectiveLayer( int aLayer ) const
34{
35 switch( m_stackMode )
36 {
37 default:
39 return ALL_LAYERS;
40
42 if( aLayer == m_layers.Start() || aLayer == m_layers.End() )
43 return aLayer;
44
45 if( m_layers.Start() + 1 < m_layers.End() )
46 return m_layers.Start() + 1;
47
48 return m_layers.Start();
49
51 return m_layers.Overlaps( aLayer ) ? aLayer : m_layers.Start();
52 }
53}
54
55
56std::vector<int> VIA::UniqueShapeLayers() const
57{
58 switch( m_stackMode )
59 {
60 default:
62 return { ALL_LAYERS };
63
65 return { ALL_LAYERS, INNER_LAYERS, m_layers.End() };
66
68 std::vector<int> ret;
69
70 for( int l = m_layers.Start(); l <= m_layers.End(); l++ )
71 ret.push_back( l );
72
73 return ret;
74 }
75}
76
77
78void VIA::SetStackMode( STACK_MODE aStackMode )
79{
80 m_stackMode = aStackMode;
81
82 wxASSERT_MSG( m_stackMode != STACK_MODE::FRONT_INNER_BACK || m_layers.Start() == 0,
83 wxT( "Cannot use FRONT_INNER_BACK with blind/buried vias!" ) );
84
85 // In theory, it might be good to do some housekeeping on m_diameters and m_shapes here,
86 // but it's not yet clear if the stack mode needs to be changed after initial creation.
87}
88
89
90bool VIA::PadstackMatches( const VIA& aOther ) const
91{
92 std::vector<int> myLayers = UniqueShapeLayers();
93 std::vector<int> otherLayers = aOther.UniqueShapeLayers();
94
95 if( !std::equal( myLayers.begin(), myLayers.end(), otherLayers.begin() ) )
96 return false;
97
98 for( int i : myLayers )
99 {
100 if( Diameter( i ) != aOther.Diameter( i ) )
101 return false;
102 }
103
104 return true;
105}
106
107
108bool VIA::PushoutForce( NODE* aNode, const ITEM* aOther, VECTOR2I& aForce )
109{
110 int clearance = aNode->GetClearance( this, aOther, false );
111 VECTOR2I elementForce;
112
113 for( int layer : RelevantShapeLayers( aOther ) )
114 {
115 aOther->Shape( layer )->Collide( Shape( layer ), clearance, &elementForce );
116
117 if( elementForce.SquaredEuclideanNorm() > aForce.SquaredEuclideanNorm() )
118 aForce = elementForce;
119 }
120
121 return ( aForce != VECTOR2I( 0, 0 ) );
122}
123
124
125bool VIA::PushoutForce( NODE* aNode, const VECTOR2I& aDirection, VECTOR2I& aForce,
126 int aCollisionMask, int aMaxIterations )
127{
128 int iter = 0;
129 VIA mv( *this );
130 VECTOR2I totalForce;
131
133 PNS_DBG( dbg, AddPoint, Pos(), YELLOW, 100000, wxString::Format( "via-force-init-pos, iter %d", aMaxIterations ) );
134
135 while( iter < aMaxIterations )
136 {
138 opt.m_limitCount = 1;
139 opt.m_kindMask = aCollisionMask;
140 opt.m_useClearanceEpsilon = false;
141
142 NODE::OPT_OBSTACLE obs = aNode->CheckColliding( &mv, opt );
143
144 if( !obs )
145 break;
146
147 VECTOR2I force;
148 bool collFound = mv.PushoutForce( aNode, obs->m_item, force );
149
150 if( !collFound )
151 {
152 if( obs )
153 {
154 // might happen (although rarely) that we see a collision, but the MTV
155 // is zero... Assume force propagation has failed in such case.
156 return false;
157 }
158 PNS_DBG( dbg, Message, wxString::Format( "no-coll %d", iter ) );
159 break;
160 }
161
162 // TODO(JE) padstacks -- what is the correct logic here?
163 const int threshold = Diameter( EffectiveLayer( 0 ) ) / 4; // another stupid heuristic.
164 const int forceMag = force.EuclideanNorm();
165
166 // We've been through a lot of iterations already and our pushout force is still too big?
167 // Perhaps the barycentric force goes in the wrong direction, let's try to move along
168 // the 'lead' vector instead (usually backwards to the cursor)
169 if( iter > aMaxIterations / 2 && forceMag > threshold )
170 {
171 VECTOR2I l = aDirection.Resize( threshold );
172 totalForce += l;
173
175 ff.Append( mv.Pos() );
176 ff.Append( mv.Pos() + l );
177
178 mv.SetPos( mv.Pos() + l );
179
180 PNS_DBG( dbg, AddShape, &ff, YELLOW, 100000, "via-force-lead" );
181 }
182 else if( collFound ) // push along the minmum translation vector
183 {
184 // Limit the force magnitude to, say, 25% of the via diameter
185 // This adds a few iterations for large areas (e.g. keepouts)
186 // But makes the algorithm more predictable and less 'jumpy'
187 if( forceMag > threshold )
188 {
189 force.Resize( threshold );
190 }
191
192 totalForce += force;
193
195 ff.Append( mv.Pos() );
196 ff.Append( mv.Pos() + force );
197
198 mv.SetPos( mv.Pos() + force );
199
200 PNS_DBG( dbg, AddShape, &ff, WHITE, 100000, "via-force-coll" );
201 }
202
203 iter++;
204 }
205
206 if( iter == aMaxIterations )
207 return false;
208
209 PNS_DBG( dbg, AddPoint, ( Pos() + totalForce ), WHITE, 1000000, "via-force-new" );
210
211 aForce = totalForce;
212
213 return true;
214}
215
216
217const SHAPE_LINE_CHAIN VIA::Hull( int aClearance, int aWalkaroundThickness, int aLayer ) const
218{
219 wxASSERT_MSG( aLayer >= 0 || m_stackMode == STACK_MODE::NORMAL,
220 wxT( "Warning: VIA::Hull called with invalid layer but viastack is complex" ) );
221
222 int cl = ( aClearance + aWalkaroundThickness / 2 );
223 int width = Diameter( aLayer );
224
225 if( m_hole && !ROUTER::GetInstance()->GetInterface()->IsFlashedOnLayer( this, aLayer ) )
226 width = m_hole->Radius() * 2;
227
228 // Chamfer = width * ( 1 - sqrt(2)/2 ) for equilateral octagon
229 return OctagonalHull( m_pos - VECTOR2I( width / 2, width / 2 ),
230 VECTOR2I( width, width ),
231 cl, ( 2 * cl + width ) * ( 1.0 - M_SQRT1_2 ) );
232}
233
234
236{
237 VIA* v = new VIA();
238
239 v->m_uid = m_uid; // fixme: oop
240 v->SetNet( Net() );
241 v->SetLayers( Layers() );
242 v->m_pos = m_pos;
245 v->m_drill = m_drill;
246
247 for( const auto& [layer, shape] : m_shapes )
248 v->m_shapes[layer] = SHAPE_CIRCLE( m_pos, shape.GetRadius() );
249
251 v->m_rank = m_rank;
252 v->m_marker = m_marker;
253 v->m_viaType = m_viaType;
254 v->m_parent = m_parent;
255 v->m_isFree = m_isFree;
257
258 return v;
259}
260
261
262OPT_BOX2I VIA::ChangedArea( const VIA* aOther ) const
263{
264 if( aOther->Pos() != Pos() )
265 {
266 BOX2I tmp;
267
268 for( int layer : UniqueShapeLayers() )
269 tmp.Merge( Shape( layer )->BBox() );
270
271 for( int layer : aOther->UniqueShapeLayers() )
272 tmp.Merge( aOther->Shape( layer )->BBox() );
273
274 return tmp;
275 }
276
277 return OPT_BOX2I();
278}
279
280
282{
283 VIA_HANDLE h;
284 h.pos = Pos();
285 h.layers = Layers();
286 h.net = Net();
287 h.valid = true;
288 return h;
289}
290
291
292const std::string VIA::Format( ) const
293{
294 std::stringstream ss;
295 ss << ITEM::Format() << " drill " << m_drill << " ";
296 // TODO(JE) padstacks
297 ss << Shape( 0 )->Format( false );
298 return ss.str();
299}
300
301}
std::optional< BOX2I > OPT_BOX2I
Definition: box2.h:926
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:658
int Radius() const
Definition: pns_hole.cpp:103
static HOLE * MakeCircularHole(const VECTOR2I &pos, int radius, PNS_LAYER_RANGE aLayers)
Definition: pns_hole.cpp:131
Base class for PNS router board items.
Definition: pns_item.h:97
void SetLayers(const PNS_LAYER_RANGE &aLayers)
Definition: pns_item.h:200
virtual const std::string Format() const
Definition: pns_item.cpp:329
bool m_isVirtual
Definition: pns_item.h:313
virtual const SHAPE * Shape(int aLayer) const
Return the geometrical shape of the item.
Definition: pns_item.h:229
const PNS_LAYER_RANGE & Layers() const
Definition: pns_item.h:199
virtual NET_HANDLE Net() const
Definition: pns_item.h:197
PNS_LAYER_RANGE m_layers
Definition: pns_item.h:306
std::set< int > RelevantShapeLayers(const ITEM *aOther) const
Returns the set of layers on which either this or the other item can have a unique shape.
Definition: pns_item.cpp:94
void SetNet(NET_HANDLE aNet)
Definition: pns_item.h:196
int m_marker
Definition: pns_item.h:310
BOARD_ITEM * m_parent
Definition: pns_item.h:305
int m_rank
Definition: pns_item.h:311
Keep the router "world" - i.e.
Definition: pns_node.h:231
int GetClearance(const ITEM *aA, const ITEM *aB, bool aUseClearanceEpsilon=true) const
Return the pre-set worst case clearance between any pair of items.
Definition: pns_node.cpp:129
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Check if the item collides with anything else in the world, and if found, returns the obstacle.
Definition: pns_node.cpp:410
std::optional< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:241
virtual DEBUG_DECORATOR * GetDebugDecorator()=0
ROUTER_IFACE * GetInterface() const
Definition: pns_router.h:223
static ROUTER * GetInstance()
Definition: pns_router.cpp:81
VIA()
Definition: pns_via.h:79
int Diameter(int aLayer) const
Definition: pns_via.h:191
OPT_BOX2I ChangedArea(const VIA *aOther) const
Definition: pns_via.cpp:262
std::vector< int > UniqueShapeLayers() const override
Return a list of layers that have unique (potentially different) shapes.
Definition: pns_via.cpp:56
int m_drill
Definition: pns_via.h:279
const VECTOR2I & Pos() const
Definition: pns_via.h:175
virtual void SetHole(HOLE *aHole) override
Definition: pns_via.h:255
std::map< int, int > m_diameters
May contain 1..n diameters depending on m_stackMode.
Definition: pns_via.h:276
const VIA_HANDLE MakeHandle() const
Definition: pns_via.cpp:281
const SHAPE * Shape(int aLayer) const override
Return the geometrical shape of the item.
Definition: pns_via.h:229
virtual const std::string Format() const override
Definition: pns_via.cpp:292
bool PushoutForce(NODE *aNode, const VECTOR2I &aDirection, VECTOR2I &aForce, int aCollisionMask=ITEM::ANY_T, int aMaxIterations=10)
Definition: pns_via.cpp:125
std::map< int, SHAPE_CIRCLE > m_shapes
Definition: pns_via.h:277
int EffectiveLayer(int aLayer) const
Definition: pns_via.cpp:33
void SetPos(const VECTOR2I &aPos)
Definition: pns_via.h:177
VIATYPE m_viaType
Definition: pns_via.h:281
HOLE * m_hole
Definition: pns_via.h:283
bool PadstackMatches(const VIA &aOther) const
Definition: pns_via.cpp:90
VECTOR2I m_pos
Definition: pns_via.h:280
const SHAPE_LINE_CHAIN Hull(int aClearance=0, int aWalkaroundThickness=0, int aLayer=-1) const override
Definition: pns_via.cpp:217
STACK_MODE
Definition: pns_via.h:62
static constexpr int ALL_LAYERS
Definition: pns_via.h:76
static constexpr int INNER_LAYERS
Definition: pns_via.h:77
void SetStackMode(STACK_MODE aStackMode)
Definition: pns_via.cpp:78
bool m_isFree
Definition: pns_via.h:282
STACK_MODE m_stackMode
Definition: pns_via.h:273
VIA * Clone() const override
Return a deep copy of the item.
Definition: pns_via.cpp:235
int Start() const
Definition: pns_layerset.h:86
bool Overlaps(const PNS_LAYER_RANGE &aOther) const
Definition: pns_layerset.h:67
int End() const
Definition: pns_layerset.h:91
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
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:181
virtual const std::string Format(bool aCplusPlus=true) const
Definition: shape.cpp:46
virtual const BOX2I BBox(int aClearance=0) const =0
Compute a bounding box of the shape, with a margin of aClearance a collision.
constexpr extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition: vector2d.h:307
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:283
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:385
@ WHITE
Definition: color4d.h:48
@ YELLOW
Definition: color4d.h:67
Push and Shove diff pair dimensions (gap) settings dialog.
const SHAPE_LINE_CHAIN OctagonalHull(const VECTOR2I &aP0, const VECTOR2I &aSize, int aClearance, int aChamfer)
Definition: pns_utils.cpp:36
#define PNS_DBG(dbg, method,...)
VECTOR2I pos
Definition: pns_via.h:53
NET_HANDLE net
Definition: pns_via.h:55
PNS_LAYER_RANGE layers
Definition: pns_via.h:54
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691