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

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

started kd based bottom-up view cells

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,
217                                        const float minT,
218                                        const float maxT,
219                                        Vector3 &entP,
220                                        Vector3 &extP) const;
221
222private:
223  Vector3 loc, dir;             // Describes ray origin and vector
224 
225  // The inverted direction of the ray components. It is computed optionally
226  // by the ray traversal algorithm using function ComputeInvertedDir();
227  mutable Vector3 invDir;
228
229  // Type of the ray: primary, shadow, dummy etc., see ERayType above
230  int mType;
231 
232 
233  // unique ID of a ray for the use in the mailboxes
234  int ID;
235 
236  // unique ID of a ray for the use with a transformations - this one
237  // never can be changed that allows the nesting of transformations
238  // and caching the transformed rays correctly
239  int transfID;
240 
241  // the ID generator fo each ray instantiated
242  static int genID; 
243
244  // When ray shot from the source(camera/light), this number is equal
245  // to the number of bounces of the ray, also called the depth of the
246  // ray (primary ray has its depth zero)
247  int depth;
248
249       
250       
251  void Init();
252
253  // precompute some values that are necessary
254  void Precompute();
255
256  friend class AxisAlignedBox3;
257  friend class Plane3;
258
259  // for CKDR GEMS
260  friend float DistanceToXPlane(const Vector3 &vec, const Ray &ray);
261  friend float DistanceToYPlane(const Vector3 &vec, const Ray &ray);
262  friend float DistanceToZPlane(const Vector3 &vec, const Ray &ray);
263  friend int MakeIntersectLine(const Plane3 &p, const Plane3 &q, Ray &ray);
264
265        friend ostream &operator<<(ostream &s, const Ray &r) {
266                return s<<"Ray:loc="<<r.loc<<" dir="<<r.dir;
267        }
268};
269
270
271class PassingRaySet {
272public:
273  enum { Resolution = 2 };
274  int mDirectionalContributions[3*Resolution*Resolution];
275  int mRays;
276  int mContributions;
277
278        PassingRaySet() {
279    Reset();
280  }
281
282        void
283  Reset();
284 
285  void AddRay(const Ray &ray, const int contributions);
286        void AddRay2(const Ray &ray,
287                                                         const int objects,
288                                                         const int viewcells);
289
290  int GetEntryIndex(const Vector3 &direction) const;
291
292  friend ostream &operator<<(ostream &s, const PassingRaySet &set);
293
294};
295
296struct SimpleRay
297{
298        Vector3 mOrigin;
299        Vector3 mDirection;
300        SimpleRay() {}
301        SimpleRay(const Vector3 &o, const Vector3 &d):mOrigin(o), mDirection(d) {}
302};
303
304typedef vector<SimpleRay> SimpleRayContainer;
305
306#endif
307
Note: See TracBrowser for help on using the repository browser.