00001 #ifndef __bounding_sphere_hpp__
00002 #define __bounding_sphere_hpp__
00003
00004 #include <crbn/basic/vec3.hpp>
00005 #include <crbn/basic/mat4.hpp>
00006
00007
00008 struct bsphere{
00009 vec3 center;
00010 float radius;
00011
00012 bsphere() : center(0.0, 0.0 , 0.0), radius(0.0) {}
00013 bsphere(const vec3& c, const float& r) : center(c), radius(r) {}
00014 bsphere(const bsphere& b) : center(b.center), radius(b.radius) {}
00015 };
00016
00017
00018 inline bool inside( bsphere& b, vec3& v ) {
00019 return (vdistance(v, b.center) <= b.radius);
00020 }
00021
00022
00023 inline void merge( bsphere& a, bsphere& b, bsphere& c ) {
00024 float bc = vdistance(b.center, c.center);
00025 a.radius = 0.5 * (b.radius + c.radius + bc);
00026
00027 vsub(a.center, c.center, b.center);
00028 vmul(a.center, (c.radius - b.radius) / bc);
00029 vadd(a.center, c.center);
00030 vadd(a.center, c.center);
00031 vmul(a.center, 0.5);
00032 }
00033
00034
00035 inline void merge( bsphere& a, bsphere& b ) {
00036 bsphere c(a);
00037 merge(a, b, c);
00038 }
00039
00040
00041 inline void transform( bsphere& a, bsphere& b, mat4& m ) {
00042
00043 vec3 v;
00044 vec3 pole(b.center);
00045 pole.z += b.radius;
00046
00047
00048
00049 mmulp( v, m, pole);
00050 mmulp( a.center, m, b.center);
00051
00052
00053 a.radius = vdistance(v, a.center);
00054 }
00055
00056
00057 inline bool overlap( bsphere& a, bsphere& b ) {
00058 return (vdistance(a.center, b.center) <= (a.radius + b.radius));
00059 }
00060
00061 #endif // __bounding_sphere_hpp__