KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_item.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 The 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_node.h"
23#include "pns_item.h"
24#include "pns_line.h"
25#include "pns_router.h"
26
29
31
32namespace PNS {
33
34static void dumpObstacles( const PNS::NODE::OBSTACLES &obstacles )
35{
36 printf( "&&&& %zu obstacles: \n", obstacles.size() );
37
38 for( const auto& obs : obstacles )
39 {
40 printf( "%p [%s] - %p [%s], clearance %d\n",
41 obs.m_head, obs.m_head->KindStr().c_str(),
42 obs.m_item, obs.m_item->KindStr().c_str(),
43 obs.m_clearance );
44 }
45}
46
47
48// prune self-collisions, i.e. a via/pad annular ring with its own hole
49static bool shouldWeConsiderHoleCollisions( const ITEM* aItem, const ITEM* aHead )
50{
51 const HOLE* holeI = aItem->OfKind( ITEM::HOLE_T ) ? static_cast<const HOLE*>( aItem ) : nullptr;
52 const HOLE* holeH = aHead->OfKind( ITEM::HOLE_T ) ? static_cast<const HOLE*>( aHead ) : nullptr;
53
54 if( holeI && holeH ) // hole-to-hole case
55 {
56 const ITEM* parentI = holeI->ParentPadVia();
57 const ITEM* parentH = holeH->ParentPadVia();
58
59 if( !parentH || !parentI )
60 return true;
61
62 const VIA* parentViaI = dyn_cast<const VIA*>( parentI );
63 const VIA* parentViaH = dyn_cast<const VIA*>( parentH );
64
65 // Note to self: the if() below is an ugly heuristic to determine if we aren't trying
66 // to check for collisions of the hole of the via with another (although identical)
67 // copy of it. Such case occurs when checking a LINE against a NODE where this LINE
68 // has been already added. LINE has no notion of ownership of it's via (it's just a
69 // copy) and before hole-to-hole clearance support has been introduced it didn't matter
70 // becasue we didn't consider collisions of the objects belonging to the same net anyway
71 // Now that hole clearance check doesn't care about the nets assigned to the parent
72 // vias/solids, I'll probably have to refactor the LINE class to manage ownership of
73 // its (optional) VIA. For the moment, we just treat via holes that are geometrically
74 // identical and belonging to the same net as non-colliding.
75
76 if( parentViaI && parentViaH && parentViaI->Pos() == parentViaH->Pos()
77 && parentViaI->PadstackMatches( *parentViaH )
78 && parentViaI->Net() == parentViaH->Net()
79 && parentViaI->Drill() == parentViaH->Drill() )
80 return false;
81
82 return parentI != parentH;
83 }
84
85 if( holeI )
86 return holeI->ParentPadVia() != aHead;
87 else if( holeH )
88 return holeH->ParentPadVia() != aItem;
89 else
90 return true;
91}
92
93
94std::set<int> ITEM::RelevantShapeLayers( const ITEM* aOther ) const
95{
96 std::vector<int> myLayers = UniqueShapeLayers();
97 std::vector<int> otherLayers = aOther->UniqueShapeLayers();
98
99 if( !HasUniqueShapeLayers() && !aOther->HasUniqueShapeLayers() )
100 return { -1 };
101
102 // TODO(JE) at this point we should also mask off the layers of each item.
103 // In the case that one item is a via and the other is a track, we don't want to test
104 // more than once even if the via has multiple unique layers
105
106 std::set<int> relevantLayers;
107
108 std::set_union( myLayers.begin(), myLayers.end(), otherLayers.begin(), otherLayers.end(),
109 std::inserter( relevantLayers, relevantLayers.begin() ) );
110
111 return relevantLayers;
112}
113
114
115bool ITEM::collideSimple( const ITEM* aHead, const NODE* aNode, int aLayer,
116 COLLISION_SEARCH_CONTEXT* aCtx ) const
117{
118 // Note: if 'this' is a pad or a via then its hole is a separate PNS::ITEM in the node's
119 // index and we don't need to deal with holeI here. The same is *not* true of the routing
120 // "head", so we do need to handle holeH.
121 int lineWidthI = 0;
122
123 //const SHAPE* shapeH = aHead->Shape();
124 const HOLE* holeH = aHead->Hole();
125 const HOLE* holeI = Hole();
126
127 int lineWidthH = 0;
128 bool collisionsFound = false;
129
130 if( this == aHead ) // we cannot be self-colliding
131 return false;
132
133 if ( !shouldWeConsiderHoleCollisions( this, aHead ) )
134 return false;
135
136 // Special cases for "head" lines with vias attached at the end. Note that this does not
137 // support head-line-via to head-line-via collisions, but you can't route two independent
138 // tracks at once so it shouldn't come up.
139
140 if( const auto line = dyn_cast<const LINE*>( this ) )
141 {
142 if( line->EndsWithVia() )
143 collisionsFound |= line->Via().collideSimple( aHead, aNode, aLayer, aCtx );
144 }
145
146 if( const auto line = dyn_cast<const LINE*>( aHead ) )
147 {
148 if( line->EndsWithVia() )
149 collisionsFound |= line->Via().collideSimple( this, aNode, aLayer, aCtx );
150 }
151
152 // And a special case for the "head" via's hole.
153 if( aHead->HasHole() && shouldWeConsiderHoleCollisions( this, holeH ) )
154 {
155 if( Net() != holeH->Net() && collideSimple( holeH, aNode, aLayer, aCtx ) )
156 collisionsFound = true;
157 }
158 if( HasHole() && shouldWeConsiderHoleCollisions( holeI, aHead ) )
159 {
160 collisionsFound |= holeI->collideSimple( aHead, aNode, aLayer, aCtx );
161 }
162
163 // Sadly collision routines ignore SHAPE_POLY_LINE widths so we have to pass them in as part
164 // of the clearance value.
165 if( m_kind == LINE_T )
166 lineWidthI = static_cast<const LINE*>( this )->Width() / 2;
167
168 if( aHead->m_kind == LINE_T )
169 lineWidthH = static_cast<const LINE*>( aHead )->Width() / 2;
170
171 // check if we are not on completely different layers first
172 if( !m_layers.Overlaps( aHead->m_layers ) )
173 return false;
174
175 // fixme: this f***ing singleton must go...
176 ROUTER* router = ROUTER::GetInstance();
177 ROUTER_IFACE* iface = router ? router->GetInterface() : nullptr;
178 bool differentNetsOnly = true;
179 bool enforce = false;
180 int clearance;
181
182 if( aCtx )
183 differentNetsOnly = aCtx->options.m_differentNetsOnly;
184
185 // Hole-to-hole collisions don't have anything to do with nets
186 if( Kind() == HOLE_T && aHead->Kind() == HOLE_T )
187 differentNetsOnly = false;
188
189 if( differentNetsOnly && Net() == aHead->Net() && aHead->Net() )
190 {
191 // same nets? no clearance!
192 clearance = -1;
193 }
194 else if( differentNetsOnly && ( IsFreePad() || aHead->IsFreePad() ) )
195 {
196 // a pad associated with a "free" pin (NIC) doesn't have a net until it has been used
197 clearance = -1;
198 }
199 else if( aNode->GetRuleResolver()->IsKeepout( this, aHead, &enforce )
200 || aNode->GetRuleResolver()->IsKeepout( aHead, this, &enforce ) )
201 {
202 if( enforce )
203 clearance = 0; // keepouts are exact boundary; no clearance
204 else
205 clearance = -1;
206 }
207 else if( iface && !iface->IsFlashedOnLayer( this, aHead->Layers() ) )
208 {
209 clearance = -1;
210 }
211 else if( iface && !iface->IsFlashedOnLayer( aHead, Layers() ) )
212 {
213 clearance = -1;
214 }
215 else if( aCtx && aCtx->options.m_overrideClearance >= 0 )
216 {
218 }
219 else
220 {
221 clearance = aNode->GetClearance( this, aHead, aCtx ? aCtx->options.m_useClearanceEpsilon
222 : false );
223 }
224
225 if( clearance >= 0 )
226 {
227 // Note: we can't do castellation or net-tie processing in GetClearance() because they
228 // depend on *where* the collision is.
229
230 bool checkCastellation = ( m_parent && m_parent->GetLayer() == Edge_Cuts )
231 || aNode->GetRuleResolver()->IsNonPlatedSlot( this );
232
233 bool checkNetTie = aNode->GetRuleResolver()->IsInNetTie( this );
234
235 const SHAPE* shapeI = Shape( aLayer );
236 const SHAPE* shapeH = aHead->Shape( aLayer );
237
238 if( checkCastellation || checkNetTie )
239 {
240 // Slow method
241 int actual;
242 VECTOR2I pos;
243
244 // The extra "1" here is to account for the fact that the hulls are built to exactly
245 // the clearance distance, so we need to allow for no collision when exactly at the
246 // clearance distance.
247 if( shapeH->Collide( shapeI, clearance + lineWidthH + lineWidthI - 1, &actual, &pos ) )
248 {
249 if( checkCastellation && aNode->QueryEdgeExclusions( pos ) )
250 return false;
251
252 if( checkNetTie && aNode->GetRuleResolver()->IsNetTieExclusion( aHead, pos, this ) )
253 return false;
254
255 if( aCtx )
256 {
257 collisionsFound = true;
258 OBSTACLE obs;
259 obs.m_head = const_cast<ITEM*>( aHead );
260 obs.m_item = const_cast<ITEM*>( this );
262 obs.m_distFirst = 0;
263 obs.m_maxFanoutWidth = 0;
264 aCtx->obstacles.insert( obs );
265 }
266 else
267 {
268 return true;
269 }
270 }
271 }
272 else
273 {
274 // Fast method
275 // The extra "1" here is to account for the fact that the hulls are built to exactly
276 // the clearance distance, so we need to allow for no collision when exactly at the
277 // clearance distance.
278 if( shapeH->Collide( shapeI, clearance + lineWidthH + lineWidthI - 1 ) )
279 {
280 if( aCtx )
281 {
282 collisionsFound = true;
283 OBSTACLE obs;
284 obs.m_head = const_cast<ITEM*>( aHead );
285 obs.m_item = const_cast<ITEM*>( this );
287 obs.m_distFirst = 0;
288 obs.m_maxFanoutWidth = 0;
289 aCtx->obstacles.insert( obs );
290 }
291 else
292 {
293 return true;
294 }
295 }
296 }
297 }
298
299 return collisionsFound;
300}
301
302
303bool ITEM::Collide( const ITEM* aOther, const NODE* aNode, int aLayer,
304 COLLISION_SEARCH_CONTEXT *aCtx ) const
305{
306 if( collideSimple( aOther, aNode, aLayer, aCtx ) )
307 return true;
308
309 return false;
310}
311
312
313std::string ITEM::KindStr() const
314{
315 switch( m_kind )
316 {
317 case ARC_T: return "arc";
318 case LINE_T: return "line";
319 case SEGMENT_T: return "segment";
320 case VIA_T: return "via";
321 case JOINT_T: return "joint";
322 case SOLID_T: return "solid";
323 case DIFF_PAIR_T: return "diff-pair";
324 case HOLE_T: return "hole";
325
326 default: return "unknown";
327 }
328}
329
330
332{
333}
334
335
336const std::string ITEM::Format() const
337{
338 ROUTER* router = ROUTER::GetInstance();
339 ROUTER_IFACE* iface = router ? router->GetInterface() : nullptr;
340
341 std::stringstream ss;
342 ss << KindStr() << " ";
343
344 if( iface )
345 ss << "net " << iface->GetNetName( Net() ) << " ";
346
347 ss << "layers " << m_layers.Start() << " " << m_layers.End();
348 return ss.str();
349}
350
351
352const NODE* ITEM::OwningNode() const
353{
354 if( ParentPadVia() )
355 return static_cast<const NODE*>( ParentPadVia()->Owner() );
356 else
357 return static_cast<const NODE*>( Owner() );
358}
359
361{
362 static UNIQ_ID uidCount = 0; // fixme: make atomic
363 return uidCount++;
364}
365
366} // namespace PNS
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:239
virtual NET_HANDLE Net() const override
Definition: pns_hole.h:56
ITEM * ParentPadVia() const override
Definition: pns_hole.h:72
Base class for PNS router board items.
Definition: pns_item.h:98
bool IsFreePad() const
Definition: pns_item.h:276
PnsKind m_kind
Definition: pns_item.h:304
virtual bool HasUniqueShapeLayers() const
Definition: pns_item.h:240
virtual const std::string Format() const
Definition: pns_item.cpp:336
virtual ITEM * ParentPadVia() const
Definition: pns_item.h:281
virtual std::vector< int > UniqueShapeLayers() const
Return a list of layers that have unique (potentially different) shapes.
Definition: pns_item.h:238
virtual const SHAPE * Shape(int aLayer) const
Return the geometrical shape of the item.
Definition: pns_item.h:230
const PNS_LAYER_RANGE & Layers() const
Definition: pns_item.h:200
virtual NET_HANDLE Net() const
Definition: pns_item.h:198
PNS_LAYER_RANGE m_layers
Definition: pns_item.h:307
PnsKind Kind() const
Return the type (kind) of the item.
Definition: pns_item.h:171
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
@ SEGMENT_T
Definition: pns_item.h:107
@ DIFF_PAIR_T
Definition: pns_item.h:110
bool Collide(const ITEM *aHead, const NODE *aNode, int aLayer, COLLISION_SEARCH_CONTEXT *aCtx=nullptr) const
Check for a collision (clearance violation) with between us and item aOther.
Definition: pns_item.cpp:303
bool collideSimple(const ITEM *aHead, const NODE *aNode, int aLayer, COLLISION_SEARCH_CONTEXT *aCtx) const
Definition: pns_item.cpp:115
virtual ~ITEM()
Definition: pns_item.cpp:331
virtual const NODE * OwningNode() const
Definition: pns_item.cpp:352
bool OfKind(int aKindMask) const
Definition: pns_item.h:179
std::string KindStr() const
Definition: pns_item.cpp:313
virtual HOLE * Hole() const
Definition: pns_item.h:292
virtual bool HasHole() const
Definition: pns_item.h:291
BOARD_ITEM * m_parent
Definition: pns_item.h:306
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:62
static UNIQ_ID genNextUid()
Definition: pns_item.cpp:360
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
bool QueryEdgeExclusions(const VECTOR2I &aPos) const
Definition: pns_node.cpp:715
RULE_RESOLVER * GetRuleResolver() const
Return the number of joints.
Definition: pns_node.h:269
std::set< OBSTACLE > OBSTACLES
Definition: pns_node.h:243
const ITEM_OWNER * Owner() const
Return the owner of this item, or NULL if there's none.
Definition: pns_item.h:72
virtual wxString GetNetName(PNS::NET_HANDLE aNet) const =0
virtual bool IsFlashedOnLayer(const PNS::ITEM *aItem, int aLayer) const =0
ROUTER_IFACE * GetInterface() const
Definition: pns_router.h:223
static ROUTER * GetInstance()
Definition: pns_router.cpp:81
virtual bool IsNonPlatedSlot(const PNS::ITEM *aItem)=0
virtual bool IsNetTieExclusion(const ITEM *aItem, const VECTOR2I &aCollisionPos, const ITEM *aCollidingItem)=0
virtual bool IsKeepout(const ITEM *aObstacle, const ITEM *aItem, bool *aEnforce)=0
virtual bool IsInNetTie(const ITEM *aA)=0
const VECTOR2I & Pos() const
Definition: pns_via.h:175
int Drill() const
Definition: pns_via.h:211
bool PadstackMatches(const VIA &aOther) const
Definition: pns_via.cpp:90
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
An abstract shape on 2D plane.
Definition: shape.h:126
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
VECTOR2_TRAITS< int32_t >::extended_type extended_type
Definition: vector2d.h:73
@ Edge_Cuts
Definition: layer_ids.h:112
Push and Shove diff pair dimensions (gap) settings dialog.
static void dumpObstacles(const PNS::NODE::OBSTACLES &obstacles)
Definition: pns_item.cpp:34
static bool shouldWeConsiderHoleCollisions(const ITEM *aItem, const ITEM *aHead)
Definition: pns_item.cpp:49
VECTOR2I::extended_type ecoord
Definition: pns_item.cpp:30
const COLLISION_SEARCH_OPTIONS options
Definition: pns_node.h:133
std::set< OBSTACLE > & obstacles
Definition: pns_node.h:132
Hold an object colliding with another object, along with some useful data about the collision.
Definition: pns_node.h:87
int m_distFirst
... and the distance thereof
Definition: pns_node.h:93
int m_clearance
Definition: pns_node.h:91
int m_maxFanoutWidth
worst case (largest) width of the tracks connected to the item
Definition: pns_node.h:94
ITEM * m_head
Line we search collisions against.
Definition: pns_node.h:88
ITEM * m_item
Item found to be colliding with m_head.
Definition: pns_node.h:89
int clearance
int actual