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