Fix router cursor glitching on H/V track segments.

Also don't disable grid snapping when holding Shift over tracks.
This commit is contained in:
Alex 2022-11-20 03:51:27 +05:00
parent 9e5d96b9d3
commit 110bc6abd1
1 changed files with 38 additions and 27 deletions

View File

@ -78,42 +78,53 @@ PCB_GRID_HELPER::PCB_GRID_HELPER( TOOL_MANAGER* aToolMgr, MAGNETIC_SETTINGS* aMa
VECTOR2I PCB_GRID_HELPER::AlignToSegment( const VECTOR2I& aPoint, const SEG& aSeg ) VECTOR2I PCB_GRID_HELPER::AlignToSegment( const VECTOR2I& aPoint, const SEG& aSeg )
{ {
OPT_VECTOR2I pts[6];
const int c_gridSnapEpsilon = 2; const int c_gridSnapEpsilon = 2;
VECTOR2I aligned = Align( aPoint );
if( !m_enableSnap ) if( !m_enableSnap )
return aPoint; return aligned;
VECTOR2I nearest = Align( aPoint ); std::vector<VECTOR2I> points;
SEG pos_slope( nearest + VECTOR2I( -1, 1 ), nearest + VECTOR2I( 1, -1 ) ); const SEG testSegments[] = { SEG( aligned, aligned + VECTOR2( 1, 0 ) ),
SEG neg_slope( nearest + VECTOR2I( -1, -1 ), nearest + VECTOR2I( 1, 1 ) ); SEG( aligned, aligned + VECTOR2( 0, 1 ) ),
int max_i = 2; SEG( aligned, aligned + VECTOR2( 1, 1 ) ),
SEG( aligned, aligned + VECTOR2( 1, -1 ) ) };
pts[0] = aSeg.A; for( const SEG& seg : testSegments )
pts[1] = aSeg.B; {
OPT_VECTOR2I vec = aSeg.IntersectLines( seg );
if( !aSeg.ApproxParallel( pos_slope ) ) if( vec && aSeg.Distance( *vec ) <= c_gridSnapEpsilon )
pts[max_i++] = aSeg.IntersectLines( pos_slope ); points.push_back( *vec );
}
if( !aSeg.ApproxParallel( neg_slope ) )
pts[max_i++] = aSeg.IntersectLines( neg_slope );
VECTOR2I nearest = aligned;
int min_d = std::numeric_limits<int>::max(); int min_d = std::numeric_limits<int>::max();
for( int i = 0; i < max_i; i++ ) // Snap by distance between pointer and endpoints
for( const VECTOR2I& pt : { aSeg.A, aSeg.B } )
{ {
if( pts[i] && aSeg.Distance( *pts[i] ) <= c_gridSnapEpsilon ) int d = ( pt - aPoint ).EuclideanNorm();
{
int d = (*pts[i] - aPoint).EuclideanNorm();
if( d < min_d ) if( d < min_d )
{ {
min_d = d; min_d = d;
nearest = *pts[i]; nearest = pt;
} }
} }
// Snap by distance between aligned cursor and intersections
for( const VECTOR2I& pt : points )
{
int d = ( pt - aligned ).EuclideanNorm();
if( d < min_d )
{
min_d = d;
nearest = pt;
}
} }
return nearest; return nearest;