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 |
|
---|
110 | /*virtual void CastRaysEye4(SimpleRayContainer &rays,
|
---|
111 | VssRayContainer &vssRays,
|
---|
112 | const AxisAlignedBox3 &sbox,
|
---|
113 | const bool castDoubleRay,
|
---|
114 | const bool pruneInvalidRays = true) = 0;
|
---|
115 | */
|
---|
116 |
|
---|
117 | // This sorts only rays by origin
|
---|
118 | virtual void
|
---|
119 | SortRays(SimpleRayContainer &rays);
|
---|
120 |
|
---|
121 | // This sorts the ray by origin and direction
|
---|
122 | virtual void
|
---|
123 | SortRays2(SimpleRayContainer &rays);
|
---|
124 |
|
---|
125 | VssRay *RequestRay(const Vector3 &origin,
|
---|
126 | const Vector3 &termination,
|
---|
127 | Intersectable *originObject,
|
---|
128 | Intersectable *terminationObject,
|
---|
129 | const int pass,
|
---|
130 | const float pdf);
|
---|
131 |
|
---|
132 | // pool of vss rays to be used in one pass of the sampling
|
---|
133 | struct VssRayPool
|
---|
134 | {
|
---|
135 | VssRayPool(): mRays(NULL), mIndex(0), mNumber(0)
|
---|
136 | {}
|
---|
137 |
|
---|
138 | ~VssRayPool()
|
---|
139 | {
|
---|
140 | delete []mRays;
|
---|
141 | }
|
---|
142 |
|
---|
143 | void Reserve(const int number)
|
---|
144 | {
|
---|
145 | DEL_PTR(mRays);
|
---|
146 | mRays = new VssRay[number];
|
---|
147 | mNumber = number;
|
---|
148 | }
|
---|
149 |
|
---|
150 | void Clear()
|
---|
151 | {
|
---|
152 | mIndex = 0;
|
---|
153 | }
|
---|
154 |
|
---|
155 | VssRay *Alloc()
|
---|
156 | {
|
---|
157 | #if 1
|
---|
158 | // reset pool
|
---|
159 | if (mIndex == mNumber)
|
---|
160 | {
|
---|
161 | std::cerr << "warning: ray pool too small! " << std::endl;
|
---|
162 | mIndex = 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 | // raypool larger index => enlarge ray pool
|
---|
166 | #else
|
---|
167 | if (mNumber == mIndex)
|
---|
168 | {
|
---|
169 | cerr << "warning: ray pool too small! " << "reserving " << mNumber * 2 << " rays" << std::endl;
|
---|
170 | Reserve(mNumber * 2);
|
---|
171 | }
|
---|
172 | #endif
|
---|
173 | return mRays + mIndex ++;
|
---|
174 | }
|
---|
175 | protected:
|
---|
176 | VssRay *mRays;
|
---|
177 | int mIndex;
|
---|
178 | int mNumber;
|
---|
179 | };
|
---|
180 |
|
---|
181 |
|
---|
182 | VssRayPool mVssRayPool;
|
---|
183 |
|
---|
184 | void ReserveVssRayPool(const int n)
|
---|
185 | {
|
---|
186 | mVssRayPool.Reserve(n);
|
---|
187 | }
|
---|
188 |
|
---|
189 | void InitPass()
|
---|
190 | {
|
---|
191 | mVssRayPool.Clear();
|
---|
192 | }
|
---|
193 |
|
---|
194 | PerfTimer rawCastTimer;
|
---|
195 |
|
---|
196 | protected:
|
---|
197 |
|
---|
198 |
|
---|
199 | void _SortRays(SimpleRayContainer &rays,
|
---|
200 | const int l,
|
---|
201 | const int r,
|
---|
202 | const int depth,
|
---|
203 | float *box);
|
---|
204 |
|
---|
205 | void _SortRays2(SimpleRayContainer &rays,
|
---|
206 | const int l,
|
---|
207 | const int r,
|
---|
208 | const int depth,
|
---|
209 | float box[12]);
|
---|
210 |
|
---|
211 | struct Intersection
|
---|
212 | {
|
---|
213 | Intersection(): mObject(NULL), mFaceId(0)
|
---|
214 | {}
|
---|
215 |
|
---|
216 | Intersection(const Vector3 &p, const Vector3 &n, Intersectable *o, const int f):
|
---|
217 | mPoint(p), mNormal(n), mObject(o), mFaceId(f)
|
---|
218 | {}
|
---|
219 |
|
---|
220 | Intersection(const Vector3 &p): mPoint(p), mObject(NULL), mFaceId(0)
|
---|
221 | {}
|
---|
222 |
|
---|
223 |
|
---|
224 | ////////////
|
---|
225 |
|
---|
226 | Vector3 mPoint;
|
---|
227 | Vector3 mNormal;
|
---|
228 | Intersectable *mObject;
|
---|
229 | int mFaceId;
|
---|
230 | };
|
---|
231 |
|
---|
232 |
|
---|
233 | int ProcessRay(const SimpleRay &ray,
|
---|
234 | Intersection &hitA,
|
---|
235 | Intersection &hitB,
|
---|
236 | VssRayContainer &vssRays,
|
---|
237 | const AxisAlignedBox3 &box,
|
---|
238 | const bool castDoubleRay,
|
---|
239 | const bool pruneInvalidRays = true);
|
---|
240 |
|
---|
241 | /** Checks if ray is valid.
|
---|
242 | I.e., the ray is in valid view space.
|
---|
243 | @note: clamps the ray to valid view space.
|
---|
244 | */
|
---|
245 | bool ValidateRay(const Vector3 &origin,
|
---|
246 | const Vector3 &direction,
|
---|
247 | const AxisAlignedBox3 &box,
|
---|
248 | Intersection &hit);
|
---|
249 |
|
---|
250 | bool ClipToViewSpaceBox(const Vector3 &origin,
|
---|
251 | const Vector3 &termination,
|
---|
252 | Vector3 &clippedOrigin,
|
---|
253 | Vector3 &clippedTermination);
|
---|
254 |
|
---|
255 | const Preprocessor &mPreprocessor;
|
---|
256 | #if 1
|
---|
257 | public:
|
---|
258 | // Added by VH for debugging
|
---|
259 | Intersection intersect;
|
---|
260 | #endif
|
---|
261 | };
|
---|
262 |
|
---|
263 |
|
---|
264 | }
|
---|
265 |
|
---|
266 | #endif
|
---|