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

Revision 191, 6.9 KB checked in by bittner, 19 years ago (diff)

basic sampling strategies

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 Intersection {
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    Intersection(const float t,
38                 Intersectable *object,
39                 const int face):mT(t), mObject(object), mFace(face) {}
40
41    Intersection() {}
42
43    bool operator<(
44                   const Intersection &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  Intersection sourceObject;
57 
58  vector<Intersection> 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  Intersectable *GetIntersectionObject(const int i) const {
77    return intersections[i].mObject;
78  }
79 
80  Vector3 GetIntersectionPoint(const int i) const {
81    return Extrap(intersections[i].mT);
82  }
83 
84  // Inititalize the ray again when already constructed
85  void Init(const Vector3 &wherefrom, const Vector3 &whichdir,
86            const int _type,
87            bool dirNormalized = false) {
88    loc = wherefrom;
89    dir = (dirNormalized) ? whichdir: Normalize(whichdir) ;
90    mType = _type;
91    depth = 0;
92    Init();
93  }
94
95  // --------------------------------------------------------
96  // Extrapolate ray given a signed distance, returns a point
97  // --------------------------------------------------------
98  Vector3 Extrap(float t) const {
99    return loc + dir * t;
100  }
101
102  // -----------------------------------
103  // Return parameter given point on ray
104  // -----------------------------------
105  float Interp(Vector3 &x) const {
106    for (int i = 0; i < 3; i++)
107      if (Abs(dir[i]) > Limits::Small)
108        return (x[i] - loc[i]) / dir[i];
109    return 0;
110  }
111
112  // -----------------------------------
113  // Reflects direction of reflection for the ray,
114  // given the normal to the surface.
115  // -----------------------------------
116  Vector3 ReflectRay(const Vector3 &N) const {
117    return N * 2.0 * DotProd(N, -dir) + dir;
118  }
119  void ReflectRay(Vector3 &result, const Vector3 &N) const {
120    result =  N * 2.0 * DotProd(N, -dir) + dir;
121  }
122
123  // Computes the inverted direction of the ray, used optionally by
124  // a ray traversal algorithm.
125  void ComputeInvertedDir() const;
126
127  // Given the matrix 4x4, transforms the ray to another space
128  void ApplyTransform(const Matrix4x4 &tform) {
129    loc = tform * loc;
130    dir = RotateOnly(tform, dir);
131    // note that normalization to the unit size of the direction
132    // is NOT computed -- this is what we want.
133    Precompute();
134  }
135
136  // returns ID of this ray (use for mailboxes)
137  int GetID() const { return ID; }
138
139  // returns the transfrom ID of the ray (use for ray transformations)
140  int GetTransformID() const { return transfID; }
141
142  // copy the transform ID from an input ray
143  void CopyTransformID(const Ray &ray) { transfID = ray.transfID; }
144 
145  // set unique ID for a given ray - always avoid setting to zero
146  void SetID() {
147    if ((ID = ++genID) == 0)
148      ID = ++genID;
149    transfID = ID;
150  }
151  // set ID to explicit value - it can be even 0 for rays transformed
152  // to the canonical object space to supress the mailbox failure.
153  void SetID(int newID) {
154    ID = newID;
155    // note that transfID is not changed!
156  }
157
158
159  // the object on which the ray starts at
160  const Intersection* GetStartObject() const { return &intersections[0]; }
161  const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
162 
163 
164  void SetLoc(const Vector3 &l);
165  Vector3& GetLoc() { return loc; }
166  Vector3  GetLoc() const { return loc; }
167
168  float  GetLoc(const int axis) const { return loc[axis]; }
169
170  void SetDir(const Vector3 &ndir) { dir = ndir;}
171  Vector3& GetDir() { return dir; }
172  Vector3  GetDir() const { return dir; }
173  float  GetDir(const int axis) const { return dir[axis]; }
174
175  int GetType() const { return mType; }
176 
177  // make such operation to slightly change the ray direction
178  // in case any component of ray direction is zero.
179  void CorrectZeroComponents();
180
181  // the depth of the ray - primary rays are in the depth 0
182  int GetDepth() const { return depth;}
183  void SetDepth(int newDepth) { depth = newDepth;}
184 
185private:
186  Vector3 loc, dir;             // Describes ray origin and vector
187 
188  // The inverted direction of the ray components. It is computed optionally
189  // by the ray traversal algorithm using function ComputeInvertedDir();
190  mutable Vector3 invDir;
191
192  // Type of the ray: primary, shadow, dummy etc., see ERayType above
193  int mType;
194 
195 
196  // unique ID of a ray for the use in the mailboxes
197  int ID;
198 
199  // unique ID of a ray for the use with a transformations - this one
200  // never can be changed that allows the nesting of transformations
201  // and caching the transformed rays correctly
202  int transfID;
203 
204  // the ID generator fo each ray instantiated
205  static int genID; 
206
207  // When ray shot from the source(camera/light), this number is equal
208  // to the number of bounces of the ray, also called the depth of the
209  // ray (primary ray has its depth zero)
210  int      depth;
211 
212 
213  void Init();
214
215  // precompute some values that are necessary
216  void Precompute();
217
218  friend class AxisAlignedBox3;
219  friend class Plane;
220
221  // for CKDR GEMS
222  friend float DistanceToXPlane(const Vector3 &vec, const Ray &ray);
223  friend float DistanceToYPlane(const Vector3 &vec, const Ray &ray);
224  friend float DistanceToZPlane(const Vector3 &vec, const Ray &ray);
225  friend int MakeIntersectLine(const Plane3 &p, const Plane3 &q, Ray &ray);
226};
227
228
229class PassingRaySet {
230public:
231  enum { Resolution = 2 };
232  int mDirectionalContributions[3*Resolution*Resolution];
233  int mRays;
234  int mContributions;
235  PassingRaySet() {
236    Reset();
237  }
238  void
239  Reset();
240 
241  void AddRay(const Ray &ray, const int contributions);
242  int GetEntryIndex(const Vector3 &direction) const;
243
244  friend ostream &operator<<(ostream &s, const PassingRaySet &set);
245
246};
247
248
249#endif
250
Note: See TracBrowser for help on using the repository browser.