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

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

vsspreprocessor updates

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