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

Revision 2014, 8.4 KB checked in by bittner, 17 years ago (diff)

timer start

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