Implementation derived from Wild Magic (Version 2) Software Library, available from http://www.geometrictools.com/Downloads/WildMagic2p5.zip under free license
| 369 | /// Implementation derived from Wild Magic (Version 2) Software Library, available |
| 370 | /// from http://www.geometrictools.com/Downloads/WildMagic2p5.zip under free license |
| 371 | int SpherePrimitiveEvaluator::intersectionPoints( const Imath::V3f &origin, const Imath::V3f &direction, |
| 372 | std::vector<PrimitiveEvaluator::ResultPtr> &results, float maxDistance ) const |
| 373 | { |
| 374 | results.clear(); |
| 375 | |
| 376 | Imath::V3f dir = direction.normalized(); |
| 377 | (void)direction; |
| 378 | float a0 = origin.dot(origin) - m_sphere->radius() * m_sphere->radius(); |
| 379 | float a1 = dir.dot( origin ); |
| 380 | float discr = a1 * a1 - a0; |
| 381 | |
| 382 | if (discr < 0.0) |
| 383 | { |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | if ( discr >= std::numeric_limits<float>::epsilon() ) |
| 388 | { |
| 389 | float root = sqrt( discr ); |
| 390 | float t0 = -a1 - root; |
| 391 | |
| 392 | if ( t0 >= 0.0 ) |
| 393 | { |
| 394 | Imath::V3f p0 = origin + t0 * dir; |
| 395 | if ( (origin - p0).length() < maxDistance ) |
| 396 | { |
| 397 | ResultPtr r = boost::static_pointer_cast< Result > ( createResult() ); |
| 398 | r->m_p = p0; |
| 399 | |
| 400 | results.push_back( r ); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | float t1 = -a1 + root; |
| 405 | if ( t1 >= 0.0 ) |
| 406 | { |
| 407 | Imath::V3f p1 = origin + t1 * dir; |
| 408 | if ( (origin - p1).length() < maxDistance ) |
| 409 | { |
| 410 | ResultPtr r = boost::static_pointer_cast< Result > ( createResult() ); |
| 411 | r->m_p = p1; |
| 412 | |
| 413 | results.push_back( r ); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | float t = -a1; |
| 420 | |
| 421 | if ( t >= 0.0 ) |
| 422 | { |
| 423 | Imath::V3f p = origin + t * dir; |
| 424 | if ( (origin - p).length() < maxDistance ) |
| 425 | { |
| 426 | ResultPtr r = boost::static_pointer_cast< Result > ( createResult() ); |
| 427 | r->m_p = p; |
| 428 |