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

Revision 426, 8.3 KB checked in by mattausch, 19 years ago (diff)

fixed ray bug in vspkdtree
added visualizations

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