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

Revision 1112, 8.8 KB checked in by bittner, 18 years ago (diff)

Merge with Olivers code

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