KiCad PCB EDA Suite
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 VECTOR2I e( B - A );
153 const VECTOR2I f( aSeg.B - aSeg.A );
154 const VECTOR2I ac( 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 *aPt = VECTOR2I( aSeg.A.x + rescale( q, (ecoord) f.x, d ),
175 aSeg.A.y + rescale( q, (ecoord) f.y, d ) );
176 }
177
178 return true;
179}
180
181
182bool SEG::Intersects( const SEG& aSeg ) const
183{
184 return intersects( aSeg );
185}
186
187
188OPT_VECTOR2I SEG::Intersect( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines ) const
189{
190 VECTOR2I ip;
191
192 if( intersects( aSeg, aIgnoreEndpoints, aLines, &ip ) )
193 return ip;
194 else
195 return OPT_VECTOR2I();
196}
197
198
200{
201 VECTOR2I slope( B - A );
202 VECTOR2I endPoint = slope.Perpendicular() + aP;
203
204 return SEG( aP, endPoint );
205}
206
207
208SEG SEG::ParallelSeg( const VECTOR2I& aP ) const
209{
210 VECTOR2I slope( B - A );
211 VECTOR2I endPoint = slope + aP;
212
213 return SEG( aP, endPoint );
214}
215
216
217bool SEG::ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) const
218{
219 return (ecoord) ( aC.y - aA.y ) * ( aB.x - aA.x ) > (ecoord) ( aB.y - aA.y ) * ( aC.x - aA.x );
220}
221
222
223bool SEG::Collide( const SEG& aSeg, int aClearance, int* aActual ) const
224{
225 // check for intersection
226 // fixme: move to a method
227 if( ccw( A, aSeg.A, aSeg.B ) != ccw( B, aSeg.A, aSeg.B ) &&
228 ccw( A, B, aSeg.A ) != ccw( A, B, aSeg.B ) )
229 {
230 if( aActual )
231 *aActual = 0;
232
233 return true;
234 }
235
237
238 dist_sq = std::min( dist_sq, SquaredDistance( aSeg.A ) );
239 dist_sq = std::min( dist_sq, SquaredDistance( aSeg.B ) );
240 dist_sq = std::min( dist_sq, aSeg.SquaredDistance( A ) );
241 dist_sq = std::min( dist_sq, aSeg.SquaredDistance( B ) );
242
243 if( dist_sq == 0 || dist_sq < (ecoord) aClearance * aClearance )
244 {
245 if( aActual )
246 *aActual = isqrt( dist_sq );
247
248 return true;
249 }
250
251 return false;
252}
253
254
255bool SEG::Contains( const VECTOR2I& aP ) const
256{
257 return Distance( aP ) <= 1;
258}
259
260
261const VECTOR2I SEG::NearestPoint( const VECTOR2I& aP ) const
262{
263 VECTOR2I d = B - A;
264 ecoord l_squared = d.Dot( d );
265
266 if( l_squared == 0 )
267 return A;
268
269 ecoord t = d.Dot( aP - A );
270
271 if( t < 0 )
272 return A;
273 else if( t > l_squared )
274 return B;
275
276 int xp = rescale( t, (ecoord) d.x, l_squared );
277 int yp = rescale( t, (ecoord) d.y, l_squared );
278
279 return A + VECTOR2I( xp, yp );
280}
281
282
283const VECTOR2I SEG::ReflectPoint( const VECTOR2I& aP ) const
284{
285 VECTOR2I d = B - A;
286 VECTOR2I::extended_type l_squared = d.Dot( d );
287 VECTOR2I::extended_type t = d.Dot( aP - A );
288 VECTOR2I c;
289
290 if( !l_squared )
291 c = aP;
292 else
293 {
294 c.x = A.x + rescale( t, static_cast<VECTOR2I::extended_type>( d.x ), l_squared );
295 c.y = A.y + rescale( t, static_cast<VECTOR2I::extended_type>( d.y ), l_squared );
296 }
297
298 return 2 * c - aP;
299}
300
301
303{
304 VECTOR2I d = B - A;
305 ecoord l_squared = d.Dot( d );
306
307 if( l_squared == 0 )
308 return A;
309
310 ecoord t = d.Dot( aP - A );
311
312 int xp = rescale( t, ecoord{ d.x }, l_squared );
313 int yp = rescale( t, ecoord{ d.y }, l_squared );
314
315 return A + VECTOR2I( xp, yp );
316}
317
318
319int SEG::Distance( const SEG& aSeg ) const
320{
321 return isqrt( SquaredDistance( aSeg ) );
322}
323
324
325int SEG::Distance( const VECTOR2I& aP ) const
326{
327 return isqrt( SquaredDistance( aP ) );
328}
329
330
331int SEG::LineDistance( const VECTOR2I& aP, bool aDetermineSide ) const
332{
333 ecoord p = ecoord{ A.y } - B.y;
334 ecoord q = ecoord{ B.x } - A.x;
335 ecoord r = -p * A.x - q * A.y;
336 ecoord l = p * p + q * q;
337 ecoord det = p * aP.x + q * aP.y + r;
338 ecoord dist_sq = 0;
339
340 if( l > 0 )
341 {
342 dist_sq = rescale( det, det, l );
343 }
344
345 ecoord dist = isqrt( dist_sq );
346
347 return aDetermineSide ? dist : std::abs( dist );
348}
349
350
351bool SEG::mutualDistance( const SEG& aSeg, ecoord& aD1, ecoord& aD2 ) const
352{
353 SEG a( *this );
354 SEG b( aSeg );
355
356 if( a.SquaredLength() < b.SquaredLength() )
357 {
358 std::swap(a, b);
359 }
360
361 ecoord p = ecoord{ a.A.y } - a.B.y;
362 ecoord q = ecoord{ a.B.x } - a.A.x;
363 ecoord r = -p * a.A.x - q * a.A.y;
364
365 ecoord l = p * p + q * q;
366
367 if( l == 0 )
368 return false;
369
370 ecoord det1 = p * b.A.x + q * b.A.y + r;
371 ecoord det2 = p * b.B.x + q * b.B.y + r;
372
373 ecoord dsq1 = rescale( det1, det1, l );
374 ecoord dsq2 = rescale( det2, det2, l );
375
376 aD1 = sgn( det1 ) * isqrt( dsq1 );
377 aD2 = sgn( det2 ) * isqrt( dsq2 );
378
379 return true;
380}
381
382bool SEG::ApproxCollinear( const SEG& aSeg, int aDistanceThreshold ) const
383{
384 ecoord d1, d2;
385
386 if( ! mutualDistance( aSeg, d1, d2 ) )
387 return false;
388
389 return std::abs( d1 ) <= aDistanceThreshold && std::abs( d2 ) <= aDistanceThreshold;
390}
391
392
393bool SEG::ApproxParallel( const SEG& aSeg, int aDistanceThreshold ) const
394{
395 ecoord d1, d2;
396
397 if( ! mutualDistance( aSeg, d1, d2 ) )
398 return false;
399
400 return std::abs( d1 - d2 ) <= (ecoord) aDistanceThreshold;
401}
402
403
404bool SEG::ApproxPerpendicular( const SEG& aSeg ) const
405{
406 SEG perp = PerpendicularSeg( A );
407
408 return aSeg.ApproxParallel( perp );
409}
EDA_ANGLE Normalize180()
Definition: eda_angle.h:271
Definition: seg.h:42
const VECTOR2I ReflectPoint(const VECTOR2I &aP) const
Reflect a point using this segment as axis.
Definition: seg.cpp:283
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:331
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
bool mutualDistance(const SEG &aSeg, ecoord &aD1, ecoord &aD2) const
Definition: seg.cpp:351
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:261
bool Intersects(const SEG &aSeg) const
Definition: seg.cpp:182
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:188
bool Collide(const SEG &aSeg, int aClearance, int *aActual=nullptr) const
Definition: seg.cpp:223
bool ApproxParallel(const SEG &aSeg, int aDistanceThreshold=1) const
Definition: seg.cpp:393
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:208
ecoord SquaredLength() const
Definition: seg.h:331
bool ApproxPerpendicular(const SEG &aSeg) const
Definition: seg.cpp:404
bool ApproxCollinear(const SEG &aSeg, int aDistanceThreshold=1) const
Definition: seg.cpp:382
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition: seg.cpp:319
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:217
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:302
SEG PerpendicularSeg(const VECTOR2I &aP) const
Compute a segment perpendicular to this one, passing through point aP.
Definition: seg.cpp:199
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:79
VECTOR2_TRAITS< T >::extended_type extended_type
Definition: vector2d.h:76
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition: vector2d.h:307
extended_type Cross(const VECTOR2< T > &aVector) const
Compute cross product of self with aVector.
Definition: vector2d.h:487
extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:495
static constexpr EDA_ANGLE & ANGLE_180
Definition: eda_angle.h:416
E_SERIE r
Definition: eserie.cpp:41
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:401
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:118
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618