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

Revision 401, 8.2 KB checked in by bittner, 19 years ago (diff)

vsspreprocessor updates

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