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

Revision 1566, 7.6 KB checked in by mattausch, 18 years ago (diff)
RevLine 
[376]1#ifndef __VSS_RAY_H
2#define __VSS_RAY_H
[372]3
4#include <vector>
5using namespace std;
6#include "Vector3.h"
[410]7#include "Ray.h"
[492]8#include "Containers.h"
[372]9
[860]10namespace GtpVisibilityPreprocessor {
11
[372]12class AxisAlignedBox3;
13class Intersectable;
[1133]14class KdNode;
[372]15
[1528]16
[372]17class VssRay {
18public:
[1528]19
20 // various flags
[372]21  enum {
[1566]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
[372]27  };
28
[438]29  static int mailID;
[372]30  int mMailbox;
31       
[438]32  // side of the ray - used for the ray classification
33  //  char mSide;
[372]34       
[438]35  // computed t
[463]36  //  float mT;
[372]37
[438]38  // inverse of the ray size
39  float mInvSize;
40 
41  // counter of references to this ray
[372]42  short mRefCount;
[438]43 
44  // various flags
[372]45  char mFlags;
46       
47  Vector3 mOrigin;
48  Vector3 mTermination;
49       
[438]50  /// Termination object for the ray
51  /// only the termination object is actually used
52  Intersectable *mOriginObject;
53  Intersectable *mTerminationObject;
[464]54
[492]55  ViewCellContainer mViewCells;
56 
[1133]57
[464]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;
[492]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;
[534]73
74  // probability of this ray
[556]75  float mPdf;
[464]76 
[438]77  //////////////////////////////
[1133]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
[464]86  VssRay(
87                 const Vector3 &origin,
[438]88                 const Vector3 &termination,
89                 Intersectable *originObject,
[464]90                 Intersectable *terminationObject,
[537]91                 const int pass = 0,
[556]92                 const float pdf = 1.0f
[1528]93                 );
[372]94       
[1528]95  VssRay(const Ray &ray);
[492]96       
97 
[1528]98  void Precompute();
[376]99
[372]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
[438]108  int HitCount() const {
[386]109#if BIDIRECTIONAL_RAY
[438]110        if (mOriginObject && mTerminationObject)
111          return 2;
112        if (mOriginObject || mTerminationObject)
113          return 1;
114        return 0;
[386]115#else
[438]116        return (mTerminationObject) ? 1 : 0;
[386]117#endif
[438]118  }
[372]119       
[438]120  Vector3 GetOrigin() const { return mOrigin; }
[372]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
[448]126  float Length() const { return Distance(mOrigin, mTermination); }
127
[438]128  Vector3 Extrap(const float t) const {
129        return GetOrigin() + t * GetDir();
130  }
[434]131       
[438]132  float GetDirParametrization(const int axis) const;
[492]133  float GetOpositeDirParametrization(const int axis) const;
[438]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; }
[372]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,
[438]148                                 float &tmin,
149                                 float &tmax) const;
[372]150       
151  bool
152  Intersects(const AxisAlignedBox3 &box,
[438]153                         float &tmin,
154                         float &tmax) const;
[372]155       
156  bool
157  IntersectsSphere(const Vector3 &center,
[438]158                                   const float sqrRadius,
159                                   Vector3 &point,
160                                   float &t) const;
[372]161       
162  void
163  Translate(const Vector3 &translation) {
164    mOrigin += translation;
165    mTermination += translation;
166  }
[427]167
[438]168  void SetupEndPoints(const Vector3 &origin,
169                                          const Vector3 &termination)
170  {
171        mOrigin = origin;
172        mTermination = termination;
173        Precompute();
174  }
[427]175                                                                                       
[372]176  bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
177
[1112]178  char Flags() const { return mFlags;}  void SetFlags(char orFlag) { mFlags |= orFlag;}
[372]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)
[438]193                mRefCount++;
[372]194      else {
[438]195                cerr<<"Trying to unref already deleted ray!"<<endl;
196                exit(1);
[372]197      }
198  }
[434]199
[438]200  static Vector3
201  GetDirection(const float a, const float b) {
202        return Vector3(sin(a), sin(b), cos(a));
203  }
[434]204
[492]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  }
[1259]226
[1528]227  /** Returns the data sampled on either the ray origin or termination.
228  */
[1259]229  void GetSampleData(
230          const bool isTerminaton,
231          Vector3 &pt,
232          Intersectable **obj,
233          KdNode **node) const;
234
[1221]235  friend ostream& operator<< (ostream &s, const VssRay &vssRay);
236   
[372]237};
238
[1528]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
[446]258void
259GenerateExtendedConvexCombinationWeights(float &w1,
260                                                                                 float &w2,
261                                                                                 float &w3,
262                                                                                 const float overlap
263                                                                                 );
[372]264
[464]265void
266GenerateExtendedConvexCombinationWeights2(float &w1,
267                                                                                  float &w2,
268                                                                                  const float overlap
269                                                                                  );
270
[1221]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
[446]280// --------------------------------------------------------------
281// For sorting rays
282// --------------------------------------------------------------
[1528]283struct SortableEntry
[446]284{
285  enum EType {
286    ERayMin,
287    ERayMax
288  };
[372]289
[446]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
[467]304struct VssRayContainer : public vector<VssRay *>
305{
306  void PrintStatistics(ostream &s);
[1112]307  int SelectRays(const int number, VssRayContainer &selected, const bool copy=false) const;
[492]308  int
309  GetContributingRays(VssRayContainer &selected,
310                                          const int minPass
311                                          ) const;
312 
[467]313};
[372]314
[863]315};
[372]316
317#endif
Note: See TracBrowser for help on using the repository browser.