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