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