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

RevLine 
[162]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;
[308]13class ViewCell;
[362]14class BspLeaf;
[426]15class VssRay;
[162]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:
[209]25  enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT };
[369]26       
[162]27  enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
28
[350]29  /// if ray is on back (front) side of plane, or goes from the
30  /// front (back) to the back (front)
[349]31  enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
[374]32       
[191]33  struct Intersection {
[162]34    // the point of intersection
35    float mT;
[176]36
[162]37    // can be either mesh or a viewcell
38    Intersectable *mObject;
[369]39               
[162]40    // the face of the intersectable
41    int mFace;
[176]42
[191]43    Intersection(const float t,
[392]44                                 Intersectable *object,
45                                 const int face):mT(t), mObject(object), mFace(face) {}
[369]46               
[191]47    Intersection() {}
[369]48               
[162]49    bool operator<(
[369]50                                                                         const Intersection &b) const {
[162]51      return
[369]52                                mT
53                                <
54                                b.mT;
[162]55    }
56   
57  };
[366]58
[370]59  struct BspIntersection {
60    // the point of intersection
61    float mT;
[366]62
[370]63    BspLeaf *mLeaf;
64
65    BspIntersection(const float t, BspLeaf *l):
[374]66                        mT(t), mLeaf(l) {}
67               
[370]68    BspIntersection() {}
[374]69               
[370]70    bool operator<(const BspIntersection &b) const {
71      return mT <b.mT; }
72  };
73
[176]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 */
[191]77  Intersection sourceObject;
[176]78 
[191]79  vector<Intersection> intersections;
[370]80  vector<BspIntersection> bspIntersections;
[362]81  vector<KdLeaf *> kdLeaves;
[374]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       
[162]89  // constructors
90  Ray(const Vector3 &wherefrom,
91      const Vector3 &whichdir,
[374]92      const int _type):mFlags(0) {
[162]93    loc = wherefrom;
[209]94    if (_type == LINE_SEGMENT)
95      dir = whichdir;
96    else
97      dir = Normalize(whichdir);
[162]98    mType = _type;
99    depth = 0;
[374]100                Init();
[162]101  }
102  // dummy constructor
103  Ray() {}
[426]104
105  /** Construct ray from a vss ray.
106  */
107  Ray(const VssRay &vssRay);   
[374]108       
[191]109  Intersectable *GetIntersectionObject(const int i) const {
110    return intersections[i].mObject;
111  }
[162]112 
[191]113  Vector3 GetIntersectionPoint(const int i) const {
114    return Extrap(intersections[i].mT);
115  }
116 
[162]117  // Inititalize the ray again when already constructed
[245]118  void Init(const Vector3 &wherefrom,
[392]119                        const Vector3 &whichdir,
120                        const int _type,
121                        bool dirNormalized = false) {
[162]122    loc = wherefrom;
[245]123    dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ;
[162]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)
[328]171  int GetId() const { return ID; }
[162]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
[328]180  void SetId() {
[162]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.
[328]187  void SetId(int newID) {
[162]188    ID = newID;
189    // note that transfID is not changed!
190  }
191
192
193  // the object on which the ray starts at
[191]194  const Intersection* GetStartObject() const { return &intersections[0]; }
195  const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
[162]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 
[327]219  /** Classifies ray with respect to the plane.
220  */
[406]221  int ClassifyPlane(const Plane3 &plane,
222                                        const float minT,
223                                        const float maxT,
224                                        Vector3 &entP,
225                                        Vector3 &extP) const;
[327]226
[162]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;
[176]240 
[162]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)
[374]252  int depth;
253
254       
255       
[162]256  void Init();
257
258  // precompute some values that are necessary
259  void Precompute();
260
261  friend class AxisAlignedBox3;
[327]262  friend class Plane3;
[162]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);
[340]269
270        friend ostream &operator<<(ostream &s, const Ray &r) {
271                return s<<"Ray:loc="<<r.loc<<" dir="<<r.dir;
272        }
[162]273};
274
275
[191]276class PassingRaySet {
277public:
278  enum { Resolution = 2 };
279  int mDirectionalContributions[3*Resolution*Resolution];
280  int mRays;
281  int mContributions;
[369]282
283        PassingRaySet() {
[191]284    Reset();
285  }
[369]286
287        void
[191]288  Reset();
289 
290  void AddRay(const Ray &ray, const int contributions);
[369]291        void AddRay2(const Ray &ray,
292                                                         const int objects,
293                                                         const int viewcells);
294
[191]295  int GetEntryIndex(const Vector3 &direction) const;
[162]296
[191]297  friend ostream &operator<<(ostream &s, const PassingRaySet &set);
[162]298
[191]299};
300
[401]301struct SimpleRay
302{
303        Vector3 mOrigin;
304        Vector3 mDirection;
305        SimpleRay() {}
306        SimpleRay(const Vector3 &o, const Vector3 &d):mOrigin(o), mDirection(d) {}
307};
[191]308
[401]309typedef vector<SimpleRay> SimpleRayContainer;
310
[162]311#endif
312
Note: See TracBrowser for help on using the repository browser.