KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_multi_dragger.cpp
Go to the documentation of this file.
1/*
2 * KiRouter - a push-and-(sometimes-)shove PCB router
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 * Author: Tomasz Wlostowski <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "pns_multi_dragger.h"
22#include "pns_router.h"
23#include "pns_debug_decorator.h"
24#include "pns_walkaround.h"
25#include "pns_shove.h"
26
27namespace PNS
28{
29
31{
32 m_world = nullptr;
33 m_lastNode = nullptr;
34}
35
36
40
41// here we initialize everything that's needed for multidrag. this means:
42bool MULTI_DRAGGER::Start( const VECTOR2I& aP, ITEM_SET& aPrimitives )
43{
44 m_lastNode = nullptr;
45 m_dragStatus = false;
47
48 // check if the initial ("leader") primitive set is empty...
49 if( aPrimitives.Empty() )
50 return false;
51
52 m_mdragLines.clear();
53
54 // find all LINEs to be dragged. Indicate the LINE that contains the point (aP)
55 // as the "primary line", the multidrag algo will place all other lines in such way
56 // that the cursor position lies on the primary line.
57 for( ITEM* pitem : aPrimitives.Items() )
58 {
59 LINKED_ITEM* litem = static_cast<LINKED_ITEM*>( pitem );
60 bool redundant = false;
61 for( auto& l : m_mdragLines )
62 {
63 if( l.originalLine.ContainsLink( litem ) )
64 {
65 l.originalLeaders.push_back( litem );
66 redundant = true;
67 break;
68 }
69 }
70
71 // we can possibly have multiple SEGMENTs in aPrimitives that belong to the same line.
72 // We reject these.
73 if( !redundant )
74 {
75 MDRAG_LINE l;
76 l.originalLine = m_world->AssembleLine( litem );
77 l.originalLeaders.push_back( litem );
78 l.isDraggable = true;
79 m_mdragLines.push_back( std::move( l ) );
80 }
81 }
82
83 int n = 0;
84
85 bool anyStrictCornersFound = false;
86 bool anyStrictMidSegsFound = false;
87
88 for( auto& l : m_mdragLines )
89 {
90 const int thr = l.originalLine.Width() / 2;
91
92 const VECTOR2I& origFirst = l.originalLine.CLine().CPoint( 0 );
93 const int distFirst = ( origFirst - aP ).EuclideanNorm();
94
95 const VECTOR2I& origLast = l.originalLine.CLine().CLastPoint();
96 const int distLast = ( origLast - aP ).EuclideanNorm();
97
98 l.cornerDistance = std::min( distFirst, distLast );
99
100 if( aPrimitives.FindVertex( origLast ) )
101 {
102 l.cornerIsLast = true;
103 l.leaderSegIndex = l.originalLine.SegmentCount() - 1;
104 l.cornerDistance = distLast;
105 l.isCorner = true;
106
107 if( distLast <= thr )
108 {
109 l.isStrict = true;
110 l.cornerDistance = 0;
111 }
112 }
113
114 if( aPrimitives.FindVertex( origFirst ) )
115 {
116 l.cornerIsLast = false;
117 l.leaderSegIndex = 0;
118 l.cornerDistance = distFirst;
119 l.isCorner = true;
120
121 if( distFirst <= thr )
122 {
123 l.isStrict = true;
124 l.cornerDistance = 0;
125 }
126
127 }
128
129
130 const auto& links = l.originalLine.Links();
131
132 for( int lidx = 0; lidx < (int) links.size(); lidx++ )
133 {
134 if( auto lseg = dyn_cast<SEGMENT*>( links[lidx] ) )
135 {
136
137 if( !aPrimitives.Contains( lseg ) )
138 continue;
139
140 int d = lseg->Seg().Distance( aP );
141
142 l.midSeg = lseg->Seg();
143 l.isMidSeg = true;
144 l.leaderSegIndex = lidx;
145 l.leaderSegDistance = d + thr;
146
147 if( d < thr && !l.isStrict )
148 {
149 l.isCorner = false;
150 l.isStrict = true;
151 l.leaderSegDistance = 0;
152 }
153 }
154 }
155
156 if( l.isStrict )
157 {
158 anyStrictCornersFound |= l.isCorner;
159 anyStrictMidSegsFound |= !l.isCorner;
160 }
161 }
162
163 if( anyStrictCornersFound )
165 else if (anyStrictMidSegsFound )
167 else
168 {
169 int minLeadSegDist = std::numeric_limits<int>::max();
170 int minCornerDist = std::numeric_limits<int>::max();
171 MDRAG_LINE *bestSeg = nullptr;
172 MDRAG_LINE *bestCorner = nullptr;
173
174 for( auto& l : m_mdragLines )
175 {
176 if( l.cornerDistance < minCornerDist )
177 {
178 minCornerDist = l.cornerDistance;
179 bestCorner = &l;
180 }
181 if( l.leaderSegDistance < minLeadSegDist )
182 {
183 minLeadSegDist = l.leaderSegDistance;
184 bestSeg = &l;
185 }
186 }
187
188 if( bestCorner && bestSeg )
189 {
190 if( minCornerDist < minLeadSegDist )
191 {
193 bestCorner->isPrimaryLine = true;
194 }
195 else
196 {
198 bestSeg->isPrimaryLine = true;
199 }
200 }
201 else if ( bestCorner )
202 {
204 bestCorner->isPrimaryLine = true;
205 }
206 else if ( bestSeg )
207 {
209 bestSeg->isPrimaryLine = true;
210 }
211 else return false; // can it really happen?
212 }
213
214 if( m_dragMode == DM_CORNER )
215 {
216 for( auto& l : m_mdragLines )
217 {
218 // make sure the corner to drag is the last one
219 if ( !l.cornerIsLast )
220 {
221 l.originalLine.Reverse();
222 l.cornerIsLast = true;
223 }
224 // and if it's connected (non-trivial fanout), disregard it
225
226 const JOINT* jt = m_world->FindJoint( l.originalLine.CLastPoint(), &l.originalLine );
227
228 assert (jt != nullptr);
229
230 if( !jt->IsTrivialEndpoint() )
231 {
232 m_dragMode = DM_SEGMENT; // fallback to segment mode if non-trivial endpoints found
233 }
234 }
235 }
236
237 for( auto& l : m_mdragLines )
238 {
239 if( (anyStrictCornersFound || anyStrictMidSegsFound) && l.isStrict )
240 {
241 l.isPrimaryLine = true;
242 break;
243 }
244 }
245
246 m_origDraggedItems = aPrimitives;
247
248 if( Settings().Mode() == RM_Shove )
249 {
250 m_preShoveNode = m_world->Branch();
251
252 for( auto& l : m_mdragLines )
253 {
254 m_preShoveNode->Remove( l.originalLine );
255 }
256
257 m_shove.reset( new SHOVE( m_preShoveNode, Router() ) );
258 m_shove->SetLogger( Logger() );
259 m_shove->SetDebugDecorator( Dbg() );
260 m_shove->SetDefaultShovePolicy( SHOVE::SHP_SHOVE | SHOVE::SHP_DONT_LOCK_ENDPOINTS );
261 }
262
263 return true;
264}
265
266
270
271
273{
274 return DM_CORNER;
275}
276
277bool clipToOtherLine( NODE* aNode, const LINE& aRef, LINE& aClipped )
278{
279 std::set<OBSTACLE> obstacles;
280 COLLISION_SEARCH_CONTEXT ctx( obstacles );
281
282 constexpr int clipLengthThreshold = 100;
283
284 //DEBUG_DECORATOR* dbg = ROUTER::GetInstance()->GetInterface()->GetDebugDecorator();
285
286 LINE l( aClipped );
287 SHAPE_LINE_CHAIN tightest;
288
289 bool didClip = false;
290 int curL = l.CLine().Length();
291 int step = curL / 2 - 1;
292
293 while( step > clipLengthThreshold )
294 {
295 SHAPE_LINE_CHAIN sl_tmp( aClipped.CLine() );
296 VECTOR2I pclip = sl_tmp.PointAlong( curL );
297 int idx = sl_tmp.Split( pclip );
298 sl_tmp = sl_tmp.Slice(0, idx);
299
300 l.SetShape( sl_tmp );
301
302 //PNS_DBG( dbg, 3int, pclip, WHITE, 500000, wxT(""));
303
304 if( l.Collide( &aRef, aNode, l.Layer(), &ctx ) )
305 {
306 didClip = true;
307 curL -= step;
308 step /= 2;
309 }
310 else
311 {
312 tightest = std::move( sl_tmp );
313
314 if( didClip )
315 {
316 curL += step;
317 step /= 2;
318 }
319 else
320 {
321 break;
322 }
323 }
324 }
325
326 aClipped.SetShape( tightest );
327
328 return didClip;
329}
330
331
332
333
334const std::vector<NET_HANDLE> MULTI_DRAGGER::CurrentNets() const
335{
336 std::set<NET_HANDLE> uniqueNets;
337 for( auto &l : m_mdragLines )
338 {
339 NET_HANDLE net = l.draggedLine.Net();
340 if( net )
341 uniqueNets.insert( net );
342 }
343
344 return std::vector<NET_HANDLE>( uniqueNets.begin(), uniqueNets.end() );
345}
346
347// this is what ultimately gets called when the user clicks/releases the mouse button
348// during drag.
349bool MULTI_DRAGGER::FixRoute( bool aForceCommit )
350{
351 NODE* node = CurrentNode();
352
353 if( node )
354 {
355 // last drag status is OK?
356 if( !m_dragStatus && !Settings().AllowDRCViolations() )
357 return false;
358
359 // commit the current world state
360 Router()->CommitRouting( node );
361 return true;
362 }
363
364 return false;
365}
366
367bool MULTI_DRAGGER::tryWalkaround( NODE* aNode, LINE& aOrig, LINE& aWalk )
368{
369 WALKAROUND walkaround( aNode, Router() );
370 bool ok = false;
371 walkaround.SetSolidsOnly( false );
372 walkaround.SetDebugDecorator( Dbg() );
373 walkaround.SetLogger( Logger() );
374 walkaround.SetIterationLimit( Settings().WalkaroundIterationLimit() );
375 walkaround.SetLengthLimit( true, 3.0 );
377
378 aWalk = aOrig;
379
380 WALKAROUND::RESULT wr = walkaround.Route( aWalk );
381
383 {
384 aWalk = wr.lines[ WALKAROUND::WP_SHORTEST ];
385 return true;
386 }
387
388 return false;
389}
390
392{
393 const SEG origLeader = aLine.preDragLine.CSegment( aLine.leaderSegIndex );
394 const DIRECTION_45 origLeaderDir( origLeader );
395
396 for ( int i = 0; i < aLine.draggedLine.SegmentCount(); i++ )
397 {
398 const SEG& curSeg = aLine.draggedLine.CSegment(i);
399 const DIRECTION_45 curDir( curSeg );
400
401 auto ip = curSeg.IntersectLines( m_guide );
402 PNS_DBG(Dbg(), Message, wxString::Format("s %d ip=%d c=%s o=%s", i, ip?1:0, curDir.Format(), origLeaderDir.Format() ));
403 if( ip && curSeg.Contains( *ip ) )
404 {
405 if( curDir == origLeaderDir || curDir == origLeaderDir.Opposite() )
406 return i;
407 }
408 }
409
410 return -1;
411}
412
413void MULTI_DRAGGER::restoreLeaderSegments( std::vector<MDRAG_LINE>& aCompletedLines )
414{
415 m_leaderSegments.clear();
416
417 for( auto& l : aCompletedLines )
418 {
419 if( l.dragOK )
420 {
421 if( m_dragMode == DM_CORNER )
422 {
423 if( l.draggedLine.LinkCount() > 0 )
424 {
425 m_leaderSegments.push_back(
426 static_cast<PNS::ITEM*>( l.draggedLine.GetLink( -1 ) ) );
427 }
428 }
429 else
430 {
431 int newLeaderIdx = findNewLeaderSegment( l );
432 if( newLeaderIdx >= 0 && newLeaderIdx < l.draggedLine.LinkCount() )
433 {
434 m_leaderSegments.push_back(
435 static_cast<PNS::ITEM*>( l.draggedLine.GetLink( newLeaderIdx ) ) );
436 }
437 }
438 }
439 }
440}
441
442bool MULTI_DRAGGER::multidragWalkaround( std::vector<MDRAG_LINE>& aCompletedLines )
443{
444 // fixme: rewrite using shared_ptr...
445 if( m_lastNode )
446 {
447 delete m_lastNode;
448 m_lastNode = nullptr;
449 }
450
451 auto compareDragStartDist = []( const MDRAG_LINE& a, const MDRAG_LINE& b ) -> int
452 {
453 return a.dragDist < b.dragDist;
454 };
455
456 std::sort( aCompletedLines.begin(), aCompletedLines.end(), compareDragStartDist );
457
458
459 NODE* preWalkNode = m_world->Branch();
460
461 for( auto& l : aCompletedLines )
462 {
463 PNS_DBG( Dbg(), AddItem, &l.originalLine, BLUE, 100000, wxString::Format("prewalk-remove lc=%d", l.originalLine.LinkCount() ) );
464 preWalkNode->Remove( l.originalLine );
465 }
466
467 bool fail = false;
468
469 NODE* tmpNodes[2];
470 int totalLength[2];
471
472 for( int attempt = 0; attempt < 2; attempt++ )
473 {
474 NODE *node = tmpNodes[attempt] = preWalkNode->Branch();
475 totalLength[attempt] = 0;
476 fail = false;
477
478 for( int lidx = 0; lidx < aCompletedLines.size(); lidx++ )
479 {
480 MDRAG_LINE& l = aCompletedLines[attempt ? aCompletedLines.size() - 1 - lidx : lidx];
481
482 LINE walk( l.draggedLine );
483 auto result = tryWalkaround( node, l.draggedLine, walk );
484
485 PNS_DBG( Dbg(), AddItem, &l.draggedLine, YELLOW, 100000, wxString::Format("dragged lidx=%d attempt=%d dd=%d isPrimary=%d", lidx, attempt, l.dragDist, l.isPrimaryLine?1:0) );
486 PNS_DBG( Dbg(), AddItem, &walk, BLUE, 100000, wxString::Format("walk lidx=%d attempt=%d", lidx, attempt) );
487
488
489 if( result )
490 {
491 node->Add( walk );
492 totalLength[attempt] += walk.CLine().Length() - l.draggedLine.CLine().Length();
493 l.draggedLine = std::move( walk );
494 }
495 else
496 {
497 delete node;
498 tmpNodes[attempt] = nullptr;
499 fail = true;
500 break;
501 }
502 }
503 }
504
505 if( fail )
506 return false;
507
508
509 bool rv = false;
510
511 if( tmpNodes[0] && tmpNodes[1] )
512 {
513 if ( totalLength[0] < totalLength[1] )
514 {
515 delete tmpNodes[1];
516 m_lastNode = tmpNodes[0];
517 rv = true;
518 }
519 else
520 {
521 delete tmpNodes[0];
522 m_lastNode = tmpNodes[1];
523 rv = true;
524 }
525 }
526 else if ( tmpNodes[0] )
527 {
528 m_lastNode = tmpNodes[0];
529 rv = true;
530 }
531 else if ( tmpNodes[1] )
532 {
533 m_lastNode = tmpNodes[1];
534 rv = true;
535 }
536
537 restoreLeaderSegments( aCompletedLines );
538
539 return rv;
540}
541
542
543bool MULTI_DRAGGER::multidragMarkObstacles( std::vector<MDRAG_LINE>& aCompletedLines )
544{
545
546// fixme: rewrite using shared_ptr...
547 if( m_lastNode )
548 {
549 delete m_lastNode;
550 m_lastNode = nullptr;
551 }
552
553 // m_lastNode contains the temporary (post-modification) state. Think of it as
554 // of an efficient undo buffer. We don't change the PCB directly, but a branch of it
555 // created below. We can then commit its state (applying the modifications to the host board
556 // by calling ROUTING::CommitRouting(m_lastNode) or simply discard it.
557 m_lastNode = m_world->Branch();
558
559
560 int nclipped = 0;
561 for( int l1 = 0; l1 < aCompletedLines.size(); l1++ )
562 {
563 for( int l2 = l1 + 1; l2 < aCompletedLines.size(); l2++ )
564 {
565 const auto& l1l = aCompletedLines[l1].draggedLine;
566 auto l2l = aCompletedLines[l2].draggedLine;
567
568 if( clipToOtherLine( m_lastNode, l1l, l2l ) )
569 {
570 aCompletedLines[l2].draggedLine = l2l;
571 nclipped++;
572 }
573 }
574 }
575
576 for ( auto&l : aCompletedLines )
577 {
578 m_lastNode->Remove( l.originalLine );
579 m_lastNode->Add( l.draggedLine );
580 }
581
582 restoreLeaderSegments( aCompletedLines );
583
584 return true;
585}
586
587bool MULTI_DRAGGER::multidragShove( std::vector<MDRAG_LINE>& aCompletedLines )
588{
589 if( m_lastNode )
590 {
591 delete m_lastNode;
592 m_lastNode = nullptr;
593 }
594
595 if( !m_shove )
596 return false;
597
598 auto compareDragStartDist = []( const MDRAG_LINE& a, const MDRAG_LINE& b ) -> int
599 {
600 return a.dragDist < b.dragDist;
601 };
602
603 std::sort( aCompletedLines.begin(), aCompletedLines.end(), compareDragStartDist );
604
605 auto iface = Router()->GetInterface();
606
607 for( auto& l : m_mdragLines )
608 {
609 PNS_DBG( Dbg(), Message, wxString::Format ( wxT("net %-30s: isCorner %d isStrict %d c-Dist %-10d l-dist %-10d leadIndex %-2d CisLast %d dragDist %-10d"),
610 iface->GetNetName( l.draggedLine.Net() ),
611 (int) l.isCorner?1:0,
612 (int) l.isStrict?1:0,
613 (int) l.cornerDistance,
614 (int) l.leaderSegDistance,
615 (int) l.leaderSegIndex,
616 (int) l.cornerIsLast?1:0,
617 (int) l.dragDist ) );
618 }
619
620
621 m_shove->SetDefaultShovePolicy( SHOVE::SHP_SHOVE );
622 m_shove->ClearHeads();
623
624 for( auto& l : aCompletedLines )
625 {
626 PNS_DBG( Dbg(), AddItem, &l.draggedLine, GREEN, 0, "dragged-line" );
627 m_shove->AddHeads( l.draggedLine, SHOVE::SHP_SHOVE | SHOVE::SHP_DONT_OPTIMIZE );
628 }
629
630 auto status = m_shove->Run();
631
632 m_lastNode = m_shove->CurrentNode()->Branch();
633
634 if( status == SHOVE::SH_OK )
635 {
636 for( int i = 0; i < aCompletedLines.size(); i++ )
637 {
638 MDRAG_LINE&l = aCompletedLines[i];
639 if( m_shove->HeadsModified( i ) )
640 l.draggedLine = m_shove->GetModifiedHead( i );
641
642 // this should not be linked (assert in rt-test)
644
645 m_lastNode->Add( l.draggedLine );
646 }
647 }
648 else
649 {
650 return false;
651 }
652
653 restoreLeaderSegments( aCompletedLines );
654
655 return true;
656}
657
658// this is called every time the user moves the mouse while dragging a set of multiple tracks
660{
661 std::optional<LINE> primaryPreDrag, primaryDragged;
662
663
664
665 SEG lastPreDrag;
666 DIRECTION_45 primaryDir;
667 VECTOR2I perp;
668
669 DIRECTION_45 primaryLastSegDir;
670 std::vector<MDRAG_LINE> completed;
671
672 auto tryPosture = [&] ( int aVariant ) -> bool
673 {
674 MDRAG_LINE* primaryLine = nullptr;
675
676 for( auto &l : m_mdragLines )
677 {
678 l.dragOK = false;
679 l.preDragLine = l.originalLine;
680 //PNS_DBG( Dbg(), AddItem, &l.originalLine, GREEN, 300000, "par" );
681 if( l.isPrimaryLine )
682 {
683
684 //PNS_DBG( Dbg(), AddItem, &l.originalLine, BLUE, 300000, wxT("mdrag-prim"));
685
686 // create a copy of the primary line (pre-drag and post-drag).
687 // the pre-drag version is necessary for NODE::Remove() to be able to
688 // find out the segments before modification by the multidrag algorithm
689 primaryDragged = l.originalLine;
690 primaryDragged->ClearLinks();
691 primaryPreDrag = l.originalLine;
692 primaryLine = &l;
693
694 }
695 }
696
697 if( aVariant == 1 && (primaryPreDrag->PointCount() > 2) )
698 {
699 primaryPreDrag->Line().Remove( -1 );
700 primaryDragged->Line().Remove( -1 );
701
702 for( auto&l : m_mdragLines )
703 {
704 l.preDragLine.Line().Remove(-1);
705 }
706 }
707
708 completed.clear();
709
710 int snapThreshold = Settings().SmoothDraggedSegments() ? primaryDragged->Width() / 4 : 0;
711
712 if( m_dragMode == DM_CORNER )
713 {
714 // first, drag only the primary line
715 // PNS_DBG( Dbg(), AddPoint, primaryDragged->CLastPoint(), YELLOW, 600000, wxT("mdrag-sec"));
716
717 lastPreDrag = primaryPreDrag->CSegment( -1 );
718 primaryDir = DIRECTION_45( lastPreDrag );
719
720 primaryDragged->SetSnapThreshhold( snapThreshold );
721 primaryDragged->DragCorner( aP, primaryDragged->PointCount() - 1, false );
722
723 SEG lastPrimDrag = primaryDragged->CSegment( -1 );
724
725 if ( aVariant == 2 )
726 lastPrimDrag = lastPreDrag;
727
728 if( primaryDragged->SegmentCount() > 0 )
729 {
730 auto lastSeg = primaryDragged->CSegment( -1 );
731 if( DIRECTION_45( lastSeg ) != primaryDir )
732 {
733 if( lastSeg.Length() < primaryDragged->Width() )
734 {
735 lastPrimDrag = lastPreDrag;
736 }
737 }
738 }
739
740 perp = (lastPrimDrag.B - lastPrimDrag.A).Perpendicular();
741
742 primaryLastSegDir = DIRECTION_45( lastPrimDrag );
743
744// PNS_DBG( Dbg(), AddShape, &ll, LIGHTBLUE, 200000, "par" );
745 PNS_DBG( Dbg(), AddItem, &(*primaryDragged), LIGHTGRAY, 100000, "prim" );
746 }
747 else
748 {
749
750 SHAPE_LINE_CHAIN ll2( { lastPreDrag.A, lastPreDrag.B } );
751 PNS_DBG( Dbg(), AddShape, &ll2, LIGHTYELLOW, 300000, "par" );
752 lastPreDrag = primaryDragged->CSegment( primaryLine->leaderSegIndex );
753 primaryDragged->SetSnapThreshhold( snapThreshold );
754 primaryDragged->DragSegment( aP, primaryLine->leaderSegIndex );
755 perp = (primaryLine->midSeg.B - primaryLine->midSeg.A).Perpendicular();
756 m_guide = SEG( aP, aP + perp );
757 }
758
759
761 m_draggedItems.Clear();
762
763 // now drag all other lines
764 for( auto& l : m_mdragLines )
765 {
766 //PNS_DBG( Dbg(), AddPoint, l.originalLine.CPoint( l.cornerIndex ), WHITE, 1000000, wxT("l-end"));
767 if( l.isDraggable )
768 {
769 l.dragOK = false;
770 //PNS_DBG( Dbg(), AddItem, &l.originalLine, GREEN, 100000, wxT("mdrag-sec"));
771
772 // reject nulls
773 if( l.preDragLine.SegmentCount() >= 1 )
774 {
775
776 //PNS_DBG( Dbg(), AddPoint, l.preDragLine.CPoint( l.cornerIndex ), YELLOW, 600000, wxT("mdrag-sec"));
777
778 // check the direction of the last segment of the line against the direction of
779 // the last segment of the primary line (both before dragging) and perform drag
780 // only when the directions are the same. The algorithm here is quite trival and
781 // otherwise would produce really awkward results. There's of course a TON of
782 // room for improvement here :-)
783
784 if( m_dragMode == DM_CORNER )
785 {
786 DIRECTION_45 parallelDir( l.preDragLine.CSegment( -1 ) );
787
788 auto leadAngle = primaryDir.Angle( parallelDir );
789
790 if( leadAngle == DIRECTION_45::ANG_OBTUSE
791 || leadAngle == DIRECTION_45::ANG_STRAIGHT )
792 {
793 // compute the distance between the primary line and the last point of
794 // the currently processed line
795 int dist = lastPreDrag.LineDistance( l.preDragLine.CLastPoint(), true );
796
797 // now project it on the perpendicular line we computed before
798 auto projected = aP + perp.Resize( dist );
799
800
801 LINE parallelDragged( l.preDragLine );
802
803
804 parallelDragged.ClearLinks();
805 //m_lastNode->Remove( parallelDragged );
806 // drag the non-primary line's end trying to place it at the projected point
807 parallelDragged.DragCorner( projected, parallelDragged.PointCount() - 1,
808 false, primaryLastSegDir );
809
810 //PNS_DBG( Dbg(), AddPoint, projected, LIGHTYELLOW, 600000,
811 // wxT( "l-end" ) );
812
813 l.dragOK = true;
814
815 if( !l.isPrimaryLine )
816 {
817 l.draggedLine = parallelDragged;
818 completed.push_back( l );
819 m_draggedItems.Add( parallelDragged );
820 }
821 }
822 }
823 else if ( m_dragMode == DM_SEGMENT )
824 {
825 SEG sdrag = l.midSeg;
826 DIRECTION_45 refDir( lastPreDrag );
827 DIRECTION_45 curDir( sdrag );
828 auto ang = refDir.Angle( curDir );
829
831 {
832 int dist = lastPreDrag.LineDistance(
833 l.preDragLine.CPoint( l.leaderSegIndex ), true );
834 auto projected = aP + perp.Resize( dist );
835
836 SEG sperp( aP, aP + perp.Resize( 10000000 ) );
837 VECTOR2I startProj = sperp.LineProject( m_dragStartPoint );
838
839 SHAPE_LINE_CHAIN ll( { sperp.A, sperp.B } );
840
841
842 PNS_DBG( Dbg(), AddShape, &ll, LIGHTBLUE, 100000, "par" );
843 SHAPE_LINE_CHAIN ll2( { sdrag.A, sdrag.B } );
844 PNS_DBG( Dbg(), AddShape, &ll2, LIGHTBLUE, 100000, "sdrag" );
845 VECTOR2I v = projected - startProj;
846 l.dragDist = v.EuclideanNorm() * sign( v.Dot( perp ) );
847 l.dragOK = true;
848
849 if( !l.isPrimaryLine )
850 {
851 l.draggedLine = l.preDragLine;
852 l.draggedLine.ClearLinks();
853 l.draggedLine.SetSnapThreshhold( snapThreshold );
854 l.draggedLine.DragSegment( projected, l.leaderSegIndex, false );
855 completed.push_back( l );
856 PNS_DBG( Dbg(), AddItem, &l.draggedLine, LIGHTBLUE, 100000,
857 "dragged" );
858 }
859
860
861 PNS_DBG( Dbg(), AddPoint, startProj, LIGHTBLUE, 400000,
862 wxT( "startProj" ) );
863 PNS_DBG( Dbg(), AddPoint, projected, LIGHTRED, 400000,
864 wxString::Format( "pro dd=%d", l.dragDist ) );
865 }
866 }
867 }
868 }
869
870 if (l.isPrimaryLine)
871 {
872 l.draggedLine = *primaryDragged;
873 l.dragOK = true;
874 completed.push_back( l );
875 }
876 }
877
878 if( m_dragMode == DM_SEGMENT )
879 return true;
880 else
881 {
882 for ( const auto &l: completed )
883 {
884 if( !l.dragOK && aVariant < 2 )
885 return false;
886
887 if( l.isPrimaryLine )
888 continue;
889
890 DIRECTION_45 lastDir ( l.draggedLine.CSegment(-1) );
891
892 if( lastDir != primaryLastSegDir )
893 return false;
894 }
895 }
896
897 return true;
898 };
899
900 bool res = false;
901
902 for( int variant = 0; variant < 3; variant++ )
903 {
904 res = tryPosture( 0 );
905 if( res )
906 break;
907 }
908
909 switch( Settings().Mode() )
910 {
911 case RM_Walkaround:
912 m_dragStatus = multidragWalkaround ( completed );
913 break;
914
915 case RM_Shove:
916 m_dragStatus = multidragShove ( completed );
917 break;
918
919 case RM_MarkObstacles:
921 break;
922
923
924
925 default:
926 break;
927 }
928
929 return m_dragStatus;
930}
931
932
937
938
940{
941 return m_draggedItems;
942}
943
944
946{
947 // fixme: should we care?
948 return 0;
949}
950
951
952} // namespace PNS
Represent route directions & corner angles in a 45-degree metric.
Definition direction45.h:37
AngleType Angle(const DIRECTION_45 &aOther) const
Return the type of angle between directions (this) and aOther.
const std::string Format() const
Format the direction in a human readable word.
DIRECTION_45 Opposite() const
Return a direction opposite (180 degree) to (this).
void SetDebugDecorator(DEBUG_DECORATOR *aDecorator)
Assign a debug decorator allowing this algo to draw extra graphics for visual debugging.
void SetLogger(LOGGER *aLogger)
virtual LOGGER * Logger()
ROUTER * Router() const
Return current router settings.
ROUTING_SETTINGS & Settings() const
Return the logger object, allowing to dump geometry to a file.
DEBUG_DECORATOR * Dbg() const
DRAG_ALGO(ROUTER *aRouter)
bool Empty() const
Definition pns_itemset.h:82
bool Contains(ITEM *aItem) const
std::vector< ITEM * > & Items()
Definition pns_itemset.h:87
ITEM * FindVertex(const VECTOR2I &aV) const
Base class for PNS router board items.
Definition pns_item.h:98
virtual int Layer() const
Definition pns_item.h:216
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:305
A 2D point on a given set of layers and belonging to a certain net, that links together a number of b...
Definition pns_joint.h:43
bool IsTrivialEndpoint() const
Definition pns_joint.h:176
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition pns_line.h:62
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Return the shape of the line.
Definition pns_line.h:131
const SHAPE_LINE_CHAIN & CLine() const
Definition pns_line.h:142
void DragCorner(const VECTOR2I &aP, int aIndex, bool aFreeAngle=false, DIRECTION_45 aPreferredEndingDirection=DIRECTION_45())
Definition pns_line.cpp:831
int SegmentCount() const
Definition pns_line.h:144
int PointCount() const
Definition pns_line.h:145
const SEG CSegment(int aIdx) const
Set line width.
Definition pns_line.h:152
bool multidragShove(std::vector< MDRAG_LINE > &aCompletedLines)
bool multidragMarkObstacles(std::vector< MDRAG_LINE > &aCompletedLines)
std::vector< PNS::ITEM * > m_leaderSegments
virtual bool Start(const VECTOR2I &aP, ITEM_SET &aPrimitives) override
Function Start()
bool FixRoute(bool aForceCommit) override
Function FixRoute()
bool Drag(const VECTOR2I &aP) override
Function Drag()
int CurrentLayer() const override
Function CurrentLayer()
NODE * CurrentNode() const override
Function CurrentNode()
std::vector< MDRAG_LINE > m_mdragLines
bool tryWalkaround(NODE *aNode, LINE &aOrig, LINE &aWalk)
void SetMode(PNS::DRAG_MODE aDragMode) override
int findNewLeaderSegment(const MDRAG_LINE &aLine) const
void restoreLeaderSegments(std::vector< MDRAG_LINE > &aCompletedLines)
bool multidragWalkaround(std::vector< MDRAG_LINE > &aCompletedLines)
const ITEM_SET Traces() override
Function Traces()
const std::vector< NET_HANDLE > CurrentNets() const override
Function CurrentNets()
MULTI_DRAGGER(ROUTER *aRouter)
PNS::DRAG_MODE Mode() const override
std::unique_ptr< SHOVE > m_shove
Keep the router "world" - i.e.
Definition pns_node.h:232
NODE * Branch()
Create a lightweight copy (called branch) of self that tracks the changes (added/removed items) wrs t...
Definition pns_node.cpp:143
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Add an item to the current node.
Definition pns_node.cpp:665
void Remove(ARC *aArc)
Remove an item from this branch.
Definition pns_node.cpp:909
ROUTER_IFACE * GetInterface() const
Definition pns_router.h:232
void CommitRouting()
bool SmoothDraggedSegments() const
Enable/disable smoothing segments during dragging.
The actual Push and Shove algorithm.
Definition pns_shove.h:46
@ SHP_DONT_OPTIMIZE
Definition pns_shove.h:65
@ SHP_DONT_LOCK_ENDPOINTS
Definition pns_shove.h:66
void SetIterationLimit(const int aIterLimit)
void SetLengthLimit(bool aEnable, double aLengthExpansionFactor)
void SetSolidsOnly(bool aSolidsOnly)
STATUS Route(const LINE &aInitialPath, LINE &aWalkPath, bool aOptimize=true)
void SetAllowedPolicies(std::vector< WALK_POLICY > aPolicies)
Definition seg.h:42
VECTOR2I A
Definition seg.h:49
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Return the closest Euclidean distance between point aP and the line defined by the ends of segment (t...
Definition seg.cpp:717
VECTOR2I B
Definition seg.h:50
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Compute the intersection point of lines passing through ends of (this) and aSeg.
Definition seg.h:220
bool Contains(const SEG &aSeg) const
Definition seg.h:324
VECTOR2I LineProject(const VECTOR2I &aP) const
Compute the perpendicular projection point of aP on a line passing through ends of the segment.
Definition seg.cpp:656
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const VECTOR2I PointAlong(int aPathLength) const
int Split(const VECTOR2I &aP, bool aExact=false)
Insert the point aP belonging to one of the our segments, splitting the adjacent segment in two.
const SHAPE_LINE_CHAIN Slice(int aStartIndex, int aEndIndex) const
Return a subset of this line chain containing the [start_index, end_index] range of points.
long long int Length() const
Return length of the line chain in Euclidean metric.
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:283
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition vector2d.h:554
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition vector2d.h:385
@ LIGHTBLUE
Definition color4d.h:62
@ BLUE
Definition color4d.h:56
@ LIGHTGRAY
Definition color4d.h:47
@ LIGHTYELLOW
Definition color4d.h:49
@ GREEN
Definition color4d.h:57
@ YELLOW
Definition color4d.h:67
@ LIGHTRED
Definition color4d.h:65
Push and Shove diff pair dimensions (gap) settings dialog.
@ RM_MarkObstacles
Ignore collisions, mark obstacles.
@ RM_Walkaround
Only walk around.
@ RM_Shove
Only shove.
void * NET_HANDLE
Definition pns_item.h:55
DRAG_MODE
Definition pns_router.h:71
@ DM_CORNER
Definition pns_router.h:72
@ DM_SEGMENT
Definition pns_router.h:73
bool clipToOtherLine(NODE *aNode, const LINE &aRef, LINE &aClipped)
#define PNS_DBG(dbg, method,...)
std::vector< PNS::ITEM * > originalLeaders
LINE lines[MaxWalkPolicies]
STATUS status[MaxWalkPolicies]
VECTOR3I res
wxString result
Test unit parsing edge cases and error handling.
Casted dyn_cast(From aObject)
A lightweight dynamic downcast.
Definition typeinfo.h:61
constexpr int sign(T val)
Definition util.h:145
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695