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
23#include <core/typeinfo.h>
24
25#include "pns_router.h"
26#include "pns_debug_decorator.h"
27#include "pns_walkaround.h"
28#include "pns_shove.h"
29
30namespace PNS
31{
32
34{
35 m_world = nullptr;
36 m_lastNode = nullptr;
37}
38
39
43
44// here we initialize everything that's needed for multidrag. this means:
45bool MULTI_DRAGGER::Start( const VECTOR2I& aP, ITEM_SET& aPrimitives )
46{
47 m_lastNode = nullptr;
48 m_dragStatus = false;
50
51 // check if the initial ("leader") primitive set is empty...
52 if( aPrimitives.Empty() )
53 return false;
54
55 m_mdragLines.clear();
56
57 // find all LINEs to be dragged. Indicate the LINE that contains the point (aP)
58 // as the "primary line", the multidrag algo will place all other lines in such way
59 // that the cursor position lies on the primary line.
60 for( ITEM* pitem : aPrimitives.Items() )
61 {
62 LINKED_ITEM* litem = static_cast<LINKED_ITEM*>( pitem );
63 bool redundant = false;
64 for( auto& l : m_mdragLines )
65 {
66 if( l.originalLine.ContainsLink( litem ) )
67 {
68 l.originalLeaders.push_back( litem );
69 redundant = true;
70 break;
71 }
72 }
73
74 // we can possibly have multiple SEGMENTs in aPrimitives that belong to the same line.
75 // We reject these.
76 if( !redundant )
77 {
78 MDRAG_LINE l;
79 l.originalLine = m_world->AssembleLine( litem );
80 l.originalLeaders.push_back( litem );
81 l.isDraggable = true;
82 l.mdragIndex = static_cast<int>( m_mdragLines.size() );
83 m_mdragLines.push_back( std::move( l ) );
84 }
85 }
86
87 bool anyStrictCornersFound = false;
88 bool anyStrictMidSegsFound = false;
89
90 for( auto& l : m_mdragLines )
91 {
92 const int thr = l.originalLine.Width() / 2;
93
94 const VECTOR2I& origFirst = l.originalLine.CLine().CPoint( 0 );
95 const int distFirst = ( origFirst - aP ).EuclideanNorm();
96
97 const VECTOR2I& origLast = l.originalLine.CLine().CLastPoint();
98 const int distLast = ( origLast - aP ).EuclideanNorm();
99
100 l.cornerDistance = std::min( distFirst, distLast );
101
102 bool takeFirst = false;
103 auto ilast = aPrimitives.FindVertex( origLast );
104 auto ifirst = aPrimitives.FindVertex( origFirst );
105
106 if( ilast && ifirst )
107 takeFirst = distFirst < distLast;
108 else if( ilast )
109 takeFirst = false;
110 else if( ifirst )
111 takeFirst = true;
112
113 if( ifirst || ilast )
114 {
115 if( takeFirst )
116 {
117 l.cornerIsLast = false;
118 l.leaderSegIndex = 0;
119 l.cornerDistance = distFirst;
120 l.isCorner = true;
121
122 if( distFirst <= thr )
123 {
124 l.isStrict = true;
125 l.cornerDistance = 0;
126 }
127 }
128 else
129 {
130 l.cornerIsLast = true;
131 l.leaderSegIndex = l.originalLine.SegmentCount() - 1;
132 l.cornerDistance = distLast;
133 l.isCorner = true;
134
135 if( distLast <= thr )
136 {
137 l.isStrict = true;
138 l.cornerDistance = 0;
139 }
140 }
141 }
142
143 const auto& links = l.originalLine.Links();
144
145 for( int lidx = 0; lidx < (int) links.size(); lidx++ )
146 {
147 if( auto lseg = dyn_cast<SEGMENT*>( links[lidx] ) )
148 {
149
150 if( !aPrimitives.Contains( lseg ) )
151 continue;
152
153 int d = lseg->Seg().Distance( aP );
154
155 l.midSeg = lseg->Seg();
156 l.isMidSeg = true;
157 l.leaderSegIndex = lidx;
158 l.leaderSegDistance = d + thr;
159
160 if( d < thr && !l.isStrict )
161 {
162 l.isCorner = false;
163 l.isStrict = true;
164 l.leaderSegDistance = 0;
165 }
166 }
167 }
168
169 if( l.isStrict )
170 {
171 anyStrictCornersFound |= l.isCorner;
172 anyStrictMidSegsFound |= !l.isCorner;
173 }
174 }
175
176 if( anyStrictCornersFound )
178 else if (anyStrictMidSegsFound )
180 else
181 {
182 int minLeadSegDist = std::numeric_limits<int>::max();
183 int minCornerDist = std::numeric_limits<int>::max();
184 MDRAG_LINE *bestSeg = nullptr;
185 MDRAG_LINE *bestCorner = nullptr;
186
187 for( auto& l : m_mdragLines )
188 {
189 if( l.cornerDistance < minCornerDist )
190 {
191 minCornerDist = l.cornerDistance;
192 bestCorner = &l;
193 }
194 if( l.leaderSegDistance < minLeadSegDist )
195 {
196 minLeadSegDist = l.leaderSegDistance;
197 bestSeg = &l;
198 }
199 }
200
201 if( bestCorner && bestSeg )
202 {
203 if( minCornerDist < minLeadSegDist )
204 {
206 bestCorner->isPrimaryLine = true;
207 }
208 else
209 {
211 bestSeg->isPrimaryLine = true;
212 }
213 }
214 else if ( bestCorner )
215 {
217 bestCorner->isPrimaryLine = true;
218 }
219 else if ( bestSeg )
220 {
222 bestSeg->isPrimaryLine = true;
223 }
224 else return false; // can it really happen?
225 }
226
227 if( m_dragMode == DM_CORNER )
228 {
229 for( auto& l : m_mdragLines )
230 {
231 // make sure the corner to drag is the last one
232 if ( !l.cornerIsLast )
233 {
234 l.originalLine.Reverse();
235 l.cornerIsLast = true;
236 }
237 // and if it's connected (non-trivial fanout), disregard it
238
239 const JOINT* jt = m_world->FindJoint( l.originalLine.CLastPoint(), &l.originalLine );
240
241 assert (jt != nullptr);
242
243 if( !jt->IsTrivialEndpoint() )
244 {
245 m_dragMode = DM_SEGMENT; // fallback to segment mode if non-trivial endpoints found
246 }
247 }
248 }
249
250 for( auto& l : m_mdragLines )
251 {
252 if( (anyStrictCornersFound || anyStrictMidSegsFound) && l.isStrict )
253 {
254 l.isPrimaryLine = true;
255 break;
256 }
257 }
258
259 m_origDraggedItems = aPrimitives;
260
261 if( Settings().Mode() == RM_Shove )
262 {
263 m_preShoveNode = m_world->Branch();
264
265 for( auto& l : m_mdragLines )
266 {
267 m_preShoveNode->Remove( l.originalLine );
268 }
269
270 m_shove.reset( new SHOVE( m_preShoveNode, Router() ) );
271 m_shove->SetLogger( Logger() );
272 m_shove->SetDebugDecorator( Dbg() );
273 m_shove->SetDefaultShovePolicy( SHOVE::SHP_SHOVE | SHOVE::SHP_DONT_LOCK_ENDPOINTS );
274 }
275
276 return true;
277}
278
279
283
284
286{
287 return DM_CORNER;
288}
289
290bool clipToOtherLine( NODE* aNode, const LINE& aRef, LINE& aClipped )
291{
292 std::set<OBSTACLE> obstacles;
293 COLLISION_SEARCH_CONTEXT ctx( obstacles );
294
295 constexpr int clipLengthThreshold = 100;
296
297 //DEBUG_DECORATOR* dbg = ROUTER::GetInstance()->GetInterface()->GetDebugDecorator();
298
299 LINE l( aClipped );
300 SHAPE_LINE_CHAIN tightest;
301
302 bool didClip = false;
303 int curL = l.CLine().Length();
304 int step = curL / 2 - 1;
305
306 while( step > clipLengthThreshold )
307 {
308 SHAPE_LINE_CHAIN sl_tmp( aClipped.CLine() );
309 VECTOR2I pclip = sl_tmp.PointAlong( curL );
310 int idx = sl_tmp.Split( pclip );
311 sl_tmp = sl_tmp.Slice(0, idx);
312
313 l.SetShape( sl_tmp );
314
315 //PNS_DBG( dbg, 3int, pclip, WHITE, 500000, wxT(""));
316
317 if( l.Collide( &aRef, aNode, l.Layer(), &ctx ) )
318 {
319 didClip = true;
320 curL -= step;
321 step /= 2;
322 }
323 else
324 {
325 tightest = std::move( sl_tmp );
326
327 if( didClip )
328 {
329 curL += step;
330 step /= 2;
331 }
332 else
333 {
334 break;
335 }
336 }
337 }
338
339 aClipped.SetShape( tightest );
340
341 return didClip;
342}
343
344
345
346
347const std::vector<NET_HANDLE> MULTI_DRAGGER::CurrentNets() const
348{
349 std::set<NET_HANDLE> uniqueNets;
350 for( auto &l : m_mdragLines )
351 {
352 NET_HANDLE net = l.draggedLine.Net();
353 if( net )
354 uniqueNets.insert( net );
355 }
356
357 return std::vector<NET_HANDLE>( uniqueNets.begin(), uniqueNets.end() );
358}
359
360// this is what ultimately gets called when the user clicks/releases the mouse button
361// during drag.
362bool MULTI_DRAGGER::FixRoute( bool aForceCommit )
363{
364 NODE* node = CurrentNode();
365
366 if( node )
367 {
368 // last drag status is OK?
369 if( !m_dragStatus && !Settings().AllowDRCViolations() )
370 return false;
371
372 // commit the current world state
373 Router()->CommitRouting( node );
374 return true;
375 }
376
377 return false;
378}
379
380bool MULTI_DRAGGER::tryWalkaround( NODE* aNode, LINE& aOrig, LINE& aWalk )
381{
382 WALKAROUND walkaround( aNode, Router() );
383 walkaround.SetSolidsOnly( false );
384 walkaround.SetDebugDecorator( Dbg() );
385 walkaround.SetLogger( Logger() );
386 walkaround.SetIterationLimit( Settings().WalkaroundIterationLimit() );
387 walkaround.SetLengthLimit( true, 3.0 );
389
390 aWalk = aOrig;
391
392 WALKAROUND::RESULT wr = walkaround.Route( aWalk );
393
395 {
396 aWalk = wr.lines[ WALKAROUND::WP_SHORTEST ];
397 return true;
398 }
399
400 return false;
401}
402
404{
405 const SEG origLeader = aLine.preDragLine.CSegment( aLine.leaderSegIndex );
406 const DIRECTION_45 origLeaderDir( origLeader );
407
408 for ( int i = 0; i < aLine.draggedLine.SegmentCount(); i++ )
409 {
410 const SEG& curSeg = aLine.draggedLine.CSegment(i);
411 const DIRECTION_45 curDir( curSeg );
412
413 auto ip = curSeg.IntersectLines( m_guide );
414 PNS_DBG(Dbg(), Message, wxString::Format("s %d ip=%d c=%s o=%s", i, ip?1:0, curDir.Format(), origLeaderDir.Format() ));
415 if( ip && curSeg.Contains( *ip ) )
416 {
417 if( curDir == origLeaderDir || curDir == origLeaderDir.Opposite() )
418 return i;
419 }
420 }
421
422 return -1;
423}
424
425void MULTI_DRAGGER::restoreLeaderSegments( std::vector<MDRAG_LINE>& aCompletedLines )
426{
427 m_leaderSegments.clear();
428
429 for( auto& l : aCompletedLines )
430 {
431 if( l.dragOK )
432 {
433 if( m_dragMode == DM_CORNER )
434 {
435 if( l.draggedLine.LinkCount() > 0 )
436 {
437 m_leaderSegments.push_back(
438 static_cast<PNS::ITEM*>( l.draggedLine.GetLink( -1 ) ) );
439 }
440 }
441 else
442 {
443 int newLeaderIdx = findNewLeaderSegment( l );
444 if( newLeaderIdx >= 0 && newLeaderIdx < l.draggedLine.LinkCount() )
445 {
446 m_leaderSegments.push_back(
447 static_cast<PNS::ITEM*>( l.draggedLine.GetLink( newLeaderIdx ) ) );
448 }
449 }
450 }
451 }
452}
453
454bool MULTI_DRAGGER::multidragWalkaround( std::vector<MDRAG_LINE>& aCompletedLines )
455{
456 // fixme: rewrite using shared_ptr...
457 if( m_lastNode )
458 {
459 delete m_lastNode;
460 m_lastNode = nullptr;
461 }
462
463 auto compareDragStartDist = []( const MDRAG_LINE& a, const MDRAG_LINE& b ) -> int
464 {
465 return a.dragDist < b.dragDist;
466 };
467
468 std::sort( aCompletedLines.begin(), aCompletedLines.end(), compareDragStartDist );
469
470
471 NODE* preWalkNode = m_world->Branch();
472
473 for( auto& l : aCompletedLines )
474 {
475 PNS_DBG( Dbg(), AddItem, &l.originalLine, BLUE, 100000, wxString::Format("prewalk-remove lc=%d", l.originalLine.LinkCount() ) );
476 preWalkNode->Remove( l.originalLine );
477 }
478
479 bool fail = false;
480
481 NODE* tmpNodes[2];
482 int totalLength[2];
483
484 for( int attempt = 0; attempt < 2; attempt++ )
485 {
486 NODE *node = tmpNodes[attempt] = preWalkNode->Branch();
487 totalLength[attempt] = 0;
488 fail = false;
489
490 for( int lidx = 0; lidx < (int) aCompletedLines.size(); lidx++ )
491 {
492 MDRAG_LINE& l = aCompletedLines[attempt ? aCompletedLines.size() - 1 - lidx : lidx];
493
494 LINE walk( l.draggedLine );
495 auto result = tryWalkaround( node, l.draggedLine, walk );
496
497 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) );
498 PNS_DBG( Dbg(), AddItem, &walk, BLUE, 100000, wxString::Format("walk lidx=%d attempt=%d", lidx, attempt) );
499
500
501 if( result )
502 {
503 node->Add( walk );
504 totalLength[attempt] += walk.CLine().Length() - l.draggedLine.CLine().Length();
505 l.draggedLine = std::move( walk );
506 }
507 else
508 {
509 delete node;
510 tmpNodes[attempt] = nullptr;
511 fail = true;
512 break;
513 }
514 }
515 }
516
517 if( fail )
518 return false;
519
520
521 bool rv = false;
522
523 if( tmpNodes[0] && tmpNodes[1] )
524 {
525 if ( totalLength[0] < totalLength[1] )
526 {
527 delete tmpNodes[1];
528 m_lastNode = tmpNodes[0];
529 rv = true;
530 }
531 else
532 {
533 delete tmpNodes[0];
534 m_lastNode = tmpNodes[1];
535 rv = true;
536 }
537 }
538 else if ( tmpNodes[0] )
539 {
540 m_lastNode = tmpNodes[0];
541 rv = true;
542 }
543 else if ( tmpNodes[1] )
544 {
545 m_lastNode = tmpNodes[1];
546 rv = true;
547 }
548
549 restoreLeaderSegments( aCompletedLines );
550
551 return rv;
552}
553
554
555bool MULTI_DRAGGER::multidragMarkObstacles( std::vector<MDRAG_LINE>& aCompletedLines )
556{
557
558// fixme: rewrite using shared_ptr...
559 if( m_lastNode )
560 {
561 delete m_lastNode;
562 m_lastNode = nullptr;
563 }
564
565 // m_lastNode contains the temporary (post-modification) state. Think of it as
566 // of an efficient undo buffer. We don't change the PCB directly, but a branch of it
567 // created below. We can then commit its state (applying the modifications to the host board
568 // by calling ROUTING::CommitRouting(m_lastNode) or simply discard it.
569 m_lastNode = m_world->Branch();
570
571
572 for( int l1 = 0; l1 < (int)aCompletedLines.size(); l1++ )
573 {
574 for( int l2 = l1 + 1; l2 < (int)aCompletedLines.size(); l2++ )
575 {
576 const auto& l1l = aCompletedLines[l1].draggedLine;
577 auto l2l = aCompletedLines[l2].draggedLine;
578
579 if( clipToOtherLine( m_lastNode, l1l, l2l ) )
580 aCompletedLines[l2].draggedLine = l2l;
581 }
582 }
583
584 for ( auto&l : aCompletedLines )
585 {
586 m_lastNode->Remove( l.originalLine );
587 m_lastNode->Add( l.draggedLine );
588 }
589
590 restoreLeaderSegments( aCompletedLines );
591
592 return true;
593}
594
595bool MULTI_DRAGGER::multidragShove( std::vector<MDRAG_LINE>& aCompletedLines )
596{
597 if( m_lastNode )
598 {
599 delete m_lastNode;
600 m_lastNode = nullptr;
601 }
602
603 if( !m_shove )
604 return false;
605
606 auto compareDragStartDist = []( const MDRAG_LINE& a, const MDRAG_LINE& b ) -> int
607 {
608 return a.dragDist < b.dragDist;
609 };
610
611 std::sort( aCompletedLines.begin(), aCompletedLines.end(), compareDragStartDist );
612
613 auto iface = Router()->GetInterface();
614
615 for( auto& l : m_mdragLines )
616 {
617 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"),
618 iface->GetNetName( l.draggedLine.Net() ),
619 (int) l.isCorner?1:0,
620 (int) l.isStrict?1:0,
621 (int) l.cornerDistance,
622 (int) l.leaderSegDistance,
623 (int) l.leaderSegIndex,
624 (int) l.cornerIsLast?1:0,
625 (int) l.dragDist ) );
626 }
627
628
629 m_shove->SetDefaultShovePolicy( SHOVE::SHP_SHOVE );
630 m_shove->ClearHeads();
631
632 for( auto& l : aCompletedLines )
633 {
634 PNS_DBG( Dbg(), AddItem, &l.draggedLine, GREEN, 0, "dragged-line" );
635 m_shove->AddHeads( l.draggedLine, SHOVE::SHP_SHOVE | SHOVE::SHP_DONT_OPTIMIZE );
636 }
637
638 auto status = m_shove->Run();
639
640 m_lastNode = m_shove->CurrentNode()->Branch();
641
642 // Re-add any m_mdragLines that were removed from m_preShoveNode during Start() but
643 // are not part of aCompletedLines. Without this, lines that fail the drag angle check
644 // would be silently deleted from the board.
645 std::set<int> completedIndices;
646
647 for( const auto& cl : aCompletedLines )
648 completedIndices.insert( cl.mdragIndex );
649
650 for( const auto& ml : m_mdragLines )
651 {
652 if( completedIndices.find( ml.mdragIndex ) == completedIndices.end() )
653 {
654 LINE preserved( ml.originalLine );
655 preserved.ClearLinks();
656 m_lastNode->Add( preserved );
657 }
658 }
659
660 if( status == SHOVE::SH_OK )
661 {
662 for( int i = 0; i < (int) aCompletedLines.size(); i++ )
663 {
664 MDRAG_LINE&l = aCompletedLines[i];
665
666 if( m_shove->HeadsModified( i ) )
667 l.draggedLine = m_shove->GetModifiedHead( i );
668
669 // this should not be linked (assert in rt-test)
671
672 m_lastNode->Add( l.draggedLine );
673 }
674 }
675 else
676 {
677 return false;
678 }
679
680 restoreLeaderSegments( aCompletedLines );
681
682 return true;
683}
684
685// this is called every time the user moves the mouse while dragging a set of multiple tracks
687{
688 std::optional<LINE> primaryPreDrag, primaryDragged;
689
690
691
692 SEG lastPreDrag;
693 DIRECTION_45 primaryDir;
694 VECTOR2I perp;
695
696 DIRECTION_45 primaryLastSegDir;
697 std::vector<MDRAG_LINE> completed;
698
699 auto tryPosture = [&] ( int aVariant ) -> bool
700 {
701 MDRAG_LINE* primaryLine = nullptr;
702
703 for( auto &l : m_mdragLines )
704 {
705 l.dragOK = false;
706 l.preDragLine = l.originalLine;
707 //PNS_DBG( Dbg(), AddItem, &l.originalLine, GREEN, 300000, "par" );
708 if( l.isPrimaryLine )
709 {
710
711 //PNS_DBG( Dbg(), AddItem, &l.originalLine, BLUE, 300000, wxT("mdrag-prim"));
712
713 // create a copy of the primary line (pre-drag and post-drag).
714 // the pre-drag version is necessary for NODE::Remove() to be able to
715 // find out the segments before modification by the multidrag algorithm
716 primaryDragged = l.originalLine;
717 primaryDragged->ClearLinks();
718 primaryPreDrag = l.originalLine;
719 primaryLine = &l;
720
721 }
722 }
723
724 if( aVariant == 1 && (primaryPreDrag->PointCount() > 2) )
725 {
726 primaryPreDrag->Line().Remove( -1 );
727 primaryDragged->Line().Remove( -1 );
728
729 for( auto&l : m_mdragLines )
730 {
731 l.preDragLine.Line().Remove(-1);
732 }
733 }
734
735 completed.clear();
736
737 int snapThreshold = Settings().SmoothDraggedSegments() ? primaryDragged->Width() / 4 : 0;
738
739 if( m_dragMode == DM_CORNER )
740 {
741 // first, drag only the primary line
742 PNS_DBG( Dbg(), AddPoint, primaryDragged->CLastPoint(), YELLOW, 600000, wxT("mdrag-sec"));
743
744 lastPreDrag = primaryPreDrag->CSegment( -1 );
745 primaryDir = DIRECTION_45( lastPreDrag );
746
747 primaryDragged->SetSnapThreshhold( snapThreshold );
748 primaryDragged->DragCorner( aP, primaryDragged->PointCount() - 1, false );
749
750
751 if( primaryDragged->SegmentCount() > 0 )
752 {
753 SEG lastPrimDrag = primaryDragged->CSegment( -1 );
754
755 if ( aVariant == 2 )
756 lastPrimDrag = lastPreDrag;
757
758 auto lastSeg = primaryDragged->CSegment( -1 );
759 if( DIRECTION_45( lastSeg ) != primaryDir )
760 {
761 if( lastSeg.Length() < primaryDragged->Width() )
762 {
763 lastPrimDrag = lastPreDrag;
764 }
765 }
766
767 perp = (lastPrimDrag.B - lastPrimDrag.A).Perpendicular();
768 primaryLastSegDir = DIRECTION_45( lastPrimDrag );
769
770
771 PNS_DBG( Dbg(), AddItem, &(*primaryDragged), LIGHTGRAY, 100000, "prim" );
772 PNS_DBG( Dbg(), AddShape, SEG(lastPrimDrag.B, lastPrimDrag.B + perp), LIGHTGRAY, 100000, wxString::Format("prim-perp-seg") );
773 } else {
774 return false;
775 }
776
777
778
779// PNS_DBG( Dbg(), AddShape, &ll, LIGHTBLUE, 200000, "par" );
780
781 }
782 else
783 {
784
785 SHAPE_LINE_CHAIN ll2( { lastPreDrag.A, lastPreDrag.B } );
786 PNS_DBG( Dbg(), AddShape, &ll2, LIGHTYELLOW, 300000, "par" );
787 lastPreDrag = primaryDragged->CSegment( primaryLine->leaderSegIndex );
788 primaryDragged->SetSnapThreshhold( snapThreshold );
789 primaryDragged->DragSegment( aP, primaryLine->leaderSegIndex );
790 perp = (primaryLine->midSeg.B - primaryLine->midSeg.A).Perpendicular();
791 m_guide = SEG( aP, aP + perp );
792 }
793
794
796 m_draggedItems.Clear();
797
798 // now drag all other lines
799 for( auto& l : m_mdragLines )
800 {
801 //PNS_DBG( Dbg(), AddPoint, l.originalLine.CPoint( l.cornerIndex ), WHITE, 1000000, wxT("l-end"));
802 if( l.isDraggable )
803 {
804 l.dragOK = false;
805 //PNS_DBG( Dbg(), AddItem, &l.originalLine, GREEN, 100000, wxT("mdrag-sec"));
806
807 // reject nulls
808 if( l.preDragLine.SegmentCount() >= 1 )
809 {
810
811 //PNS_DBG( Dbg(), AddPoint, l.preDragLine.CPoint( l.cornerIndex ), YELLOW, 600000, wxT("mdrag-sec"));
812
813 // check the direction of the last segment of the line against the direction of
814 // the last segment of the primary line (both before dragging) and perform drag
815 // only when the directions are the same. The algorithm here is quite trival and
816 // otherwise would produce really awkward results. There's of course a TON of
817 // room for improvement here :-)
818
819 if( m_dragMode == DM_CORNER )
820 {
821 DIRECTION_45 parallelDir( l.preDragLine.CSegment( -1 ) );
822
823 auto leadAngle = primaryDir.Angle( parallelDir );
824
825 if( leadAngle == DIRECTION_45::ANG_OBTUSE
826 || leadAngle == DIRECTION_45::ANG_RIGHT
827 || leadAngle == DIRECTION_45::ANG_STRAIGHT )
828 {
829 // compute the distance between the primary line and the last point of
830 // the currently processed line
831 int dist = lastPreDrag.LineDistance( l.preDragLine.CLastPoint(), true );
832
833 // now project it on the perpendicular line we computed before
834 auto projected = aP + perp.Resize( dist );
835
836
837 LINE parallelDragged( l.preDragLine );
838
839 PNS_DBG( Dbg(), AddPoint, projected, LIGHTGRAY, 100000, "dragged-c" );
840 PNS_DBG( Dbg(), AddPoint, parallelDragged.CLastPoint(), LIGHTGRAY, 100000, wxString::Format("orig-c cil %d", l.cornerIsLast?1:0) );
841
842 parallelDragged.ClearLinks();
843 //m_lastNode->Remove( parallelDragged );
844 // drag the non-primary line's end trying to place it at the projected point
845 parallelDragged.DragCorner( projected, parallelDragged.PointCount() - 1,
846 false, primaryLastSegDir );
847
848 PNS_DBG( Dbg(), AddPoint, projected, LIGHTYELLOW, 600000,
849 wxT( "l-end" ) );
850
851 l.dragOK = true;
852
853 if( !l.isPrimaryLine )
854 {
855 l.draggedLine = parallelDragged;
856 completed.push_back( l );
857 m_draggedItems.Add( parallelDragged );
858 }
859 }
860 }
861 else if ( m_dragMode == DM_SEGMENT )
862 {
863 SEG sdrag = l.midSeg;
864 DIRECTION_45 refDir( lastPreDrag );
865 DIRECTION_45 curDir( sdrag );
866 auto ang = refDir.Angle( curDir );
867
869 {
870 int dist = lastPreDrag.LineDistance(
871 l.preDragLine.CPoint( l.leaderSegIndex ), true );
872 auto projected = aP + perp.Resize( dist );
873
874 SEG sperp( aP, aP + perp.Resize( 10000000 ) );
875 VECTOR2I startProj = sperp.LineProject( m_dragStartPoint );
876
877 SHAPE_LINE_CHAIN ll( { sperp.A, sperp.B } );
878
879
880 PNS_DBG( Dbg(), AddShape, &ll, LIGHTBLUE, 100000, "par" );
881 SHAPE_LINE_CHAIN ll2( { sdrag.A, sdrag.B } );
882 PNS_DBG( Dbg(), AddShape, &ll2, LIGHTBLUE, 100000, "sdrag" );
883 VECTOR2I v = projected - startProj;
884 l.dragDist = v.EuclideanNorm() * sign( v.Dot( perp ) );
885 l.dragOK = true;
886
887 if( !l.isPrimaryLine )
888 {
889 l.draggedLine = l.preDragLine;
890 l.draggedLine.ClearLinks();
891 l.draggedLine.SetSnapThreshhold( snapThreshold );
892 l.draggedLine.DragSegment( projected, l.leaderSegIndex, false );
893 completed.push_back( l );
894 PNS_DBG( Dbg(), AddItem, &l.draggedLine, LIGHTBLUE, 100000,
895 "dragged" );
896 }
897
898
899 PNS_DBG( Dbg(), AddPoint, startProj, LIGHTBLUE, 400000,
900 wxT( "startProj" ) );
901 PNS_DBG( Dbg(), AddPoint, projected, LIGHTRED, 400000,
902 wxString::Format( "pro dd=%d", l.dragDist ) );
903 }
904 }
905 }
906 }
907
908 if (l.isPrimaryLine)
909 {
910 l.draggedLine = *primaryDragged;
911 l.dragOK = true;
912 completed.push_back( l );
913 }
914 }
915
916 if( m_dragMode == DM_SEGMENT )
917 return true;
918 else
919 {
920 for ( const auto &l: completed )
921 {
922 if( !l.dragOK && aVariant < 2 )
923 return false;
924
925 if( l.isPrimaryLine )
926 continue;
927
928 DIRECTION_45 lastDir ( l.draggedLine.CSegment(-1) );
929
930 if( lastDir != primaryLastSegDir )
931 return false;
932 }
933 }
934
935 return true;
936 };
937
938 bool res = false;
939
940 for( int variant = 0; variant < 3; variant++ )
941 {
942 res = tryPosture( variant );
943
944 if( res )
945 break;
946 }
947
948 switch( Settings().Mode() )
949 {
950 case RM_Walkaround:
951 m_dragStatus = multidragWalkaround ( completed );
952 break;
953
954 case RM_Shove:
955 m_dragStatus = multidragShove ( completed );
956 break;
957
958 case RM_MarkObstacles:
960 break;
961
962
963
964 default:
965 break;
966 }
967
968 return m_dragStatus;
969}
970
971
976
977
979{
980 return m_draggedItems;
981}
982
983
985{
986 // fixme: should we care?
987 return 0;
988}
989
990
991} // 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:294
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
const VECTOR2I & CLastPoint() const
Definition pns_line.h:151
void DragCorner(const VECTOR2I &aP, int aIndex, bool aFreeAngle=false, DIRECTION_45 aPreferredEndingDirection=DIRECTION_45())
Definition pns_line.cpp:832
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:240
NODE * Branch()
Create a lightweight copy (called branch) of self that tracks the changes (added/removed items) wrs t...
Definition pns_node.cpp:157
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Add an item to the current node.
Definition pns_node.cpp:749
void Remove(ARC *aArc)
Remove an item from this branch.
Definition pns_node.cpp:993
ROUTER_IFACE * GetInterface() const
Definition pns_router.h:236
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:746
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:685
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:76
@ DM_CORNER
Definition pns_router.h:77
@ DM_SEGMENT
Definition pns_router.h:78
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