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

Revision 1551, 3.1 KB checked in by mattausch, 18 years ago (diff)

updated view cells loading. probably no optimal for performance

RevLine 
[1520]1#include "RayCaster.h"
2#include "VssRay.h"
3#include "Ray.h"
4#include "Preprocessor.h"
5
6
7namespace GtpVisibilityPreprocessor {
8
9
10#define DEBUG_RAYCAST 0
11
12
13RayCaster::RayCaster(const Preprocessor &preprocessor):
14mPreprocessor(preprocessor)
15{
16}
17
18
[1521]19RayCaster::~RayCaster()
20{
21}
22
23
[1545]24VssRay *RayCaster::CastRay(const SimpleRay &simpleRay,
25                                                   const AxisAlignedBox3 &box,
26                                                   const bool castDoubleRay)
[1521]27{
28        VssRayContainer rays;
[1545]29        CastRay(simpleRay, rays, box, castDoubleRay);
[1521]30
31        if (!rays.empty())
32                return rays.back();
[1522]33        else
34                return NULL;
[1521]35}
36
37
[1528]38/** Checks if ray is valid, (e.g., not in empty view space or outside the view space)
39*/
40bool RayCaster::ValidateRay(const Vector3 &origin,
41                                                        const Vector3 &direction,
42                                                        const AxisAlignedBox3 &box,
43                                                        Intersection &hit)
44{
45        if (!hit.mObject)
46        {
47                // compute intersection with the scene bounding box
48                static Ray ray;
49                mPreprocessor.SetupRay(ray, origin, direction);
50
51                float tmin, tmax;
52                if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
53                {
54                        hit.mPoint = ray.Extrap(tmax);
55                }
56                else
57                {
58                        return false;
59                }               
60        }
61        else if (mPreprocessor.mDetectEmptyViewSpace)
62        {
63                if (DotProd(hit.mNormal, direction) >= 0)
64                {       
65                        hit.mObject = NULL;
66                        return false;
67                }
68        }
69
70        return true;
71}
72
73
74int RayCaster::ProcessRay(const SimpleRay &simpleRay,
75                                                  Intersection &hitA,
76                                                  Intersection &hitB,
[1520]77                                                  VssRayContainer &vssRays,
[1528]78                                                  const AxisAlignedBox3 &box,
79                                                  const bool castDoubleRay,
80                                                  const bool pruneInvalidRays)
[1520]81{
82        int hits = 0;
[1528]83
[1520]84#if DEBUG_RAYCAST
85        Debug<<"PR ";
86#endif
[1528]87       
88        if (pruneInvalidRays)
89        {
90                if (!hitA.mObject && !hitB.mObject)
91                {
92                        return 0;
93                }
94       
95                // inside test for bounding box
96                // enlarge box slightly so the view point fits for sure
97                AxisAlignedBox3 sbox = box;
98                sbox.Enlarge(Vector3(-Limits::Small));
[1520]99
[1528]100                if (!sbox.IsInside(simpleRay.mOrigin))
101                {
[1520]102                        return 0;
[1528]103                }
[1520]104        }
[1551]105
[1528]106        const bool validA =
107                ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
108        if (!validA && pruneInvalidRays)
109        {
110                return 0;
111        }
[1520]112
[1528]113        const bool validB = castDoubleRay &&
114                ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
[1520]115
[1528]116        if (!validB && pruneInvalidRays)
117        {
118                return 0;
[1520]119        }
120
[1528]121        const bool validSample = !pruneInvalidRays || (hitA.mObject != hitB.mObject);
122       
123        if (validSample)
124        {
125                if (!pruneInvalidRays || hitA.mObject)
126                {
127                        VssRay *vssRay = new VssRay(
128                                hitB.mPoint,
129                                hitA.mPoint,
130                                hitB.mObject,
131                                hitA.mObject,
[1520]132                                mPreprocessor.mPass,
[1528]133                                simpleRay.mPdf
[1520]134                                );
135                       
136                        vssRays.push_back(vssRay);
[1528]137                        ++ hits;
[1551]138                        //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
[1520]139                }
140
[1528]141                if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
142                {
143                        VssRay *vssRay = new VssRay(
144                                hitA.mPoint,
145                                hitB.mPoint,
146                                hitA.mObject,
147                                hitB.mObject,
[1520]148                                mPreprocessor.mPass,
[1528]149                                simpleRay.mPdf
[1520]150                                );
151                       
152                        vssRays.push_back(vssRay);
[1528]153                        ++ hits;
[1551]154                        //cout << "vssray 2: " << *vssRay << endl;
[1520]155                }
156        }
157
158        return hits;
159}
160
161}
Note: See TracBrowser for help on using the repository browser.