1 | #include "IntelRayCaster.h"
|
---|
2 | #include "VssRay.h"
|
---|
3 | #include "Preprocessor.h"
|
---|
4 | #include "SceneGraph.h"
|
---|
5 |
|
---|
6 | #ifdef GTP_INTERNAL
|
---|
7 | #include "ArchModeler2MLRT.hxx"
|
---|
8 |
|
---|
9 | #define DEBUG_RAYCAST 0
|
---|
10 |
|
---|
11 |
|
---|
12 | namespace GtpVisibilityPreprocessor {
|
---|
13 |
|
---|
14 |
|
---|
15 | IntelRayCaster::IntelRayCaster(const Preprocessor &preprocessor,
|
---|
16 | const string externKdTree):
|
---|
17 | RayCaster(preprocessor)
|
---|
18 | {
|
---|
19 | if (!InitRayCast(externKdTree))
|
---|
20 | cout << "warning: intel ray tracer could not be initialized!" << endl;
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | IntelRayCaster::~IntelRayCaster()
|
---|
25 | {
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | bool IntelRayCaster::InitRayCast(const string externKdTree)
|
---|
30 | {
|
---|
31 | cout<<"Intel ray cast file: " << externKdTree << endl;
|
---|
32 |
|
---|
33 | return mlrtaLoadAS(externKdTree.c_str());
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | int IntelRayCaster::CastRay(
|
---|
38 | const SimpleRay &simpleRay,
|
---|
39 | VssRayContainer &vssRays,
|
---|
40 | const AxisAlignedBox3 &box,
|
---|
41 | const bool castDoubleRay,
|
---|
42 | const bool pruneInvalidRays
|
---|
43 | )
|
---|
44 | {
|
---|
45 | //cout << "intel ray" << endl;
|
---|
46 | VssRay *vssRay = NULL;
|
---|
47 | int hits = 0;
|
---|
48 | int hittriangle;
|
---|
49 | Intersection hitA(simpleRay.mOrigin), hitB(simpleRay.mOrigin);
|
---|
50 |
|
---|
51 | float dist;
|
---|
52 | double normal[3];
|
---|
53 |
|
---|
54 | hittriangle = mlrtaIntersectAS(
|
---|
55 | &simpleRay.mOrigin.x,
|
---|
56 | &simpleRay.mDirection.x,
|
---|
57 | normal,
|
---|
58 | dist);
|
---|
59 |
|
---|
60 | if (hittriangle != -1 ) {
|
---|
61 | Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
|
---|
62 |
|
---|
63 | if (intersect)
|
---|
64 | {
|
---|
65 | hitA.mObject = intersect;
|
---|
66 | hitA.mNormal = Vector3(normal[0], normal[1], normal[2]);
|
---|
67 | // Get the normal of that face
|
---|
68 | // Mesh *mesh = ((MeshInstance *)objectA)->GetMesh();
|
---|
69 | // normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
|
---|
70 | //-rays[index+i].mDirection; // $$ temporary
|
---|
71 | hitA.mPoint = simpleRay.Extrap(dist);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (castDoubleRay)
|
---|
76 | {
|
---|
77 | Vector3 dir = -simpleRay.mDirection;
|
---|
78 | hittriangle = mlrtaIntersectAS(
|
---|
79 | &simpleRay.mOrigin.x,
|
---|
80 | &dir.x,
|
---|
81 | normal,
|
---|
82 | dist);
|
---|
83 |
|
---|
84 | Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
|
---|
85 |
|
---|
86 | if (intersect)
|
---|
87 | {
|
---|
88 | hitB.mObject = intersect;
|
---|
89 | hitB.mNormal = Vector3(normal[0], normal[1], normal[2]);
|
---|
90 | // Get the normal of that face
|
---|
91 | // Mesh *mesh = ((MeshInstance *)objectB)->GetMesh();
|
---|
92 | // normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
|
---|
93 | //-rays[index+i].mDirection; // $$ temporary
|
---|
94 | hitB.mPoint = simpleRay.Extrap(dist);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | return ProcessRay(
|
---|
99 | simpleRay,
|
---|
100 | hitA,
|
---|
101 | hitB,
|
---|
102 | vssRays,
|
---|
103 | box,
|
---|
104 | castDoubleRay,
|
---|
105 | pruneInvalidRays
|
---|
106 | );
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | void IntelRayCaster::CastRays16(
|
---|
111 | SimpleRayContainer &rays,
|
---|
112 | VssRayContainer &vssRays,
|
---|
113 | const AxisAlignedBox3 &sbox,
|
---|
114 | const bool castDoubleRay,
|
---|
115 | const bool pruneInvalidRays)
|
---|
116 | {
|
---|
117 | CastRays16(rays, 0, vssRays, sbox, castDoubleRay, pruneInvalidRays);
|
---|
118 | }
|
---|
119 |
|
---|
120 | void IntelRayCaster::CastRays16(
|
---|
121 | SimpleRayContainer &rays,
|
---|
122 | const int offset,
|
---|
123 | VssRayContainer &vssRays,
|
---|
124 | const AxisAlignedBox3 &sbox,
|
---|
125 | const bool castDoubleRay,
|
---|
126 | const bool pruneInvalidRays)
|
---|
127 | {
|
---|
128 | int i, k;
|
---|
129 | const int num = 16;
|
---|
130 |
|
---|
131 | #if DEBUG_RAYCAST
|
---|
132 | Debug<<"C16 "<<flush;
|
---|
133 | static int counter=0;
|
---|
134 | Debug<<counter++<<endl;
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | static int forward_hit_triangles[16];
|
---|
138 | static float forward_dist[16];
|
---|
139 |
|
---|
140 | static int backward_hit_triangles[16];
|
---|
141 | static float backward_dist[16];
|
---|
142 |
|
---|
143 |
|
---|
144 | Vector3 min = mPreprocessor.mSceneGraph->GetBox().Min();
|
---|
145 | Vector3 max = mPreprocessor.mSceneGraph->GetBox().Max();
|
---|
146 |
|
---|
147 | for (k=offset, i=0; i < num; i++, k++) {
|
---|
148 | #if DEBUG_RAYCAST
|
---|
149 | if (counter == 43964) {
|
---|
150 | Debug<<rays[k].mOrigin<<" "<<rays[k].mDirection<<endl;
|
---|
151 | }
|
---|
152 | #endif
|
---|
153 | mlrtaStoreRayAS16(&rays[k].mOrigin.x,
|
---|
154 | &rays[k].mDirection.x,
|
---|
155 | i);
|
---|
156 | }
|
---|
157 |
|
---|
158 | #if DEBUG_RAYCAST
|
---|
159 | Debug<<"TA\n"<<flush;
|
---|
160 | #endif
|
---|
161 |
|
---|
162 | mlrtaTraverseGroupAS16(&min.x,
|
---|
163 | &max.x,
|
---|
164 | forward_hit_triangles,
|
---|
165 | forward_dist);
|
---|
166 |
|
---|
167 | #if DEBUG_RAYCAST
|
---|
168 | Debug<<"TAB\n"<<flush;
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | if (castDoubleRay) {
|
---|
172 | for (k=offset, i=0; i < num; i++, k++) {
|
---|
173 | Vector3 dir = -rays[k].mDirection;
|
---|
174 | mlrtaStoreRayAS16(&rays[k].mOrigin.x,
|
---|
175 | &dir.x,
|
---|
176 | i);
|
---|
177 | }
|
---|
178 |
|
---|
179 | #if DEBUG_RAYCAST
|
---|
180 | Debug<<"TB\n"<<flush;
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | mlrtaTraverseGroupAS16(&min.x,
|
---|
184 | &max.x,
|
---|
185 | backward_hit_triangles,
|
---|
186 | backward_dist);
|
---|
187 | }
|
---|
188 |
|
---|
189 | #if DEBUG_RAYCAST
|
---|
190 | Debug<<"BBB\n"<<flush;
|
---|
191 | #endif
|
---|
192 |
|
---|
193 | for (i=0, k=offset; i < num; i++, k++)
|
---|
194 | {
|
---|
195 | Intersection hitA(rays[k].mOrigin), hitB(rays[k].mOrigin);
|
---|
196 |
|
---|
197 | #if DEBUG_RAYCAST
|
---|
198 | Debug<<"FH\n"<<flush;
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | Intersectable *intersect = mPreprocessor.GetParentObject(forward_hit_triangles[i]);
|
---|
202 |
|
---|
203 | if (intersect)
|
---|
204 | {
|
---|
205 | hitA.mObject = intersect;
|
---|
206 | // Get the normal of that face
|
---|
207 | hitA.mNormal = mPreprocessor.GetParentNormal(forward_hit_triangles[i]);
|
---|
208 |
|
---|
209 | //-rays[index+i].mDirection; // $$ temporary
|
---|
210 | hitA.mPoint = rays[k].Extrap(forward_dist[i]);
|
---|
211 | }
|
---|
212 |
|
---|
213 | #if DEBUG_RAYCAST
|
---|
214 | Debug<<"BH\n"<<flush;
|
---|
215 | #endif
|
---|
216 |
|
---|
217 | if (castDoubleRay)
|
---|
218 | {
|
---|
219 | Intersectable *intersect = mPreprocessor.GetParentObject(backward_hit_triangles[i]);
|
---|
220 |
|
---|
221 | if (intersect)
|
---|
222 | {
|
---|
223 | hitB.mObject = intersect;
|
---|
224 | hitB.mNormal = mPreprocessor.GetParentNormal(backward_hit_triangles[i]);
|
---|
225 |
|
---|
226 | // normalB = rays[i].mDirection; // $$ temporary
|
---|
227 | hitB.mPoint = rays[k].Extrap(-backward_dist[i]);
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | #if DEBUG_RAYCAST
|
---|
232 | Debug<<"PR\n"<<flush;
|
---|
233 | #endif
|
---|
234 |
|
---|
235 | #if 1
|
---|
236 | ProcessRay(rays[k],
|
---|
237 | hitA,
|
---|
238 | hitB,
|
---|
239 | vssRays,
|
---|
240 | sbox,
|
---|
241 | castDoubleRay,
|
---|
242 | pruneInvalidRays
|
---|
243 | );
|
---|
244 | #endif
|
---|
245 | }
|
---|
246 |
|
---|
247 | #if DEBUG_RAYCAST
|
---|
248 | Debug<<"C16F\n"<<flush;
|
---|
249 | #endif
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 |
|
---|
254 |
|
---|
255 |
|
---|
256 | void
|
---|
257 | IntelRayCaster::CastSimpleForwardRays(
|
---|
258 | SimpleRayContainer &rays,
|
---|
259 | const AxisAlignedBox3 &sbox
|
---|
260 | )
|
---|
261 | {
|
---|
262 | int hit_triangles[16];
|
---|
263 | float dist[16];
|
---|
264 | Vector3 normals[16];
|
---|
265 | Vector3 min = sbox.Min();
|
---|
266 | Vector3 max = sbox.Max();
|
---|
267 |
|
---|
268 | int packets = rays.size() / 16;
|
---|
269 |
|
---|
270 | int i, j, k = 0;
|
---|
271 | Vector3 dir;
|
---|
272 |
|
---|
273 | for (i=0; i < packets; i++) {
|
---|
274 | for (j=0; j < 16; j++, k++)
|
---|
275 | mlrtaStoreRayAS16(&rays[k].mOrigin.x,
|
---|
276 | &rays[k].mDirection.x,
|
---|
277 | j);
|
---|
278 |
|
---|
279 | mlrtaTraverseGroupAS16(&min.x,
|
---|
280 | &max.x,
|
---|
281 | hit_triangles,
|
---|
282 | dist);
|
---|
283 |
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | for (; k < rays.size(); k++) {
|
---|
288 | double normal[3];
|
---|
289 | hit_triangles[0] = mlrtaIntersectAS(
|
---|
290 | &rays[k].mOrigin.x,
|
---|
291 | &rays[k].mDirection.x,
|
---|
292 | normal,
|
---|
293 | dist[0]);
|
---|
294 | }
|
---|
295 |
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 | void
|
---|
300 | IntelRayCaster::CastRays(
|
---|
301 | SimpleRayContainer &rays,
|
---|
302 | VssRayContainer &vssRays,
|
---|
303 | const AxisAlignedBox3 &sbox,
|
---|
304 | const bool castDoubleRay,
|
---|
305 | const bool pruneInvalidRays )
|
---|
306 | {
|
---|
307 |
|
---|
308 | int buckets = rays.size()/16;
|
---|
309 | int offset = 0;
|
---|
310 |
|
---|
311 | #if 0
|
---|
312 | int time = GetTime();
|
---|
313 | CastSimpleForwardRays(rays, sbox);
|
---|
314 | cout<<1e-3*2*rays.size()/TimeDiff(time, GetTime())<<" Mrays/sec"<<endl;
|
---|
315 | #endif
|
---|
316 |
|
---|
317 | for (int i=0; i < buckets; i++, offset+=16) {
|
---|
318 | CastRays16(rays, offset, vssRays, sbox, castDoubleRay, pruneInvalidRays);
|
---|
319 |
|
---|
320 | if ((int)rays.size() > 100000 && i % (100000/16) == 0)
|
---|
321 | cout<<"\r"<<offset<<"/"<<(int)rays.size()<<"\r";
|
---|
322 | }
|
---|
323 |
|
---|
324 | for (; offset < (int)rays.size(); offset++)
|
---|
325 | CastRay(rays[offset], vssRays, sbox, castDoubleRay, pruneInvalidRays);
|
---|
326 |
|
---|
327 | }
|
---|
328 |
|
---|
329 | }
|
---|
330 |
|
---|
331 | #endif
|
---|