KiCad PCB EDA Suite
pns_topology.cpp
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2015 CERN
5  * Copyright (C) 2016 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 "pns_line.h"
23 #include "pns_segment.h"
24 #include "pns_node.h"
25 #include "pns_joint.h"
26 #include "pns_solid.h"
27 #include "pns_router.h"
28 #include "pns_utils.h"
29 
30 #include "pns_diff_pair.h"
31 #include "pns_topology.h"
32 
33 #include <class_board.h>
34 
35 namespace PNS {
36 
38 {
39  if( !aLine->IsLinked() || !aLine->SegmentCount() )
40  return false;
41 
42  LINKED_ITEM* root = aLine->GetLink( 0 );
43  LINE l = m_world->AssembleLine( root );
44  SHAPE_LINE_CHAIN simplified( l.CLine() );
45 
46  simplified.Simplify();
47 
48  if( simplified.PointCount() != l.PointCount() )
49  {
50  m_world->Remove( l );
51  LINE lnew( l );
52  lnew.SetShape( simplified );
53  m_world->Add( lnew );
54  return true;
55  }
56 
57  return false;
58 }
59 
60 
62 {
63  std::deque<JOINT*> searchQueue;
64  JOINT_SET processed;
65 
66  searchQueue.push_back( aStart );
67  processed.insert( aStart );
68 
69  while( !searchQueue.empty() )
70  {
71  JOINT* current = searchQueue.front();
72  searchQueue.pop_front();
73 
74  for( ITEM* item : current->LinkList() )
75  {
76  if( item->OfKind( ITEM::SEGMENT_T ) )
77  {
78  SEGMENT* seg = static_cast<SEGMENT*>( item );
79  JOINT* a = m_world->FindJoint( seg->Seg().A, seg );
80  JOINT* b = m_world->FindJoint( seg->Seg().B, seg );
81  JOINT* next = ( *a == *current ) ? b : a;
82 
83  if( processed.find( next ) == processed.end() )
84  {
85  processed.insert( next );
86  searchQueue.push_back( next );
87  }
88  }
89  }
90  }
91 
92  return processed;
93 }
94 
95 
96 bool TOPOLOGY::LeadingRatLine( const LINE* aTrack, SHAPE_LINE_CHAIN& aRatLine )
97 {
98  LINE track( *aTrack );
99  VECTOR2I end;
100 
101  if( !track.PointCount() )
102  return false;
103 
104  std::unique_ptr<NODE> tmpNode( m_world->Branch() );
105  tmpNode->Add( track );
106 
107  JOINT* jt = tmpNode->FindJoint( track.CPoint( -1 ), &track );
108 
109  if( !jt )
110  return false;
111 
112  if( ( !track.EndsWithVia() && jt->LinkCount() >= 2 ) || ( track.EndsWithVia() && jt->LinkCount() >= 3 ) ) // we got something connected
113  {
114  end = jt->Pos();
115  }
116  else
117  {
118  int anchor;
119 
120  TOPOLOGY topo( tmpNode.get() );
121  ITEM* it = topo.NearestUnconnectedItem( jt, &anchor );
122 
123  if( !it )
124  return false;
125 
126  end = it->Anchor( anchor );
127  }
128 
129  aRatLine.Clear();
130  aRatLine.Append( track.CPoint( -1 ) );
131  aRatLine.Append( end );
132  return true;
133 }
134 
135 
136 ITEM* TOPOLOGY::NearestUnconnectedItem( JOINT* aStart, int* aAnchor, int aKindMask )
137 {
138  std::set<ITEM*> disconnected;
139 
140  m_world->AllItemsInNet( aStart->Net(), disconnected );
141 
142  for( const JOINT* jt : ConnectedJoints( aStart ) )
143  {
144  for( ITEM* link : jt->LinkList() )
145  {
146  if( disconnected.find( link ) != disconnected.end() )
147  disconnected.erase( link );
148  }
149  }
150 
151  int best_dist = INT_MAX;
152  ITEM* best = NULL;
153 
154  for( ITEM* item : disconnected )
155  {
156  if( item->OfKind( aKindMask ) )
157  {
158  for( int i = 0; i < item->AnchorCount(); i++ )
159  {
160  VECTOR2I p = item->Anchor( i );
161  int d = ( p - aStart->Pos() ).EuclideanNorm();
162 
163  if( d < best_dist )
164  {
165  best_dist = d;
166  best = item;
167 
168  if( aAnchor )
169  *aAnchor = i;
170  }
171  }
172  }
173  }
174 
175  return best;
176 }
177 
178 
179 bool TOPOLOGY::followTrivialPath( LINE* aLine, bool aLeft, ITEM_SET& aSet, std::set<ITEM*>& aVisited )
180 {
181  assert( aLine->IsLinked() );
182 
183  VECTOR2I anchor = aLeft ? aLine->CPoint( 0 ) : aLine->CPoint( -1 );
184  LINKED_ITEM* last =
185  aLeft ? aLine->LinkedSegments().front() : aLine->LinkedSegments().back();
186  JOINT* jt = m_world->FindJoint( anchor, aLine );
187 
188  assert( jt != NULL );
189 
190  aVisited.insert( last );
191 
192  if( jt->IsNonFanoutVia() || jt->IsTraceWidthChange() )
193  {
194  ITEM* via = NULL;
195  SEGMENT* next_seg = NULL;
196 
197  for( ITEM* link : jt->Links().Items() )
198  {
199  if( link->OfKind( ITEM::VIA_T ) )
200  via = link;
201  else if( aVisited.find( link ) == aVisited.end() )
202  next_seg = static_cast<SEGMENT*>( link );
203  }
204 
205  if( !next_seg )
206  return false;
207 
208  LINE l = m_world->AssembleLine( next_seg );
209 
210  VECTOR2I nextAnchor = ( aLeft ? l.CLine().CPoint( -1 ) : l.CLine().CPoint( 0 ) );
211 
212  if( nextAnchor != anchor )
213  {
214  l.Reverse();
215  }
216 
217  if( aLeft )
218  {
219  if( via )
220  aSet.Prepend( via );
221 
222  aSet.Prepend( l );
223  }
224  else
225  {
226  if( via )
227  aSet.Add( via );
228 
229  aSet.Add( l );
230  }
231 
232  return followTrivialPath( &l, aLeft, aSet, aVisited );
233  }
234 
235  return false;
236 }
237 
238 
240 {
241  ITEM_SET path;
242  std::set<ITEM*> visited;
243  SEGMENT* seg;
244  VIA* via;
245 
246  seg = dyn_cast<SEGMENT*> (aStart);
247 
248  if(!seg && (via = dyn_cast<VIA*>( aStart ) ) )
249  {
250  JOINT *jt = m_world->FindJoint( via->Pos(), via );
251 
252  if( !jt->IsNonFanoutVia() )
253  return ITEM_SET();
254 
255  for( const auto& entry : jt->Links().Items() )
256  if( ( seg = dyn_cast<SEGMENT*>( entry.item ) ) )
257  break;
258  }
259 
260  if( !seg )
261  return ITEM_SET();
262 
263  LINE l = m_world->AssembleLine( seg );
264 
265  path.Add( l );
266 
267  followTrivialPath( &l, false, path, visited );
268  followTrivialPath( &l, true, path, visited );
269 
270  return path;
271 }
272 
273 
274 const ITEM_SET TOPOLOGY::ConnectedItems( JOINT* aStart, int aKindMask )
275 {
276  return ITEM_SET();
277 }
278 
279 
280 const ITEM_SET TOPOLOGY::ConnectedItems( ITEM* aStart, int aKindMask )
281 {
282  return ITEM_SET();
283 }
284 
285 
286 bool commonParallelProjection( SEG p, SEG n, SEG &pClip, SEG& nClip );
287 
288 
290 {
291  int refNet = aStart->Net();
292  int coupledNet = m_world->GetRuleResolver()->DpCoupledNet( refNet );
293 
294  if( coupledNet < 0 )
295  return false;
296 
297  std::set<ITEM*> coupledItems;
298 
299  m_world->AllItemsInNet( coupledNet, coupledItems );
300 
301  SEGMENT* coupledSeg = NULL, *refSeg;
302  int minDist = std::numeric_limits<int>::max();
303 
304  if( ( refSeg = dyn_cast<SEGMENT*>( aStart ) ) != NULL )
305  {
306  for( ITEM* item : coupledItems )
307  {
308  if( SEGMENT* s = dyn_cast<SEGMENT*>( item ) )
309  {
310  if( s->Layers().Start() == refSeg->Layers().Start() && s->Width() == refSeg->Width() )
311  {
312  int dist = s->Seg().Distance( refSeg->Seg() );
313  bool isParallel = refSeg->Seg().ApproxParallel( s->Seg() );
314  SEG p_clip, n_clip;
315 
316  bool isCoupled = commonParallelProjection( refSeg->Seg(), s->Seg(), p_clip, n_clip );
317 
318  if( isParallel && isCoupled && dist < minDist )
319  {
320  minDist = dist;
321  coupledSeg = s;
322  }
323  }
324  }
325  }
326  }
327  else
328  {
329  return false;
330  }
331 
332  if( !coupledSeg )
333  return false;
334 
335  LINE lp = m_world->AssembleLine( refSeg );
336  LINE ln = m_world->AssembleLine( coupledSeg );
337 
338  if( m_world->GetRuleResolver()->DpNetPolarity( refNet ) < 0 )
339  {
340  std::swap( lp, ln );
341  }
342 
343  int gap = -1;
344 
345  if( refSeg->Seg().ApproxParallel( coupledSeg->Seg() ) )
346  {
347  // Segments are parallel -> compute pair gap
348  const VECTOR2I refDir = refSeg->Anchor( 1 ) - refSeg->Anchor( 0 );
349  const VECTOR2I displacement = refSeg->Anchor( 1 ) - coupledSeg->Anchor( 1 );
350  gap = (int) std::abs( refDir.Cross( displacement ) / refDir.EuclideanNorm() ) - lp.Width();
351  }
352 
353  aPair = DIFF_PAIR( lp, ln );
354  aPair.SetWidth( lp.Width() );
355  aPair.SetLayers( lp.Layers() );
356  aPair.SetGap( gap );
357 
358  return true;
359 }
360 
361 const std::set<ITEM*> TOPOLOGY::AssembleCluster( ITEM* aStart, int aLayer )
362 {
363  std::set<ITEM*> visited;
364  std::deque<ITEM*> pending;
365 
366  pending.push_back( aStart );
367 
368  while( !pending.empty() )
369  {
370  NODE::OBSTACLES obstacles;
371  ITEM* top = pending.front();
372 
373  pending.pop_front();
374 
375  visited.insert( top );
376 
377  m_world->QueryColliding( top, obstacles, ITEM::ANY_T, -1, false );
378 
379  for( OBSTACLE& obs : obstacles )
380  {
381  if( visited.find( obs.m_item ) == visited.end() && obs.m_item->Layers().Overlaps( aLayer ) && !( obs.m_item->Marker() & MK_HEAD ) )
382  {
383  visited.insert( obs.m_item );
384  pending.push_back( obs.m_item );
385  }
386  }
387  }
388 
389  return visited;
390 }
391 
392 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:128
const SHAPE_LINE_CHAIN & CLine() const
Const accessor to the underlying shape
Definition: pns_line.h:144
const JOINT_SET ConnectedJoints(JOINT *aStart)
CITER next(CITER it)
Definition: ptree.cpp:130
extended_type Cross(const VECTOR2< T > &aVector) const
Function Cross() computes cross product of self with aVector.
Definition: vector2d.h:484
ITEM.
Definition: pns_item.h:53
bool IsTraceWidthChange() const
Definition: pns_joint.h:127
static const int dist[10][10]
Definition: ar_matrix.cpp:326
bool followTrivialPath(LINE *aLine, bool aLeft, ITEM_SET &aSet, std::set< ITEM * > &aVisited)
int SegmentCount() const
Returns the number of segments in the line
Definition: pns_line.h:150
const ITEM_SET ConnectedItems(JOINT *aStart, int aKindMask=ITEM::ANY_T)
void Prepend(const LINE &aLine)
Definition: pns_itemset.cpp:39
virtual int DpCoupledNet(int aNet)=0
const DIFF_PAIR AssembleDiffPair(SEGMENT *aStart)
LINKED_ITEM * GetLink(int aIndex) const
Definition: pns_line.h:231
ENTRIES & Items()
Definition: pns_itemset.h:140
void AllItemsInNet(int aNet, std::set< ITEM * > &aItems)
Definition: pns_node.cpp:1294
ITEM * NearestUnconnectedItem(JOINT *aStart, int *aAnchor=NULL, int aKindMask=ITEM::ANY_T)
const SEG & Seg() const
Definition: pns_segment.h:83
int Net() const
Definition: pns_joint.h:191
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
NODE * Branch()
Function Branch()
Definition: pns_node.cpp:106
const VECTOR2I & Pos() const
Definition: pns_via.h:100
int PointCount() const
Returns the number of points in the line
Definition: pns_line.h:156
bool EndsWithVia() const
Definition: pns_line.h:276
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
void Remove(ARC *aArc)
Function Remove()
Definition: pns_node.cpp:796
const VECTOR2I & CPoint(int aIdx) const
Returns the aIdx-th point of the line
Definition: pns_line.h:168
const std::set< ITEM * > AssembleCluster(ITEM *aStart, int aLayer)
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Assigns a shape to the line (a polyline/line chain)
Definition: pns_line.h:125
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
ITEM_SET & Links()
Definition: pns_joint.h:206
const VECTOR2I & CPoint(int aIndex) const
Function Point()
JOINT.
Definition: pns_joint.h:43
void SetGap(int aGap)
const ITEM_SET AssembleTrivialPath(ITEM *aStart)
void Reverse()
Reverses the point/vertex order
Definition: pns_line.cpp:851
const LINE AssembleLine(LINKED_ITEM *aSeg, int *aOriginSegmentIndex=NULL, bool aStopAtLockedJoints=false)
Function AssembleLine()
Definition: pns_node.cpp:893
#define NULL
bool LeadingRatLine(const LINE *aTrack, SHAPE_LINE_CHAIN &aRatLine)
virtual VECTOR2I Anchor(int n) const override
Definition: pns_segment.h:106
int Net() const
Definition: pns_item.h:149
NODE * m_world
Definition: pns_topology.h:71
void SetWidth(int aWidth)
std::unordered_set< SCH_ITEM * > ITEM_SET
Definition: sch_item.h:138
const LINKED_ITEMS & LinkList() const
Definition: pns_joint.h:196
void SetLayers(const LAYER_RANGE &aLayers)
Definition: pns_item.h:152
JOINT * FindJoint(const VECTOR2I &aPos, int aLayer, int aNet)
Function FindJoint()
Definition: pns_node.cpp:1027
bool SimplifyLine(LINE *aLine)
RULE_RESOLVER * GetRuleResolver() const
Definition: pns_node.h:176
Definition: seg.h:39
DIFF_PAIR.
SEGMENT_REFS & LinkedSegments()
Returns the list of segments from the owning node that constitute this line (or NULL if the line is n...
Definition: pns_line.h:209
SHAPE_LINE_CHAIN.
bool commonParallelProjection(SEG p, SEG n, SEG &pClip, SEG &nClip)
VECTOR2I A
Definition: seg.h:47
int Width() const
Returns line width
Definition: pns_line.h:187
void Clear()
Function Clear() Removes all points from the line chain.
virtual int DpNetPolarity(int aNet)=0
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:299
int LinkCount(int aMask=-1) const
Definition: pns_joint.h:211
const VECTOR2I & Pos() const
Definition: pns_joint.h:186
bool IsLinked() const
Definition: pns_line.h:214
std::vector< OBSTACLE > OBSTACLES
Definition: pns_node.h:150
Push and Shove diff pair dimensions (gap) settings dialog.
bool IsNonFanoutVia() const
Definition: pns_joint.h:113
int QueryColliding(const ITEM *aItem, OBSTACLES &aObstacles, int aKindMask=ITEM::ANY_T, int aLimitCount=-1, bool aDifferentNetsOnly=true, int aForceClearance=-1)
Function QueryColliding()
Definition: pns_node.cpp:278
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Function Add()
Definition: pns_node.cpp:620
const LAYER_RANGE & Layers() const
Definition: pns_item.h:151
std::set< JOINT * > JOINT_SET
Definition: pns_topology.h:42
Struct OBSTACLE.
Definition: pns_node.h:80
VECTOR2I B
Definition: seg.h:48