40 #ifndef __VMML__RAY__HPP__
41 #define __VMML__RAY__HPP__
43 #include <vmmlib/vector.hpp>
47 template<
typename T >
class Ray
59 Ray(
const vec3& origin,
const vec3& direction )
61 , _direction (
vmml::normalize( direction ))
76 T
test(
const vec4& sphere )
const;
80 const vec3 _direction;
85 template<
typename T >
88 const vec3 center =
vec3(sphere.x(), sphere.y(), sphere.z());
89 const T radius = sphere.w();
91 const vec3 centerVec = center - _origin;
92 const T vecProjection = centerVec.dot(_direction);
94 const T sqDistance = centerVec.squared_length();
95 const T sqRadius = radius * radius;
98 if( vecProjection < 0 && sqDistance > sqRadius )
102 const T sqCenterToProj = sqDistance - vecProjection * vecProjection;
104 if( sqCenterToProj > sqRadius )
108 const T distSurface = sqrt( sqRadius - sqCenterToProj );
110 if(sqDistance > sqRadius)
111 return vecProjection - distSurface;
113 return vecProjection + distSurface;
T test(const vec4 &sphere) const
Ray-Sphere Intersection.
heavily inspired by boost::enable_if http://www.boost.org, file: boost/utility/enable_if.hpp, Copyright 2003 Jaakko Järvi, Jeremiah Willcock, Andrew Lumsdaine
Ray(const vec3 &origin, const vec3 &direction)
Constructor.