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