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 | //////////////////////////////
|
---|
67 | VssRay(
|
---|
68 | const Vector3 &origin,
|
---|
69 | const Vector3 &termination,
|
---|
70 | Intersectable *originObject,
|
---|
71 | Intersectable *terminationObject,
|
---|
72 | const int pass = 0
|
---|
73 | ):
|
---|
74 | mMailbox(-1),
|
---|
75 | mOrigin(origin),
|
---|
76 | mTermination(termination),
|
---|
77 | mOriginObject(originObject),
|
---|
78 | mTerminationObject(terminationObject),
|
---|
79 | mRefCount(0),
|
---|
80 | mFlags(0),
|
---|
81 | mPass(pass),
|
---|
82 | mViewCells(0),
|
---|
83 | mWeightedPvsContribution(0)
|
---|
84 | // mT(1.0f)
|
---|
85 | {
|
---|
86 | Precompute();
|
---|
87 | }
|
---|
88 |
|
---|
89 | VssRay(const Ray &ray):
|
---|
90 | mRefCount(0),
|
---|
91 | mFlags(0),
|
---|
92 | mMailbox(-1),
|
---|
93 | mOriginObject(ray.sourceObject.mObject),
|
---|
94 | mPass(0),
|
---|
95 | mViewCells(0)
|
---|
96 | // mT(1.0f)
|
---|
97 | {
|
---|
98 | if (ray.sourceObject.mObject)
|
---|
99 | mOrigin = ray.Extrap(ray.sourceObject.mT);
|
---|
100 | else
|
---|
101 | mOrigin = ray.GetLoc();
|
---|
102 |
|
---|
103 | //Debug << "origin: " << mOrigin << endl;
|
---|
104 | if (!ray.intersections.empty())
|
---|
105 | {
|
---|
106 | mTermination = ray.Extrap(ray.intersections[0].mT);
|
---|
107 | mTerminationObject = ray.intersections[0].mObject;
|
---|
108 | }
|
---|
109 | else
|
---|
110 | {
|
---|
111 | mTermination = ray.Extrap(1e6);//TODO: should be Limits::Infinity
|
---|
112 | mTerminationObject = NULL;
|
---|
113 | }
|
---|
114 |
|
---|
115 | Precompute();
|
---|
116 | }
|
---|
117 |
|
---|
118 | void Precompute() {
|
---|
119 | mFlags = 0;
|
---|
120 | Vector3 dir = GetDir();
|
---|
121 |
|
---|
122 | #define BIDIRECTIONAL_RAY 0
|
---|
123 | #if BIDIRECTIONAL_RAY
|
---|
124 | if (dir.y < 0) {
|
---|
125 | // swap objects and poits
|
---|
126 | swap(mOriginObject, mTerminationObject);
|
---|
127 | swap(mOrigin, mTermination);
|
---|
128 | dir = -dir;
|
---|
129 | }
|
---|
130 | #endif
|
---|
131 | if (dir.x > 0.0f) mFlags |= FPosDirX;
|
---|
132 | if (dir.y > 0.0f) mFlags |= FPosDirY;
|
---|
133 | if (dir.z > 0.0f) mFlags |= FPosDirZ;
|
---|
134 |
|
---|
135 | mInvSize = 1.0f/Magnitude(GetDir());
|
---|
136 | }
|
---|
137 |
|
---|
138 | void Mail() { mMailbox = mailID; }
|
---|
139 | static void NewMail() { mailID++; }
|
---|
140 | bool Mailed() const { return mMailbox == mailID; }
|
---|
141 |
|
---|
142 | bool Mailed(const int mail) {
|
---|
143 | return mMailbox >= mailID + mail;
|
---|
144 | }
|
---|
145 |
|
---|
146 | int HitCount() const {
|
---|
147 | #if BIDIRECTIONAL_RAY
|
---|
148 | if (mOriginObject && mTerminationObject)
|
---|
149 | return 2;
|
---|
150 | if (mOriginObject || mTerminationObject)
|
---|
151 | return 1;
|
---|
152 | return 0;
|
---|
153 | #else
|
---|
154 | return (mTerminationObject) ? 1 : 0;
|
---|
155 | #endif
|
---|
156 | }
|
---|
157 |
|
---|
158 | Vector3 GetOrigin() const { return mOrigin; }
|
---|
159 | Vector3 GetTermination() const { return mTermination; }
|
---|
160 | Vector3 GetDir() const { return mTermination - mOrigin; }
|
---|
161 | // Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); }
|
---|
162 | Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; }
|
---|
163 |
|
---|
164 | float Length() const { return Distance(mOrigin, mTermination); }
|
---|
165 |
|
---|
166 | Vector3 Extrap(const float t) const {
|
---|
167 | return GetOrigin() + t * GetDir();
|
---|
168 | }
|
---|
169 |
|
---|
170 | float GetDirParametrization(const int axis) const;
|
---|
171 | float GetOpositeDirParametrization(const int axis) const;
|
---|
172 |
|
---|
173 | static float VssRay::GetDirParam(const int axis, const Vector3 dir);
|
---|
174 |
|
---|
175 | float GetSize() const { return 1.0f/mInvSize; }
|
---|
176 | float GetInvSize() const { return mInvSize; }
|
---|
177 | float GetOrigin(const int axis) const { return mOrigin[axis]; }
|
---|
178 | float GetTermination(const int axis) const { return mTermination[axis]; }
|
---|
179 | float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
|
---|
180 | float GetNormalizedDir(const int axis) const {
|
---|
181 | return (mTermination[axis] - mOrigin[axis])*mInvSize;
|
---|
182 | }
|
---|
183 |
|
---|
184 | bool
|
---|
185 | ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
186 | float &tmin,
|
---|
187 | float &tmax) const;
|
---|
188 |
|
---|
189 | bool
|
---|
190 | Intersects(const AxisAlignedBox3 &box,
|
---|
191 | float &tmin,
|
---|
192 | float &tmax) const;
|
---|
193 |
|
---|
194 | bool
|
---|
195 | IntersectsSphere(const Vector3 ¢er,
|
---|
196 | const float sqrRadius,
|
---|
197 | Vector3 &point,
|
---|
198 | float &t) const;
|
---|
199 |
|
---|
200 | void
|
---|
201 | Translate(const Vector3 &translation) {
|
---|
202 | mOrigin += translation;
|
---|
203 | mTermination += translation;
|
---|
204 | }
|
---|
205 |
|
---|
206 | void SetupEndPoints(const Vector3 &origin,
|
---|
207 | const Vector3 &termination)
|
---|
208 | {
|
---|
209 | mOrigin = origin;
|
---|
210 | mTermination = termination;
|
---|
211 | Precompute();
|
---|
212 | }
|
---|
213 |
|
---|
214 | bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
|
---|
215 |
|
---|
216 | char Flags() const { return mFlags;}
|
---|
217 | void SetFlags(char orFlag) { mFlags |= orFlag;}
|
---|
218 |
|
---|
219 | bool IsActive() const { return mRefCount>0; }
|
---|
220 |
|
---|
221 | // reference counting for leaf nodes
|
---|
222 | int RefCount() const { return mRefCount; }
|
---|
223 | int Ref() { return mRefCount++; }
|
---|
224 |
|
---|
225 | void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
|
---|
226 | bool ScheduledForRemoval() const { return mRefCount<0; }
|
---|
227 | void Unref() {
|
---|
228 | if (mRefCount > 0)
|
---|
229 | mRefCount--;
|
---|
230 | else
|
---|
231 | if (mRefCount < 0)
|
---|
232 | mRefCount++;
|
---|
233 | else {
|
---|
234 | cerr<<"Trying to unref already deleted ray!"<<endl;
|
---|
235 | exit(1);
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | static Vector3
|
---|
240 | GetDirection(const float a, const float b) {
|
---|
241 | return Vector3(sin(a), sin(b), cos(a));
|
---|
242 | }
|
---|
243 |
|
---|
244 | friend bool GreaterWeightedPvsContribution(const VssRay * a,
|
---|
245 | const VssRay *b) {
|
---|
246 | return a->mWeightedPvsContribution > b->mWeightedPvsContribution;
|
---|
247 | }
|
---|
248 |
|
---|
249 | float SqrDistance(const Vector3 &point) const {
|
---|
250 | Vector3 diff = point - mOrigin;
|
---|
251 | float t = DotProd(diff, GetDir());
|
---|
252 |
|
---|
253 | if ( t <= 0.0f ) {
|
---|
254 | t = 0.0f;
|
---|
255 | } else {
|
---|
256 | if (t >= 1.0f) {
|
---|
257 | t = 1.0f;
|
---|
258 | } else {
|
---|
259 | t /= SqrMagnitude(GetDir());
|
---|
260 | }
|
---|
261 | diff -= t*GetDir();
|
---|
262 | }
|
---|
263 | return SqrMagnitude(diff);
|
---|
264 | }
|
---|
265 |
|
---|
266 | };
|
---|
267 |
|
---|
268 | void
|
---|
269 | GenerateExtendedConvexCombinationWeights(float &w1,
|
---|
270 | float &w2,
|
---|
271 | float &w3,
|
---|
272 | const float overlap
|
---|
273 | );
|
---|
274 |
|
---|
275 | void
|
---|
276 | GenerateExtendedConvexCombinationWeights2(float &w1,
|
---|
277 | float &w2,
|
---|
278 | const float overlap
|
---|
279 | );
|
---|
280 |
|
---|
281 | // --------------------------------------------------------------
|
---|
282 | // For sorting rays
|
---|
283 | // --------------------------------------------------------------
|
---|
284 | struct SortableEntry
|
---|
285 | {
|
---|
286 | enum EType {
|
---|
287 | ERayMin,
|
---|
288 | ERayMax
|
---|
289 | };
|
---|
290 |
|
---|
291 | int type;
|
---|
292 | float value;
|
---|
293 | void *data;
|
---|
294 |
|
---|
295 | SortableEntry() {}
|
---|
296 | SortableEntry(const int t, const float v, void *d):type(t),
|
---|
297 | value(v),
|
---|
298 | data(d) {}
|
---|
299 |
|
---|
300 | friend bool operator<(const SortableEntry &a, const SortableEntry &b) {
|
---|
301 | return a.value < b.value;
|
---|
302 | }
|
---|
303 | };
|
---|
304 |
|
---|
305 | struct VssRayContainer : public vector<VssRay *>
|
---|
306 | {
|
---|
307 | void PrintStatistics(ostream &s);
|
---|
308 | int SelectRays(const int number, VssRayContainer &selected) const;
|
---|
309 | int
|
---|
310 | GetContributingRays(VssRayContainer &selected,
|
---|
311 | const int minPass
|
---|
312 | ) const;
|
---|
313 |
|
---|
314 | };
|
---|
315 |
|
---|
316 |
|
---|
317 |
|
---|
318 | #endif
|
---|