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