TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Ray.h
Go to the documentation of this file.
1 
12 #ifndef G3D_Ray_h
13 #define G3D_Ray_h
14 
15 #include "G3D/platform.h"
16 #include "G3D/Vector3.h"
17 #include "G3D/Triangle.h"
18 
19 namespace G3D {
20 
24 class Ray {
25 private:
26  friend class Intersect;
27 
29 
32 
35 
36 
43 
45 
47  float ibyj, jbyi, kbyj, jbyk, ibyk, kbyi;
48 
50  float c_xy, c_xz, c_yx, c_yz, c_zx, c_zy;
51 
52 public:
54  void set(const Point3& origin, const Vector3& direction);
55 
56  const Point3& origin() const {
57  return m_origin;
58  }
59 
61  const Vector3& direction() const {
62  return m_direction;
63  }
64 
66  const Vector3& invDirection() const {
67  return m_invDirection;
68  }
69 
70  Ray() {
72  }
73 
75  Ray(const Point3& origin, const Vector3& direction) {
76  set(origin, direction);
77  }
78 
79  Ray(class BinaryInput& b);
80 
81  void serialize(class BinaryOutput& b) const;
82  void deserialize(class BinaryInput& b);
83 
87  static Ray fromOriginAndDirection(const Point3& point, const Vector3& direction) {
88  return Ray(point, direction);
89  }
90 
93  Ray bumpedRay(float distance) const {
94  return Ray(m_origin + m_direction * distance, m_direction);
95  }
96 
99  Ray bumpedRay(float distance, const Vector3& bumpDirection) const {
100  return Ray(m_origin + bumpDirection * distance, m_direction);
101  }
102 
106  Point3 closestPoint(const Point3& point) const {
107  float t = m_direction.dot(point - m_origin);
108  if (t < 0) {
109  return m_origin;
110  } else {
111  return m_origin + m_direction * t;
112  }
113  }
114 
118  float distance(const Point3& point) const {
119  return (closestPoint(point) - point).magnitude();
120  }
121 
129  Point3 intersection(const class Plane& plane) const;
130 
145  float intersectionTime(const class Sphere& sphere, bool solid = false) const;
146 
147  float intersectionTime(const class Plane& plane) const;
148 
149  float intersectionTime(const class Box& box) const;
150 
151  float intersectionTime(const class AABox& box) const;
152 
158  float intersectionTime(
159  const Vector3& v0, const Vector3& v1, const Vector3& v2,
160  const Vector3& edge01, const Vector3& edge02,
161  float& w0, float& w1, float& w2) const;
162 
168  float intersectionTime(
169  const Point3& vert0,
170  const Point3& vert1,
171  const Point3& vert2,
172  const Vector3& edge01,
173  const Vector3& edge02) const;
174 
175 
177  const Point3& vert0,
178  const Point3& vert1,
179  const Point3& vert2) const {
180 
181  return intersectionTime(vert0, vert1, vert2, vert1 - vert0, vert2 - vert0);
182  }
183 
184 
186  const Point3& vert0,
187  const Point3& vert1,
188  const Point3& vert2,
189  float& w0,
190  float& w1,
191  float& w2) const {
192 
193  return intersectionTime(vert0, vert1, vert2, vert1 - vert0, vert2 - vert0, w0, w1, w2);
194  }
195 
196 
197  /* One-sided triangle
198  */
199  float intersectionTime(const Triangle& triangle) const {
200  return intersectionTime(
201  triangle.vertex(0), triangle.vertex(1), triangle.vertex(2),
202  triangle.edge01(), triangle.edge02());
203  }
204 
205 
206  float intersectionTime
208  float& w0,
209  float& w1,
210  float& w2) const {
211  return intersectionTime(triangle.vertex(0), triangle.vertex(1), triangle.vertex(2),
212  triangle.edge01(), triangle.edge02(), w0, w1, w2);
213  }
214 
218  Ray refract(
219  const Vector3& newOrigin,
220  const Vector3& normal,
221  float iInside,
222  float iOutside) const;
223 
228  Ray reflect(
229  const Vector3& newOrigin,
230  const Vector3& normal) const;
231 };
232 
233 
234 #define EPSILON 0.000001
235 #define CROSS(dest,v1,v2) \
236  dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
237  dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
238  dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
239 
240 #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
241 
242 #define SUB(dest,v1,v2) \
243  dest[0]=v1[0]-v2[0]; \
244  dest[1]=v1[1]-v2[1]; \
245  dest[2]=v1[2]-v2[2];
246 
248  const Point3& vert0,
249  const Point3& vert1,
250  const Point3& vert2,
251  const Vector3& edge1,
252  const Vector3& edge2) const {
253 
254  (void)vert1;
255  (void)vert2;
256 
257  // Barycenteric coords
258  float u, v;
259 
260  float tvec[3], pvec[3], qvec[3];
261 
262  // begin calculating determinant - also used to calculate U parameter
263  CROSS(pvec, m_direction, edge2);
264 
265  // if determinant is near zero, ray lies in plane of triangle
266  const float det = DOT(edge1, pvec);
267 
268  if (det < EPSILON) {
269  return finf();
270  }
271 
272  // calculate distance from vert0 to ray origin
273  SUB(tvec, m_origin, vert0);
274 
275  // calculate U parameter and test bounds
276  u = DOT(tvec, pvec);
277  if ((u < 0.0f) || (u > det)) {
278  // Hit the plane outside the triangle
279  return finf();
280  }
281 
282  // prepare to test V parameter
283  CROSS(qvec, tvec, edge1);
284 
285  // calculate V parameter and test bounds
286  v = DOT(m_direction, qvec);
287  if ((v < 0.0f) || (u + v > det)) {
288  // Hit the plane outside the triangle
289  return finf();
290  }
291 
292 
293  // Case where we don't need correct (u, v):
294  const float t = DOT(edge2, qvec);
295 
296  if (t >= 0.0f) {
297  // Note that det must be positive
298  return t / det;
299  } else {
300  // We had to travel backwards in time to intersect
301  return finf();
302  }
303 }
304 
305 
306 inline float Ray::intersectionTime
307 (const Point3& vert0,
308  const Point3& vert1,
309  const Point3& vert2,
310  const Vector3& edge1,
311  const Vector3& edge2,
312  float& w0,
313  float& w1,
314  float& w2) const {
315 
316  (void)vert1;
317  (void)vert2;
318 
319  // Barycenteric coords
320  float u, v;
321 
322  float tvec[3], pvec[3], qvec[3];
323 
324  // begin calculating determinant - also used to calculate U parameter
325  CROSS(pvec, m_direction, edge2);
326 
327  // if determinant is near zero, ray lies in plane of triangle
328  const float det = DOT(edge1, pvec);
329 
330  if (det < EPSILON) {
331  return finf();
332  }
333 
334  // calculate distance from vert0 to ray origin
335  SUB(tvec, m_origin, vert0);
336 
337  // calculate U parameter and test bounds
338  u = DOT(tvec, pvec);
339  if ((u < 0.0f) || (u > det)) {
340  // Hit the plane outside the triangle
341  return finf();
342  }
343 
344  // prepare to test V parameter
345  CROSS(qvec, tvec, edge1);
346 
347  // calculate V parameter and test bounds
348  v = DOT(m_direction, qvec);
349  if ((v < 0.0f) || (u + v > det)) {
350  // Hit the plane outside the triangle
351  return finf();
352  }
353 
354  float t = DOT(edge2, qvec);
355 
356  if (t >= 0) {
357  const float inv_det = 1.0f / det;
358  t *= inv_det;
359  u *= inv_det;
360  v *= inv_det;
361 
362  w0 = (1.0f - u - v);
363  w1 = u;
364  w2 = v;
365 
366  return t;
367  } else {
368  // We had to travel backwards in time to intersect
369  return finf();
370  }
371 }
372 
373 #undef EPSILON
374 #undef CROSS
375 #undef DOT
376 #undef SUB
377 
378 }// namespace
379 
380 #endif
float finf()
Definition: g3dmath.cpp:71
float c_zy
Definition: Ray.h:50
float c_xy
Definition: Ray.h:50
Definition: Ray.h:42
Definition: adtfile.h:46
Definition: Plane.h:25
Definition: Ray.h:42
void deserialize(class BinaryInput &b)
Definition: Ray.cpp:137
Definition: Ray.h:42
Definition: Ray.h:42
Definition: Ray.h:42
void serialize(class BinaryOutput &b) const
Definition: Ray.cpp:131
float c_xz
Definition: Ray.h:50
Definition: BinaryInput.h:69
float intersectionTime(const Point3 &vert0, const Point3 &vert1, const Point3 &vert2, float &w0, float &w1, float &w2) const
Definition: Ray.h:185
Definition: Ray.h:42
Ray(const Point3 &origin, const Vector3 &direction)
Definition: Ray.h:75
Definition: Ray.h:42
Definition: Intersect.h:27
Point3 closestPoint(const Point3 &point) const
Returns the closest point on the Ray to point.
Definition: Ray.h:106
Classification classification
Definition: Ray.h:44
Definition: AABox.h:25
#define SUB(dest, v1, v2)
Definition: Ray.h:242
float ibyk
Definition: Ray.h:47
#define DOT(v1, v2)
Definition: Ray.h:240
float __fastcall dot(const Vector3 &rkVector) const
Definition: Vector3.h:771
An arbitrary (oriented) 3D box, useful as a bounding box.
Definition: Box.h:35
Definition: Ray.h:42
Definition: Vector3.h:58
Definition: Ray.h:42
Ray reflect(const Vector3 &newOrigin, const Vector3 &normal) const
Definition: Ray.cpp:155
Definition: Ray.h:42
const Vector3 & edge02() const
Definition: Triangle.h:81
Definition: Ray.h:42
static const Vector3 & unitX()
Definition: Vector3.cpp:121
static const Vector3 & zero()
Definition: Vector3.cpp:119
Definition: Sphere.h:24
Definition: Ray.h:42
Definition: Ray.h:42
Definition: Ray.h:42
Ray bumpedRay(float distance, const Vector3 &bumpDirection) const
Definition: Ray.h:99
float intersectionTime(const Point3 &vert0, const Point3 &vert1, const Point3 &vert2) const
Definition: Ray.h:176
float jbyi
Definition: Ray.h:47
static Ray fromOriginAndDirection(const Point3 &point, const Vector3 &direction)
Definition: Ray.h:87
#define CROSS(dest, v1, v2)
Definition: Ray.h:235
Definition: Ray.h:42
void set(const Point3 &origin, const Vector3 &direction)
Definition: Ray.cpp:18
Classification
Definition: Ray.h:42
Ray()
Definition: Ray.h:70
const Vector3 & invDirection() const
Definition: Ray.h:66
float c_yx
Definition: Ray.h:50
Definition: Ray.h:42
Definition: Ray.h:24
Definition: AABox.h:32
const Vector3 & edge01() const
Definition: Triangle.h:76
float c_yz
Definition: Ray.h:50
Definition: Ray.h:42
Vector3 m_invDirection
Definition: Ray.h:34
Definition: Ray.h:42
float c_zx
Definition: Ray.h:50
Point3 intersection(const class Plane &plane) const
Definition: Ray.cpp:164
float kbyj
Definition: Ray.h:47
Point3 m_origin
Definition: Ray.h:28
const Point3 & origin() const
Definition: Ray.h:56
float jbyk
Definition: Ray.h:47
Definition: BinaryOutput.h:52
#define EPSILON
Definition: Ray.h:234
Definition: Ray.h:42
float ibyj
Definition: Ray.h:47
Definition: Ray.h:42
Definition: Ray.h:42
float intersectionTime(const Triangle &triangle) const
Definition: Ray.h:199
float distance(const Point3 &point) const
Definition: Ray.h:118
float kbyi
Definition: Ray.h:47
Definition: Ray.h:42
Definition: Ray.h:42
const Point3 & vertex(int n) const
Definition: Triangle.h:70
Definition: Triangle.h:34
Ray bumpedRay(float distance) const
Definition: Ray.h:93
const Vector3 & direction() const
Definition: Ray.h:61
Ray refract(const Vector3 &newOrigin, const Vector3 &normal, float iInside, float iOutside) const
Definition: Ray.cpp:144
float intersectionTime(const class Sphere &sphere, bool solid=false) const
Definition: Ray.cpp:179
Definition: Ray.h:42
Definition: Ray.h:42
Vector3 m_direction
Definition: Ray.h:31
Definition: Ray.h:42