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

Revision 1974, 5.6 KB checked in by bittner, 18 years ago (diff)

mutation updates, ray sorting, 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        // note: make no sense otherwise
31        const bool castDoubleRay = false;
32        VssRayContainer rays;
33        CastRay(simpleRay, rays, box, castDoubleRay);
34   
35        if (!rays.empty())
36                return rays.back();
37        else
38                return NULL;
39}
40
41bool
42RayCaster::ClipToViewSpaceBox(const Vector3 &origin,
43                                                          const Vector3 &termination,
44                                                          Vector3 &clippedOrigin,
45                                                          Vector3 &clippedTermination)
46{
47 
48  Ray ray(origin, termination - origin, Ray::LINE_SEGMENT);     
49  ray.Precompute();
50 
51  float tmin, tmax;
52  if ((!mPreprocessor.mViewCellsManager->
53           GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax)) ||
54          tmin>=tmax
55          )
56        return false;
57
58  if (tmin >= 1.0f || tmax <=0.0f)
59        return false;
60 
61  if (tmin > 0.0f)
62        clippedOrigin = ray.Extrap(tmin);
63  else
64        clippedOrigin = origin;
65 
66  if (tmax < 1.0f)
67        clippedTermination = ray.Extrap(tmax);
68  else
69        clippedTermination = termination;
70 
71  return true;
72}
73
74/** Checks if ray is valid, (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                {
111                        if (DotProd(hit.mNormal, direction) >= -Limits::Small)
112                        {       
113                                hit.mObject = NULL;
114                                return false;
115                        }
116                }
117        }
118
119        return true;
120}
121
122void
123RayCaster::SortRays(SimpleRayContainer &rays)
124{
125  _SortRays(rays, 0, rays.size()-1, 0);
126}
127                                       
128void
129RayCaster::_SortRays(SimpleRayContainer &rays,
130                                         const int l,
131                                         const int r,
132                                         const int axis)
133{
134  // pick-up a pivot
135  int     i=l,j=r;
136  float x = rays[(l+r)/2].GetParam(axis);
137  do {
138        while(rays[i].GetParam(axis) < x)
139          i++;
140        while(x < rays[j].GetParam(axis))
141          j--;
142       
143        if (i <= j) {
144          swap(rays[i], rays[j]);
145          i++;
146          j--;
147        }
148  } while (i<=j);
149 
150  if (l + 16 < j )
151        _SortRays(rays, l, j, (axis+1)%6);
152 
153  if (i + 16 < r)
154    _SortRays(rays, i, r, (axis+1)%6);
155}
156 
157                                       
158
159int
160RayCaster::ProcessRay(const SimpleRay &simpleRay,
161                                          Intersection &hitA,
162                                          Intersection &hitB,
163                                          VssRayContainer &vssRays,
164                                          const AxisAlignedBox3 &box,
165                                          const bool castDoubleRay,
166                                          const bool pruneInvalidRays)
167{
168        int hits = 0;
169
170#if DEBUG_RAYCAST
171        static int id=0;
172        Debug<<"PRA "<<id++<<endl<<flush;
173#endif
174       
175        if (pruneInvalidRays)
176        {
177          if (!hitA.mObject && !hitB.mObject) {
178                return 0;
179          }
180        }
181
182       
183        // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
184        if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
185          return 0;
186        }
187       
188       
189        const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
190        const bool validB = //castDoubleRay &&
191          ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
192       
193       
194#if DEBUG_RAYCAST
195        Debug<<"PR1"<<flush;
196#endif
197       
198        // reset both contributions
199        if (!validA || !validB) {
200          if (pruneInvalidRays)
201                return 0;
202         
203          // reset both contributions of this ray
204          hitA.mObject = NULL;
205          hitB.mObject = NULL;
206        }
207       
208        // 8.11. 2007 JB
209        // degenerate rays checked by geometrical constraint...
210        //      !pruneInvalidRays || (hitA.mObject != hitB.mObject);
211
212       
213#if DEBUG_RAYCAST
214        Debug<<"PR2"<<flush;
215#endif
216        const bool validSample = true;
217        if (validSample) {
218          Vector3 clipA, clipB;
219          if (!ClipToViewSpaceBox(hitA.mPoint,
220                                                          hitB.mPoint,
221                                                          clipA,
222                                                          clipB))
223                return 0;
224
225          if (!pruneInvalidRays || hitA.mObject) {
226                VssRay *vssRay = new VssRay(
227                                                                        clipB,
228                                                                        hitA.mPoint,
229                                                                        hitB.mObject,
230                                                                        hitA.mObject,
231                                                                        mPreprocessor.mPass,
232                                                                        simpleRay.mPdf);
233
234                if (validA)
235                        vssRay->mFlags |= VssRay::Valid;
236
237                vssRay->mDistribution = simpleRay.mDistribution;
238                vssRays.push_back(vssRay);
239                ++ hits;
240                //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
241        }
242
243#if DEBUG_RAYCAST
244        Debug<<"PR3"<<flush;
245#endif
246
247          if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
248                {
249                  VssRay *vssRay = new VssRay(
250                                                                          clipA,
251                                                                          hitB.mPoint,
252                                                                          hitA.mObject,
253                                                                          hitB.mObject,
254                                                                          mPreprocessor.mPass,
255                                                                          simpleRay.mPdf
256                                                                          );
257                  if (validB)
258                        vssRay->mFlags |= VssRay::Valid;
259
260                vssRay->mDistribution = simpleRay.mDistribution;
261                vssRays.push_back(vssRay);
262                ++ hits;
263                //cout << "vssray 2: " << *vssRay << endl;
264                }
265#if DEBUG_RAYCAST
266          Debug<<"PR4"<<flush;
267#endif
268        }
269       
270        return hits;
271}
272
273}
Note: See TracBrowser for help on using the repository browser.