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

Revision 370, 7.9 KB checked in by mattausch, 19 years ago (diff)

fixed specular bug in trees
added batched query manager
added t information to ray bsp leaves

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