source: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h @ 2621

Revision 2621, 5.1 KB checked in by mattausch, 16 years ago (diff)
Line 
1#ifndef _RayCaster_H__
2#define _RayCaster_H__
3
4
5#include "Containers.h"
6#include <string>
7#include <iostream>
8#include "Vector3.h"
9#include "VssRay.h"
10
11using namespace std;
12
13//
14
15
16namespace GtpVisibilityPreprocessor {
17
18
19class Intersectable;
20class VssRay;
21class SimpleRayContainer;
22class AxisAlignedBox3;
23struct VssRayContainer;
24class Preprocessor;
25struct SimpleRay;
26class RayPacket2x2;
27class Matrix4x4;
28
29/** This class provides an interface for ray casting.
30*/
31class RayCaster
32{
33
34public:
35       
36  enum {
37    INTERNAL_RAYCASTER = 0,
38    INTEL_RAYCASTER = 1,
39    HAVRAN_RAYCASTER = 2
40  };
41
42  RayCaster(const Preprocessor &preprocessor);
43
44  virtual ~RayCaster();
45 
46  virtual int Type() const = 0;
47
48  /** Wrapper for casting single ray.
49      @returns ray or NULL if invalid
50  */
51  VssRay *CastRay(const SimpleRay &simpleRay,
52                  const AxisAlignedBox3 &box,
53                  const bool castDoubleRay);
54
55  virtual int CastRay(const SimpleRay &simpleRay,
56                      VssRayContainer &vssRays,
57                      const AxisAlignedBox3 &box,
58                      const bool castDoubleRay,
59                      const bool pruneInvalidRays = true) = 0;
60
61  virtual void CastRays16(SimpleRayContainer &rays,
62                          VssRayContainer &vssRays,
63                          const AxisAlignedBox3 &sbox,
64                          const bool castDoubleRay,
65                          const bool pruneInvalidRays = true) = 0;
66 
67  virtual void CastRays(
68                        SimpleRayContainer &rays,
69                        VssRayContainer &vssRays,
70                        const AxisAlignedBox3 &sbox,
71                        const bool castDoubleRay,
72                        const bool pruneInvalidRays = true);
73
74  // Using packet of 4 rays supposing that these are coherent
75  virtual void CastRaysPacket4(Vector3 origin4[],
76                               Vector3 direction4[],
77                               int     result4[],
78                               float   dist4[]) {  }
79
80  // Using packet of 4 rays supposing that these are coherent
81  // We give a box to which each ray is clipped to before the
82  // ray shooting is computed !
83  virtual void CastRaysPacket4(const Vector3 &minBox,
84                                                           const Vector3 &maxBox,
85                                                           const Vector3 origin4[],
86                                                           const Vector3 direction4[],
87                                                           int result4[],
88                                                           float dist4[]) {  }
89 
90  // Just for testing concept
91  virtual void CastRaysPacket2x2(RayPacket2x2 &raysPack,
92                                 bool castDoubleRay,
93                                 const bool pruneInvalidRays = true)
94  { }
95 
96  /*virtual void CastRaysEye4(SimpleRayContainer &rays,
97    VssRayContainer &vssRays,
98    const AxisAlignedBox3 &sbox,
99    const bool castDoubleRay,
100    const bool pruneInvalidRays = true) = 0;
101  */
102
103  virtual void
104  SortRays(SimpleRayContainer &rays);
105
106
107  virtual void AddDynamicObjecs(const ObjectContainer &objects, const Matrix4x4 &m) = 0;
108  virtual void UpdateDynamicObjects(const Matrix4x4 &m) = 0;
109  virtual void DeleteDynamicObjects() = 0;
110
111  // pool of vss rays to be used in one pass of the sampling
112  struct VssRayPool
113  {
114    VssRayPool(): mRays(NULL), mIndex(0), mNumber(0)
115    {}
116   
117    ~VssRayPool()
118    {
119      delete []mRays;
120    }
121   
122    void Reserve(const int number)
123    {
124      DEL_PTR(mRays);
125      mRays = new VssRay[number];
126      mNumber = number;
127    }
128   
129    void Clear()
130    {
131      mIndex = 0;
132    }
133   
134    VssRay *Alloc()
135    {
136      // reset pool
137      if (mIndex == mNumber)
138        mIndex = 0;
139      return mRays + mIndex ++;
140    }
141  protected:
142    VssRay *mRays;
143    int mIndex;
144    int mNumber;
145  };
146
147
148  VssRayPool mVssRayPool;
149 
150  void ReserveVssRayPool(const int n)
151  {
152    mVssRayPool.Reserve(n);
153  }
154 
155  void InitPass()
156  {
157    mVssRayPool.Clear();
158  }
159 
160 
161protected:
162 
163  VssRay *RequestRay(const Vector3 &origin,
164                     const Vector3 &termination,
165                     Intersectable *originObject,
166                     Intersectable *terminationObject,
167                     const int pass,
168                     const float pdf);
169 
170  void _SortRays(SimpleRayContainer &rays,
171                 const int l,
172                 const int r,
173                 const int depth,
174                 float box[12]);
175 
176  struct Intersection
177  {
178    Intersection(): mObject(NULL), mFaceId(0)
179    {}
180   
181    Intersection(const Vector3 &p, const Vector3 &n, Intersectable *o, const int f):
182      mPoint(p), mNormal(n), mObject(o), mFaceId(f)
183    {}
184   
185    Intersection(const Vector3 &p): mPoint(p), mObject(NULL), mFaceId(0)
186    {}
187   
188   
189    ////////////
190   
191    Vector3 mPoint;
192    Vector3 mNormal;
193    Intersectable *mObject;
194    int mFaceId;
195  };
196
197
198  int ProcessRay(const SimpleRay &ray,
199                 Intersection &hitA,
200                 Intersection &hitB,
201                 VssRayContainer &vssRays,
202                 const AxisAlignedBox3 &box,
203                 const bool castDoubleRay,
204                 const bool pruneInvalidRays = true);
205 
206  /** Checks if ray is valid.
207      I.e., the ray is in valid view space.
208      @note: clamps the ray to valid view space.
209  */
210  bool ValidateRay(const Vector3 &origin,
211                   const Vector3 &direction,
212                   const AxisAlignedBox3 &box,
213                   Intersection &hit);
214 
215  bool ClipToViewSpaceBox(const Vector3 &origin,
216                          const Vector3 &termination,
217                          Vector3 &clippedOrigin,
218                          Vector3 &clippedTermination);
219 
220  const Preprocessor &mPreprocessor;
221#if 1
222public:
223  // Added by VH for debugging
224  Intersection intersect;
225#endif 
226};
227
228
229}
230
231#endif
Note: See TracBrowser for help on using the repository browser.