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

Revision 1785, 8.1 KB checked in by bittner, 18 years ago (diff)

merge, filter update, renderebuffer made functional

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