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
124
127
129
130 return true;
131}
132
133
135{
136}
137
138
140{
141 long long int totalP = m_padToDieLength + lineLength( m_tunedPathP, m_startPad_p, m_endPad_p );
142 long long int totalN = m_padToDieLength + lineLength( m_tunedPathN, m_startPad_n, m_endPad_n );
143 return std::max( totalP, totalN );
144}
145
146
148{
149 const VECTOR2I a( ( aCoupledSegs.coupledP.A + aCoupledSegs.coupledN.A ) / 2 );
150 const VECTOR2I b( ( aCoupledSegs.coupledP.B + aCoupledSegs.coupledN.B ) / 2 );
151
152 return SEG( a, b );
153}
154
155
157{
158 VECTOR2I midp = ( aPair.coupledP.A + aPair.coupledN.A ) / 2;
159
160 //DrawDebugPoint(midp, 6);
161
162 return aPair.coupledP.Side( midp ) > 0;
163}
164
165
166bool DP_MEANDER_PLACER::Move( const VECTOR2I& aP, ITEM* aEndItem )
167{
168// return false;
169
170 DIFF_PAIR::COUPLED_SEGMENTS_VEC coupledSegments;
171
172 if( m_currentNode )
173 delete m_currentNode;
174
176
177 SHAPE_LINE_CHAIN preP, tunedP, postP;
178 SHAPE_LINE_CHAIN preN, tunedN, postN;
179
180 cutTunedLine( m_originPair.CP(), m_currentStart, aP, preP, tunedP, postP );
181 cutTunedLine( m_originPair.CN(), m_currentStart, aP, preN, tunedN, postN );
182
183 DIFF_PAIR tuned( m_originPair );
184
185 tuned.SetShape( tunedP, tunedN );
186
187 tuned.CoupledSegmentPairs( coupledSegments );
188
189 if( coupledSegments.size() == 0 )
190 return false;
191
192 m_result = MEANDERED_LINE( this, true );
193 m_result.SetWidth( tuned.Width() );
194
195 int offset = ( tuned.Gap() + tuned.Width() ) / 2;
196
197 if( pairOrientation( coupledSegments[0] ) )
198 offset *= -1;
199
200 m_result.SetBaselineOffset( offset );
201
202 for( const ITEM* item : m_tunedPathP.CItems() )
203 {
204 if( const LINE* l = dyn_cast<const LINE*>( item ) )
205 PNS_DBG( Dbg(), AddShape, &l->CLine(), YELLOW, 10000, wxT( "tuned-path-p" ) );
206 }
207
208 for( const ITEM* item : m_tunedPathN.CItems() )
209 {
210 if( const LINE* l = dyn_cast<const LINE*>( item ) )
211 PNS_DBG( Dbg(), AddShape, &l->CLine(), YELLOW, 10000, wxT( "tuned-path-n" ) );
212 }
213
214 int curIndexP = 0, curIndexN = 0;
215
216 for( const DIFF_PAIR::COUPLED_SEGMENTS& sp : coupledSegments )
217 {
218 SEG base = baselineSegment( sp );
219
220 PNS_DBG( Dbg(), AddShape, base, GREEN, 10000, wxT( "dp-baseline" ) );
221
222 while( sp.indexP >= curIndexP && curIndexP != -1 )
223 {
224 if( tunedP.IsArcSegment( curIndexP ) )
225 {
226 ssize_t arcIndex = tunedP.ArcIndex( curIndexP );
227
228 m_result.AddArcAndPt( tunedP.Arc( arcIndex ), tunedN.CPoint( curIndexN ) );
229 }
230 else
231 {
232 m_result.AddCorner( tunedP.CPoint( curIndexP ), tunedN.CPoint( curIndexN ) );
233 }
234
235 curIndexP = tunedP.NextShape( curIndexP );
236 }
237
238 while( sp.indexN >= curIndexN && curIndexN != -1 )
239 {
240 if( tunedN.IsArcSegment( curIndexN ) )
241 {
242 ssize_t arcIndex = tunedN.ArcIndex( curIndexN );
243
244 m_result.AddPtAndArc( tunedP.CPoint( sp.indexP ), tunedN.Arc( arcIndex ) );
245 }
246 else
247 {
248 m_result.AddCorner( tunedP.CPoint( sp.indexP ), tunedN.CPoint( curIndexN ) );
249 }
250
251 curIndexN = tunedN.NextShape( curIndexN );
252 }
253
254 m_result.MeanderSegment( base, base.Side( aP ) < 0 );
255 }
256
257 while( curIndexP < tunedP.PointCount() && curIndexP != -1 )
258 {
259 if( tunedP.IsArcSegment( curIndexP ) )
260 {
261 ssize_t arcIndex = tunedP.ArcIndex( curIndexP );
262
263 m_result.AddArcAndPt( tunedP.Arc( arcIndex ), tunedN.CPoint( curIndexN ) );
264 }
265 else
266 {
267 m_result.AddCorner( tunedP.CPoint( curIndexP ), tunedN.CPoint( curIndexN ) );
268 }
269
270 curIndexP = tunedP.NextShape( curIndexP );
271 }
272
273 while( curIndexN < tunedN.PointCount() && curIndexN != -1 )
274 {
275 if( tunedN.IsArcSegment( curIndexN ) )
276 {
277 ssize_t arcIndex = tunedN.ArcIndex( curIndexN );
278
279 m_result.AddPtAndArc( tunedP.CPoint( -1 ), tunedN.Arc( arcIndex ) );
280 }
281 else
282 {
283 m_result.AddCorner( tunedP.CPoint( -1 ), tunedN.CPoint( curIndexN ) );
284 }
285
286 curIndexN = tunedN.NextShape( curIndexN );
287 }
288
289 long long int dpLen = origPathLength();
290
292
294 {
296 m_lastLength = dpLen;
297 }
298 else
299 {
300 m_lastLength = dpLen - std::max( tunedP.Length(), tunedN.Length() );
302 }
303
304 if( m_lastStatus != TOO_LONG )
305 {
306 tunedP.Clear();
307 tunedN.Clear();
308
309 for( MEANDER_SHAPE* m : m_result.Meanders() )
310 {
311 if( m->Type() != MT_EMPTY )
312 {
313 tunedP.Append( m->CLine( 0 ) );
314 tunedN.Append( m->CLine( 1 ) );
315 }
316 }
317
318 m_lastLength += std::max( tunedP.Length(), tunedN.Length() );
319
322
323 if( comp > 0 )
325 else if( comp < 0 )
327 else
329 }
330
332 m_finalShapeP.Append( preP );
333 m_finalShapeP.Append( tunedP );
334 m_finalShapeP.Append( postP );
336
338 m_finalShapeN.Append( preN );
339 m_finalShapeN.Append( tunedN );
340 m_finalShapeN.Append( postN );
342
343 return true;
344}
345
346
347bool DP_MEANDER_PLACER::FixRoute( const VECTOR2I& aP, ITEM* aEndItem, bool aForceFinish )
348{
351
352 m_currentNode->Add( lP );
353 m_currentNode->Add( lN );
354
356
357 return true;
358}
359
360
362{
364 return true;
365}
366
367
369{
370 return m_originPair.CP().SegmentCount() > 0 || m_originPair.CN().SegmentCount() > 0;
371}
372
373
375{
376 if( m_currentNode )
378
379 m_currentNode = nullptr;
380 return true;
381}
382
383
385{
386 LINE l1( m_originPair.PLine(), aShape->CLine( 0 ) );
387 LINE l2( m_originPair.NLine(), aShape->CLine( 1 ) );
388
389 if( m_currentNode->CheckColliding( &l1 ) )
390 return false;
391
392 if( m_currentNode->CheckColliding( &l2 ) )
393 return false;
394
395 int w = aShape->Width();
396 int clearance = w + m_settings.m_spacing;
397
398 return m_result.CheckSelfIntersections( aShape, clearance );
399}
400
401
403{
406
407 ITEM_SET traces;
408
409 traces.Add( &m_currentTraceP );
410 traces.Add( &m_currentTraceN );
411
412 return traces;
413}
414
415
417{
418 return m_currentStart;
419}
420
421
423{
424 return m_currentEnd;
425}
426
427
429{
430 return m_initialSegment->Layers().Start();
431}
432
433
434const wxString DP_MEANDER_PLACER::TuningInfo( EDA_UNITS aUnits ) const
435{
436 wxString status;
437
438 switch( m_lastStatus )
439 {
440 case TOO_LONG:
441 status = _( "Too long: " );
442 break;
443 case TOO_SHORT:
444 status = _("Too short: " );
445 break;
446 case TUNED:
447 status = _( "Tuned: " );
448 break;
449 default:
450 return _( "?" );
451 }
452
454 status += wxT( "/" );
456 status += wxT( " (gap: " );
458 status += wxT( ")" );
459
460 return status;
461}
462
463
465{
466 return m_lastStatus;
467}
468
469const std::vector<int> DP_MEANDER_PLACER::CurrentNets() const
470{
471 std::vector<int> rv;
472 rv.push_back( m_originPair.NetP() );
473 rv.push_back( m_originPair.NetN() );
474 return rv;
475}
476
477}
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:139
Represent a set of meanders fitted over a single or two lines.
Definition: pns_meander.h:386
void SetBaselineOffset(int aOffset)
Set the parallel offset between the base segment and the meandered line.
Definition: pns_meander.h:481
void SetWidth(int aWidth)
Set the line width.
Definition: pns_meander.h:466
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:489
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.
int m_padToDieLength
Width of the meandered trace(s).
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
Total length added by pad to die size.
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:92
int m_lengthTolerance
Target skew value for diff pair de-skewing.
Definition: pns_meander.h:101
int m_spacing
Amplitude/spacing adjustment step.
Definition: pns_meander.h:83
The geometry of a single meander.
Definition: pns_meander.h:111
int Width() const
Definition: pns_meander.h:282
const SHAPE_LINE_CHAIN & CLine(int aShape) const
Definition: pns_meander.h:232
Keep the router "world" - i.e.
Definition: pns_node.h:155
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:468
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Add an item to the current node.
Definition: pns_node.cpp:656
void KillChildren()
Definition: pns_node.cpp:1451
void Remove(ARC *aArc)
Remove an item from this branch.
Definition: pns_node.cpp:873
void SetFailureReason(const wxString &aReason)
Definition: pns_router.h:205
void CommitRouting()
Definition: pns_router.cpp:840
NODE * GetWorld() const
Definition: pns_router.h:156
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:323
Push and Shove diff pair dimensions (gap) settings dialog.
@ MT_EMPTY
Definition: pns_meander.h:46
#define PNS_DBG(dbg, method,...)