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

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