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

RevLine 
[1520]1#include "RayCaster.h"
2#include "VssRay.h"
3#include "Ray.h"
4#include "Preprocessor.h"
[1942]5#include "ViewCellsManager.h"
[1520]6
7
8namespace GtpVisibilityPreprocessor {
9
10
11#define DEBUG_RAYCAST 0
12
[1942]13#define EXACT_BOX_CLIPPING 0
[1520]14
15RayCaster::RayCaster(const Preprocessor &preprocessor):
16mPreprocessor(preprocessor)
17{
18}
19
20
[1521]21RayCaster::~RayCaster()
22{
23}
24
25
[1545]26VssRay *RayCaster::CastRay(const SimpleRay &simpleRay,
[1932]27                                                   const AxisAlignedBox3 &box)
28                                                   //const bool castDoubleRay)
[1521]29{
[1932]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;
[1521]39}
40
[1942]41bool
42RayCaster::ClipToViewSpaceBox(const Vector3 &origin,
43                                                          const Vector3 &termination,
44                                                          Vector3 &clippedOrigin,
45                                                          Vector3 &clippedTermination)
46{
[1952]47 
[1942]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;
[1521]57
[1942]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
[1528]74/** Checks if ray is valid, (e.g., not in empty view space or outside the view space)
75*/
[1867]76bool
77RayCaster::ValidateRay(const Vector3 &origin,
78                                           const Vector3 &direction,
79                                           const AxisAlignedBox3 &box,
80                                           Intersection &hit)
[1528]81{
[1932]82        if (!hit.mObject) 
83        {
84                // compute intersection with the scene bounding box
[1584]85#if EXACT_BOX_CLIPPING
[1932]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                }
[1584]103#else
[1932]104                hit.mPoint = origin + direction * Magnitude(box.Diagonal());
[1584]105#endif
106        }
[1932]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;
[1528]120}
121
122
[1867]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)
[1520]131{
132        int hits = 0;
[1528]133
[1520]134#if DEBUG_RAYCAST
[1952]135        static int id=0;
136        Debug<<"PRA "<<id++<<endl<<flush;
[1520]137#endif
[1528]138       
[1932]139        if (pruneInvalidRays)
140        {
[1952]141          if (!hitA.mObject && !hitB.mObject) {
142                return 0;
143          }
144          if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
145                return 0;
146          }
[1520]147        }
[1584]148       
[1952]149        // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
150        const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
[1932]151        const bool validB = //castDoubleRay &&
[1952]152          ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
[1867]153       
[1952]154       
[1757]155#if DEBUG_RAYCAST
156        Debug<<"PR1"<<flush;
157#endif
[1584]158       
[1867]159        // reset both contributions
[1952]160        if (!validA || !validB) {
161          if (pruneInvalidRays)
162                return 0;
[1867]163         
[1952]164          // reset both contributions of this ray
165          hitA.mObject = NULL;
166          hitB.mObject = NULL;
167        }
168       
[1867]169        // 8.11. 2007 JB
170        // degenerate rays checked by geometrical constraint...
171        //      !pruneInvalidRays || (hitA.mObject != hitB.mObject);
[1942]172
[1584]173       
[1757]174#if DEBUG_RAYCAST
175        Debug<<"PR2"<<flush;
176#endif
[1942]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;
[1952]185
[1942]186          if (!pruneInvalidRays || hitA.mObject) {
187                VssRay *vssRay = new VssRay(
188                                                                        clipB,
[1867]189                                                                        hitA.mPoint,
190                                                                        hitB.mObject,
191                                                                        hitA.mObject,
192                                                                        mPreprocessor.mPass,
[1932]193                                                                        simpleRay.mPdf);
194
[1867]195                if (validA)
[1932]196                        vssRay->mFlags |= VssRay::Valid;
197
[1883]198                vssRay->mDistribution = simpleRay.mDistribution;
[1867]199                vssRays.push_back(vssRay);
200                ++ hits;
201                //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
[1932]202        }
203
[1867]204#if DEBUG_RAYCAST
[1932]205        Debug<<"PR3"<<flush;
[1867]206#endif
207
[1942]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)
[1771]219                        vssRay->mFlags |= VssRay::Valid;
[1932]220
221                vssRay->mDistribution = simpleRay.mDistribution;
222                vssRays.push_back(vssRay);
223                ++ hits;
224                //cout << "vssray 2: " << *vssRay << endl;
[1942]225                }
226#if DEBUG_RAYCAST
227          Debug<<"PR4"<<flush;
228#endif
[1520]229        }
[1867]230       
[1520]231        return hits;
232}
233
[1583]234}
Note: See TracBrowser for help on using the repository browser.