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
188 const VECTOR2I ReflectPoint( const VECTOR2I& aP ) const;
189
199 OPT_VECTOR2I Intersect( const SEG& aSeg, bool aIgnoreEndpoints = false,
200 bool aLines = false ) const;
201
202 bool Intersects( const SEG& aSeg ) const;
203
210 OPT_VECTOR2I IntersectLines( const SEG& aSeg ) const
211 {
212 return Intersect( aSeg, false, true );
213 }
214
221 SEG PerpendicularSeg( const VECTOR2I& aP ) const;
222
229 SEG ParallelSeg( const VECTOR2I& aP ) const;
230
231 bool Collide( const SEG& aSeg, int aClearance, int* aActual = nullptr ) const;
232
233 ecoord SquaredDistance( const SEG& aSeg ) const;
234
241 int Distance( const SEG& aSeg ) const;
242
243 ecoord SquaredDistance( const VECTOR2I& aP ) const
244 {
245 return ( NearestPoint( aP ) - aP ).SquaredEuclideanNorm();
246 }
247
254 int Distance( const VECTOR2I& aP ) const;
255
256 void CanonicalCoefs( ecoord& qA, ecoord& qB, ecoord& qC ) const
257 {
258 qA = ecoord{ A.y } - B.y;
259 qB = ecoord{ B.x } - A.x;
260 qC = -qA * A.x - qB * A.y;
261 }
262
269 bool Collinear( const SEG& aSeg ) const
270 {
271 ecoord qa, qb, qc;
272 CanonicalCoefs( qa, qb, qc );
273
274 ecoord d1 = std::abs( aSeg.A.x * qa + aSeg.A.y * qb + qc );
275 ecoord d2 = std::abs( aSeg.B.x * qa + aSeg.B.y * qb + qc );
276
277 return ( d1 <= 1 && d2 <= 1 );
278 }
279
280 bool ApproxCollinear( const SEG& aSeg, int aDistanceThreshold = 1 ) const;
281 bool ApproxParallel( const SEG& aSeg, int aDistanceThreshold = 1 ) const;
282 bool ApproxPerpendicular( const SEG& aSeg ) const;
283
284 bool Overlaps( const SEG& aSeg ) const
285 {
286 if( aSeg.A == aSeg.B ) // single point corner case
287 {
288 if( A == aSeg.A || B == aSeg.A )
289 return false;
290
291 return Contains( aSeg.A );
292 }
293
294 if( !Collinear( aSeg ) )
295 return false;
296
297 if( Contains( aSeg.A ) || Contains( aSeg.B ) )
298 return true;
299
300 if( aSeg.Contains( A ) || aSeg.Contains( B ) )
301 return true;
302
303 return false;
304 }
305
306
307 bool Contains( const SEG& aSeg ) const
308 {
309 if( aSeg.A == aSeg.B ) // single point corner case
310 return Contains( aSeg.A );
311
312 if( !Collinear( aSeg ) )
313 return false;
314
315 if( Contains( aSeg.A ) && Contains( aSeg.B ) )
316 return true;
317
318 return false;
319 }
320
326 int Length() const
327 {
328 return ( A - B ).EuclideanNorm();
329 }
330
332 {
333 return ( A - B ).SquaredEuclideanNorm();
334 }
335
336 ecoord TCoef( const VECTOR2I& aP ) const;
337
344 int Index() const
345 {
346 return m_index;
347 }
348
349 bool Contains( const VECTOR2I& aP ) const;
350
351 void Reverse()
352 {
353 std::swap( A, B );
354 }
355
356 SEG Reversed() const
357 {
358 return SEG( B, A );
359 }
360
363 {
364 return A + ( B - A ) / 2;
365 }
366
367 bool operator<( const SEG& aSeg ) const
368 {
369 if( A == aSeg.A )
370 return B < aSeg.B;
371
372 return A < aSeg.A;
373 }
374
375private:
376 bool ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I &aC ) const;
377
378 bool intersects( const SEG& aSeg, bool aIgnoreEndpoints = false, bool aLines = false,
379 VECTOR2I* aPt = nullptr ) const;
380
381 bool mutualDistanceSquared( const SEG& aSeg, ecoord& aD1, ecoord& aD2 ) const;
382
383private:
386};
387
388inline SEG::ecoord SEG::TCoef( const VECTOR2I& aP ) const
389{
390 VECTOR2I d = B - A;
391 return d.Dot( aP - A);
392}
393
394inline std::ostream& operator<<( std::ostream& aStream, const SEG& aSeg )
395{
396 aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";
397
398 return aStream;
399}
400
401#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: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
int m_index
< index within the parent shape (used when m_is_local == false)
Definition: seg.h:385
VECTOR2I::extended_type ecoord
Definition: seg.h:44
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:256
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:394
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:344
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:269
ecoord SquaredDistance(const VECTOR2I &aP) const
Definition: seg.h:243
bool Intersects(const SEG &aSeg) const
Definition: seg.cpp:190
int Length() const
Return the length (this).
Definition: seg.h:326
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:196
static SEG::ecoord Square(int a)
Definition: seg.h:123
VECTOR2I Center() const
Definition: seg.h:362
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
bool Collinear(const SEG &aSeg) const
Check if segment aSeg lies on the same line as (this).
Definition: seg.h:269
SEG ParallelSeg(const VECTOR2I &aP) const
Compute a segment parallel to this one, passing through point aP.
Definition: seg.cpp:216
ecoord TCoef(const VECTOR2I &aP) const
Definition: seg.h:388
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Compute the intersection point of lines passing through ends of (this) and aSeg.
Definition: seg.h:210
ecoord SquaredLength() const
Definition: seg.h:331
bool operator==(const SEG &aSeg) const
Definition: seg.h:113
void Reverse()
Definition: seg.h:351
bool ApproxPerpendicular(const SEG &aSeg) const
Definition: seg.cpp:414
bool ApproxCollinear(const SEG &aSeg, int aDistanceThreshold=1) const
Definition: seg.cpp:390
bool operator<(const SEG &aSeg) const
Definition: seg.h:367
bool Overlaps(const SEG &aSeg) const
Definition: seg.h:284
bool mutualDistanceSquared(const SEG &aSeg, ecoord &aD1, ecoord &aD2) const
Definition: seg.cpp:361
bool operator!=(const SEG &aSeg) const
Definition: seg.h:118
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
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:312
SEG PerpendicularSeg(const VECTOR2I &aP) const
Compute a segment perpendicular to this one, passing through point aP.
Definition: seg.cpp:207
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:356
VECTOR2_TRAITS< int >::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:465
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:424
std::ostream & operator<<(std::ostream &aStream, const SEG &aSeg)
Definition: seg.h:394
std::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:39
VECTOR2I::extended_type ecoord