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

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