source: GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h @ 1584

Revision 1584, 9.1 KB checked in by bittner, 18 years ago (diff)

functional ray casting - fixed computeinvdir issue

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