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

Line 
1#include "RayCaster.h"
2#include "VssRay.h"
3#include "Ray.h"
4#include "Preprocessor.h"
5#include "ViewCellsManager.h"
6
7
8namespace GtpVisibilityPreprocessor {
9
10
11#define DEBUG_RAYCAST 0
12
13#define EXACT_BOX_CLIPPING 0
14
15RayCaster::RayCaster(const Preprocessor &preprocessor):
16mPreprocessor(preprocessor)
17{
18}
19
20
21RayCaster::~RayCaster()
22{
23}
24
25
26VssRay *RayCaster::CastRay(const SimpleRay &simpleRay,
27                                                   const AxisAlignedBox3 &box,
28                                                   const bool castDoubleRay)
29{
30        VssRayContainer rays;
31        CastRay(simpleRay, rays, box, castDoubleRay, true);
32   
33        if (!rays.empty())
34                return rays.back();
35        else
36                return NULL;
37}
38
39
40bool
41RayCaster::ClipToViewSpaceBox(const Vector3 &origin,
42                                                          const Vector3 &termination,
43                                                          Vector3 &clippedOrigin,
44                                                          Vector3 &clippedTermination)
45{
46 
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;
56
57  if (tmin >= 1.0f || tmax <= 0.0f)
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
73/** Checks if ray is valid
74        (e.g., not in empty view space or outside the view space)
75*/
76bool
77RayCaster::ValidateRay(const Vector3 &origin,
78                                           const Vector3 &direction,
79                                           const AxisAlignedBox3 &box,
80                                           Intersection &hit)
81{
82        if (!hit.mObject) 
83        {
84                // compute intersection with the scene bounding box
85#if EXACT_BOX_CLIPPING
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                }
103#else
104                hit.mPoint = origin + direction * Magnitude(box.Diagonal());
105#endif
106        }
107        else
108        {
109          if (mPreprocessor.mDetectEmptyViewSpace)  {
110                if (DotProd(hit.mNormal, direction) >= -Limits::Small)  {       
111                  hit.mObject = NULL;
112                  return false;
113                }
114          }
115        }
116       
117        return true;
118}
119
120void
121RayCaster::SortRays(SimpleRayContainer &rays)
122{
123  AxisAlignedBox3 box =
124        mPreprocessor.mViewCellsManager->GetViewSpaceBox();
125 
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
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 
155  _SortRays(rays,
156                        0,
157                        (int)rays.size()-1,
158                        0,
159                        b
160                        );
161}
162                                       
163void
164RayCaster::_SortRays(SimpleRayContainer &rays,
165                                         const int l,
166                                         const int r,
167                                         const int depth,
168                                         float box[12])
169{
170  // pick-up a pivot
171  int axis;
172 
173  float maxDiff = -1.0f;
174  // get the largest axis
175  int offset = 0;
176  int i;
177
178  //const int batchsize = 16384;
179  const int batchsize = 8192;
180  //const int batchsize = 128;
181
182  //if (r - l < 16*batchsize)
183  //    offset = 3;
184       
185  //  if (depth%2==0)
186  //    offset = 3;
187 
188  for (i=offset; i < offset + 3; i++) {
189        float diff = box[i + 6] - box[i];
190        if (diff > maxDiff) {
191          maxDiff = diff;
192          axis = i;
193        }
194  }
195
196  //  cout<<depth<<" "<<axis<<" "<<l<<" "<<r<<endl;
197 
198  i=l;
199  int j=r;
200
201  float x = (box[axis] + box[axis+6])*0.5f;
202  //  float x = rays[(l+r)/2].GetParam(axis);
203  do {
204        while(i<j && rays[i].GetParam(axis) < x)
205          i++;
206        while(i<j && x < rays[j].GetParam(axis))
207          j--;
208       
209        if (i <= j) {
210          swap(rays[i], rays[j]);
211          i++;
212          j--;
213        }
214  } while (i<=j);
215
216 
217  if (l + batchsize < j ) {
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;
223  } else {
224        //      for (int k=0; k < 6; k++)
225        //        cout<<k<<" "<<box[k]<<" - "<<box[k+6]<<endl;
226  }
227 
228  if (i + batchsize < r) {
229        // set new min
230        box[axis] = x;
231    _SortRays(rays, i, r, depth+1, box);
232  } else {
233        //      for (int k=0; k < 6; k++)
234        //        cout<<k<<" "<<box[k]<<" - "<<box[k+6]<<endl;
235  }
236       
237}
238 
239                                       
240
241int
242RayCaster::ProcessRay(const SimpleRay &simpleRay,
243                                          Intersection &hitA,
244                                          Intersection &hitB,
245                                          VssRayContainer &vssRays,
246                                          const AxisAlignedBox3 &box,
247                                          const bool castDoubleRay,
248                                          const bool pruneInvalidRays)
249{
250  int hits = 0;
251
252
253#if DEBUG_RAYCAST
254  static int id=0;
255  Debug<<"PRA "<<id++<<endl<<flush;
256#endif
257       
258        if (pruneInvalidRays)
259        {
260          if (!hitA.mObject && !hitB.mObject) {
261                return 0;
262          }
263        }
264       
265        // regardless of the pruneInvalidRays setting reject
266        // rays whic degenerate to a point
267        if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
268          return 0;
269        }
270       
271        const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
272        const bool validB = //castDoubleRay &&
273          ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
274       
275       
276#if DEBUG_RAYCAST
277        Debug<<"PR1"<<flush;
278#endif
279       
280        // reset both contributions
281        if (!validA || !validB) {
282          if (0 || pruneInvalidRays)
283                return 0;
284         
285          // reset both contributions of this ray
286          hitA.mObject = NULL;
287          hitB.mObject = NULL;
288        }
289       
290        // 8.11. 2007 JB
291        // degenerate rays checked by geometrical constraint...
292        //      !pruneInvalidRays || (hitA.mObject != hitB.mObject);
293
294       
295#if DEBUG_RAYCAST
296        Debug<<"PR2"<<flush;
297#endif
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;
306
307          if (!pruneInvalidRays || hitA.mObject) {
308#if DEBUG_RAYCAST
309                Debug<<"PR2a"<<flush;
310#endif
311               
312                VssRay *vssRay = mVssRayPool.Alloc();
313
314#if DEBUG_RAYCAST
315                Debug<<"PR2b"<<flush;
316#endif
317                *vssRay = VssRay(
318                                                 !castDoubleRay ? simpleRay.mOrigin : clipB,
319                                                 hitA.mPoint,
320                                                 hitB.mObject,
321                                                 hitA.mObject,
322                                                 mPreprocessor.mPass,
323                                                 1.0f //simpleRay.mPdf
324                                                 );
325#if DEBUG_RAYCAST
326                Debug<<"PR2c"<<flush;
327#endif
328               
329                if (validA)
330                  vssRay->mFlags |= VssRay::Valid;
331               
332                vssRay->mDistribution = simpleRay.mDistribution;
333                vssRay->mGeneratorId = simpleRay.mGeneratorId;
334
335                vssRays.push_back(vssRay);
336                ++ hits;
337                //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
338        }
339
340#if DEBUG_RAYCAST
341        Debug<<"PR3"<<flush;
342#endif
343
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,
354                        1.0f //simpleRay.mPdf
355                        );
356
357                if (validB)
358                        vssRay->mFlags |= VssRay::Valid;
359
360                vssRay->mDistribution = simpleRay.mDistribution;
361                vssRay->mGeneratorId = simpleRay.mGeneratorId;
362                vssRays.push_back(vssRay);
363                ++ hits;
364                //cout << "vssray 2: " << *vssRay << endl;
365        }
366#if DEBUG_RAYCAST
367          Debug<<"PR4"<<flush;
368#endif
369        }
370       
371        return hits;
372}
373
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 
394}
395
396}
Note: See TracBrowser for help on using the repository browser.