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

Revision 2729, 11.7 KB checked in by mattausch, 16 years ago (diff)

worked on gvs

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;
[2575]16bool saveRays = false;
[2606]17bool saveMutationRays = false;
[2575]18const int saveRaysStart = 3000000;
[2572]19int cntSavedRaysFLUSH = 0;
20unsigned long int cntSavedRays = 0;
[2574]21const int intSavedLIMIT = 1024;
[2575]22
[2572]23void
24InitSaving()
25{
26  fileOut = fopen("fileRays.txt", "wb");
27  if (!fileOut) {
28    cout << "ERROR: file fileRays.txt cannot be opened .. exiting" << endl;
29    exit(3);
30  }
31}
32void
33FinishSaving()
34{
35  fclose(fileOut);
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;
[2606]44  if (saveRays || saveMutationRays)
[2572]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
[2606]63
[2582]64// Using packet of 4 rays supposing that these are coherent
65// We give a box to which each ray is clipped to before the
66// ray shooting is computed !
[2606]67
68// Using packet of 4 rays supposing that these are coherent
69// We give a box to which each ray is clipped to before the
70// ray shooting is computed !
[2582]71void IntelRayCaster::CastRaysPacket4(const Vector3 &minBox,
72                                                                         const Vector3 &maxBox,
73                                                                         const Vector3 origin4[],
74                                                                         const Vector3 dirs4[],
75                                                                         int     result4[],
76                                                                         float   dist4[])
77{
78  for (int i = 0; i < 4; i++) {
79    mlrtaStoreRayASEye4(&origin4[i].x, &dirs4[i].x, i);
80  }
81 
[2729]82  rawCastTimer.Entry();
[2582]83  mlrtaTraverseGroupASEye4(&minBox.x, &maxBox.x, result4, dist4);
[2729]84  rawCastTimer.Exit();
[2606]85
86  if (saveMutationRays) {
87    fprintf(fileOut, "I %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f\n",
88                        minBox.x, minBox.y, minBox.z, maxBox.x, maxBox.y, maxBox.z);
89    for (int i = 0; i < 4; i++) {
90      fprintf(fileOut, "%d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f\n",
91                          cntSavedRays,
92                          origin4[i].x,
93                          origin4[i].y,
94                          origin4[i].z,
95                          dirs4[i].x,
96                          dirs4[i].y,
97                          dirs4[i].z,
98                          (result4[i] != -1) ? 1 : 0,
99                          (result4[i] != -1) ? dist4[i] : 0);
100      cntSavedRays ++;
101      cntSavedRaysFLUSH++;
102    }
103    if (cntSavedRaysFLUSH > intSavedLIMIT) {
104      fflush(fileOut);
105      cntSavedRaysFLUSH = 0;
106    }
107  } // if we are saving rays
108
[2582]109  return;
110}
[1520]111
[2582]112
113 
114int IntelRayCaster::CastRay(const SimpleRay &simpleRay,
115                                                        VssRayContainer &vssRays,
116                                                        const AxisAlignedBox3 &box,
117                                                        const bool castDoubleRay,
118                                                        const bool pruneInvalidRays
119                                                        )
[2572]120
121  //cout << "intel ray" << endl;
122  VssRay *vssRay  = NULL;
123  int hits = 0;
124  int hittriangle;
125  Intersection hitA(simpleRay.mOrigin), hitB(simpleRay.mOrigin);
126 
127  float dist;
128  float dist1, dist2;
129  double normal[3];
[2729]130
131  rawCastTimer.Entry();
[2572]132 
133  hittriangle = mlrtaIntersectAS(
134                                 &simpleRay.mOrigin.x,
135                                 &simpleRay.mDirection.x,
136                                 normal,
137                                 dist);
[2729]138
139  rawCastTimer.Exit();
[2572]140  dist1 = dist;
141 
142  if (hittriangle != -1 ) {
143    Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
144   
145    if (intersect)
146    {
147      hitA.mObject = intersect;
148      hitA.mNormal = Vector3(normal[0], normal[1], normal[2]);
149      // Get the normal of that face
150      //                Mesh *mesh = ((MeshInstance *)objectA)->GetMesh();
151      //                normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
152      //-rays[index+i].mDirection; // $$ temporary
153      hitA.mPoint = simpleRay.Extrap(dist);
154    }
155  }
156 
157  if (castDoubleRay)
158  {
159    Vector3 dir = -simpleRay.mDirection;
[2729]160
161        rawCastTimer.Entry();
162
163        hittriangle = mlrtaIntersectAS(
[2572]164                                   &simpleRay.mOrigin.x,
165                                   &dir.x,
166                                   normal,
167                                   dist);
[2729]168       
169        rawCastTimer.Exit();
170
[2572]171    dist2 = dist;
[1528]172
[2572]173    Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
[1786]174               
[2572]175    if (intersect)
176    {
177      hitB.mObject = intersect;
178      hitB.mNormal = Vector3(normal[0], normal[1], normal[2]);
179      // Get the normal of that face
180      //                Mesh *mesh = ((MeshInstance *)objectB)->GetMesh();
181      //                normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
182      //-rays[index+i].mDirection; // $$ temporary
183      hitB.mPoint = simpleRay.Extrap(dist);
184    }
185  }
[1520]186
[2574]187  if (saveRays && preprocessor->mTotalRaysCast > saveRaysStart) {   
[2572]188    if (castDoubleRay)
[1520]189        {
[2572]190      fprintf(fileOut, "D %d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f %d %4.7f\n",
191              cntSavedRays,
192              simpleRay.mOrigin.x,
193              simpleRay.mOrigin.y,
194              simpleRay.mOrigin.z,
195              simpleRay.mDirection.x,
196              simpleRay.mDirection.y,
197              simpleRay.mDirection.z,
198              hitA.mObject ? 1 : 0,
199              hitA.mObject ? dist1 : 0,
200              hitB.mObject ? 1 : 0,
201              hitB.mObject ? dist2 : 0
202              );
[1520]203        }
[2572]204    else
205      fprintf(fileOut, "S %d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f\n",
206              cntSavedRays,
207              simpleRay.mOrigin.x,
208              simpleRay.mOrigin.y,
209              simpleRay.mOrigin.z,
210              simpleRay.mDirection.x,
211              simpleRay.mDirection.y,
212              simpleRay.mDirection.z,
213              hitA.mObject ? 1 : 0,
214              hitA.mObject ? dist1 : 0
215              );
216    cntSavedRays++;
217    cntSavedRaysFLUSH++;
218    if (cntSavedRaysFLUSH > intSavedLIMIT) {
219      fflush(fileOut);
220      cntSavedRaysFLUSH = 0;
221    }
222  }     
223 
224  return ProcessRay(
225                    simpleRay,
226                    hitA,
227                    hitB,
228                    vssRays,
229                    box,
230                    castDoubleRay,
231                    pruneInvalidRays
232                    );
[1520]233}
[2572]234 
[1520]235
236void IntelRayCaster::CastRays16(
[2572]237                                SimpleRayContainer &rays,
238                                VssRayContainer &vssRays,
239                                const AxisAlignedBox3 &sbox,
240                                const bool castDoubleRay,
241                                const bool pruneInvalidRays)
[1520]242{
[2572]243  CastRays16(rays, 0, vssRays, sbox, castDoubleRay, pruneInvalidRays);
[2076]244}
[2572]245 
[2076]246void IntelRayCaster::CastRays16(
[2572]247                                SimpleRayContainer &rays,
248                                const int offset,
249                                VssRayContainer &vssRays,
250                                const AxisAlignedBox3 &sbox,
251                                const bool castDoubleRay,
252                                const bool pruneInvalidRays)
253
[2076]254  int i, k;
255  const int num = 16;
256 
[1520]257#if DEBUG_RAYCAST
[2076]258  Debug<<"C16 "<<flush;
259  static int counter=0;
260  Debug<<counter++<<endl;
[1520]261#endif
[2076]262 
263  static int forward_hit_triangles[16];
264  static float forward_dist[16];
265 
266  static int backward_hit_triangles[16];
267  static float backward_dist[16];
268 
269 
270  Vector3 min = mPreprocessor.mSceneGraph->GetBox().Min();
271  Vector3 max = mPreprocessor.mSceneGraph->GetBox().Max();
[1520]272
[2076]273  for (k=offset, i=0; i < num; i++, k++) {
[1983]274#if DEBUG_RAYCAST
[2572]275    if (counter == 43964) {
276      Debug<<rays[k].mOrigin<<" "<<rays[k].mDirection<<endl;
277    }
[1983]278#endif
[2729]279
[2572]280    mlrtaStoreRayAS16(&rays[k].mOrigin.x,
281                      &rays[k].mDirection.x,
282                      i);
[2076]283  }
284 
[1757]285#if DEBUG_RAYCAST
[2076]286  Debug<<"TA\n"<<flush;
[1757]287#endif
[2729]288  rawCastTimer.Entry();
289
[2076]290  mlrtaTraverseGroupAS16(&min.x,
[2572]291                         &max.x,
292                         forward_hit_triangles,
293                         forward_dist);
[2076]294 
[2729]295  rawCastTimer.Exit();
296 
[1757]297#if DEBUG_RAYCAST
[2076]298  Debug<<"TAB\n"<<flush;
[1757]299#endif
300
[2076]301  if (castDoubleRay) {
[2572]302    for (k=offset, i=0; i < num; i++, k++)  {
303      Vector3 dir = -rays[k].mDirection;
304      mlrtaStoreRayAS16(&rays[k].mOrigin.x,
305                        &dir.x,
306                        i);
307    }
308   
[1757]309#if DEBUG_RAYCAST
[2572]310    Debug<<"TB\n"<<flush;
[1983]311#endif
[2729]312    rawCastTimer.Entry();
[2572]313    mlrtaTraverseGroupAS16(&min.x,
314                           &max.x,
315                           backward_hit_triangles,
316                           backward_dist);
[2729]317
318        rawCastTimer.Exit();
[2572]319  }
[1983]320       
321#if DEBUG_RAYCAST
[2572]322  Debug<<"BBB\n"<<flush;
[1757]323#endif
324
[2574]325  if (saveRays && preprocessor->mTotalRaysCast > saveRaysStart) {
[2572]326    if (castDoubleRay)
327      fprintf(fileOut, "G\n");
328    else
329      fprintf(fileOut, "H\n");
330  }
331 
332  for (i=0, k=offset; i < num; i++, k++)
333  {
334    Intersection hitA(rays[k].mOrigin), hitB(rays[k].mOrigin);
335
[1757]336#if DEBUG_RAYCAST
[2572]337    Debug<<"FH\n"<<flush;
[1757]338#endif
[1984]339
[2572]340    Intersectable *intersect =
341      mPreprocessor.GetParentObject(forward_hit_triangles[i]);
[1786]342
[2572]343    if (intersect)
344    {
345      hitA.mObject = intersect;
346      // Get the normal of that face
347      hitA.mNormal = mPreprocessor.GetParentNormal(forward_hit_triangles[i]);
348     
349      //-rays[index+i].mDirection; // $$ temporary
350      hitA.mPoint = rays[k].Extrap(forward_dist[i]);
351    }
352   
[1757]353#if DEBUG_RAYCAST
[2572]354    Debug<<"BH\n"<<flush;
[1757]355#endif
[1786]356
[2572]357    if (castDoubleRay)
358    {
359      Intersectable *intersect =
[2710]360                  mPreprocessor.GetParentObject(backward_hit_triangles[i]);
[2572]361     
362      if (intersect)
363      {
[2710]364                  hitB.mObject = intersect;
365                  hitB.mNormal = mPreprocessor.GetParentNormal(backward_hit_triangles[i]);
366                 
367                  // normalB = rays[i].mDirection; // $$ temporary
368                  hitB.mPoint = rays[k].Extrap(-backward_dist[i]);
[2572]369      }
370    }
[1786]371
372
[2574]373    if (saveRays && preprocessor->mTotalRaysCast > saveRaysStart) {
[2572]374      if (castDoubleRay)     
375        fprintf(fileOut, "%d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f %d %4.7f\n",
376                cntSavedRays,
377                rays[k].mOrigin.x,
378                rays[k].mOrigin.y,
379                rays[k].mOrigin.z,
380                rays[k].mDirection.x,
381                rays[k].mDirection.y,
382                rays[k].mDirection.z,
383                hitA.mObject ? 1 : 0,
384                hitA.mObject ? forward_dist[i] : 0,
385                hitB.mObject ? 1 : 0,
386                hitB.mObject ? -backward_dist[i] : 0
387                );
388      else
389        fprintf(fileOut, "%d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f\n",
390                cntSavedRays,
391                rays[k].mOrigin.x,
392                rays[k].mOrigin.y,
393                rays[k].mOrigin.z,
394                rays[k].mDirection.x,
395                rays[k].mDirection.y,
396                rays[k].mDirection.z,
397                hitA.mObject ? 1 : 0,
398                hitA.mObject ? forward_dist[i] : 0
399                );
400      cntSavedRays++;
401      cntSavedRaysFLUSH++;
402    }
[1786]403
[2572]404   
[1757]405#if DEBUG_RAYCAST
[2572]406    Debug<<"PR\n"<<flush;
[1757]407#endif
[1786]408
[2076]409#if 1
[2572]410    ProcessRay(rays[k],
411               hitA,
412               hitB,
413               vssRays,
414               sbox,
415               castDoubleRay,
416               pruneInvalidRays
417               );
[2076]418#endif
[2572]419  } // for
[1786]420
[2572]421  if (saveRays) {
422    if (cntSavedRaysFLUSH > intSavedLIMIT) {
423      fflush(fileOut);
424      cntSavedRaysFLUSH = 0;
425    }
426  }
427
[1520]428#if DEBUG_RAYCAST
[2572]429  Debug<<"C16F\n"<<flush;
[1520]430#endif
431}
432
[2076]433
434
[2105]435
436
[2076]437void
[2105]438IntelRayCaster::CastSimpleForwardRays(
[2572]439                                      SimpleRayContainer &rays,
440                                      const AxisAlignedBox3 &sbox
441                                      )
[2105]442{
443  int hit_triangles[16];
444  float dist[16];
445  Vector3 normals[16];
446  Vector3 min = sbox.Min();
447  Vector3 max = sbox.Max();
448 
[2705]449  int packets = (int)rays.size() / 16;
[2105]450 
451  int i, j, k = 0;
452  Vector3 dir;
453 
454  for (i=0; i < packets; i++) {
[2572]455    for (j=0; j < 16; j++, k++)
456      mlrtaStoreRayAS16(&rays[k].mOrigin.x,
457                        &rays[k].mDirection.x,
458                        j);
[2729]459    rawCastTimer.Entry();
460
[2572]461    mlrtaTraverseGroupAS16(&min.x,
462                           &max.x,
463                           hit_triangles,
464                           dist);       
[2729]465        rawCastTimer.Exit();
[2572]466  } // for
[2105]467
468
469  for (; k < rays.size(); k++) {
470        double normal[3];
[2729]471        rawCastTimer.Entry();
[2105]472        hit_triangles[0] = mlrtaIntersectAS(
[2572]473                                            &rays[k].mOrigin.x,
474                                            &rays[k].mDirection.x,
475                                            normal,
476                                            dist[0]);
[2729]477        rawCastTimer.Exit();
[2105]478  }
479 
480}
481
482
483void
[2705]484IntelRayCaster::CastRays(SimpleRayContainer &rays,
485                                                 VssRayContainer &vssRays,
486                                                 const AxisAlignedBox3 &sbox,
487                                                 bool castDoubleRay,
488                                                 bool pruneInvalidRays)
[2076]489{
490
[2705]491  int buckets = (int)rays.size() / 16;
[2076]492  int offset = 0;
[2105]493
494#if 0
495  int time = GetTime();
496  CastSimpleForwardRays(rays, sbox);
497  cout<<1e-3*2*rays.size()/TimeDiff(time, GetTime())<<" Mrays/sec"<<endl;
498#endif
[2076]499 
500  for (int i=0; i < buckets; i++, offset+=16) {
501        CastRays16(rays, offset, vssRays, sbox, castDoubleRay, pruneInvalidRays);
502
503        if ((int)rays.size() > 100000 && i % (100000/16) == 0)
504          cout<<"\r"<<offset<<"/"<<(int)rays.size()<<"\r";
505  }
506
[2187]507  for (; offset < (int)rays.size(); offset++)
[2076]508        CastRay(rays[offset], vssRays, sbox, castDoubleRay, pruneInvalidRays);
509
[1520]510}
511
[2076]512}
513
[1520]514#endif
Note: See TracBrowser for help on using the repository browser.