KiCad PCB EDA Suite
Loading...
Searching...
No Matches
seg.h
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 * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef __SEG_H
28#define __SEG_H
29
30#include <math.h> // for sqrt
31#include <stdlib.h> // for abs
32#include <optional>
33#include <ostream> // for operator<<, ostream, basic_os...
34#include <type_traits> // for swap
35
36#include <math/vector2d.h>
37#include <geometry/eda_angle.h>
38
39typedef std::optional<VECTOR2I> OPT_VECTOR2I;
40
41class SEG
42{
43public:
45 friend inline std::ostream& operator<<( std::ostream& aStream, const SEG& aSeg );
46
47 /* Start and the of the segment. Public, to make access simpler.
48 */
51
56 {
57 m_index = -1;
58 }
59
63 SEG( int aX1, int aY1, int aX2, int aY2 ) :
64 A( VECTOR2I( aX1, aY1 ) ),
65 B( VECTOR2I( aX2, aY2 ) )
66 {
67 m_index = -1;
68 }
69
73 SEG( const VECTOR2I& aA, const VECTOR2I& aB ) :
74 A( aA ),
75 B( aB )
76 {
77 m_index = -1;
78 }
79
87 SEG( const VECTOR2I& aA, const VECTOR2I& aB, int aIndex ) :
88 A( aA ),
89 B( aB )
90 {
91 m_index = aIndex;
92 }
93
97 SEG( const SEG& aSeg ) :
98 A( aSeg.A ),
99 B( aSeg.B ),
100 m_index( aSeg.m_index )
101 {
102 }
103
104 SEG& operator=( const SEG& aSeg )
105 {
106 A = aSeg.A;
107 B = aSeg.B;
108 m_index = aSeg.m_index;
109
110 return *this;
111 }
112
113 bool operator==( const SEG& aSeg ) const
114 {
115 return (A == aSeg.A && B == aSeg.B) ;
116 }
117
118 bool operator!=( const SEG& aSeg ) const
119 {
120 return (A != aSeg.A || B != aSeg.B);
121 }
122
123 static SEG::ecoord Square( int a )
124 {
125 return ecoord( a ) * a;
126 }
127
135 VECTOR2I LineProject( const VECTOR2I& aP ) const;
136
143 int Side( const VECTOR2I& aP ) const
144 {
145 const ecoord det = ( B - A ).Cross( aP - A );
146
147 return det < 0 ? -1 : ( det > 0 ? 1 : 0 );
148 }
149
159 int LineDistance( const VECTOR2I& aP, bool aDetermineSide = false ) const;
160
167 EDA_ANGLE Angle( const SEG& aOther ) const;
168
174 const VECTOR2I NearestPoint( const VECTOR2I& aP ) const;
175
181 const VECTOR2I NearestPoint( const SEG &aSeg ) const;
182
191 bool NearestPoints( const SEG& aSeg, VECTOR2I& aPtA, VECTOR2I& aPtB, int64_t& aDistSq ) const;
192
198 const VECTOR2I ReflectPoint( const VECTOR2I& aP ) const;
199
209 OPT_VECTOR2I Intersect( const SEG& aSeg, bool aIgnoreEndpoints = false,
210 bool aLines = false ) const;
211
212 bool Intersects( const SEG& aSeg ) const;
213
220 OPT_VECTOR2I IntersectLines( const SEG& aSeg ) const
221 {
222 return Intersect( aSeg, false, true );
223 }
224
231 SEG PerpendicularSeg( const VECTOR2I& aP ) const;
232
239 SEG ParallelSeg( const VECTOR2I& aP ) const;
240
241 bool Collide( const SEG& aSeg, int aClearance, int* aActual = nullptr ) const;
242
243 ecoord SquaredDistance( const SEG& aSeg ) const;
244
251 int Distance( const SEG& aSeg ) const;
252
253 ecoord SquaredDistance( const VECTOR2I& aP ) const
254 {
255 return ( NearestPoint( aP ) - aP ).SquaredEuclideanNorm();
256 }
257
264 int Distance( const VECTOR2I& aP ) const;
265
266 void CanonicalCoefs( ecoord& qA, ecoord& qB, ecoord& qC ) const
267 {
268 qA = ecoord{ A.y } - B.y;
269 qB = ecoord{ B.x } - A.x;
270 qC = -qA * A.x - qB * A.y;
271 }
272
279 bool Collinear( const SEG& aSeg ) const
280 {
281 ecoord qa, qb, qc;
282 CanonicalCoefs( qa, qb, qc );
283
284 ecoord d1 = std::abs( aSeg.A.x * qa + aSeg.A.y * qb + qc );
285 ecoord d2 = std::abs( aSeg.B.x * qa + aSeg.B.y * qb + qc );
286
287 return ( d1 <= 1 && d2 <= 1 );
288 }
289
290 bool ApproxCollinear( const SEG& aSeg, int aDistanceThreshold = 1 ) const;
291 bool ApproxParallel( const SEG& aSeg, int aDistanceThreshold = 1 ) const;
292 bool ApproxPerpendicular( const SEG& aSeg ) const;
293
294 bool Overlaps( const SEG& aSeg ) const
295 {
296 if( aSeg.A == aSeg.B ) // single point corner case
297 {
298 if( A == aSeg.A || B == aSeg.A )
299 return false;
300
301 return Contains( aSeg.A );
302 }
303
304 if( !Collinear( aSeg ) )
305 return false;
306
307 if( Contains( aSeg.A ) || Contains( aSeg.B ) )
308 return true;
309
310 if( aSeg.Contains( A ) || aSeg.Contains( B ) )
311 return true;
312
313 return false;
314 }
315
316
317 bool Contains( const SEG& aSeg ) const
318 {
319 if( aSeg.A == aSeg.B ) // single point corner case
320 return Contains( aSeg.A );
321
322 if( !Collinear( aSeg ) )
323 return false;
324
325 if( Contains( aSeg.A ) && Contains( aSeg.B ) )
326 return true;
327
328 return false;
329 }
330
336 int Length() const
337 {
338 return ( A - B ).EuclideanNorm();
339 }
340
342 {
343 return ( A - B ).SquaredEuclideanNorm();
344 }
345
346 ecoord TCoef( const VECTOR2I& aP ) const;
347
354 int Index() const
355 {
356 return m_index;
357 }
358
359 bool Contains( const VECTOR2I& aP ) const;
360
361 void Reverse()
362 {
363 std::swap( A, B );
364 }
365
366 SEG Reversed() const
367 {
368 return SEG( B, A );
369 }
370
373 {
374 return A + ( B - A ) / 2;
375 }
376
377 bool operator<( const SEG& aSeg ) const
378 {
379 if( A == aSeg.A )
380 return B < aSeg.B;
381
382 return A < aSeg.A;
383 }
384
385private:
386 bool ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I &aC ) const;
387
388 bool intersects( const SEG& aSeg, bool aIgnoreEndpoints = false, bool aLines = false,
389 VECTOR2I* aPt = nullptr ) const;
390
391 bool mutualDistanceSquared( const SEG& aSeg, ecoord& aD1, ecoord& aD2 ) const;
392
393private:
396};
397
398inline SEG::ecoord SEG::TCoef( const VECTOR2I& aP ) const
399{
400 VECTOR2I d = B - A;
401 return d.Dot( aP - A);
402}
403
404inline std::ostream& operator<<( std::ostream& aStream, const SEG& aSeg )
405{
406 aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";
407
408 return aStream;
409}
410
411#endif // __SEG_H
Definition: seg.h:42
const VECTOR2I ReflectPoint(const VECTOR2I &aP) const
Reflect a point using this segment as axis.
Definition: seg.cpp:350
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:400
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:208
int m_index
< index within the parent shape (used when m_is_local == false)
Definition: seg.h:395
VECTOR2I::extended_type ecoord
Definition: seg.h:44
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:266
SEG(const VECTOR2I &aA, const VECTOR2I &aB, int aIndex)
Create a segment between (aA) and (aB), referenced to a multi-segment shape.
Definition: seg.h:87
friend std::ostream & operator<<(std::ostream &aStream, const SEG &aSeg)
Definition: seg.h:404
SEG & operator=(const SEG &aSeg)
Definition: seg.h:104
VECTOR2I B
Definition: seg.h:50
int Index() const
Return the index of this segment in its parent shape (applicable only to non-local segments).
Definition: seg.h:354
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:327
ecoord SquaredDistance(const VECTOR2I &aP) const
Definition: seg.h:253
bool Intersects(const SEG &aSeg) const
Definition: seg.cpp:248
int Length() const
Return the length (this).
Definition: seg.h:336
SEG(int aX1, int aY1, int aX2, int aY2)
Create a segment between (aX1, aY1) and (aX2, aY2).
Definition: seg.h:63
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:254
static SEG::ecoord Square(int a)
Definition: seg.h:123
VECTOR2I Center() const
Definition: seg.h:372
bool Collide(const SEG &aSeg, int aClearance, int *aActual=nullptr) const
Definition: seg.cpp:289
bool ApproxParallel(const SEG &aSeg, int aDistanceThreshold=1) const
Definition: seg.cpp:461
SEG()
Create an empty (0, 0) segment.
Definition: seg.h:55
bool Collinear(const SEG &aSeg) const
Check if segment aSeg lies on the same line as (this).
Definition: seg.h:279
SEG ParallelSeg(const VECTOR2I &aP) const
Compute a segment parallel to this one, passing through point aP.
Definition: seg.cpp:274
ecoord TCoef(const VECTOR2I &aP) const
Definition: seg.h:398
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Compute the intersection point of lines passing through ends of (this) and aSeg.
Definition: seg.h:220
ecoord SquaredLength() const
Definition: seg.h:341
bool operator==(const SEG &aSeg) const
Definition: seg.h:113
void Reverse()
Definition: seg.h:361
bool ApproxPerpendicular(const SEG &aSeg) const
Definition: seg.cpp:473
bool ApproxCollinear(const SEG &aSeg, int aDistanceThreshold=1) const
Definition: seg.cpp:449
bool operator<(const SEG &aSeg) const
Definition: seg.h:377
bool Overlaps(const SEG &aSeg) const
Definition: seg.h:294
bool mutualDistanceSquared(const SEG &aSeg, ecoord &aD1, ecoord &aD2) const
Definition: seg.cpp:420
bool operator!=(const SEG &aSeg) const
Definition: seg.h:118
bool NearestPoints(const SEG &aSeg, VECTOR2I &aPtA, VECTOR2I &aPtB, int64_t &aDistSq) const
Compute closest points between this segment and aSeg.
Definition: seg.cpp:150
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition: seg.cpp:388
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:283
bool Contains(const SEG &aSeg) const
Definition: seg.h:317
SEG(const SEG &aSeg)
Copy constructor.
Definition: seg.h:97
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:371
SEG PerpendicularSeg(const VECTOR2I &aP) const
Compute a segment perpendicular to this one, passing through point aP.
Definition: seg.cpp:265
int Side(const VECTOR2I &aP) const
Determine on which side of directed line passing via segment ends point aP lies.
Definition: seg.h:143
EDA_ANGLE Angle(const SEG &aOther) const
Determine the smallest angle between two segments.
Definition: seg.cpp:97
SEG(const VECTOR2I &aA, const VECTOR2I &aB)
Create a segment between (aA) and (aB).
Definition: seg.h:73
SEG Reversed() const
Returns the center point of the line.
Definition: seg.h:366
VECTOR2_TRAITS< int32_t >::extended_type extended_type
Definition: vector2d.h:72
extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:543
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1170
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
std::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:39
VECTOR2I::extended_type ecoord