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

Revision 2035, 7.0 KB checked in by bittner, 17 years ago (diff)

merge

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, (e.g., not in empty view space or outside the view space)
74*/
75bool
76RayCaster::ValidateRay(const Vector3 &origin,
77                                           const Vector3 &direction,
78                                           const AxisAlignedBox3 &box,
79                                           Intersection &hit)
80{
81        if (!hit.mObject) 
82        {
83                // compute intersection with the scene bounding box
84#if EXACT_BOX_CLIPPING
85                static Ray ray;
86                mPreprocessor.SetupRay(ray, origin, direction);
87
88                float tmin, tmax;
89                if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
90                {
91                        hit.mPoint = ray.Extrap(tmax);
92                }
93                else
94                {
95                        // cout<<" invalid hp "<<tmin<<" "<<tmax<<endl;
96                        // cout<<" box "<<box<<endl;
97                        // cout<<" origin "<<origin<<endl;
98                        // cout<<" direction "<<direction<<endl;
99                        // cout<< "inv dir"<<ray.GetInvDir()<<endl;
100                        return false;
101                }
102#else
103                hit.mPoint = origin + direction * Magnitude(box.Diagonal());
104#endif
105        }
106        else
107        {
108          if (mPreprocessor.mDetectEmptyViewSpace)  {
109                if (DotProd(hit.mNormal, direction) >= -Limits::Small)  {       
110                  hit.mObject = NULL;
111                  return false;
112                }
113          }
114        }
115       
116        return true;
117}
118
119void
120RayCaster::SortRays(SimpleRayContainer &rays)
121{
122  AxisAlignedBox3 box =
123        mPreprocessor.mViewCellsManager->GetViewSpaceBox();
124       
125  float b[12]={
126        box.Min().x,
127        box.Min().y,
128        box.Min().z,
129        -1,
130        -1,
131        -1,
132        box.Max().x,
133        box.Max().y,
134        box.Max().z,
135        1,
136        1,
137        1
138  };
139
140  _SortRays(rays,
141                        0,
142                        (int)rays.size()-1,
143                        0,
144                        b
145                        );
146}
147                                       
148void
149RayCaster::_SortRays(SimpleRayContainer &rays,
150                                         const int l,
151                                         const int r,
152                                         const int depth,
153                                         float box[12])
154{
155  // pick-up a pivot
156  int axis;
157 
158  float maxDiff = -1.0f;
159  // get the largest axis
160  int offset = 0;
161  int i;
162
163  const int batchsize = 8192;
164
165  //  if (r - l < 32*batchsize)
166  //    offset = 3;
167       
168  //  if (depth%5==0)
169  //    offset = 3;
170 
171  for (i=offset; i < offset + 3; i++) {
172        float diff = box[i + 6] - box[i];
173        if (diff > maxDiff) {
174          maxDiff = diff;
175          axis = i;
176        }
177  }
178
179  //  cout<<depth<<" "<<axis<<" "<<l<<" "<<r<<endl;
180 
181  i=l;
182  int j=r;
183
184  float x = (box[axis] + box[axis+6])*0.5f;
185  //  float x = rays[(l+r)/2].GetParam(axis);
186  do {
187        while(i<j && rays[i].GetParam(axis) < x)
188          i++;
189        while(i<j && x < rays[j].GetParam(axis))
190          j--;
191       
192        if (i <= j) {
193          swap(rays[i], rays[j]);
194          i++;
195          j--;
196        }
197  } while (i<=j);
198
199 
200  if (l + batchsize < j ) {
201        // set new max
202        float save = box[axis+6];
203        box[axis+6] = x;
204        _SortRays(rays, l, j, depth+1, box);
205        box[axis+6] = save;
206  } else {
207        //      for (int k=0; k < 6; k++)
208        //        cout<<k<<" "<<box[k]<<" - "<<box[k+6]<<endl;
209  }
210 
211  if (i + batchsize < r) {
212        // set new min
213        box[axis] = x;
214    _SortRays(rays, i, r, depth+1, box);
215  } else {
216        //      for (int k=0; k < 6; k++)
217        //        cout<<k<<" "<<box[k]<<" - "<<box[k+6]<<endl;
218  }
219       
220}
221 
222                                       
223
224int
225RayCaster::ProcessRay(const SimpleRay &simpleRay,
226                                          Intersection &hitA,
227                                          Intersection &hitB,
228                                          VssRayContainer &vssRays,
229                                          const AxisAlignedBox3 &box,
230                                          const bool castDoubleRay,
231                                          const bool pruneInvalidRays)
232{
233  int hits = 0;
234
235
236#if DEBUG_RAYCAST
237  static int id=0;
238  Debug<<"PRA "<<id++<<endl<<flush;
239#endif
240       
241        if (pruneInvalidRays)
242        {
243          if (!hitA.mObject && !hitB.mObject) {
244                return 0;
245          }
246        }
247
248       
249        // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
250        if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
251          return 0;
252        }
253       
254       
255        const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
256        const bool validB = //castDoubleRay &&
257          ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
258       
259       
260#if DEBUG_RAYCAST
261        Debug<<"PR1"<<flush;
262#endif
263       
264        // reset both contributions
265        if (!validA || !validB) {
266          if (pruneInvalidRays)
267                return 0;
268         
269          // reset both contributions of this ray
270          hitA.mObject = NULL;
271          hitB.mObject = NULL;
272        }
273       
274        // 8.11. 2007 JB
275        // degenerate rays checked by geometrical constraint...
276        //      !pruneInvalidRays || (hitA.mObject != hitB.mObject);
277
278       
279#if DEBUG_RAYCAST
280        Debug<<"PR2"<<flush;
281#endif
282        const bool validSample = true;
283        if (validSample) {
284          Vector3 clipA, clipB;
285          if (!ClipToViewSpaceBox(hitA.mPoint,
286                                                          hitB.mPoint,
287                                                          clipA,
288                                                          clipB))
289                return 0;
290
291          if (!pruneInvalidRays || hitA.mObject) {
292#if DEBUG_RAYCAST
293                Debug<<"PR2a"<<flush;
294#endif
295               
296                VssRay *vssRay = mVssRayPool.Alloc();
297
298#if DEBUG_RAYCAST
299                Debug<<"PR2b"<<flush;
300#endif
301                *vssRay = VssRay(
302                                                 !castDoubleRay ? simpleRay.mOrigin : clipB,
303                                                 hitA.mPoint,
304                                                 hitB.mObject,
305                                                 hitA.mObject,
306                                                 mPreprocessor.mPass,
307                                                 simpleRay.mPdf
308                                                 );
309#if DEBUG_RAYCAST
310                Debug<<"PR2c"<<flush;
311#endif
312               
313                if (validA)
314                  vssRay->mFlags |= VssRay::Valid;
315
316               
317                vssRay->mDistribution = simpleRay.mDistribution;
318                vssRay->mGeneratorId = simpleRay.mGeneratorId;
319
320                vssRays.push_back(vssRay);
321                ++ hits;
322                //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
323        }
324
325#if DEBUG_RAYCAST
326        Debug<<"PR3"<<flush;
327#endif
328
329          if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
330                {
331                  VssRay *vssRay = mVssRayPool.Alloc();
332                               
333                  *vssRay = VssRay(
334                                                   clipA,
335                                                   hitB.mPoint,
336                                                   hitA.mObject,
337                                                   hitB.mObject,
338                                                   mPreprocessor.mPass,
339                                                   simpleRay.mPdf
340                                                   );
341                  if (validB)
342                        vssRay->mFlags |= VssRay::Valid;
343
344                 
345                vssRay->mDistribution = simpleRay.mDistribution;
346                vssRay->mGeneratorId = simpleRay.mGeneratorId;
347                vssRays.push_back(vssRay);
348                ++ hits;
349                //cout << "vssray 2: " << *vssRay << endl;
350                }
351#if DEBUG_RAYCAST
352          Debug<<"PR4"<<flush;
353#endif
354        }
355       
356        return hits;
357}
358
359}
Note: See TracBrowser for help on using the repository browser.