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

Revision 860, 8.8 KB checked in by mattausch, 19 years ago (diff)
RevLine 
[162]1#ifndef __RAY_H__
2#define __RAY_H__
3
4#include <vector>
5#include "Matrix4x4.h"
6#include "Vector3.h"
[475]7//#include "ViewCellBsp.h"
[162]8
[860]9namespace GtpVisibilityPreprocessor {
10
[162]11// forward declarations
12class Plane3;
13class Intersectable;
14class KdLeaf;
15class MeshInstance;
[308]16class ViewCell;
[362]17class BspLeaf;
[426]18class VssRay;
[162]19
[448]20
[162]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:
[209]29  enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT };
[369]30       
[162]31  enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
32
[350]33  /// if ray is on back (front) side of plane, or goes from the
34  /// front (back) to the back (front)
[349]35  enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
[374]36       
[191]37  struct Intersection {
[162]38    // the point of intersection
39    float mT;
[176]40
[162]41    // can be either mesh or a viewcell
42    Intersectable *mObject;
[369]43               
[162]44    // the face of the intersectable
45    int mFace;
[176]46
[191]47    Intersection(const float t,
[392]48                                 Intersectable *object,
49                                 const int face):mT(t), mObject(object), mFace(face) {}
[369]50               
[428]51                                 Intersection(): mT(0), mObject(NULL), mFace(0) {}
[369]52               
[162]53    bool operator<(
[448]54                                   const Intersection &b) const {
55
[162]56      return
[369]57                                mT
58                                <
59                                b.mT;
[162]60    }
61   
62  };
[366]63
64
[176]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 */
[191]68  Intersection sourceObject;
[176]69 
[191]70  vector<Intersection> intersections;
[475]71 // vector<BspIntersection> bspIntersections;
[362]72  vector<KdLeaf *> kdLeaves;
[374]73  vector<Intersectable *> testedObjects;
74
[534]75  // various flags
76  enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4, CULL_BACKFACES=8};
77  int mFlags;
[374]78
79       
[162]80  // constructors
81  Ray(const Vector3 &wherefrom,
82      const Vector3 &whichdir,
[534]83      const int _type):mFlags(CULL_BACKFACES) {
[162]84    loc = wherefrom;
[209]85    if (_type == LINE_SEGMENT)
86      dir = whichdir;
87    else
88      dir = Normalize(whichdir);
[162]89    mType = _type;
90    depth = 0;
[374]91                Init();
[162]92  }
93  // dummy constructor
94  Ray() {}
[426]95
96  /** Construct ray from a vss ray.
97  */
[466]98  Ray(const VssRay &vssRay) {
99        Init(vssRay);
[534]100        mFlags |= CULL_BACKFACES;
[466]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
[191]112  Intersectable *GetIntersectionObject(const int i) const {
113    return intersections[i].mObject;
114  }
[162]115 
[191]116  Vector3 GetIntersectionPoint(const int i) const {
117    return Extrap(intersections[i].mT);
118  }
119 
[162]120  // Inititalize the ray again when already constructed
[245]121  void Init(const Vector3 &wherefrom,
[392]122                        const Vector3 &whichdir,
123                        const int _type,
124                        bool dirNormalized = false) {
[162]125    loc = wherefrom;
[245]126    dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ;
[162]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)
[328]174  int GetId() const { return ID; }
[162]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
[328]183  void SetId() {
[162]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.
[328]190  void SetId(int newID) {
[162]191    ID = newID;
192    // note that transfID is not changed!
193  }
194
195
196  // the object on which the ray starts at
[191]197  const Intersection* GetStartObject() const { return &intersections[0]; }
198  const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
[162]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 
[327]222  /** Classifies ray with respect to the plane.
223  */
[406]224  int ClassifyPlane(const Plane3 &plane,
225                                        const float minT,
226                                        const float maxT,
227                                        Vector3 &entP,
228                                        Vector3 &extP) const;
[327]229
[162]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;
[176]243 
[162]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)
[374]255  int depth;
256
257       
258       
[162]259  void Init();
260
261  // precompute some values that are necessary
262  void Precompute();
263
264  friend class AxisAlignedBox3;
[327]265  friend class Plane3;
[162]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);
[340]272
273        friend ostream &operator<<(ostream &s, const Ray &r) {
274                return s<<"Ray:loc="<<r.loc<<" dir="<<r.dir;
275        }
[162]276};
277
278
[191]279class PassingRaySet {
280public:
281  enum { Resolution = 2 };
282  int mDirectionalContributions[3*Resolution*Resolution];
283  int mRays;
284  int mContributions;
[369]285
286        PassingRaySet() {
[191]287    Reset();
288  }
[369]289
290        void
[191]291  Reset();
292 
293  void AddRay(const Ray &ray, const int contributions);
[369]294        void AddRay2(const Ray &ray,
295                                                         const int objects,
296                                                         const int viewcells);
297
[191]298  int GetEntryIndex(const Vector3 &direction) const;
[162]299
[191]300  friend ostream &operator<<(ostream &s, const PassingRaySet &set);
[162]301
[191]302};
303
[401]304struct SimpleRay
305{
[534]306  Vector3 mOrigin;
307  Vector3 mDirection;
[556]308  float mPdf;
[534]309
310  SimpleRay() {}
[537]311  SimpleRay(const Vector3 &o, const Vector3 &d, const float p=1.0f):
[556]312        mOrigin(o), mDirection(d), mPdf(p) {}
[401]313};
[191]314
[537]315class SimpleRayContainer : public vector<SimpleRay>
316{
317public:
318 
[556]319  float mSumPdf;
[537]320 
[556]321  SimpleRayContainer():vector<SimpleRay>(), mSumPdf(0.0f) {}
[537]322 
[556]323  void NormalizePdf() {
[537]324        iterator it = begin();
[556]325        float c = 1.0f/mSumPdf;
[537]326        for (; it != end(); it++)
[556]327          (*it).mPdf*=c;
[537]328  }
[563]329
330  void AddRay(const SimpleRay &ray) {
331        push_back(ray);
332        mSumPdf += ray.mPdf;
333  }
[537]334};
[401]335
[860]336}
337
[162]338#endif
339
Note: See TracBrowser for help on using the repository browser.