1 | #include "RayCaster.h"
|
---|
2 | #include "VssRay.h"
|
---|
3 | #include "Ray.h"
|
---|
4 | #include "Preprocessor.h"
|
---|
5 | #include "ViewCellsManager.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | namespace GtpVisibilityPreprocessor {
|
---|
9 |
|
---|
10 |
|
---|
11 | #define DEBUG_RAYCAST 0
|
---|
12 |
|
---|
13 | #define EXACT_BOX_CLIPPING 0
|
---|
14 |
|
---|
15 | RayCaster::RayCaster(const Preprocessor &preprocessor):
|
---|
16 | mPreprocessor(preprocessor)
|
---|
17 | {
|
---|
18 | }
|
---|
19 |
|
---|
20 |
|
---|
21 | RayCaster::~RayCaster()
|
---|
22 | {
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | VssRay *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 |
|
---|
41 | bool
|
---|
42 | RayCaster::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 | */
|
---|
76 | bool
|
---|
77 | RayCaster::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 |
|
---|
123 | int
|
---|
124 | RayCaster::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 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
|
---|
148 | if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
|
---|
149 | return 0;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
154 | const bool validB = //castDoubleRay &&
|
---|
155 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
156 |
|
---|
157 |
|
---|
158 | #if DEBUG_RAYCAST
|
---|
159 | Debug<<"PR1"<<flush;
|
---|
160 | #endif
|
---|
161 |
|
---|
162 | // reset both contributions
|
---|
163 | if (!validA || !validB) {
|
---|
164 | if (pruneInvalidRays)
|
---|
165 | return 0;
|
---|
166 |
|
---|
167 | // reset both contributions of this ray
|
---|
168 | hitA.mObject = NULL;
|
---|
169 | hitB.mObject = NULL;
|
---|
170 | }
|
---|
171 |
|
---|
172 | // 8.11. 2007 JB
|
---|
173 | // degenerate rays checked by geometrical constraint...
|
---|
174 | // !pruneInvalidRays || (hitA.mObject != hitB.mObject);
|
---|
175 |
|
---|
176 |
|
---|
177 | #if DEBUG_RAYCAST
|
---|
178 | Debug<<"PR2"<<flush;
|
---|
179 | #endif
|
---|
180 | const bool validSample = true;
|
---|
181 | if (validSample) {
|
---|
182 | Vector3 clipA, clipB;
|
---|
183 | if (!ClipToViewSpaceBox(hitA.mPoint,
|
---|
184 | hitB.mPoint,
|
---|
185 | clipA,
|
---|
186 | clipB))
|
---|
187 | return 0;
|
---|
188 |
|
---|
189 | if (!pruneInvalidRays || hitA.mObject) {
|
---|
190 | VssRay *vssRay = new VssRay(
|
---|
191 | clipB,
|
---|
192 | hitA.mPoint,
|
---|
193 | hitB.mObject,
|
---|
194 | hitA.mObject,
|
---|
195 | mPreprocessor.mPass,
|
---|
196 | simpleRay.mPdf);
|
---|
197 |
|
---|
198 | if (validA)
|
---|
199 | vssRay->mFlags |= VssRay::Valid;
|
---|
200 |
|
---|
201 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
202 | vssRays.push_back(vssRay);
|
---|
203 | ++ hits;
|
---|
204 | //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
205 | }
|
---|
206 |
|
---|
207 | #if DEBUG_RAYCAST
|
---|
208 | Debug<<"PR3"<<flush;
|
---|
209 | #endif
|
---|
210 |
|
---|
211 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
212 | {
|
---|
213 | VssRay *vssRay = new VssRay(
|
---|
214 | clipA,
|
---|
215 | hitB.mPoint,
|
---|
216 | hitA.mObject,
|
---|
217 | hitB.mObject,
|
---|
218 | mPreprocessor.mPass,
|
---|
219 | simpleRay.mPdf
|
---|
220 | );
|
---|
221 | if (validB)
|
---|
222 | vssRay->mFlags |= VssRay::Valid;
|
---|
223 |
|
---|
224 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
225 | vssRays.push_back(vssRay);
|
---|
226 | ++ hits;
|
---|
227 | //cout << "vssray 2: " << *vssRay << endl;
|
---|
228 | }
|
---|
229 | #if DEBUG_RAYCAST
|
---|
230 | Debug<<"PR4"<<flush;
|
---|
231 | #endif
|
---|
232 | }
|
---|
233 |
|
---|
234 | return hits;
|
---|
235 | }
|
---|
236 |
|
---|
237 | }
|
---|