/** For reference, see http://realtimecollisiondetection.net/blog/?p=20 . */ Sphere Sphere::FitThroughPoints(const vec &a, const vec &b, const vec &c) { Sphere sphere; vec ab = b-a; vec ac = c-a; float s, t; bool success = FitSphereThroughPoints(ab, ac, s, t); if (!success) { LOGW("Sphere::FitThroughPoints(a,b,c) failed! The three input points are collinear!"); sphere.SetDegenerate(); return sphere; } const vec p = s*ab + t*ac; // In our translated coordinate space, the origin lies on the sphere, so the distance of p from origin // gives the radius of the sphere. sphere.r = p.Length(); // Translate back to original coordinate space. sphere.pos = a + p; return sphere; }
/** For reference, see http://realtimecollisiondetection.net/blog/?p=20 . */ Sphere Sphere::FitThroughPoints(const vec &a, const vec &b, const vec &c, const vec &d) { Sphere sphere; float s,t,u; const vec ab = b-a; const vec ac = c-a; const vec ad = d-a; bool success = FitSphereThroughPoints(ab, ac, ad, s, t, u); if (success) { const vec center = s*ab + t*ac + u*ad; sphere.r = center.Length(); sphere.pos = a + center; } else { LOGW("Sphere::FitThroughPoints through four points failed! The points lie on the same plane!"); sphere.SetDegenerate(); } return sphere; }
vec Sphere::ExtremePoint(const vec &direction) const { float len = direction.Length(); assume(len > 0.f); return pos + direction * (r / len); }