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