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

Revision 1990, 6.6 KB checked in by mattausch, 17 years ago (diff)
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 keepOrigin)
29{
30        // note: make no sense otherwise
31        const bool castDoubleRay = false;
32       
33        VssRayContainer rays;
34        CastRay(simpleRay, rays, box, castDoubleRay, true, keepOrigin);
35   
36        if (!rays.empty())
37                return rays.back();
38        else
39                return NULL;
40}
41
42
43bool
44RayCaster::ClipToViewSpaceBox(const Vector3 &origin,
45                                                          const Vector3 &termination,
46                                                          Vector3 &clippedOrigin,
47                                                          Vector3 &clippedTermination)
48{
49 
50  Ray ray(origin, termination - origin, Ray::LINE_SEGMENT);     
51  ray.Precompute();
52 
53  float tmin, tmax;
54  if ((!mPreprocessor.mViewCellsManager->
55           GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax)) ||
56          tmin>=tmax
57          )
58        return false;
59
60  if (tmin >= 1.0f || tmax <=0.0f)
61        return false;
62 
63  if (tmin > 0.0f)
64        clippedOrigin = ray.Extrap(tmin);
65  else
66        clippedOrigin = origin;
67 
68  if (tmax < 1.0f)
69        clippedTermination = ray.Extrap(tmax);
70  else
71        clippedTermination = termination;
72 
73  return true;
74}
75
76/** Checks if ray is valid, (e.g., not in empty view space or outside the view space)
77*/
78bool
79RayCaster::ValidateRay(const Vector3 &origin,
80                                           const Vector3 &direction,
81                                           const AxisAlignedBox3 &box,
82                                           Intersection &hit)
83{
84        if (!hit.mObject) 
85        {
86                // compute intersection with the scene bounding box
87#if EXACT_BOX_CLIPPING
88                static Ray ray;
89                mPreprocessor.SetupRay(ray, origin, direction);
90
91                float tmin, tmax;
92                if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
93                {
94                        hit.mPoint = ray.Extrap(tmax);
95                }
96                else
97                {
98                        // cout<<" invalid hp "<<tmin<<" "<<tmax<<endl;
99                        // cout<<" box "<<box<<endl;
100                        // cout<<" origin "<<origin<<endl;
101                        // cout<<" direction "<<direction<<endl;
102                        // cout<< "inv dir"<<ray.GetInvDir()<<endl;
103                        return false;
104                }
105#else
106                hit.mPoint = origin + direction * Magnitude(box.Diagonal());
107#endif
108        }
109        else
110        {
111                if (mPreprocessor.mDetectEmptyViewSpace)
112                {
113                        if (DotProd(hit.mNormal, direction) >= -Limits::Small)
114                        {       
115                                hit.mObject = NULL;
116                                return false;
117                        }
118                }
119        }
120
121        return true;
122}
123
124void
125RayCaster::SortRays(SimpleRayContainer &rays)
126{
127  AxisAlignedBox3 box =
128        mPreprocessor.mViewCellsManager->GetViewSpaceBox();
129       
130  float b[12]={
131        box.Min().x,
132        box.Min().y,
133        box.Min().z,
134        -1,
135        -1,
136        -1,
137        box.Max().x,
138        box.Max().y,
139        box.Max().z,
140        1,
141        1,
142        1
143  };
144
145  _SortRays(rays,
146                        0,
147                        (int)rays.size()-1,
148                        0,
149                        b
150                        );
151}
152                                       
153void
154RayCaster::_SortRays(SimpleRayContainer &rays,
155                                         const int l,
156                                         const int r,
157                                         const int depth,
158                                         float box[12])
159{
160  // pick-up a pivot
161  int axis;
162 
163  float maxDiff = -1.0f;
164  // get the largest axis
165  int offset = 0;
166  int i;
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<<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(rays[i].GetParam(axis) < x && i<=j)
188          i++;
189        while(x < rays[j].GetParam(axis) && i<=j)
190          j--;
191       
192        if (i <= j) {
193          swap(rays[i], rays[j]);
194          i++;
195          j--;
196        }
197  } while (i<=j);
198 
199  if (l + 64 < j ) {
200        // set new max
201        float save = box[axis+6];
202        box[axis+6] = x;
203        _SortRays(rays, l, j, depth+1, box);
204        box[axis+6] = save;
205  }
206 
207  if (i + 64 < r) {
208        // set new min
209        box[axis] = x;
210    _SortRays(rays, i, r, depth+1, box);
211  }
212}
213 
214                                       
215
216int
217RayCaster::ProcessRay(const SimpleRay &simpleRay,
218                                          Intersection &hitA,
219                                          Intersection &hitB,
220                                          VssRayContainer &vssRays,
221                                          const AxisAlignedBox3 &box,
222                                          const bool castDoubleRay,
223                                          const bool pruneInvalidRays,
224                                          const bool keepOrigin)
225{
226  int hits = 0;
227
228#if DEBUG_RAYCAST
229        static int id=0;
230        Debug<<"PRA "<<id++<<endl<<flush;
231#endif
232       
233        if (pruneInvalidRays)
234        {
235          if (!hitA.mObject && !hitB.mObject) {
236                return 0;
237          }
238        }
239
240       
241        // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
242        if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
243                return 0;
244        }
245       
246       
247        const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
248        const bool validB = //castDoubleRay &&
249          ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
250       
251       
252#if DEBUG_RAYCAST
253        Debug<<"PR1"<<flush;
254#endif
255       
256        // reset both contributions
257        if (!validA || !validB) {
258          if (pruneInvalidRays)
259                return 0;
260         
261          // reset both contributions of this ray
262          hitA.mObject = NULL;
263          hitB.mObject = NULL;
264        }
265       
266        // 8.11. 2007 JB
267        // degenerate rays checked by geometrical constraint...
268        //      !pruneInvalidRays || (hitA.mObject != hitB.mObject);
269
270       
271#if DEBUG_RAYCAST
272        Debug<<"PR2"<<flush;
273#endif
274        const bool validSample = true;
275        if (validSample) {
276          Vector3 clipA, clipB;
277          if (!ClipToViewSpaceBox(hitA.mPoint,
278                                                          hitB.mPoint,
279                                                          clipA,
280                                                          clipB))
281                return 0;
282
283          if (!pruneInvalidRays || hitA.mObject) {
284                VssRay *vssRay = new VssRay(
285                                                                        keepOrigin ? simpleRay.mOrigin : clipB,
286                                                                        hitA.mPoint,
287                                                                        hitB.mObject,
288                                                                        hitA.mObject,
289                                                                        mPreprocessor.mPass,
290                                                                        simpleRay.mPdf
291                                                                        );
292
293                if (validA)
294                        vssRay->mFlags |= VssRay::Valid;
295
296                vssRay->mDistribution = simpleRay.mDistribution;
297                vssRay->mGeneratorId = simpleRay.mGeneratorId;
298
299                vssRays.push_back(vssRay);
300                ++ hits;
301                //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
302        }
303
304#if DEBUG_RAYCAST
305        Debug<<"PR3"<<flush;
306#endif
307
308          if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
309                {
310                  VssRay *vssRay = new VssRay(
311                                                                          keepOrigin ? simpleRay.mOrigin : clipA,
312                                                                          hitB.mPoint,
313                                                                          hitA.mObject,
314                                                                          hitB.mObject,
315                                                                          mPreprocessor.mPass,
316                                                                          simpleRay.mPdf
317                                                                          );
318                  if (validB)
319                        vssRay->mFlags |= VssRay::Valid;
320
321                vssRay->mDistribution = simpleRay.mDistribution;
322                vssRay->mGeneratorId = simpleRay.mGeneratorId;
323                vssRays.push_back(vssRay);
324                ++ hits;
325                //cout << "vssray 2: " << *vssRay << endl;
326                }
327#if DEBUG_RAYCAST
328          Debug<<"PR4"<<flush;
329#endif
330        }
331       
332        return hits;
333}
334
335}
Note: See TracBrowser for help on using the repository browser.