00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __TSPHERE_H__
00011 #define __TSPHERE_H__
00012
00013
00014
00015
00016 #include <Definitions.h>
00017
00018 #include <Memory/mmgr.h>
00019
00020
00021
00022
00023 #ifndef min
00024 #define min(x,y) ((x) < (y) ? (x) : (y))
00025 #endif
00026
00027 #ifndef max
00028 #define max(x,y) ((x) > (y) ? (x) : (y))
00029 #endif
00030
00031
00032
00033
00034 template<typename T>
00035 class TSphere
00036 {
00037
00038
00039
00040 public:
00041 TSphere() {}
00042 TSphere(CVector3f vCenter, T radius) : m_vCenter(vCenter), m_Radius(radius) {}
00043 TSphere(const TSphere &other) : m_vCenter(other.m_vCenter), m_Radius(other.m_Radius) {}
00044 const TSphere &operator =(const TSphere &other) {m_vCenter = other.m_vCenter; m_Radius = other.m_Radius; return *this;}
00045
00046 const TSphere<T> operator+(const TVector3<T> &vec) const {return TSphere<T>(m_vCenter + vec, m_Radius);}
00047 const TSphere<T> &operator+=(const TVector3<T> &vec) {m_Center += vec; return *this;}
00048
00049 const TSphere<T> operator*(const TVector3<T> &vec) const {return TSphere<T>(m_vCenter, m_Radius * max(vec.m_X, max(vec.m_Y, vec.m_Z)));}
00050 const TSphere<T> &operator*=(const TVector3<T> &vec) {m_Radius *= max(vec.m_X, max(vec.m_Y, vec.m_Z)); return *this;}
00051 const TSphere<T> operator*(const T &value) const {return TSphere<T>(m_vCenter, m_Radius * value);}
00052 const TSphere<T> &operator*=(const T &value) {m_Radius *= value; return *this;}
00053
00054 const TSphere<T> GetRotatedSphere(const CVector3f &vRotation) const;
00055 bool RayIntersection(const CVector3f &vRayOrigin, const CVector3f &vRayDir, float &fDistance) const;
00056 bool Intersects(const TSphere<T> &other) const;
00057
00058 void Log(word wTargetLog) const {LOG(wTargetLog, "Center: (%.2f, %.2f, %.2f), R: %.2f",
00059 (float) m_vCenter.m_X, (float) m_vCenter.m_Y, (float) m_vCenter.m_Z, (float) m_Radius);}
00060
00061
00062
00063
00064 public:
00065 TVector3<T> m_vCenter;
00066 T m_Radius;
00067
00068 };
00069
00070 typedef TSphere<float> CSpheref;
00071
00072 template<typename T>
00073 const TSphere<T> TSphere<T>::GetRotatedSphere(const CVector3f &vRotation) const
00074 {
00075 CMatrix44 mat;
00076 mat.RotationZXY(vRotation);
00077
00078 return TSphere<T>(mat * m_vCenter, m_Radius);
00079 }
00080
00081 template<typename T>
00082 bool TSphere<T>::RayIntersection(const CVector3f &vRayOrigin, const CVector3f &vRayDir, float &fDistance) const
00083 {
00084 CVector3f eo(m_vCenter - vRayOrigin);
00085
00086 float v = eo.Dot(vRayDir);
00087 float disc = m_Radius * m_Radius - (eo.Dot(eo) - v * v);
00088 if(disc < 0.0f)
00089 return false;
00090
00091 fDistance = v - sqrt(disc);
00092 return true;
00093
00094
00095
00096
00097
00098
00099
00100 }
00101
00102 template<typename T>
00103 bool TSphere<T>::Intersects(const TSphere<T> &other) const
00104 {
00105 return (other.m_vCenter - m_vCenter).Length() < other.m_Radius + m_Radius;
00106 }
00107
00108 #endif