source: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h @ 308

Revision 308, 7.1 KB checked in by mattausch, 19 years ago (diff)

bsp tree view cells sampling possible

Line 
1#ifndef __RAY_H__
2#define __RAY_H__
3
4#include <vector>
5#include "Matrix4x4.h"
6#include "Vector3.h"
7
8// forward declarations
9class Plane3;
10class Intersectable;
11class KdLeaf;
12class MeshInstance;
13class ViewCell;
14
15// -------------------------------------------------------------------
16// CRay class.  A ray is defined by a location and a direction.
17// The direction is always normalized (length == 1).
18// -------------------------------------------------------------------
19
20class Ray
21{
22public:
23  enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT };
24
25  enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
26
27  struct Intersection {
28    // the point of intersection
29    float mT;
30
31    // can be either mesh or a viewcell
32    Intersectable *mObject;
33
34    // the face of the intersectable
35    int mFace;
36
37    Intersection(const float t,
38                 Intersectable *object,
39                 const int face):mT(t), mObject(object), mFace(face) {}
40
41    Intersection() {}
42
43    bool operator<(
44                   const Intersection &b) const {
45      return
46        mT
47        <
48        b.mT;
49    }
50   
51  };
52 
53  // I should have some abstract cell data type !!! here
54  // corresponds to the spatial elementary cell
55  /** intersection with the source object if any */
56  Intersection sourceObject;
57 
58  vector<Intersection> intersections;
59  vector<KdLeaf *> leaves;
60  vector<MeshInstance *> meshes;
61  vector<ViewCell *> viewCells;
62
63  // constructors
64  Ray(const Vector3 &wherefrom,
65      const Vector3 &whichdir,
66      const int _type,
67      const void *_originCell = NULL) {
68    loc = wherefrom;
69    if (_type == LINE_SEGMENT)
70      dir = whichdir;
71    else
72      dir = Normalize(whichdir);
73    mType = _type;
74    depth = 0;
75    Init();
76  }
77  // dummy constructor
78  Ray() {}
79
80  Intersectable *GetIntersectionObject(const int i) const {
81    return intersections[i].mObject;
82  }
83 
84  Vector3 GetIntersectionPoint(const int i) const {
85    return Extrap(intersections[i].mT);
86  }
87 
88  // Inititalize the ray again when already constructed
89  void Init(const Vector3 &wherefrom,
90            const Vector3 &whichdir,
91            const int _type,
92            bool dirNormalized = false) {
93    loc = wherefrom;
94    dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ;
95    mType = _type;
96    depth = 0;
97    Init();
98  }
99
100  // --------------------------------------------------------
101  // Extrapolate ray given a signed distance, returns a point
102  // --------------------------------------------------------
103  Vector3 Extrap(float t) const {
104    return loc + dir * t;
105  }
106
107  // -----------------------------------
108  // Return parameter given point on ray
109  // -----------------------------------
110  float Interp(Vector3 &x) const {
111    for (int i = 0; i < 3; i++)
112      if (Abs(dir[i]) > Limits::Small)
113        return (x[i] - loc[i]) / dir[i];
114    return 0;
115  }
116
117  // -----------------------------------
118  // Reflects direction of reflection for the ray,
119  // given the normal to the surface.
120  // -----------------------------------
121  Vector3 ReflectRay(const Vector3 &N) const {
122    return N * 2.0 * DotProd(N, -dir) + dir;
123  }
124  void ReflectRay(Vector3 &result, const Vector3 &N) const {
125    result =  N * 2.0 * DotProd(N, -dir) + dir;
126  }
127
128  // Computes the inverted direction of the ray, used optionally by
129  // a ray traversal algorithm.
130  void ComputeInvertedDir() const;
131
132  // Given the matrix 4x4, transforms the ray to another space
133  void ApplyTransform(const Matrix4x4 &tform) {
134    loc = tform * loc;
135    dir = RotateOnly(tform, dir);
136    // note that normalization to the unit size of the direction
137    // is NOT computed -- this is what we want.
138    Precompute();
139  }
140
141  // returns ID of this ray (use for mailboxes)
142  int GetID() const { return ID; }
143
144  // returns the transfrom ID of the ray (use for ray transformations)
145  int GetTransformID() const { return transfID; }
146
147  // copy the transform ID from an input ray
148  void CopyTransformID(const Ray &ray) { transfID = ray.transfID; }
149 
150  // set unique ID for a given ray - always avoid setting to zero
151  void SetID() {
152    if ((ID = ++genID) == 0)
153      ID = ++genID;
154    transfID = ID;
155  }
156  // set ID to explicit value - it can be even 0 for rays transformed
157  // to the canonical object space to supress the mailbox failure.
158  void SetID(int newID) {
159    ID = newID;
160    // note that transfID is not changed!
161  }
162
163
164  // the object on which the ray starts at
165  const Intersection* GetStartObject() const { return &intersections[0]; }
166  const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
167 
168 
169  void SetLoc(const Vector3 &l);
170  Vector3& GetLoc() { return loc; }
171  Vector3  GetLoc() const { return loc; }
172
173  float  GetLoc(const int axis) const { return loc[axis]; }
174
175  void SetDir(const Vector3 &ndir) { dir = ndir;}
176  Vector3& GetDir() { return dir; }
177  Vector3  GetDir() const { return dir; }
178  float  GetDir(const int axis) const { return dir[axis]; }
179
180  int GetType() const { return mType; }
181 
182  // make such operation to slightly change the ray direction
183  // in case any component of ray direction is zero.
184  void CorrectZeroComponents();
185
186  // the depth of the ray - primary rays are in the depth 0
187  int GetDepth() const { return depth;}
188  void SetDepth(int newDepth) { depth = newDepth;}
189 
190private:
191  Vector3 loc, dir;             // Describes ray origin and vector
192 
193  // The inverted direction of the ray components. It is computed optionally
194  // by the ray traversal algorithm using function ComputeInvertedDir();
195  mutable Vector3 invDir;
196
197  // Type of the ray: primary, shadow, dummy etc., see ERayType above
198  int mType;
199 
200 
201  // unique ID of a ray for the use in the mailboxes
202  int ID;
203 
204  // unique ID of a ray for the use with a transformations - this one
205  // never can be changed that allows the nesting of transformations
206  // and caching the transformed rays correctly
207  int transfID;
208 
209  // the ID generator fo each ray instantiated
210  static int genID; 
211
212  // When ray shot from the source(camera/light), this number is equal
213  // to the number of bounces of the ray, also called the depth of the
214  // ray (primary ray has its depth zero)
215  int      depth;
216 
217 
218  void Init();
219
220  // precompute some values that are necessary
221  void Precompute();
222
223  friend class AxisAlignedBox3;
224  friend class Plane;
225
226  // for CKDR GEMS
227  friend float DistanceToXPlane(const Vector3 &vec, const Ray &ray);
228  friend float DistanceToYPlane(const Vector3 &vec, const Ray &ray);
229  friend float DistanceToZPlane(const Vector3 &vec, const Ray &ray);
230  friend int MakeIntersectLine(const Plane3 &p, const Plane3 &q, Ray &ray);
231};
232
233
234class PassingRaySet {
235public:
236  enum { Resolution = 2 };
237  int mDirectionalContributions[3*Resolution*Resolution];
238  int mRays;
239  int mContributions;
240  PassingRaySet() {
241    Reset();
242  }
243  void
244  Reset();
245 
246  void AddRay(const Ray &ray, const int contributions);
247  int GetEntryIndex(const Vector3 &direction) const;
248
249  friend ostream &operator<<(ostream &s, const PassingRaySet &set);
250
251};
252
253
254#endif
255
Note: See TracBrowser for help on using the repository browser.