source: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h @ 563

Revision 563, 8.7 KB checked in by bittner, 19 years ago (diff)

rss sampling changes, preprocessor::GenerateRays?

Line 
1#ifndef __RAY_H__
2#define __RAY_H__
3
4#include <vector>
5#include "Matrix4x4.h"
6#include "Vector3.h"
7//#include "ViewCellBsp.h"
8
9// forward declarations
10class Plane3;
11class Intersectable;
12class KdLeaf;
13class MeshInstance;
14class ViewCell;
15class BspLeaf;
16class VssRay;
17
18
19// -------------------------------------------------------------------
20// CRay class.  A ray is defined by a location and a direction.
21// The direction is always normalized (length == 1).
22// -------------------------------------------------------------------
23
24class Ray
25{
26public:
27  enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT };
28       
29  enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
30
31  /// if ray is on back (front) side of plane, or goes from the
32  /// front (back) to the back (front)
33  enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
34       
35  struct Intersection {
36    // the point of intersection
37    float mT;
38
39    // can be either mesh or a viewcell
40    Intersectable *mObject;
41               
42    // the face of the intersectable
43    int mFace;
44
45    Intersection(const float t,
46                                 Intersectable *object,
47                                 const int face):mT(t), mObject(object), mFace(face) {}
48               
49                                 Intersection(): mT(0), mObject(NULL), mFace(0) {}
50               
51    bool operator<(
52                                   const Intersection &b) const {
53
54      return
55                                mT
56                                <
57                                b.mT;
58    }
59   
60  };
61
62
63  // I should have some abstract cell data type !!! here
64  // corresponds to the spatial elementary cell
65  /** intersection with the source object if any */
66  Intersection sourceObject;
67 
68  vector<Intersection> intersections;
69 // vector<BspIntersection> bspIntersections;
70  vector<KdLeaf *> kdLeaves;
71  vector<Intersectable *> testedObjects;
72
73  // various flags
74  enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4, CULL_BACKFACES=8};
75  int mFlags;
76
77       
78  // constructors
79  Ray(const Vector3 &wherefrom,
80      const Vector3 &whichdir,
81      const int _type):mFlags(CULL_BACKFACES) {
82    loc = wherefrom;
83    if (_type == LINE_SEGMENT)
84      dir = whichdir;
85    else
86      dir = Normalize(whichdir);
87    mType = _type;
88    depth = 0;
89                Init();
90  }
91  // dummy constructor
92  Ray() {}
93
94  /** Construct ray from a vss ray.
95  */
96  Ray(const VssRay &vssRay) {
97        Init(vssRay);
98        mFlags |= CULL_BACKFACES;
99  }
100
101  void Clear() {
102        intersections.clear();
103        kdLeaves.clear();
104        testedObjects.clear();
105        //      bspIntersections.clear();
106  }
107
108  void Init(const VssRay &vssRay);
109
110  Intersectable *GetIntersectionObject(const int i) const {
111    return intersections[i].mObject;
112  }
113 
114  Vector3 GetIntersectionPoint(const int i) const {
115    return Extrap(intersections[i].mT);
116  }
117 
118  // Inititalize the ray again when already constructed
119  void Init(const Vector3 &wherefrom,
120                        const Vector3 &whichdir,
121                        const int _type,
122                        bool dirNormalized = false) {
123    loc = wherefrom;
124    dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ;
125    mType = _type;
126    depth = 0;
127    Init();
128  }
129
130  // --------------------------------------------------------
131  // Extrapolate ray given a signed distance, returns a point
132  // --------------------------------------------------------
133  Vector3 Extrap(float t) const {
134    return loc + dir * t;
135  }
136
137  // -----------------------------------
138  // Return parameter given point on ray
139  // -----------------------------------
140  float Interp(Vector3 &x) const {
141    for (int i = 0; i < 3; i++)
142      if (Abs(dir[i]) > Limits::Small)
143        return (x[i] - loc[i]) / dir[i];
144    return 0;
145  }
146
147  // -----------------------------------
148  // Reflects direction of reflection for the ray,
149  // given the normal to the surface.
150  // -----------------------------------
151  Vector3 ReflectRay(const Vector3 &N) const {
152    return N * 2.0 * DotProd(N, -dir) + dir;
153  }
154  void ReflectRay(Vector3 &result, const Vector3 &N) const {
155    result =  N * 2.0 * DotProd(N, -dir) + dir;
156  }
157
158  // Computes the inverted direction of the ray, used optionally by
159  // a ray traversal algorithm.
160  void ComputeInvertedDir() const;
161
162  // Given the matrix 4x4, transforms the ray to another space
163  void ApplyTransform(const Matrix4x4 &tform) {
164    loc = tform * loc;
165    dir = RotateOnly(tform, dir);
166    // note that normalization to the unit size of the direction
167    // is NOT computed -- this is what we want.
168    Precompute();
169  }
170
171  // returns ID of this ray (use for mailboxes)
172  int GetId() const { return ID; }
173
174  // returns the transfrom ID of the ray (use for ray transformations)
175  int GetTransformID() const { return transfID; }
176
177  // copy the transform ID from an input ray
178  void CopyTransformID(const Ray &ray) { transfID = ray.transfID; }
179 
180  // set unique ID for a given ray - always avoid setting to zero
181  void SetId() {
182    if ((ID = ++genID) == 0)
183      ID = ++genID;
184    transfID = ID;
185  }
186  // set ID to explicit value - it can be even 0 for rays transformed
187  // to the canonical object space to supress the mailbox failure.
188  void SetId(int newID) {
189    ID = newID;
190    // note that transfID is not changed!
191  }
192
193
194  // the object on which the ray starts at
195  const Intersection* GetStartObject() const { return &intersections[0]; }
196  const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
197 
198 
199  void SetLoc(const Vector3 &l);
200  Vector3& GetLoc() { return loc; }
201  Vector3  GetLoc() const { return loc; }
202
203  float  GetLoc(const int axis) const { return loc[axis]; }
204
205  void SetDir(const Vector3 &ndir) { dir = ndir;}
206  Vector3& GetDir() { return dir; }
207  Vector3  GetDir() const { return dir; }
208  float  GetDir(const int axis) const { return dir[axis]; }
209
210  int GetType() const { return mType; }
211 
212  // make such operation to slightly change the ray direction
213  // in case any component of ray direction is zero.
214  void CorrectZeroComponents();
215
216  // the depth of the ray - primary rays are in the depth 0
217  int GetDepth() const { return depth;}
218  void SetDepth(int newDepth) { depth = newDepth;}
219 
220  /** Classifies ray with respect to the plane.
221  */
222  int ClassifyPlane(const Plane3 &plane,
223                                        const float minT,
224                                        const float maxT,
225                                        Vector3 &entP,
226                                        Vector3 &extP) const;
227
228private:
229  Vector3 loc, dir;             // Describes ray origin and vector
230 
231  // The inverted direction of the ray components. It is computed optionally
232  // by the ray traversal algorithm using function ComputeInvertedDir();
233  mutable Vector3 invDir;
234
235  // Type of the ray: primary, shadow, dummy etc., see ERayType above
236  int mType;
237 
238 
239  // unique ID of a ray for the use in the mailboxes
240  int ID;
241 
242  // unique ID of a ray for the use with a transformations - this one
243  // never can be changed that allows the nesting of transformations
244  // and caching the transformed rays correctly
245  int transfID;
246 
247  // the ID generator fo each ray instantiated
248  static int genID; 
249
250  // When ray shot from the source(camera/light), this number is equal
251  // to the number of bounces of the ray, also called the depth of the
252  // ray (primary ray has its depth zero)
253  int depth;
254
255       
256       
257  void Init();
258
259  // precompute some values that are necessary
260  void Precompute();
261
262  friend class AxisAlignedBox3;
263  friend class Plane3;
264
265  // for CKDR GEMS
266  friend float DistanceToXPlane(const Vector3 &vec, const Ray &ray);
267  friend float DistanceToYPlane(const Vector3 &vec, const Ray &ray);
268  friend float DistanceToZPlane(const Vector3 &vec, const Ray &ray);
269  friend int MakeIntersectLine(const Plane3 &p, const Plane3 &q, Ray &ray);
270
271        friend ostream &operator<<(ostream &s, const Ray &r) {
272                return s<<"Ray:loc="<<r.loc<<" dir="<<r.dir;
273        }
274};
275
276
277class PassingRaySet {
278public:
279  enum { Resolution = 2 };
280  int mDirectionalContributions[3*Resolution*Resolution];
281  int mRays;
282  int mContributions;
283
284        PassingRaySet() {
285    Reset();
286  }
287
288        void
289  Reset();
290 
291  void AddRay(const Ray &ray, const int contributions);
292        void AddRay2(const Ray &ray,
293                                                         const int objects,
294                                                         const int viewcells);
295
296  int GetEntryIndex(const Vector3 &direction) const;
297
298  friend ostream &operator<<(ostream &s, const PassingRaySet &set);
299
300};
301
302struct SimpleRay
303{
304  Vector3 mOrigin;
305  Vector3 mDirection;
306  float mPdf;
307
308  SimpleRay() {}
309  SimpleRay(const Vector3 &o, const Vector3 &d, const float p=1.0f):
310        mOrigin(o), mDirection(d), mPdf(p) {}
311};
312
313class SimpleRayContainer : public vector<SimpleRay>
314{
315public:
316 
317  float mSumPdf;
318 
319  SimpleRayContainer():vector<SimpleRay>(), mSumPdf(0.0f) {}
320 
321  void NormalizePdf() {
322        iterator it = begin();
323        float c = 1.0f/mSumPdf;
324        for (; it != end(); it++)
325          (*it).mPdf*=c;
326  }
327
328  void AddRay(const SimpleRay &ray) {
329        push_back(ray);
330        mSumPdf += ray.mPdf;
331  }
332};
333
334#endif
335
Note: See TracBrowser for help on using the repository browser.