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

Revision 1883, 9.4 KB checked in by bittner, 18 years ago (diff)

mixture distribution initial coding

RevLine 
[162]1#ifndef __RAY_H__
2#define __RAY_H__
3
4#include <vector>
[1824]5#include <algorithm>
[162]6#include "Matrix4x4.h"
7#include "Vector3.h"
8
[1473]9
[860]10namespace GtpVisibilityPreprocessor {
11
[162]12// forward declarations
13class Plane3;
14class Intersectable;
15class KdLeaf;
16class MeshInstance;
[308]17class ViewCell;
[362]18class BspLeaf;
[426]19class VssRay;
[162]20
[448]21
[162]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:
[209]30  enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT };
[369]31       
[162]32  enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
33
[350]34  /// if ray is on back (front) side of plane, or goes from the
35  /// front (back) to the back (front)
[349]36  enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
[374]37       
[1528]38  struct Intersection
39  {
40          /// the point of intersection
41          float mT;
[176]42
[1528]43          /// the normal of the intersection
44          Vector3 mNormal;
[448]45
[1528]46          /// can be either mesh or a viewcell
47          Intersectable *mObject;
48
[1824]49                /// the face of the intersectable
[1528]50          int mFace;
51
52          Intersection(const float t,
[1824]53                                                                 const Vector3 &normal,
54                                                                 Intersectable *object,
55                                                                 const int face):
56                        mT(t), mNormal(normal), mObject(object), mFace(face)
[1528]57          {}
[1824]58               
[1528]59          Intersection(): mT(0), mNormal(0,0,0), mObject(NULL), mFace(0)
60          {}
[1824]61               
[1528]62          bool operator<(const Intersection &b) const
63          {
64                  return mT < b.mT;
65          }
[162]66  };
[366]67
68
[176]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 */
[191]72  Intersection sourceObject;
[176]73 
[191]74  vector<Intersection> intersections;
[475]75 // vector<BspIntersection> bspIntersections;
[362]76  vector<KdLeaf *> kdLeaves;
[374]77  vector<Intersectable *> testedObjects;
78
[534]79  // various flags
80  enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4, CULL_BACKFACES=8};
81  int mFlags;
[374]82
83       
[162]84  // constructors
85  Ray(const Vector3 &wherefrom,
86      const Vector3 &whichdir,
[534]87      const int _type):mFlags(CULL_BACKFACES) {
[162]88    loc = wherefrom;
[209]89    if (_type == LINE_SEGMENT)
90      dir = whichdir;
91    else
92      dir = Normalize(whichdir);
[162]93    mType = _type;
94    depth = 0;
[374]95                Init();
[162]96  }
97  // dummy constructor
98  Ray() {}
[426]99
100  /** Construct ray from a vss ray.
101  */
[466]102  Ray(const VssRay &vssRay) {
103        Init(vssRay);
[534]104        mFlags |= CULL_BACKFACES;
[466]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
[1824]116        void SortIntersections() {
117                sort(intersections.begin(), intersections.end());
118        }
119       
[191]120  Intersectable *GetIntersectionObject(const int i) const {
121    return intersections[i].mObject;
122  }
[162]123 
[191]124  Vector3 GetIntersectionPoint(const int i) const {
125    return Extrap(intersections[i].mT);
126  }
127 
[162]128  // Inititalize the ray again when already constructed
[245]129  void Init(const Vector3 &wherefrom,
[392]130                        const Vector3 &whichdir,
131                        const int _type,
132                        bool dirNormalized = false) {
[162]133    loc = wherefrom;
[245]134    dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ;
[162]135    mType = _type;
136    depth = 0;
137    Init();
[1584]138        Precompute();
[162]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)
[328]183  int GetId() const { return ID; }
[162]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
[328]192  void SetId() {
[162]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.
[328]199  void SetId(int newID) {
[162]200    ID = newID;
201    // note that transfID is not changed!
202  }
203
204
205  // the object on which the ray starts at
[191]206  const Intersection* GetStartObject() const { return &intersections[0]; }
207  const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
[162]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; }
[1824]222  void SetType(const int t) { mType = t; }
[162]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 
[327]232  /** Classifies ray with respect to the plane.
233  */
[406]234  int ClassifyPlane(const Plane3 &plane,
235                                        const float minT,
236                                        const float maxT,
237                                        Vector3 &entP,
238                                        Vector3 &extP) const;
[327]239
[1584]240  Vector3 GetInvDir() const { return invDir; }
241 
[162]242private:
243  Vector3 loc, dir;             // Describes ray origin and vector
244 
245  // The inverted direction of the ray components. It is computed optionally
246  // by the ray traversal algorithm using function ComputeInvertedDir();
247  mutable Vector3 invDir;
248
249  // Type of the ray: primary, shadow, dummy etc., see ERayType above
250  int mType;
251 
252 
253  // unique ID of a ray for the use in the mailboxes
254  int ID;
[176]255 
[162]256  // unique ID of a ray for the use with a transformations - this one
257  // never can be changed that allows the nesting of transformations
258  // and caching the transformed rays correctly
259  int transfID;
260 
261  // the ID generator fo each ray instantiated
262  static int genID; 
263
264  // When ray shot from the source(camera/light), this number is equal
265  // to the number of bounces of the ray, also called the depth of the
266  // ray (primary ray has its depth zero)
[374]267  int depth;
268
269       
270       
[162]271  void Init();
272
273  // precompute some values that are necessary
274  void Precompute();
275
276  friend class AxisAlignedBox3;
[327]277  friend class Plane3;
[162]278
279  // for CKDR GEMS
280  friend float DistanceToXPlane(const Vector3 &vec, const Ray &ray);
281  friend float DistanceToYPlane(const Vector3 &vec, const Ray &ray);
282  friend float DistanceToZPlane(const Vector3 &vec, const Ray &ray);
283  friend int MakeIntersectLine(const Plane3 &p, const Plane3 &q, Ray &ray);
[340]284
285        friend ostream &operator<<(ostream &s, const Ray &r) {
286                return s<<"Ray:loc="<<r.loc<<" dir="<<r.dir;
287        }
[162]288};
289
290
[191]291class PassingRaySet {
292public:
293  enum { Resolution = 2 };
294  int mDirectionalContributions[3*Resolution*Resolution];
295  int mRays;
296  int mContributions;
[369]297
298        PassingRaySet() {
[191]299    Reset();
300  }
[369]301
302        void
[191]303  Reset();
304 
305  void AddRay(const Ray &ray, const int contributions);
[369]306        void AddRay2(const Ray &ray,
307                                                         const int objects,
308                                                         const int viewcells);
309
[191]310  int GetEntryIndex(const Vector3 &direction) const;
[162]311
[191]312  friend ostream &operator<<(ostream &s, const PassingRaySet &set);
[162]313
[191]314};
315
[401]316struct SimpleRay
317{
[534]318  Vector3 mOrigin;
319  Vector3 mDirection;
[1883]320  short mType;
321  short mDistribution;
[556]322  float mPdf;
[1883]323 
[1867]324  SimpleRay(): mType(Ray::LOCAL_RAY) {
325  }
326 
[1883]327  SimpleRay(const Vector3 &o,
328                        const Vector3 &d,
329                        const short distribution,
330                        const float weight
331                        ):
332        mOrigin(o),
333        mDirection(d),
334        mType(Ray::LOCAL_RAY),
335        mDistribution(distribution),
336        mPdf(weight)
337  {     
[1877]338        //      mId = sSimpleRayId++;
[1867]339  }
[1883]340 
[1528]341  Vector3 Extrap(const float t) const {
[1281]342        return mOrigin + mDirection * t;
343  }
[401]344};
[191]345
[537]346class SimpleRayContainer : public vector<SimpleRay>
347{
348public:
[1528]349   
[1112]350  SimpleRayContainer():vector<SimpleRay>() {}
[537]351 
[1112]352  void NormalizePdf(float scale = 1.0f) {
[537]353        iterator it = begin();
[1112]354        float sumPdf = 0.0f;
[537]355        for (; it != end(); it++)
[1112]356          sumPdf += (*it).mPdf;
357
358        float c = scale/sumPdf;
359       
360        for (it = begin(); it != end(); it++) {
[556]361          (*it).mPdf*=c;
[1112]362        }
[537]363  }
[1112]364 
[563]365  void AddRay(const SimpleRay &ray) {
366        push_back(ray);
367  }
[537]368};
[401]369
[860]370}
371
[162]372#endif
373
Note: See TracBrowser for help on using the repository browser.