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

Revision 1952, 5.0 KB checked in by bittner, 17 years ago (diff)

mutation strategy

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
122
123int
124RayCaster::ProcessRay(const SimpleRay &simpleRay,
125                                          Intersection &hitA,
126                                          Intersection &hitB,
127                                          VssRayContainer &vssRays,
128                                          const AxisAlignedBox3 &box,
129                                          const bool castDoubleRay,
130                                          const bool pruneInvalidRays)
131{
132        int hits = 0;
133
134#if DEBUG_RAYCAST
135        static int id=0;
136        Debug<<"PRA "<<id++<<endl<<flush;
137#endif
138       
139        if (pruneInvalidRays)
140        {
141          if (!hitA.mObject && !hitB.mObject) {
142                return 0;
143          }
144          if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
145                return 0;
146          }
147        }
148       
149        // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
150        const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
151        const bool validB = //castDoubleRay &&
152          ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
153       
154       
155#if DEBUG_RAYCAST
156        Debug<<"PR1"<<flush;
157#endif
158       
159        // reset both contributions
160        if (!validA || !validB) {
161          if (pruneInvalidRays)
162                return 0;
163         
164          // reset both contributions of this ray
165          hitA.mObject = NULL;
166          hitB.mObject = NULL;
167        }
168       
169        // 8.11. 2007 JB
170        // degenerate rays checked by geometrical constraint...
171        //      !pruneInvalidRays || (hitA.mObject != hitB.mObject);
172
173       
174#if DEBUG_RAYCAST
175        Debug<<"PR2"<<flush;
176#endif
177        const bool validSample = true;
178        if (validSample) {
179          Vector3 clipA, clipB;
180          if (!ClipToViewSpaceBox(hitA.mPoint,
181                                                          hitB.mPoint,
182                                                          clipA,
183                                                          clipB))
184                return 0;
185
186          if (!pruneInvalidRays || hitA.mObject) {
187                VssRay *vssRay = new VssRay(
188                                                                        clipB,
189                                                                        hitA.mPoint,
190                                                                        hitB.mObject,
191                                                                        hitA.mObject,
192                                                                        mPreprocessor.mPass,
193                                                                        simpleRay.mPdf);
194
195                if (validA)
196                        vssRay->mFlags |= VssRay::Valid;
197
198                vssRay->mDistribution = simpleRay.mDistribution;
199                vssRays.push_back(vssRay);
200                ++ hits;
201                //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
202        }
203
204#if DEBUG_RAYCAST
205        Debug<<"PR3"<<flush;
206#endif
207
208          if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
209                {
210                  VssRay *vssRay = new VssRay(
211                                                                          clipA,
212                                                                          hitB.mPoint,
213                                                                          hitA.mObject,
214                                                                          hitB.mObject,
215                                                                          mPreprocessor.mPass,
216                                                                          simpleRay.mPdf
217                                                                          );
218                  if (validB)
219                        vssRay->mFlags |= VssRay::Valid;
220
221                vssRay->mDistribution = simpleRay.mDistribution;
222                vssRays.push_back(vssRay);
223                ++ hits;
224                //cout << "vssray 2: " << *vssRay << endl;
225                }
226#if DEBUG_RAYCAST
227          Debug<<"PR4"<<flush;
228#endif
229        }
230       
231        return hits;
232}
233
234}
Note: See TracBrowser for help on using the repository browser.