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