1 | #include "RayCaster.h"
|
---|
2 | #include "VssRay.h"
|
---|
3 | #include "Ray.h"
|
---|
4 | #include "Preprocessor.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace GtpVisibilityPreprocessor {
|
---|
8 |
|
---|
9 |
|
---|
10 | #define DEBUG_RAYCAST 0
|
---|
11 |
|
---|
12 |
|
---|
13 | RayCaster::RayCaster(const Preprocessor &preprocessor):
|
---|
14 | mPreprocessor(preprocessor)
|
---|
15 | {
|
---|
16 | }
|
---|
17 |
|
---|
18 |
|
---|
19 | RayCaster::~RayCaster()
|
---|
20 | {
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | VssRay *RayCaster::CastRay(const SimpleRay &simpleRay,
|
---|
25 | const AxisAlignedBox3 &box,
|
---|
26 | const bool castDoubleRay)
|
---|
27 | {
|
---|
28 | VssRayContainer rays;
|
---|
29 | CastRay(simpleRay, rays, box, castDoubleRay);
|
---|
30 |
|
---|
31 | if (!rays.empty())
|
---|
32 | return rays.back();
|
---|
33 | else
|
---|
34 | return NULL;
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | /** Checks if ray is valid, (e.g., not in empty view space or outside the view space)
|
---|
39 | */
|
---|
40 | bool 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 |
|
---|
74 | int RayCaster::ProcessRay(const SimpleRay &simpleRay,
|
---|
75 | Intersection &hitA,
|
---|
76 | Intersection &hitB,
|
---|
77 | VssRayContainer &vssRays,
|
---|
78 | const AxisAlignedBox3 &box,
|
---|
79 | const bool castDoubleRay,
|
---|
80 | const bool pruneInvalidRays)
|
---|
81 | {
|
---|
82 | int hits = 0;
|
---|
83 |
|
---|
84 | #if DEBUG_RAYCAST
|
---|
85 | Debug<<"PR ";
|
---|
86 | #endif
|
---|
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));
|
---|
99 |
|
---|
100 | if (!sbox.IsInside(simpleRay.mOrigin))
|
---|
101 | {
|
---|
102 | return 0;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | // cout << "here81 " << hitA.mObject << endl;
|
---|
106 | const bool validA =
|
---|
107 | ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
108 | if (!validA && pruneInvalidRays)
|
---|
109 | {
|
---|
110 | return 0;
|
---|
111 | }
|
---|
112 |
|
---|
113 | const bool validB = castDoubleRay &&
|
---|
114 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
115 |
|
---|
116 | if (!validB && pruneInvalidRays)
|
---|
117 | {
|
---|
118 | return 0;
|
---|
119 | }
|
---|
120 |
|
---|
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,
|
---|
132 | mPreprocessor.mPass,
|
---|
133 | simpleRay.mPdf
|
---|
134 | );
|
---|
135 |
|
---|
136 | vssRays.push_back(vssRay);
|
---|
137 | ++ hits;
|
---|
138 | //cout << "here70 vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
142 | {
|
---|
143 | VssRay *vssRay = new VssRay(
|
---|
144 | hitA.mPoint,
|
---|
145 | hitB.mPoint,
|
---|
146 | hitA.mObject,
|
---|
147 | hitB.mObject,
|
---|
148 | mPreprocessor.mPass,
|
---|
149 | simpleRay.mPdf
|
---|
150 | );
|
---|
151 |
|
---|
152 | vssRays.push_back(vssRay);
|
---|
153 | ++ hits;
|
---|
154 | //cout << "here71 vssray 2: " << *vssRay << endl;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | return hits;
|
---|
159 | }
|
---|
160 |
|
---|
161 | } |
---|