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

Revision 1883, 8.3 KB checked in by bittner, 18 years ago (diff)

mixture distribution initial coding

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