[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 |
|
---|
| 8 | namespace GtpVisibilityPreprocessor {
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | #define DEBUG_RAYCAST 0
|
---|
| 12 |
|
---|
[1942] | 13 | #define EXACT_BOX_CLIPPING 0
|
---|
[1520] | 14 |
|
---|
| 15 | RayCaster::RayCaster(const Preprocessor &preprocessor):
|
---|
| 16 | mPreprocessor(preprocessor)
|
---|
| 17 | {
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 |
|
---|
[1521] | 21 | RayCaster::~RayCaster()
|
---|
| 22 | {
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 |
|
---|
[1545] | 26 | VssRay *RayCaster::CastRay(const SimpleRay &simpleRay,
|
---|
[1990] | 27 | const AxisAlignedBox3 &box,
|
---|
[1996] | 28 | const bool castDoubleRay)
|
---|
[1521] | 29 | {
|
---|
[1932] | 30 | VssRayContainer rays;
|
---|
[1996] | 31 | CastRay(simpleRay, rays, box, castDoubleRay, true);
|
---|
[1932] | 32 |
|
---|
| 33 | if (!rays.empty())
|
---|
| 34 | return rays.back();
|
---|
| 35 | else
|
---|
| 36 | return NULL;
|
---|
[1521] | 37 | }
|
---|
| 38 |
|
---|
[1990] | 39 |
|
---|
[1942] | 40 | bool
|
---|
| 41 | RayCaster::ClipToViewSpaceBox(const Vector3 &origin,
|
---|
| 42 | const Vector3 &termination,
|
---|
| 43 | Vector3 &clippedOrigin,
|
---|
| 44 | Vector3 &clippedTermination)
|
---|
| 45 | {
|
---|
[1952] | 46 |
|
---|
[1942] | 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;
|
---|
[1521] | 56 |
|
---|
[2070] | 57 | if (tmin >= 1.0f || tmax <= 0.0f)
|
---|
[1942] | 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 |
|
---|
[2070] | 73 | /** Checks if ray is valid
|
---|
| 74 | (e.g., not in empty view space or outside the view space)
|
---|
[1528] | 75 | */
|
---|
[1867] | 76 | bool
|
---|
| 77 | RayCaster::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 | {
|
---|
[2035] | 109 | if (mPreprocessor.mDetectEmptyViewSpace) {
|
---|
| 110 | if (DotProd(hit.mNormal, direction) >= -Limits::Small) {
|
---|
| 111 | hit.mObject = NULL;
|
---|
| 112 | return false;
|
---|
[1932] | 113 | }
|
---|
[2035] | 114 | }
|
---|
[1932] | 115 | }
|
---|
[2035] | 116 |
|
---|
[1932] | 117 | return true;
|
---|
[1528] | 118 | }
|
---|
| 119 |
|
---|
[1974] | 120 | void
|
---|
| 121 | RayCaster::SortRays(SimpleRayContainer &rays)
|
---|
| 122 | {
|
---|
[1984] | 123 | AxisAlignedBox3 box =
|
---|
| 124 | mPreprocessor.mViewCellsManager->GetViewSpaceBox();
|
---|
[2105] | 125 |
|
---|
[1984] | 126 | float b[12]={
|
---|
| 127 | box.Min().x,
|
---|
| 128 | box.Min().y,
|
---|
| 129 | box.Min().z,
|
---|
| 130 | -1,
|
---|
| 131 | -1,
|
---|
| 132 | -1,
|
---|
| 133 | box.Max().x,
|
---|
| 134 | box.Max().y,
|
---|
| 135 | box.Max().z,
|
---|
| 136 | 1,
|
---|
| 137 | 1,
|
---|
| 138 | 1
|
---|
| 139 | };
|
---|
| 140 |
|
---|
[2105] | 141 | #if 0
|
---|
| 142 | static vector<SimpleRay *> pointerArray;
|
---|
| 143 |
|
---|
| 144 | if (pointerArray.size()!=rays.size()) {
|
---|
| 145 | // realloc the pointerarray
|
---|
| 146 | pointerArray.resize(rays.size());
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | // init pointer array
|
---|
| 150 | SimpleRay *p = &pointerArray[0];
|
---|
| 151 | for (i=0; i < rays.size(); i++, p++)
|
---|
| 152 | pointerArray[i] = p;
|
---|
| 153 | #endif
|
---|
| 154 |
|
---|
[1984] | 155 | _SortRays(rays,
|
---|
| 156 | 0,
|
---|
[1990] | 157 | (int)rays.size()-1,
|
---|
[1984] | 158 | 0,
|
---|
| 159 | b
|
---|
| 160 | );
|
---|
[1974] | 161 | }
|
---|
| 162 |
|
---|
| 163 | void
|
---|
| 164 | RayCaster::_SortRays(SimpleRayContainer &rays,
|
---|
| 165 | const int l,
|
---|
| 166 | const int r,
|
---|
[1984] | 167 | const int depth,
|
---|
| 168 | float box[12])
|
---|
[1974] | 169 | {
|
---|
| 170 | // pick-up a pivot
|
---|
[1984] | 171 | int axis;
|
---|
| 172 |
|
---|
| 173 | float maxDiff = -1.0f;
|
---|
| 174 | // get the largest axis
|
---|
| 175 | int offset = 0;
|
---|
[1990] | 176 | int i;
|
---|
[1984] | 177 |
|
---|
[2105] | 178 | //const int batchsize = 16384;
|
---|
[2008] | 179 | const int batchsize = 8192;
|
---|
[2105] | 180 | //const int batchsize = 128;
|
---|
[2008] | 181 |
|
---|
[2105] | 182 | //if (r - l < 16*batchsize)
|
---|
| 183 | // offset = 3;
|
---|
[2008] | 184 |
|
---|
[2105] | 185 | // if (depth%2==0)
|
---|
[2008] | 186 | // offset = 3;
|
---|
[1984] | 187 |
|
---|
[2002] | 188 | for (i=offset; i < offset + 3; i++) {
|
---|
| 189 | float diff = box[i + 6] - box[i];
|
---|
[1984] | 190 | if (diff > maxDiff) {
|
---|
| 191 | maxDiff = diff;
|
---|
| 192 | axis = i;
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[2008] | 196 | // cout<<depth<<" "<<axis<<" "<<l<<" "<<r<<endl;
|
---|
[1984] | 197 |
|
---|
[1990] | 198 | i=l;
|
---|
| 199 | int j=r;
|
---|
| 200 |
|
---|
[1984] | 201 | float x = (box[axis] + box[axis+6])*0.5f;
|
---|
| 202 | // float x = rays[(l+r)/2].GetParam(axis);
|
---|
[1974] | 203 | do {
|
---|
[2008] | 204 | while(i<j && rays[i].GetParam(axis) < x)
|
---|
[1974] | 205 | i++;
|
---|
[2008] | 206 | while(i<j && x < rays[j].GetParam(axis))
|
---|
[1974] | 207 | j--;
|
---|
| 208 |
|
---|
| 209 | if (i <= j) {
|
---|
| 210 | swap(rays[i], rays[j]);
|
---|
| 211 | i++;
|
---|
| 212 | j--;
|
---|
| 213 | }
|
---|
| 214 | } while (i<=j);
|
---|
[2008] | 215 |
|
---|
[1974] | 216 |
|
---|
[2008] | 217 | if (l + batchsize < j ) {
|
---|
[1984] | 218 | // set new max
|
---|
| 219 | float save = box[axis+6];
|
---|
| 220 | box[axis+6] = x;
|
---|
| 221 | _SortRays(rays, l, j, depth+1, box);
|
---|
| 222 | box[axis+6] = save;
|
---|
[2008] | 223 | } else {
|
---|
| 224 | // for (int k=0; k < 6; k++)
|
---|
| 225 | // cout<<k<<" "<<box[k]<<" - "<<box[k+6]<<endl;
|
---|
[1984] | 226 | }
|
---|
[1974] | 227 |
|
---|
[2008] | 228 | if (i + batchsize < r) {
|
---|
[1984] | 229 | // set new min
|
---|
| 230 | box[axis] = x;
|
---|
| 231 | _SortRays(rays, i, r, depth+1, box);
|
---|
[2008] | 232 | } else {
|
---|
| 233 | // for (int k=0; k < 6; k++)
|
---|
| 234 | // cout<<k<<" "<<box[k]<<" - "<<box[k+6]<<endl;
|
---|
[1984] | 235 | }
|
---|
[2008] | 236 |
|
---|
[1974] | 237 | }
|
---|
| 238 |
|
---|
| 239 |
|
---|
[2187] | 240 | VssRay *RayCaster::RequestRay(const Vector3 &origin,
|
---|
| 241 | const Vector3 &termination,
|
---|
| 242 | Intersectable *originObject,
|
---|
| 243 | Intersectable *terminationObject,
|
---|
| 244 | const int pass,
|
---|
| 245 | const float pdf)
|
---|
| 246 | {
|
---|
| 247 | #if DEBUG_RAYCAST
|
---|
| 248 | Debug<<"PR2a"<<flush;
|
---|
| 249 | #endif
|
---|
| 250 |
|
---|
| 251 | // old method: always allocate
|
---|
[2198] | 252 | if (0) return new VssRay(origin, termination, originObject, terminationObject, pass, pdf);
|
---|
[1528] | 253 |
|
---|
[2187] | 254 | VssRay *vssRay = mVssRayPool.Alloc();
|
---|
| 255 |
|
---|
| 256 | #if DEBUG_RAYCAST
|
---|
| 257 | Debug<<"PR2b"<<flush;
|
---|
| 258 | #endif
|
---|
| 259 |
|
---|
| 260 | *vssRay = VssRay(origin, termination, originObject, terminationObject, pass, pdf);
|
---|
| 261 |
|
---|
| 262 | #if DEBUG_RAYCAST
|
---|
| 263 | Debug<<"PR2c"<<flush;
|
---|
| 264 | #endif
|
---|
| 265 |
|
---|
| 266 | return vssRay;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 |
|
---|
[1867] | 270 | int
|
---|
| 271 | RayCaster::ProcessRay(const SimpleRay &simpleRay,
|
---|
| 272 | Intersection &hitA,
|
---|
| 273 | Intersection &hitB,
|
---|
| 274 | VssRayContainer &vssRays,
|
---|
| 275 | const AxisAlignedBox3 &box,
|
---|
| 276 | const bool castDoubleRay,
|
---|
[1996] | 277 | const bool pruneInvalidRays)
|
---|
[1520] | 278 | {
|
---|
[1984] | 279 | int hits = 0;
|
---|
[1528] | 280 |
|
---|
[2014] | 281 |
|
---|
[1520] | 282 | #if DEBUG_RAYCAST
|
---|
[2013] | 283 | static int id=0;
|
---|
| 284 | Debug<<"PRA "<<id++<<endl<<flush;
|
---|
[1520] | 285 | #endif
|
---|
[1528] | 286 |
|
---|
[1932] | 287 | if (pruneInvalidRays)
|
---|
| 288 | {
|
---|
[1952] | 289 | if (!hitA.mObject && !hitB.mObject) {
|
---|
| 290 | return 0;
|
---|
| 291 | }
|
---|
[1520] | 292 | }
|
---|
[1584] | 293 |
|
---|
[2070] | 294 | // regardless of the pruneInvalidRays setting reject
|
---|
| 295 | // rays whic degenerate to a point
|
---|
[1966] | 296 | if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
|
---|
[2035] | 297 | return 0;
|
---|
[1966] | 298 | }
|
---|
| 299 |
|
---|
[1952] | 300 | const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
[1932] | 301 | const bool validB = //castDoubleRay &&
|
---|
[1952] | 302 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
[1867] | 303 |
|
---|
[1952] | 304 |
|
---|
[1757] | 305 | #if DEBUG_RAYCAST
|
---|
| 306 | Debug<<"PR1"<<flush;
|
---|
| 307 | #endif
|
---|
[1584] | 308 |
|
---|
[1867] | 309 | // reset both contributions
|
---|
[1952] | 310 | if (!validA || !validB) {
|
---|
[2070] | 311 | if (0 || pruneInvalidRays)
|
---|
[1952] | 312 | return 0;
|
---|
[1867] | 313 |
|
---|
[1952] | 314 | // reset both contributions of this ray
|
---|
| 315 | hitA.mObject = NULL;
|
---|
| 316 | hitB.mObject = NULL;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[1867] | 319 | // 8.11. 2007 JB
|
---|
| 320 | // degenerate rays checked by geometrical constraint...
|
---|
| 321 | // !pruneInvalidRays || (hitA.mObject != hitB.mObject);
|
---|
[1942] | 322 |
|
---|
[1584] | 323 |
|
---|
[1757] | 324 | #if DEBUG_RAYCAST
|
---|
| 325 | Debug<<"PR2"<<flush;
|
---|
| 326 | #endif
|
---|
[1942] | 327 | const bool validSample = true;
|
---|
| 328 | if (validSample) {
|
---|
| 329 | Vector3 clipA, clipB;
|
---|
| 330 | if (!ClipToViewSpaceBox(hitA.mPoint,
|
---|
| 331 | hitB.mPoint,
|
---|
| 332 | clipA,
|
---|
| 333 | clipB))
|
---|
| 334 | return 0;
|
---|
[1952] | 335 |
|
---|
[1942] | 336 | if (!pruneInvalidRays || hitA.mObject) {
|
---|
[2014] | 337 |
|
---|
[2187] | 338 | VssRay *vssRay =
|
---|
| 339 | RequestRay(!castDoubleRay ? simpleRay.mOrigin : clipB,
|
---|
[2012] | 340 | hitA.mPoint,
|
---|
| 341 | hitB.mObject,
|
---|
| 342 | hitA.mObject,
|
---|
| 343 | mPreprocessor.mPass,
|
---|
[2105] | 344 | 1.0f //simpleRay.mPdf
|
---|
[2012] | 345 | );
|
---|
[2187] | 346 |
|
---|
[1867] | 347 | if (validA)
|
---|
[2021] | 348 | vssRay->mFlags |= VssRay::Valid;
|
---|
| 349 |
|
---|
[1883] | 350 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1989] | 351 | vssRay->mGeneratorId = simpleRay.mGeneratorId;
|
---|
| 352 |
|
---|
[1867] | 353 | vssRays.push_back(vssRay);
|
---|
| 354 | ++ hits;
|
---|
| 355 | //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
[1932] | 356 | }
|
---|
| 357 |
|
---|
[1867] | 358 | #if DEBUG_RAYCAST
|
---|
[1932] | 359 | Debug<<"PR3"<<flush;
|
---|
[1867] | 360 | #endif
|
---|
| 361 |
|
---|
[2070] | 362 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
| 363 | {
|
---|
[2187] | 364 | VssRay *vssRay = RequestRay(
|
---|
| 365 | clipA,
|
---|
| 366 | hitB.mPoint,
|
---|
| 367 | hitA.mObject,
|
---|
| 368 | hitB.mObject,
|
---|
| 369 | mPreprocessor.mPass,
|
---|
| 370 | 1.0f //simpleRay.mPdf
|
---|
| 371 | );
|
---|
[2070] | 372 |
|
---|
| 373 | if (validB)
|
---|
[1771] | 374 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1932] | 375 |
|
---|
| 376 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1989] | 377 | vssRay->mGeneratorId = simpleRay.mGeneratorId;
|
---|
[1932] | 378 | vssRays.push_back(vssRay);
|
---|
| 379 | ++ hits;
|
---|
| 380 | //cout << "vssray 2: " << *vssRay << endl;
|
---|
[2070] | 381 | }
|
---|
[1942] | 382 | #if DEBUG_RAYCAST
|
---|
| 383 | Debug<<"PR4"<<flush;
|
---|
| 384 | #endif
|
---|
[1520] | 385 | }
|
---|
[1867] | 386 |
|
---|
[1520] | 387 | return hits;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
[2076] | 390 |
|
---|
| 391 | void
|
---|
| 392 | RayCaster::CastRays(
|
---|
| 393 | SimpleRayContainer &rays,
|
---|
| 394 | VssRayContainer &vssRays,
|
---|
| 395 | const AxisAlignedBox3 &sbox,
|
---|
| 396 | const bool castDoubleRay,
|
---|
| 397 | const bool pruneInvalidRays )
|
---|
| 398 | {
|
---|
[2187] | 399 | SimpleRayContainer::const_iterator rit, rit_end = rays.end();
|
---|
| 400 |
|
---|
| 401 | for (rit = rays.begin(); rit != rit_end; ++ rit) {
|
---|
| 402 | CastRay(
|
---|
[2076] | 403 | *rit,
|
---|
| 404 | vssRays,
|
---|
| 405 | sbox,
|
---|
| 406 | castDoubleRay,
|
---|
| 407 | pruneInvalidRays);
|
---|
[2187] | 408 | }
|
---|
[1583] | 409 | }
|
---|
[2076] | 410 |
|
---|
[2187] | 411 |
|
---|
[2076] | 412 | }
|
---|