static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_LINE_CHAIN& aB, int aClearance, bool aNeedMTV, VECTOR2I& aMTV ) { bool found = false; for( int s = 0; s < aB.SegmentCount(); s++ ) { if( aA.Collide( aB.CSegment( s ), aClearance ) ) { found = true; break; } } if( !aNeedMTV || !found ) return found; SHAPE_CIRCLE cmoved( aA ); VECTOR2I f_total( 0, 0 ); for( int s = 0; s < aB.SegmentCount(); s++ ) { VECTOR2I f = pushoutForce( cmoved, aB.CSegment( s ), aClearance ); cmoved.SetCenter( cmoved.GetCenter() + f ); f_total += f; } aMTV = f_total; return found; }
SHAPE_LINE_CHAIN dragCornerInternal( const SHAPE_LINE_CHAIN& aOrigin, const VECTOR2I& aP ) { optional<SHAPE_LINE_CHAIN> picked; int i; int d = 2; if( aOrigin.CSegment( -1 ).Length() > 100000 * 30 ) // fixme: constant/parameter? d = 1; for( i = aOrigin.SegmentCount() - d; i >= 0; i-- ) { DIRECTION_45 d_start ( aOrigin.CSegment( i ) ); VECTOR2I p_start = aOrigin.CPoint( i ); SHAPE_LINE_CHAIN paths[2]; DIRECTION_45 dirs[2]; DIRECTION_45 d_prev = ( i > 0 ? DIRECTION_45( aOrigin.CSegment( i - 1 ) ) : DIRECTION_45() ); for( int j = 0; j < 2; j++ ) { paths[j] = d_start.BuildInitialTrace( p_start, aP, j ); dirs[j] = DIRECTION_45( paths[j].CSegment( 0 ) ); } for( int j = 0; j < 2; j++ ) { if( dirs[j] == d_start ) { picked = paths[j]; break; } } if( picked ) break; for( int j = 0; j < 2; j++ ) { if( dirs[j].IsObtuse( d_prev ) ) { picked = paths[j]; break; } } if( picked ) break; } if( picked ) { SHAPE_LINE_CHAIN path = aOrigin.Slice( 0, i ); path.Append( *picked ); return path; } return DIRECTION_45().BuildInitialTrace( aOrigin.CPoint( 0 ), aP, true ); }
static bool checkGap( const SHAPE_LINE_CHAIN &p, const SHAPE_LINE_CHAIN &n, int gap ) { int i, j; for( i = 0; i < p.SegmentCount(); i++ ) { for( j = 0; j < n.SegmentCount() ; j++ ) { int dist = p.CSegment( i ).Distance( n.CSegment( j ) ); if( dist < gap - 100 ) return false; } } return true; }
void ROUTER_PREVIEW_ITEM::drawLineChain( const SHAPE_LINE_CHAIN& aL, KIGFX::GAL* aGal ) const { for( int s = 0; s < aL.SegmentCount(); s++ ) aGal->DrawLine( aL.CSegment( s ).A, aL.CSegment( s ).B ); if( aL.IsClosed() ) aGal->DrawLine( aL.CSegment( -1 ).B, aL.CSegment( 0 ).A ); }
bool PNS_LINE_PLACER::buildInitialLine( const VECTOR2I& aP, PNS_LINE& aHead ) { SHAPE_LINE_CHAIN l; if( m_p_start == aP ) { l.Clear(); } else { if( Settings().GetFreeAngleMode() && Settings().Mode() == RM_MarkObstacles ) { l = SHAPE_LINE_CHAIN( m_p_start, aP ); } else { l = m_direction.BuildInitialTrace( m_p_start, aP ); } if( l.SegmentCount() > 1 && m_orthoMode ) { VECTOR2I newLast = l.CSegment( 0 ).LineProject( l.CPoint( -1 ) ); l.Remove( -1, -1 ); l.Point( 1 ) = newLast; } } aHead.SetShape( l ); if( !m_placingVia ) return true; PNS_VIA v( makeVia( aP ) ); v.SetNet( aHead.Net() ); if( m_currentMode == RM_MarkObstacles ) { aHead.AppendVia( v ); return true; } VECTOR2I force; VECTOR2I lead = aP - m_p_start; bool solidsOnly = ( m_currentMode != RM_Walkaround ); if( v.PushoutForce( m_currentNode, lead, force, solidsOnly, 40 ) ) { SHAPE_LINE_CHAIN line = m_direction.BuildInitialTrace( m_p_start, aP + force ); aHead = PNS_LINE( aHead, line ); v.SetPos( v.Pos() + force ); return true; } return false; // via placement unsuccessful }
static inline bool Collide( const SHAPE_LINE_CHAIN& aA, const SHAPE_LINE_CHAIN& aB, int aClearance, bool aNeedMTV, VECTOR2I& aMTV ) { for( int i = 0; i < aB.SegmentCount(); i++ ) if( aA.Collide( aB.CSegment( i ), aClearance ) ) return true; return false; }
static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_LINE_CHAIN& aB, int aClearance, bool aNeedMTV, VECTOR2I& aMTV ) { for( int s = 0; s < aB.SegmentCount(); s++ ) { if( aA.Collide( aB.CSegment( s ), aClearance ) ) return true; } return false; }
int SHAPE_LINE_CHAIN::Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& aIp ) const { BOX2I bb_other = aChain.BBox(); for( int s1 = 0; s1 < SegmentCount(); s1++ ) { const SEG& a = CSegment( s1 ); const BOX2I bb_cur( a.A, a.B - a.A ); if( !bb_other.Intersects( bb_cur ) ) continue; for( int s2 = 0; s2 < aChain.SegmentCount(); s2++ ) { const SEG& b = aChain.CSegment( s2 ); INTERSECTION is; if( a.Collinear( b ) ) { is.our = a; is.their = b; if( a.Contains( b.A ) ) { is.p = b.A; aIp.push_back( is ); } if( a.Contains( b.B ) ) { is.p = b.B; aIp.push_back( is ); } if( b.Contains( a.A ) ) { is.p = a.A; aIp.push_back( is ); } if( b.Contains( a.B ) ) { is.p = a.B; aIp.push_back( is ); } } else { OPT_VECTOR2I p = a.Intersect( b ); if( p ) { is.p = *p; is.our = a; is.their = b; aIp.push_back( is ); } } } } return aIp.size(); }
VECTOR2I PNS_LINE::snapToNeighbourSegments( const SHAPE_LINE_CHAIN& aPath, const VECTOR2I &aP, int aIndex, int aThreshold ) const { VECTOR2I snap_p[2]; DIRECTION_45 dragDir( aPath.CSegment( aIndex ) ); int snap_d[2] = { -1, -1 }; if( aThreshold == 0 ) return aP; if( aIndex >= 2 ) { SEG s = aPath.CSegment( aIndex - 2 ); if( DIRECTION_45( s ) == dragDir ) snap_d[0] = s.LineDistance( aP ); snap_p[0] = s.A; } if( aIndex < aPath.SegmentCount() - 2 ) { SEG s = aPath.CSegment( aIndex + 2 ); if( DIRECTION_45( s ) == dragDir ) snap_d[1] = s.LineDistance(aP); snap_p[1] = s.A; } VECTOR2I best = aP; int minDist = INT_MAX; for( int i = 0; i < 2; i++ ) { if( snap_d[i] >= 0 && snap_d[i] < minDist && snap_d[i] <= aThreshold ) { minDist = snap_d[i]; best = snap_p[i]; } } return best; }
VECTOR2I PNS_LINE::snapDraggedCorner( const SHAPE_LINE_CHAIN& aPath, const VECTOR2I& aP, int aIndex, int aThreshold ) const { int s_start = std::max( aIndex - 2, 0 ); int s_end = std::min( aIndex + 2, aPath.SegmentCount() - 1 ); int i, j; int best_dist = INT_MAX; VECTOR2I best_snap = aP; if( aThreshold <= 0 ) return aP; for( i = s_start; i <= s_end; i++ ) { const SEG& a = aPath.CSegment( i ); for( j = s_start; j < i; j++ ) { const SEG& b = aPath.CSegment( j ); if( !( DIRECTION_45( a ).IsObtuse(DIRECTION_45( b ) ) ) ) continue; OPT_VECTOR2I ip = a.IntersectLines(b); if( ip ) { int dist = ( *ip - aP ).EuclideanNorm(); if( dist < aThreshold && dist < best_dist ) { best_dist = dist; best_snap = *ip; } } } } return best_snap; }
PNS_WALKAROUND::WalkaroundStatus PNS_WALKAROUND::Route( const PNS_LINE& aInitialPath, PNS_LINE& aWalkPath, bool aOptimize ) { PNS_LINE path_cw( aInitialPath ), path_ccw( aInitialPath ); WalkaroundStatus s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS; SHAPE_LINE_CHAIN best_path; start( aInitialPath ); m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath ); m_recursiveBlockageCount = 0; aWalkPath = aInitialPath; while( m_iteration < m_iteration_limit ) { if( s_cw != STUCK ) s_cw = singleStep( path_cw, true ); if( s_ccw != STUCK ) s_ccw = singleStep( path_ccw, false ); if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) ) { int len_cw = path_cw.GetCLine().Length(); int len_ccw = path_ccw.GetCLine().Length(); if( m_forceLongerPath ) aWalkPath = (len_cw > len_ccw ? path_cw : path_ccw); else aWalkPath = (len_cw < len_ccw ? path_cw : path_ccw); break; } else if( s_cw == DONE && !m_forceLongerPath ) { aWalkPath = path_cw; break; } else if( s_ccw == DONE && !m_forceLongerPath ) { aWalkPath = path_ccw; break; } m_iteration++; } if( m_iteration == m_iteration_limit ) { int len_cw = path_cw.GetCLine().Length(); int len_ccw = path_ccw.GetCLine().Length(); if( m_forceLongerPath ) aWalkPath = (len_cw > len_ccw ? path_cw : path_ccw); else aWalkPath = (len_cw < len_ccw ? path_cw : path_ccw); } if( m_cursorApproachMode ) { // int len_cw = path_cw.GetCLine().Length(); // int len_ccw = path_ccw.GetCLine().Length(); bool found = false; SHAPE_LINE_CHAIN l = aWalkPath.GetCLine(); for( int i = 0; i < l.SegmentCount(); i++ ) { const SEG s = l.Segment( i ); VECTOR2I nearest = s.NearestPoint( m_cursorPos ); VECTOR2I::extended_type dist_a = ( s.A - m_cursorPos ).SquaredEuclideanNorm(); VECTOR2I::extended_type dist_b = ( s.B - m_cursorPos ).SquaredEuclideanNorm(); VECTOR2I::extended_type dist_n = ( nearest - m_cursorPos ).SquaredEuclideanNorm(); if( dist_n <= dist_a && dist_n < dist_b ) { // PNSDisplayDebugLine( l, 3 ); l.Remove( i + 1, -1 ); l.Append( nearest ); l.Simplify(); found = true; break; } } if( found ) { aWalkPath = aInitialPath; aWalkPath.SetShape( l ); } } aWalkPath.SetWorld( m_world ); aWalkPath.GetLine().Simplify(); WalkaroundStatus st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK; if( aOptimize && st == DONE ) PNS_OPTIMIZER::Optimize( &aWalkPath, PNS_OPTIMIZER::MERGE_OBTUSE, m_world ); return st; }
WALKAROUND::WALKAROUND_STATUS WALKAROUND::Route( const LINE& aInitialPath, LINE& aWalkPath, bool aOptimize ) { LINE path_cw( aInitialPath ), path_ccw( aInitialPath ); WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS; SHAPE_LINE_CHAIN best_path; // special case for via-in-the-middle-of-track placement if( aInitialPath.PointCount() <= 1 ) { if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) ) return STUCK; aWalkPath = aInitialPath; return DONE; } start( aInitialPath ); m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath ); m_recursiveBlockageCount = 0; aWalkPath = aInitialPath; if( m_forceWinding ) { s_cw = m_forceCw ? IN_PROGRESS : STUCK; s_ccw = m_forceCw ? STUCK : IN_PROGRESS; m_forceSingleDirection = true; } else { m_forceSingleDirection = false; } while( m_iteration < m_iterationLimit ) { if( s_cw != STUCK ) s_cw = singleStep( path_cw, true ); if( s_ccw != STUCK ) s_ccw = singleStep( path_ccw, false ); if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) ) { int len_cw = path_cw.CLine().Length(); int len_ccw = path_ccw.CLine().Length(); if( m_forceLongerPath ) aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw ); else aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw ); break; } else if( s_cw == DONE && !m_forceLongerPath ) { aWalkPath = path_cw; break; } else if( s_ccw == DONE && !m_forceLongerPath ) { aWalkPath = path_ccw; break; } m_iteration++; } if( m_iteration == m_iterationLimit ) { int len_cw = path_cw.CLine().Length(); int len_ccw = path_ccw.CLine().Length(); if( m_forceLongerPath ) aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw ); else aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw ); } if( m_cursorApproachMode ) { // int len_cw = path_cw.GetCLine().Length(); // int len_ccw = path_ccw.GetCLine().Length(); bool found = false; SHAPE_LINE_CHAIN l = aWalkPath.CLine(); for( int i = 0; i < l.SegmentCount(); i++ ) { const SEG s = l.Segment( i ); VECTOR2I nearest = s.NearestPoint( m_cursorPos ); VECTOR2I::extended_type dist_a = ( s.A - m_cursorPos ).SquaredEuclideanNorm(); VECTOR2I::extended_type dist_b = ( s.B - m_cursorPos ).SquaredEuclideanNorm(); VECTOR2I::extended_type dist_n = ( nearest - m_cursorPos ).SquaredEuclideanNorm(); if( dist_n <= dist_a && dist_n < dist_b ) { l.Remove( i + 1, -1 ); l.Append( nearest ); l.Simplify(); found = true; break; } } if( found ) { aWalkPath = aInitialPath; aWalkPath.SetShape( l ); } } aWalkPath.Line().Simplify(); if( aWalkPath.SegmentCount() < 1 ) return STUCK; if( aWalkPath.CPoint( -1 ) != aInitialPath.CPoint( -1 ) ) return STUCK; if( aWalkPath.CPoint( 0 ) != aInitialPath.CPoint( 0 ) ) return STUCK; WALKAROUND_STATUS st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK; if( st == DONE ) { if( aOptimize ) OPTIMIZER::Optimize( &aWalkPath, OPTIMIZER::MERGE_OBTUSE, m_world ); } return st; }
bool PNS_DIFF_PAIR_PLACER::FixRoute( const VECTOR2I& aP, PNS_ITEM* aEndItem ) { if( !m_fitOk ) return false; if( m_currentTrace.CP().SegmentCount() < 1 || m_currentTrace.CN().SegmentCount() < 1 ) return false; if( m_currentTrace.CP().SegmentCount() > 1 ) m_initialDiagonal = !DIRECTION_45( m_currentTrace.CP().CSegment( -2 ) ).IsDiagonal(); PNS_TOPOLOGY topo( m_lastNode ); if( !m_snapOnTarget && !m_currentTrace.EndsWithVias() ) { SHAPE_LINE_CHAIN newP ( m_currentTrace.CP() ); SHAPE_LINE_CHAIN newN ( m_currentTrace.CN() ); if( newP.SegmentCount() > 1 && newN.SegmentCount() > 1 ) { newP.Remove( -1, -1 ); newN.Remove( -1, -1 ); } m_currentTrace.SetShape( newP, newN ); } if( m_currentTrace.EndsWithVias() ) { m_lastNode->Add( m_currentTrace.PLine().Via().Clone() ); m_lastNode->Add( m_currentTrace.NLine().Via().Clone() ); m_chainedPlacement = false; } else m_chainedPlacement = !m_snapOnTarget; PNS_LINE lineP( m_currentTrace.PLine() ); PNS_LINE lineN( m_currentTrace.NLine() ); m_lastNode->Add( &lineP ); m_lastNode->Add( &lineN ); topo.SimplifyLine( &lineP ); topo.SimplifyLine( &lineN ); m_prevPair = m_currentTrace.EndingPrimitives(); Router()->CommitRouting( m_lastNode ); m_lastNode = NULL; m_placingVia = false; if( m_snapOnTarget ) { m_idle = true; return true; } else { initPlacement(); return false; } }