size_t closestPointToRay(const V3f* points, size_t nPoints, const V3f& rayOrigin, const V3f& rayDirection, double longitudinalScale, double* distance) { const V3f T = rayDirection.normalized(); const double f = longitudinalScale*longitudinalScale - 1; size_t nearestIdx = -1; double nearestDist2 = DBL_MAX; for(size_t i = 0; i < nPoints; ++i) { const V3f v = points[i] - rayOrigin; // vector from ray origin to point const double a = v.dot(T); // distance along ray to point of closest approach to test point const double r2 = v.length2() + f*a*a; if(r2 < nearestDist2) { // new closest angle to axis nearestDist2 = r2; nearestIdx = i; } } if(distance) { if(nPoints == 0) *distance = DBL_MAX; else *distance = sqrt(nearestDist2); } return nearestIdx; }
bool SpherePrimitiveEvaluator::closestPoint( const V3f &p, PrimitiveEvaluator::Result *result ) const { assert( dynamic_cast<Result *>( result ) ); Result *sr = static_cast<Result *>( result ); sr->m_p = p.normalized() * m_sphere->radius(); return true; }
int ImagePrimitiveEvaluator::intersectionPoints( const V3f &origin, const V3f &direction, std::vector<PrimitiveEvaluator::ResultPtr> &results, float maxDistance ) const { results.clear(); V3f hitPoint; Box3f bound = m_image->bound(); bool hit = boxIntersects( bound , origin, direction.normalized(), hitPoint ); if ( hit ) { if ( ( origin - hitPoint ).length2() < maxDistance * maxDistance ) { ResultPtr result = staticPointerCast< Result >( createResult() ); result->m_p = hitPoint; results.push_back( result ); } } return results.size(); }