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 if( Intersects( aSeg ) )
83 return 0;
84
85 const VECTOR2I pts[4] =
86 {
87 aSeg.NearestPoint( A ) - A,
88 aSeg.NearestPoint( B ) - B,
89 NearestPoint( aSeg.A ) - aSeg.A,
90 NearestPoint( aSeg.B ) - aSeg.B
91 };
92
94
95 for( int i = 0; i < 4; i++ )
96 m = std::min( m, pts[i].SquaredEuclideanNorm() );
97
98 return m;
99}
100
101
102EDA_ANGLE SEG::Angle( const SEG& aOther ) const
103{
104 EDA_ANGLE thisAngle = EDA_ANGLE( A - B ).Normalize180();
105 EDA_ANGLE otherAngle = EDA_ANGLE( aOther.A - aOther.B ).Normalize180();
106
107 return std::abs( ( thisAngle - otherAngle ).Normalize180() );
108}
109
110
111const VECTOR2I SEG::NearestPoint( const SEG& aSeg ) const
112{
113 if( OPT_VECTOR2I p = Intersect( aSeg ) )
114 return *p;
115
116 const VECTOR2I pts_origin[4] =
117 {
118 aSeg.NearestPoint( A ),
119 aSeg.NearestPoint( B ),
120 NearestPoint( aSeg.A ),
121 NearestPoint( aSeg.B )
122 };
123
124
125 const VECTOR2I* pts_out[4] =
126 {
127 &A,
128 &B,
129 &pts_origin[2],
130 &pts_origin[3]
131 };
132
133 const ecoord pts_dist[4] =
134 {
135 ( pts_origin[0] - A ).SquaredEuclideanNorm(),
136 ( pts_origin[1] - B ).SquaredEuclideanNorm(),
137 ( pts_origin[2] - aSeg.A ).SquaredEuclideanNorm(),
138 ( pts_origin[3] - aSeg.B ).SquaredEuclideanNorm()
139 };
140
141 int min_i = 0;
142
143 for( int i = 0; i < 4; i++ )
144 {
145 if( pts_dist[i] < pts_dist[min_i] )
146 min_i = i;
147 }
148
149 return *pts_out[min_i];
150}
151
152
153bool SEG::NearestPoints( const SEG& aSeg, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const
154{
155 if( OPT_VECTOR2I p = Intersect( aSeg ) )
156 {
157 aPtA = aPtB = *p;
158 aDistSq = 0;
159
160 return true;
161 }
162
163 const VECTOR2I pts_origin[4] =
164 {
165 aSeg.NearestPoint( A ),
166 aSeg.NearestPoint( B ),
167 NearestPoint( aSeg.A ),
168 NearestPoint( aSeg.B )
169 };
170
171 const VECTOR2I* pts_a_out[4] =
172 {
173 &A,
174 &B,
175 &pts_origin[2],
176 &pts_origin[3]
177 };
178
179 const VECTOR2I* pts_b_out[4] =
180 {
181 &pts_origin[0],
182 &pts_origin[1],
183 &aSeg.A,
184 &aSeg.B
185 };
186
187 const ecoord pts_dist[4] =
188 {
189 ( pts_origin[0] - A ).SquaredEuclideanNorm(),
190 ( pts_origin[1] - B ).SquaredEuclideanNorm(),
191 ( pts_origin[2] - aSeg.A ).SquaredEuclideanNorm(),
192 ( pts_origin[3] - aSeg.B ).SquaredEuclideanNorm()
193 };
194
195 int min_i = 0;
196
197 for( int i = 0; i < 4; i++ )
198 {
199 if( pts_dist[i] < pts_dist[min_i] )
200 min_i = i;
201 }
202
203 aPtA = *pts_a_out[min_i];
204 aPtB = *pts_b_out[min_i];
205 aDistSq = pts_dist[min_i];
206
207 return true;
208}
209
210
211bool SEG::checkCollinearOverlap( const SEG& aSeg, bool useXAxis, bool aIgnoreEndpoints, VECTOR2I* aPt ) const
212{
213 // Extract coordinates based on the chosen axis
214 int seg1_start, seg1_end, seg2_start, seg2_end;
215 int coord1_start, coord1_end; // For calculating other axis coordinate
216
217 if( useXAxis )
218 {
219 seg1_start = A.x; seg1_end = B.x;
220 seg2_start = aSeg.A.x; seg2_end = aSeg.B.x;
221 coord1_start = A.y; coord1_end = B.y;
222 }
223 else
224 {
225 seg1_start = A.y; seg1_end = B.y;
226 seg2_start = aSeg.A.y; seg2_end = aSeg.B.y;
227 coord1_start = A.x; coord1_end = B.x;
228 }
229
230 // Find segment ranges on the projection axis
231 const int seg1_min = std::min( seg1_start, seg1_end );
232 const int seg1_max = std::max( seg1_start, seg1_end );
233 const int seg2_min = std::min( seg2_start, seg2_end );
234 const int seg2_max = std::max( seg2_start, seg2_end );
235
236 // Check for overlap
237 const bool overlaps = seg1_max >= seg2_min && seg2_max >= seg1_min;
238 if( !overlaps )
239 return false;
240
241 // Check if intersection is only at endpoints when aIgnoreEndpoints is true
242 if( aIgnoreEndpoints )
243 {
244 // Calculate overlap region
245 const int overlap_start = std::max( seg1_min, seg2_min );
246 const int overlap_end = std::min( seg1_max, seg2_max );
247
248 // If overlap region has zero length, segments only touch at endpoint
249 if( overlap_start == overlap_end )
250 {
251 // Check if this endpoint touching involves actual segment endpoints
252 // (not just projected endpoints due to min/max calculation)
253 bool isEndpointTouch = false;
254
255 // Check if the touch point corresponds to actual segment endpoints
256 if( overlap_start == seg1_min || overlap_start == seg1_max )
257 {
258 // Touch point is at seg1's endpoint, check if it's also at seg2's endpoint
259 if( overlap_start == seg2_min || overlap_start == seg2_max )
260 {
261 isEndpointTouch = true;
262 }
263 }
264
265 if( isEndpointTouch )
266 return false; // Ignore endpoint-only intersection
267 }
268 }
269
270 // Calculate intersection point if requested
271 if( aPt )
272 {
273 // Find midpoint of overlap region
274 const int overlap_start = std::max( seg1_min, seg2_min );
275 const int overlap_end = std::min( seg1_max, seg2_max );
276 const int intersection_proj = ( overlap_start + overlap_end ) / 2;
277
278 // Calculate corresponding coordinate on the other axis
279 int intersection_other;
280 if( seg1_end != seg1_start )
281 {
282 // Use this segment's line equation to find other coordinate
283 intersection_other = coord1_start + static_cast<int>(
284 rescale( intersection_proj - seg1_start, coord1_end - coord1_start, seg1_end - seg1_start ) );
285 }
286 else
287 {
288 // Degenerate segment (point) or perpendicular to projection axis
289 intersection_other = coord1_start;
290 }
291
292 // Set result based on projection axis
293 if( useXAxis )
294 *aPt = VECTOR2I( intersection_proj, intersection_other );
295 else
296 *aPt = VECTOR2I( intersection_other, intersection_proj );
297 }
298
299 return true;
300}
301
302
303bool SEG::intersects( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines, VECTOR2I* aPt ) const
304{
305 // Quick rejection: check if segment bounding boxes overlap
306 // (Skip for line mode since infinite lines can intersect anywhere)
307 if( !aLines )
308 {
309 const int this_min_x = std::min( A.x, B.x );
310 const int this_max_x = std::max( A.x, B.x );
311 const int this_min_y = std::min( A.y, B.y );
312 const int this_max_y = std::max( A.y, B.y );
313
314 const int other_min_x = std::min( aSeg.A.x, aSeg.B.x );
315 const int other_max_x = std::max( aSeg.A.x, aSeg.B.x );
316 const int other_min_y = std::min( aSeg.A.y, aSeg.B.y );
317 const int other_max_y = std::max( aSeg.A.y, aSeg.B.y );
318
319 if( this_max_x < other_min_x || other_max_x < this_min_x ||
320 this_max_y < other_min_y || other_max_y < this_min_y )
321 {
322 return false;
323 }
324 }
325
326 // Calculate direction vectors and offset vector using VECTOR2 operations
327 // Using parametric form: P₁ = A + t*dir1, P₂ = aSeg.A + s*dir2
328 const VECTOR2L dir1 = VECTOR2L( B ) - A; // direction vector e
329 const VECTOR2L dir2 = VECTOR2L( aSeg.B ) - aSeg.A; // direction vector f
330 const VECTOR2L offset = VECTOR2L( aSeg.A ) - A; // offset vector ac
331 const ecoord determinant = dir2.Cross( dir1 );
332
333 // Handle parallel/collinear case
334 if( determinant == 0 )
335 {
336 // Check if lines are collinear (not just parallel) using cross product
337 // Lines are collinear if offset vector is also parallel to direction vector
338 const ecoord collinear_test = dir1.Cross( offset );
339
340 if( collinear_test != 0 )
341 return false; // Parallel but not collinear
342
343 // Lines are collinear - for infinite lines, they always intersect
344 if( aLines )
345 {
346 // For infinite collinear lines, intersection point is ambiguous
347 // Use the midpoint between the two segment start points as a reasonable choice
348 if( aPt )
349 {
350 // If aSeg is degenerate (point), use its start point
351 if( aSeg.A == aSeg.B )
352 {
353 *aPt = aSeg.A;
354 }
355 else if( A == B )
356 { // If this segment is degenerate (point), use its start point
357 *aPt = A;
358 }
359 else
360 {
361 const VECTOR2I midpoint = ( A + aSeg.A ) / 2;
362 *aPt = midpoint;
363 }
364 }
365 return true;
366 }
367
368 // For segments, check overlap using the axis with larger coordinate range
369 const bool use_x_axis = std::abs( dir1.x ) >= std::abs( dir1.y );
370 return checkCollinearOverlap( aSeg, use_x_axis, aIgnoreEndpoints, aPt );
371 }
372
373 // param2_num = f × ac (parameter for second segment: s = p/d)
374 // param1_num = e × ac (parameter for first segment: t = q/d)
375 const ecoord param2_num = dir2.Cross( offset );
376 const ecoord param1_num = dir1.Cross( offset );
377
378 // For segments (not infinite lines), check if intersection is within both segments
379 if( !aLines )
380 {
381 // Parameters must be in [0,1] for intersection within segments
382 // Since we're comparing t = q/d and s = p/d to [0,1], we need to handle sign of d
383 if( determinant > 0 )
384 {
385 // d > 0: check 0 ≤ q ≤ d and 0 ≤ p ≤ d
386 if( param1_num < 0 || param1_num > determinant ||
387 param2_num < 0 || param2_num > determinant )
388 return false;
389 }
390 else
391 {
392 // d < 0: check d ≤ q ≤ 0 and d ≤ p ≤ 0
393 if( param1_num > 0 || param1_num < determinant ||
394 param2_num > 0 || param2_num < determinant )
395 return false;
396 }
397
398 // Optionally exclude endpoint intersections (when segments share vertices)
399 if( aIgnoreEndpoints &&
400 ( param1_num == 0 || param1_num == determinant ) &&
401 ( param2_num == 0 || param2_num == determinant ) )
402 {
403 return false;
404 }
405 }
406
407 if( aPt )
408 {
409 // Use parametric equation: intersection = aSeg.A + (q/d) * f
410 const VECTOR2L scaled_dir2( rescale( param1_num, dir2.x, determinant ),
411 rescale( param1_num, dir2.y, determinant ) );
412 const VECTOR2L result = VECTOR2L( aSeg.A ) + scaled_dir2;
413
414 // Verify result fits in coordinate type range
415 constexpr ecoord max_coord = std::numeric_limits<VECTOR2I::coord_type>::max();
416 constexpr ecoord min_coord = std::numeric_limits<VECTOR2I::coord_type>::min();
417
418 if( result.x > max_coord || result.x < min_coord ||
419 result.y > max_coord || result.y < min_coord )
420 {
421 return false; // Intersection exists but coordinates overflow
422 }
423
424 *aPt = VECTOR2I( static_cast<int>( result.x ), static_cast<int>( result.y ) );
425 }
426
427 return true;
428}
429
430
431bool SEG::Intersects( const SEG& aSeg ) const
432{
433 return intersects( aSeg );
434}
435
436
437OPT_VECTOR2I SEG::Intersect( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines ) const
438{
439 VECTOR2I ip;
440
441 if( intersects( aSeg, aIgnoreEndpoints, aLines, &ip ) )
442 return ip;
443 else
444 return OPT_VECTOR2I();
445}
446
447
448bool SEG::IntersectsLine( double aSlope, double aOffset, VECTOR2I& aIntersection ) const
449{
450 const VECTOR2L segA( A );
451 const VECTOR2L segB( B );
452 const VECTOR2L segDir = segB - segA;
453
454 // Handle vertical segment case
455 if( segDir.x == 0 )
456 {
457 // Vertical segment: x = A.x, find y on the line
458 const double intersect_y = aSlope * A.x + aOffset;
459 const int intersect_y_int = KiROUND( intersect_y );
460
461 // Check if intersection is within segment's y-range
462 const int seg_min_y = std::min( A.y, B.y );
463 const int seg_max_y = std::max( A.y, B.y );
464
465 if( intersect_y_int >= seg_min_y && intersect_y_int <= seg_max_y )
466 {
467 aIntersection = VECTOR2I( A.x, intersect_y_int );
468 return true;
469 }
470 return false;
471 }
472
473 const VECTOR2L lineDir( 1000, static_cast<ecoord>( aSlope * 1000 ) );
474 const ecoord cross_product = segDir.Cross( lineDir );
475
476 if( cross_product == 0 )
477 {
478 // Parallel lines - check if segment lies on the line
479 const double expected_y = aSlope * A.x + aOffset;
480 const double diff = std::abs( A.y - expected_y );
481
482 if( diff < 0.5 )
483 {
484 // Collinear: segment lies on the line, return midpoint
485 aIntersection = ( A + B ) / 2;
486 return true;
487 }
488
489 return false; // Parallel but not collinear
490 }
491
492 // Find intersection using parametric equations
493 // Segment: P = segA + t * segDir
494 // Line: y = aSlope * x + aOffset
495 //
496 // At intersection: segA.y + t * segDir.y = aSlope * (segA.x + t * segDir.x) + aOffset
497 // Solving for t: t = (aSlope * segA.x + aOffset - segA.y) / (segDir.y - aSlope * segDir.x)
498
499 const double numerator = aSlope * segA.x + aOffset - segA.y;
500 const double denominator = segDir.y - aSlope * segDir.x;
501
502 const double t = numerator / denominator;
503
504 // Check if intersection is within segment bounds
505 if( t >= 0.0 && t <= 1.0 )
506 {
507 const double intersect_x = segA.x + t * segDir.x;
508 const double intersect_y = segA.y + t * segDir.y;
509
510 aIntersection = VECTOR2I( KiROUND( intersect_x ), KiROUND( intersect_y ) );
511 return true;
512 }
513
514 return false;
515}
516
517
519{
520 VECTOR2I slope( B - A );
521 VECTOR2I endPoint = slope.Perpendicular() + aP;
522
523 return SEG( aP, endPoint );
524}
525
526
527SEG SEG::ParallelSeg( const VECTOR2I& aP ) const
528{
529 VECTOR2I slope( B - A );
530 VECTOR2I endPoint = slope + aP;
531
532 return SEG( aP, endPoint );
533}
534
535
536bool SEG::Collide( const SEG& aSeg, int aClearance, int* aActual ) const
537{
538 // Handle negative clearance
539 if( aClearance < 0 )
540 {
541 if( aActual )
542 *aActual = 0;
543
544 return false;
545 }
546
547 // Check for exact intersection first
548 if( intersects( aSeg, false, false ) )
549 {
550 if( aActual )
551 *aActual = 0;
552
553 return true;
554 }
555
556 const ecoord clearance_sq = static_cast<ecoord>( aClearance ) * aClearance;
557 ecoord min_dist_sq = VECTOR2I::ECOORD_MAX;
558
559 auto checkDistance = [&]( ecoord dist, ecoord& min_dist ) -> bool
560 {
561 if( dist == 0 )
562 {
563 if( aActual )
564 *aActual = 0;
565
566 return true;
567 }
568
569 min_dist = std::min( min_dist, dist );
570 return false; // Continue checking
571 };
572
573 // There are 4 points to check: start and end of this segment, and
574 // start and end of the other segment.
575 if( checkDistance( SquaredDistance( aSeg.A ), min_dist_sq ) ||
576 checkDistance( SquaredDistance( aSeg.B ), min_dist_sq ) ||
577 checkDistance( aSeg.SquaredDistance( A ), min_dist_sq ) ||
578 checkDistance( aSeg.SquaredDistance( B ), min_dist_sq ) )
579 {
580 return true;
581 }
582
583 if( min_dist_sq < clearance_sq )
584 {
585 if( aActual )
586 *aActual = static_cast<int>( isqrt( min_dist_sq ) );
587
588 return true;
589 }
590
591 if( aActual )
592 *aActual = static_cast<int>( isqrt( min_dist_sq ) );
593
594 return false;
595}
596
597
598bool SEG::Contains( const VECTOR2I& aP ) const
599{
600 return SquaredDistance( aP ) <= 3;
601}
602
603
604const VECTOR2I SEG::NearestPoint( const VECTOR2I& aP ) const
605{
606 // Inlined for performance reasons
607 VECTOR2L d;
608 d.x = static_cast<int64_t>( B.x ) - A.x;
609 d.y = static_cast<int64_t>( B.y ) - A.y;
610
611 ecoord l_squared( d.x * d.x + d.y * d.y );
612
613 if( l_squared == 0 )
614 return A;
615
616 // Inlined for performance reasons
617 VECTOR2L pa;
618 pa.x = static_cast<int64_t>( aP.x ) - A.x;
619 pa.y = static_cast<int64_t>( aP.y ) - A.y;
620
621 ecoord t = d.Dot( pa );
622
623 if( t < 0 )
624 return A;
625 else if( t > l_squared )
626 return B;
627
628 ecoord xp = rescale( t, (ecoord) d.x, l_squared );
629 ecoord yp = rescale( t, (ecoord) d.y, l_squared );
630
631 return VECTOR2<ecoord>( A.x + xp, A.y + yp );
632}
633
634
635const VECTOR2I SEG::ReflectPoint( const VECTOR2I& aP ) const
636{
637 VECTOR2I d = B - A;
638 VECTOR2I::extended_type l_squared = d.Dot( d );
639 VECTOR2I::extended_type t = d.Dot( aP - A );
641
642 if( !l_squared )
643 {
644 c = aP;
645 }
646 else
647 {
648 c.x = A.x + rescale( t, static_cast<VECTOR2I::extended_type>( d.x ), l_squared );
649 c.y = A.y + rescale( t, static_cast<VECTOR2I::extended_type>( d.y ), l_squared );
650 }
651
652 return VECTOR2<ecoord>( 2 * c.x - aP.x, 2 * c.y - aP.y );
653}
654
655
657{
658 VECTOR2I d = B - A;
659 ecoord l_squared = d.Dot( d );
660
661 if( l_squared == 0 )
662 return A;
663
664 ecoord t = d.Dot( aP - A );
665
666 ecoord xp = rescale( t, ecoord{ d.x }, l_squared );
667 ecoord yp = rescale( t, ecoord{ d.y }, l_squared );
668
669 return VECTOR2<ecoord>( A.x + xp, A.y + yp );
670}
671
672
673int SEG::Distance( const SEG& aSeg ) const
674{
675 return int( isqrt( SquaredDistance( aSeg ) ) );
676}
677
678
679int SEG::Distance( const VECTOR2I& aP ) const
680{
681 return int( isqrt( SquaredDistance( aP ) ) );
682}
683
684
686{
687 VECTOR2<ecoord> ab( ecoord( B.x ) - A.x, ecoord( B.y ) - A.y );
688 VECTOR2<ecoord> ap( ecoord( aP.x ) - A.x, ecoord( aP.y ) - A.y );
689
690 ecoord e = ap.Dot( ab );
691
692 if( e <= 0 )
693 return ap.SquaredEuclideanNorm();
694
696
697 if( e >= f )
698 {
699 VECTOR2<ecoord> bp( ecoord( aP.x ) - B.x, ecoord( aP.y ) - B.y );
700
701 return bp.Dot( bp );
702 }
703
704 const double g = ap.SquaredEuclideanNorm() - ( double( e ) * e ) / f;
705
706 // The only way g can be negative is if there was a rounding error since
707 // e is the projection of aP onto ab and therefore cannot be greater than
708 // the length of ap and f is guaranteed to be greater than e, meaning
709 // e * e / f cannot be greater than ap.SquaredEuclideanNorm()
710 if( g < 0 || g > static_cast<double>( std::numeric_limits<ecoord>::max() ) )
711 return 0;
712
713 return KiROUND<double, ecoord>( g );
714}
715
716
717int SEG::LineDistance( const VECTOR2I& aP, bool aDetermineSide ) const
718{
719 ecoord p = ecoord{ A.y } - B.y;
720 ecoord q = ecoord{ B.x } - A.x;
721 ecoord r = -p * A.x - q * A.y;
722 ecoord l = p * p + q * q;
723 ecoord det = p * aP.x + q * aP.y + r;
724 ecoord dist_sq = 0;
725
726 if( l > 0 )
727 {
728 dist_sq = rescale( det, det, l );
729 }
730
731 ecoord dist = isqrt( dist_sq );
732
733 return static_cast<int>( aDetermineSide ? sgn( det ) * dist : std::abs( dist ) );
734}
735
736
737bool SEG::mutualDistanceSquared( const SEG& aSeg, ecoord& aD1, ecoord& aD2 ) const
738{
739 SEG a( *this );
740 SEG b( aSeg );
741
742 if( a.SquaredLength() < b.SquaredLength() )
743 std::swap(a, b);
744
745 ecoord p = ecoord{ a.A.y } - a.B.y;
746 ecoord q = ecoord{ a.B.x } - a.A.x;
747 ecoord r = -p * a.A.x - q * a.A.y;
748
749 ecoord l = p * p + q * q;
750
751 if( l == 0 )
752 return false;
753
754 ecoord det1 = p * b.A.x + q * b.A.y + r;
755 ecoord det2 = p * b.B.x + q * b.B.y + r;
756
757 ecoord dsq1 = rescale( det1, det1, l );
758 ecoord dsq2 = rescale( det2, det2, l );
759
760 aD1 = sgn( det1 ) * dsq1;
761 aD2 = sgn( det2 ) * dsq2;
762
763 return true;
764}
765
766bool SEG::ApproxCollinear( const SEG& aSeg, int aDistanceThreshold ) const
767{
768 ecoord thresholdSquared = Square( aDistanceThreshold );
769 ecoord d1_sq, d2_sq;
770
771 if( !mutualDistanceSquared( aSeg, d1_sq, d2_sq ) )
772 return false;
773
774 return std::abs( d1_sq ) <= thresholdSquared && std::abs( d2_sq ) <= thresholdSquared;
775}
776
777
778bool SEG::ApproxParallel( const SEG& aSeg, int aDistanceThreshold ) const
779{
780 ecoord thresholdSquared = Square( aDistanceThreshold );
781 ecoord d1_sq, d2_sq;
782
783 if( ! mutualDistanceSquared( aSeg, d1_sq, d2_sq ) )
784 return false;
785
786 return std::abs( d1_sq - d2_sq ) <= thresholdSquared;
787}
788
789
790bool SEG::ApproxPerpendicular( const SEG& aSeg ) const
791{
792 SEG perp = PerpendicularSeg( A );
793
794 return aSeg.ApproxParallel( perp );
795}
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:635
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:717
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:448
bool intersects(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false, VECTOR2I *aPt=nullptr) const
Definition seg.cpp:303
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:604
bool Intersects(const SEG &aSeg) const
Definition seg.cpp:431
bool checkCollinearOverlap(const SEG &aSeg, bool useXAxis, bool aIgnoreEndpoints, VECTOR2I *aPt) const
Definition seg.cpp:211
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:437
static SEG::ecoord Square(int a)
Definition seg.h:123
bool Collide(const SEG &aSeg, int aClearance, int *aActual=nullptr) const
Definition seg.cpp:536
bool ApproxParallel(const SEG &aSeg, int aDistanceThreshold=1) const
Definition seg.cpp:778
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:527
ecoord SquaredLength() const
Definition seg.h:348
bool ApproxPerpendicular(const SEG &aSeg) const
Definition seg.cpp:790
bool ApproxCollinear(const SEG &aSeg, int aDistanceThreshold=1) const
Definition seg.cpp:766
bool mutualDistanceSquared(const SEG &aSeg, ecoord &aD1, ecoord &aD2) const
Definition seg.cpp:737
bool NearestPoints(const SEG &aSeg, VECTOR2I &aPtA, VECTOR2I &aPtB, int64_t &aDistSq) const
Compute closest points between this segment and aSeg.
Definition seg.cpp:153
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition seg.cpp:673
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:656
SEG PerpendicularSeg(const VECTOR2I &aP) const
Compute a segment perpendicular to this one, passing through point aP.
Definition seg.cpp:518
EDA_ANGLE Angle(const SEG &aOther) const
Determine the smallest angle between two segments.
Definition seg.cpp:102
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