KiCad PCB EDA Suite
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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef __SEG_H
26 #define __SEG_H
27 
28 #include <cstdio>
29 #include <climits>
30 
31 #include <math/vector2d.h>
32 #include <core/optional.h>
33 
35 
36 class SEG
37 {
38 private:
40 
41 public:
42  friend inline std::ostream& operator<<( std::ostream& aStream, const SEG& aSeg );
43 
44  /* Start and the of the segment. Public, to make access simpler.
45  */
48 
52  SEG()
53  {
54  m_index = -1;
55  }
56 
61  SEG( int aX1, int aY1, int aX2, int aY2 ) :
62  A ( VECTOR2I( aX1, aY1 ) ),
63  B ( VECTOR2I( aX2, aY2 ) )
64  {
65  m_index = -1;
66  }
67 
72  SEG( const VECTOR2I& aA, const VECTOR2I& aB ) : A( aA ), B( aB )
73  {
74  m_index = -1;
75  }
76 
84  SEG( const VECTOR2I& aA, const VECTOR2I& aB, int aIndex ) : A( aA ), B( aB )
85  {
86  m_index = aIndex;
87  }
88 
92  SEG( const SEG& aSeg ) : A( aSeg.A ), B( aSeg.B ), m_index( aSeg.m_index )
93  {
94  }
95 
96  SEG& operator=( const SEG& aSeg )
97  {
98  A = aSeg.A;
99  B = aSeg.B;
100  m_index = aSeg.m_index;
101 
102  return *this;
103  }
104 
113  VECTOR2I LineProject( const VECTOR2I& aP ) const;
114 
122  int Side( const VECTOR2I& aP ) const
123  {
124  const ecoord det = ( B - A ).Cross( aP - A );
125 
126  return det < 0 ? -1 : ( det > 0 ? 1 : 0 );
127  }
128 
139  int LineDistance( const VECTOR2I& aP, bool aDetermineSide = false ) const;
140 
147  const VECTOR2I NearestPoint( const VECTOR2I &aP ) const;
148 
159  OPT_VECTOR2I Intersect( const SEG& aSeg, bool aIgnoreEndpoints = false,
160  bool aLines = false ) const;
161 
169  OPT_VECTOR2I IntersectLines( const SEG& aSeg ) const
170  {
171  return Intersect( aSeg, false, true );
172  }
173 
174  bool Collide( const SEG& aSeg, int aClearance ) const;
175 
176  ecoord SquaredDistance( const SEG& aSeg ) const;
177 
185  int Distance( const SEG& aSeg ) const
186  {
187  return sqrt( SquaredDistance( aSeg ) );
188  }
189 
190  ecoord SquaredDistance( const VECTOR2I& aP ) const
191  {
192  return ( NearestPoint( aP ) - aP ).SquaredEuclideanNorm();
193  }
194 
202  int Distance( const VECTOR2I& aP ) const
203  {
204  return sqrt( SquaredDistance( aP ) );
205  }
206 
207  void CanonicalCoefs( ecoord& qA, ecoord& qB, ecoord& qC ) const
208  {
209  qA = A.y - B.y;
210  qB = B.x - A.x;
211  qC = -qA * A.x - qB * A.y;
212  }
213 
221  bool Collinear( const SEG& aSeg ) const
222  {
223  ecoord qa, qb, qc;
224  CanonicalCoefs( qa, qb, qc );
225 
226  ecoord d1 = std::abs( aSeg.A.x * qa + aSeg.A.y * qb + qc );
227  ecoord d2 = std::abs( aSeg.B.x * qa + aSeg.B.y * qb + qc );
228 
229  return ( d1 <= 1 && d2 <= 1 );
230  }
231 
232  bool ApproxCollinear( const SEG& aSeg ) const
233  {
234  ecoord p, q, r;
235  CanonicalCoefs( p, q, r );
236 
237  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
238  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
239 
240  return std::abs( dist1 ) <= 1 && std::abs( dist2 ) <= 1;
241  }
242 
243  bool ApproxParallel ( const SEG& aSeg ) const
244  {
245  ecoord p, q, r;
246  CanonicalCoefs( p, q, r );
247 
248  ecoord dist1 = ( p * aSeg.A.x + q * aSeg.A.y + r ) / sqrt( p * p + q * q );
249  ecoord dist2 = ( p * aSeg.B.x + q * aSeg.B.y + r ) / sqrt( p * p + q * q );
250 
251  return std::abs( dist1 - dist2 ) <= 1;
252  }
253 
254 
255  bool Overlaps( const SEG& aSeg ) const
256  {
257  if( aSeg.A == aSeg.B ) // single point corner case
258  {
259  if( A == aSeg.A || B == aSeg.A )
260  return false;
261 
262  return Contains( aSeg.A );
263  }
264 
265  if( !Collinear( aSeg ) )
266  return false;
267 
268  if( Contains( aSeg.A ) || Contains( aSeg.B ) )
269  return true;
270  if( aSeg.Contains( A ) || aSeg.Contains( B ) )
271  return true;
272 
273  return false;
274  }
275 
282  int Length() const
283  {
284  return ( A - B ).EuclideanNorm();
285  }
286 
287  ecoord SquaredLength() const
288  {
289  return ( A - B ).SquaredEuclideanNorm();
290  }
291 
292  ecoord TCoef( const VECTOR2I& aP ) const;
293 
300  int Index() const
301  {
302  return m_index;
303  }
304 
305  bool Contains( const VECTOR2I& aP ) const;
306 
307  bool PointCloserThan( const VECTOR2I& aP, int aDist ) const;
308 
309  void Reverse()
310  {
311  std::swap( A, B );
312  }
313 
314 private:
315  bool ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I &aC ) const;
316 
318  int m_index;
319 };
320 
321 inline VECTOR2I SEG::LineProject( const VECTOR2I& aP ) const
322 {
323  VECTOR2I d = B - A;
324  ecoord l_squared = d.Dot( d );
325 
326  if( l_squared == 0 )
327  return A;
328 
329  ecoord t = d.Dot( aP - A );
330 
331  int xp = rescale( t, (ecoord)d.x, l_squared );
332  int yp = rescale( t, (ecoord)d.y, l_squared );
333 
334  return A + VECTOR2I( xp, yp );
335 }
336 
337 inline int SEG::LineDistance( const VECTOR2I& aP, bool aDetermineSide ) const
338 {
339  ecoord p = A.y - B.y;
340  ecoord q = B.x - A.x;
341  ecoord r = -p * A.x - q * A.y;
342 
343  ecoord dist = ( p * aP.x + q * aP.y + r ) / sqrt( p * p + q * q );
344 
345  return aDetermineSide ? dist : std::abs( dist );
346 }
347 
348 inline SEG::ecoord SEG::TCoef( const VECTOR2I& aP ) const
349 {
350  VECTOR2I d = B - A;
351  return d.Dot( aP - A);
352 }
353 
354 inline const VECTOR2I SEG::NearestPoint( const VECTOR2I& aP ) const
355 {
356  VECTOR2I d = B - A;
357  ecoord l_squared = d.Dot( d );
358 
359  if( l_squared == 0 )
360  return A;
361 
362  ecoord t = d.Dot( aP - A );
363 
364  if( t < 0 )
365  return A;
366  else if( t > l_squared )
367  return B;
368 
369  int xp = rescale( t, (ecoord)d.x, l_squared );
370  int yp = rescale( t, (ecoord)d.y, l_squared );
371 
372  return A + VECTOR2I( xp, yp );
373 }
374 
375 inline std::ostream& operator<<( std::ostream& aStream, const SEG& aSeg )
376 {
377  aStream << "[ " << aSeg.A << " - " << aSeg.B << " ]";
378 
379  return aStream;
380 }
381 
382 #endif // __SEG_H
int Index() const
Function Index()
Definition: seg.h:300
void CanonicalCoefs(ecoord &qA, ecoord &qB, ecoord &qC) const
Definition: seg.h:207
VECTOR2_TRAITS< T >::extended_type extended_type
Definition: vector2d.h:77
ecoord SquaredLength() const
Definition: seg.h:287
extended_type Dot(const VECTOR2< T > &aVector) const
Function Dot() computes dot product of self with aVector.
Definition: vector2d.h:487
int Side(const VECTOR2I &aP) const
Function Side()
Definition: seg.h:122
int Length() const
Function Length()
Definition: seg.h:282
SEG()
Default constructor Creates an empty (0, 0) segment.
Definition: seg.h:52
VECTOR2I LineProject(const VECTOR2I &aP) const
Function LineProject()
Definition: seg.h:321
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:169
std::ostream & operator<<(std::ostream &aStream, const SEG &aSeg)
Definition: seg.h:375
static const int dist[10][10]
Definition: dist.cpp:57
friend std::ostream & operator<<(std::ostream &aStream, const SEG &aSeg)
Definition: seg.h:375
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:99
VECTOR2< int > VECTOR2I
Definition: vector2d.h:589
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:76
#define abs(a)
Definition: auxiliary.h:84
bool ApproxCollinear(const SEG &aSeg) const
Definition: seg.h:232
SEG & operator=(const SEG &aSeg)
Definition: seg.h:96
ecoord TCoef(const VECTOR2I &aP) const
Definition: seg.h:348
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:128
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:34
SEG(const VECTOR2I &aA, const VECTOR2I &aB)
Constructor Creates a segment between (aA) and (aB)
Definition: seg.h:72
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Function LineDistance()
Definition: seg.h:337
SEG(int aX1, int aY1, int aX2, int aY2)
Constructor Creates a segment between (aX1, aY1) and (aX2, aY2)
Definition: seg.h:61
SEG(const SEG &aSeg)
Copy constructor.
Definition: seg.h:92
ecoord SquaredDistance(const VECTOR2I &aP) const
Definition: seg.h:190
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: math_util.cpp:32
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:354
Definition: seg.h:36
void Reverse()
Definition: seg.h:309
bool Overlaps(const SEG &aSeg) const
Definition: seg.h:255
VECTOR2I A
Definition: seg.h:46
int m_index
index withing the parent shape (used when m_is_local == false)
Definition: seg.h:318
int Distance(const VECTOR2I &aP) const
Function Distance()
Definition: seg.h:202
boost::optional< T > OPT
Definition: optional.h:7
VECTOR2I::extended_type ecoord
Definition: seg.h:39
bool PointCloserThan(const VECTOR2I &aP, int aDist) const
Definition: seg.cpp:34
bool Collide(const SEG &aSeg, int aClearance) const
Definition: seg.cpp:134
bool Collinear(const SEG &aSeg) const
Function Collinear()
Definition: seg.h:221
bool ApproxParallel(const SEG &aSeg) const
Definition: seg.h:243
bool Contains(const VECTOR2I &aP) const
Definition: seg.cpp:155
int Distance(const SEG &aSeg) const
Function Distance()
Definition: seg.h:185
double Cross(const Point &a, const Point &b)
Perform the cross product on two vectors. In 2D this produces a scalar.
Definition: shapes.h:287
SEG(const VECTOR2I &aA, const VECTOR2I &aB, int aIndex)
Constructor Creates a segment between (aA) and (aB), referenced to a multi-segment shape...
Definition: seg.h:84
VECTOR2I B
Definition: seg.h:47