HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VGEO_Ray.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Mantra (C++)
7  *
8  * COMMENTS: Ray class used by mantra's ray tracer
9  *
10  */
11 
12 #ifndef __VGEO_Ray_H__
13 #define __VGEO_Ray_H__
14 
15 #include "VGEO_API.h"
16 #include <VM/VM_SIMD.h>
17 #include <UT/UT_BoundingBox.h>
18 #include <UT/UT_IntrusivePtr.h>
19 #include <UT/UT_Vector3.h>
20 #include <SYS/SYS_Inline.h>
21 #include <SYS/SYS_Math.h>
22 #include <iostream>
23 
24 #define VGEO_RAY_VECTORIZED 1
25 
26 class VPRM_Space;
28 
30 public:
31  VGEO_Ray() {}
32 
33  VGEO_Ray(const UT_Vector3 &P, const UT_Vector3 &D)
34  {
35  init(P, D);
36  }
37 
38  /// Initialize the ray for the given position and direction
39  void init(const UT_Vector3 &P, const UT_Vector3 &D);
40 
41  /// Construct a transformed version of this ray
42  void transform(VGEO_Ray &newRay, const VPRM_SpacePtr &space,
43  float now) const;
44 
45  /// Construct a translated version of this ray
46  void translate(VGEO_Ray &newRay, const UT_Vector3 &t) const
47  {
48  newRay = *this;
49 #if !VGEO_RAY_VECTORIZED
50  newRay.myP += t;
51 #else
52  (*reinterpret_cast<UT_Vector3*>(&newRay.myP)) += t;
53 #endif
54  }
55  void itranslate(VGEO_Ray &newRay, const UT_Vector3 &t) const
56  {
57  newRay = *this;
58 #if !VGEO_RAY_VECTORIZED
59  newRay.myP -= t;
60 #else
61  (*reinterpret_cast<UT_Vector3*>(&newRay.myP)) -= t;
62 #endif
63  }
64 
65  /// This method adjusts the ranges for the ray so that tmin is where
66  /// the ray enters the bounding box, and tmax is where it exists.
67  /// Returns true when the ray intersects the bounding box.
69  float &tmin, float &tmax) const
70  {
71 #if !VGEO_RAY_VECTORIZED
72  getRangeAxis<0>(box, tmin, tmax);
73  getRangeAxis<1>(box, tmin, tmax);
74  getRangeAxis<2>(box, tmin, tmax);
75 #else
76  //v4uf ta(box.vals[0][0], box.vals[1][0], box.vals[2][0], 0);
77  //v4uf tb(box.vals[0][1], box.vals[1][1], box.vals[2][1], 0);
78  //v4uf t1 = (ta & mySign) | andn(mySign, tb)
79  //v4uf t2 = (tb & mySign) | andn(mySign, ta)
80  int sign0 = mySign[0];
81  int sign1 = mySign[1];
82  int sign2 = mySign[2];
83  v4uf t1(box.vals[0][sign0 ], box.vals[1][sign1 ], box.vals[2][sign2 ], tmax);
84  v4uf t2(box.vals[0][sign0^1], box.vals[1][sign1^1], box.vals[2][sign2^1], tmin);
85  t1 -= myP;
86  t2 -= myP;
87  t1 *= myID;
88  t2 *= myID;
89  v4uf tmaxtemp = vmin(t1, t1.swizzle<2,3,2,3>());
90  v4uf tmintemp = vmax(t2, t2.swizzle<2,3,2,3>());
91  float tmaxlocal = vmin(tmaxtemp, tmaxtemp.swizzle<1,1,1,1>())[0];
92  float tminlocal = vmax(tmintemp, tmintemp.swizzle<1,1,1,1>())[0];
93  tmax = tmaxlocal;
94  tmin = tminlocal;
95  return tminlocal <= tmaxlocal;
96 #endif
97 
98 #if 0
99  int sign0 = mySign[0];
100  int sign1 = mySign[1];
101  int sign2 = mySign[2];
102  UT_Vector3 t1(box.vals[0][sign0 ], box.vals[1][sign1 ], box.vals[2][sign2 ]);
103  UT_Vector3 t2(box.vals[0][sign0^1], box.vals[1][sign1^1], box.vals[2][sign2^1]);
104  t1 -= myP;
105  t2 -= myP;
106  t1 *= myID;
107  t2 *= myID;
108  tmin = SYSmax(tmin, t2(0), t2(1), t2(2));
109  tmax = SYSmin(tmax, t1(0), t1(1), t1(2));
110 #endif
111 
112  return tmin <= tmax;
113  }
114 
117 #if !VGEO_RAY_VECTORIZED
118  { return myP; }
119 #else
120  { return *reinterpret_cast<const UT_Vector3*>(&myP); }
121 #endif
124 #if !VGEO_RAY_VECTORIZED
125  { return myID; }
126 #else
127  { return *reinterpret_cast<const UT_Vector3*>(&myID); }
128 #endif
130  const UT_Vector3 &getD() const { return myD; }
132  UT_Vector3 getPt(const float &t) const { return getP() + getD() * t; }
134  const int *getSign() const { return mySign; }
135 
136  /// @{
137  /// The following forms are used to represent the ray as a set of two
138  /// orthogonal planes. The planes are not orthonormal and you MUST call
139  /// getN1 before you call getN2 or getMaxD.
140  const UT_Vector3 &getN1() const
141  {
142  if (myMaxD < 0) computeN();
143  return myN1;
144  }
145  const UT_Vector3 &getN2() const { return myN2; }
146  int getMaxD() const { return myMaxD; }
147  /// @}
148 
149  /// Save the ray as text to the stream
150  void save(std::ostream &os) const;
151 
152  /// Dump the ray to stderr
153  void dump() const { save(std::cerr); }
154 
155 private:
156 #if !VGEO_RAY_VECTORIZED
157  UT_Vector3 myP; // Position
158  UT_Vector3 myID; // Inverse direction
159 #else
160  /// Position (component 3 must be 0)
161  v4uf myP;
162  /// 1 / direction (component 3 must be 1)
163  v4uf myID;
164  /// Sign of direction (component 3 is ignored)
165  /// -1 == -ve; 0 == +ve
166  //v4uu mySign;
167 
168 #endif
169  int mySign[3]; // Sign of direction (1 == +)
170 
171  /// Direction
172  UT_Vector3 myD;
173 
174 #if !VGEO_RAY_VECTORIZED
175  template <int axis>
176  inline void getRangeAxis(const UT_BoundingBox &box,
177  float &tmin, float &tmax) const
178  {
179  int sign = mySign[axis];
180  float t1 = (box.vals[axis][sign] - myP(axis)) * myID(axis);
181  float t2 = (box.vals[axis][sign^1] - myP(axis)) * myID(axis);
182  tmax = t1 < tmax ? t1 : tmax;
183  tmin = t2 > tmin ? t2 : tmin;
184  }
185 #endif
186  void computeN() const;
187 
188  mutable UT_Vector3 myN1, myN2; // Plane representations of ray
189  mutable int myMaxD; // Maximum ray direction
190 };
191 
192 #endif
#define SYSmax(a, b)
Definition: SYS_Math.h:1521
GLuint GLenum GLenum transform
Definition: glew.h:14742
void translate(VGEO_Ray &newRay, const UT_Vector3 &t) const
Construct a translated version of this ray.
Definition: VGEO_Ray.h:46
void dump() const
Dump the ray to stderr.
Definition: VGEO_Ray.h:153
SYS_FORCE_INLINE UT_Vector3 getPt(const float &t) const
Definition: VGEO_Ray.h:132
void itranslate(VGEO_Ray &newRay, const UT_Vector3 &t) const
Definition: VGEO_Ray.h:55
SYS_FORCE_INLINE const int * getSign() const
Definition: VGEO_Ray.h:134
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
Definition: VM_SIMD.h:186
VGEO_Ray()
Definition: VGEO_Ray.h:31
int getMaxD() const
Definition: VGEO_Ray.h:146
int sign(T a)
Definition: ImathFun.h:63
SYS_FORCE_INLINE const UT_Vector3 & getID() const
Definition: VGEO_Ray.h:123
const UT_Vector3 & getN2() const
Definition: VGEO_Ray.h:145
SYS_FORCE_INLINE const UT_Vector3 & getD() const
Definition: VGEO_Ray.h:130
SYS_FORCE_INLINE const UT_Vector3 & getP() const
Definition: VGEO_Ray.h:116
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition: glew.h:12681
#define VGEO_API
Definition: VGEO_API.h:10
SYS_FORCE_INLINE bool getBoxRange(const UT_BoundingBox &box, float &tmin, float &tmax) const
Definition: VGEO_Ray.h:68
VGEO_Ray(const UT_Vector3 &P, const UT_Vector3 &D)
Definition: VGEO_Ray.h:33
SYS_FORCE_INLINE v4uf swizzle() const
Definition: VM_SIMD.h:333
const UT_Vector3 & getN1() const
Definition: VGEO_Ray.h:140
#define const
Definition: zconf.h:214
#define SYSmin(a, b)
Definition: SYS_Math.h:1522
GLdouble GLdouble t
Definition: glew.h:1398