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

Revision 209, 7.0 KB checked in by bittner, 19 years ago (diff)

data added

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