source: GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp @ 2574

Revision 2574, 10.0 KB checked in by bittner, 17 years ago (diff)

minor updates of mutation

Line 
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
12namespace GtpVisibilityPreprocessor {
13
14
15FILE *fileOut = 0;
16bool saveRays = true;
17  const int saveRaysStart = 3000000;
18int cntSavedRaysFLUSH = 0;
19unsigned long int cntSavedRays = 0;
20const int intSavedLIMIT = 1024;
21void
22InitSaving()
23{
24  fileOut = fopen("fileRays.txt", "wb");
25  if (!fileOut) {
26    cout << "ERROR: file fileRays.txt cannot be opened .. exiting" << endl;
27    exit(3);
28  }
29}
30void
31FinishSaving()
32{
33  fclose(fileOut);
34}
35
36 
37
38IntelRayCaster::IntelRayCaster(const Preprocessor &preprocessor,
39                               const string externKdTree):
40RayCaster(preprocessor)
41{
42  if (!InitRayCast(externKdTree))
43    cout << "warning: intel ray tracer could not be initialized!" << endl;
44  if (saveRays)
45    InitSaving();
46}
47
48
49IntelRayCaster::~IntelRayCaster()
50{
51  if (saveRays)
52    FinishSaving();
53}
54
55
56bool IntelRayCaster::InitRayCast(const string externKdTree)
57{
58  cout<<"Intel ray cast file: " << externKdTree << endl;
59       
60  return mlrtaLoadAS(externKdTree.c_str());
61}
62
63
64int IntelRayCaster::CastRay(
65                            const SimpleRay &simpleRay,
66                            VssRayContainer &vssRays,
67                            const AxisAlignedBox3 &box,
68                            const bool castDoubleRay,
69                            const bool pruneInvalidRays
70                            )
71
72  //cout << "intel ray" << endl;
73  VssRay *vssRay  = NULL;
74  int hits = 0;
75  int hittriangle;
76  Intersection hitA(simpleRay.mOrigin), hitB(simpleRay.mOrigin);
77 
78  float dist;
79  float dist1, dist2;
80  double normal[3];
81 
82  hittriangle = mlrtaIntersectAS(
83                                 &simpleRay.mOrigin.x,
84                                 &simpleRay.mDirection.x,
85                                 normal,
86                                 dist);
87  dist1 = dist;
88 
89  if (hittriangle != -1 ) {
90    Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
91   
92    if (intersect)
93    {
94      hitA.mObject = intersect;
95      hitA.mNormal = Vector3(normal[0], normal[1], normal[2]);
96      // Get the normal of that face
97      //                Mesh *mesh = ((MeshInstance *)objectA)->GetMesh();
98      //                normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
99      //-rays[index+i].mDirection; // $$ temporary
100      hitA.mPoint = simpleRay.Extrap(dist);
101    }
102  }
103 
104  if (castDoubleRay)
105  {
106    Vector3 dir = -simpleRay.mDirection;
107    hittriangle = mlrtaIntersectAS(
108                                   &simpleRay.mOrigin.x,
109                                   &dir.x,
110                                   normal,
111                                   dist);
112    dist2 = dist;
113
114    Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
115               
116    if (intersect)
117    {
118      hitB.mObject = intersect;
119      hitB.mNormal = Vector3(normal[0], normal[1], normal[2]);
120      // Get the normal of that face
121      //                Mesh *mesh = ((MeshInstance *)objectB)->GetMesh();
122      //                normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
123      //-rays[index+i].mDirection; // $$ temporary
124      hitB.mPoint = simpleRay.Extrap(dist);
125    }
126  }
127
128  if (saveRays && preprocessor->mTotalRaysCast > saveRaysStart) {   
129    if (castDoubleRay)
130        {
131      fprintf(fileOut, "D %d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f %d %4.7f\n",
132              cntSavedRays,
133              simpleRay.mOrigin.x,
134              simpleRay.mOrigin.y,
135              simpleRay.mOrigin.z,
136              simpleRay.mDirection.x,
137              simpleRay.mDirection.y,
138              simpleRay.mDirection.z,
139              hitA.mObject ? 1 : 0,
140              hitA.mObject ? dist1 : 0,
141              hitB.mObject ? 1 : 0,
142              hitB.mObject ? dist2 : 0
143              );
144        }
145    else
146      fprintf(fileOut, "S %d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f\n",
147              cntSavedRays,
148              simpleRay.mOrigin.x,
149              simpleRay.mOrigin.y,
150              simpleRay.mOrigin.z,
151              simpleRay.mDirection.x,
152              simpleRay.mDirection.y,
153              simpleRay.mDirection.z,
154              hitA.mObject ? 1 : 0,
155              hitA.mObject ? dist1 : 0
156              );
157    cntSavedRays++;
158    cntSavedRaysFLUSH++;
159    if (cntSavedRaysFLUSH > intSavedLIMIT) {
160      fflush(fileOut);
161      cntSavedRaysFLUSH = 0;
162    }
163  }     
164 
165  return ProcessRay(
166                    simpleRay,
167                    hitA,
168                    hitB,
169                    vssRays,
170                    box,
171                    castDoubleRay,
172                    pruneInvalidRays
173                    );
174}
175 
176
177void IntelRayCaster::CastRays16(
178                                SimpleRayContainer &rays,
179                                VssRayContainer &vssRays,
180                                const AxisAlignedBox3 &sbox,
181                                const bool castDoubleRay,
182                                const bool pruneInvalidRays)
183{
184  CastRays16(rays, 0, vssRays, sbox, castDoubleRay, pruneInvalidRays);
185}
186 
187void IntelRayCaster::CastRays16(
188                                SimpleRayContainer &rays,
189                                const int offset,
190                                VssRayContainer &vssRays,
191                                const AxisAlignedBox3 &sbox,
192                                const bool castDoubleRay,
193                                const bool pruneInvalidRays)
194
195  int i, k;
196  const int num = 16;
197 
198#if DEBUG_RAYCAST
199  Debug<<"C16 "<<flush;
200  static int counter=0;
201  Debug<<counter++<<endl;
202#endif
203 
204  static int forward_hit_triangles[16];
205  static float forward_dist[16];
206 
207  static int backward_hit_triangles[16];
208  static float backward_dist[16];
209 
210 
211  Vector3 min = mPreprocessor.mSceneGraph->GetBox().Min();
212  Vector3 max = mPreprocessor.mSceneGraph->GetBox().Max();
213
214  for (k=offset, i=0; i < num; i++, k++) {
215#if DEBUG_RAYCAST
216    if (counter == 43964) {
217      Debug<<rays[k].mOrigin<<" "<<rays[k].mDirection<<endl;
218    }
219#endif
220    mlrtaStoreRayAS16(&rays[k].mOrigin.x,
221                      &rays[k].mDirection.x,
222                      i);
223  }
224 
225#if DEBUG_RAYCAST
226  Debug<<"TA\n"<<flush;
227#endif
228 
229  mlrtaTraverseGroupAS16(&min.x,
230                         &max.x,
231                         forward_hit_triangles,
232                         forward_dist);
233 
234#if DEBUG_RAYCAST
235  Debug<<"TAB\n"<<flush;
236#endif
237
238  if (castDoubleRay) {
239    for (k=offset, i=0; i < num; i++, k++)  {
240      Vector3 dir = -rays[k].mDirection;
241      mlrtaStoreRayAS16(&rays[k].mOrigin.x,
242                        &dir.x,
243                        i);
244    }
245   
246#if DEBUG_RAYCAST
247    Debug<<"TB\n"<<flush;
248#endif
249   
250    mlrtaTraverseGroupAS16(&min.x,
251                           &max.x,
252                           backward_hit_triangles,
253                           backward_dist);
254  }
255       
256#if DEBUG_RAYCAST
257  Debug<<"BBB\n"<<flush;
258#endif
259
260  if (saveRays && preprocessor->mTotalRaysCast > saveRaysStart) {
261    if (castDoubleRay)
262      fprintf(fileOut, "G\n");
263    else
264      fprintf(fileOut, "H\n");
265  }
266 
267  for (i=0, k=offset; i < num; i++, k++)
268  {
269    Intersection hitA(rays[k].mOrigin), hitB(rays[k].mOrigin);
270
271#if DEBUG_RAYCAST
272    Debug<<"FH\n"<<flush;
273#endif
274
275    Intersectable *intersect =
276      mPreprocessor.GetParentObject(forward_hit_triangles[i]);
277
278    if (intersect)
279    {
280      hitA.mObject = intersect;
281      // Get the normal of that face
282      hitA.mNormal = mPreprocessor.GetParentNormal(forward_hit_triangles[i]);
283     
284      //-rays[index+i].mDirection; // $$ temporary
285      hitA.mPoint = rays[k].Extrap(forward_dist[i]);
286    }
287   
288#if DEBUG_RAYCAST
289    Debug<<"BH\n"<<flush;
290#endif
291
292    if (castDoubleRay)
293    {
294      Intersectable *intersect =
295        mPreprocessor.GetParentObject(backward_hit_triangles[i]);
296     
297      if (intersect)
298      {
299        hitB.mObject = intersect;
300        hitB.mNormal = mPreprocessor.GetParentNormal(backward_hit_triangles[i]);
301       
302        // normalB = rays[i].mDirection; // $$ temporary
303        hitB.mPoint = rays[k].Extrap(-backward_dist[i]);
304      }
305    }
306
307
308    if (saveRays && preprocessor->mTotalRaysCast > saveRaysStart) {
309      if (castDoubleRay)     
310        fprintf(fileOut, "%d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f %d %4.7f\n",
311                cntSavedRays,
312                rays[k].mOrigin.x,
313                rays[k].mOrigin.y,
314                rays[k].mOrigin.z,
315                rays[k].mDirection.x,
316                rays[k].mDirection.y,
317                rays[k].mDirection.z,
318                hitA.mObject ? 1 : 0,
319                hitA.mObject ? forward_dist[i] : 0,
320                hitB.mObject ? 1 : 0,
321                hitB.mObject ? -backward_dist[i] : 0
322                );
323      else
324        fprintf(fileOut, "%d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f\n",
325                cntSavedRays,
326                rays[k].mOrigin.x,
327                rays[k].mOrigin.y,
328                rays[k].mOrigin.z,
329                rays[k].mDirection.x,
330                rays[k].mDirection.y,
331                rays[k].mDirection.z,
332                hitA.mObject ? 1 : 0,
333                hitA.mObject ? forward_dist[i] : 0
334                );
335      cntSavedRays++;
336      cntSavedRaysFLUSH++;
337    }
338
339   
340#if DEBUG_RAYCAST
341    Debug<<"PR\n"<<flush;
342#endif
343
344#if 1
345    ProcessRay(rays[k],
346               hitA,
347               hitB,
348               vssRays,
349               sbox,
350               castDoubleRay,
351               pruneInvalidRays
352               );
353#endif
354  } // for
355
356  if (saveRays) {
357    if (cntSavedRaysFLUSH > intSavedLIMIT) {
358      fflush(fileOut);
359      cntSavedRaysFLUSH = 0;
360    }
361  }
362
363#if DEBUG_RAYCAST
364  Debug<<"C16F\n"<<flush;
365#endif
366}
367
368
369
370
371
372void
373IntelRayCaster::CastSimpleForwardRays(
374                                      SimpleRayContainer &rays,
375                                      const AxisAlignedBox3 &sbox
376                                      )
377{
378  int hit_triangles[16];
379  float dist[16];
380  Vector3 normals[16];
381  Vector3 min = sbox.Min();
382  Vector3 max = sbox.Max();
383 
384  int packets = rays.size() / 16;
385 
386  int i, j, k = 0;
387  Vector3 dir;
388 
389  for (i=0; i < packets; i++) {
390    for (j=0; j < 16; j++, k++)
391      mlrtaStoreRayAS16(&rays[k].mOrigin.x,
392                        &rays[k].mDirection.x,
393                        j);
394   
395    mlrtaTraverseGroupAS16(&min.x,
396                           &max.x,
397                           hit_triangles,
398                           dist);       
399  } // for
400
401
402  for (; k < rays.size(); k++) {
403        double normal[3];
404        hit_triangles[0] = mlrtaIntersectAS(
405                                            &rays[k].mOrigin.x,
406                                            &rays[k].mDirection.x,
407                                            normal,
408                                            dist[0]);
409  }
410 
411}
412
413
414void
415IntelRayCaster::CastRays(
416                         SimpleRayContainer &rays,
417                         VssRayContainer &vssRays,
418                         const AxisAlignedBox3 &sbox,
419                         const bool castDoubleRay,
420                         const bool pruneInvalidRays )
421{
422
423  int buckets = rays.size()/16;
424  int offset = 0;
425
426#if 0
427  int time = GetTime();
428  CastSimpleForwardRays(rays, sbox);
429  cout<<1e-3*2*rays.size()/TimeDiff(time, GetTime())<<" Mrays/sec"<<endl;
430#endif
431 
432  for (int i=0; i < buckets; i++, offset+=16) {
433        CastRays16(rays, offset, vssRays, sbox, castDoubleRay, pruneInvalidRays);
434
435        if ((int)rays.size() > 100000 && i % (100000/16) == 0)
436          cout<<"\r"<<offset<<"/"<<(int)rays.size()<<"\r";
437  }
438
439  for (; offset < (int)rays.size(); offset++)
440        CastRay(rays[offset], vssRays, sbox, castDoubleRay, pruneInvalidRays);
441
442}
443
444}
445
446#endif
Note: See TracBrowser for help on using the repository browser.