source: GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h @ 2168

Revision 2168, 8.4 KB checked in by mattausch, 17 years ago (diff)
Line 
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//using namespace std;
10
11
12namespace GtpVisibilityPreprocessor {
13
14class AxisAlignedBox3;
15class Intersectable;
16class KdNode;
17class Ray;
18
19#define ABS_CONTRIBUTION_WEIGHT 0.0f
20#define VSS_STORE_VIEWCELLS 1
21 
22class VssRay {
23public:
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 &center,
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  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                cerr<<"Trying to unref already deleted ray!"<<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 ostream& operator<< (ostream &s, const VssRay &vssRay);
252   
253};
254
255inline void VssRay::GetSampleData(const bool isTermination,
256                                                                  Vector3 &pt,
257                                                                  Intersectable **obj,
258                                                                  KdNode **node) const
259{
260        if (isTermination)
261        {
262                pt = mTermination;
263                *obj = mTerminationObject;
264                *node = mTerminationNode;
265        }
266        else
267        {
268                pt = mOrigin;
269                *obj = mOriginObject;
270                *node = mOriginNode;
271        }
272}
273
274void
275GenerateExtendedConvexCombinationWeights(float &w1,
276                                                                                 float &w2,
277                                                                                 float &w3,
278                                                                                 const float overlap
279                                                                                 );
280
281void
282GenerateExtendedConvexCombinationWeights2(float &w1,
283                                                                                  float &w2,
284                                                                                  const float overlap
285                                                                                  );
286
287// Overload << operator for C++-style output
288inline ostream&
289operator<< (ostream &s, const VssRay &vssRay)
290{
291        return s
292                << "(" << vssRay.mPass << ", " << vssRay.mOrigin << ", " << vssRay.mTermination
293                << ", " << vssRay.mOriginObject << ", " << vssRay.mTerminationObject << ", " << vssRay.mPdf << ")";
294}
295
296// --------------------------------------------------------------
297// For sorting rays
298// --------------------------------------------------------------
299struct SortableEntry
300{
301  enum EType {
302    ERayMin,
303    ERayMax
304  };
305
306  int type;
307  float value;
308  void *data;
309 
310  SortableEntry() {}
311  SortableEntry(const int t, const float v, void *d):type(t),
312                                                                                                         value(v),
313                                                                                                         data(d) {}
314       
315  friend bool operator<(const SortableEntry &a, const SortableEntry &b) {
316    return a.value < b.value;
317  }
318};
319
320struct VssRayContainer : public vector<VssRay *>
321{
322  void PrintStatistics(ostream &s);
323  int SelectRays(const int number, VssRayContainer &selected, const bool copy=false) const;
324  int
325  GetContributingRays(VssRayContainer &selected,
326                                          const int minPass
327                                          ) const;
328 
329};
330
331/*
332struct VssRayDistribution {
333  VssRayDistribution() { mContribution = -1.0f; }
334  SimpleRayContainer mRays;
335  vector<VssRayContainer> mVssRays;
336  float mContribution;
337  float mTime;
338};
339
340struct VssRayDistributionMixture {
341  VssRayDistributionMixture() {}
342 
343  vector<VssRayDistribution> distributions;
344};
345*/
346
347};
348
349#endif
Note: See TracBrowser for help on using the repository browser.