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 (C) 2021 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>
61T isqrt(T x)
62{
63 T r = (T) std::sqrt((double) x);
64 T sqrt_max = sqrt_max_typed<T>;
65
66 while (r < sqrt_max && r * r < x)
67 r++;
68 while (r > sqrt_max || r * r > x)
69 r--;
70
71 return r;
72}
73
74
76{
77 if( Intersects( aSeg ) )
78 return 0;
79
80 const VECTOR2I pts[4] =
81 {
82 aSeg.NearestPoint( A ) - A,
83 aSeg.NearestPoint( B ) - B,
84 NearestPoint( aSeg.A ) - aSeg.A,
85 NearestPoint( aSeg.B ) - aSeg.B
86 };
87
89
90 for( int i = 0; i < 4; i++ )
91 m = std::min( m, pts[i].SquaredEuclideanNorm() );
92
93 return m;
94}
95
96
97EDA_ANGLE SEG::Angle( const SEG& aOther ) const
98{
99 EDA_ANGLE thisAngle = EDA_ANGLE( A - B ).Normalize180();
100 EDA_ANGLE otherAngle = EDA_ANGLE( aOther.A - aOther.B ).Normalize180();
101
102 EDA_ANGLE angle = std::abs( ( thisAngle - otherAngle ).Normalize180() );
103
104 return std::min( ANGLE_180 - angle, angle );
105}
106
107
108const VECTOR2I SEG::NearestPoint( const SEG& aSeg ) const
109{
110 if( OPT_VECTOR2I p = Intersect( aSeg ) )
111 return *p;
112
113 const VECTOR2I pts_origin[4] =
114 {
115 aSeg.NearestPoint( A ),
116 aSeg.NearestPoint( B ),
117 NearestPoint( aSeg.A ),
118 NearestPoint( aSeg.B )
119 };
120
121
122 const VECTOR2I* pts_out[4] =
123 {
124 &A,
125 &B,
126 &pts_origin[2],
127 &pts_origin[3]
128 };
129
130 const ecoord pts_dist[4] =
131 {
132 ( pts_origin[0] - A ).SquaredEuclideanNorm(),
133 ( pts_origin[1] - B ).SquaredEuclideanNorm(),
134 ( pts_origin[2] - aSeg.A ).SquaredEuclideanNorm(),
135 ( pts_origin[3] - aSeg.B ).SquaredEuclideanNorm()
136 };
137
138 int min_i = 0;
139
140 for( int i = 0; i < 4; i++ )
141 {
142 if( pts_dist[i] < pts_dist[min_i] )
143 min_i = i;
144 }
145
146 return *pts_out[min_i];
147}
148
149
150bool SEG::intersects( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines, VECTOR2I* aPt ) const
151{
152 const VECTOR2<ecoord> e = VECTOR2<ecoord>( B ) - A;
153 const VECTOR2<ecoord> f = VECTOR2<ecoord>( aSeg.B ) - aSeg.A;
154 const VECTOR2<ecoord> ac = VECTOR2<ecoord>( aSeg.A ) - A;
155
156 ecoord d = f.Cross( e );
157 ecoord p = f.Cross( ac );
158 ecoord q = e.Cross( ac );
159
160 if( d == 0 )
161 return false;
162
163 if( !aLines && d > 0 && ( q < 0 || q > d || p < 0 || p > d ) )
164 return false;
165
166 if( !aLines && d < 0 && ( q < d || p < d || p > 0 || q > 0 ) )
167 return false;
168
169 if( !aLines && aIgnoreEndpoints && ( q == 0 || q == d ) && ( p == 0 || p == d ) )
170 return false;
171
172 if( aPt )
173 {
174 VECTOR2<ecoord> result( aSeg.A.x + rescale( q, (ecoord) f.x, d ),
175 aSeg.A.y + rescale( q, (ecoord) f.y, d ) );
176
177 if( abs( result.x ) > std::numeric_limits<VECTOR2I::coord_type>::max()
178 || abs( result.y ) > std::numeric_limits<VECTOR2I::coord_type>::max() )
179 {
180 return false;
181 }
182
183 *aPt = VECTOR2I( (int) result.x, (int) result.y );
184 }
185
186 return true;
187}
188
189
190bool SEG::Intersects( const SEG& aSeg ) const
191{
192 return intersects( aSeg );
193}
194
195
196OPT_VECTOR2I SEG::Intersect( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines ) const
197{
198 VECTOR2I ip;
199
200 if( intersects( aSeg, aIgnoreEndpoints, aLines, &ip ) )
201 return ip;
202 else
203 return OPT_VECTOR2I();
204}
205
206
208{
209 VECTOR2I slope( B - A );
210 VECTOR2I endPoint = slope.Perpendicular() + aP;
211
212 return SEG( aP, endPoint );
213}
214
215
216SEG SEG::ParallelSeg( const VECTOR2I& aP ) const
217{
218 VECTOR2I slope( B - A );
219 VECTOR2I endPoint = slope + aP;
220
221 return SEG( aP, endPoint );
222}
223
224
225bool SEG::ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) const
226{
227 return (ecoord) ( aC.y - aA.y ) * ( aB.x - aA.x ) > (ecoord) ( aB.y - aA.y ) * ( aC.x - aA.x );
228}
229
230
231bool SEG::Collide( const SEG& aSeg, int aClearance, int* aActual ) const
232{
233 // check for intersection
234 // fixme: move to a method
235 if( ccw( A, aSeg.A, aSeg.B ) != ccw( B, aSeg.A, aSeg.B ) &&
236 ccw( A, B, aSeg.A ) != ccw( A, B, aSeg.B ) )
237 {
238 if( aActual )
239 *aActual = 0;
240
241 return true;
242 }
243
245
246 dist_sq = std::min( dist_sq, SquaredDistance( aSeg.A ) );
247 dist_sq = std::min( dist_sq, SquaredDistance( aSeg.B ) );
248 dist_sq = std::min( dist_sq, aSeg.SquaredDistance( A ) );
249 dist_sq = std::min( dist_sq, aSeg.SquaredDistance( B ) );
250
251 if( dist_sq == 0 || dist_sq < (ecoord) aClearance * aClearance )
252 {
253 if( aActual )
254 *aActual = int( isqrt( dist_sq ) );
255
256 return true;
257 }
258
259 return false;
260}
261
262
263bool SEG::Contains( const VECTOR2I& aP ) const
264{
265 return Distance( aP ) <= 1;
266}
267
268
269const VECTOR2I SEG::NearestPoint( const VECTOR2I& aP ) const
270{
271 VECTOR2I d = B - A;
272 ecoord l_squared = d.Dot( d );
273
274 if( l_squared == 0 )
275 return A;
276
277 ecoord t = d.Dot( aP - A );
278
279 if( t < 0 )
280 return A;
281 else if( t > l_squared )
282 return B;
283
284 ecoord xp = rescale( t, (ecoord) d.x, l_squared );
285 ecoord yp = rescale( t, (ecoord) d.y, l_squared );
286
287 return VECTOR2<ecoord>( A.x + xp, A.y + yp );
288}
289
290
291const VECTOR2I SEG::ReflectPoint( const VECTOR2I& aP ) const
292{
293 VECTOR2I d = B - A;
294 VECTOR2I::extended_type l_squared = d.Dot( d );
295 VECTOR2I::extended_type t = d.Dot( aP - A );
297
298 if( !l_squared )
299 {
300 c = aP;
301 }
302 else
303 {
304 c.x = A.x + rescale( t, static_cast<VECTOR2I::extended_type>( d.x ), l_squared );
305 c.y = A.y + rescale( t, static_cast<VECTOR2I::extended_type>( d.y ), l_squared );
306 }
307
308 return VECTOR2<ecoord>( 2 * c.x - aP.x, 2 * c.y - aP.y );
309}
310
311
313{
314 VECTOR2I d = B - A;
315 ecoord l_squared = d.Dot( d );
316
317 if( l_squared == 0 )
318 return A;
319
320 ecoord t = d.Dot( aP - A );
321
322 ecoord xp = rescale( t, ecoord{ d.x }, l_squared );
323 ecoord yp = rescale( t, ecoord{ d.y }, l_squared );
324
325 return VECTOR2<ecoord>( A.x + xp, A.y + yp );
326}
327
328
329int SEG::Distance( const SEG& aSeg ) const
330{
331 return int( isqrt( SquaredDistance( aSeg ) ) );
332}
333
334
335int SEG::Distance( const VECTOR2I& aP ) const
336{
337 return int( isqrt( SquaredDistance( aP ) ) );
338}
339
340
341int SEG::LineDistance( const VECTOR2I& aP, bool aDetermineSide ) const
342{
343 ecoord p = ecoord{ A.y } - B.y;
344 ecoord q = ecoord{ B.x } - A.x;
345 ecoord r = -p * A.x - q * A.y;
346 ecoord l = p * p + q * q;
347 ecoord det = p * aP.x + q * aP.y + r;
348 ecoord dist_sq = 0;
349
350 if( l > 0 )
351 {
352 dist_sq = rescale( det, det, l );
353 }
354
355 ecoord dist = isqrt( dist_sq );
356
357 return static_cast<int>( aDetermineSide ? dist : std::abs( dist ) );
358}
359
360
361bool SEG::mutualDistanceSquared( const SEG& aSeg, ecoord& aD1, ecoord& aD2 ) const
362{
363 SEG a( *this );
364 SEG b( aSeg );
365
366 if( a.SquaredLength() < b.SquaredLength() )
367 std::swap(a, b);
368
369 ecoord p = ecoord{ a.A.y } - a.B.y;
370 ecoord q = ecoord{ a.B.x } - a.A.x;
371 ecoord r = -p * a.A.x - q * a.A.y;
372
373 ecoord l = p * p + q * q;
374
375 if( l == 0 )
376 return false;
377
378 ecoord det1 = p * b.A.x + q * b.A.y + r;
379 ecoord det2 = p * b.B.x + q * b.B.y + r;
380
381 ecoord dsq1 = rescale( det1, det1, l );
382 ecoord dsq2 = rescale( det2, det2, l );
383
384 aD1 = sgn( det1 ) * dsq1;
385 aD2 = sgn( det2 ) * dsq2;
386
387 return true;
388}
389
390bool SEG::ApproxCollinear( const SEG& aSeg, int aDistanceThreshold ) const
391{
392 ecoord thresholdSquared = Square( aDistanceThreshold );
393 ecoord d1_sq, d2_sq;
394
395 if( !mutualDistanceSquared( aSeg, d1_sq, d2_sq ) )
396 return false;
397
398 return std::abs( d1_sq ) <= thresholdSquared && std::abs( d2_sq ) <= thresholdSquared;
399}
400
401
402bool SEG::ApproxParallel( const SEG& aSeg, int aDistanceThreshold ) const
403{
404 ecoord thresholdSquared = Square( aDistanceThreshold );
405 ecoord d1_sq, d2_sq;
406
407 if( ! mutualDistanceSquared( aSeg, d1_sq, d2_sq ) )
408 return false;
409
410 return std::abs( d1_sq - d2_sq ) <= thresholdSquared;
411}
412
413
414bool SEG::ApproxPerpendicular( const SEG& aSeg ) const
415{
416 SEG perp = PerpendicularSeg( A );
417
418 return aSeg.ApproxParallel( perp );
419}
EDA_ANGLE Normalize180()
Definition: eda_angle.h:260
Definition: seg.h:42
const VECTOR2I ReflectPoint(const VECTOR2I &aP) const
Reflect a point using this segment as axis.
Definition: seg.cpp:291
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:341
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:75
bool intersects(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false, VECTOR2I *aPt=nullptr) const
Definition: seg.cpp:150
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:269
bool Intersects(const SEG &aSeg) const
Definition: seg.cpp:190
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:196
static SEG::ecoord Square(int a)
Definition: seg.h:123
bool Collide(const SEG &aSeg, int aClearance, int *aActual=nullptr) const
Definition: seg.cpp:231
bool ApproxParallel(const SEG &aSeg, int aDistanceThreshold=1) const
Definition: seg.cpp:402
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:216
ecoord SquaredLength() const
Definition: seg.h:331
bool ApproxPerpendicular(const SEG &aSeg) const
Definition: seg.cpp:414
bool ApproxCollinear(const SEG &aSeg, int aDistanceThreshold=1) const
Definition: seg.cpp:390
bool mutualDistanceSquared(const SEG &aSeg, ecoord &aD1, ecoord &aD2) const
Definition: seg.cpp:361
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition: seg.cpp:329
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:225
bool Contains(const SEG &aSeg) const
Definition: seg.h:307
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:312
SEG PerpendicularSeg(const VECTOR2I &aP) const
Compute a segment perpendicular to this one, passing through point aP.
Definition: seg.cpp:207
EDA_ANGLE Angle(const SEG &aOther) const
Determine the smallest angle between two segments.
Definition: seg.cpp:97
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:75
VECTOR2_TRAITS< T >::extended_type extended_type
Definition: vector2d.h:72
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition: vector2d.h:309
extended_type Cross(const VECTOR2< T > &aVector) const
Compute cross product of self with aVector.
Definition: vector2d.h:500
extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:508
static constexpr EDA_ANGLE ANGLE_180
Definition: eda_angle.h:405
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
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
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:162
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:638