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 | Ray ray(origin, termination - origin, Ray::LINE_SEGMENT);
|
---|
48 | ray.Precompute();
|
---|
49 |
|
---|
50 | float tmin, tmax;
|
---|
51 | if ((!mPreprocessor.mViewCellsManager->
|
---|
52 | GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax)) ||
|
---|
53 | tmin>=tmax
|
---|
54 | )
|
---|
55 | return false;
|
---|
56 |
|
---|
57 | if (tmin >= 1.0f || tmax <=0.0f)
|
---|
58 | return false;
|
---|
59 |
|
---|
60 | if (tmin > 0.0f)
|
---|
61 | clippedOrigin = ray.Extrap(tmin);
|
---|
62 | else
|
---|
63 | clippedOrigin = origin;
|
---|
64 |
|
---|
65 | if (tmax < 1.0f)
|
---|
66 | clippedTermination = ray.Extrap(tmax);
|
---|
67 | else
|
---|
68 | clippedTermination = termination;
|
---|
69 |
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /** Checks if ray is valid, (e.g., not in empty view space or outside the view space)
|
---|
74 | */
|
---|
75 | bool
|
---|
76 | RayCaster::ValidateRay(const Vector3 &origin,
|
---|
77 | const Vector3 &direction,
|
---|
78 | const AxisAlignedBox3 &box,
|
---|
79 | Intersection &hit)
|
---|
80 | {
|
---|
81 | if (!hit.mObject)
|
---|
82 | {
|
---|
83 | // compute intersection with the scene bounding box
|
---|
84 | #if EXACT_BOX_CLIPPING
|
---|
85 | static Ray ray;
|
---|
86 | mPreprocessor.SetupRay(ray, origin, direction);
|
---|
87 |
|
---|
88 | float tmin, tmax;
|
---|
89 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
|
---|
90 | {
|
---|
91 | hit.mPoint = ray.Extrap(tmax);
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | // cout<<" invalid hp "<<tmin<<" "<<tmax<<endl;
|
---|
96 | // cout<<" box "<<box<<endl;
|
---|
97 | // cout<<" origin "<<origin<<endl;
|
---|
98 | // cout<<" direction "<<direction<<endl;
|
---|
99 | // cout<< "inv dir"<<ray.GetInvDir()<<endl;
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 | #else
|
---|
103 | hit.mPoint = origin + direction * Magnitude(box.Diagonal());
|
---|
104 | #endif
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | if (mPreprocessor.mDetectEmptyViewSpace)
|
---|
109 | {
|
---|
110 | if (DotProd(hit.mNormal, direction) >= -Limits::Small)
|
---|
111 | {
|
---|
112 | hit.mObject = NULL;
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | int
|
---|
123 | RayCaster::ProcessRay(const SimpleRay &simpleRay,
|
---|
124 | Intersection &hitA,
|
---|
125 | Intersection &hitB,
|
---|
126 | VssRayContainer &vssRays,
|
---|
127 | const AxisAlignedBox3 &box,
|
---|
128 | const bool castDoubleRay,
|
---|
129 | const bool pruneInvalidRays)
|
---|
130 | {
|
---|
131 | int hits = 0;
|
---|
132 |
|
---|
133 | #if DEBUG_RAYCAST
|
---|
134 | Debug<<"PRA"<<flush;
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
|
---|
138 | // if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small))
|
---|
139 | // return 0;
|
---|
140 |
|
---|
141 | if (pruneInvalidRays)
|
---|
142 | {
|
---|
143 | if (!hitA.mObject && !hitB.mObject)
|
---|
144 | {
|
---|
145 | return 0;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | const bool validA =
|
---|
150 | ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
151 |
|
---|
152 | // note: should we check for backward valitidy also for single rays?
|
---|
153 | const bool validB = //castDoubleRay &&
|
---|
154 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
155 |
|
---|
156 | #if DEBUG_RAYCAST
|
---|
157 | Debug<<"PR1"<<flush;
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | // reset both contributions
|
---|
161 | if (!validA || !validB)
|
---|
162 | {
|
---|
163 | if (pruneInvalidRays)
|
---|
164 | return 0;
|
---|
165 |
|
---|
166 | // reset both contributions of this ray
|
---|
167 | hitA.mObject = NULL;
|
---|
168 | hitB.mObject = NULL;
|
---|
169 | }
|
---|
170 |
|
---|
171 | // 8.11. 2007 JB
|
---|
172 | // degenerate rays checked by geometrical constraint...
|
---|
173 | // !pruneInvalidRays || (hitA.mObject != hitB.mObject);
|
---|
174 |
|
---|
175 |
|
---|
176 | #if DEBUG_RAYCAST
|
---|
177 | Debug<<"PR2"<<flush;
|
---|
178 | #endif
|
---|
179 | const bool validSample = true;
|
---|
180 | if (validSample) {
|
---|
181 | Vector3 clipA, clipB;
|
---|
182 | if (!ClipToViewSpaceBox(hitA.mPoint,
|
---|
183 | hitB.mPoint,
|
---|
184 | clipA,
|
---|
185 | clipB))
|
---|
186 | return 0;
|
---|
187 | if (!pruneInvalidRays || hitA.mObject) {
|
---|
188 | VssRay *vssRay = new VssRay(
|
---|
189 | clipB,
|
---|
190 | hitA.mPoint,
|
---|
191 | hitB.mObject,
|
---|
192 | hitA.mObject,
|
---|
193 | mPreprocessor.mPass,
|
---|
194 | simpleRay.mPdf);
|
---|
195 |
|
---|
196 | if (validA)
|
---|
197 | vssRay->mFlags |= VssRay::Valid;
|
---|
198 |
|
---|
199 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
200 | vssRays.push_back(vssRay);
|
---|
201 | ++ hits;
|
---|
202 | //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
203 | }
|
---|
204 |
|
---|
205 | #if DEBUG_RAYCAST
|
---|
206 | Debug<<"PR3"<<flush;
|
---|
207 | #endif
|
---|
208 |
|
---|
209 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
210 | {
|
---|
211 | VssRay *vssRay = new VssRay(
|
---|
212 | clipA,
|
---|
213 | hitB.mPoint,
|
---|
214 | hitA.mObject,
|
---|
215 | hitB.mObject,
|
---|
216 | mPreprocessor.mPass,
|
---|
217 | simpleRay.mPdf
|
---|
218 | );
|
---|
219 | if (validB)
|
---|
220 | vssRay->mFlags |= VssRay::Valid;
|
---|
221 |
|
---|
222 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
223 | vssRays.push_back(vssRay);
|
---|
224 | ++ hits;
|
---|
225 | //cout << "vssray 2: " << *vssRay << endl;
|
---|
226 | }
|
---|
227 | #if DEBUG_RAYCAST
|
---|
228 | Debug<<"PR4"<<flush;
|
---|
229 | #endif
|
---|
230 | }
|
---|
231 |
|
---|
232 | return hits;
|
---|
233 | }
|
---|
234 |
|
---|
235 | }
|
---|