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 <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 #include <algorithm> // for min
26 #include <geometry/seg.h>
27 #include <math/util.h> // for rescale
28 #include <math/vector2d.h> // for VECTOR2I, VECTOR2
29 
30 template <typename T>
31 int sgn( T aVal )
32 {
33  return ( T( 0 ) < aVal ) - ( aVal < T( 0 ) );
34 }
35 
36 
37 SEG::ecoord SEG::SquaredDistance( const SEG& aSeg ) const
38 {
39  // fixme: rather inefficient....
40  if( Intersect( aSeg ) )
41  return 0;
42 
43  const VECTOR2I pts[4] =
44  {
45  aSeg.NearestPoint( A ) - A,
46  aSeg.NearestPoint( B ) - B,
47  NearestPoint( aSeg.A ) - aSeg.A,
48  NearestPoint( aSeg.B ) - aSeg.B
49  };
50 
52 
53  for( int i = 0; i < 4; i++ )
54  m = std::min( m, pts[i].SquaredEuclideanNorm() );
55 
56  return m;
57 }
58 
59 
60 const VECTOR2I SEG::NearestPoint( const SEG& aSeg ) const
61 {
62  if( auto p = Intersect( aSeg ) )
63  return *p;
64 
65  const VECTOR2I pts_origin[4] =
66  {
67  aSeg.NearestPoint( A ),
68  aSeg.NearestPoint( B ),
69  NearestPoint( aSeg.A ),
70  NearestPoint( aSeg.B )
71  };
72 
73  const ecoord pts_dist[4] =
74  {
75  ( pts_origin[0] - A ).SquaredEuclideanNorm(),
76  ( pts_origin[1] - B ).SquaredEuclideanNorm(),
77  ( pts_origin[2] - aSeg.A ).SquaredEuclideanNorm(),
78  ( pts_origin[3] - aSeg.B ).SquaredEuclideanNorm()
79  };
80 
81  int min_i = 0;
82 
83  for( int i = 0; i < 4; i++ )
84  {
85  if( pts_dist[i] < pts_dist[min_i] )
86  min_i = i;
87  }
88 
89  return pts_origin[min_i];
90 }
91 
92 
93 OPT_VECTOR2I SEG::Intersect( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines ) const
94 {
95  const VECTOR2I e( B - A );
96  const VECTOR2I f( aSeg.B - aSeg.A );
97  const VECTOR2I ac( aSeg.A - A );
98 
99  ecoord d = f.Cross( e );
100  ecoord p = f.Cross( ac );
101  ecoord q = e.Cross( ac );
102 
103  if( d == 0 )
104  return OPT_VECTOR2I();
105 
106  if( !aLines && d > 0 && ( q < 0 || q > d || p < 0 || p > d ) )
107  return OPT_VECTOR2I();
108 
109  if( !aLines && d < 0 && ( q < d || p < d || p > 0 || q > 0 ) )
110  return OPT_VECTOR2I();
111 
112  if( !aLines && aIgnoreEndpoints && ( q == 0 || q == d ) && ( p == 0 || p == d ) )
113  return OPT_VECTOR2I();
114 
115  VECTOR2I ip( aSeg.A.x + rescale( q, (ecoord) f.x, d ),
116  aSeg.A.y + rescale( q, (ecoord) f.y, d ) );
117 
118  return ip;
119 }
120 
121 
122 bool SEG::ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) const
123 {
124  return (ecoord) ( aC.y - aA.y ) * ( aB.x - aA.x ) > (ecoord) ( aB.y - aA.y ) * ( aC.x - aA.x );
125 }
126 
127 
128 bool SEG::Collide( const SEG& aSeg, int aClearance, int* aActual ) const
129 {
130  // check for intersection
131  // fixme: move to a method
132  if( ccw( A, aSeg.A, aSeg.B ) != ccw( B, aSeg.A, aSeg.B ) &&
133  ccw( A, B, aSeg.A ) != ccw( A, B, aSeg.B ) )
134  {
135  if( aActual )
136  *aActual = 0;
137 
138  return true;
139  }
140 
141  ecoord dist_sq = VECTOR2I::ECOORD_MAX;
142 
143  dist_sq = std::min( dist_sq, SquaredDistance( aSeg.A ) );
144  dist_sq = std::min( dist_sq, SquaredDistance( aSeg.B ) );
145  dist_sq = std::min( dist_sq, aSeg.SquaredDistance( A ) );
146  dist_sq = std::min( dist_sq, aSeg.SquaredDistance( B ) );
147 
148  if( dist_sq == 0 || dist_sq < (ecoord) aClearance * aClearance )
149  {
150  if( aActual )
151  *aActual = sqrt( dist_sq );
152 
153  return true;
154  }
155 
156  return false;
157 }
158 
159 
160 bool SEG::Contains( const VECTOR2I& aP ) const
161 {
162  return SquaredDistance( aP ) < 1; // 1 * 1 to be pedantic
163 }
extended_type Cross(const VECTOR2< T > &aVector) const
Function Cross() computes cross product of self with aVector.
Definition: vector2d.h:484
bool ccw(const VECTOR2I &aA, const VECTOR2I &aB, const VECTOR2I &aC) const
Definition: seg.cpp:122
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:93
bool Collide(const SEG &aSeg, int aClearance, int *aActual=nullptr) const
Definition: seg.cpp:128
VECTOR2I::extended_type ecoord
Definition: seg.h:42
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:37
T
enum T contains all this lexer's tokens.
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:80
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:37
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:395
int sgn(T aVal)
Definition: seg.cpp:31
Definition: seg.h:39
VECTOR2I A
Definition: seg.h:47
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:95
bool Contains(const SEG &aSeg) const
Definition: seg.h:299
VECTOR2I B
Definition: seg.h:48