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
261 int Distance( const VECTOR2I& aP ) const;
262
263 void CanonicalCoefs( ecoord& qA, ecoord& qB, ecoord& qC ) const
264 {
265 qA = ecoord{ A.y } - B.y;
266 qB = ecoord{ B.x } - A.x;
267 qC = -qA * A.x - qB * A.y;
268 }
269
276 bool Collinear( const SEG& aSeg ) const
277 {
278 ecoord qa, qb, qc;
279 CanonicalCoefs( qa, qb, qc );
280
281 ecoord d1 = std::abs( aSeg.A.x * qa + aSeg.A.y * qb + qc );
282 ecoord d2 = std::abs( aSeg.B.x * qa + aSeg.B.y * qb + qc );
283
284 return ( d1 <= 1 && d2 <= 1 );
285 }
286
287 bool ApproxCollinear( const SEG& aSeg, int aDistanceThreshold = 1 ) const;
288 bool ApproxParallel( const SEG& aSeg, int aDistanceThreshold = 1 ) const;
289 bool ApproxPerpendicular( const SEG& aSeg ) const;
290
291 bool Overlaps( const SEG& aSeg ) const
292 {
293 if( aSeg.A == aSeg.B ) // single point corner case
294 {
295 if( A == aSeg.A || B == aSeg.A )
296 return false;
297
298 return Contains( aSeg.A );
299 }
300
301 if( !Collinear( aSeg ) )
302 return false;
303
304 if( Contains( aSeg.A ) || Contains( aSeg.B ) )
305 return true;
306
307 if( aSeg.Contains( A ) || aSeg.Contains( B ) )
308 return true;
309
310 return false;
311 }
312
313
314 bool Contains( const SEG& aSeg ) const
315 {
316 if( aSeg.A == aSeg.B ) // single point corner case
317 return Contains( aSeg.A );
318
319 if( !Collinear( aSeg ) )
320 return false;
321
322 if( Contains( aSeg.A ) && Contains( aSeg.B ) )
323 return true;
324
325 return false;
326 }
327
333 int Length() const
334 {
335 return ( A - B ).EuclideanNorm();
336 }
337
339 {
340 return ( A - B ).SquaredEuclideanNorm();
341 }
342
343 ecoord TCoef( const VECTOR2I& aP ) const;
344
351 int Index() const
352 {
353 return m_index;
354 }
355
356 bool Contains( const VECTOR2I& aP ) const;
357
358 void Reverse()
359 {
360 std::swap( A, B );
361 }
362
363 SEG Reversed() const
364 {
365 return SEG( B, A );
366 }
367
370 {
371 return A + ( B - A ) / 2;
372 }
373
374 bool operator<( const SEG& aSeg ) const
375 {
376 if( A == aSeg.A )
377 return B < aSeg.B;
378
379 return A < aSeg.A;
380 }
381
382private:
383 bool ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I &aC ) const;
384
385 bool intersects( const SEG& aSeg, bool aIgnoreEndpoints = false, bool aLines = false,
386 VECTOR2I* aPt = nullptr ) const;
387
388 bool mutualDistanceSquared( const SEG& aSeg, ecoord& aD1, ecoord& aD2 ) const;
389
390private:
393};
394
395inline SEG::ecoord SEG::TCoef( const VECTOR2I& aP ) const
396{
397 VECTOR2I d = B - A;
398 return d.Dot( aP - A);
399}
400
401inline std::ostream& operator<<( std::ostream& aStream, const SEG& aSeg )
402{
403 aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";
404
405 return aStream;
406}
407
408#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:428
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:392
VECTOR2I::extended_type ecoord
Definition: seg.h:44
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:263
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:401
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:351
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:327
bool Intersects(const SEG &aSeg) const
Definition: seg.cpp:248
int Length() const
Return the length (this).
Definition: seg.h:333
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:369
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:489
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:276
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:395
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:338
bool operator==(const SEG &aSeg) const
Definition: seg.h:113
void Reverse()
Definition: seg.h:358
bool ApproxPerpendicular(const SEG &aSeg) const
Definition: seg.cpp:501
bool ApproxCollinear(const SEG &aSeg, int aDistanceThreshold=1) const
Definition: seg.cpp:477
bool operator<(const SEG &aSeg) const
Definition: seg.h:374
bool Overlaps(const SEG &aSeg) const
Definition: seg.h:291
bool mutualDistanceSquared(const SEG &aSeg, ecoord &aD1, ecoord &aD2) const
Definition: seg.cpp:448
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:314
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:363
VECTOR2_TRAITS< int32_t >::extended_type extended_type
Definition: vector2d.h:73
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:550
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1210
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