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

RevLine 
[1520]1#include "IntelRayCaster.h"
2#include "VssRay.h"
3#include "Preprocessor.h"
[1942]4#include "SceneGraph.h"
[1520]5
6#ifdef GTP_INTERNAL
7#include "ArchModeler2MLRT.hxx"
8
[1952]9#define DEBUG_RAYCAST 0
[1520]10
11
12namespace GtpVisibilityPreprocessor {
13
14
[2572]15FILE *fileOut = 0;
16bool saveRays = true;
[2574]17  const int saveRaysStart = 3000000;
[2572]18int cntSavedRaysFLUSH = 0;
19unsigned long int cntSavedRays = 0;
[2574]20const int intSavedLIMIT = 1024;
[2572]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
[1960]38IntelRayCaster::IntelRayCaster(const Preprocessor &preprocessor,
[2572]39                               const string externKdTree):
[1520]40RayCaster(preprocessor)
41{
[2572]42  if (!InitRayCast(externKdTree))
43    cout << "warning: intel ray tracer could not be initialized!" << endl;
44  if (saveRays)
45    InitSaving();
[1520]46}
47
48
49IntelRayCaster::~IntelRayCaster()
50{
[2572]51  if (saveRays)
52    FinishSaving();
[1520]53}
54
55
56bool IntelRayCaster::InitRayCast(const string externKdTree)
57{
[2572]58  cout<<"Intel ray cast file: " << externKdTree << endl;
[2003]59       
[2572]60  return mlrtaLoadAS(externKdTree.c_str());
[1520]61}
62
63
64int IntelRayCaster::CastRay(
[2572]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;
[1528]113
[2572]114    Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
[1786]115               
[2572]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  }
[1520]127
[2574]128  if (saveRays && preprocessor->mTotalRaysCast > saveRaysStart) {   
[2572]129    if (castDoubleRay)
[1520]130        {
[2572]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              );
[1520]144        }
[2572]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                    );
[1520]174}
[2572]175 
[1520]176
177void IntelRayCaster::CastRays16(
[2572]178                                SimpleRayContainer &rays,
179                                VssRayContainer &vssRays,
180                                const AxisAlignedBox3 &sbox,
181                                const bool castDoubleRay,
182                                const bool pruneInvalidRays)
[1520]183{
[2572]184  CastRays16(rays, 0, vssRays, sbox, castDoubleRay, pruneInvalidRays);
[2076]185}
[2572]186 
[2076]187void IntelRayCaster::CastRays16(
[2572]188                                SimpleRayContainer &rays,
189                                const int offset,
190                                VssRayContainer &vssRays,
191                                const AxisAlignedBox3 &sbox,
192                                const bool castDoubleRay,
193                                const bool pruneInvalidRays)
194
[2076]195  int i, k;
196  const int num = 16;
197 
[1520]198#if DEBUG_RAYCAST
[2076]199  Debug<<"C16 "<<flush;
200  static int counter=0;
201  Debug<<counter++<<endl;
[1520]202#endif
[2076]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();
[1520]213
[2076]214  for (k=offset, i=0; i < num; i++, k++) {
[1983]215#if DEBUG_RAYCAST
[2572]216    if (counter == 43964) {
217      Debug<<rays[k].mOrigin<<" "<<rays[k].mDirection<<endl;
218    }
[1983]219#endif
[2572]220    mlrtaStoreRayAS16(&rays[k].mOrigin.x,
221                      &rays[k].mDirection.x,
222                      i);
[2076]223  }
224 
[1757]225#if DEBUG_RAYCAST
[2076]226  Debug<<"TA\n"<<flush;
[1757]227#endif
[2076]228 
229  mlrtaTraverseGroupAS16(&min.x,
[2572]230                         &max.x,
231                         forward_hit_triangles,
232                         forward_dist);
[2076]233 
[1757]234#if DEBUG_RAYCAST
[2076]235  Debug<<"TAB\n"<<flush;
[1757]236#endif
237
[2076]238  if (castDoubleRay) {
[2572]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   
[1757]246#if DEBUG_RAYCAST
[2572]247    Debug<<"TB\n"<<flush;
[1983]248#endif
[2572]249   
250    mlrtaTraverseGroupAS16(&min.x,
251                           &max.x,
252                           backward_hit_triangles,
253                           backward_dist);
254  }
[1983]255       
256#if DEBUG_RAYCAST
[2572]257  Debug<<"BBB\n"<<flush;
[1757]258#endif
259
[2574]260  if (saveRays && preprocessor->mTotalRaysCast > saveRaysStart) {
[2572]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
[1757]271#if DEBUG_RAYCAST
[2572]272    Debug<<"FH\n"<<flush;
[1757]273#endif
[1984]274
[2572]275    Intersectable *intersect =
276      mPreprocessor.GetParentObject(forward_hit_triangles[i]);
[1786]277
[2572]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   
[1757]288#if DEBUG_RAYCAST
[2572]289    Debug<<"BH\n"<<flush;
[1757]290#endif
[1786]291
[2572]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    }
[1786]306
307
[2574]308    if (saveRays && preprocessor->mTotalRaysCast > saveRaysStart) {
[2572]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    }
[1786]338
[2572]339   
[1757]340#if DEBUG_RAYCAST
[2572]341    Debug<<"PR\n"<<flush;
[1757]342#endif
[1786]343
[2076]344#if 1
[2572]345    ProcessRay(rays[k],
346               hitA,
347               hitB,
348               vssRays,
349               sbox,
350               castDoubleRay,
351               pruneInvalidRays
352               );
[2076]353#endif
[2572]354  } // for
[1786]355
[2572]356  if (saveRays) {
357    if (cntSavedRaysFLUSH > intSavedLIMIT) {
358      fflush(fileOut);
359      cntSavedRaysFLUSH = 0;
360    }
361  }
362
[1520]363#if DEBUG_RAYCAST
[2572]364  Debug<<"C16F\n"<<flush;
[1520]365#endif
366}
367
[2076]368
369
[2105]370
371
[2076]372void
[2105]373IntelRayCaster::CastSimpleForwardRays(
[2572]374                                      SimpleRayContainer &rays,
375                                      const AxisAlignedBox3 &sbox
376                                      )
[2105]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++) {
[2572]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
[2105]400
401
402  for (; k < rays.size(); k++) {
403        double normal[3];
404        hit_triangles[0] = mlrtaIntersectAS(
[2572]405                                            &rays[k].mOrigin.x,
406                                            &rays[k].mDirection.x,
407                                            normal,
408                                            dist[0]);
[2105]409  }
410 
411}
412
413
414void
[2076]415IntelRayCaster::CastRays(
[2572]416                         SimpleRayContainer &rays,
417                         VssRayContainer &vssRays,
418                         const AxisAlignedBox3 &sbox,
419                         const bool castDoubleRay,
420                         const bool pruneInvalidRays )
[2076]421{
422
423  int buckets = rays.size()/16;
424  int offset = 0;
[2105]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
[2076]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
[2187]439  for (; offset < (int)rays.size(); offset++)
[2076]440        CastRay(rays[offset], vssRays, sbox, castDoubleRay, pruneInvalidRays);
441
[1520]442}
443
[2076]444}
445
[1520]446#endif
Note: See TracBrowser for help on using the repository browser.