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

Line 
1#ifndef __RAY_H__
2#define __RAY_H__
3
4#include <vector>
5#include "Matrix4x4.h"
6#include "Vector3.h"
7
8
9namespace GtpVisibilityPreprocessor {
10
11// forward declarations
12class Plane3;
13class Intersectable;
14class KdLeaf;
15class MeshInstance;
16class ViewCell;
17class BspLeaf;
18class VssRay;
19
20
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:
29  enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT };
30       
31  enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
32
33  /// if ray is on back (front) side of plane, or goes from the
34  /// front (back) to the back (front)
35  enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
36       
37  struct Intersection
38  {
39          /// the point of intersection
40          float mT;
41
42          /// the normal of the intersection
43          Vector3 mNormal;
44
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          }
65  };
66
67
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 */
71  Intersection sourceObject;
72 
73  vector<Intersection> intersections;
74 // vector<BspIntersection> bspIntersections;
75  vector<KdLeaf *> kdLeaves;
76  vector<Intersectable *> testedObjects;
77
78  // various flags
79  enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4, CULL_BACKFACES=8};
80  int mFlags;
81
82       
83  // constructors
84  Ray(const Vector3 &wherefrom,
85      const Vector3 &whichdir,
86      const int _type):mFlags(CULL_BACKFACES) {
87    loc = wherefrom;
88    if (_type == LINE_SEGMENT)
89      dir = whichdir;
90    else
91      dir = Normalize(whichdir);
92    mType = _type;
93    depth = 0;
94                Init();
95  }
96  // dummy constructor
97  Ray() {}
98
99  /** Construct ray from a vss ray.
100  */
101  Ray(const VssRay &vssRay) {
102        Init(vssRay);
103        mFlags |= CULL_BACKFACES;
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
115  Intersectable *GetIntersectionObject(const int i) const {
116    return intersections[i].mObject;
117  }
118 
119  Vector3 GetIntersectionPoint(const int i) const {
120    return Extrap(intersections[i].mT);
121  }
122 
123  // Inititalize the ray again when already constructed
124  void Init(const Vector3 &wherefrom,
125                        const Vector3 &whichdir,
126                        const int _type,
127                        bool dirNormalized = false) {
128    loc = wherefrom;
129    dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ;
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)
177  int GetId() const { return ID; }
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
186  void SetId() {
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.
193  void SetId(int newID) {
194    ID = newID;
195    // note that transfID is not changed!
196  }
197
198
199  // the object on which the ray starts at
200  const Intersection* GetStartObject() const { return &intersections[0]; }
201  const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
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 
225  /** Classifies ray with respect to the plane.
226  */
227  int ClassifyPlane(const Plane3 &plane,
228                                        const float minT,
229                                        const float maxT,
230                                        Vector3 &entP,
231                                        Vector3 &extP) const;
232
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;
246 
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)
258  int depth;
259
260       
261       
262  void Init();
263
264  // precompute some values that are necessary
265  void Precompute();
266
267  friend class AxisAlignedBox3;
268  friend class Plane3;
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);
275
276        friend ostream &operator<<(ostream &s, const Ray &r) {
277                return s<<"Ray:loc="<<r.loc<<" dir="<<r.dir;
278        }
279};
280
281
282class PassingRaySet {
283public:
284  enum { Resolution = 2 };
285  int mDirectionalContributions[3*Resolution*Resolution];
286  int mRays;
287  int mContributions;
288
289        PassingRaySet() {
290    Reset();
291  }
292
293        void
294  Reset();
295 
296  void AddRay(const Ray &ray, const int contributions);
297        void AddRay2(const Ray &ray,
298                                                         const int objects,
299                                                         const int viewcells);
300
301  int GetEntryIndex(const Vector3 &direction) const;
302
303  friend ostream &operator<<(ostream &s, const PassingRaySet &set);
304
305};
306
307struct SimpleRay
308{
309  Vector3 mOrigin;
310  Vector3 mDirection;
311  float mPdf;
312
313  SimpleRay() {}
314  SimpleRay(const Vector3 &o, const Vector3 &d, const float p=1.0f):
315        mOrigin(o), mDirection(d), mPdf(p) {}
316
317  Vector3 Extrap(const float t) const {
318        return mOrigin + mDirection * t;
319  }
320};
321
322class SimpleRayContainer : public vector<SimpleRay>
323{
324public:
325   
326  SimpleRayContainer():vector<SimpleRay>() {}
327 
328  void NormalizePdf(float scale = 1.0f) {
329        iterator it = begin();
330        float sumPdf = 0.0f;
331        for (; it != end(); it++)
332          sumPdf += (*it).mPdf;
333
334        float c = scale/sumPdf;
335       
336        for (it = begin(); it != end(); it++) {
337          (*it).mPdf*=c;
338        }
339  }
340 
341  void AddRay(const SimpleRay &ray) {
342        push_back(ray);
343  }
344};
345
346}
347
348#endif
349
Note: See TracBrowser for help on using the repository browser.