source: GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h @ 1594

Revision 1594, 9.1 KB checked in by bittner, 18 years ago (diff)

support for kd tree based pvs

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