source: GTP/trunk/Lib/Vis/Preprocessing/src/Camera.cpp @ 2635

Revision 2635, 14.1 KB checked in by bittner, 16 years ago (diff)

GetPvsCost? changed, APplyFilter2 collects small kdnodes

Line 
1#include <algorithm>
2
3// the devil library
4#include <IL/il.h>
5#include <IL/ilu.h>
6#include <IL/ilut.h>
7#include <cassert>
8
9#include "common.h"
10#include "Camera.h"
11#include "Ray.h"
12#include "SimpleRay.h"
13#include "KdTree.h"
14#include "Mesh.h"
15#include "Exporter.h"
16#include "SceneGraph.h"
17#include "Preprocessor.h"
18#include "RayCaster.h"
19#include "tgaimg.h"
20
21
22#ifdef USE_HAVRAN_RAYCASTER
23//#include "timer.h"
24#include "raypack.h"
25#endif
26
27using namespace std;
28
29namespace GtpVisibilityPreprocessor {
30
31
32void
33InitDevIl()
34{
35  ilInit();
36  ILuint ImageName;
37  ilGenImages(1, &ImageName);
38  ilBindImage(ImageName);
39  ilEnable(IL_FILE_OVERWRITE);
40
41  //  ilRegisterFormat(IL_RGBA);
42  //  ilRegisterType(IL_FLOAT);
43
44  //  ilEnable(IL_ORIGIN_SET);
45  //  ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
46}
47
48bool
49Camera::SnapImage(string filename,
50                  KdTree *tree,
51                  SceneGraph *sceneGraph
52                 )
53{
54  int x;
55  int y;
56
57  bool exportRays = false;
58  CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
59 
60  vector<Ray *> rays;
61
62  long t1 = GetTime();
63
64  Ray ray;
65
66  for (y = 0; y < mHeight; y++) {
67    cout<<"+" << flush;
68    for (x = 0; x < mWidth; x++) {
69      SetupRay(ray, mWidth - (x + 1), mHeight - (y + 1));
70
71          bool debug = true;
72          //      (y == mHeight/2) && (x== mWidth/3);
73          //      MeshDebug = debug;
74         
75          if (debug)
76                ray.mFlags = (Ray::STORE_TESTED_OBJECTS | Ray::STORE_KDLEAVES);
77          else
78                ray.mFlags &= ~(Ray::STORE_TESTED_OBJECTS|Ray::STORE_KDLEAVES);
79         
80          if (tree->CastRay(ray)) {
81                //              cout<<"I1";
82
83                if (ray.intersections.size()) {
84                  sort(ray.intersections.begin(), ray.intersections.end());
85                  MeshInstance *mesh = (MeshInstance*)ray.intersections[0].mObject;
86                  RgbColor color(1,1,1);
87                  if (mesh->GetMesh()->mMaterial)
88                        color = mesh->GetMesh()->mMaterial->mDiffuseColor;
89
90                  tgaimg.SetPixel(x, y, color.r * 255.f, color.g * 255.f, color.b * 255.f);
91                }
92          }
93          if (debug) {
94            Ray *nray = new Ray(ray);
95            rays.push_back(nray);
96          }
97    }
98  }
99 
100  long t2 = GetTime();
101  cout<<"#RAY_CAST_TIME\n";
102  cout<<TimeDiff(t1, t2)<<"\n";
103 
104 
105  cout<<"Saving image"<<endl;
106  tgaimg.SaveToFile(filename.c_str()); 
107
108  cout<<"done."<<endl<<flush;
109 
110  Exporter *exporter = NULL;
111  if (1) {
112    exporter = Exporter::GetExporter(filename + "-rays" + ".x3d");
113    //exporter->SetFilled();
114
115        exporter->SetWireframe();
116
117        if (sceneGraph)
118          exporter->ExportScene(sceneGraph->GetRoot());
119       
120    //exporter->ExportKdTree(*tree);
121        //exporter->ExportBspTree(*bsptree);
122    exporter->ExportRays(rays, 2000);
123    int k =0;
124    for (int j=0; j < rays.size(); j++)
125      if (rays[j]->kdLeaves.size()) {
126                Ray *ray = rays[j];
127                int i;
128                exporter->SetWireframe();
129
130                if (1)
131                  for (i= 0; i < ray->kdLeaves.size(); i++)
132                        exporter->ExportBox(tree->GetBox(ray->kdLeaves[i]));
133
134                exporter->SetFilled();
135               
136                if (1)
137                  for (i= 0; i < ray->testedObjects.size(); i++)
138                        exporter->ExportIntersectable(ray->testedObjects[i]);
139      }
140       
141    delete exporter;
142  }
143 
144  return true;
145}
146
147
148void
149Camera::SetupRay(Ray &ray, const int x, const int y)
150{
151  Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
152  Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
153  Vector3 target = xv + yv + mDirection;
154
155  ray.Clear();
156  ray.Init(mPosition, target, Ray::LOCAL_RAY);
157  ray.mFlags &= ~Ray::CULL_BACKFACES;
158}
159
160
161void
162Camera::SetupRay(SimpleRay &ray, const int x, const int y)
163{
164  Vector3 xv = mRight*((x - mWidth/2)/(float)mWidth);
165  Vector3 yv = mUp*((y - mHeight/2)/(float)mHeight);
166  Vector3 dir = mDirection - xv + yv;
167  dir.Normalize();
168 
169  ray.Set(mPosition, dir, 0,
170          1.0f, ~(SimpleRay::F_BIDIRECTIONAL));
171}
172
173bool
174Camera::SnapImage(string filename,
175                  RayCaster *raycaster,
176                  AxisAlignedBox3 &bbox,
177                  SceneGraph *sceneGraph
178                  )
179{
180  int x;
181  int y;
182
183  bool exportRays = false;
184  CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
185  tgaimg.FillInImage(0.0, 0.0f, 200.f);
186 
187  vector<Ray *> rays;
188
189  long t1 = GetTime();
190
191  SimpleRay ray;
192  VssRayContainer vssRays;
193
194  bool doubleRays = true;
195  bool exploitDoubleRays = true;
196  if (!doubleRays)
197    exploitDoubleRays = false;
198 
199  //CTimer timer;
200  //timer.Start();
201 
202  //int ystart = 92;
203  int xstart = 0;
204  int ymax = mHeight;
205  for (y = 0; y < ymax; y++) {
206    cout<<"+" << flush;
207    for (x = 0; x < mWidth; x++) {
208      SetupRay(ray, x, y);
209      if (exploitDoubleRays)
210        ray.mDirection = - ray.mDirection;
211
212      bool debug = false;
213      //          (y == mHeight/2) && (x== mWidth/3);
214      //          MeshDebug = debug;
215
216      int res = raycaster->CastRay(ray,
217                                   vssRays,
218                                   bbox,
219                                   doubleRays, // castDoubleRay,
220                                   false); // pruneInvalidRays
221
222      // cout << "End ------------------------ "
223      // << SimpleRay::IntersectionRes[0].intersectable
224      // << " t = " << SimpleRay::IntersectionRes[0].tdist
225      // << endl;
226     
227      if (res) {
228        Vector3 normal = SimpleRay::IntersectionRes[0].intersectable->GetNormal(0);
229        float v =
230          ray.mDirection.x * normal.x +
231          ray.mDirection.y * normal.y +
232          ray.mDirection.z * normal.z;
233        v = fabs(v);
234        v *= 200.0f;
235        if (v < 50.f) v = 50.f;
236        tgaimg.SetPixel(x, y, v, v, v);
237      }
238     
239      if (debug) {
240        Ray *nray = new Ray(ray.mOrigin, ray.mDirection,
241                            Ray::LOCAL_RAY);
242        rays.push_back(nray);
243      }
244    }
245  } // for y
246
247  //timer.Stop();
248 
249  long t2 = GetTime();
250  cout<<"\n#RAY_CAST_TIME = ";
251  cout << TimeDiff(t1, t2)<<" [mikrosec]\n"; 
252 
253  cout<<"Saving image"<<endl;
254
255  tgaimg.SaveToFile(filename.c_str()); 
256 
257  //ilRegisterType(IL_FLOAT);
258  //ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
259  //ilSaveImage((char *const)filename.c_str());
260  //delete buffer;
261
262  cout<<"done."<<endl<<flush;
263   
264  return true;
265}
266
267
268bool
269Camera::SnapImage2(string filename,
270                   RayCaster *raycaster,
271                   AxisAlignedBox3 &bbox,
272                   SceneGraph *sceneGraph)
273{
274  int x;
275  int y;
276
277  bool exportRays = false;
278  CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
279  tgaimg.FillInImage(0.0, 0.0f, 200.f);
280 
281  vector<Ray *> raysDebug;
282
283  long t1 = GetTime();
284
285  SimpleRay ray;
286  SimpleRayContainer rays;
287  VssRayContainer vssRays;
288
289  bool doubleRays = false;
290  bool exploitDoubleRays = false;
291  int batchSize = 16;
292  if (!doubleRays)
293    exploitDoubleRays = false;
294  int shiftIndex = 0;
295  if (exploitDoubleRays)
296    shiftIndex = batchSize;
297   
298  //CTimer timer;
299  //timer.Start();
300 
301  //int ystart = 92;
302  int xstart = 0;
303  int ymax = mHeight;
304  for (y = 0; y < ymax; y++) {
305    if (y == 250) {
306      cout << "Debug y = " << y << endl;
307    }
308   
309    cout<<"+" << flush;
310    for (x = 0; x < mWidth; x += batchSize ) {
311      rays.erase(rays.begin(), rays.end());
312      int xi;
313      for (xi = 0; (xi < batchSize) && (x+xi < mWidth); xi++ ) {
314        SetupRay(ray, x + xi, y);
315        if (exploitDoubleRays)
316          ray.mDirection = -ray.mDirection;
317        rays.push_back(ray);
318      } // for
319
320      bool debug = false;
321      //          (y == mHeight/2) && (x== mWidth/3);
322      //          MeshDebug = debug;
323
324#if 1
325      assert(batchSize == 16);
326      raycaster->CastRays16(rays,
327                            vssRays,
328                            bbox,
329                            doubleRays, // castDoubleRay,
330                            false); // pruneInvalidRays
331#else
332      raycaster->CastSimpleForwardRays(rays, bbox);
333#endif
334     
335      // cout << "End ------------------------ "
336      // << SimpleRay::IntersectionRes[0].intersectable
337      // << " t = " << SimpleRay::IntersectionRes[0].tdist
338      // << endl;
339     
340      for (xi = 0; (xi < batchSize) && (x+xi < mWidth); xi++ ) {
341        Intersectable* res =
342          SimpleRay::IntersectionRes[xi + shiftIndex].intersectable;
343
344        if (res) {
345          Vector3 normal = res->GetNormal(0);
346          float v =
347            rays[xi].mDirection.x * normal.x +
348            rays[xi].mDirection.y * normal.y +
349            rays[xi].mDirection.z * normal.z;
350          v = fabs(v);
351          v *= 200.0f;
352          if (v < 50.f) v = 50.f;
353          tgaimg.SetPixel(x + xi, y, v, v, v);
354        }
355      }
356     
357      if (debug) {
358        Ray *nray = new Ray(ray.mOrigin, ray.mDirection,
359                            Ray::LOCAL_RAY);
360        raysDebug.push_back(nray);
361      }
362    } // for x
363  } // for y
364
365  //timer.Stop();
366 
367  long t2 = GetTime();
368  cout<<"\n#RAY_CAST_TIME = ";
369  cout << TimeDiff(t1, t2)<<" [mikrosec]\n"; 
370 
371  cout<<"Saving image"<<endl;
372
373  tgaimg.SaveToFile(filename.c_str()); 
374 
375  //ilRegisterType(IL_FLOAT);
376  //ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_FLOAT, buffer);
377  //ilSaveImage((char *const)filename.c_str());
378  //delete buffer;
379
380  cout<<"done."<<endl<<flush;
381   
382  return true;
383}
384
385 
386bool
387Camera::SnapImagePacket(string filename,
388                        RayCaster *raycaster,
389                        AxisAlignedBox3 &bbox,
390                        SceneGraph *sceneGraph
391                        )
392{
393#ifdef USE_HAVRAN_RAYCASTER
394#ifdef _USE_HAVRAN_SSE
395 
396  int x;
397  int y;
398
399  bool exportRays = false; 
400
401  //  - components*mWidth;
402  CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
403  tgaimg.FillInImage(0.0, 0.0f, 200.f); // blue color image
404 
405  vector<Ray *> rays;
406
407  long t1 = GetTime();
408
409  SimpleRay ray;
410  VssRayContainer vssRays;
411
412  //CTimer timer;
413  //timer.Start();
414
415  bool doubleRays = false;
416  bool exploitDoubleRays = false;
417  if (!doubleRays)
418    exploitDoubleRays = false;
419 
420  GALIGN16 RayPacket2x2 rp;
421  //int ystart = 92;
422  //int xstart = 738;
423  int pixels = 0;;
424  for (y = 0; y < mHeight-1; y+=2) {
425  //for (y = ystart-2; y > 0; y-=2) {
426    for (x = 0; x < mWidth-1; x+=2) {
427      int i = 0;
428      for (int yi = 0; yi < 2; yi++) {
429        for (int xi = 0; xi < 2; xi++) {
430          SetupRay(ray, x+xi, y+yi);
431          rp.SetLoc(i, ray.mOrigin);
432          rp.SetDir(i, ray.mDirection);
433          if (exploitDoubleRays)
434            rp.SetDir(i, -ray.mDirection);
435          i++;
436        } // for xi
437      } // for yi     
438      pixels += 4;
439      if (pixels > mWidth) {
440        cout << "+" << flush;
441        pixels = 0;
442      }
443     
444      raycaster->CastRaysPacket2x2(rp, doubleRays, false);
445
446      i = 0;
447      for (int yi = 0; yi < 2; yi++) {
448        for (int xi = 0; xi < 2; xi++) {
449          Intersectable* res = rp.GetObject(i);
450          if (res) {
451#if 0
452            // This is debugging code to find a bug
453            for (int j = 0; j < 4; j++) {
454              cout << " j = " << j << " obj = "
455                   << rp.GetObject(j)
456                   << " t = " << rp.GetT(j);
457              ray.Set(rp.GetLoc(j), rp.GetDir(j), 0, 1.0f, 0);
458              int res = raycaster->CastRay(ray,
459                                           vssRays,
460                                           bbox,
461                                           doubleRays, // castDoubleRay,
462                                           false); // pruneInvalidRays
463              cout << " correct result obj = "
464                   << SimpleRay::IntersectionRes[0].intersectable
465                   << " t = "
466                   << SimpleRay::IntersectionRes[0].tdist
467                   << endl;             
468            } // for
469
470            raycaster->CastRaysPacket2x2(rp, false, false);
471#endif
472           
473            Vector3 normal = res->GetNormal(0);
474            float v =
475              ray.mDirection.x * normal.x +
476              ray.mDirection.y * normal.y +
477              ray.mDirection.z * normal.z;
478            v = fabs(v);
479            v *= 200.0f;
480            if (v < 50.f) v = 50.f;
481            tgaimg.SetPixel(x+xi, y+yi, v, v, v);
482          }
483          i++;
484        } // xi
485      } // yi
486    } // for x
487  } // for y
488
489  //timer.Stop();
490 
491  long t2 = GetTime();
492  cout<<"\n#RAY_CAST_TIME = ";
493  cout << TimeDiff(t1, t2)<<" [mikrosec]\n"; 
494 
495  cout<<"Saving image"<<endl;
496  tgaimg.SaveToFile(filename.c_str()); 
497   
498#endif // _USE_HAVRAN_SSE
499#endif // USE_HAVRAN_RAYCASTER
500  cout<<"done."<<endl<<flush;
501   
502  return true;
503}
504 
505inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
506{
507  return obj1->mId < obj2->mId;
508}
509
510
511bool
512Camera::SnapImagePacket2(string filename,
513                        RayCaster *raycaster,
514                        AxisAlignedBox3 &bbox,
515                        SceneGraph *sceneGraph
516                        )
517{
518#ifdef USE_HAVRAN_RAYCASTER
519#ifdef _USE_HAVRAN_SSE
520 
521  int x;
522  int y;
523
524  bool exportRays = false; 
525
526  //  - components*mWidth;
527  CTGAImage tgaimg; tgaimg.Init(mWidth, mHeight);
528  tgaimg.FillInImage(0.0, 0.0f, 200.f); // blue color image
529 
530  vector<Ray *> rays;
531
532  long t1 = GetTime();
533
534  SimpleRay ray;
535  VssRayContainer vssRays;
536
537  //CTimer timer;
538  //timer.Start();
539
540  bool doubleRays = false;
541  bool exploitDoubleRays = false;
542  if (!doubleRays)
543    exploitDoubleRays = false;
544 
545  Vector3 origin4[4];
546  Vector3 direction4[4];
547  int     result4[4];
548  float   dist4[4];
549 
550  GALIGN16 RayPacket2x2 rp;
551  //int ystart = 92;
552  //int xstart = 738;
553  int pixels = 0;;
554  for (y = 0; y < mHeight-1; y+=2) {
555  //for (y = ystart-2; y > 0; y-=2) {
556    for (x = 0; x < mWidth-1; x+=2) {
557      int i = 0;
558      for (int yi = 0; yi < 2; yi++) {
559        for (int xi = 0; xi < 2; xi++) {
560          SetupRay(ray, x+xi, y+yi);
561          origin4[i] = ray.mOrigin;
562          direction4[i] = ray.mDirection;
563          if (exploitDoubleRays)
564            direction4[i] = -ray.mDirection;
565          i++;
566        } // for xi
567      } // for yi     
568      pixels += 4;
569      if (pixels > mWidth) {
570        cout << "+" << flush;
571        pixels = 0;
572      }
573     
574      raycaster->CastRaysPacket4(bbox.Min(), bbox.Max(),
575                                 origin4,
576                                 direction4,
577                                 result4,
578                                 dist4);
579
580      i = 0;
581      for (int yi = 0; yi < 2; yi++) {
582        for (int xi = 0; xi < 2; xi++) {
583          int objId = result4[i];
584          if (objId != -1) {
585            MeshInstance dummyInst(NULL);
586            dummyInst.SetId(objId);
587            ObjectContainer &objects = preprocessor->mObjects;
588            ObjectContainer::const_iterator oit =
589              lower_bound(objects.begin(), objects.end(),
590                          (Intersectable *)&dummyInst, ilt);
591     
592            if ((oit != objects.end()) && ((*oit)->GetId() == objId)) {
593              Intersectable *res = *oit;
594           
595              Vector3 normal = res->GetNormal(0);
596              float v =
597                ray.mDirection.x * normal.x +
598                ray.mDirection.y * normal.y +
599                ray.mDirection.z * normal.z;
600              v = fabs(v);
601              v *= 200.0f;
602              if (v < 50.f) v = 50.f;
603              tgaimg.SetPixel(x+xi, y+yi, v, v, v);
604            }
605            else {
606              cout <<"*" << endl;
607            }
608           
609          } // object intersected
610          i++;
611        } // xi
612      } // yi
613    } // for x
614  } // for y
615
616  //timer.Stop();
617 
618  long t2 = GetTime();
619  cout<<"\n#RAY_CAST_TIME = ";
620  cout << TimeDiff(t1, t2)<<" [mikrosec]\n"; 
621 
622  cout<<"Saving image"<<endl;
623  tgaimg.SaveToFile(filename.c_str()); 
624   
625#endif // _USE_HAVRAN_SSE
626#endif // USE_HAVRAN_RAYCASTER
627  cout<<"done."<<endl<<flush;
628   
629  return true;
630}
631
632}
633
Note: See TracBrowser for help on using the repository browser.