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-2021 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 <optional>
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"
30#include "pns_diff_pair.h"
31#include "pns_router.h"
32#include "pns_solid.h"
33
34namespace PNS {
35
37 MEANDER_PLACER_BASE( aRouter )
38{
39 m_world = nullptr;
40 m_currentNode = nullptr;
41
42 m_padToDieP = 0;
43 m_padToDieN = 0;
44
45 // Init temporary variables (do not leave uninitialized members)
46 m_initialSegment = nullptr;
47 m_lastLength = 0;
49}
50
51
53{
54}
55
56
58{
59 return m_currentTraceP;
60}
61
62
63NODE* DP_MEANDER_PLACER::CurrentNode( bool aLoopsRemoved ) const
64{
65 if( !m_currentNode )
66 return m_world;
67
68 return m_currentNode;
69}
70
71
72bool DP_MEANDER_PLACER::Start( const VECTOR2I& aP, ITEM* aStartItem )
73{
74 if( !aStartItem || !aStartItem->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
75 {
76 Router()->SetFailureReason( _( "Please select a track whose length you want to tune." ) );
77 return false;
78 }
79
80 m_initialSegment = static_cast<LINKED_ITEM*>( aStartItem );
81 m_currentNode = nullptr;
83
84 m_world = Router()->GetWorld()->Branch();
85
86 TOPOLOGY topo( m_world );
87
89 {
90 Router()->SetFailureReason( _( "Unable to find complementary differential pair "
91 "net for length tuning. Make sure the names of the nets "
92 "belonging to a differential pair end with either _N/_P "
93 "or +/-." ) );
94 return false;
95 }
96
97 if( m_originPair.Gap() < 0 )
98 m_originPair.SetGap( Router()->Sizes().DiffPairGap() );
99
101 return false;
102
104
105 m_padToDieP = 0;
106
107 if( m_startPad_p )
109
110 if( m_endPad_p )
112
114
115 m_padToDieN = 0;
116
117 if( m_startPad_n )
119
120 if( m_endPad_n )
122
125
127
128 return true;
129}
130
131
133{
134}
135
136
138{
139 long long int totalP = m_padToDieP + lineLength( m_tunedPathP, m_startPad_p, m_endPad_p );
140 long long int totalN = m_padToDieN + lineLength( m_tunedPathN, m_startPad_n, m_endPad_n );
141 return std::max( totalP, totalN );
142}
143
144
146{
147 const VECTOR2I a( ( aCoupledSegs.coupledP.A + aCoupledSegs.coupledN.A ) / 2 );
148 const VECTOR2I b( ( aCoupledSegs.coupledP.B + aCoupledSegs.coupledN.B ) / 2 );
149
150 return SEG( a, b );
151}
152
153
155{
156 VECTOR2I midp = ( aPair.coupledP.A + aPair.coupledN.A ) / 2;
157
158 //DrawDebugPoint(midp, 6);
159
160 return aPair.coupledP.Side( midp ) > 0;
161}
162
163
164bool DP_MEANDER_PLACER::Move( const VECTOR2I& aP, ITEM* aEndItem )
165{
166// return false;
167
168 DIFF_PAIR::COUPLED_SEGMENTS_VEC coupledSegments;
169
170 if( m_currentNode )
171 delete m_currentNode;
172
174
175 SHAPE_LINE_CHAIN preP, tunedP, postP;
176 SHAPE_LINE_CHAIN preN, tunedN, postN;
177
178 cutTunedLine( m_originPair.CP(), m_currentStart, aP, preP, tunedP, postP );
179 cutTunedLine( m_originPair.CN(), m_currentStart, aP, preN, tunedN, postN );
180
181 auto updateStatus =
182 [&]()
183 {
186
187 if( comp > 0 )
189 else if( comp < 0 )
191 else
193 };
194
195 DIFF_PAIR tuned( m_originPair );
196
197 tuned.SetShape( tunedP, tunedN );
198
199 tuned.CoupledSegmentPairs( coupledSegments );
200
201 if( coupledSegments.size() == 0 )
202 {
203 // Tuning started at an uncoupled area of the DP; we won't get a valid result until the
204 // cursor is moved far enough along a coupled area. Prevent the track from disappearing and
205 // the length from being zero by just using the original.
209 updateStatus();
210
211 return false;
212 }
213
214 m_result = MEANDERED_LINE( this, true );
215 m_result.SetWidth( tuned.Width() );
216
217 int offset = ( tuned.Gap() + tuned.Width() ) / 2;
218
219 if( pairOrientation( coupledSegments[0] ) )
220 offset *= -1;
221
222 m_result.SetBaselineOffset( offset );
223
224 for( const ITEM* item : m_tunedPathP.CItems() )
225 {
226 if( const LINE* l = dyn_cast<const LINE*>( item ) )
227 PNS_DBG( Dbg(), AddShape, &l->CLine(), YELLOW, 10000, wxT( "tuned-path-p" ) );
228 }
229
230 for( const ITEM* item : m_tunedPathN.CItems() )
231 {
232 if( const LINE* l = dyn_cast<const LINE*>( item ) )
233 PNS_DBG( Dbg(), AddShape, &l->CLine(), YELLOW, 10000, wxT( "tuned-path-n" ) );
234 }
235
236 int curIndexP = 0, curIndexN = 0;
237
238 for( const DIFF_PAIR::COUPLED_SEGMENTS& sp : coupledSegments )
239 {
240 SEG base = baselineSegment( sp );
241
242 PNS_DBG( Dbg(), AddShape, base, GREEN, 10000, wxT( "dp-baseline" ) );
243
244 while( sp.indexP >= curIndexP && curIndexP != -1 )
245 {
246 if( tunedP.IsArcSegment( curIndexP ) )
247 {
248 ssize_t arcIndex = tunedP.ArcIndex( curIndexP );
249
250 m_result.AddArcAndPt( tunedP.Arc( arcIndex ), tunedN.CPoint( curIndexN ) );
251 }
252 else
253 {
254 m_result.AddCorner( tunedP.CPoint( curIndexP ), tunedN.CPoint( curIndexN ) );
255 }
256
257 curIndexP = tunedP.NextShape( curIndexP );
258 }
259
260 while( sp.indexN >= curIndexN && curIndexN != -1 )
261 {
262 if( tunedN.IsArcSegment( curIndexN ) )
263 {
264 ssize_t arcIndex = tunedN.ArcIndex( curIndexN );
265
266 m_result.AddPtAndArc( tunedP.CPoint( sp.indexP ), tunedN.Arc( arcIndex ) );
267 }
268 else
269 {
270 m_result.AddCorner( tunedP.CPoint( sp.indexP ), tunedN.CPoint( curIndexN ) );
271 }
272
273 curIndexN = tunedN.NextShape( curIndexN );
274 }
275
276 m_result.MeanderSegment( base, base.Side( aP ) < 0 );
277 }
278
279 while( curIndexP < tunedP.PointCount() && curIndexP != -1 )
280 {
281 if( tunedP.IsArcSegment( curIndexP ) )
282 {
283 ssize_t arcIndex = tunedP.ArcIndex( curIndexP );
284
285 m_result.AddArcAndPt( tunedP.Arc( arcIndex ), tunedN.CPoint( curIndexN ) );
286 }
287 else
288 {
289 m_result.AddCorner( tunedP.CPoint( curIndexP ), tunedN.CPoint( curIndexN ) );
290 }
291
292 curIndexP = tunedP.NextShape( curIndexP );
293 }
294
295 while( curIndexN < tunedN.PointCount() && curIndexN != -1 )
296 {
297 if( tunedN.IsArcSegment( curIndexN ) )
298 {
299 ssize_t arcIndex = tunedN.ArcIndex( curIndexN );
300
301 m_result.AddPtAndArc( tunedP.CPoint( -1 ), tunedN.Arc( arcIndex ) );
302 }
303 else
304 {
305 m_result.AddCorner( tunedP.CPoint( -1 ), tunedN.CPoint( curIndexN ) );
306 }
307
308 curIndexN = tunedN.NextShape( curIndexN );
309 }
310
311 long long int dpLen = origPathLength();
312
314
316 {
318 m_lastLength = dpLen;
319 }
320 else
321 {
322 m_lastLength = dpLen - std::max( tunedP.Length(), tunedN.Length() );
324 }
325
326 if( m_lastStatus != TOO_LONG )
327 {
328 tunedP.Clear();
329 tunedN.Clear();
330
331 for( MEANDER_SHAPE* m : m_result.Meanders() )
332 {
333 if( m->Type() != MT_EMPTY )
334 {
335 tunedP.Append( m->CLine( 0 ) );
336 tunedN.Append( m->CLine( 1 ) );
337 }
338 }
339
340 m_lastLength += std::max( tunedP.Length(), tunedN.Length() );
341 updateStatus();
342 }
343
345 m_finalShapeP.Append( preP );
346 m_finalShapeP.Append( tunedP );
347 m_finalShapeP.Append( postP );
349
351 m_finalShapeN.Append( preN );
352 m_finalShapeN.Append( tunedN );
353 m_finalShapeN.Append( postN );
355
356 return true;
357}
358
359
360bool DP_MEANDER_PLACER::FixRoute( const VECTOR2I& aP, ITEM* aEndItem, bool aForceFinish )
361{
364
365 m_currentNode->Add( lP );
366 m_currentNode->Add( lN );
367
369
370 return true;
371}
372
373
375{
377 return true;
378}
379
380
382{
383 return m_originPair.CP().SegmentCount() > 0 || m_originPair.CN().SegmentCount() > 0;
384}
385
386
388{
389 if( m_currentNode )
391
392 m_currentNode = nullptr;
393 return true;
394}
395
396
398{
399 LINE l1( m_originPair.PLine(), aShape->CLine( 0 ) );
400 LINE l2( m_originPair.NLine(), aShape->CLine( 1 ) );
401
402 if( m_currentNode->CheckColliding( &l1 ) )
403 return false;
404
405 if( m_currentNode->CheckColliding( &l2 ) )
406 return false;
407
408 int w = aShape->Width();
409 int clearance = w + m_settings.m_spacing;
410
411 return m_result.CheckSelfIntersections( aShape, clearance );
412}
413
414
416{
419
420 ITEM_SET traces;
421
422 traces.Add( &m_currentTraceP );
423 traces.Add( &m_currentTraceN );
424
425 return traces;
426}
427
428
430{
431 return m_currentStart;
432}
433
434
436{
437 return m_currentEnd;
438}
439
440
442{
443 return m_initialSegment->Layers().Start();
444}
445
446
447const wxString DP_MEANDER_PLACER::TuningInfo( EDA_UNITS aUnits ) const
448{
449 wxString status;
450
451 switch( m_lastStatus )
452 {
453 case TOO_LONG:
454 status = _( "Too long: " );
455 break;
456 case TOO_SHORT:
457 status = _("Too short: " );
458 break;
459 case TUNED:
460 status = _( "Tuned: " );
461 break;
462 default:
463 return _( "?" );
464 }
465
467 status += wxT( "/" );
469 status += wxT( " (gap: " );
471 status += wxT( ")" );
472
473 return status;
474}
475
476
478{
479 return m_lastStatus;
480}
481
482
483const std::vector<int> DP_MEANDER_PLACER::CurrentNets() const
484{
485 std::vector<int> rv;
486 rv.push_back( m_originPair.NetP() );
487 rv.push_back( m_originPair.NetN() );
488 return rv;
489}
490
491}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
int Start() const
Definition: pns_layerset.h:82
ROUTER * Router() const
Return current router settings.
Definition: pns_algo_base.h:54
DEBUG_DECORATOR * Dbg() const
Definition: pns_algo_base.h:78
Basic class for a differential pair.
const SHAPE_LINE_CHAIN & CN() const
int Width() const
std::vector< COUPLED_SEGMENTS > COUPLED_SEGMENTS_VEC
int NetN() const
void SetShape(const SHAPE_LINE_CHAIN &aP, const SHAPE_LINE_CHAIN &aN, bool aSwapLanes=false)
int Gap() const
int NetP() const
void SetGap(int aGap)
const SHAPE_LINE_CHAIN & CP() const
void CoupledSegmentPairs(COUPLED_SEGMENTS_VEC &aPairs) const
bool Start(const VECTOR2I &aP, ITEM *aStartItem) override
Start routing a single track at point aP, taking item aStartItem as anchor (unless NULL).
bool CheckFit(MEANDER_SHAPE *aShape) override
Checks if it's OK to place the shape aShape (i.e.
const ITEM_SET Traces() override
Function Traces()
bool pairOrientation(const DIFF_PAIR::COUPLED_SEGMENTS &aPair)
const std::vector< int > CurrentNets() const override
Function CurrentNets()
DP_MEANDER_PLACER(ROUTER *aRouter)
VECTOR2I m_currentStart
Current world state.
bool FixRoute(const VECTOR2I &aP, ITEM *aEndItem, bool aForceFinish=false) override
Commit the currently routed track to the parent node, taking aP as the final end point and aEndItem a...
int CurrentLayer() const override
Function CurrentLayer()
TUNING_STATUS TuningStatus() const override
Return the tuning status (too short, too long, etc.) of the trace(s) being tuned.
const SEG baselineSegment(const DIFF_PAIR::COUPLED_SEGMENTS &aCoupledSegs)
const wxString TuningInfo(EDA_UNITS aUnits) const override
Return a string describing the status and length of the tuned traces.
bool HasPlacedAnything() const override
bool Move(const VECTOR2I &aP, ITEM *aEndItem) override
Move the end of the currently routed trace to the point aP, taking aEndItem as anchor (if not NULL).
const VECTOR2I & CurrentEnd() const override
Function CurrentEnd()
const VECTOR2I & CurrentStart() const override
Function CurrentStart()
long long int origPathLength() const
Current routing start point (end of tail, beginning of head).
NODE * CurrentNode(bool aLoopsRemoved=false) const override
Return the most recent world state.
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
const ENTRIES & CItems() const
Definition: pns_itemset.h:136
Base class for PNS router board items.
Definition: pns_item.h:56
@ SEGMENT_T
Definition: pns_item.h:66
const LAYER_RANGE & Layers() const
Definition: pns_item.h:156
bool OfKind(int aKindMask) const
Return true if the item's type matches the mask aKindMask.
Definition: pns_item.h:140
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:61
int SegmentCount() const
Definition: pns_line.h:144
Represent a set of meanders fitted over a single or two lines.
Definition: pns_meander.h:412
void SetBaselineOffset(int aOffset)
Set the parallel offset between the base segment and the meandered line.
Definition: pns_meander.h:507
void SetWidth(int aWidth)
Set the line width.
Definition: pns_meander.h:492
void AddCorner(const VECTOR2I &aA, const VECTOR2I &aB=VECTOR2I(0, 0))
Create a dummy meander shape representing a line corner.
void MeanderSegment(const SEG &aSeg, bool aSide, int aBaseIndex=0)
Fit maximum amplitude meanders on a given segment and adds to the current line.
Definition: pns_meander.cpp:45
void AddArcAndPt(const SHAPE_ARC &aArc1, const VECTOR2I &aPt2)
Create a dummy meander shape representing an arc corner.
bool CheckSelfIntersections(MEANDER_SHAPE *aShape, int aClearance)
Check if the given shape is intersecting with any other meander in the current line.
std::vector< MEANDER_SHAPE * > & Meanders()
Definition: pns_meander.h:515
void AddPtAndArc(const VECTOR2I &aPt1, const SHAPE_ARC &aArc2)
Create a dummy meander shape representing an arc corner.
Base class for Single trace & Differential pair meandering tools, as both of them share a lot of code...
void tuneLineLength(MEANDERED_LINE &aTuned, long long int aElongation)
Take a set of meanders in aTuned and tunes their length to extend the original line length by aElonga...
TUNING_STATUS
< Result of the length tuning operation
int m_currentWidth
Meander settings.
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)
Extract the part of a track to be meandered, depending on the starting point and the cursor position.
MEANDER_SETTINGS m_settings
The current end point.
int compareWithTolerance(long long int aValue, long long int aExpected, long long int aTolerance=0) const
Compare aValue against aExpected with given tolerance.
NODE * m_world
Width of the meandered trace(s).
VECTOR2I getSnappedStartPoint(LINKED_ITEM *aStartItem, VECTOR2I aStartPoint)
long long int lineLength(const ITEM_SET &aLine, const SOLID *aStartPad, const SOLID *aEndPad) const
Calculate the total length of the line represented by an item set (tracks and vias)
long long int m_targetLength
Type of corners for the meandered line.
Definition: pns_meander.h:93
int m_lengthTolerance
Target skew value for diff pair de-skewing.
Definition: pns_meander.h:105
int m_spacing
Amplitude/spacing adjustment step.
Definition: pns_meander.h:84
The geometry of a single meander.
Definition: pns_meander.h:115
int Width() const
Definition: pns_meander.h:297
const SHAPE_LINE_CHAIN & CLine(int aShape) const
Definition: pns_meander.h:237
Keep the router "world" - i.e.
Definition: pns_node.h:156
NODE * Branch()
Create a lightweight copy (called branch) of self that tracks the changes (added/removed items) wrs t...
Definition: pns_node.cpp:139
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Check if the item collides with anything else in the world, and if found, returns the obstacle.
Definition: pns_node.cpp:472
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Add an item to the current node.
Definition: pns_node.cpp:664
void KillChildren()
Definition: pns_node.cpp:1467
void Remove(ARC *aArc)
Remove an item from this branch.
Definition: pns_node.cpp:889
void SetFailureReason(const wxString &aReason)
Definition: pns_router.h:209
void CommitRouting()
Definition: pns_router.cpp:895
NODE * GetWorld() const
Definition: pns_router.h:160
int GetPadToDie() const
Definition: pns_solid.h:103
const DIFF_PAIR AssembleDiffPair(SEGMENT *aStart)
const ITEM_SET AssembleTuningPath(ITEM *aStart, SOLID **aStartPad=nullptr, SOLID **aEndPad=nullptr)
Like AssembleTrivialPath, but follows the track length algorithm, which discards segments that are fu...
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
int Side(const VECTOR2I &aP) const
Determine on which side of directed line passing via segment ends point aP lies.
Definition: seg.h:143
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const SHAPE_ARC & Arc(size_t aArc) const
SHAPE_LINE_CHAIN & Simplify(bool aRemoveColinear=true)
Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
int NextShape(int aPointIndex, bool aForwards=true) const
Return the vertex index of the next shape in the chain, or -1 if aPointIndex is the last shape.
int PointCount() const
Return the number of points (vertices) in this line chain.
ssize_t ArcIndex(size_t aSegment) const
Return the arc index for the given segment index.
void Clear()
Remove all points from the line chain.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
int SegmentCount() const
Return the number of segments in this line chain.
bool IsArcSegment(size_t aSegment) const
long long int Length() const
Return length of the line chain in Euclidean metric.
@ GREEN
Definition: color4d.h:57
@ YELLOW
Definition: color4d.h:67
#define _(s)
EDA_UNITS
Definition: eda_units.h:43
wxString MessageTextFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A helper to convert the double length aValue to a string in inches, millimeters, or unscaled units.
Definition: eda_units.cpp:326
Push and Shove diff pair dimensions (gap) settings dialog.
@ MT_EMPTY
Definition: pns_meander.h:46
#define PNS_DBG(dbg, method,...)