source: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp @ 2105

Revision 2105, 7.7 KB checked in by bittner, 17 years ago (diff)

simple ray separated

RevLine 
[1520]1#include "RayCaster.h"
2#include "VssRay.h"
3#include "Ray.h"
4#include "Preprocessor.h"
[1942]5#include "ViewCellsManager.h"
[1520]6
7
8namespace GtpVisibilityPreprocessor {
9
10
11#define DEBUG_RAYCAST 0
12
[1942]13#define EXACT_BOX_CLIPPING 0
[1520]14
15RayCaster::RayCaster(const Preprocessor &preprocessor):
16mPreprocessor(preprocessor)
17{
18}
19
20
[1521]21RayCaster::~RayCaster()
22{
23}
24
25
[1545]26VssRay *RayCaster::CastRay(const SimpleRay &simpleRay,
[1990]27                                                   const AxisAlignedBox3 &box,
[1996]28                                                   const bool castDoubleRay)
[1521]29{
[1932]30        VssRayContainer rays;
[1996]31        CastRay(simpleRay, rays, box, castDoubleRay, true);
[1932]32   
33        if (!rays.empty())
34                return rays.back();
35        else
36                return NULL;
[1521]37}
38
[1990]39
[1942]40bool
41RayCaster::ClipToViewSpaceBox(const Vector3 &origin,
42                                                          const Vector3 &termination,
43                                                          Vector3 &clippedOrigin,
44                                                          Vector3 &clippedTermination)
45{
[1952]46 
[1942]47  Ray ray(origin, termination - origin, Ray::LINE_SEGMENT);     
48  ray.Precompute();
49 
50  float tmin, tmax;
51  if ((!mPreprocessor.mViewCellsManager->
52           GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax)) ||
53          tmin>=tmax
54          )
55        return false;
[1521]56
[2070]57  if (tmin >= 1.0f || tmax <= 0.0f)
[1942]58        return false;
59 
60  if (tmin > 0.0f)
61        clippedOrigin = ray.Extrap(tmin);
62  else
63        clippedOrigin = origin;
64 
65  if (tmax < 1.0f)
66        clippedTermination = ray.Extrap(tmax);
67  else
68        clippedTermination = termination;
69 
70  return true;
71}
72
[2070]73/** Checks if ray is valid
74        (e.g., not in empty view space or outside the view space)
[1528]75*/
[1867]76bool
77RayCaster::ValidateRay(const Vector3 &origin,
78                                           const Vector3 &direction,
79                                           const AxisAlignedBox3 &box,
80                                           Intersection &hit)
[1528]81{
[1932]82        if (!hit.mObject) 
83        {
84                // compute intersection with the scene bounding box
[1584]85#if EXACT_BOX_CLIPPING
[1932]86                static Ray ray;
87                mPreprocessor.SetupRay(ray, origin, direction);
88
89                float tmin, tmax;
90                if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
91                {
92                        hit.mPoint = ray.Extrap(tmax);
93                }
94                else
95                {
96                        // cout<<" invalid hp "<<tmin<<" "<<tmax<<endl;
97                        // cout<<" box "<<box<<endl;
98                        // cout<<" origin "<<origin<<endl;
99                        // cout<<" direction "<<direction<<endl;
100                        // cout<< "inv dir"<<ray.GetInvDir()<<endl;
101                        return false;
102                }
[1584]103#else
[1932]104                hit.mPoint = origin + direction * Magnitude(box.Diagonal());
[1584]105#endif
106        }
[1932]107        else
108        {
[2035]109          if (mPreprocessor.mDetectEmptyViewSpace)  {
110                if (DotProd(hit.mNormal, direction) >= -Limits::Small)  {       
111                  hit.mObject = NULL;
112                  return false;
[1932]113                }
[2035]114          }
[1932]115        }
[2035]116       
[1932]117        return true;
[1528]118}
119
[1974]120void
121RayCaster::SortRays(SimpleRayContainer &rays)
122{
[1984]123  AxisAlignedBox3 box =
124        mPreprocessor.mViewCellsManager->GetViewSpaceBox();
[2105]125 
[1984]126  float b[12]={
127        box.Min().x,
128        box.Min().y,
129        box.Min().z,
130        -1,
131        -1,
132        -1,
133        box.Max().x,
134        box.Max().y,
135        box.Max().z,
136        1,
137        1,
138        1
139  };
140
[2105]141#if 0
142  static vector<SimpleRay *> pointerArray;
143
144  if (pointerArray.size()!=rays.size()) {
145        // realloc the pointerarray
146        pointerArray.resize(rays.size());
147  }
148
149  // init pointer array
150  SimpleRay *p = &pointerArray[0];
151  for (i=0; i < rays.size(); i++, p++)
152        pointerArray[i] = p;
153#endif
154 
[1984]155  _SortRays(rays,
156                        0,
[1990]157                        (int)rays.size()-1,
[1984]158                        0,
159                        b
160                        );
[1974]161}
162                                       
163void
164RayCaster::_SortRays(SimpleRayContainer &rays,
165                                         const int l,
166                                         const int r,
[1984]167                                         const int depth,
168                                         float box[12])
[1974]169{
170  // pick-up a pivot
[1984]171  int axis;
172 
173  float maxDiff = -1.0f;
174  // get the largest axis
175  int offset = 0;
[1990]176  int i;
[1984]177
[2105]178  //const int batchsize = 16384;
[2008]179  const int batchsize = 8192;
[2105]180  //const int batchsize = 128;
[2008]181
[2105]182  //if (r - l < 16*batchsize)
183  //    offset = 3;
[2008]184       
[2105]185  //  if (depth%2==0)
[2008]186  //    offset = 3;
[1984]187 
[2002]188  for (i=offset; i < offset + 3; i++) {
189        float diff = box[i + 6] - box[i];
[1984]190        if (diff > maxDiff) {
191          maxDiff = diff;
192          axis = i;
193        }
194  }
195
[2008]196  //  cout<<depth<<" "<<axis<<" "<<l<<" "<<r<<endl;
[1984]197 
[1990]198  i=l;
199  int j=r;
200
[1984]201  float x = (box[axis] + box[axis+6])*0.5f;
202  //  float x = rays[(l+r)/2].GetParam(axis);
[1974]203  do {
[2008]204        while(i<j && rays[i].GetParam(axis) < x)
[1974]205          i++;
[2008]206        while(i<j && x < rays[j].GetParam(axis))
[1974]207          j--;
208       
209        if (i <= j) {
210          swap(rays[i], rays[j]);
211          i++;
212          j--;
213        }
214  } while (i<=j);
[2008]215
[1974]216 
[2008]217  if (l + batchsize < j ) {
[1984]218        // set new max
219        float save = box[axis+6];
220        box[axis+6] = x;
221        _SortRays(rays, l, j, depth+1, box);
222        box[axis+6] = save;
[2008]223  } else {
224        //      for (int k=0; k < 6; k++)
225        //        cout<<k<<" "<<box[k]<<" - "<<box[k+6]<<endl;
[1984]226  }
[1974]227 
[2008]228  if (i + batchsize < r) {
[1984]229        // set new min
230        box[axis] = x;
231    _SortRays(rays, i, r, depth+1, box);
[2008]232  } else {
233        //      for (int k=0; k < 6; k++)
234        //        cout<<k<<" "<<box[k]<<" - "<<box[k+6]<<endl;
[1984]235  }
[2008]236       
[1974]237}
238 
239                                       
[1528]240
[1867]241int
242RayCaster::ProcessRay(const SimpleRay &simpleRay,
243                                          Intersection &hitA,
244                                          Intersection &hitB,
245                                          VssRayContainer &vssRays,
246                                          const AxisAlignedBox3 &box,
247                                          const bool castDoubleRay,
[1996]248                                          const bool pruneInvalidRays)
[1520]249{
[1984]250  int hits = 0;
[1528]251
[2014]252
[1520]253#if DEBUG_RAYCAST
[2013]254  static int id=0;
255  Debug<<"PRA "<<id++<<endl<<flush;
[1520]256#endif
[1528]257       
[1932]258        if (pruneInvalidRays)
259        {
[1952]260          if (!hitA.mObject && !hitB.mObject) {
261                return 0;
262          }
[1520]263        }
[1584]264       
[2070]265        // regardless of the pruneInvalidRays setting reject
266        // rays whic degenerate to a point
[1966]267        if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
[2035]268          return 0;
[1966]269        }
270       
[1952]271        const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
[1932]272        const bool validB = //castDoubleRay &&
[1952]273          ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
[1867]274       
[1952]275       
[1757]276#if DEBUG_RAYCAST
277        Debug<<"PR1"<<flush;
278#endif
[1584]279       
[1867]280        // reset both contributions
[1952]281        if (!validA || !validB) {
[2070]282          if (0 || pruneInvalidRays)
[1952]283                return 0;
[1867]284         
[1952]285          // reset both contributions of this ray
286          hitA.mObject = NULL;
287          hitB.mObject = NULL;
288        }
289       
[1867]290        // 8.11. 2007 JB
291        // degenerate rays checked by geometrical constraint...
292        //      !pruneInvalidRays || (hitA.mObject != hitB.mObject);
[1942]293
[1584]294       
[1757]295#if DEBUG_RAYCAST
296        Debug<<"PR2"<<flush;
297#endif
[1942]298        const bool validSample = true;
299        if (validSample) {
300          Vector3 clipA, clipB;
301          if (!ClipToViewSpaceBox(hitA.mPoint,
302                                                          hitB.mPoint,
303                                                          clipA,
304                                                          clipB))
305                return 0;
[1952]306
[1942]307          if (!pruneInvalidRays || hitA.mObject) {
[2014]308#if DEBUG_RAYCAST
309                Debug<<"PR2a"<<flush;
310#endif
311               
[2012]312                VssRay *vssRay = mVssRayPool.Alloc();
[2014]313
314#if DEBUG_RAYCAST
315                Debug<<"PR2b"<<flush;
316#endif
[2012]317                *vssRay = VssRay(
318                                                 !castDoubleRay ? simpleRay.mOrigin : clipB,
319                                                 hitA.mPoint,
320                                                 hitB.mObject,
321                                                 hitA.mObject,
322                                                 mPreprocessor.mPass,
[2105]323                                                 1.0f //simpleRay.mPdf
[2012]324                                                 );
[2014]325#if DEBUG_RAYCAST
326                Debug<<"PR2c"<<flush;
327#endif
[2021]328               
[1867]329                if (validA)
[2021]330                  vssRay->mFlags |= VssRay::Valid;
331               
[1883]332                vssRay->mDistribution = simpleRay.mDistribution;
[1989]333                vssRay->mGeneratorId = simpleRay.mGeneratorId;
334
[1867]335                vssRays.push_back(vssRay);
336                ++ hits;
337                //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
[1932]338        }
339
[1867]340#if DEBUG_RAYCAST
[1932]341        Debug<<"PR3"<<flush;
[1867]342#endif
343
[2070]344        if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
345        {
346                VssRay *vssRay = mVssRayPool.Alloc();
347
348                *vssRay = VssRay(
349                        clipA,
350                        hitB.mPoint,
351                        hitA.mObject,
352                        hitB.mObject,
353                        mPreprocessor.mPass,
[2105]354                        1.0f //simpleRay.mPdf
[2070]355                        );
356
357                if (validB)
[1771]358                        vssRay->mFlags |= VssRay::Valid;
[1932]359
360                vssRay->mDistribution = simpleRay.mDistribution;
[1989]361                vssRay->mGeneratorId = simpleRay.mGeneratorId;
[1932]362                vssRays.push_back(vssRay);
363                ++ hits;
364                //cout << "vssray 2: " << *vssRay << endl;
[2070]365        }
[1942]366#if DEBUG_RAYCAST
367          Debug<<"PR4"<<flush;
368#endif
[1520]369        }
[1867]370       
[1520]371        return hits;
372}
373
[2076]374
375void
376RayCaster::CastRays(
377                                        SimpleRayContainer &rays,
378                                        VssRayContainer &vssRays,
379                                        const AxisAlignedBox3 &sbox,
380                                        const bool castDoubleRay,
381                                        const bool pruneInvalidRays )
382{
383  SimpleRayContainer::const_iterator rit, rit_end = rays.end();
384 
385  for (rit = rays.begin(); rit != rit_end; ++ rit) {
386        CastRay(
387                        *rit,                           
388                        vssRays,
389                        sbox,
390                        castDoubleRay,
391                        pruneInvalidRays);
392  }
393 
[1583]394}
[2076]395
396}
Note: See TracBrowser for help on using the repository browser.