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

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

bsp merging possible again

Line 
1#ifndef __RAY_H__
2#define __RAY_H__
3
4#include <vector>
5#include "Matrix4x4.h"
6#include "Vector3.h"
7//#include "ViewCellBsp.h"
8
9// forward declarations
10class Plane3;
11class Intersectable;
12class KdLeaf;
13class MeshInstance;
14class ViewCell;
15class BspLeaf;
16class VssRay;
17
18
19// -------------------------------------------------------------------
20// CRay class.  A ray is defined by a location and a direction.
21// The direction is always normalized (length == 1).
22// -------------------------------------------------------------------
23
24class Ray
25{
26public:
27  enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT };
28       
29  enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
30
31  /// if ray is on back (front) side of plane, or goes from the
32  /// front (back) to the back (front)
33  enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
34       
35  struct Intersection {
36    // the point of intersection
37    float mT;
38
39    // can be either mesh or a viewcell
40    Intersectable *mObject;
41               
42    // the face of the intersectable
43    int mFace;
44
45    Intersection(const float t,
46                                 Intersectable *object,
47                                 const int face):mT(t), mObject(object), mFace(face) {}
48               
49                                 Intersection(): mT(0), mObject(NULL), mFace(0) {}
50               
51    bool operator<(
52                                   const Intersection &b) const {
53
54      return
55                                mT
56                                <
57                                b.mT;
58    }
59   
60  };
61
62
63  // I should have some abstract cell data type !!! here
64  // corresponds to the spatial elementary cell
65  /** intersection with the source object if any */
66  Intersection sourceObject;
67 
68  vector<Intersection> intersections;
69 // vector<BspIntersection> bspIntersections;
70  vector<KdLeaf *> kdLeaves;
71  vector<Intersectable *> testedObjects;
72
73        // various flags
74        enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4};
75        int mFlags;
76
77       
78  // constructors
79  Ray(const Vector3 &wherefrom,
80      const Vector3 &whichdir,
81      const int _type):mFlags(0) {
82    loc = wherefrom;
83    if (_type == LINE_SEGMENT)
84      dir = whichdir;
85    else
86      dir = Normalize(whichdir);
87    mType = _type;
88    depth = 0;
89                Init();
90  }
91  // dummy constructor
92  Ray() {}
93
94  /** Construct ray from a vss ray.
95  */
96  Ray(const VssRay &vssRay) {
97        Init(vssRay);
98  }
99
100  void Clear() {
101        intersections.clear();
102        kdLeaves.clear();
103        testedObjects.clear();
104        //      bspIntersections.clear();
105  }
106
107  void Init(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.