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

RevLine 
[376]1#ifndef __VSS_RAY_H
2#define __VSS_RAY_H
[372]3
4#include <vector>
5using namespace std;
6#include "Vector3.h"
[410]7#include "Ray.h"
[492]8#include "Containers.h"
[372]9
[860]10namespace GtpVisibilityPreprocessor {
11
[372]12class AxisAlignedBox3;
13class Intersectable;
[1133]14class KdNode;
[372]15
[1528]16
[372]17class VssRay {
18public:
[1528]19
20 // various flags
[372]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
[438]27  static int mailID;
[372]28  int mMailbox;
29       
[438]30  // side of the ray - used for the ray classification
31  //  char mSide;
[372]32       
[438]33  // computed t
[463]34  //  float mT;
[372]35
[438]36  // inverse of the ray size
37  float mInvSize;
38 
39  // counter of references to this ray
[372]40  short mRefCount;
[438]41 
42  // various flags
[372]43  char mFlags;
44       
45  Vector3 mOrigin;
46  Vector3 mTermination;
47       
[438]48  /// Termination object for the ray
49  /// only the termination object is actually used
50  Intersectable *mOriginObject;
51  Intersectable *mTerminationObject;
[464]52
[492]53  ViewCellContainer mViewCells;
54 
[1133]55
[464]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;
[492]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;
[534]71
72  // probability of this ray
[556]73  float mPdf;
[464]74 
[438]75  //////////////////////////////
[1133]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
[464]84  VssRay(
85                 const Vector3 &origin,
[438]86                 const Vector3 &termination,
87                 Intersectable *originObject,
[464]88                 Intersectable *terminationObject,
[537]89                 const int pass = 0,
[556]90                 const float pdf = 1.0f
[1528]91                 );
[372]92       
[1528]93  VssRay(const Ray &ray);
[492]94       
95 
[1528]96  void Precompute();
[376]97
[372]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
[438]106  int HitCount() const {
[386]107#if BIDIRECTIONAL_RAY
[438]108        if (mOriginObject && mTerminationObject)
109          return 2;
110        if (mOriginObject || mTerminationObject)
111          return 1;
112        return 0;
[386]113#else
[438]114        return (mTerminationObject) ? 1 : 0;
[386]115#endif
[438]116  }
[372]117       
[438]118  Vector3 GetOrigin() const { return mOrigin; }
[372]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
[448]124  float Length() const { return Distance(mOrigin, mTermination); }
125
[438]126  Vector3 Extrap(const float t) const {
127        return GetOrigin() + t * GetDir();
128  }
[434]129       
[438]130  float GetDirParametrization(const int axis) const;
[492]131  float GetOpositeDirParametrization(const int axis) const;
[438]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; }
[372]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,
[438]146                                 float &tmin,
147                                 float &tmax) const;
[372]148       
149  bool
150  Intersects(const AxisAlignedBox3 &box,
[438]151                         float &tmin,
152                         float &tmax) const;
[372]153       
154  bool
155  IntersectsSphere(const Vector3 &center,
[438]156                                   const float sqrRadius,
157                                   Vector3 &point,
158                                   float &t) const;
[372]159       
160  void
161  Translate(const Vector3 &translation) {
162    mOrigin += translation;
163    mTermination += translation;
164  }
[427]165
[438]166  void SetupEndPoints(const Vector3 &origin,
167                                          const Vector3 &termination)
168  {
169        mOrigin = origin;
170        mTermination = termination;
171        Precompute();
172  }
[427]173                                                                                       
[372]174  bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
175
[1112]176  char Flags() const { return mFlags;}  void SetFlags(char orFlag) { mFlags |= orFlag;}
[372]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)
[438]191                mRefCount++;
[372]192      else {
[438]193                cerr<<"Trying to unref already deleted ray!"<<endl;
194                exit(1);
[372]195      }
196  }
[434]197
[438]198  static Vector3
199  GetDirection(const float a, const float b) {
200        return Vector3(sin(a), sin(b), cos(a));
201  }
[434]202
[492]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  }
[1259]224
[1528]225  /** Returns the data sampled on either the ray origin or termination.
226  */
[1259]227  void GetSampleData(
228          const bool isTerminaton,
229          Vector3 &pt,
230          Intersectable **obj,
231          KdNode **node) const;
232
[1221]233  friend ostream& operator<< (ostream &s, const VssRay &vssRay);
234   
[372]235};
236
[1528]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
[446]256void
257GenerateExtendedConvexCombinationWeights(float &w1,
258                                                                                 float &w2,
259                                                                                 float &w3,
260                                                                                 const float overlap
261                                                                                 );
[372]262
[464]263void
264GenerateExtendedConvexCombinationWeights2(float &w1,
265                                                                                  float &w2,
266                                                                                  const float overlap
267                                                                                  );
268
[1221]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
[446]278// --------------------------------------------------------------
279// For sorting rays
280// --------------------------------------------------------------
[1528]281struct SortableEntry
[446]282{
283  enum EType {
284    ERayMin,
285    ERayMax
286  };
[372]287
[446]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
[467]302struct VssRayContainer : public vector<VssRay *>
303{
304  void PrintStatistics(ostream &s);
[1112]305  int SelectRays(const int number, VssRayContainer &selected, const bool copy=false) const;
[492]306  int
307  GetContributingRays(VssRayContainer &selected,
308                                          const int minPass
309                                          ) const;
310 
[467]311};
[372]312
[863]313};
[372]314
315#endif
Note: See TracBrowser for help on using the repository browser.