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

Revision 1528, 9.0 KB checked in by mattausch, 18 years ago (diff)

worked on gvs

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
48          /// the face of the intersectable
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();
133  }
134
135  // --------------------------------------------------------
136  // Extrapolate ray given a signed distance, returns a point
137  // --------------------------------------------------------
138  Vector3 Extrap(float t) const {
139    return loc + dir * t;
140  }
141
142  // -----------------------------------
143  // Return parameter given point on ray
144  // -----------------------------------
145  float Interp(Vector3 &x) const {
146    for (int i = 0; i < 3; i++)
147      if (Abs(dir[i]) > Limits::Small)
148        return (x[i] - loc[i]) / dir[i];
149    return 0;
150  }
151
152  // -----------------------------------
153  // Reflects direction of reflection for the ray,
154  // given the normal to the surface.
155  // -----------------------------------
156  Vector3 ReflectRay(const Vector3 &N) const {
157    return N * 2.0 * DotProd(N, -dir) + dir;
158  }
159  void ReflectRay(Vector3 &result, const Vector3 &N) const {
160    result =  N * 2.0 * DotProd(N, -dir) + dir;
161  }
162
163  // Computes the inverted direction of the ray, used optionally by
164  // a ray traversal algorithm.
165  void ComputeInvertedDir() const;
166
167  // Given the matrix 4x4, transforms the ray to another space
168  void ApplyTransform(const Matrix4x4 &tform) {
169    loc = tform * loc;
170    dir = RotateOnly(tform, dir);
171    // note that normalization to the unit size of the direction
172    // is NOT computed -- this is what we want.
173    Precompute();
174  }
175
176  // returns ID of this ray (use for mailboxes)
[328]177  int GetId() const { return ID; }
[162]178
179  // returns the transfrom ID of the ray (use for ray transformations)
180  int GetTransformID() const { return transfID; }
181
182  // copy the transform ID from an input ray
183  void CopyTransformID(const Ray &ray) { transfID = ray.transfID; }
184 
185  // set unique ID for a given ray - always avoid setting to zero
[328]186  void SetId() {
[162]187    if ((ID = ++genID) == 0)
188      ID = ++genID;
189    transfID = ID;
190  }
191  // set ID to explicit value - it can be even 0 for rays transformed
192  // to the canonical object space to supress the mailbox failure.
[328]193  void SetId(int newID) {
[162]194    ID = newID;
195    // note that transfID is not changed!
196  }
197
198
199  // the object on which the ray starts at
[191]200  const Intersection* GetStartObject() const { return &intersections[0]; }
201  const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
[162]202 
203 
204  void SetLoc(const Vector3 &l);
205  Vector3& GetLoc() { return loc; }
206  Vector3  GetLoc() const { return loc; }
207
208  float  GetLoc(const int axis) const { return loc[axis]; }
209
210  void SetDir(const Vector3 &ndir) { dir = ndir;}
211  Vector3& GetDir() { return dir; }
212  Vector3  GetDir() const { return dir; }
213  float  GetDir(const int axis) const { return dir[axis]; }
214
215  int GetType() const { return mType; }
216 
217  // make such operation to slightly change the ray direction
218  // in case any component of ray direction is zero.
219  void CorrectZeroComponents();
220
221  // the depth of the ray - primary rays are in the depth 0
222  int GetDepth() const { return depth;}
223  void SetDepth(int newDepth) { depth = newDepth;}
224 
[327]225  /** Classifies ray with respect to the plane.
226  */
[406]227  int ClassifyPlane(const Plane3 &plane,
228                                        const float minT,
229                                        const float maxT,
230                                        Vector3 &entP,
231                                        Vector3 &extP) const;
[327]232
[162]233private:
234  Vector3 loc, dir;             // Describes ray origin and vector
235 
236  // The inverted direction of the ray components. It is computed optionally
237  // by the ray traversal algorithm using function ComputeInvertedDir();
238  mutable Vector3 invDir;
239
240  // Type of the ray: primary, shadow, dummy etc., see ERayType above
241  int mType;
242 
243 
244  // unique ID of a ray for the use in the mailboxes
245  int ID;
[176]246 
[162]247  // unique ID of a ray for the use with a transformations - this one
248  // never can be changed that allows the nesting of transformations
249  // and caching the transformed rays correctly
250  int transfID;
251 
252  // the ID generator fo each ray instantiated
253  static int genID; 
254
255  // When ray shot from the source(camera/light), this number is equal
256  // to the number of bounces of the ray, also called the depth of the
257  // ray (primary ray has its depth zero)
[374]258  int depth;
259
260       
261       
[162]262  void Init();
263
264  // precompute some values that are necessary
265  void Precompute();
266
267  friend class AxisAlignedBox3;
[327]268  friend class Plane3;
[162]269
270  // for CKDR GEMS
271  friend float DistanceToXPlane(const Vector3 &vec, const Ray &ray);
272  friend float DistanceToYPlane(const Vector3 &vec, const Ray &ray);
273  friend float DistanceToZPlane(const Vector3 &vec, const Ray &ray);
274  friend int MakeIntersectLine(const Plane3 &p, const Plane3 &q, Ray &ray);
[340]275
276        friend ostream &operator<<(ostream &s, const Ray &r) {
277                return s<<"Ray:loc="<<r.loc<<" dir="<<r.dir;
278        }
[162]279};
280
281
[191]282class PassingRaySet {
283public:
284  enum { Resolution = 2 };
285  int mDirectionalContributions[3*Resolution*Resolution];
286  int mRays;
287  int mContributions;
[369]288
289        PassingRaySet() {
[191]290    Reset();
291  }
[369]292
293        void
[191]294  Reset();
295 
296  void AddRay(const Ray &ray, const int contributions);
[369]297        void AddRay2(const Ray &ray,
298                                                         const int objects,
299                                                         const int viewcells);
300
[191]301  int GetEntryIndex(const Vector3 &direction) const;
[162]302
[191]303  friend ostream &operator<<(ostream &s, const PassingRaySet &set);
[162]304
[191]305};
306
[401]307struct SimpleRay
308{
[534]309  Vector3 mOrigin;
310  Vector3 mDirection;
[556]311  float mPdf;
[534]312
313  SimpleRay() {}
[537]314  SimpleRay(const Vector3 &o, const Vector3 &d, const float p=1.0f):
[556]315        mOrigin(o), mDirection(d), mPdf(p) {}
[1281]316
[1528]317  Vector3 Extrap(const float t) const {
[1281]318        return mOrigin + mDirection * t;
319  }
[401]320};
[191]321
[537]322class SimpleRayContainer : public vector<SimpleRay>
323{
324public:
[1528]325   
[1112]326  SimpleRayContainer():vector<SimpleRay>() {}
[537]327 
[1112]328  void NormalizePdf(float scale = 1.0f) {
[537]329        iterator it = begin();
[1112]330        float sumPdf = 0.0f;
[537]331        for (; it != end(); it++)
[1112]332          sumPdf += (*it).mPdf;
333
334        float c = scale/sumPdf;
335       
336        for (it = begin(); it != end(); it++) {
[556]337          (*it).mPdf*=c;
[1112]338        }
[537]339  }
[1112]340 
[563]341  void AddRay(const SimpleRay &ray) {
342        push_back(ray);
343  }
[537]344};
[401]345
[860]346}
347
[162]348#endif
349
Note: See TracBrowser for help on using the repository browser.