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

Revision 2609, 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
[2609]22#define VSS_STORE_VIEWCELLS 0
[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;
[2593]71  /// Distribution used to generate this ray
[1883]72  short mDistribution;
[2593]73  /// number of cells where this ray made a contribution to the PVS
[1883]74  int mPvsContribution;
[2593]75  /// sum of relative ray contributions per object
[464]76  float mRelativePvsContribution;
[2609]77  /** weighted contribution to the pvs (based on the pass the ray was casted at)
78         ccomputed by the prperocessor
79  */
[492]80  float mWeightedPvsContribution;
[2593]81  /// probability of this ray
[556]82  float mPdf;
[464]83 
[438]84  //////////////////////////////
[1133]85
86 
87  /// the kd node holding the termination point
88  KdNode *mTerminationNode;
89  /// the kd node holding the origin point
90  KdNode *mOriginNode;
91
[2014]92  VssRay() {}
93 
[464]94  VssRay(
95                 const Vector3 &origin,
[438]96                 const Vector3 &termination,
97                 Intersectable *originObject,
[464]98                 Intersectable *terminationObject,
[537]99                 const int pass = 0,
[556]100                 const float pdf = 1.0f
[1528]101                 );
[372]102       
[1528]103  VssRay(const Ray &ray);
[492]104       
[2609]105  void Init(const Ray &ray);
106
[1528]107  void Precompute();
[376]108
[372]109  void Mail() { mMailbox = mailID; }
110  static void NewMail() { mailID++; }
111  bool Mailed() const { return mMailbox == mailID; }
112
113  bool Mailed(const int mail) {
114    return mMailbox >= mailID + mail;
115  }
116
[438]117  int HitCount() const {
[386]118#if BIDIRECTIONAL_RAY
[438]119        if (mOriginObject && mTerminationObject)
120          return 2;
121        if (mOriginObject || mTerminationObject)
122          return 1;
123        return 0;
[386]124#else
[438]125        return (mTerminationObject) ? 1 : 0;
[386]126#endif
[438]127  }
[372]128       
[438]129  Vector3 GetOrigin() const { return mOrigin; }
[372]130  Vector3 GetTermination() const { return mTermination; }
131  Vector3 GetDir() const { return mTermination - mOrigin; }
132  //  Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); }
133  Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; }
134
[448]135  float Length() const { return Distance(mOrigin, mTermination); }
136
[438]137  Vector3 Extrap(const float t) const {
138        return GetOrigin() + t * GetDir();
139  }
[434]140       
[438]141  float GetDirParametrization(const int axis) const;
[492]142  float GetOpositeDirParametrization(const int axis) const;
[438]143
[2575]144  static float GetDirParam(const int axis, const Vector3 dir);
145  static Vector3 GetInvDirParam(const float alpha, const float beta);
[438]146
147  float GetSize() const { return  1.0f/mInvSize; }
148  float GetInvSize() const { return  mInvSize; }
[372]149  float GetOrigin(const int axis) const { return mOrigin[axis]; }
150  float GetTermination(const int axis) const { return mTermination[axis]; }
151  float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
152  float GetNormalizedDir(const int axis) const {
153    return (mTermination[axis] - mOrigin[axis])*mInvSize;
154  }
155       
156  bool
157  ComputeMinMaxT(const AxisAlignedBox3 &box,
[438]158                                 float &tmin,
159                                 float &tmax) const;
[372]160       
161  bool
162  Intersects(const AxisAlignedBox3 &box,
[438]163                         float &tmin,
164                         float &tmax) const;
[372]165       
166  bool
167  IntersectsSphere(const Vector3 &center,
[438]168                                   const float sqrRadius,
169                                   Vector3 &point,
170                                   float &t) const;
[372]171       
172  void
173  Translate(const Vector3 &translation) {
174    mOrigin += translation;
175    mTermination += translation;
176  }
[427]177
[438]178  void SetupEndPoints(const Vector3 &origin,
179                                          const Vector3 &termination)
180  {
181        mOrigin = origin;
182        mTermination = termination;
183        Precompute();
184  }
[427]185                                                                                       
[2234]186  inline bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
[372]187
[1112]188  char Flags() const { return mFlags;}  void SetFlags(char orFlag) { mFlags |= orFlag;}
[372]189 
190  bool IsActive() const { return mRefCount>0; }
191       
192  // reference counting for leaf nodes
193  int RefCount() const { return mRefCount; }
194  int Ref() { return mRefCount++; }
195       
196  void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
197  bool ScheduledForRemoval() const { return mRefCount<0; }
198  void Unref() {
199    if (mRefCount > 0)
200      mRefCount--;
201    else
202      if (mRefCount < 0)
[438]203                mRefCount++;
[372]204      else {
[2176]205                std::cerr<<"Trying to unref already deleted ray!"<<std::endl;
[438]206                exit(1);
[372]207      }
208  }
[434]209
[438]210  static Vector3
211  GetDirection(const float a, const float b) {
[1824]212        return GetInvDirParam(a, b);
213        //return Vector3(sin(a), sin(b), cos(a));
[438]214  }
[434]215
[492]216  friend bool GreaterWeightedPvsContribution(const VssRay * a,
217                                                                                         const VssRay *b) {
218        return a->mWeightedPvsContribution > b->mWeightedPvsContribution;
219  }
220
221  float SqrDistance(const Vector3 &point) const {
222        Vector3 diff = point - mOrigin;
223        float t = DotProd(diff, GetDir());
224       
225    if ( t <= 0.0f ) {
226          t = 0.0f;
227    } else {
228          if (t >= 1.0f) {
229                t = 1.0f;
230          } else {
231        t /= SqrMagnitude(GetDir());
232          }
233          diff -= t*GetDir();
234        }
235    return SqrMagnitude(diff);
236  }
[1259]237
[1528]238  /** Returns the data sampled on either the ray origin or termination.
239  */
[1259]240  void GetSampleData(
241          const bool isTerminaton,
242          Vector3 &pt,
243          Intersectable **obj,
244          KdNode **node) const;
245
[2176]246  friend std::ostream& operator<< (std::ostream &s, const VssRay &vssRay);
[1221]247   
[372]248};
249
[2233]250
[1528]251inline void VssRay::GetSampleData(const bool isTermination,
252                                                                  Vector3 &pt,
253                                                                  Intersectable **obj,
254                                                                  KdNode **node) const
255{
256        if (isTermination)
257        {
258                pt = mTermination;
259                *obj = mTerminationObject;
260                *node = mTerminationNode;
261        }
262        else
263        {
264                pt = mOrigin;
265                *obj = mOriginObject;
266                *node = mOriginNode;
267        }
268}
269
[2233]270
[446]271void
272GenerateExtendedConvexCombinationWeights(float &w1,
273                                                                                 float &w2,
274                                                                                 float &w3,
275                                                                                 const float overlap
276                                                                                 );
[372]277
[464]278void
279GenerateExtendedConvexCombinationWeights2(float &w1,
280                                                                                  float &w2,
281                                                                                  const float overlap
282                                                                                  );
283
[1221]284// Overload << operator for C++-style output
[2176]285inline std::ostream&
286operator<< (std::ostream &s, const VssRay &vssRay)
[1221]287{
288        return s
289                << "(" << vssRay.mPass << ", " << vssRay.mOrigin << ", " << vssRay.mTermination
290                << ", " << vssRay.mOriginObject << ", " << vssRay.mTerminationObject << ", " << vssRay.mPdf << ")";
291}
292
[446]293// --------------------------------------------------------------
294// For sorting rays
295// --------------------------------------------------------------
[1528]296struct SortableEntry
[446]297{
298  enum EType {
299    ERayMin,
300    ERayMax
301  };
[372]302
[446]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
[467]317struct VssRayContainer : public vector<VssRay *>
318{
[2176]319  void PrintStatistics(std::ostream &s);
[1112]320  int SelectRays(const int number, VssRayContainer &selected, const bool copy=false) const;
[2353]321  int GetContributingRays(VssRayContainer &selected, const int minPass) const;
[492]322 
[467]323};
[372]324
325
[2353]326struct RayPacket
327{
328        RayPacket(const VssRayContainer &rays)
329        {
330                for (int i = 0; i < 4; ++ i)
331                        mOriginX[i] = rays[i]->mOrigin[0];
332
333                for (int i = 0; i < 4; ++ i)
334                        mOriginY[i] = rays[i]->mOrigin[1];
335
336                for (int i = 0; i < 4; ++ i)
337                        mOriginZ[i] = rays[i]->mOrigin[2];
338        }
339#ifdef USE_SSE 
340        union { float mOriginX[4]; __m128 mOriginX4; };
341        union { float mOriginY[4]; __m128 mOriginY4; };
342        union { float mOriginZ[4]; __m128 mOriginZ4; };
343
344        union { float mTerminationX[4]; __m128 mTerminationX4; };
345        union { float mTerminationY[4]; __m128 mTerminationY4; };
346        union { float mTerminationZ[4]; __m128 mTerminationZ4; };
347#else
348        float mOriginX[4];
349        float mOriginY[4];
350        float mOriginZ[4];
351
352        float mTerminationX[4];
353        float mTerminationY[4];
354        float mTerminationZ[4];
355#endif
356        ViewCellContainer mViewCells[4];
[1715]357};
358
[2353]359
[1715]360};
361
[372]362#endif
Note: See TracBrowser for help on using the repository browser.