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

Revision 350, 7.5 KB checked in by mattausch, 19 years ago (diff)

ray merge started

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