[2582] | 1 | // =================================================================== |
---|
| 2 | // $Id: sbbox.cpp $ |
---|
| 3 | // |
---|
| 4 | // ssbbox.cpp |
---|
| 5 | // Simply bounding box for ray tracing only, 24 Bytes in size. |
---|
| 6 | // REPLACEMENT_STRING |
---|
| 7 | // |
---|
| 8 | // Copyright by Vlastimil Havran, 2007 - email to "vhavran AT seznam.cz" |
---|
| 9 | // Initial coding by Vlasta Havran, December 2005 |
---|
| 10 | |
---|
| 11 | // GOLEM headers |
---|
| 12 | #include "sbbox.h" |
---|
| 13 | |
---|
| 14 | #undef INLINE |
---|
| 15 | #define INLINE |
---|
| 16 | //#define INLINE INLINE |
---|
| 17 | |
---|
| 18 | namespace GtpVisibilityPreprocessor { |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | void |
---|
| 22 | Describe(const SBBox &b, ostream &app, int ind) |
---|
| 23 | { |
---|
| 24 | indent(app, ind); |
---|
| 25 | app << "SBBox: min at(" << b.Min() << "), max at(" << b.Max() << ")\n"; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | // Function describing the box |
---|
| 29 | void |
---|
| 30 | DescribeXYZ(const SBBox &b, ostream &app, int ind) |
---|
| 31 | { |
---|
| 32 | indent(app, ind); |
---|
| 33 | app << " box = ( " |
---|
| 34 | << b.Min().x << " , " << b.Max().x << " )( " |
---|
| 35 | << b.Min().y << " , " << b.Max().y << " )( " |
---|
| 36 | << b.Min().z << " , " << b.Max().z << " )\n"; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | bool |
---|
| 41 | SBBox::Includes(const SBBox &b) const |
---|
| 42 | { |
---|
| 43 | if (b.pp[0].x >= pp[0].x && |
---|
| 44 | b.pp[0].y >= pp[0].y && |
---|
| 45 | b.pp[0].z >= pp[0].z && |
---|
| 46 | b.pp[1].x <= pp[1].x && |
---|
| 47 | b.pp[1].y <= pp[1].y && |
---|
| 48 | b.pp[1].z <= pp[1].z) |
---|
| 49 | return true; |
---|
| 50 | return false; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | bool |
---|
| 54 | SBBox::Includes(const SBBox &b, float eps) const |
---|
| 55 | { |
---|
| 56 | if (b.pp[0].x >= pp[0].x - eps && |
---|
| 57 | b.pp[0].y >= pp[0].y - eps && |
---|
| 58 | b.pp[0].z >= pp[0].z - eps && |
---|
| 59 | b.pp[1].x <= pp[1].x + eps && |
---|
| 60 | b.pp[1].y <= pp[1].y + eps && |
---|
| 61 | b.pp[1].z <= pp[1].z + eps) |
---|
| 62 | return true; |
---|
| 63 | return false; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | #if 0 |
---|
| 67 | // The implementation I, the first version implemented by Vlastimil |
---|
| 68 | // Havran, without looking into the literature |
---|
| 69 | INLINE bool |
---|
| 70 | SBBox::RayIntersect(const CRay &ray, |
---|
| 71 | float &tmin, float &tmax) const |
---|
| 72 | { |
---|
| 73 | float interval_min = tmin; |
---|
| 74 | float interval_max = tmax; |
---|
| 75 | |
---|
| 76 | const CVector3D &rayLoc = ray.GetLoc(); |
---|
| 77 | const CVector3D &rayinvDir = ray.GetInvertedDir(); |
---|
| 78 | |
---|
| 79 | float t0, t1; |
---|
| 80 | if (rayinvDir.x > 0) { |
---|
| 81 | t0 = (pp[0].x - rayLoc.x) * rayinvDir.x; |
---|
| 82 | t1 = (pp[1].x - rayLoc.x) * rayinvDir.x; |
---|
| 83 | } |
---|
| 84 | else { |
---|
| 85 | t0 = (pp[1].x - rayLoc.x) * rayinvDir.x; |
---|
| 86 | t1 = (pp[0].x - rayLoc.x) * rayinvDir.x; |
---|
| 87 | } |
---|
| 88 | assert(t0 <= t1); |
---|
| 89 | if (t0 > interval_min) |
---|
| 90 | interval_min = t0; |
---|
| 91 | if (t1 < interval_max) |
---|
| 92 | interval_max = t1; |
---|
| 93 | if (interval_min > interval_max) |
---|
| 94 | return false; |
---|
| 95 | |
---|
| 96 | if (rayinvDir.y > 0) { |
---|
| 97 | t0 = (pp[0].y - rayLoc.y) * rayinvDir.y; |
---|
| 98 | t1 = (pp[1].y - rayLoc.y) * rayinvDir.y; |
---|
| 99 | } |
---|
| 100 | else { |
---|
| 101 | t0 = (pp[1].y - rayLoc.y) * rayinvDir.y; |
---|
| 102 | t1 = (pp[0].y - rayLoc.y) * rayinvDir.y; |
---|
| 103 | } |
---|
| 104 | assert(t0 <= t1); |
---|
| 105 | if (t0 > interval_min) |
---|
| 106 | interval_min = t0; |
---|
| 107 | if (t1 < interval_max) |
---|
| 108 | interval_max = t1; |
---|
| 109 | if (interval_min > interval_max) |
---|
| 110 | return false; |
---|
| 111 | |
---|
| 112 | if (rayinvDir.z > 0) { |
---|
| 113 | t0 = (pp[0].z - rayLoc.z) * rayinvDir.z; |
---|
| 114 | t1 = (pp[1].z - rayLoc.z) * rayinvDir.z; |
---|
| 115 | } |
---|
| 116 | else { |
---|
| 117 | t0 = (pp[1].z - rayLoc.z) * rayinvDir.z; |
---|
| 118 | t1 = (pp[0].z - rayLoc.z) * rayinvDir.z; |
---|
| 119 | } |
---|
| 120 | assert(t0 <= t1); |
---|
| 121 | if (t0 > interval_min) |
---|
| 122 | interval_min = t0; |
---|
| 123 | if (t1 < interval_max) |
---|
| 124 | interval_max = t1; |
---|
| 125 | |
---|
| 126 | // return true if the box is intersected |
---|
| 127 | // return false if not intersected by ray |
---|
| 128 | if ( (interval_max > 0.0f) && |
---|
| 129 | (interval_min <= interval_max) ) { |
---|
| 130 | // yes, intersected, update tmin and tmax for current node |
---|
| 131 | tmin = interval_min; |
---|
| 132 | tmax = interval_max; |
---|
| 133 | return true; |
---|
| 134 | } |
---|
| 135 | return false; // not intersected |
---|
| 136 | } |
---|
| 137 | #endif |
---|
| 138 | |
---|
| 139 | // ------------------------------------------------------------- |
---|
| 140 | #if 0 |
---|
| 141 | // The implementation II |
---|
| 142 | // The code follows the article by Mueller/Geimer 2003 paper |
---|
| 143 | |
---|
| 144 | INLINE |
---|
| 145 | float minf(const float a, const float b) { return (a < b) ? a : b;} |
---|
| 146 | INLINE |
---|
| 147 | float maxf(const float a, const float b) { return (a > b) ? a : b;} |
---|
| 148 | |
---|
| 149 | INLINE bool |
---|
| 150 | SBBox::RayIntersect(const CRay &ray, |
---|
| 151 | float &tmin, |
---|
| 152 | float &tmax) const |
---|
| 153 | { |
---|
| 154 | //assert(tmin <= tmax); |
---|
| 155 | const CVector3D &rayLoc = ray.GetLoc(); |
---|
| 156 | const CVector3D &rayinvDir = ray.GetInvertedDir(); |
---|
| 157 | |
---|
| 158 | register float t0, t1; |
---|
| 159 | // coordinate x |
---|
| 160 | t0 = (pp[0].x - rayLoc.x) * rayinvDir.x; |
---|
| 161 | t1 = (pp[1].x - rayLoc.x) * rayinvDir.x; |
---|
| 162 | register float ttmin = maxf(minf(t0, t1), tmin); |
---|
| 163 | register float ttmax = minf(maxf(t0, t1), tmax); |
---|
| 164 | // coordinate y |
---|
| 165 | t0 = (pp[0].y - rayLoc.y) * rayinvDir.y; |
---|
| 166 | t1 = (pp[1].y - rayLoc.y) * rayinvDir.y; |
---|
| 167 | ttmin = maxf(minf(t0, t1), ttmin); |
---|
| 168 | ttmax = minf(maxf(t0, t1), ttmax); |
---|
| 169 | // coordinate z |
---|
| 170 | t0 = (pp[0].z - rayLoc.z) * rayinvDir.z; |
---|
| 171 | t1 = (pp[1].z - rayLoc.z) * rayinvDir.z; |
---|
| 172 | ttmin = maxf(minf(t0, t1), ttmin); |
---|
| 173 | ttmax = minf(maxf(t0, t1), ttmax); |
---|
| 174 | |
---|
| 175 | // return true if the box is intersected |
---|
| 176 | // return false if not intersected by ray |
---|
| 177 | if ( (ttmax > 0.f) && |
---|
| 178 | (ttmin <= ttmax)) { |
---|
| 179 | // yes, intersected, update tmin and tmax for current node |
---|
| 180 | tmin = ttmin; |
---|
| 181 | tmax = ttmax; |
---|
| 182 | return true; |
---|
| 183 | } |
---|
| 184 | return false; // not intersected |
---|
| 185 | } |
---|
| 186 | #endif |
---|
| 187 | |
---|
| 188 | // ------------------------------------------------------------- |
---|
| 189 | #if 0 |
---|
| 190 | // The implementation III |
---|
| 191 | // The code follows the article by Mueller/Geimer 2003 paper |
---|
| 192 | // - improved possibly by earlier termination. |
---|
| 193 | INLINE |
---|
| 194 | float minf(const float a, const float b) { return (a < b) ? a : b;} |
---|
| 195 | INLINE |
---|
| 196 | float maxf(const float a, const float b) { return (a > b) ? a : b;} |
---|
| 197 | |
---|
| 198 | INLINE bool |
---|
| 199 | SBBox::RayIntersect(const CRay &ray, |
---|
| 200 | float &tmin, |
---|
| 201 | float &tmax) const |
---|
| 202 | { |
---|
| 203 | //assert(tmin <= tmax); |
---|
| 204 | const CVector3D &rayLoc = ray.GetLoc(); |
---|
| 205 | const CVector3D &rayinvDir = ray.GetInvertedDir(); |
---|
| 206 | |
---|
| 207 | register float t0, t1; |
---|
| 208 | // coordinate x |
---|
| 209 | t0 = (pp[0].x - rayLoc.x) * rayinvDir.x; |
---|
| 210 | t1 = (pp[1].x - rayLoc.x) * rayinvDir.x; |
---|
| 211 | register float ttmin = maxf(minf(t0, t1), tmin); |
---|
| 212 | register float ttmax = minf(maxf(t0, t1), tmax); |
---|
| 213 | if (ttmin > ttmax) |
---|
| 214 | return false; |
---|
| 215 | |
---|
| 216 | // coordinate y |
---|
| 217 | t0 = (pp[0].y - rayLoc.y) * rayinvDir.y; |
---|
| 218 | t1 = (pp[1].y - rayLoc.y) * rayinvDir.y; |
---|
| 219 | ttmin = maxf(minf(t0, t1), ttmin); |
---|
| 220 | ttmax = minf(maxf(t0, t1), ttmax); |
---|
| 221 | if (ttmin > ttmax) |
---|
| 222 | return false; |
---|
| 223 | |
---|
| 224 | // coordinate z |
---|
| 225 | t0 = (pp[0].z - rayLoc.z) * rayinvDir.z; |
---|
| 226 | t1 = (pp[1].z - rayLoc.z) * rayinvDir.z; |
---|
| 227 | ttmin = maxf(minf(t0, t1), ttmin); |
---|
| 228 | ttmax = minf(maxf(t0, t1), ttmax); |
---|
| 229 | |
---|
| 230 | // return true if the box is intersected |
---|
| 231 | // return false if not intersected by ray |
---|
| 232 | if ( (ttmax > 0.f) && |
---|
| 233 | (ttmin <= ttmax)) { |
---|
| 234 | // yes, intersected, update tmin and tmax for current node |
---|
| 235 | tmin = ttmin; |
---|
| 236 | tmax = ttmax; |
---|
| 237 | return true; |
---|
| 238 | } |
---|
| 239 | return false; // not intersected |
---|
| 240 | } |
---|
| 241 | #endif |
---|
| 242 | |
---|
| 243 | #if 0 |
---|
| 244 | // The implementation IV |
---|
| 245 | // - improved implementation I - it does not work for shadow rays ? |
---|
| 246 | INLINE |
---|
| 247 | float minf(const float a, const float b) { return (a < b) ? a : b;} |
---|
| 248 | INLINE |
---|
| 249 | float maxf(const float a, const float b) { return (a > b) ? a : b;} |
---|
| 250 | |
---|
| 251 | INLINE bool |
---|
| 252 | SBBox::RayIntersect(const CRay &ray, |
---|
| 253 | float &tmin, |
---|
| 254 | float &tmax) const |
---|
| 255 | { |
---|
| 256 | register float interval_min = tmin; |
---|
| 257 | register float interval_max = tmax; |
---|
| 258 | |
---|
| 259 | const CVector3D &rayLoc = ray.GetLoc(); |
---|
| 260 | const CVector3D &rayinvDir = ray.GetInvertedDir(); |
---|
| 261 | |
---|
| 262 | register float t0, t1; |
---|
| 263 | if (rayinvDir.x > 0) { |
---|
| 264 | t0 = (pp[0].x - rayLoc.x) * rayinvDir.x; |
---|
| 265 | t1 = (pp[1].x - rayLoc.x) * rayinvDir.x; |
---|
| 266 | } |
---|
| 267 | else { |
---|
| 268 | t0 = (pp[1].x - rayLoc.x) * rayinvDir.x; |
---|
| 269 | t1 = (pp[0].x - rayLoc.x) * rayinvDir.x; |
---|
| 270 | } |
---|
| 271 | assert(t0 <= t1); |
---|
| 272 | interval_min = maxf(t0, interval_min); |
---|
| 273 | interval_max = minf(t1, interval_min); |
---|
| 274 | if (interval_min > interval_max) |
---|
| 275 | return false; |
---|
| 276 | |
---|
| 277 | if (rayinvDir.y > 0) { |
---|
| 278 | t0 = (pp[0].y - rayLoc.y) * rayinvDir.y; |
---|
| 279 | t1 = (pp[1].y - rayLoc.y) * rayinvDir.y; |
---|
| 280 | } |
---|
| 281 | else { |
---|
| 282 | t0 = (pp[1].y - rayLoc.y) * rayinvDir.y; |
---|
| 283 | t1 = (pp[0].y - rayLoc.y) * rayinvDir.y; |
---|
| 284 | } |
---|
| 285 | assert(t0 <= t1); |
---|
| 286 | interval_min = maxf(t0, interval_min); |
---|
| 287 | interval_max = minf(t1, interval_min); |
---|
| 288 | if (interval_min > interval_max) |
---|
| 289 | return false; |
---|
| 290 | |
---|
| 291 | if (rayinvDir.z > 0) { |
---|
| 292 | t0 = (pp[0].z - rayLoc.z) * rayinvDir.z; |
---|
| 293 | t1 = (pp[1].z - rayLoc.z) * rayinvDir.z; |
---|
| 294 | } |
---|
| 295 | else { |
---|
| 296 | t0 = (pp[1].z - rayLoc.z) * rayinvDir.z; |
---|
| 297 | t1 = (pp[0].z - rayLoc.z) * rayinvDir.z; |
---|
| 298 | } |
---|
| 299 | assert(t0 <= t1); |
---|
| 300 | interval_min = maxf(t0, interval_min); |
---|
| 301 | interval_max = minf(t1, interval_min); |
---|
| 302 | |
---|
| 303 | // return true if the box is intersected |
---|
| 304 | // return false if not intersected by ray |
---|
| 305 | if ( (interval_max > 0.f) && |
---|
| 306 | (interval_min <= interval_max)) { |
---|
| 307 | // yes, intersected, update tmin and tmax for current node |
---|
| 308 | tmin = interval_min; |
---|
| 309 | tmax = interval_max; |
---|
| 310 | return true; |
---|
| 311 | } |
---|
| 312 | return false; // not intersected |
---|
| 313 | } |
---|
| 314 | #endif |
---|
| 315 | |
---|
| 316 | // ------------------------------------------------------------- |
---|
| 317 | #if 0 |
---|
| 318 | // The implementation V |
---|
| 319 | // The code follows the article by Mueller/Geimer 2003 paper |
---|
| 320 | // - changed. - it does not work for shadow rays ? |
---|
| 321 | INLINE bool |
---|
| 322 | SBBox::RayIntersect(const CRay &ray, |
---|
| 323 | float &tmin, |
---|
| 324 | float &tmax) const |
---|
| 325 | { |
---|
| 326 | //assert(tmin <= tmax); |
---|
| 327 | const CVector3D &rayLoc = ray.GetLoc(); |
---|
| 328 | const CVector3D &rayinvDir = ray.GetInvertedDir(); |
---|
| 329 | |
---|
| 330 | register float tt_min = tmin; |
---|
| 331 | register float tt_max = tmax; |
---|
| 332 | |
---|
| 333 | register float t0, t1; |
---|
| 334 | // coordinate x |
---|
| 335 | t0 = (pp[0].x - rayLoc.x) * rayinvDir.x; |
---|
| 336 | t1 = (pp[1].x - rayLoc.x) * rayinvDir.x; |
---|
| 337 | register float rmin, rmax; |
---|
| 338 | if (t1 < t0) { |
---|
| 339 | rmin = t1; // min |
---|
| 340 | rmax = t0; // max |
---|
| 341 | } |
---|
| 342 | else { |
---|
| 343 | rmin = t0; // min |
---|
| 344 | rmax = t1; // max |
---|
| 345 | } |
---|
| 346 | assert(rmin <= rmax); |
---|
| 347 | if (tt_min < rmin) |
---|
| 348 | tt_min = rmin; // take maximum |
---|
| 349 | if (tt_max > rmax) |
---|
| 350 | tt_max = rmax; // take minimum |
---|
| 351 | if (tt_min > tt_max) |
---|
| 352 | return false; |
---|
| 353 | |
---|
| 354 | // coordinate y |
---|
| 355 | t0 = (pp[0].y - rayLoc.y) * rayinvDir.y; |
---|
| 356 | t1 = (pp[1].y - rayLoc.y) * rayinvDir.y; |
---|
| 357 | if (t1 < t0) { |
---|
| 358 | rmin = t1; // min |
---|
| 359 | rmax = t0; // max |
---|
| 360 | } |
---|
| 361 | else { |
---|
| 362 | rmin = t0; // min |
---|
| 363 | rmax = t1; // max |
---|
| 364 | } |
---|
| 365 | assert(rmin <= rmax); |
---|
| 366 | if (tt_min < rmin) |
---|
| 367 | tt_min = rmin; // take maximum |
---|
| 368 | if (tt_max > rmax) |
---|
| 369 | tt_max = rmax; // take minimum |
---|
| 370 | if (tt_min > tt_max) |
---|
| 371 | return false; |
---|
| 372 | |
---|
| 373 | // coordinate z |
---|
| 374 | t0 = (pp[0].z - rayLoc.z) * rayinvDir.z; |
---|
| 375 | t1 = (pp[1].z - rayLoc.z) * rayinvDir.z; |
---|
| 376 | if (t1 < t0) { |
---|
| 377 | rmin = t1; // min |
---|
| 378 | rmax = t0; // max |
---|
| 379 | } |
---|
| 380 | else { |
---|
| 381 | rmin = t0; // min |
---|
| 382 | rmax = t1; // max |
---|
| 383 | } |
---|
| 384 | assert(rmin <= rmax); |
---|
| 385 | if (tt_min < rmin) |
---|
| 386 | tt_min = rmin; // take maximum |
---|
| 387 | if (tt_max > rmax) |
---|
| 388 | tt_max = rmax; // take minimum |
---|
| 389 | if ((tt_min > tt_max) || |
---|
| 390 | (tt_max < 0.f) ) |
---|
| 391 | return false; // not intersected |
---|
| 392 | |
---|
| 393 | // yes, intersected, update tmin and tmax for current node |
---|
| 394 | tmin = tt_min; |
---|
| 395 | tmax = tt_max; |
---|
| 396 | return true; |
---|
| 397 | } |
---|
| 398 | #endif |
---|
| 399 | |
---|
| 400 | // ------------------------------------------------------------- |
---|
| 401 | #if 0 |
---|
| 402 | // The implementation VI |
---|
| 403 | // The code follows the article in SIGGRAPH 2005 course notes, |
---|
| 404 | // page 59, likely by Peter Shirley and Boulos Solomon. |
---|
| 405 | |
---|
| 406 | INLINE |
---|
| 407 | float minf(const float a, const float b) { return (a < b) ? a : b;} |
---|
| 408 | INLINE |
---|
| 409 | float maxf(const float a, const float b) { return (a > b) ? a : b;} |
---|
| 410 | |
---|
| 411 | INLINE bool |
---|
| 412 | SBBox::RayIntersect(const CRay &ray, |
---|
| 413 | float &t0, |
---|
| 414 | float &t1) const |
---|
| 415 | { |
---|
| 416 | //assert(tmin <= tmax); |
---|
| 417 | const CVector3D &rayLoc = ray.GetLoc(); |
---|
| 418 | const CVector3D &rayinvDir = ray.GetInvertedDir(); |
---|
| 419 | |
---|
| 420 | // coordinate x |
---|
| 421 | float tmin = (pp[ray.GetSign(0)].x - rayLoc.x) * rayinvDir.x; |
---|
| 422 | float tmax = (pp[1-ray.GetSign(0)].x - rayLoc.x) * rayinvDir.x; |
---|
| 423 | float tymin = (pp[ray.GetSign(1)].y - rayLoc.y) * rayinvDir.y; |
---|
| 424 | float tymax = (pp[1-ray.GetSign(1)].y - rayLoc.y) * rayinvDir.y; |
---|
| 425 | if ( (tmin > tymax) || (tymin > tmax) ) |
---|
| 426 | return false; |
---|
| 427 | if (tymin > tmin) |
---|
| 428 | tmin = tymin; |
---|
| 429 | if (tymax < tmax) |
---|
| 430 | tmax = tymax; |
---|
| 431 | float tzmin = (pp[ray.GetSign(2)].z - rayLoc.z) * rayinvDir.z; |
---|
| 432 | float tzmax = (pp[1-ray.GetSign(2)].z - rayLoc.y) * rayinvDir.z; |
---|
| 433 | if ( (tmin > tzmax) || (tzmin > tmax) ) |
---|
| 434 | return false; |
---|
| 435 | if (tzmin > tmin) |
---|
| 436 | tmin = tzmin; |
---|
| 437 | if (tzmax < tmax) |
---|
| 438 | tmax = tzmax; |
---|
| 439 | return ( (tmin < t1) && (tmax >= 0.f) ); |
---|
| 440 | } |
---|
| 441 | #endif |
---|
| 442 | // End of ray-box intersections |
---|
| 443 | |
---|
| 444 | // ---------------------------------------------------------- |
---|
| 445 | |
---|
| 446 | } // namespace |
---|