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

Revision 1824, 8.2 KB checked in by bittner, 18 years ago (diff)

global lines support

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