00001 00002 00003 00004 00005 00006 #include "sphere.h" 00007 #include <vector> 00008 00009 using std::vector; 00010 using std::string; 00011 00012 // This actually makes the static init work. 00013 Sphere::StaticInit Sphere::m_init; 00014 00015 //{{{ 00016 Sphere::~Sphere() {} 00017 //}}} 00018 00019 //{{{ 00020 bool Sphere::collides_with(const Ray &ray, double &t) const { 00021 bool rv = false; 00022 Point3D v = ray.origin() - m_center; 00023 00024 double b = 2 * dot_product(v, ray.direction()); 00025 double b_squared = b * b; 00026 double _ac = 4 * (dot_product(v, v) - m_radius * m_radius); 00027 00028 if ( b_squared > _ac ) { 00029 00030 double discriminant = sqrtf( b_squared - _ac ); 00031 double t2 = (-b + discriminant) * 0.5; 00032 00033 if ( t2 > 0.0 ) { 00034 double t1 = (-b - discriminant) * 0.5; 00035 if ( t1 < t ) { 00036 t = t1; 00037 rv = true; 00038 } 00039 } 00040 } 00041 00042 return rv; 00043 } 00044 //}}} 00045 //{{{ 00046 Ray Sphere::get_normal(const Point3D &p) const { 00047 return Ray(p, p - m_center); 00048 } 00049 //}}}