source: GTP/trunk/Lib/Vis/Preprocessing/src/HavranRayCaster.cpp @ 2583

Revision 2583, 11.6 KB checked in by bittner, 16 years ago (diff)

Havran ray caster functional except enhanced features used by mutation and object based pvs

Line 
1
2// Written by Vlastimil Havran, December 2007
3
4// This macro allows to use the ray shooting written by
5// Vlastimil Havran, 2007-2008
6
7
8#include "HavranRayCaster.h"
9#include "VssRay.h"
10#include "KdTree.h"
11#include "Preprocessor.h"
12#include "IntersectableWrapper.h"
13
14#ifdef USE_HAVRAN_RAYCASTER
15#include "timer.h"
16#include "ktball.h"
17#include "ktb.h"
18#include "raypack.h"
19#endif // USE_HAVRAN_RAYCASTER
20
21#define DEBUG_RAYCAST 0
22
23namespace GtpVisibilityPreprocessor {
24
25
26#ifdef USE_HAVRAN_RAYCASTER
27// static rays
28RayPacket2x2
29HavranRayCaster::raypack;
30#endif // USE_HAVRAN_RAYCASTER
31 
32HavranRayCaster::HavranRayCaster(const Preprocessor &preprocessor):
33  RayCaster(preprocessor), mKtbtree(0)
34{
35#ifdef USE_HAVRAN_RAYCASTER
36  mKtbtree = new CKTB();
37#endif 
38}
39 
40
41HavranRayCaster::~HavranRayCaster()
42{
43#ifdef USE_HAVRAN_RAYCASTER
44  delete mKtbtree;
45  mKtbtree = 0;
46#endif // USE_HAVRAN_RAYCASTER
47}
48
49void
50HavranRayCaster::Build(ObjectContainer &objlist)
51{
52#ifdef USE_HAVRAN_RAYCASTER
53
54  CTimer timer;
55
56  cout << "\nBuilding up kd-tree for Havran ray caster ..."<<endl<<flush;
57
58  timer.Start();
59  mKtbtree->BuildUp(objlist);
60  timer.Stop();
61  cout << "\nBuilding up kd-tree is finished, user time = "
62       << timer.UserTime() << " real time = " << timer.RealTime() <<
63        endl <<flush;
64#endif
65}
66
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 !
71void HavranRayCaster::CastRaysPacket4(Vector3 &boxmax,
72                                                                          Vector3 &boxmin,
73                                                                          Vector3 origin4[],
74                                                                          Vector3 direction4[],
75                                                                          int     result4[],
76                                                                          float   dist4[])
77{
78#ifdef USE_HAVRAN_RAYCASTER
79  for (int i = 0; i < 4; i++) {
80    result4[i] = -1;
81    raypack.SetLoc(i, origin4[i]);
82    raypack.SetDir(i, direction4[i]);
83  }
84 
85  // The same operations for packets of rays, if implemented by
86  // a particular ASDS, otherwise it is emulated by decomposition
87  // of a packet to individual rays and traced individually.
88  mKtbtree->FindNearestI(raypack, boxmin, boxmax);
89
90  for (int i = 0; i < 4; i++) {
91    // if ray intersects an object, we set the pointer to
92    // this object
93    Intersectable* intersectable;
94    if ( (intersectable = raypack.GetObject(i)) )
95          // $$JB this is very hacky - > should be replaced with fetching the index of the triangle
96          result4[i] = (int)intersectable;
97  }
98 
99  return;
100#endif
101}
102
103int HavranRayCaster::CastRay(const SimpleRay &simpleRay,
104                             VssRayContainer &vssRays,
105                             const AxisAlignedBox3 &box,
106                             const bool castDoubleRay,
107                             const bool pruneInvalidRays)
108{
109#ifdef USE_HAVRAN_RAYCASTER
110
111  int hits = 0;
112  Intersection hitA(simpleRay.mOrigin), hitB(simpleRay.mOrigin);
113 
114  // inside test for bounding box
115  // enlarge box slightly so the view point fits for sure
116  //  AxisAlignedBox3 sbox = box;
117  //  sbox.Enlarge(Vector3(-Limits::Small));
118  // $$ JB moved here from Validate routine
119
120//   if (!box.IsInside(simpleRay.mOrigin)) {
121//      cout<<"out of box "<<simpleRay.mOrigin<<" "<<box<<endl;
122//      return 0;
123//   }
124 
125  // ray.mFlags &= ~Ray::CULL_BACKFACES;
126
127  if (mKtbtree->FindNearestI(simpleRay))  {
128    hitA.mObject = SimpleRay::IntersectionRes[0].intersectable;
129    float tdist = SimpleRay::IntersectionRes[0].tdist;
130    hitA.mPoint = simpleRay.Extrap(tdist);
131    hitA.mNormal = SimpleRay::IntersectionRes[0].intersectable->GetNormal(0);
132    //hitA.mNormal = (dynamic_cast< TriangleIntersectable *>
133    //              (hitA.mObject))->GetNormal(0);
134  }
135 
136 
137  if (castDoubleRay) {
138    Vector3 *v = (Vector3*)(&simpleRay.mDirection);
139    *v = -(*v);
140    // ray.mFlags &= ~Ray::CULL_BACKFACES;
141    if (mKtbtree->FindNearestI(simpleRay))  {
142      hitB.mObject = SimpleRay::IntersectionRes[0].intersectable;
143      float tdist = SimpleRay::IntersectionRes[0].tdist;
144      hitB.mPoint = simpleRay.Extrap(tdist);
145      hitB.mNormal = SimpleRay::IntersectionRes[0].intersectable->GetNormal(0);
146      //hitB.mNormal = (dynamic_cast< TriangleIntersectable *>
147      //              (hitB.mObject))->GetNormal(0);
148    }
149    // restore the direction to the original
150    *v = -simpleRay.mDirection;
151  }
152
153  // This code is also in IntelRayCaster.cpp
154  return ProcessRay(
155                    simpleRay,
156                    hitA,
157                    hitB,
158                    vssRays,
159                    box,
160                    castDoubleRay,
161                    pruneInvalidRays
162                    );
163#else // USE_HAVRAN_RAYCASTER
164  return 0;
165#endif
166}
167
168void HavranRayCaster::CastRays16(SimpleRayContainer &rays,
169                                 VssRayContainer &vssRays,
170                                 const AxisAlignedBox3 &sbox,
171                                 const bool castDoubleRay,
172                                 const bool pruneInvalidRays)
173{
174  CastRays16(rays, 0, vssRays, sbox, castDoubleRay, pruneInvalidRays);
175}
176
177
178void HavranRayCaster::CastRays16(SimpleRayContainer &rays,
179                                 int offset,
180                                 VssRayContainer &vssRays,
181                                 const AxisAlignedBox3 &sbox,
182                                 const bool castDoubleRays,
183                                 const bool pruneInvalidRays)
184{
185#ifdef USE_HAVRAN_RAYCASTER
186 
187#if DEBUG_RAYCAST
188  Debug << "C16 " << flush;
189#endif
190
191#if 0
192  SimpleRayContainer::const_iterator sit, sit_end = rays.end();
193
194  // no acceleration for ray bundles implemented right now
195  for (sit = rays.begin(); sit != sit_end; ++ sit)
196  {
197    CastRay(*sit, vssRays, sbox, castDoubleRays, pruneInvalidRays);
198  }
199#else
200  if (castDoubleRays) {
201#if 0
202    // Special routine to cast double sided rays
203    mKtbtree->SetOffset(0);
204    mKtbtree->FindNearestI_16twoDir(rays);
205#else
206    // Here we decompose shooting into two phases
207#if 1   
208    // Here we shoot first forward rays and then backward ones
209    mKtbtree->SetOffset(0);
210    mKtbtree->FindNearestI_16oneDir(rays, offset);
211    SimpleRayContainer::iterator sit = rays.begin() + offset;
212    SimpleRayContainer::const_iterator sit_end = rays.begin() + offset + 16;
213    for (; sit != sit_end; ++ sit)
214    {
215      (*sit).mDirection = - (*sit).mDirection;
216    }
217    // We store the results at different place
218    mKtbtree->SetOffset(16);
219    mKtbtree->FindNearestI_16oneDir(rays, offset);
220    sit = rays.begin() + offset;
221    for ( ; sit != sit_end; ++ sit)
222    {
223      (*sit).mDirection = - (*sit).mDirection;
224    }
225#else
226    // Here we shoot first backward rays and forward ones
227    SimpleRayContainer::iterator sit = rays.begin() + offset;
228    SimpleRayContainer::const_iterator sit_end = rays.begin() + offset + 16;
229    for ( ; sit != sit_end; ++ sit)
230    {
231      (*sit).mDirection = - (*sit).mDirection;
232    }
233    // backward rays to be shot
234    mKtbtree->SetOffset(16);
235    mKtbtree->FindNearestI_16oneDir(rays, offset);
236    sit = rays.begin() + offset;
237    for ( ; sit != sit_end; ++ sit)
238    {
239      (*sit).mDirection = - (*sit).mDirection;
240    }
241    // forward rays to be shot
242    mKtbtree->SetOffset(0);
243    mKtbtree->FindNearestI_16oneDir(rays, offset);
244#endif   
245#endif   
246  }
247  else {
248    // Shoot all 16 rays  at the same time using a special algorithm
249    mKtbtree->SetOffset(0);
250    mKtbtree->FindNearestI_16oneDir(rays, offset);   
251  }
252#endif
253
254 
255  for (int i=0, k=offset; i < 16; i++, k++)
256  {
257    Intersection hitA(rays[k].mOrigin), hitB(rays[k].mOrigin);
258
259#if DEBUG_RAYCAST
260    Debug<<"FH\n"<<flush;
261#endif
262
263    Intersectable *intersect = SimpleRay::IntersectionRes[i].intersectable;
264
265    if (intersect)
266    {
267      hitA.mObject = intersect;
268      // Get the normal of that face
269      hitA.mNormal = intersect->GetNormal(0);
270     
271      //-rays[index+i].mDirection; // $$ temporary
272      float tdist = SimpleRay::IntersectionRes[i].tdist;
273      hitA.mPoint = rays[k].Extrap(tdist);
274    }
275   
276#if DEBUG_RAYCAST
277    Debug<<"BH\n"<<flush;
278#endif
279
280    if (castDoubleRays)
281    {
282      Intersectable *intersect =
283        SimpleRay::IntersectionRes[i+16].intersectable;
284
285      if (intersect)
286      {
287        hitB.mObject = intersect;
288        hitB.mNormal = intersect->GetNormal(0);;
289        float tdist = SimpleRay::IntersectionRes[i].tdist;
290        hitB.mPoint = rays[k].Extrap(-tdist);
291      }
292    }
293   
294#if DEBUG_RAYCAST
295    Debug<<"PR\n"<<flush;
296#endif
297
298#if 1
299    ProcessRay(rays[k],
300               hitA,
301               hitB,
302               vssRays,
303               sbox,
304               castDoubleRays,
305               pruneInvalidRays
306               );
307#endif
308  } // for
309
310 
311#if DEBUG_RAYCAST
312  Debug<<"C16F\n"<<flush;
313#endif
314
315#endif // USE_HAVRAN_RAYCASTER
316}
317
318
319
320void
321HavranRayCaster::CastSimpleForwardRays(
322                                       SimpleRayContainer &rays,
323                                       const AxisAlignedBox3 &sbox
324                                      )
325{
326  int hit_triangles[16];
327  float dist[16];
328  Vector3 normals[16];
329  Vector3 min = sbox.Min();
330  Vector3 max = sbox.Max();
331 
332  int packets = rays.size() / 16;
333 
334  int i, j, k = 0;
335  Vector3 dir;
336 
337  // By groups of rays
338  for (i=0; i < packets; i++) {
339    int offset = i * 16;
340    mKtbtree->FindNearestI_16oneDir(rays, offset);
341    // ??? What to do with the results ? These are
342    // not used at the moment also in IntelRayCaster.cpp
343  } // for
344
345
346  for (; k < rays.size(); k++) {
347    double normal[3];
348    mKtbtree->FindNearestI(rays[k]);
349    // ??? What to do with the results ? These are
350    // not used at the moment also in IntelRayCaster.cpp
351  }
352
353
354  return;
355}
356
357void HavranRayCaster::CastRays(
358                               SimpleRayContainer &rays,
359                               VssRayContainer &vssRays,
360                               const AxisAlignedBox3 &sbox,
361                               const bool castDoubleRay,
362                               const bool pruneInvalidRays )
363{
364  int buckets = rays.size()/16;
365  int offset = 0;
366
367#if 0
368  int time = GetTime();
369  CastSimpleForwardRays(rays, sbox);
370  cout<<1e-3*2*rays.size()/TimeDiff(time, GetTime())<<" Mrays/sec"<<endl;
371#endif
372 
373  for (int i=0; i < buckets; i++, offset+=16) {
374    CastRays16(rays, offset, vssRays, sbox,
375               castDoubleRay, pruneInvalidRays);
376
377    if ((int)rays.size() > 100000 && i % (100000/16) == 0)
378      cout<<"\r"<<offset<<"/"<<(int)rays.size()<<"\r";
379  }
380
381  for (; offset < (int)rays.size(); offset++)
382    CastRay(rays[offset], vssRays, sbox, castDoubleRay, pruneInvalidRays);
383}
384
385#if 0
386// BUG1 41579  196948 1064111
387// BUG2 254    1672   10869
388 
389// Just for testing concept
390void
391HavranRayCaster::CastRaysPacket2x2(RayPacket2x2 &raysPack,
392                                                                   bool castDoubleRay,
393                                                                   const bool pruneInvalidRays)
394{
395#ifdef USE_HAVRAN_RAYCASTER
396
397  if (castDoubleRay) {
398    // cast forward rays
399    mKtbtree->FindNearestI(raysPack);
400    for (int i = 0; i < 4; i++)
401      raysPack.SetDir(i, -raysPack.GetDir(i));
402    // cast backward rays
403    mKtbtree->FindNearestI(raysPack);
404    // reverse the rays back
405    for (int i = 0; i < 4; i++)
406      raysPack.SetDir(i, -raysPack.GetDir(i));
407  }
408  else {
409    // only rays forwards
410    mKtbtree->FindNearestI(raysPack);
411#if 0
412    // Only verification of correctness by casting single rays
413    static int cntBugs = 0;
414    SimpleRay ray;
415    int cntErrors = 0, cntDistErrors = 0;
416    bool newBug = false;
417    for (int i = 0; i < 4; i++) {
418      ray.mOrigin = raysPack.GetLoc(i);
419      ray.mDirection = raysPack.GetDir(i);
420      mKtbtree->FindNearestI(ray);
421      if (raysPack.GetObject(i) != SimpleRay::IntersectionRes[0].intersectable) {
422        float dist = (raysPack.GetT(i) - SimpleRay::IntersectionRes[0].tdist);
423        if (fabs(dist) > 0.001f) {
424          cntErrors++; newBug = true;
425          cntBugs++;
426          cout << " BUG1 d= " << dist;
427        }
428      }
429      else {
430        float dist = 0.f;
431        if (raysPack.GetObject(i) && SimpleRay::IntersectionRes[0].intersectable)
432          if (fabs((dist=(fabs (raysPack.GetT(i) - SimpleRay::IntersectionRes[0].tdist)))) > 1.f) {
433            cntDistErrors++; newBug = true; cntBugs++;
434            cout << " BUG2 distdf= " << dist ;     
435          }
436      }
437    } // for
438    if (newBug) cout << " CB= " << cntBugs << "\n";
439#endif
440  }
441
442  return;
443#endif // USE_HAVRAN_RAYCASTER
444}
445#endif
446
447
448} // the namespace
Note: See TracBrowser for help on using the repository browser.