KiCad PCB EDA Suite
pns_dp_meander_placer.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 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 <core/optional.h>
23 
24 #include <base_units.h> // God forgive me doing this...
25 
26 #include "pns_node.h"
27 #include "pns_itemset.h"
28 #include "pns_topology.h"
29 #include "pns_dp_meander_placer.h"
30 #include "pns_diff_pair.h"
31 #include "pns_router.h"
32 #include "pns_utils.h"
33 
34 namespace PNS {
35 
37  MEANDER_PLACER_BASE( aRouter )
38 {
39  m_world = NULL;
40  m_currentNode = NULL;
41 
42  // Init temporary variables (do not leave uninitialized members)
43  m_initialSegment = NULL;
44  m_lastLength = 0;
46 }
47 
48 
50 {
51 }
52 
53 
55 {
56  return m_currentTraceP;
57 }
58 
59 
60 NODE* DP_MEANDER_PLACER::CurrentNode( bool aLoopsRemoved ) const
61 {
62  if( !m_currentNode )
63  return m_world;
64 
65  return m_currentNode;
66 }
67 
68 
69 bool DP_MEANDER_PLACER::Start( const VECTOR2I& aP, ITEM* aStartItem )
70 {
71  VECTOR2I p;
72 
73  if( !aStartItem || !aStartItem->OfKind( ITEM::SEGMENT_T ) )
74  {
75  Router()->SetFailureReason( _( "Please select a track whose length you want to tune." ) );
76  return false;
77  }
78 
79  m_initialSegment = static_cast<SEGMENT*>( aStartItem );
80 
81  p = m_initialSegment->Seg().NearestPoint( aP );
82 
83  m_currentNode=NULL;
84  m_currentStart = p;
85 
86  m_world = Router()->GetWorld()->Branch();
87 
88  TOPOLOGY topo( m_world );
89 
91  {
92  Router()->SetFailureReason( _( "Unable to find complementary differential pair "
93  "net for length tuning. Make sure the names of the nets belonging "
94  "to a differential pair end with either _N/_P or +/-." ) );
95  return false;
96  }
97 
98  if( m_originPair.Gap() < 0 )
99  m_originPair.SetGap( Router()->Sizes().DiffPairGap() );
100 
101  if( !m_originPair.PLine().SegmentCount() ||
103  return false;
104 
107 
110 
112 
113  return true;
114 }
115 
116 
118 {
119 }
120 
121 
123 {
124  int totalP = 0;
125  int totalN = 0;
126 
127  for( const ITEM* item : m_tunedPathP.CItems() )
128  {
129  if( const LINE* l = dyn_cast<const LINE*>( item ) )
130  totalP += l->CLine().Length();
131 
132  }
133 
134  for( const ITEM* item : m_tunedPathN.CItems() )
135  {
136  if( const LINE* l = dyn_cast<const LINE*>( item ) )
137  totalN += l->CLine().Length();
138  }
139 
140  return std::max( totalP, totalN );
141 }
142 
143 
145 {
146  const VECTOR2I a( ( aCoupledSegs.coupledP.A + aCoupledSegs.coupledN.A ) / 2 );
147  const VECTOR2I b( ( aCoupledSegs.coupledP.B + aCoupledSegs.coupledN.B ) / 2 );
148 
149  return SEG( a, b );
150 }
151 
152 
153 static bool pairOrientation( const DIFF_PAIR::COUPLED_SEGMENTS& aPair )
154 {
155  VECTOR2I midp = ( aPair.coupledP.A + aPair.coupledN.A ) / 2;
156 
157  //DrawDebugPoint(midp, 6);
158 
159  return aPair.coupledP.Side( midp ) > 0;
160 }
161 
162 
163 bool DP_MEANDER_PLACER::Move( const VECTOR2I& aP, ITEM* aEndItem )
164 {
165 // return false;
166 
167  DIFF_PAIR::COUPLED_SEGMENTS_VEC coupledSegments;
168 
169  if( m_currentNode )
170  delete m_currentNode;
171 
173 
174  SHAPE_LINE_CHAIN preP, tunedP, postP;
175  SHAPE_LINE_CHAIN preN, tunedN, postN;
176 
177  cutTunedLine( m_originPair.CP(), m_currentStart, aP, preP, tunedP, postP );
178  cutTunedLine( m_originPair.CN(), m_currentStart, aP, preN, tunedN, postN );
179 
180  DIFF_PAIR tuned( m_originPair );
181 
182  tuned.SetShape( tunedP, tunedN );
183 
184  tuned.CoupledSegmentPairs( coupledSegments );
185 
186  if( coupledSegments.size() == 0 )
187  return false;
188 
189  //Router()->DisplayDebugLine( tuned.CP(), 5, 20000 );
190  //Router()->DisplayDebugLine( tuned.CN(), 4, 20000 );
191 
192  //Router()->DisplayDebugLine( m_originPair.CP(), 5, 20000 );
193  //Router()->DisplayDebugLine( m_originPair.CN(), 4, 20000 );
194 
195  m_result = MEANDERED_LINE( this, true );
196  m_result.SetWidth( tuned.Width() );
197 
198  int offset = ( tuned.Gap() + tuned.Width() ) / 2;
199 
200  if( !pairOrientation( coupledSegments[0] ) )
201  offset *= -1;
202 
203  m_result.SetBaselineOffset( offset );
204 
205  for( const ITEM* item : m_tunedPathP.CItems() )
206  {
207  if( const LINE* l = dyn_cast<const LINE*>( item ) )
208  Dbg()->AddLine( l->CLine(), 5, 10000 );
209  }
210 
211  for( const ITEM* item : m_tunedPathN.CItems() )
212  {
213  if( const LINE* l = dyn_cast<const LINE*>( item ) )
214  Dbg()->AddLine( l->CLine(), 5, 10000 );
215  }
216 
217  int curIndexP = 0, curIndexN = 0;
218 
219  for( const DIFF_PAIR::COUPLED_SEGMENTS& sp : coupledSegments )
220  {
221  SEG base = baselineSegment( sp );
222 
223  Dbg()->AddSegment( base, 3 );
224 
225  while( sp.indexP >= curIndexP )
226  {
227  m_result.AddCorner( tunedP.CPoint( curIndexP ), tunedN.CPoint( curIndexN ) );
228  curIndexP++;
229  }
230 
231  while( sp.indexN >= curIndexN )
232  {
233  m_result.AddCorner( tunedP.CPoint( sp.indexP ), tunedN.CPoint( curIndexN ) );
234  curIndexN++;
235  }
236 
237  m_result.MeanderSegment( base );
238  }
239 
240  while( curIndexP < tunedP.PointCount() )
241  m_result.AddCorner( tunedP.CPoint( curIndexP++ ), tunedN.CPoint( curIndexN ) );
242 
243  while( curIndexN < tunedN.PointCount() )
244  m_result.AddCorner( tunedP.CPoint( -1 ), tunedN.CPoint( curIndexN++ ) );
245 
246  int dpLen = origPathLength();
247 
249 
251  {
253  m_lastLength = dpLen;
254  }
255  else
256  {
257  m_lastLength = dpLen - std::max( tunedP.Length(), tunedN.Length() );
259  }
260 
261  if( m_lastStatus != TOO_LONG )
262  {
263  tunedP.Clear();
264  tunedN.Clear();
265 
266  for( MEANDER_SHAPE* m : m_result.Meanders() )
267  {
268  if( m->Type() != MT_EMPTY )
269  {
270  tunedP.Append( m->CLine( 0 ) );
271  tunedN.Append( m->CLine( 1 ) );
272  }
273  }
274 
275  m_lastLength += std::max( tunedP.Length(), tunedN.Length() );
276 
278 
279  if( comp > 0 )
281  else if( comp < 0 )
283  else
285  }
286 
288  m_finalShapeP.Append( preP );
289  m_finalShapeP.Append( tunedP );
290  m_finalShapeP.Append( postP );
292 
294  m_finalShapeN.Append( preN );
295  m_finalShapeN.Append( tunedN );
296  m_finalShapeN.Append( postN );
298 
299  return true;
300 }
301 
302 
303 bool DP_MEANDER_PLACER::FixRoute( const VECTOR2I& aP, ITEM* aEndItem )
304 {
307 
308  m_currentNode->Add( lP );
309  m_currentNode->Add( lN );
310 
312 
313  return true;
314 }
315 
316 
318 {
319  LINE l1( m_originPair.PLine(), aShape->CLine( 0 ) );
320  LINE l2( m_originPair.NLine(), aShape->CLine( 1 ) );
321 
322  if( m_currentNode->CheckColliding( &l1 ) )
323  return false;
324 
325  if( m_currentNode->CheckColliding( &l2 ) )
326  return false;
327 
328  int w = aShape->Width();
329  int clearance = w + m_settings.m_spacing;
330 
331  return m_result.CheckSelfIntersections( aShape, clearance );
332 }
333 
334 
336 {
339 
340  ITEM_SET traces;
341 
342  traces.Add( &m_currentTraceP );
343  traces.Add( &m_currentTraceN );
344 
345  return traces;
346 }
347 
348 
350 {
351  return m_currentEnd;
352 }
353 
354 
356 {
357  return m_initialSegment->Layers().Start();
358 }
359 
360 
361 const wxString DP_MEANDER_PLACER::TuningInfo() const
362 {
363  wxString status;
364 
365  switch( m_lastStatus )
366  {
367  case TOO_LONG:
368  status = _( "Too long: " );
369  break;
370  case TOO_SHORT:
371  status = _("Too short: " );
372  break;
373  case TUNED:
374  status = _( "Tuned: " );
375  break;
376  default:
377  return _( "?" );
378  }
379 
380  status += LengthDoubleToString( (double) m_lastLength, false );
381  status += "/";
382  status += LengthDoubleToString( (double) m_settings.m_targetLength, false );
383  status += " (gap: ";
384  status += LengthDoubleToString( (double) m_originPair.Gap(), false );
385  status += ")";
386 
387  return status;
388 }
389 
390 
392 {
393  return m_lastStatus;
394 }
395 
396 const std::vector<int> DP_MEANDER_PLACER::CurrentNets() const
397 {
398  std::vector<int> rv;
399  rv.push_back( m_originPair.NetP() );
400  rv.push_back( m_originPair.NetN() );
401  return rv;
402 }
403 
404 }
Class ITEM.
Definition: pns_item.h:53
Class MEANDER_PLACER_BASE.
int NetP() const
int Width() const
bool CheckSelfIntersections(MEANDER_SHAPE *aShape, int aClearance)
Function CheckSelfIntersections()
Class NODE.
Definition: pns_node.h:137
const ENTRIES & CItems() const
Definition: pns_itemset.h:138
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:207
Class MEANDER_SETTINGS.
Definition: pns_meander.h:104
Implementation of conversion functions that require both schematic and board internal units...
const std::vector< int > CurrentNets() const override
Function CurrentNets()
int Side(const VECTOR2I &aP) const
Function Side()
Definition: seg.h:132
void AddCorner(const VECTOR2I &aA, const VECTOR2I &aB=VECTOR2I(0, 0))
Function AddCorner()
DEBUG_DECORATOR * Dbg() const
Definition: pns_algo_base.h:70
const DIFF_PAIR AssembleDiffPair(SEGMENT *aStart)
NODE * m_currentNode
Current world state
void MeanderSegment(const SEG &aSeg, int aBaseIndex=0)
Function MeanderSegment()
Definition: pns_meander.cpp:46
VECTOR2I m_currentEnd
current end point
NODE * m_world
pointer to world to search colliding items
const SHAPE_LINE_CHAIN & CN() const
VECTOR2I m_currentStart
current routing start point (end of tail, beginning of head)
SEGMENT * GetLink(int aIndex) const
Definition: pns_line.h:203
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
NODE * Branch()
Function Branch()
Definition: pns_node.cpp:107
TUNING_STATUS
Result of the length tuning operation
NODE * GetWorld() const
Definition: pns_router.h:143
int m_currentWidth
width of the meandered trace(s)
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
bool Start(const VECTOR2I &aP, ITEM *aStartItem) override
Function Start()
bool CheckFit(MEANDER_SHAPE *aShape) override
Function CheckFit()
void SetWidth(int aWidth)
Function SetWidth()
Definition: pns_meander.h:457
const wxString TuningInfo() const override
Function TuningInfo()
const SEG baselineSegment(const DIFF_PAIR::COUPLED_SEGMENTS &aCoupledSegs)
bool OfKind(int aKindMask) const
Function OfKind()
Definition: pns_item.h:130
int NetN() const
const ITEM_SET Traces() override
Function Traces()
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
const VECTOR2I & CurrentEnd() const override
Function CurrentEnd()
virtual void AddLine(const SHAPE_LINE_CHAIN &aLine, int aType=0, int aWidth=0)
void SetGap(int aGap)
std::vector< COUPLED_SEGMENTS > COUPLED_SEGMENTS_VEC
NODE * CurrentNode(bool aLoopsRemoved=false) const override
Function CurrentNode()
const ITEM_SET AssembleTrivialPath(ITEM *aStart)
virtual void AddSegment(SEG aS, int aColor)
int SegmentCount() const
Returns the number of segments in the line
Definition: pns_line.h:129
int Gap() const
std::vector< MEANDER_SHAPE * > & Meanders()
Function Meanders()
Definition: pns_meander.h:483
void tuneLineLength(MEANDERED_LINE &aTuned, int aElongation)
Function tuneLineLength()
void SetFailureReason(const wxString &aReason)
Definition: pns_router.h:213
wxString LengthDoubleToString(double aValue, bool aConvertToMils)
Function LengthDoubleToString is a helper to convert the double length aValue to a string in inches...
Definition: base_units.cpp:123
bool FixRoute(const VECTOR2I &aP, ITEM *aEndItem) override
Function FixRoute()
static bool pairOrientation(const DIFF_PAIR::COUPLED_SEGMENTS &aPair)
int Start() const
Definition: pns_layerset.h:83
ROUTER * Router() const
Returns the instance of our router
Definition: pns_algo_base.h:49
void Remove(SOLID *aSolid)
Function Remove()
Definition: pns_node.cpp:727
Class MEANDERED_LINE.
Definition: pns_meander.h:395
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:364
Definition: seg.h:36
Class DIFF_PAIR.
int compareWithTolerance(int aValue, int aExpected, int aTolerance=0) const
Function compareWithTolerance()
TUNING_STATUS TuningStatus() const override
Function TuningStatus()
MEANDER_SETTINGS m_settings
meandering settings
int m_lengthTolerance
allowable tuning error
Definition: pns_meander.h:90
void CommitRouting(NODE *aNode)
Definition: pns_router.cpp:326
#define max(a, b)
Definition: auxiliary.h:86
Class SHAPE_LINE_CHAIN.
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Function CheckColliding()
Definition: pns_node.cpp:425
VECTOR2I A
Definition: seg.h:46
void Clear()
Function Clear() Removes all points from the line chain.
const SEG & Seg() const
Definition: pns_segment.h:93
int m_targetLength
desired length of the tuned line/diff pair
Definition: pns_meander.h:84
int m_spacing
meandering period/spacing (see dialog picture for explanation)
Definition: pns_meander.h:80
int Width() const
Function Width()
Definition: pns_meander.h:305
bool Move(const VECTOR2I &aP, ITEM *aEndItem) override
Function Move()
int CurrentLayer() const override
Function CurrentLayer()
DP_MEANDER_PLACER(ROUTER *aRouter)
Push and Shove diff pair dimensions (gap) settings dialog.
void SetBaselineOffset(int aOffset)
Function SetBaselineOffset()
Definition: pns_meander.h:473
void Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Function Add()
Definition: pns_node.cpp:594
void cutTunedLine(const SHAPE_LINE_CHAIN &aOrigin, const VECTOR2I &aTuneStart, const VECTOR2I &aCursorPos, SHAPE_LINE_CHAIN &aPre, SHAPE_LINE_CHAIN &aTuned, SHAPE_LINE_CHAIN &aPost)
Function cutTunedLine()
const SHAPE_LINE_CHAIN & CLine(int aShape) const
Function CLine()
Definition: pns_meander.h:241
const SHAPE_LINE_CHAIN & CP() const
VECTOR2I B
Definition: seg.h:47