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

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