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

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