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

Revision 1867, 8.2 KB checked in by bittner, 18 years ago (diff)

merge, global lines, rss sampling updates

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 0.9f
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  // number of cells where this ray made a contribution to the PVS
70  short mPvsContribution;
71 
72  // sum of relative ray contributions per object
73  float mRelativePvsContribution;
74
75  // weighted contribution to the pvs (based on the pass the ray was casted at)
76  // computed by the prperocessor
77  float mWeightedPvsContribution;
78
79  // probability of this ray
80  float mPdf;
81 
82  //////////////////////////////
83
84 
85  /// the kd node holding the termination point
86  KdNode *mTerminationNode;
87  /// the kd node holding the origin point
88  KdNode *mOriginNode;
89
90
91  VssRay(
92                 const Vector3 &origin,
93                 const Vector3 &termination,
94                 Intersectable *originObject,
95                 Intersectable *terminationObject,
96                 const int pass = 0,
97                 const float pdf = 1.0f
98                 );
99       
100  VssRay(const Ray &ray);
101       
102 
103  void Precompute();
104
105  void Mail() { mMailbox = mailID; }
106  static void NewMail() { mailID++; }
107  bool Mailed() const { return mMailbox == mailID; }
108
109  bool Mailed(const int mail) {
110    return mMailbox >= mailID + mail;
111  }
112
113  int HitCount() const {
114#if BIDIRECTIONAL_RAY
115        if (mOriginObject && mTerminationObject)
116          return 2;
117        if (mOriginObject || mTerminationObject)
118          return 1;
119        return 0;
120#else
121        return (mTerminationObject) ? 1 : 0;
122#endif
123  }
124       
125  Vector3 GetOrigin() const { return mOrigin; }
126  Vector3 GetTermination() const { return mTermination; }
127  Vector3 GetDir() const { return mTermination - mOrigin; }
128  //  Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); }
129  Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; }
130
131  float Length() const { return Distance(mOrigin, mTermination); }
132
133  Vector3 Extrap(const float t) const {
134        return GetOrigin() + t * GetDir();
135  }
136       
137  float GetDirParametrization(const int axis) const;
138  float GetOpositeDirParametrization(const int axis) const;
139
140  static float VssRay::GetDirParam(const int axis, const Vector3 dir);
141  static Vector3 VssRay::GetInvDirParam(const float alpha, const float beta);
142
143  float GetSize() const { return  1.0f/mInvSize; }
144  float GetInvSize() const { return  mInvSize; }
145  float GetOrigin(const int axis) const { return mOrigin[axis]; }
146  float GetTermination(const int axis) const { return mTermination[axis]; }
147  float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
148  float GetNormalizedDir(const int axis) const {
149    return (mTermination[axis] - mOrigin[axis])*mInvSize;
150  }
151       
152  bool
153  ComputeMinMaxT(const AxisAlignedBox3 &box,
154                                 float &tmin,
155                                 float &tmax) const;
156       
157  bool
158  Intersects(const AxisAlignedBox3 &box,
159                         float &tmin,
160                         float &tmax) const;
161       
162  bool
163  IntersectsSphere(const Vector3 &center,
164                                   const float sqrRadius,
165                                   Vector3 &point,
166                                   float &t) const;
167       
168  void
169  Translate(const Vector3 &translation) {
170    mOrigin += translation;
171    mTermination += translation;
172  }
173
174  void SetupEndPoints(const Vector3 &origin,
175                                          const Vector3 &termination)
176  {
177        mOrigin = origin;
178        mTermination = termination;
179        Precompute();
180  }
181                                                                                       
182  bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
183
184  char Flags() const { return mFlags;}  void SetFlags(char orFlag) { mFlags |= orFlag;}
185 
186  bool IsActive() const { return mRefCount>0; }
187       
188  // reference counting for leaf nodes
189  int RefCount() const { return mRefCount; }
190  int Ref() { return mRefCount++; }
191       
192  void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
193  bool ScheduledForRemoval() const { return mRefCount<0; }
194  void Unref() {
195    if (mRefCount > 0)
196      mRefCount--;
197    else
198      if (mRefCount < 0)
199                mRefCount++;
200      else {
201                cerr<<"Trying to unref already deleted ray!"<<endl;
202                exit(1);
203      }
204  }
205
206  static Vector3
207  GetDirection(const float a, const float b) {
208        return GetInvDirParam(a, b);
209        //return Vector3(sin(a), sin(b), cos(a));
210  }
211
212  friend bool GreaterWeightedPvsContribution(const VssRay * a,
213                                                                                         const VssRay *b) {
214        return a->mWeightedPvsContribution > b->mWeightedPvsContribution;
215  }
216
217  float SqrDistance(const Vector3 &point) const {
218        Vector3 diff = point - mOrigin;
219        float t = DotProd(diff, GetDir());
220       
221    if ( t <= 0.0f ) {
222          t = 0.0f;
223    } else {
224          if (t >= 1.0f) {
225                t = 1.0f;
226          } else {
227        t /= SqrMagnitude(GetDir());
228          }
229          diff -= t*GetDir();
230        }
231    return SqrMagnitude(diff);
232  }
233
234  /** Returns the data sampled on either the ray origin or termination.
235  */
236  void GetSampleData(
237          const bool isTerminaton,
238          Vector3 &pt,
239          Intersectable **obj,
240          KdNode **node) const;
241
242  friend ostream& operator<< (ostream &s, const VssRay &vssRay);
243   
244};
245
246inline void VssRay::GetSampleData(const bool isTermination,
247                                                                  Vector3 &pt,
248                                                                  Intersectable **obj,
249                                                                  KdNode **node) const
250{
251        if (isTermination)
252        {
253                pt = mTermination;
254                *obj = mTerminationObject;
255                *node = mTerminationNode;
256        }
257        else
258        {
259                pt = mOrigin;
260                *obj = mOriginObject;
261                *node = mOriginNode;
262        }
263}
264
265void
266GenerateExtendedConvexCombinationWeights(float &w1,
267                                                                                 float &w2,
268                                                                                 float &w3,
269                                                                                 const float overlap
270                                                                                 );
271
272void
273GenerateExtendedConvexCombinationWeights2(float &w1,
274                                                                                  float &w2,
275                                                                                  const float overlap
276                                                                                  );
277
278// Overload << operator for C++-style output
279inline ostream&
280operator<< (ostream &s, const VssRay &vssRay)
281{
282        return s
283                << "(" << vssRay.mPass << ", " << vssRay.mOrigin << ", " << vssRay.mTermination
284                << ", " << vssRay.mOriginObject << ", " << vssRay.mTerminationObject << ", " << vssRay.mPdf << ")";
285}
286
287// --------------------------------------------------------------
288// For sorting rays
289// --------------------------------------------------------------
290struct 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
311struct VssRayContainer : public vector<VssRay *>
312{
313  void PrintStatistics(ostream &s);
314  int SelectRays(const int number, VssRayContainer &selected, const bool copy=false) const;
315  int
316  GetContributingRays(VssRayContainer &selected,
317                                          const int minPass
318                                          ) const;
319 
320};
321
322/*
323struct VssRayDistribution {
324  VssRayDistribution() { mContribution = -1.0f; }
325  SimpleRayContainer mRays;
326  vector<VssRayContainer> mVssRays;
327  float mContribution;
328  float mTime;
329};
330
331struct VssRayDistributionMixture {
332  VssRayDistributionMixture() {}
333 
334  vector<VssRayDistribution> distributions;
335};
336*/
337
338};
339
340#endif
Note: See TracBrowser for help on using the repository browser.