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

Revision 448, 8.4 KB checked in by mattausch, 19 years ago (diff)

fixed bug in VspBspTree?
view cells in VssPreprocessor?
bounding rays for vspkdtree

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
[448]17
[162]18// -------------------------------------------------------------------
19// CRay class.  A ray is defined by a location and a direction.
20// The direction is always normalized (length == 1).
21// -------------------------------------------------------------------
22
23class Ray
24{
25public:
[209]26  enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT };
[369]27       
[162]28  enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
29
[350]30  /// if ray is on back (front) side of plane, or goes from the
31  /// front (back) to the back (front)
[349]32  enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
[374]33       
[191]34  struct Intersection {
[162]35    // the point of intersection
36    float mT;
[176]37
[162]38    // can be either mesh or a viewcell
39    Intersectable *mObject;
[369]40               
[162]41    // the face of the intersectable
42    int mFace;
[176]43
[191]44    Intersection(const float t,
[392]45                                 Intersectable *object,
46                                 const int face):mT(t), mObject(object), mFace(face) {}
[369]47               
[428]48                                 Intersection(): mT(0), mObject(NULL), mFace(0) {}
[369]49               
[162]50    bool operator<(
[448]51                                   const Intersection &b) const {
52
[162]53      return
[369]54                                mT
55                                <
56                                b.mT;
[162]57    }
58   
59  };
[366]60
[370]61  struct BspIntersection {
62    // the point of intersection
63    float mT;
[366]64
[370]65    BspLeaf *mLeaf;
66
67    BspIntersection(const float t, BspLeaf *l):
[374]68                        mT(t), mLeaf(l) {}
69               
[370]70    BspIntersection() {}
[374]71               
[370]72    bool operator<(const BspIntersection &b) const {
73      return mT <b.mT; }
74  };
75
[176]76  // I should have some abstract cell data type !!! here
77  // corresponds to the spatial elementary cell
78  /** intersection with the source object if any */
[191]79  Intersection sourceObject;
[176]80 
[191]81  vector<Intersection> intersections;
[370]82  vector<BspIntersection> bspIntersections;
[362]83  vector<KdLeaf *> kdLeaves;
[374]84  vector<Intersectable *> testedObjects;
85
86        // various flags
87        enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4};
88        int mFlags;
89
90       
[162]91  // constructors
92  Ray(const Vector3 &wherefrom,
93      const Vector3 &whichdir,
[374]94      const int _type):mFlags(0) {
[162]95    loc = wherefrom;
[209]96    if (_type == LINE_SEGMENT)
97      dir = whichdir;
98    else
99      dir = Normalize(whichdir);
[162]100    mType = _type;
101    depth = 0;
[374]102                Init();
[162]103  }
104  // dummy constructor
105  Ray() {}
[426]106
107  /** Construct ray from a vss ray.
108  */
109  Ray(const VssRay &vssRay);   
[374]110       
[191]111  Intersectable *GetIntersectionObject(const int i) const {
112    return intersections[i].mObject;
113  }
[162]114 
[191]115  Vector3 GetIntersectionPoint(const int i) const {
116    return Extrap(intersections[i].mT);
117  }
118 
[162]119  // Inititalize the ray again when already constructed
[245]120  void Init(const Vector3 &wherefrom,
[392]121                        const Vector3 &whichdir,
122                        const int _type,
123                        bool dirNormalized = false) {
[162]124    loc = wherefrom;
[245]125    dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ;
[162]126    mType = _type;
127    depth = 0;
128    Init();
129  }
130
131  // --------------------------------------------------------
132  // Extrapolate ray given a signed distance, returns a point
133  // --------------------------------------------------------
134  Vector3 Extrap(float t) const {
135    return loc + dir * t;
136  }
137
138  // -----------------------------------
139  // Return parameter given point on ray
140  // -----------------------------------
141  float Interp(Vector3 &x) const {
142    for (int i = 0; i < 3; i++)
143      if (Abs(dir[i]) > Limits::Small)
144        return (x[i] - loc[i]) / dir[i];
145    return 0;
146  }
147
148  // -----------------------------------
149  // Reflects direction of reflection for the ray,
150  // given the normal to the surface.
151  // -----------------------------------
152  Vector3 ReflectRay(const Vector3 &N) const {
153    return N * 2.0 * DotProd(N, -dir) + dir;
154  }
155  void ReflectRay(Vector3 &result, const Vector3 &N) const {
156    result =  N * 2.0 * DotProd(N, -dir) + dir;
157  }
158
159  // Computes the inverted direction of the ray, used optionally by
160  // a ray traversal algorithm.
161  void ComputeInvertedDir() const;
162
163  // Given the matrix 4x4, transforms the ray to another space
164  void ApplyTransform(const Matrix4x4 &tform) {
165    loc = tform * loc;
166    dir = RotateOnly(tform, dir);
167    // note that normalization to the unit size of the direction
168    // is NOT computed -- this is what we want.
169    Precompute();
170  }
171
172  // returns ID of this ray (use for mailboxes)
[328]173  int GetId() const { return ID; }
[162]174
175  // returns the transfrom ID of the ray (use for ray transformations)
176  int GetTransformID() const { return transfID; }
177
178  // copy the transform ID from an input ray
179  void CopyTransformID(const Ray &ray) { transfID = ray.transfID; }
180 
181  // set unique ID for a given ray - always avoid setting to zero
[328]182  void SetId() {
[162]183    if ((ID = ++genID) == 0)
184      ID = ++genID;
185    transfID = ID;
186  }
187  // set ID to explicit value - it can be even 0 for rays transformed
188  // to the canonical object space to supress the mailbox failure.
[328]189  void SetId(int newID) {
[162]190    ID = newID;
191    // note that transfID is not changed!
192  }
193
194
195  // the object on which the ray starts at
[191]196  const Intersection* GetStartObject() const { return &intersections[0]; }
197  const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
[162]198 
199 
200  void SetLoc(const Vector3 &l);
201  Vector3& GetLoc() { return loc; }
202  Vector3  GetLoc() const { return loc; }
203
204  float  GetLoc(const int axis) const { return loc[axis]; }
205
206  void SetDir(const Vector3 &ndir) { dir = ndir;}
207  Vector3& GetDir() { return dir; }
208  Vector3  GetDir() const { return dir; }
209  float  GetDir(const int axis) const { return dir[axis]; }
210
211  int GetType() const { return mType; }
212 
213  // make such operation to slightly change the ray direction
214  // in case any component of ray direction is zero.
215  void CorrectZeroComponents();
216
217  // the depth of the ray - primary rays are in the depth 0
218  int GetDepth() const { return depth;}
219  void SetDepth(int newDepth) { depth = newDepth;}
220 
[327]221  /** Classifies ray with respect to the plane.
222  */
[406]223  int ClassifyPlane(const Plane3 &plane,
224                                        const float minT,
225                                        const float maxT,
226                                        Vector3 &entP,
227                                        Vector3 &extP) const;
[327]228
[162]229private:
230  Vector3 loc, dir;             // Describes ray origin and vector
231 
232  // The inverted direction of the ray components. It is computed optionally
233  // by the ray traversal algorithm using function ComputeInvertedDir();
234  mutable Vector3 invDir;
235
236  // Type of the ray: primary, shadow, dummy etc., see ERayType above
237  int mType;
238 
239 
240  // unique ID of a ray for the use in the mailboxes
241  int ID;
[176]242 
[162]243  // unique ID of a ray for the use with a transformations - this one
244  // never can be changed that allows the nesting of transformations
245  // and caching the transformed rays correctly
246  int transfID;
247 
248  // the ID generator fo each ray instantiated
249  static int genID; 
250
251  // When ray shot from the source(camera/light), this number is equal
252  // to the number of bounces of the ray, also called the depth of the
253  // ray (primary ray has its depth zero)
[374]254  int depth;
255
256       
257       
[162]258  void Init();
259
260  // precompute some values that are necessary
261  void Precompute();
262
263  friend class AxisAlignedBox3;
[327]264  friend class Plane3;
[162]265
266  // for CKDR GEMS
267  friend float DistanceToXPlane(const Vector3 &vec, const Ray &ray);
268  friend float DistanceToYPlane(const Vector3 &vec, const Ray &ray);
269  friend float DistanceToZPlane(const Vector3 &vec, const Ray &ray);
270  friend int MakeIntersectLine(const Plane3 &p, const Plane3 &q, Ray &ray);
[340]271
272        friend ostream &operator<<(ostream &s, const Ray &r) {
273                return s<<"Ray:loc="<<r.loc<<" dir="<<r.dir;
274        }
[162]275};
276
277
[191]278class PassingRaySet {
279public:
280  enum { Resolution = 2 };
281  int mDirectionalContributions[3*Resolution*Resolution];
282  int mRays;
283  int mContributions;
[369]284
285        PassingRaySet() {
[191]286    Reset();
287  }
[369]288
289        void
[191]290  Reset();
291 
292  void AddRay(const Ray &ray, const int contributions);
[369]293        void AddRay2(const Ray &ray,
294                                                         const int objects,
295                                                         const int viewcells);
296
[191]297  int GetEntryIndex(const Vector3 &direction) const;
[162]298
[191]299  friend ostream &operator<<(ostream &s, const PassingRaySet &set);
[162]300
[191]301};
302
[401]303struct SimpleRay
304{
305        Vector3 mOrigin;
306        Vector3 mDirection;
307        SimpleRay() {}
308        SimpleRay(const Vector3 &o, const Vector3 &d):mOrigin(o), mDirection(d) {}
309};
[191]310
[401]311typedef vector<SimpleRay> SimpleRayContainer;
312
[162]313#endif
314
Note: See TracBrowser for help on using the repository browser.