KiCad PCB EDA Suite
Loading...
Searching...
No Matches
seg.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2013 CERN
5 * @author Tomasz Wlostowski <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <algorithm> // for min
27#include <geometry/seg.h>
28#include <math/util.h> // for rescale
29#include <math/vector2d.h> // for VECTOR2I, VECTOR2
30#include <trigo.h> // for RAD2DEG
31
32template <typename T>
33int sgn( T aVal )
34{
35 return ( T( 0 ) < aVal ) - ( aVal < T( 0 ) );
36}
37
38template <typename T>
39constexpr T sqrt_helper(T x, T lo, T hi)
40{
41 if (lo == hi)
42 return lo;
43
44 const T mid = (lo + hi + 1) / 2;
45 if (x / mid < mid)
46 return sqrt_helper<T>(x, lo, mid - 1);
47 else
48 return sqrt_helper(x, mid, hi);
49}
50
51template <typename T>
52constexpr T ct_sqrt(T x)
53{
54 return sqrt_helper<T>(x, 0, x / 2 + 1);
55}
56
57template <typename T>
58static constexpr T sqrt_max_typed = ct_sqrt( std::numeric_limits<T>::max() );
59
60template <typename T>
62{
63 T sqrt_max = sqrt_max_typed<T>;
64
65 if( x < 0 )
66 return sqrt_max;
67
68 T r = (T) std::sqrt( (double) x );
69
70 while( r < sqrt_max && r * r < x )
71 r++;
72
73 while( r > sqrt_max || r * r > x )
74 r--;
75
76 return r;
77}
78
79
81{
82 // Handle zero-length segments (points) specially.
83 // The Intersects() check below doesn't handle this case correctly because
84 // the cross product with a zero vector is always zero, causing false positives.
85 if( A == B )
86 return aSeg.SquaredDistance( A );
87
88 if( aSeg.A == aSeg.B )
89 return SquaredDistance( aSeg.A );
90
91 if( Intersects( aSeg ) )
92 return 0;
93
94 const VECTOR2I pts[4] =
95 {
96 aSeg.NearestPoint( A ) - A,
97 aSeg.NearestPoint( B ) - B,
98 NearestPoint( aSeg.A ) - aSeg.A,
99 NearestPoint( aSeg.B ) - aSeg.B
100 };
101
103
104 for( int i = 0; i < 4; i++ )
105 m = std::min( m, pts[i].SquaredEuclideanNorm() );
106
107 return m;
108}
109
110
111EDA_ANGLE SEG::Angle( const SEG& aOther ) const
112{
113 EDA_ANGLE thisAngle = EDA_ANGLE( A - B ).Normalize180();
114 EDA_ANGLE otherAngle = EDA_ANGLE( aOther.A - aOther.B ).Normalize180();
115
116 return std::abs( ( thisAngle - otherAngle ).Normalize180() );
117}
118
119
120const VECTOR2I SEG::NearestPoint( const SEG& aSeg ) const
121{
122 if( OPT_VECTOR2I p = Intersect( aSeg ) )
123 return *p;
124
125 const VECTOR2I pts_origin[4] =
126 {
127 aSeg.NearestPoint( A ),
128 aSeg.NearestPoint( B ),
129 NearestPoint( aSeg.A ),
130 NearestPoint( aSeg.B )
131 };
132
133
134 const VECTOR2I* pts_out[4] =
135 {
136 &A,
137 &B,
138 &pts_origin[2],
139 &pts_origin[3]
140 };
141
142 const ecoord pts_dist[4] =
143 {
144 ( pts_origin[0] - A ).SquaredEuclideanNorm(),
145 ( pts_origin[1] - B ).SquaredEuclideanNorm(),
146 ( pts_origin[2] - aSeg.A ).SquaredEuclideanNorm(),
147 ( pts_origin[3] - aSeg.B ).SquaredEuclideanNorm()
148 };
149
150 int min_i = 0;
151
152 for( int i = 0; i < 4; i++ )
153 {
154 if( pts_dist[i] < pts_dist[min_i] )
155 min_i = i;
156 }
157
158 return *pts_out[min_i];
159}
160
161
162bool SEG::NearestPoints( const SEG& aSeg, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const
163{
164 if( OPT_VECTOR2I p = Intersect( aSeg ) )
165 {
166 aPtA = aPtB = *p;
167 aDistSq = 0;
168
169 return true;
170 }
171
172 const VECTOR2I pts_origin[4] =
173 {
174 aSeg.NearestPoint( A ),
175 aSeg.NearestPoint( B ),
176 NearestPoint( aSeg.A ),
177 NearestPoint( aSeg.B )
178 };
179
180 const VECTOR2I* pts_a_out[4] =
181 {
182 &A,
183 &B,
184 &pts_origin[2],
185 &pts_origin[3]
186 };
187
188 const VECTOR2I* pts_b_out[4] =
189 {
190 &pts_origin[0],
191 &pts_origin[1],
192 &aSeg.A,
193 &aSeg.B
194 };
195
196 const ecoord pts_dist[4] =
197 {
198 ( pts_origin[0] - A ).SquaredEuclideanNorm(),
199 ( pts_origin[1] - B ).SquaredEuclideanNorm(),
200 ( pts_origin[2] - aSeg.A ).SquaredEuclideanNorm(),
201 ( pts_origin[3] - aSeg.B ).SquaredEuclideanNorm()
202 };
203
204 int min_i = 0;
205
206 for( int i = 0; i < 4; i++ )
207 {
208 if( pts_dist[i] < pts_dist[min_i] )
209 min_i = i;
210 }
211
212 aPtA = *pts_a_out[min_i];
213 aPtB = *pts_b_out[min_i];
214 aDistSq = pts_dist[min_i];
215
216 return true;
217}
218
219
220bool SEG::checkCollinearOverlap( const SEG& aSeg, bool useXAxis, bool aIgnoreEndpoints, VECTOR2I* aPt ) const
221{
222 // Extract coordinates based on the chosen axis
223 int seg1_start, seg1_end, seg2_start, seg2_end;
224 int coord1_start, coord1_end; // For calculating other axis coordinate
225
226 if( useXAxis )
227 {
228 seg1_start = A.x; seg1_end = B.x;
229 seg2_start = aSeg.A.x; seg2_end = aSeg.B.x;
230 coord1_start = A.y; coord1_end = B.y;
231 }
232 else
233 {
234 seg1_start = A.y; seg1_end = B.y;
235 seg2_start = aSeg.A.y; seg2_end = aSeg.B.y;
236 coord1_start = A.x; coord1_end = B.x;
237 }
238
239 // Find segment ranges on the projection axis
240 const int seg1_min = std::min( seg1_start, seg1_end );
241 const int seg1_max = std::max( seg1_start, seg1_end );
242 const int seg2_min = std::min( seg2_start, seg2_end );
243 const int seg2_max = std::max( seg2_start, seg2_end );
244
245 // Check for overlap
246 const bool overlaps = seg1_max >= seg2_min && seg2_max >= seg1_min;
247 if( !overlaps )
248 return false;
249
250 // Check if intersection is only at endpoints when aIgnoreEndpoints is true
251 if( aIgnoreEndpoints )
252 {
253 // Calculate overlap region
254 const int overlap_start = std::max( seg1_min, seg2_min );
255 const int overlap_end = std::min( seg1_max, seg2_max );
256
257 // If overlap region has zero length, segments only touch at endpoint
258 if( overlap_start == overlap_end )
259 {
260 // Check if this endpoint touching involves actual segment endpoints
261 // (not just projected endpoints due to min/max calculation)
262 bool isEndpointTouch = false;
263
264 // Check if the touch point corresponds to actual segment endpoints
265 if( overlap_start == seg1_min || overlap_start == seg1_max )
266 {
267 // Touch point is at seg1's endpoint, check if it's also at seg2's endpoint
268 if( overlap_start == seg2_min || overlap_start == seg2_max )
269 {
270 isEndpointTouch = true;
271 }
272 }
273
274 if( isEndpointTouch )
275 return false; // Ignore endpoint-only intersection
276 }
277 }
278
279 // Calculate intersection point if requested
280 if( aPt )
281 {
282 // Find midpoint of overlap region
283 const int overlap_start = std::max( seg1_min, seg2_min );
284 const int overlap_end = std::min( seg1_max, seg2_max );
285 const int intersection_proj = ( overlap_start + overlap_end ) / 2;
286
287 // Calculate corresponding coordinate on the other axis
288 int intersection_other;
289 if( seg1_end != seg1_start )
290 {
291 // Use this segment's line equation to find other coordinate
292 intersection_other = coord1_start + static_cast<int>(
293 rescale( intersection_proj - seg1_start, coord1_end - coord1_start, seg1_end - seg1_start ) );
294 }
295 else
296 {
297 // Degenerate segment (point) or perpendicular to projection axis
298 intersection_other = coord1_start;
299 }
300
301 // Set result based on projection axis
302 if( useXAxis )
303 *aPt = VECTOR2I( intersection_proj, intersection_other );
304 else
305 *aPt = VECTOR2I( intersection_other, intersection_proj );
306 }
307
308 return true;
309}
310
311
312bool SEG::intersects( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines, VECTOR2I* aPt ) const
313{
314 // Quick rejection: check if segment bounding boxes overlap
315 // (Skip for line mode since infinite lines can intersect anywhere)
316 if( !aLines )
317 {
318 const int this_min_x = std::min( A.x, B.x );
319 const int this_max_x = std::max( A.x, B.x );
320 const int this_min_y = std::min( A.y, B.y );
321 const int this_max_y = std::max( A.y, B.y );
322
323 const int other_min_x = std::min( aSeg.A.x, aSeg.B.x );
324 const int other_max_x = std::max( aSeg.A.x, aSeg.B.x );
325 const int other_min_y = std::min( aSeg.A.y, aSeg.B.y );
326 const int other_max_y = std::max( aSeg.A.y, aSeg.B.y );
327
328 if( this_max_x < other_min_x || other_max_x < this_min_x ||
329 this_max_y < other_min_y || other_max_y < this_min_y )
330 {
331 return false;
332 }
333 }
334
335 // Calculate direction vectors and offset vector using VECTOR2 operations
336 // Using parametric form: P₁ = A + t*dir1, P₂ = aSeg.A + s*dir2
337 const VECTOR2L dir1 = VECTOR2L( B ) - A; // direction vector e
338 const VECTOR2L dir2 = VECTOR2L( aSeg.B ) - aSeg.A; // direction vector f
339 const VECTOR2L offset = VECTOR2L( aSeg.A ) - A; // offset vector ac
340 const ecoord determinant = dir2.Cross( dir1 );
341
342 // Handle parallel/collinear case
343 if( determinant == 0 )
344 {
345 // Check if lines are collinear (not just parallel) using cross product
346 // Lines are collinear if offset vector is also parallel to direction vector
347 const ecoord collinear_test = dir1.Cross( offset );
348
349 if( collinear_test != 0 )
350 return false; // Parallel but not collinear
351
352 // Lines are collinear - for infinite lines, they always intersect
353 if( aLines )
354 {
355 // For infinite collinear lines, intersection point is ambiguous
356 // Use the midpoint between the two segment start points as a reasonable choice
357 if( aPt )
358 {
359 // If aSeg is degenerate (point), use its start point
360 if( aSeg.A == aSeg.B )
361 {
362 *aPt = aSeg.A;
363 }
364 else if( A == B )
365 { // If this segment is degenerate (point), use its start point
366 *aPt = A;
367 }
368 else
369 {
370 const VECTOR2I midpoint = ( A + aSeg.A ) / 2;
371 *aPt = midpoint;
372 }
373 }
374 return true;
375 }
376
377 // For segments, check overlap using the axis with larger coordinate range
378 const bool use_x_axis = std::abs( dir1.x ) >= std::abs( dir1.y );
379 return checkCollinearOverlap( aSeg, use_x_axis, aIgnoreEndpoints, aPt );
380 }
381
382 // param2_num = f × ac (parameter for second segment: s = p/d)
383 // param1_num = e × ac (parameter for first segment: t = q/d)
384 const ecoord param2_num = dir2.Cross( offset );
385 const ecoord param1_num = dir1.Cross( offset );
386
387 // For segments (not infinite lines), check if intersection is within both segments
388 if( !aLines )
389 {
390 // Parameters must be in [0,1] for intersection within segments
391 // Since we're comparing t = q/d and s = p/d to [0,1], we need to handle sign of d
392 if( determinant > 0 )
393 {
394 // d > 0: check 0 ≤ q ≤ d and 0 ≤ p ≤ d
395 if( param1_num < 0 || param1_num > determinant ||
396 param2_num < 0 || param2_num > determinant )
397 return false;
398 }
399 else
400 {
401 // d < 0: check d ≤ q ≤ 0 and d ≤ p ≤ 0
402 if( param1_num > 0 || param1_num < determinant ||
403 param2_num > 0 || param2_num < determinant )
404 return false;
405 }
406
407 // Optionally exclude endpoint intersections (when segments share vertices)
408 if( aIgnoreEndpoints &&
409 ( param1_num == 0 || param1_num == determinant ) &&
410 ( param2_num == 0 || param2_num == determinant ) )
411 {
412 return false;
413 }
414 }
415
416 if( aPt )
417 {
418 // Use parametric equation: intersection = aSeg.A + (q/d) * f
419 const VECTOR2L scaled_dir2( rescale( param1_num, dir2.x, determinant ),
420 rescale( param1_num, dir2.y, determinant ) );
421 const VECTOR2L result = VECTOR2L( aSeg.A ) + scaled_dir2;
422
423 // Verify result fits in coordinate type range
424 constexpr ecoord max_coord = std::numeric_limits<VECTOR2I::coord_type>::max();
425 constexpr ecoord min_coord = std::numeric_limits<VECTOR2I::coord_type>::min();
426
427 if( result.x > max_coord || result.x < min_coord ||
428 result.y > max_coord || result.y < min_coord )
429 {
430 return false; // Intersection exists but coordinates overflow
431 }
432
433 *aPt = VECTOR2I( static_cast<int>( result.x ), static_cast<int>( result.y ) );
434 }
435
436 return true;
437}
438
439
440bool SEG::Intersects( const SEG& aSeg ) const
441{
442 return intersects( aSeg );
443}
444
445
446OPT_VECTOR2I SEG::Intersect( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines ) const
447{
448 VECTOR2I ip;
449
450 if( intersects( aSeg, aIgnoreEndpoints, aLines, &ip ) )
451 return ip;
452 else
453 return OPT_VECTOR2I();
454}
455
456
457bool SEG::IntersectsLine( double aSlope, double aOffset, VECTOR2I& aIntersection ) const
458{
459 const VECTOR2L segA( A );
460 const VECTOR2L segB( B );
461 const VECTOR2L segDir = segB - segA;
462
463 // Handle vertical segment case
464 if( segDir.x == 0 )
465 {
466 // Vertical segment: x = A.x, find y on the line
467 const double intersect_y = aSlope * A.x + aOffset;
468 const int intersect_y_int = KiROUND( intersect_y );
469
470 // Check if intersection is within segment's y-range
471 const int seg_min_y = std::min( A.y, B.y );
472 const int seg_max_y = std::max( A.y, B.y );
473
474 if( intersect_y_int >= seg_min_y && intersect_y_int <= seg_max_y )
475 {
476 aIntersection = VECTOR2I( A.x, intersect_y_int );
477 return true;
478 }
479 return false;
480 }
481
482 const VECTOR2L lineDir( 1000, static_cast<ecoord>( aSlope * 1000 ) );
483 const ecoord cross_product = segDir.Cross( lineDir );
484
485 if( cross_product == 0 )
486 {
487 // Parallel lines - check if segment lies on the line
488 const double expected_y = aSlope * A.x + aOffset;
489 const double diff = std::abs( A.y - expected_y );
490
491 if( diff < 0.5 )
492 {
493 // Collinear: segment lies on the line, return midpoint
494 aIntersection = ( A + B ) / 2;
495 return true;
496 }
497
498 return false; // Parallel but not collinear
499 }
500
501 // Find intersection using parametric equations
502 // Segment: P = segA + t * segDir
503 // Line: y = aSlope * x + aOffset
504 //
505 // At intersection: segA.y + t * segDir.y = aSlope * (segA.x + t * segDir.x) + aOffset
506 // Solving for t: t = (aSlope * segA.x + aOffset - segA.y) / (segDir.y - aSlope * segDir.x)
507
508 const double numerator = aSlope * segA.x + aOffset - segA.y;
509 const double denominator = segDir.y - aSlope * segDir.x;
510
511 const double t = numerator / denominator;
512
513 // Check if intersection is within segment bounds
514 if( t >= 0.0 && t <= 1.0 )
515 {
516 aIntersection = KiROUND( segA.x + t * segDir.x, segA.y + t * segDir.y );
517 return true;
518 }
519
520 return false;
521}
522
523
525{
526 VECTOR2I slope( B - A );
527 VECTOR2I endPoint = slope.Perpendicular() + aP;
528
529 return SEG( aP, endPoint );
530}
531
532
533SEG SEG::ParallelSeg( const VECTOR2I& aP ) const
534{
535 VECTOR2I slope( B - A );
536 VECTOR2I endPoint = slope + aP;
537
538 return SEG( aP, endPoint );
539}
540
541
542bool SEG::Collide( const SEG& aSeg, int aClearance, int* aActual ) const
543{
544 // Handle negative clearance
545 if( aClearance < 0 )
546 {
547 if( aActual )
548 *aActual = 0;
549
550 return false;
551 }
552
553 // Handle zero-length segments (points) specially.
554 // The intersects() check below doesn't handle this case correctly because
555 // the cross product with a zero vector is always zero, causing false positives.
556 if( A == B )
557 {
558 int dist = aSeg.Distance( A );
559
560 if( aActual )
561 *aActual = dist;
562
563 return dist == 0 || dist < aClearance;
564 }
565
566 if( aSeg.A == aSeg.B )
567 {
568 int dist = Distance( aSeg.A );
569
570 if( aActual )
571 *aActual = dist;
572
573 return dist == 0 || dist < aClearance;
574 }
575
576 // Check for exact intersection first
577 if( intersects( aSeg, false, false ) )
578 {
579 if( aActual )
580 *aActual = 0;
581
582 return true;
583 }
584
585 const ecoord clearance_sq = static_cast<ecoord>( aClearance ) * aClearance;
586 ecoord min_dist_sq = VECTOR2I::ECOORD_MAX;
587
588 auto checkDistance = [&]( ecoord dist, ecoord& min_dist ) -> bool
589 {
590 if( dist == 0 )
591 {
592 if( aActual )
593 *aActual = 0;
594
595 return true;
596 }
597
598 min_dist = std::min( min_dist, dist );
599 return false; // Continue checking
600 };
601
602 // There are 4 points to check: start and end of this segment, and
603 // start and end of the other segment.
604 if( checkDistance( SquaredDistance( aSeg.A ), min_dist_sq ) ||
605 checkDistance( SquaredDistance( aSeg.B ), min_dist_sq ) ||
606 checkDistance( aSeg.SquaredDistance( A ), min_dist_sq ) ||
607 checkDistance( aSeg.SquaredDistance( B ), min_dist_sq ) )
608 {
609 return true;
610 }
611
612 if( min_dist_sq < clearance_sq )
613 {
614 if( aActual )
615 *aActual = static_cast<int>( isqrt( min_dist_sq ) );
616
617 return true;
618 }
619
620 if( aActual )
621 *aActual = static_cast<int>( isqrt( min_dist_sq ) );
622
623 return false;
624}
625
626
627bool SEG::Contains( const VECTOR2I& aP ) const
628{
629 return SquaredDistance( aP ) <= 3;
630}
631
632
633const VECTOR2I SEG::NearestPoint( const VECTOR2I& aP ) const
634{
635 // Inlined for performance reasons
636 VECTOR2L d;
637 d.x = static_cast<int64_t>( B.x ) - A.x;
638 d.y = static_cast<int64_t>( B.y ) - A.y;
639
640 ecoord l_squared( d.x * d.x + d.y * d.y );
641
642 if( l_squared == 0 )
643 return A;
644
645 // Inlined for performance reasons
646 VECTOR2L pa;
647 pa.x = static_cast<int64_t>( aP.x ) - A.x;
648 pa.y = static_cast<int64_t>( aP.y ) - A.y;
649
650 ecoord t = d.Dot( pa );
651
652 if( t < 0 )
653 return A;
654 else if( t > l_squared )
655 return B;
656
657 ecoord xp = rescale( t, (ecoord) d.x, l_squared );
658 ecoord yp = rescale( t, (ecoord) d.y, l_squared );
659
660 return VECTOR2<ecoord>( A.x + xp, A.y + yp );
661}
662
663
664const VECTOR2I SEG::ReflectPoint( const VECTOR2I& aP ) const
665{
666 VECTOR2I d = B - A;
667 VECTOR2I::extended_type l_squared = d.Dot( d );
668 VECTOR2I::extended_type t = d.Dot( aP - A );
670
671 if( !l_squared )
672 {
673 c = aP;
674 }
675 else
676 {
677 c.x = A.x + rescale( t, static_cast<VECTOR2I::extended_type>( d.x ), l_squared );
678 c.y = A.y + rescale( t, static_cast<VECTOR2I::extended_type>( d.y ), l_squared );
679 }
680
681 return VECTOR2<ecoord>( 2 * c.x - aP.x, 2 * c.y - aP.y );
682}
683
684
686{
687 VECTOR2I d = B - A;
688 ecoord l_squared = d.Dot( d );
689
690 if( l_squared == 0 )
691 return A;
692
693 ecoord t = d.Dot( aP - A );
694
695 ecoord xp = rescale( t, ecoord{ d.x }, l_squared );
696 ecoord yp = rescale( t, ecoord{ d.y }, l_squared );
697
698 return VECTOR2<ecoord>( A.x + xp, A.y + yp );
699}
700
701
702int SEG::Distance( const SEG& aSeg ) const
703{
704 return int( isqrt( SquaredDistance( aSeg ) ) );
705}
706
707
708int SEG::Distance( const VECTOR2I& aP ) const
709{
710 return int( isqrt( SquaredDistance( aP ) ) );
711}
712
713
715{
716 VECTOR2<ecoord> ab( ecoord( B.x ) - A.x, ecoord( B.y ) - A.y );
717 VECTOR2<ecoord> ap( ecoord( aP.x ) - A.x, ecoord( aP.y ) - A.y );
718
719 ecoord e = ap.Dot( ab );
720
721 if( e <= 0 )
722 return ap.SquaredEuclideanNorm();
723
725
726 if( e >= f )
727 {
728 VECTOR2<ecoord> bp( ecoord( aP.x ) - B.x, ecoord( aP.y ) - B.y );
729
730 return bp.Dot( bp );
731 }
732
733 const double g = ap.SquaredEuclideanNorm() - ( double( e ) * e ) / f;
734
735 // The only way g can be negative is if there was a rounding error since
736 // e is the projection of aP onto ab and therefore cannot be greater than
737 // the length of ap and f is guaranteed to be greater than e, meaning
738 // e * e / f cannot be greater than ap.SquaredEuclideanNorm()
739 if( g < 0 || g > static_cast<double>( std::numeric_limits<ecoord>::max() ) )
740 return 0;
741
742 return KiROUND<double, ecoord>( g );
743}
744
745
746int SEG::LineDistance( const VECTOR2I& aP, bool aDetermineSide ) const
747{
748 ecoord p = ecoord{ A.y } - B.y;
749 ecoord q = ecoord{ B.x } - A.x;
750 ecoord r = -p * A.x - q * A.y;
751 ecoord l = p * p + q * q;
752 ecoord det = p * aP.x + q * aP.y + r;
753 ecoord dist_sq = 0;
754
755 if( l > 0 )
756 {
757 dist_sq = rescale( det, det, l );
758 }
759
760 ecoord dist = isqrt( dist_sq );
761
762 return static_cast<int>( aDetermineSide ? sgn( det ) * dist : std::abs( dist ) );
763}
764
765
766bool SEG::mutualDistanceSquared( const SEG& aSeg, ecoord& aD1, ecoord& aD2 ) const
767{
768 SEG a( *this );
769 SEG b( aSeg );
770
771 if( a.SquaredLength() < b.SquaredLength() )
772 std::swap(a, b);
773
774 ecoord p = ecoord{ a.A.y } - a.B.y;
775 ecoord q = ecoord{ a.B.x } - a.A.x;
776 ecoord r = -p * a.A.x - q * a.A.y;
777
778 ecoord l = p * p + q * q;
779
780 if( l == 0 )
781 return false;
782
783 ecoord det1 = p * b.A.x + q * b.A.y + r;
784 ecoord det2 = p * b.B.x + q * b.B.y + r;
785
786 ecoord dsq1 = rescale( det1, det1, l );
787 ecoord dsq2 = rescale( det2, det2, l );
788
789 aD1 = sgn( det1 ) * dsq1;
790 aD2 = sgn( det2 ) * dsq2;
791
792 return true;
793}
794
795bool SEG::ApproxCollinear( const SEG& aSeg, int aDistanceThreshold ) const
796{
797 ecoord thresholdSquared = Square( aDistanceThreshold );
798 ecoord d1_sq, d2_sq;
799
800 if( !mutualDistanceSquared( aSeg, d1_sq, d2_sq ) )
801 return false;
802
803 return std::abs( d1_sq ) <= thresholdSquared && std::abs( d2_sq ) <= thresholdSquared;
804}
805
806
807bool SEG::ApproxParallel( const SEG& aSeg, int aDistanceThreshold ) const
808{
809 ecoord thresholdSquared = Square( aDistanceThreshold );
810 ecoord d1_sq, d2_sq;
811
812 if( ! mutualDistanceSquared( aSeg, d1_sq, d2_sq ) )
813 return false;
814
815 return std::abs( d1_sq - d2_sq ) <= thresholdSquared;
816}
817
818
819bool SEG::ApproxPerpendicular( const SEG& aSeg ) const
820{
821 SEG perp = PerpendicularSeg( A );
822
823 return aSeg.ApproxParallel( perp );
824}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
EDA_ANGLE Normalize180()
Definition eda_angle.h:268
const VECTOR2I ReflectPoint(const VECTOR2I &aP) const
Reflect a point using this segment as axis.
Definition seg.cpp:664
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
ecoord SquaredDistance(const SEG &aSeg) const
Definition seg.cpp:80
bool IntersectsLine(double aSlope, double aOffset, VECTOR2I &aIntersection) const
Check if this segment intersects a line defined by slope aSlope and offset aOffset.
Definition seg.cpp:457
bool intersects(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false, VECTOR2I *aPt=nullptr) const
Definition seg.cpp:312
VECTOR2I::extended_type ecoord
Definition seg.h:44
VECTOR2I B
Definition seg.h:50
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition seg.cpp:633
bool Intersects(const SEG &aSeg) const
Definition seg.cpp:440
bool checkCollinearOverlap(const SEG &aSeg, bool useXAxis, bool aIgnoreEndpoints, VECTOR2I *aPt) const
Definition seg.cpp:220
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition seg.cpp:446
static SEG::ecoord Square(int a)
Definition seg.h:123
bool Collide(const SEG &aSeg, int aClearance, int *aActual=nullptr) const
Definition seg.cpp:542
bool ApproxParallel(const SEG &aSeg, int aDistanceThreshold=1) const
Definition seg.cpp:807
SEG()
Create an empty (0, 0) segment.
Definition seg.h:55
SEG ParallelSeg(const VECTOR2I &aP) const
Compute a segment parallel to this one, passing through point aP.
Definition seg.cpp:533
ecoord SquaredLength() const
Definition seg.h:348
bool ApproxPerpendicular(const SEG &aSeg) const
Definition seg.cpp:819
bool ApproxCollinear(const SEG &aSeg, int aDistanceThreshold=1) const
Definition seg.cpp:795
bool mutualDistanceSquared(const SEG &aSeg, ecoord &aD1, ecoord &aD2) const
Definition seg.cpp:766
bool NearestPoints(const SEG &aSeg, VECTOR2I &aPtA, VECTOR2I &aPtB, int64_t &aDistSq) const
Compute closest points between this segment and aSeg.
Definition seg.cpp:162
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition seg.cpp:702
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
SEG PerpendicularSeg(const VECTOR2I &aP) const
Compute a segment perpendicular to this one, passing through point aP.
Definition seg.cpp:524
EDA_ANGLE Angle(const SEG &aOther) const
Determine the smallest angle between two segments.
Definition seg.cpp:111
Define a general 2D-vector/point.
Definition vector2d.h:71
constexpr extended_type Cross(const VECTOR2< T > &aVector) const
Compute cross product of self with aVector.
Definition vector2d.h:546
constexpr extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition vector2d.h:307
static constexpr extended_type ECOORD_MAX
Definition vector2d.h:76
VECTOR2_TRAITS< int32_t >::extended_type extended_type
Definition vector2d.h:73
constexpr VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition vector2d.h:314
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition vector2d.h:554
static coord2_t cross_product(const VECTOR2I &O, const VECTOR2I &A, const VECTOR2I &B)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
constexpr T ct_sqrt(T x)
Definition seg.cpp:52
constexpr T sqrt_helper(T x, T lo, T hi)
Definition seg.cpp:39
static constexpr T sqrt_max_typed
Definition seg.cpp:58
int sgn(T aVal)
Definition seg.cpp:33
T isqrt(T x)
Definition seg.cpp:61
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:39
wxString result
Test unit parsing edge cases and error handling.
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition util.h:139
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< int64_t > VECTOR2L
Definition vector2d.h:696