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( holeH && shouldWeConsiderHoleCollisions( this, holeH ) )
154 {
155 if( Net() != holeH->Net() && collideSimple( holeH, aNode, aLayer, aCtx ) )
156 collisionsFound = true;
157 }
158 if( holeI && 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 {
201 if( enforce )
202 clearance = 0; // keepouts are exact boundary; no clearance
203 else
204 clearance = -1;
205 }
206 else if( iface && !iface->IsFlashedOnLayer( this, aHead->Layers() ) )
207 {
208 clearance = -1;
209 }
210 else if( iface && !iface->IsFlashedOnLayer( aHead, Layers() ) )
211 {
212 clearance = -1;
213 }
214 else if( aCtx && aCtx->options.m_overrideClearance >= 0 )
215 {
216 clearance = aCtx->options.m_overrideClearance;
217 }
218 else
219 {
220 clearance = aNode->GetClearance( this, aHead, aCtx ? aCtx->options.m_useClearanceEpsilon
221 : false );
222 }
223
224 if( clearance >= 0 )
225 {
226 // Note: we can't do castellation or net-tie processing in GetClearance() because they
227 // depend on *where* the collision is.
228
229 bool checkCastellation = ( m_parent && m_parent->GetLayer() == Edge_Cuts )
230 || aNode->GetRuleResolver()->IsNonPlatedSlot( this );
231
232 bool checkNetTie = aNode->GetRuleResolver()->IsInNetTie( this );
233
234 const SHAPE* shapeI = Shape( aLayer );
235 const SHAPE* shapeH = aHead->Shape( aLayer );
236
237 if( checkCastellation || checkNetTie )
238 {
239 // Slow method
240 int actual;
241 VECTOR2I pos;
242
243 // The extra "1" here is to account for the fact that the hulls are built to exactly
244 // the clearance distance, so we need to allow for no collision when exactly at the
245 // clearance distance.
246 if( shapeH->Collide( shapeI, clearance + lineWidthH + lineWidthI - 1, &actual, &pos ) )
247 {
248 if( checkCastellation && aNode->QueryEdgeExclusions( pos ) )
249 return false;
250
251 if( checkNetTie && aNode->GetRuleResolver()->IsNetTieExclusion( aHead, pos, this ) )
252 return false;
253
254 if( aCtx )
255 {
256 collisionsFound = true;
257 OBSTACLE obs;
258 obs.m_head = const_cast<ITEM*>( aHead );
259 obs.m_item = const_cast<ITEM*>( this );
260 obs.m_clearance = clearance;
261 obs.m_distFirst = 0;
262 obs.m_maxFanoutWidth = 0;
263 aCtx->obstacles.insert( obs );
264 }
265 else
266 {
267 return true;
268 }
269 }
270 }
271 else
272 {
273 // Fast method
274 // The extra "1" here is to account for the fact that the hulls are built to exactly
275 // the clearance distance, so we need to allow for no collision when exactly at the
276 // clearance distance.
277 if( shapeH->Collide( shapeI, clearance + lineWidthH + lineWidthI - 1 ) )
278 {
279 if( aCtx )
280 {
281 collisionsFound = true;
282 OBSTACLE obs;
283 obs.m_head = const_cast<ITEM*>( aHead );
284 obs.m_item = const_cast<ITEM*>( this );
285 obs.m_clearance = clearance;
286 obs.m_distFirst = 0;
287 obs.m_maxFanoutWidth = 0;
288 aCtx->obstacles.insert( obs );
289 }
290 else
291 {
292 return true;
293 }
294 }
295 }
296 }
297
298 return collisionsFound;
299}
300
301
302bool ITEM::Collide( const ITEM* aOther, const NODE* aNode, int aLayer,
303 COLLISION_SEARCH_CONTEXT *aCtx ) const
304{
305 if( collideSimple( aOther, aNode, aLayer, aCtx ) )
306 return true;
307
308 return false;
309}
310
311
312std::string ITEM::KindStr() const
313{
314 switch( m_kind )
315 {
316 case ARC_T: return "arc";
317 case LINE_T: return "line";
318 case SEGMENT_T: return "segment";
319 case VIA_T: return "via";
320 case JOINT_T: return "joint";
321 case SOLID_T: return "solid";
322 case DIFF_PAIR_T: return "diff-pair";
323 case HOLE_T: return "hole";
324
325 default: return "unknown";
326 }
327}
328
329
331{
332}
333
334
335const std::string ITEM::Format() const
336{
337 ROUTER* router = ROUTER::GetInstance();
338 ROUTER_IFACE* iface = router ? router->GetInterface() : nullptr;
339
340 std::stringstream ss;
341 ss << KindStr() << " ";
342
343 if( iface )
344 ss << "net " << iface->GetNetName( Net() ) << " ";
345
346 ss << "layers " << m_layers.Start() << " " << m_layers.End();
347 return ss.str();
348}
349
350
351const NODE* ITEM::OwningNode() const
352{
353 if( ParentPadVia() )
354 return static_cast<const NODE*>( ParentPadVia()->Owner() );
355 else
356 return static_cast<const NODE*>( Owner() );
357}
358
360{
361 static UNIQ_ID uidCount = 0; // fixme: make atomic
362 return uidCount++;
363}
364
365} // 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:335
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:302
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:330
virtual const NODE * OwningNode() const
Definition: pns_item.cpp:351
bool OfKind(int aKindMask) const
Definition: pns_item.h:179
std::string KindStr() const
Definition: pns_item.cpp:312
virtual HOLE * Hole() const
Definition: pns_item.h:292
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:359
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