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 struct WALK_STATE
480 {
481 NODE *node;
482 int totalLength = 0;
483 std::vector<LINE> postWalkLines;
484 bool fail = false;
485 };
486
487 WALK_STATE walkState[2];
488
489 for( int attempt = 0; attempt < 2; attempt++ )
490 {
491 WALK_STATE *state = &walkState[ attempt ];
492 state->node = preWalkNode->Branch();
493 state->postWalkLines.resize( aCompletedLines.size() );
494
495 for( int lidx = 0; lidx < (int) aCompletedLines.size(); lidx++ )
496 {
497 MDRAG_LINE& l = aCompletedLines[attempt ? aCompletedLines.size() - 1 - lidx : lidx];
498 LINE walk( l.draggedLine );
499
500 auto result = tryWalkaround( state->node, l.draggedLine, walk );
501
502 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) );
503 PNS_DBG( Dbg(), AddItem, &walk, BLUE, 100000, wxString::Format("walk lidx=%d attempt=%d", lidx, attempt) );
504
505 if( result )
506 {
507 state->node->Add( walk );
508 state->totalLength += walk.CLine().Length() - l.draggedLine.CLine().Length();
509 state->postWalkLines[lidx] = walk;
510 }
511 else
512 {
513 state->fail = true;
514 break;
515 }
516 }
517 }
518
519 std::optional<int> bestAttempt;
520
521 if( !walkState[0].fail && !walkState[1].fail )
522 {
523 if ( walkState[0].totalLength < walkState[1].totalLength )
524 {
525 bestAttempt = 0;
526 }
527 else
528 {
529 bestAttempt = 1;
530 }
531 }
532 else if ( !walkState[0].fail )
533 {
534 bestAttempt = 0;
535 }
536 else if ( !walkState[1].fail )
537 {
538 bestAttempt = 1;
539 }
540
541 if( !bestAttempt )
542 {
543 delete walkState[0].node;
544 delete walkState[1].node;
545 return false;
546 }
547 else
548 {
549 for( int lidx = 0; lidx < (int) aCompletedLines.size(); lidx++ )
550 {
551 aCompletedLines[lidx].draggedLine = walkState[ *bestAttempt ].postWalkLines[ lidx ];
552 }
553
554 m_lastNode = walkState[ *bestAttempt ].node;
555 delete walkState[1 - *bestAttempt].node;
556 }
557
558 // trip asan for qa
559 /*for( auto& l : aCompletedLines )
560 for( auto lnk : l.draggedLine.Links() )
561 assert( lnk->Parent() != reinterpret_cast<BOARD_ITEM*>( 0xdeadbeef ) );*/
562
563 restoreLeaderSegments( aCompletedLines );
564
565 return true;
566}
567
568
569bool MULTI_DRAGGER::multidragMarkObstacles( std::vector<MDRAG_LINE>& aCompletedLines )
570{
571
572// fixme: rewrite using shared_ptr...
573 if( m_lastNode )
574 {
575 delete m_lastNode;
576 m_lastNode = nullptr;
577 }
578
579 // m_lastNode contains the temporary (post-modification) state. Think of it as
580 // of an efficient undo buffer. We don't change the PCB directly, but a branch of it
581 // created below. We can then commit its state (applying the modifications to the host board
582 // by calling ROUTING::CommitRouting(m_lastNode) or simply discard it.
583 m_lastNode = m_world->Branch();
584
585
586 for( int l1 = 0; l1 < (int)aCompletedLines.size(); l1++ )
587 {
588 for( int l2 = l1 + 1; l2 < (int)aCompletedLines.size(); l2++ )
589 {
590 const auto& l1l = aCompletedLines[l1].draggedLine;
591 auto l2l = aCompletedLines[l2].draggedLine;
592
593 if( clipToOtherLine( m_lastNode, l1l, l2l ) )
594 aCompletedLines[l2].draggedLine = l2l;
595 }
596 }
597
598 for ( auto&l : aCompletedLines )
599 {
600 m_lastNode->Remove( l.originalLine );
601 m_lastNode->Add( l.draggedLine );
602 }
603
604 restoreLeaderSegments( aCompletedLines );
605
606 return true;
607}
608
609bool MULTI_DRAGGER::multidragShove( std::vector<MDRAG_LINE>& aCompletedLines )
610{
611 if( m_lastNode )
612 {
613 delete m_lastNode;
614 m_lastNode = nullptr;
615 }
616
617 if( !m_shove )
618 return false;
619
620 auto compareDragStartDist = []( const MDRAG_LINE& a, const MDRAG_LINE& b ) -> int
621 {
622 return a.dragDist < b.dragDist;
623 };
624
625 std::sort( aCompletedLines.begin(), aCompletedLines.end(), compareDragStartDist );
626
627 auto iface = Router()->GetInterface();
628
629 for( auto& l : m_mdragLines )
630 {
631 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"),
632 iface->GetNetName( l.draggedLine.Net() ),
633 (int) l.isCorner?1:0,
634 (int) l.isStrict?1:0,
635 (int) l.cornerDistance,
636 (int) l.leaderSegDistance,
637 (int) l.leaderSegIndex,
638 (int) l.cornerIsLast?1:0,
639 (int) l.dragDist ) );
640 }
641
642
643 m_shove->SetDefaultShovePolicy( SHOVE::SHP_SHOVE );
644 m_shove->ClearHeads();
645
646 for( auto& l : aCompletedLines )
647 {
648 PNS_DBG( Dbg(), AddItem, &l.draggedLine, GREEN, 0, "dragged-line" );
649 m_shove->AddHeads( l.draggedLine, SHOVE::SHP_SHOVE | SHOVE::SHP_DONT_OPTIMIZE );
650 }
651
652 auto status = m_shove->Run();
653
654 m_lastNode = m_shove->CurrentNode()->Branch();
655
656 // Re-add any m_mdragLines that were removed from m_preShoveNode during Start() but
657 // are not part of aCompletedLines. Without this, lines that fail the drag angle check
658 // would be silently deleted from the board.
659 std::set<int> completedIndices;
660
661 for( const auto& cl : aCompletedLines )
662 completedIndices.insert( cl.mdragIndex );
663
664 for( const auto& ml : m_mdragLines )
665 {
666 if( completedIndices.find( ml.mdragIndex ) == completedIndices.end() )
667 {
668 LINE preserved( ml.originalLine );
669 preserved.ClearLinks();
670 m_lastNode->Add( preserved );
671 }
672 }
673
674 if( status == SHOVE::SH_OK )
675 {
676 for( int i = 0; i < (int) aCompletedLines.size(); i++ )
677 {
678 MDRAG_LINE&l = aCompletedLines[i];
679
680 if( m_shove->HeadsModified( i ) )
681 l.draggedLine = m_shove->GetModifiedHead( i );
682
683 // this should not be linked (assert in rt-test)
685
686 m_lastNode->Add( l.draggedLine );
687 }
688 }
689 else
690 {
691 return false;
692 }
693
694 restoreLeaderSegments( aCompletedLines );
695
696 return true;
697}
698
699// this is called every time the user moves the mouse while dragging a set of multiple tracks
701{
702 std::optional<LINE> primaryPreDrag, primaryDragged;
703
704
705
706 SEG lastPreDrag;
707 DIRECTION_45 primaryDir;
708 VECTOR2I perp;
709
710 DIRECTION_45 primaryLastSegDir;
711 std::vector<MDRAG_LINE> completed;
712
713 auto tryPosture = [&] ( int aVariant ) -> bool
714 {
715 MDRAG_LINE* primaryLine = nullptr;
716
717 for( auto &l : m_mdragLines )
718 {
719 l.dragOK = false;
720 l.preDragLine = l.originalLine;
721 //PNS_DBG( Dbg(), AddItem, &l.originalLine, GREEN, 300000, "par" );
722 if( l.isPrimaryLine )
723 {
724
725 //PNS_DBG( Dbg(), AddItem, &l.originalLine, BLUE, 300000, wxT("mdrag-prim"));
726
727 // create a copy of the primary line (pre-drag and post-drag).
728 // the pre-drag version is necessary for NODE::Remove() to be able to
729 // find out the segments before modification by the multidrag algorithm
730 primaryDragged = l.originalLine;
731 primaryDragged->ClearLinks();
732 primaryPreDrag = l.originalLine;
733 primaryLine = &l;
734
735 }
736 }
737
738 if( aVariant == 1 && (primaryPreDrag->PointCount() > 2) )
739 {
740 primaryPreDrag->Line().Remove( -1 );
741 primaryDragged->Line().Remove( -1 );
742
743 for( auto&l : m_mdragLines )
744 {
745 l.preDragLine.Line().Remove(-1);
746 }
747 }
748
749 completed.clear();
750
751 int snapThreshold = Settings().SmoothDraggedSegments() ? primaryDragged->Width() / 4 : 0;
752
753 if( m_dragMode == DM_CORNER )
754 {
755 // first, drag only the primary line
756 PNS_DBG( Dbg(), AddPoint, primaryDragged->CLastPoint(), YELLOW, 600000, wxT("mdrag-sec"));
757
758 lastPreDrag = primaryPreDrag->CSegment( -1 );
759 primaryDir = DIRECTION_45( lastPreDrag );
760
761 primaryDragged->SetSnapThreshhold( snapThreshold );
762 primaryDragged->DragCorner( aP, primaryDragged->PointCount() - 1, false );
763
764
765 if( primaryDragged->SegmentCount() > 0 )
766 {
767 SEG lastPrimDrag = primaryDragged->CSegment( -1 );
768
769 if ( aVariant == 2 )
770 lastPrimDrag = lastPreDrag;
771
772 auto lastSeg = primaryDragged->CSegment( -1 );
773 if( DIRECTION_45( lastSeg ) != primaryDir )
774 {
775 if( lastSeg.Length() < primaryDragged->Width() )
776 {
777 lastPrimDrag = lastPreDrag;
778 }
779 }
780
781 perp = (lastPrimDrag.B - lastPrimDrag.A).Perpendicular();
782 primaryLastSegDir = DIRECTION_45( lastPrimDrag );
783
784
785 PNS_DBG( Dbg(), AddItem, &(*primaryDragged), LIGHTGRAY, 100000, "prim" );
786 PNS_DBG( Dbg(), AddShape, SEG(lastPrimDrag.B, lastPrimDrag.B + perp), LIGHTGRAY, 100000, wxString::Format("prim-perp-seg") );
787 } else {
788 return false;
789 }
790
791
792
793// PNS_DBG( Dbg(), AddShape, &ll, LIGHTBLUE, 200000, "par" );
794
795 }
796 else
797 {
798
799 SHAPE_LINE_CHAIN ll2( { lastPreDrag.A, lastPreDrag.B } );
800 PNS_DBG( Dbg(), AddShape, &ll2, LIGHTYELLOW, 300000, "par" );
801 lastPreDrag = primaryDragged->CSegment( primaryLine->leaderSegIndex );
802 primaryDragged->SetSnapThreshhold( snapThreshold );
803 primaryDragged->DragSegment( aP, primaryLine->leaderSegIndex );
804 perp = (primaryLine->midSeg.B - primaryLine->midSeg.A).Perpendicular();
805 m_guide = SEG( aP, aP + perp );
806 }
807
808
810 m_draggedItems.Clear();
811
812 // now drag all other lines
813 for( auto& l : m_mdragLines )
814 {
815 //PNS_DBG( Dbg(), AddPoint, l.originalLine.CPoint( l.cornerIndex ), WHITE, 1000000, wxT("l-end"));
816 if( l.isDraggable )
817 {
818 l.dragOK = false;
819 //PNS_DBG( Dbg(), AddItem, &l.originalLine, GREEN, 100000, wxT("mdrag-sec"));
820
821 // reject nulls
822 if( l.preDragLine.SegmentCount() >= 1 )
823 {
824
825 //PNS_DBG( Dbg(), AddPoint, l.preDragLine.CPoint( l.cornerIndex ), YELLOW, 600000, wxT("mdrag-sec"));
826
827 // check the direction of the last segment of the line against the direction of
828 // the last segment of the primary line (both before dragging) and perform drag
829 // only when the directions are the same. The algorithm here is quite trival and
830 // otherwise would produce really awkward results. There's of course a TON of
831 // room for improvement here :-)
832
833 if( m_dragMode == DM_CORNER )
834 {
835 DIRECTION_45 parallelDir( l.preDragLine.CSegment( -1 ) );
836
837 auto leadAngle = primaryDir.Angle( parallelDir );
838
839 if( leadAngle == DIRECTION_45::ANG_OBTUSE
840 || leadAngle == DIRECTION_45::ANG_RIGHT
841 || leadAngle == DIRECTION_45::ANG_STRAIGHT )
842 {
843 // compute the distance between the primary line and the last point of
844 // the currently processed line
845 int dist = lastPreDrag.LineDistance( l.preDragLine.CLastPoint(), true );
846
847 // now project it on the perpendicular line we computed before
848 auto projected = aP + perp.Resize( dist );
849
850
851 LINE parallelDragged( l.preDragLine );
852
853 PNS_DBG( Dbg(), AddPoint, projected, LIGHTGRAY, 100000, "dragged-c" );
854 PNS_DBG( Dbg(), AddPoint, parallelDragged.CLastPoint(), LIGHTGRAY, 100000, wxString::Format("orig-c cil %d", l.cornerIsLast?1:0) );
855
856 parallelDragged.ClearLinks();
857 //m_lastNode->Remove( parallelDragged );
858 // drag the non-primary line's end trying to place it at the projected point
859 parallelDragged.DragCorner( projected, parallelDragged.PointCount() - 1,
860 false, primaryLastSegDir );
861
862 PNS_DBG( Dbg(), AddPoint, projected, LIGHTYELLOW, 600000,
863 wxT( "l-end" ) );
864
865 l.dragOK = true;
866
867 if( !l.isPrimaryLine )
868 {
869 l.draggedLine = parallelDragged;
870 completed.push_back( l );
871 m_draggedItems.Add( parallelDragged );
872 }
873 }
874 }
875 else if ( m_dragMode == DM_SEGMENT )
876 {
877 SEG sdrag = l.midSeg;
878 DIRECTION_45 refDir( lastPreDrag );
879 DIRECTION_45 curDir( sdrag );
880 auto ang = refDir.Angle( curDir );
881
883 {
884 int dist = lastPreDrag.LineDistance(
885 l.preDragLine.CPoint( l.leaderSegIndex ), true );
886 auto projected = aP + perp.Resize( dist );
887
888 SEG sperp( aP, aP + perp.Resize( 10000000 ) );
889 VECTOR2I startProj = sperp.LineProject( m_dragStartPoint );
890
891 SHAPE_LINE_CHAIN ll( { sperp.A, sperp.B } );
892
893
894 PNS_DBG( Dbg(), AddShape, &ll, LIGHTBLUE, 100000, "par" );
895 SHAPE_LINE_CHAIN ll2( { sdrag.A, sdrag.B } );
896 PNS_DBG( Dbg(), AddShape, &ll2, LIGHTBLUE, 100000, "sdrag" );
897 VECTOR2I v = projected - startProj;
898 l.dragDist = v.EuclideanNorm() * sign( v.Dot( perp ) );
899 l.dragOK = true;
900
901 if( !l.isPrimaryLine )
902 {
903 l.draggedLine = l.preDragLine;
904 l.draggedLine.ClearLinks();
905 l.draggedLine.SetSnapThreshhold( snapThreshold );
906 l.draggedLine.DragSegment( projected, l.leaderSegIndex, false );
907 completed.push_back( l );
908 PNS_DBG( Dbg(), AddItem, &l.draggedLine, LIGHTBLUE, 100000,
909 "dragged" );
910 }
911
912
913 PNS_DBG( Dbg(), AddPoint, startProj, LIGHTBLUE, 400000,
914 wxT( "startProj" ) );
915 PNS_DBG( Dbg(), AddPoint, projected, LIGHTRED, 400000,
916 wxString::Format( "pro dd=%d", l.dragDist ) );
917 }
918 }
919 }
920 }
921
922 if (l.isPrimaryLine)
923 {
924 l.draggedLine = *primaryDragged;
925 l.dragOK = true;
926 completed.push_back( l );
927 }
928 }
929
930 if( m_dragMode == DM_SEGMENT )
931 return true;
932 else
933 {
934 for ( const auto &l: completed )
935 {
936 if( !l.dragOK && aVariant < 2 )
937 return false;
938
939 if( l.isPrimaryLine )
940 continue;
941
942 DIRECTION_45 lastDir ( l.draggedLine.CSegment(-1) );
943
944 if( lastDir != primaryLastSegDir )
945 return false;
946 }
947 }
948
949 return true;
950 };
951
952 bool res = false;
953
954 for( int variant = 0; variant < 3; variant++ )
955 {
956 res = tryPosture( variant );
957
958 if( res )
959 break;
960 }
961
962 switch( Settings().Mode() )
963 {
964 case RM_Walkaround:
965 m_dragStatus = multidragWalkaround ( completed );
966 break;
967
968 case RM_Shove:
969 m_dragStatus = multidragShove ( completed );
970 break;
971
972 case RM_MarkObstacles:
974 break;
975
976
977
978 default:
979 break;
980 }
981
982 return m_dragStatus;
983}
984
985
990
991
993{
994 return m_draggedItems;
995}
996
997
999{
1000 // fixme: should we care?
1001 return 0;
1002}
1003
1004
1005} // 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
void Remove(ARC *aArc)
Remove an item from this branch.
Definition pns_node.cpp:991
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:546
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:60
constexpr int sign(T val)
Definition util.h:145
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687