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 l.mdragIndex = static_cast<int>( m_mdragLines.size() );
80 m_mdragLines.push_back( std::move( l ) );
81 }
82 }
83
84 int n = 0;
85
86 bool anyStrictCornersFound = false;
87 bool anyStrictMidSegsFound = false;
88
89 for( auto& l : m_mdragLines )
90 {
91 const int thr = l.originalLine.Width() / 2;
92
93 const VECTOR2I& origFirst = l.originalLine.CLine().CPoint( 0 );
94 const int distFirst = ( origFirst - aP ).EuclideanNorm();
95
96 const VECTOR2I& origLast = l.originalLine.CLine().CLastPoint();
97 const int distLast = ( origLast - aP ).EuclideanNorm();
98
99 l.cornerDistance = std::min( distFirst, distLast );
100
101 bool takeFirst = false;
102 auto ilast = aPrimitives.FindVertex( origLast );
103 auto ifirst = aPrimitives.FindVertex( origFirst );
104
105 if( ilast && ifirst )
106 takeFirst = distFirst < distLast;
107 else if( ilast )
108 takeFirst = false;
109 else if( ifirst )
110 takeFirst = true;
111
112 if( ifirst || ilast )
113 {
114 if( takeFirst )
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 else
128 {
129 l.cornerIsLast = true;
130 l.leaderSegIndex = l.originalLine.SegmentCount() - 1;
131 l.cornerDistance = distLast;
132 l.isCorner = true;
133
134 if( distLast <= thr )
135 {
136 l.isStrict = true;
137 l.cornerDistance = 0;
138 }
139 }
140 }
141
142 const auto& links = l.originalLine.Links();
143
144 for( int lidx = 0; lidx < (int) links.size(); lidx++ )
145 {
146 if( auto lseg = dyn_cast<SEGMENT*>( links[lidx] ) )
147 {
148
149 if( !aPrimitives.Contains( lseg ) )
150 continue;
151
152 int d = lseg->Seg().Distance( aP );
153
154 l.midSeg = lseg->Seg();
155 l.isMidSeg = true;
156 l.leaderSegIndex = lidx;
157 l.leaderSegDistance = d + thr;
158
159 if( d < thr && !l.isStrict )
160 {
161 l.isCorner = false;
162 l.isStrict = true;
163 l.leaderSegDistance = 0;
164 }
165 }
166 }
167
168 if( l.isStrict )
169 {
170 anyStrictCornersFound |= l.isCorner;
171 anyStrictMidSegsFound |= !l.isCorner;
172 }
173 }
174
175 if( anyStrictCornersFound )
177 else if (anyStrictMidSegsFound )
179 else
180 {
181 int minLeadSegDist = std::numeric_limits<int>::max();
182 int minCornerDist = std::numeric_limits<int>::max();
183 MDRAG_LINE *bestSeg = nullptr;
184 MDRAG_LINE *bestCorner = nullptr;
185
186 for( auto& l : m_mdragLines )
187 {
188 if( l.cornerDistance < minCornerDist )
189 {
190 minCornerDist = l.cornerDistance;
191 bestCorner = &l;
192 }
193 if( l.leaderSegDistance < minLeadSegDist )
194 {
195 minLeadSegDist = l.leaderSegDistance;
196 bestSeg = &l;
197 }
198 }
199
200 if( bestCorner && bestSeg )
201 {
202 if( minCornerDist < minLeadSegDist )
203 {
205 bestCorner->isPrimaryLine = true;
206 }
207 else
208 {
210 bestSeg->isPrimaryLine = true;
211 }
212 }
213 else if ( bestCorner )
214 {
216 bestCorner->isPrimaryLine = true;
217 }
218 else if ( bestSeg )
219 {
221 bestSeg->isPrimaryLine = true;
222 }
223 else return false; // can it really happen?
224 }
225
226 if( m_dragMode == DM_CORNER )
227 {
228 for( auto& l : m_mdragLines )
229 {
230 // make sure the corner to drag is the last one
231 if ( !l.cornerIsLast )
232 {
233 l.originalLine.Reverse();
234 l.cornerIsLast = true;
235 }
236 // and if it's connected (non-trivial fanout), disregard it
237
238 const JOINT* jt = m_world->FindJoint( l.originalLine.CLastPoint(), &l.originalLine );
239
240 assert (jt != nullptr);
241
242 if( !jt->IsTrivialEndpoint() )
243 {
244 m_dragMode = DM_SEGMENT; // fallback to segment mode if non-trivial endpoints found
245 }
246 }
247 }
248
249 for( auto& l : m_mdragLines )
250 {
251 if( (anyStrictCornersFound || anyStrictMidSegsFound) && l.isStrict )
252 {
253 l.isPrimaryLine = true;
254 break;
255 }
256 }
257
258 m_origDraggedItems = aPrimitives;
259
260 if( Settings().Mode() == RM_Shove )
261 {
262 m_preShoveNode = m_world->Branch();
263
264 for( auto& l : m_mdragLines )
265 {
266 m_preShoveNode->Remove( l.originalLine );
267 }
268
269 m_shove.reset( new SHOVE( m_preShoveNode, Router() ) );
270 m_shove->SetLogger( Logger() );
271 m_shove->SetDebugDecorator( Dbg() );
272 m_shove->SetDefaultShovePolicy( SHOVE::SHP_SHOVE | SHOVE::SHP_DONT_LOCK_ENDPOINTS );
273 }
274
275 return true;
276}
277
278
282
283
285{
286 return DM_CORNER;
287}
288
289bool clipToOtherLine( NODE* aNode, const LINE& aRef, LINE& aClipped )
290{
291 std::set<OBSTACLE> obstacles;
292 COLLISION_SEARCH_CONTEXT ctx( obstacles );
293
294 constexpr int clipLengthThreshold = 100;
295
296 //DEBUG_DECORATOR* dbg = ROUTER::GetInstance()->GetInterface()->GetDebugDecorator();
297
298 LINE l( aClipped );
299 SHAPE_LINE_CHAIN tightest;
300
301 bool didClip = false;
302 int curL = l.CLine().Length();
303 int step = curL / 2 - 1;
304
305 while( step > clipLengthThreshold )
306 {
307 SHAPE_LINE_CHAIN sl_tmp( aClipped.CLine() );
308 VECTOR2I pclip = sl_tmp.PointAlong( curL );
309 int idx = sl_tmp.Split( pclip );
310 sl_tmp = sl_tmp.Slice(0, idx);
311
312 l.SetShape( sl_tmp );
313
314 //PNS_DBG( dbg, 3int, pclip, WHITE, 500000, wxT(""));
315
316 if( l.Collide( &aRef, aNode, l.Layer(), &ctx ) )
317 {
318 didClip = true;
319 curL -= step;
320 step /= 2;
321 }
322 else
323 {
324 tightest = std::move( sl_tmp );
325
326 if( didClip )
327 {
328 curL += step;
329 step /= 2;
330 }
331 else
332 {
333 break;
334 }
335 }
336 }
337
338 aClipped.SetShape( tightest );
339
340 return didClip;
341}
342
343
344
345
346const std::vector<NET_HANDLE> MULTI_DRAGGER::CurrentNets() const
347{
348 std::set<NET_HANDLE> uniqueNets;
349 for( auto &l : m_mdragLines )
350 {
351 NET_HANDLE net = l.draggedLine.Net();
352 if( net )
353 uniqueNets.insert( net );
354 }
355
356 return std::vector<NET_HANDLE>( uniqueNets.begin(), uniqueNets.end() );
357}
358
359// this is what ultimately gets called when the user clicks/releases the mouse button
360// during drag.
361bool MULTI_DRAGGER::FixRoute( bool aForceCommit )
362{
363 NODE* node = CurrentNode();
364
365 if( node )
366 {
367 // last drag status is OK?
368 if( !m_dragStatus && !Settings().AllowDRCViolations() )
369 return false;
370
371 // commit the current world state
372 Router()->CommitRouting( node );
373 return true;
374 }
375
376 return false;
377}
378
379bool MULTI_DRAGGER::tryWalkaround( NODE* aNode, LINE& aOrig, LINE& aWalk )
380{
381 WALKAROUND walkaround( aNode, Router() );
382 bool ok = false;
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 < 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 int nclipped = 0;
573 for( int l1 = 0; l1 < aCompletedLines.size(); l1++ )
574 {
575 for( int l2 = l1 + 1; l2 < aCompletedLines.size(); l2++ )
576 {
577 const auto& l1l = aCompletedLines[l1].draggedLine;
578 auto l2l = aCompletedLines[l2].draggedLine;
579
580 if( clipToOtherLine( m_lastNode, l1l, l2l ) )
581 {
582 aCompletedLines[l2].draggedLine = l2l;
583 nclipped++;
584 }
585 }
586 }
587
588 for ( auto&l : aCompletedLines )
589 {
590 m_lastNode->Remove( l.originalLine );
591 m_lastNode->Add( l.draggedLine );
592 }
593
594 restoreLeaderSegments( aCompletedLines );
595
596 return true;
597}
598
599bool MULTI_DRAGGER::multidragShove( std::vector<MDRAG_LINE>& aCompletedLines )
600{
601 if( m_lastNode )
602 {
603 delete m_lastNode;
604 m_lastNode = nullptr;
605 }
606
607 if( !m_shove )
608 return false;
609
610 auto compareDragStartDist = []( const MDRAG_LINE& a, const MDRAG_LINE& b ) -> int
611 {
612 return a.dragDist < b.dragDist;
613 };
614
615 std::sort( aCompletedLines.begin(), aCompletedLines.end(), compareDragStartDist );
616
617 auto iface = Router()->GetInterface();
618
619 for( auto& l : m_mdragLines )
620 {
621 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"),
622 iface->GetNetName( l.draggedLine.Net() ),
623 (int) l.isCorner?1:0,
624 (int) l.isStrict?1:0,
625 (int) l.cornerDistance,
626 (int) l.leaderSegDistance,
627 (int) l.leaderSegIndex,
628 (int) l.cornerIsLast?1:0,
629 (int) l.dragDist ) );
630 }
631
632
633 m_shove->SetDefaultShovePolicy( SHOVE::SHP_SHOVE );
634 m_shove->ClearHeads();
635
636 for( auto& l : aCompletedLines )
637 {
638 PNS_DBG( Dbg(), AddItem, &l.draggedLine, GREEN, 0, "dragged-line" );
639 m_shove->AddHeads( l.draggedLine, SHOVE::SHP_SHOVE | SHOVE::SHP_DONT_OPTIMIZE );
640 }
641
642 auto status = m_shove->Run();
643
644 m_lastNode = m_shove->CurrentNode()->Branch();
645
646 // Re-add any m_mdragLines that were removed from m_preShoveNode during Start() but
647 // are not part of aCompletedLines. Without this, lines that fail the drag angle check
648 // would be silently deleted from the board.
649 std::set<int> completedIndices;
650
651 for( const auto& cl : aCompletedLines )
652 completedIndices.insert( cl.mdragIndex );
653
654 for( const auto& ml : m_mdragLines )
655 {
656 if( completedIndices.find( ml.mdragIndex ) == completedIndices.end() )
657 {
658 LINE preserved( ml.originalLine );
659 preserved.ClearLinks();
660 m_lastNode->Add( preserved );
661 }
662 }
663
664 if( status == SHOVE::SH_OK )
665 {
666 for( int i = 0; i < (int) aCompletedLines.size(); i++ )
667 {
668 MDRAG_LINE&l = aCompletedLines[i];
669
670 if( m_shove->HeadsModified( i ) )
671 l.draggedLine = m_shove->GetModifiedHead( i );
672
673 // this should not be linked (assert in rt-test)
675
676 m_lastNode->Add( l.draggedLine );
677 }
678 }
679 else
680 {
681 return false;
682 }
683
684 restoreLeaderSegments( aCompletedLines );
685
686 return true;
687}
688
689// this is called every time the user moves the mouse while dragging a set of multiple tracks
691{
692 std::optional<LINE> primaryPreDrag, primaryDragged;
693
694
695
696 SEG lastPreDrag;
697 DIRECTION_45 primaryDir;
698 VECTOR2I perp;
699
700 DIRECTION_45 primaryLastSegDir;
701 std::vector<MDRAG_LINE> completed;
702
703 auto tryPosture = [&] ( int aVariant ) -> bool
704 {
705 MDRAG_LINE* primaryLine = nullptr;
706
707 for( auto &l : m_mdragLines )
708 {
709 l.dragOK = false;
710 l.preDragLine = l.originalLine;
711 //PNS_DBG( Dbg(), AddItem, &l.originalLine, GREEN, 300000, "par" );
712 if( l.isPrimaryLine )
713 {
714
715 //PNS_DBG( Dbg(), AddItem, &l.originalLine, BLUE, 300000, wxT("mdrag-prim"));
716
717 // create a copy of the primary line (pre-drag and post-drag).
718 // the pre-drag version is necessary for NODE::Remove() to be able to
719 // find out the segments before modification by the multidrag algorithm
720 primaryDragged = l.originalLine;
721 primaryDragged->ClearLinks();
722 primaryPreDrag = l.originalLine;
723 primaryLine = &l;
724
725 }
726 }
727
728 if( aVariant == 1 && (primaryPreDrag->PointCount() > 2) )
729 {
730 primaryPreDrag->Line().Remove( -1 );
731 primaryDragged->Line().Remove( -1 );
732
733 for( auto&l : m_mdragLines )
734 {
735 l.preDragLine.Line().Remove(-1);
736 }
737 }
738
739 completed.clear();
740
741 int snapThreshold = Settings().SmoothDraggedSegments() ? primaryDragged->Width() / 4 : 0;
742
743 if( m_dragMode == DM_CORNER )
744 {
745 // first, drag only the primary line
746 PNS_DBG( Dbg(), AddPoint, primaryDragged->CLastPoint(), YELLOW, 600000, wxT("mdrag-sec"));
747
748 lastPreDrag = primaryPreDrag->CSegment( -1 );
749 primaryDir = DIRECTION_45( lastPreDrag );
750
751 primaryDragged->SetSnapThreshhold( snapThreshold );
752 primaryDragged->DragCorner( aP, primaryDragged->PointCount() - 1, false );
753
754
755 if( primaryDragged->SegmentCount() > 0 )
756 {
757 SEG lastPrimDrag = primaryDragged->CSegment( -1 );
758
759 if ( aVariant == 2 )
760 lastPrimDrag = lastPreDrag;
761
762 auto lastSeg = primaryDragged->CSegment( -1 );
763 if( DIRECTION_45( lastSeg ) != primaryDir )
764 {
765 if( lastSeg.Length() < primaryDragged->Width() )
766 {
767 lastPrimDrag = lastPreDrag;
768 }
769 }
770
771 perp = (lastPrimDrag.B - lastPrimDrag.A).Perpendicular();
772 primaryLastSegDir = DIRECTION_45( lastPrimDrag );
773
774
775 PNS_DBG( Dbg(), AddItem, &(*primaryDragged), LIGHTGRAY, 100000, "prim" );
776 PNS_DBG( Dbg(), AddShape, SEG(lastPrimDrag.B, lastPrimDrag.B + perp), LIGHTGRAY, 100000, wxString::Format("prim-perp-seg") );
777 } else {
778 return false;
779 }
780
781
782
783// PNS_DBG( Dbg(), AddShape, &ll, LIGHTBLUE, 200000, "par" );
784
785 }
786 else
787 {
788
789 SHAPE_LINE_CHAIN ll2( { lastPreDrag.A, lastPreDrag.B } );
790 PNS_DBG( Dbg(), AddShape, &ll2, LIGHTYELLOW, 300000, "par" );
791 lastPreDrag = primaryDragged->CSegment( primaryLine->leaderSegIndex );
792 primaryDragged->SetSnapThreshhold( snapThreshold );
793 primaryDragged->DragSegment( aP, primaryLine->leaderSegIndex );
794 perp = (primaryLine->midSeg.B - primaryLine->midSeg.A).Perpendicular();
795 m_guide = SEG( aP, aP + perp );
796 }
797
798
800 m_draggedItems.Clear();
801
802 // now drag all other lines
803 for( auto& l : m_mdragLines )
804 {
805 //PNS_DBG( Dbg(), AddPoint, l.originalLine.CPoint( l.cornerIndex ), WHITE, 1000000, wxT("l-end"));
806 if( l.isDraggable )
807 {
808 l.dragOK = false;
809 //PNS_DBG( Dbg(), AddItem, &l.originalLine, GREEN, 100000, wxT("mdrag-sec"));
810
811 // reject nulls
812 if( l.preDragLine.SegmentCount() >= 1 )
813 {
814
815 //PNS_DBG( Dbg(), AddPoint, l.preDragLine.CPoint( l.cornerIndex ), YELLOW, 600000, wxT("mdrag-sec"));
816
817 // check the direction of the last segment of the line against the direction of
818 // the last segment of the primary line (both before dragging) and perform drag
819 // only when the directions are the same. The algorithm here is quite trival and
820 // otherwise would produce really awkward results. There's of course a TON of
821 // room for improvement here :-)
822
823 if( m_dragMode == DM_CORNER )
824 {
825 DIRECTION_45 parallelDir( l.preDragLine.CSegment( -1 ) );
826
827 auto leadAngle = primaryDir.Angle( parallelDir );
828
829 if( leadAngle == DIRECTION_45::ANG_OBTUSE
830 || leadAngle == DIRECTION_45::ANG_RIGHT
831 || leadAngle == DIRECTION_45::ANG_STRAIGHT )
832 {
833 // compute the distance between the primary line and the last point of
834 // the currently processed line
835 int dist = lastPreDrag.LineDistance( l.preDragLine.CLastPoint(), true );
836
837 // now project it on the perpendicular line we computed before
838 auto projected = aP + perp.Resize( dist );
839
840
841 LINE parallelDragged( l.preDragLine );
842
843 PNS_DBG( Dbg(), AddPoint, projected, LIGHTGRAY, 100000, "dragged-c" );
844 PNS_DBG( Dbg(), AddPoint, parallelDragged.CLastPoint(), LIGHTGRAY, 100000, wxString::Format("orig-c cil %d", l.cornerIsLast?1:0) );
845
846 parallelDragged.ClearLinks();
847 //m_lastNode->Remove( parallelDragged );
848 // drag the non-primary line's end trying to place it at the projected point
849 parallelDragged.DragCorner( projected, parallelDragged.PointCount() - 1,
850 false, primaryLastSegDir );
851
852 PNS_DBG( Dbg(), AddPoint, projected, LIGHTYELLOW, 600000,
853 wxT( "l-end" ) );
854
855 l.dragOK = true;
856
857 if( !l.isPrimaryLine )
858 {
859 l.draggedLine = parallelDragged;
860 completed.push_back( l );
861 m_draggedItems.Add( parallelDragged );
862 }
863 }
864 }
865 else if ( m_dragMode == DM_SEGMENT )
866 {
867 SEG sdrag = l.midSeg;
868 DIRECTION_45 refDir( lastPreDrag );
869 DIRECTION_45 curDir( sdrag );
870 auto ang = refDir.Angle( curDir );
871
873 {
874 int dist = lastPreDrag.LineDistance(
875 l.preDragLine.CPoint( l.leaderSegIndex ), true );
876 auto projected = aP + perp.Resize( dist );
877
878 SEG sperp( aP, aP + perp.Resize( 10000000 ) );
879 VECTOR2I startProj = sperp.LineProject( m_dragStartPoint );
880
881 SHAPE_LINE_CHAIN ll( { sperp.A, sperp.B } );
882
883
884 PNS_DBG( Dbg(), AddShape, &ll, LIGHTBLUE, 100000, "par" );
885 SHAPE_LINE_CHAIN ll2( { sdrag.A, sdrag.B } );
886 PNS_DBG( Dbg(), AddShape, &ll2, LIGHTBLUE, 100000, "sdrag" );
887 VECTOR2I v = projected - startProj;
888 l.dragDist = v.EuclideanNorm() * sign( v.Dot( perp ) );
889 l.dragOK = true;
890
891 if( !l.isPrimaryLine )
892 {
893 l.draggedLine = l.preDragLine;
894 l.draggedLine.ClearLinks();
895 l.draggedLine.SetSnapThreshhold( snapThreshold );
896 l.draggedLine.DragSegment( projected, l.leaderSegIndex, false );
897 completed.push_back( l );
898 PNS_DBG( Dbg(), AddItem, &l.draggedLine, LIGHTBLUE, 100000,
899 "dragged" );
900 }
901
902
903 PNS_DBG( Dbg(), AddPoint, startProj, LIGHTBLUE, 400000,
904 wxT( "startProj" ) );
905 PNS_DBG( Dbg(), AddPoint, projected, LIGHTRED, 400000,
906 wxString::Format( "pro dd=%d", l.dragDist ) );
907 }
908 }
909 }
910 }
911
912 if (l.isPrimaryLine)
913 {
914 l.draggedLine = *primaryDragged;
915 l.dragOK = true;
916 completed.push_back( l );
917 }
918 }
919
920 if( m_dragMode == DM_SEGMENT )
921 return true;
922 else
923 {
924 for ( const auto &l: completed )
925 {
926 if( !l.dragOK && aVariant < 2 )
927 return false;
928
929 if( l.isPrimaryLine )
930 continue;
931
932 DIRECTION_45 lastDir ( l.draggedLine.CSegment(-1) );
933
934 if( lastDir != primaryLastSegDir )
935 return false;
936 }
937 }
938
939 return true;
940 };
941
942 bool res = false;
943
944 for( int variant = 0; variant < 3; variant++ )
945 {
946 res = tryPosture( variant );
947
948 if( res )
949 break;
950 }
951
952 switch( Settings().Mode() )
953 {
954 case RM_Walkaround:
955 m_dragStatus = multidragWalkaround ( completed );
956 break;
957
958 case RM_Shove:
959 m_dragStatus = multidragShove ( completed );
960 break;
961
962 case RM_MarkObstacles:
964 break;
965
966
967
968 default:
969 break;
970 }
971
972 return m_dragStatus;
973}
974
975
980
981
983{
984 return m_draggedItems;
985}
986
987
989{
990 // fixme: should we care?
991 return 0;
992}
993
994
995} // 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
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: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:240
NODE * Branch()
Create a lightweight copy (called branch) of self that tracks the changes (added/removed items) wrs t...
Definition pns_node.cpp:155
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Add an item to the current node.
Definition pns_node.cpp:695
void Remove(ARC *aArc)
Remove an item from this branch.
Definition pns_node.cpp:939
ROUTER_IFACE * GetInterface() const
Definition pns_router.h:231
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: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