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