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

Revision 1528, 7.5 KB checked in by mattausch, 18 years ago (diff)

worked on gvs

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