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