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 (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_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 int lineWidthH = 0;
126 bool collisionsFound = false;
127
128 if( this == aHead ) // we cannot be self-colliding
129 return false;
130
131 if ( !shouldWeConsiderHoleCollisions( this, aHead ) )
132 return false;
133
134 // Special cases for "head" lines with vias attached at the end. Note that this does not
135 // support head-line-via to head-line-via collisions, but you can't route two independent
136 // tracks at once so it shouldn't come up.
137
138 if( const auto line = dyn_cast<const LINE*>( this ) )
139 {
140 if( line->EndsWithVia() )
141 collisionsFound |= line->Via().collideSimple( aHead, aNode, aLayer, aCtx );
142 }
143
144 if( const auto line = dyn_cast<const LINE*>( aHead ) )
145 {
146 if( line->EndsWithVia() )
147 collisionsFound |= line->Via().collideSimple( this, aNode, aLayer, aCtx );
148 }
149
150 // And a special case for the "head" via's hole.
151 if( holeH && shouldWeConsiderHoleCollisions( this, holeH ) )
152 {
153 if( Net() != holeH->Net() && collideSimple( holeH, aNode, aLayer, aCtx ) )
154 collisionsFound = true;
155 }
156
157 // Sadly collision routines ignore SHAPE_POLY_LINE widths so we have to pass them in as part
158 // of the clearance value.
159 if( m_kind == LINE_T )
160 lineWidthI = static_cast<const LINE*>( this )->Width() / 2;
161
162 if( aHead->m_kind == LINE_T )
163 lineWidthH = static_cast<const LINE*>( aHead )->Width() / 2;
164
165 // check if we are not on completely different layers first
166 if( !m_layers.Overlaps( aHead->m_layers ) )
167 return false;
168
169 // fixme: this f***ing singleton must go...
170 ROUTER* router = ROUTER::GetInstance();
171 ROUTER_IFACE* iface = router ? router->GetInterface() : nullptr;
172 bool differentNetsOnly = true;
173 bool enforce = false;
174 int clearance;
175
176 if( aCtx )
177 differentNetsOnly = aCtx->options.m_differentNetsOnly;
178
179 // Hole-to-hole collisions don't have anything to do with nets
180 if( Kind() == HOLE_T && aHead->Kind() == HOLE_T )
181 differentNetsOnly = false;
182
183 if( differentNetsOnly && Net() == aHead->Net() && aHead->Net() )
184 {
185 // same nets? no clearance!
186 clearance = -1;
187 }
188 else if( differentNetsOnly && ( IsFreePad() || aHead->IsFreePad() ) )
189 {
190 // a pad associated with a "free" pin (NIC) doesn't have a net until it has been used
191 clearance = -1;
192 }
193 else if( aNode->GetRuleResolver()->IsKeepout( this, aHead, &enforce ) )
194 {
195 if( enforce )
196 clearance = 0; // keepouts are exact boundary; no clearance
197 else
198 clearance = -1;
199 }
200 else if( iface && !iface->IsFlashedOnLayer( this, aHead->Layers() ) )
201 {
202 clearance = -1;
203 }
204 else if( iface && !iface->IsFlashedOnLayer( aHead, Layers() ) )
205 {
206 clearance = -1;
207 }
208 else if( aCtx && aCtx->options.m_overrideClearance >= 0 )
209 {
210 clearance = aCtx->options.m_overrideClearance;
211 }
212 else
213 {
214 clearance = aNode->GetClearance( this, aHead, aCtx ? aCtx->options.m_useClearanceEpsilon
215 : false );
216 }
217
218 if( clearance >= 0 )
219 {
220 // Note: we can't do castellation or net-tie processing in GetClearance() because they
221 // depend on *where* the collision is.
222
223 bool checkCastellation = ( m_parent && m_parent->GetLayer() == Edge_Cuts )
224 || aNode->GetRuleResolver()->IsNonPlatedSlot( this );
225
226 bool checkNetTie = aNode->GetRuleResolver()->IsInNetTie( this );
227
228 const SHAPE* shapeI = Shape( aLayer );
229 const SHAPE* shapeH = aHead->Shape( aLayer );
230
231 if( checkCastellation || checkNetTie )
232 {
233 // Slow method
234 int actual;
235 VECTOR2I pos;
236
237 // The extra "1" here is to account for the fact that the hulls are built to exactly
238 // the clearance distance, so we need to allow for no collision when exactly at the
239 // clearance distance.
240 if( shapeH->Collide( shapeI, clearance + lineWidthH + lineWidthI - 1, &actual, &pos ) )
241 {
242 if( checkCastellation && aNode->QueryEdgeExclusions( pos ) )
243 return false;
244
245 if( checkNetTie && aNode->GetRuleResolver()->IsNetTieExclusion( aHead, pos, this ) )
246 return false;
247
248 if( aCtx )
249 {
250 collisionsFound = true;
251 OBSTACLE obs;
252 obs.m_head = const_cast<ITEM*>( aHead );
253 obs.m_item = const_cast<ITEM*>( this );
254 obs.m_clearance = clearance;
255 obs.m_distFirst = 0;
256 obs.m_maxFanoutWidth = 0;
257 aCtx->obstacles.insert( obs );
258 }
259 else
260 {
261 return true;
262 }
263 }
264 }
265 else
266 {
267 // Fast method
268 // The extra "1" here is to account for the fact that the hulls are built to exactly
269 // the clearance distance, so we need to allow for no collision when exactly at the
270 // clearance distance.
271 if( shapeH->Collide( shapeI, clearance + lineWidthH + lineWidthI - 1 ) )
272 {
273 if( aCtx )
274 {
275 collisionsFound = true;
276 OBSTACLE obs;
277 obs.m_head = const_cast<ITEM*>( aHead );
278 obs.m_item = const_cast<ITEM*>( this );
279 obs.m_clearance = clearance;
280 obs.m_distFirst = 0;
281 obs.m_maxFanoutWidth = 0;
282 aCtx->obstacles.insert( obs );
283 }
284 else
285 {
286 return true;
287 }
288 }
289 }
290 }
291
292 return collisionsFound;
293}
294
295
296bool ITEM::Collide( const ITEM* aOther, const NODE* aNode, int aLayer,
297 COLLISION_SEARCH_CONTEXT *aCtx ) const
298{
299 if( collideSimple( aOther, aNode, aLayer, aCtx ) )
300 return true;
301
302 return false;
303}
304
305
306std::string ITEM::KindStr() const
307{
308 switch( m_kind )
309 {
310 case ARC_T: return "arc";
311 case LINE_T: return "line";
312 case SEGMENT_T: return "segment";
313 case VIA_T: return "via";
314 case JOINT_T: return "joint";
315 case SOLID_T: return "solid";
316 case DIFF_PAIR_T: return "diff-pair";
317 case HOLE_T: return "hole";
318
319 default: return "unknown";
320 }
321}
322
323
325{
326}
327
328
329const std::string ITEM::Format() const
330{
331 ROUTER* router = ROUTER::GetInstance();
332 ROUTER_IFACE* iface = router ? router->GetInterface() : nullptr;
333
334 std::stringstream ss;
335 ss << KindStr() << " ";
336
337 if( iface )
338 ss << "net " << iface->GetNetName( Net() ) << " ";
339
340 ss << "layers " << m_layers.Start() << " " << m_layers.End();
341 return ss.str();
342}
343
344
345const NODE* ITEM::OwningNode() const
346{
347 if( ParentPadVia() )
348 return static_cast<const NODE*>( ParentPadVia()->Owner() );
349 else
350 return static_cast<const NODE*>( Owner() );
351}
352
354{
355 static UNIQ_ID uidCount = 0; // fixme: make atomic
356 return uidCount++;
357}
358
359} // namespace PNS
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:237
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:97
bool IsFreePad() const
Definition: pns_item.h:275
PnsKind m_kind
Definition: pns_item.h:303
virtual bool HasUniqueShapeLayers() const
Definition: pns_item.h:239
virtual const std::string Format() const
Definition: pns_item.cpp:329
virtual ITEM * ParentPadVia() const
Definition: pns_item.h:280
virtual std::vector< int > UniqueShapeLayers() const
Return a list of layers that have unique (potentially different) shapes.
Definition: pns_item.h:237
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
PnsKind Kind() const
Return the type (kind) of the item.
Definition: pns_item.h:170
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:106
@ DIFF_PAIR_T
Definition: pns_item.h:109
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:296
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:324
virtual const NODE * OwningNode() const
Definition: pns_item.cpp:345
bool OfKind(int aKindMask) const
Definition: pns_item.h:178
std::string KindStr() const
Definition: pns_item.cpp:306
virtual HOLE * Hole() const
Definition: pns_item.h:291
BOARD_ITEM * m_parent
Definition: pns_item.h:305
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:353
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:71
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