KiCad PCB EDA Suite
ray.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) 2015-2017 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 2015-2021 KiCad Developers, see AUTHORS.txt for contributors.
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 
31 #include "ray.h"
32 #include "../../3d_fastmath.h"
33 #include <cstdio>
34 #include <wx/debug.h>
35 #include <wx/log.h>
36 
37 #include <cmath>
38 
39 //static unsigned int gs_next_rayID = 0;
40 
41 void RAY::Init( const SFVEC3F& o, const SFVEC3F& d )
42 {
43  m_Origin = o;
44  m_Dir = d;
45  m_InvDir = 1.0f / d;
46 
47  rayID = 0; // Not used, just set to 0
48  //rayID = gs_next_rayID;
49  //gs_next_rayID++;
50 
51  // An Efficient and Robust Ray–Box Intersection Algorithm
52  // Amy Williams Steve Barrus R. Keith Morley Peter Shirley
53  // University of Utah
54  // http://people.csail.mit.edu/amy/papers/box-jgt.pdf
55  m_dirIsNeg[0] = m_Dir.x < 0.0f;
56  m_dirIsNeg[1] = m_Dir.y < 0.0f;
57  m_dirIsNeg[2] = m_Dir.z < 0.0f;
58 
59  // ray slope
60 
61  // "Fast Ray / Axis-Aligned Bounding Box Overlap Tests using Ray Slopes"
62  // by Martin Eisemann, Thorsten Grosch, Stefan Müller and Marcus Magnor
63  // Computer Graphics Lab, TU Braunschweig, Germany and
64  // University of Koblenz-Landau, Germany
65  // Licence: "This source code is public domain, but please mention us if you use it."
66  //
67  // https://github.com/rjw57/mcvoxel/tree/master/third-party/rayslope
68  // https://github.com/rjw57/mcvoxel/blob/master/third-party/rayslope/ray.cpp
69 
70  ibyj = m_Dir.x * m_InvDir.y;
71  jbyi = m_Dir.y * m_InvDir.x;
72  jbyk = m_Dir.y * m_InvDir.z;
73  kbyj = m_Dir.z * m_InvDir.y;
74  ibyk = m_Dir.x * m_InvDir.z;
75  kbyi = m_Dir.z * m_InvDir.x;
76  c_xy = m_Origin.y - jbyi * m_Origin.x;
77  c_xz = m_Origin.z - kbyi * m_Origin.x;
78  c_yx = m_Origin.x - ibyj * m_Origin.y;
79  c_yz = m_Origin.z - kbyj * m_Origin.y;
80  c_zx = m_Origin.x - ibyk * m_Origin.z;
81  c_zy = m_Origin.y - jbyk * m_Origin.z;
82 
83  // ray slope classification
84  if( m_Dir.x < 0 )
85  {
86  if( m_Dir.y < 0 )
87  {
88  if( m_Dir.z < 0 )
89  {
91  }
92  else if( m_Dir.z > 0 )
93  {
95  }
96  else
97  {
99  }
100  }
101  else
102  {
103  if( m_Dir.z < 0 )
104  {
106 
107  if( m_Dir.y == 0 )
109  }
110  else
111  {
112  if( ( m_Dir.y == 0 ) && ( m_Dir.z == 0 ) )
114  else if( m_Dir.z == 0 )
116  else if( m_Dir.y == 0 )
118  else
120  }
121  }
122  }
123  else
124  {
125  if( m_Dir.y < 0 )
126  {
127  if( m_Dir.z < 0 )
128  {
130 
131  if( m_Dir.x == 0 )
133  }
134  else
135  {
136  if( ( m_Dir.x == 0 ) && ( m_Dir.z == 0 ) )
138  else if( m_Dir.z == 0 )
140  else if( m_Dir.x == 0 )
142  else
144  }
145  }
146  else
147  {
148  if( m_Dir.z < 0 )
149  {
150  if( ( m_Dir.x == 0 ) && ( m_Dir.y == 0 ) )
152  else if( m_Dir.x == 0 )
154  else if( m_Dir.y == 0 )
156  else
158  }
159  else
160  {
161  if( m_Dir.x == 0 )
162  {
163  if( m_Dir.y == 0 )
165  else if( m_Dir.z == 0 )
167  else
169  }
170  else
171  {
172  if( ( m_Dir.y == 0 ) && ( m_Dir.z == 0 ) )
174  else if( m_Dir.y == 0 )
176  else if( m_Dir.z == 0 )
178  else
180  }
181  }
182  }
183  }
184 }
185 
186 
187 bool IntersectSegment( const SFVEC2F &aStartA, const SFVEC2F &aEnd_minus_startA,
188  const SFVEC2F &aStartB, const SFVEC2F &aEnd_minus_startB )
189 {
190  float rxs = aEnd_minus_startA.x * aEnd_minus_startB.y - aEnd_minus_startA.y *
191  aEnd_minus_startB.x;
192 
193  if( std::abs( rxs ) > glm::epsilon<float>() )
194  {
195  float inv_rxs = 1.0f / rxs;
196 
197  SFVEC2F pq = aStartB - aStartA;
198 
199  float t = ( pq.x * aEnd_minus_startB.y - pq.y * aEnd_minus_startB.x ) * inv_rxs;
200 
201  if( ( t < 0.0f ) || ( t > 1.0f ) )
202  return false;
203 
204  float u = ( pq.x * aEnd_minus_startA.y - pq.y * aEnd_minus_startA.x ) * inv_rxs;
205 
206  if( ( u < 0.0f ) || ( u > 1.0f ) )
207  return false;
208 
209  return true;
210  }
211 
212  return false;
213 }
214 
215 
217 bool RAY::IntersectSphere( const SFVEC3F &aCenter, float aRadius, float &aOutT0,
218  float &aOutT1 ) const
219 {
220  // Ray-sphere intersection: geometric
221  SFVEC3F OC = aCenter - m_Origin;
222  float p_dot_d = glm::dot( OC, m_Dir );
223 
224  if( p_dot_d < 0.0f )
225  return 0.0f;
226 
227  float p_dot_p = glm::dot( OC, OC );
228  float discriminant = p_dot_p - p_dot_d * p_dot_d;
229 
230  if( discriminant > aRadius*aRadius )
231  return false;
232 
233  discriminant = sqrtf( aRadius*aRadius - discriminant );
234 
235  aOutT0 = p_dot_d - discriminant;
236  aOutT1 = p_dot_d + discriminant;
237 
238  if( aOutT0 > aOutT1 )
239  {
240  float temp = aOutT0;
241  aOutT0 = aOutT1;
242  aOutT1 = temp;
243  }
244 
245  return true;
246 }
247 
248 
249 RAYSEG2D::RAYSEG2D( const SFVEC2F& s, const SFVEC2F& e )
250 {
251  m_Start = s;
252  m_End = e;
253  m_End_minus_start = e - s;
254  m_Length = glm::length( m_End_minus_start );
255  m_Dir = glm::normalize( m_End_minus_start );
256  m_InvDir = ( 1.0f / m_Dir );
257 
258  if( fabs( m_Dir.x ) < FLT_EPSILON )
259  m_InvDir.x = NextFloatDown( FLT_MAX );
260 
261  if( fabs( m_Dir.y ) < FLT_EPSILON )
262  m_InvDir.y = NextFloatDown( FLT_MAX );
263 
265 }
266 
267 
268 bool RAYSEG2D::IntersectSegment( const SFVEC2F &aStart, const SFVEC2F &aEnd_minus_start,
269  float *aOutT ) const
270 {
271  float rxs = m_End_minus_start.x * aEnd_minus_start.y - m_End_minus_start.y *
272  aEnd_minus_start.x;
273 
274  if( std::abs( rxs ) > glm::epsilon<float>() )
275  {
276  const float inv_rxs = 1.0f / rxs;
277 
278  const SFVEC2F pq = aStart - m_Start;
279 
280  const float t = ( pq.x * aEnd_minus_start.y - pq.y * aEnd_minus_start.x ) * inv_rxs;
281 
282  if( ( t < 0.0f ) || ( t > 1.0f ) )
283  return false;
284 
285  float u = ( pq.x * m_End_minus_start.y - pq.y * m_End_minus_start.x ) * inv_rxs;
286 
287  if( ( u < 0.0f ) || ( u > 1.0f ) )
288  return false;
289 
290  *aOutT = t;
291 
292  return true;
293  }
294 
295  return false;
296 }
297 
298 
299 // http://geomalgorithms.com/a02-_lines.html
300 float RAYSEG2D::DistanceToPointSquared( const SFVEC2F &aPoint ) const
301 {
302  SFVEC2F p = aPoint - m_Start;
303 
304  const float c1 = glm::dot( p, m_End_minus_start );
305 
306  if( c1 < FLT_EPSILON )
307  return glm::dot( p, p );
308 
309  if( m_DOT_End_minus_start <= c1 )
310  {
311  p = aPoint - m_End;
312  }
313  else
314  {
315  const float b = c1 / m_DOT_End_minus_start;
316  const SFVEC2F pb = m_Start + m_End_minus_start * b;
317 
318  p = aPoint - pb;
319  }
320 
321  return glm::dot( p, p );
322 }
323 
324 
325 bool RAYSEG2D::IntersectCircle( const SFVEC2F &aCenter, float aRadius, float *aOutT0,
326  float *aOutT1, SFVEC2F *aOutNormalT0, SFVEC2F *aOutNormalT1 ) const
327 {
328  // This code used directly from Steve Marschner's CS667 framework
329  // http://cs665pd.googlecode.com/svn/trunk/photon/sphere.cpp
330 
331  // Compute some factors used in computation
332  const float qx = m_Start.x - aCenter.x;
333  const float qy = m_Start.y - aCenter.y;
334 
335  const float qd = qx * m_Dir.x + qy * m_Dir.y;
336  const float qq = qx * qx + qy * qy;
337 
338  // solving the quadratic equation for t at the pts of intersection
339  // dd*t^2 + (2*qd)*t + (qq-r^2) = 0
340  const float discriminantsqr = (qd * qd - (qq - aRadius * aRadius));
341 
342  // If the discriminant is less than zero, there is no intersection
343  if( discriminantsqr < FLT_EPSILON )
344  return false;
345 
346  // Otherwise check and make sure that the intersections occur on the ray (t
347  // > 0) and return the closer one
348  const float discriminant = std::sqrt( discriminantsqr );
349  const float t1 = ( -qd - discriminant );
350  const float t2 = ( -qd + discriminant );
351 
352  if( ( ( t1 < 0.0f ) || ( t1 > m_Length ) ) && ( ( t2 < 0.0f ) || ( t2 > m_Length ) ) )
353  return false; // Neither intersection was in the ray's half line.
354 
355  // Convert the intersection to a normalized
356  *aOutT0 = t1 / m_Length;
357  *aOutT1 = t2 / m_Length;
358 
359  SFVEC2F hitPointT1 = at( t1 );
360  SFVEC2F hitPointT2 = at( t2 );
361 
362  *aOutNormalT0 = ( hitPointT1 - aCenter ) / aRadius;
363  *aOutNormalT1 = ( hitPointT2 - aCenter ) / aRadius;
364 
365  return true;
366 }
float c_xy
Definition: ray.h:78
void Init(const SFVEC3F &o, const SFVEC3F &d)
Definition: ray.cpp:41
bool IntersectSphere(const SFVEC3F &aCenter, float aRadius, float &aOutT0, float &aOutT1) const
Definition: ray.cpp:217
bool IntersectCircle(const SFVEC2F &aCenter, float aRadius, float *aOutT0, float *aOutT1, SFVEC2F *aOutNormalT0, SFVEC2F *aOutNormalT1) const
Definition: ray.cpp:325
float c_zy
Definition: ray.h:78
SFVEC2F m_Dir
Definition: ray.h:115
float jbyk
Definition: ray.h:77
float kbyi
Definition: ray.h:77
float c_yx
Definition: ray.h:78
SFVEC2F at(float t) const
Definition: ray.h:142
SFVEC3F m_InvDir
Definition: ray.h:75
float kbyj
Definition: ray.h:77
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
float NextFloatDown(float v)
Definition: 3d_fastmath.h:157
RAY_CLASSIFICATION m_Classification
Definition: ray.h:73
bool IntersectSegment(const SFVEC2F &aStart, const SFVEC2F &aEnd_minus_start, float *aOutT) const
Definition: ray.cpp:268
unsigned int m_dirIsNeg[3]
Definition: ray.h:80
float m_DOT_End_minus_start
dot( m_End_minus_start, m_End_minus_start)
Definition: ray.h:118
float m_Length
Definition: ray.h:117
SFVEC2F m_End
Definition: ray.h:113
unsigned int rayID
unique ray ID - not used - dummy
Definition: ray.h:70
SFVEC2F m_InvDir
Definition: ray.h:116
bool IntersectSegment(const SFVEC2F &aStartA, const SFVEC2F &aEnd_minus_startA, const SFVEC2F &aStartB, const SFVEC2F &aEnd_minus_startB)
Definition: ray.cpp:187
SFVEC3F m_Dir
Definition: ray.h:72
float ibyj
Definition: ray.h:77
float jbyi
Definition: ray.h:77
float c_yz
Definition: ray.h:78
SFVEC3F m_Origin
Definition: ray.h:69
SFVEC2F m_Start
Definition: ray.h:112
float c_zx
Definition: ray.h:78
RAYSEG2D(const SFVEC2F &s, const SFVEC2F &e)
Definition: ray.cpp:249
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
float DistanceToPointSquared(const SFVEC2F &aPoint) const
Definition: ray.cpp:300
float c_xz
Definition: ray.h:78
SFVEC2F m_End_minus_start
Definition: ray.h:114
float ibyk
Definition: ray.h:77