1 | #ifndef __VSS_RAY_H
|
---|
2 | #define __VSS_RAY_H
|
---|
3 |
|
---|
4 | #include <vector>
|
---|
5 | using namespace std;
|
---|
6 | #include "Vector3.h"
|
---|
7 | #include "Ray.h"
|
---|
8 |
|
---|
9 | class AxisAlignedBox3;
|
---|
10 | class Intersectable;
|
---|
11 |
|
---|
12 | class VssRay {
|
---|
13 | public:
|
---|
14 | // various flags
|
---|
15 | enum {
|
---|
16 | FPosDirX = 1, // the direction of ray in X-axis is positive
|
---|
17 | FPosDirY = 2, // the direction of ray in Y-axis is positive
|
---|
18 | FPosDirZ = 4 // the direction of ray in Z-axis is positive
|
---|
19 | };
|
---|
20 |
|
---|
21 | static int mailID;
|
---|
22 | int mMailbox;
|
---|
23 |
|
---|
24 | // side of the ray - used for the ray classification
|
---|
25 | // char mSide;
|
---|
26 |
|
---|
27 | // computed t
|
---|
28 | // float mT;
|
---|
29 |
|
---|
30 | // inverse of the ray size
|
---|
31 | float mInvSize;
|
---|
32 |
|
---|
33 | // counter of references to this ray
|
---|
34 | short mRefCount;
|
---|
35 |
|
---|
36 | // various flags
|
---|
37 | char mFlags;
|
---|
38 |
|
---|
39 | Vector3 mOrigin;
|
---|
40 | Vector3 mTermination;
|
---|
41 |
|
---|
42 | /// Termination object for the ray
|
---|
43 | /// only the termination object is actually used
|
---|
44 | Intersectable *mOriginObject;
|
---|
45 | Intersectable *mTerminationObject;
|
---|
46 |
|
---|
47 | ////////////////////////
|
---|
48 | // members related to importance sampling
|
---|
49 | // sampling pass in which this ray was generated
|
---|
50 | short mPass;
|
---|
51 |
|
---|
52 | // number of cells where this ray made a contribution to the PVS
|
---|
53 | short mPvsContribution;
|
---|
54 |
|
---|
55 | // sum of relative ray contributions per object
|
---|
56 | float mRelativePvsContribution;
|
---|
57 |
|
---|
58 | //////////////////////////////
|
---|
59 | VssRay(
|
---|
60 | const Vector3 &origin,
|
---|
61 | const Vector3 &termination,
|
---|
62 | Intersectable *originObject,
|
---|
63 | Intersectable *terminationObject,
|
---|
64 | const int pass = 0
|
---|
65 | ):
|
---|
66 | mMailbox(-1),
|
---|
67 | mOrigin(origin),
|
---|
68 | mTermination(termination),
|
---|
69 | mOriginObject(originObject),
|
---|
70 | mTerminationObject(terminationObject),
|
---|
71 | mRefCount(0),
|
---|
72 | mFlags(0),
|
---|
73 | mPass(pass)
|
---|
74 | // mT(1.0f)
|
---|
75 | {
|
---|
76 | Precompute();
|
---|
77 | }
|
---|
78 |
|
---|
79 | VssRay(const Ray &ray):
|
---|
80 | mRefCount(0),
|
---|
81 | mFlags(0),
|
---|
82 | mMailbox(-1),
|
---|
83 | mOriginObject(ray.sourceObject.mObject),
|
---|
84 | mPass(0)
|
---|
85 | // mT(1.0f)
|
---|
86 | {
|
---|
87 | if (ray.sourceObject.mObject)
|
---|
88 | mOrigin = ray.Extrap(ray.sourceObject.mT);
|
---|
89 | else
|
---|
90 | mOrigin = ray.GetLoc();
|
---|
91 |
|
---|
92 | //Debug << "origin: " << mOrigin << endl;
|
---|
93 | if (!ray.intersections.empty())
|
---|
94 | {
|
---|
95 | mTermination = ray.Extrap(ray.intersections[0].mT);
|
---|
96 | mTerminationObject = ray.intersections[0].mObject;
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | mTermination = ray.Extrap(1e6);//TODO: should be Limits::Infinity
|
---|
101 | mTerminationObject = NULL;
|
---|
102 | }
|
---|
103 |
|
---|
104 | Precompute();
|
---|
105 | }
|
---|
106 |
|
---|
107 | void Precompute() {
|
---|
108 | mFlags = 0;
|
---|
109 | Vector3 dir = GetDir();
|
---|
110 |
|
---|
111 | #define BIDIRECTIONAL_RAY 0
|
---|
112 | #if BIDIRECTIONAL_RAY
|
---|
113 | if (dir.y < 0) {
|
---|
114 | // swap objects and poits
|
---|
115 | swap(mOriginObject, mTerminationObject);
|
---|
116 | swap(mOrigin, mTermination);
|
---|
117 | dir = -dir;
|
---|
118 | }
|
---|
119 | #endif
|
---|
120 | if (dir.x > 0.0f) mFlags |= FPosDirX;
|
---|
121 | if (dir.y > 0.0f) mFlags |= FPosDirY;
|
---|
122 | if (dir.z > 0.0f) mFlags |= FPosDirZ;
|
---|
123 |
|
---|
124 | mInvSize = 1.0f/Magnitude(GetDir());
|
---|
125 | }
|
---|
126 |
|
---|
127 | void Mail() { mMailbox = mailID; }
|
---|
128 | static void NewMail() { mailID++; }
|
---|
129 | bool Mailed() const { return mMailbox == mailID; }
|
---|
130 |
|
---|
131 | bool Mailed(const int mail) {
|
---|
132 | return mMailbox >= mailID + mail;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int HitCount() const {
|
---|
136 | #if BIDIRECTIONAL_RAY
|
---|
137 | if (mOriginObject && mTerminationObject)
|
---|
138 | return 2;
|
---|
139 | if (mOriginObject || mTerminationObject)
|
---|
140 | return 1;
|
---|
141 | return 0;
|
---|
142 | #else
|
---|
143 | return (mTerminationObject) ? 1 : 0;
|
---|
144 | #endif
|
---|
145 | }
|
---|
146 |
|
---|
147 | Vector3 GetOrigin() const { return mOrigin; }
|
---|
148 | Vector3 GetTermination() const { return mTermination; }
|
---|
149 | Vector3 GetDir() const { return mTermination - mOrigin; }
|
---|
150 | // Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); }
|
---|
151 | Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; }
|
---|
152 |
|
---|
153 | float Length() const { return Distance(mOrigin, mTermination); }
|
---|
154 |
|
---|
155 | Vector3 Extrap(const float t) const {
|
---|
156 | return GetOrigin() + t * GetDir();
|
---|
157 | }
|
---|
158 |
|
---|
159 | float GetDirParametrization(const int axis) const;
|
---|
160 |
|
---|
161 | static float VssRay::GetDirParam(const int axis, const Vector3 dir);
|
---|
162 |
|
---|
163 | float GetSize() const { return 1.0f/mInvSize; }
|
---|
164 | float GetInvSize() const { return mInvSize; }
|
---|
165 | float GetOrigin(const int axis) const { return mOrigin[axis]; }
|
---|
166 | float GetTermination(const int axis) const { return mTermination[axis]; }
|
---|
167 | float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
|
---|
168 | float GetNormalizedDir(const int axis) const {
|
---|
169 | return (mTermination[axis] - mOrigin[axis])*mInvSize;
|
---|
170 | }
|
---|
171 |
|
---|
172 | bool
|
---|
173 | ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
174 | float &tmin,
|
---|
175 | float &tmax) const;
|
---|
176 |
|
---|
177 | bool
|
---|
178 | Intersects(const AxisAlignedBox3 &box,
|
---|
179 | float &tmin,
|
---|
180 | float &tmax) const;
|
---|
181 |
|
---|
182 | bool
|
---|
183 | IntersectsSphere(const Vector3 ¢er,
|
---|
184 | const float sqrRadius,
|
---|
185 | Vector3 &point,
|
---|
186 | float &t) const;
|
---|
187 |
|
---|
188 | void
|
---|
189 | Translate(const Vector3 &translation) {
|
---|
190 | mOrigin += translation;
|
---|
191 | mTermination += translation;
|
---|
192 | }
|
---|
193 |
|
---|
194 | void SetupEndPoints(const Vector3 &origin,
|
---|
195 | const Vector3 &termination)
|
---|
196 | {
|
---|
197 | mOrigin = origin;
|
---|
198 | mTermination = termination;
|
---|
199 | Precompute();
|
---|
200 | }
|
---|
201 |
|
---|
202 | bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
|
---|
203 |
|
---|
204 | char Flags() const { return mFlags;}
|
---|
205 | void SetFlags(char orFlag) { mFlags |= orFlag;}
|
---|
206 |
|
---|
207 | bool IsActive() const { return mRefCount>0; }
|
---|
208 |
|
---|
209 | // reference counting for leaf nodes
|
---|
210 | int RefCount() const { return mRefCount; }
|
---|
211 | int Ref() { return mRefCount++; }
|
---|
212 |
|
---|
213 | void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
|
---|
214 | bool ScheduledForRemoval() const { return mRefCount<0; }
|
---|
215 | void Unref() {
|
---|
216 | if (mRefCount > 0)
|
---|
217 | mRefCount--;
|
---|
218 | else
|
---|
219 | if (mRefCount < 0)
|
---|
220 | mRefCount++;
|
---|
221 | else {
|
---|
222 | cerr<<"Trying to unref already deleted ray!"<<endl;
|
---|
223 | exit(1);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | static Vector3
|
---|
228 | GetDirection(const float a, const float b) {
|
---|
229 | return Vector3(sin(a), sin(b), cos(a));
|
---|
230 | }
|
---|
231 |
|
---|
232 | };
|
---|
233 |
|
---|
234 | void
|
---|
235 | GenerateExtendedConvexCombinationWeights(float &w1,
|
---|
236 | float &w2,
|
---|
237 | float &w3,
|
---|
238 | const float overlap
|
---|
239 | );
|
---|
240 |
|
---|
241 | void
|
---|
242 | GenerateExtendedConvexCombinationWeights2(float &w1,
|
---|
243 | float &w2,
|
---|
244 | const float overlap
|
---|
245 | );
|
---|
246 |
|
---|
247 | // --------------------------------------------------------------
|
---|
248 | // For sorting rays
|
---|
249 | // --------------------------------------------------------------
|
---|
250 | struct SortableEntry
|
---|
251 | {
|
---|
252 | enum EType {
|
---|
253 | ERayMin,
|
---|
254 | ERayMax
|
---|
255 | };
|
---|
256 |
|
---|
257 | int type;
|
---|
258 | float value;
|
---|
259 | void *data;
|
---|
260 |
|
---|
261 | SortableEntry() {}
|
---|
262 | SortableEntry(const int t, const float v, void *d):type(t),
|
---|
263 | value(v),
|
---|
264 | data(d) {}
|
---|
265 |
|
---|
266 | friend bool operator<(const SortableEntry &a, const SortableEntry &b) {
|
---|
267 | return a.value < b.value;
|
---|
268 | }
|
---|
269 | };
|
---|
270 |
|
---|
271 | struct VssRayContainer : public vector<VssRay *>
|
---|
272 | {
|
---|
273 | void PrintStatistics(ostream &s);
|
---|
274 | };
|
---|
275 |
|
---|
276 |
|
---|
277 |
|
---|
278 | #endif
|
---|