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

Revision 2116, 8.4 KB checked in by mattausch, 17 years ago (diff)

implemented hashpvs

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