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

Revision 176, 6.3 KB checked in by bittner, 19 years ago (diff)

cosine sampling bug fixed

Line 
1#ifndef __RAY_H__
2#define __RAY_H__
3
4#include <vector>
5#include "Matrix4x4.h"
6#include "Vector3.h"
7
8// forward declarations
9class Plane3;
10class Intersectable;
11class KdLeaf;
12class MeshInstance;
13
14
15// -------------------------------------------------------------------
16// CRay class.  A ray is defined by a location and a direction.
17// The direction is always normalized (length == 1).
18// -------------------------------------------------------------------
19
20class Ray
21{
22public:
23  enum RayType { LOCAL_RAY, GLOBAL_RAY };
24
25  enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
26
27  struct RayIntersection {
28    // the point of intersection
29    float mT;
30
31    // can be either mesh or a viewcell
32    Intersectable *mObject;
33
34    // the face of the intersectable
35    int mFace;
36
37    RayIntersection(const float t,
38                    Intersectable *object,
39                    const int face):mT(t), mObject(object), mFace(face) {}
40
41    RayIntersection() {}
42
43    bool operator<(
44                   const RayIntersection &b) const {
45      return
46        mT
47        <
48        b.mT;
49    }
50   
51  };
52 
53  // I should have some abstract cell data type !!! here
54  // corresponds to the spatial elementary cell
55  /** intersection with the source object if any */
56  RayIntersection sourceObject;
57 
58  vector<RayIntersection> intersections;
59  vector<KdLeaf *> leaves;
60  vector<MeshInstance *> meshes;
61
62  // constructors
63  Ray(const Vector3 &wherefrom,
64      const Vector3 &whichdir,
65      const int _type,
66      const void *_originCell = NULL) {
67    loc = wherefrom;
68    dir = Normalize(whichdir);
69    mType = _type;
70    depth = 0;
71    Init();
72  }
73  // dummy constructor
74  Ray() {}
75
76 
77  // Inititalize the ray again when already constructed
78  void Init(const Vector3 &wherefrom, const Vector3 &whichdir,
79            const int _type,
80            bool dirNormalized = false) {
81    loc = wherefrom;
82    dir = (dirNormalized) ? whichdir: Normalize(whichdir) ;
83    mType = _type;
84    depth = 0;
85    Init();
86  }
87
88  // --------------------------------------------------------
89  // Extrapolate ray given a signed distance, returns a point
90  // --------------------------------------------------------
91  Vector3 Extrap(float t) const {
92    return loc + dir * t;
93  }
94
95  // -----------------------------------
96  // Return parameter given point on ray
97  // -----------------------------------
98  float Interp(Vector3 &x) const {
99    for (int i = 0; i < 3; i++)
100      if (Abs(dir[i]) > Limits::Small)
101        return (x[i] - loc[i]) / dir[i];
102    return 0;
103  }
104
105  // -----------------------------------
106  // Reflects direction of reflection for the ray,
107  // given the normal to the surface.
108  // -----------------------------------
109  Vector3 ReflectRay(const Vector3 &N) const {
110    return N * 2.0 * DotProd(N, -dir) + dir;
111  }
112  void ReflectRay(Vector3 &result, const Vector3 &N) const {
113    result =  N * 2.0 * DotProd(N, -dir) + dir;
114  }
115
116  // Computes the inverted direction of the ray, used optionally by
117  // a ray traversal algorithm.
118  void ComputeInvertedDir() const;
119
120  // Given the matrix 4x4, transforms the ray to another space
121  void ApplyTransform(const Matrix4x4 &tform) {
122    loc = tform * loc;
123    dir = RotateOnly(tform, dir);
124    // note that normalization to the unit size of the direction
125    // is NOT computed -- this is what we want.
126    Precompute();
127  }
128
129  // returns ID of this ray (use for mailboxes)
130  int GetID() const { return ID; }
131
132  // returns the transfrom ID of the ray (use for ray transformations)
133  int GetTransformID() const { return transfID; }
134
135  // copy the transform ID from an input ray
136  void CopyTransformID(const Ray &ray) { transfID = ray.transfID; }
137 
138  // set unique ID for a given ray - always avoid setting to zero
139  void SetID() {
140    if ((ID = ++genID) == 0)
141      ID = ++genID;
142    transfID = ID;
143  }
144  // set ID to explicit value - it can be even 0 for rays transformed
145  // to the canonical object space to supress the mailbox failure.
146  void SetID(int newID) {
147    ID = newID;
148    // note that transfID is not changed!
149  }
150
151
152  // the object on which the ray starts at
153  const RayIntersection* GetStartObject() const { return &intersections[0]; }
154  const RayIntersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
155 
156 
157  void SetLoc(const Vector3 &l);
158  Vector3& GetLoc() { return loc; }
159  Vector3  GetLoc() const { return loc; }
160
161  float  GetLoc(const int axis) const { return loc[axis]; }
162
163  void SetDir(const Vector3 &ndir) { dir = ndir;}
164  Vector3& GetDir() { return dir; }
165  Vector3  GetDir() const { return dir; }
166  float  GetDir(const int axis) const { return dir[axis]; }
167
168  int GetType() const { return mType; }
169 
170  // make such operation to slightly change the ray direction
171  // in case any component of ray direction is zero.
172  void CorrectZeroComponents();
173
174  // the depth of the ray - primary rays are in the depth 0
175  int GetDepth() const { return depth;}
176  void SetDepth(int newDepth) { depth = newDepth;}
177 
178private:
179  Vector3 loc, dir;             // Describes ray origin and vector
180 
181  // The inverted direction of the ray components. It is computed optionally
182  // by the ray traversal algorithm using function ComputeInvertedDir();
183  mutable Vector3 invDir;
184
185  // Type of the ray: primary, shadow, dummy etc., see ERayType above
186  int mType;
187 
188 
189  // unique ID of a ray for the use in the mailboxes
190  int ID;
191 
192  // unique ID of a ray for the use with a transformations - this one
193  // never can be changed that allows the nesting of transformations
194  // and caching the transformed rays correctly
195  int transfID;
196 
197  // the ID generator fo each ray instantiated
198  static int genID; 
199
200  // When ray shot from the source(camera/light), this number is equal
201  // to the number of bounces of the ray, also called the depth of the
202  // ray (primary ray has its depth zero)
203  int      depth;
204 
205 
206  void Init();
207
208  // precompute some values that are necessary
209  void Precompute();
210
211  friend class AxisAlignedBox3;
212  friend class Plane;
213
214  // for CKDR GEMS
215  friend float DistanceToXPlane(const Vector3 &vec, const Ray &ray);
216  friend float DistanceToYPlane(const Vector3 &vec, const Ray &ray);
217  friend float DistanceToZPlane(const Vector3 &vec, const Ray &ray);
218  friend int MakeIntersectLine(const Plane3 &p, const Plane3 &q, Ray &ray);
219};
220
221
222
223
224#endif
225
Note: See TracBrowser for help on using the repository browser.