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

Revision 2575, 8.5 KB checked in by bittner, 17 years ago (diff)

big merge: preparation for havran ray caster, check if everything works

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