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

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