00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "bounds.h"
00022
00023 #include <cstdio>
00024
00025 using namespace salt;
00026
00027 void AABB3::TransformBy(Matrix& matrix)
00028 {
00029 AABB3 bb;
00030
00031 Vector3f v(minVec);
00032 Vector3f w(maxVec);
00033
00034 bb.Encapsulate(matrix.Transform(Vector3f(v.x(),v.y(),v.z())));
00035 bb.Encapsulate(matrix.Transform(Vector3f(w.x(),v.y(),v.z())));
00036 bb.Encapsulate(matrix.Transform(Vector3f(v.x(),w.y(),v.z())));
00037 bb.Encapsulate(matrix.Transform(Vector3f(w.x(),w.y(),v.z())));
00038 bb.Encapsulate(matrix.Transform(Vector3f(v.x(),v.y(),w.z())));
00039 bb.Encapsulate(matrix.Transform(Vector3f(w.x(),v.y(),w.z())));
00040 bb.Encapsulate(matrix.Transform(Vector3f(v.x(),w.y(),w.z())));
00041 bb.Encapsulate(matrix.Transform(Vector3f(w.x(),w.y(),w.z())));
00042
00043 minVec.Set(bb.minVec);
00044 maxVec.Set(bb.maxVec);
00045 }
00046
00047 void BoundingSphere::Encapsulate(const Vector3f &v)
00048 {
00049
00050 Vector3f diff = v - center;
00051 float dist = diff.Dot(diff);
00052
00053 if (dist > radiusSq)
00054 {
00055 Vector3f diff2 = diff.Normalized() * radius;
00056 Vector3f delta = 0.5f * (diff - diff2);
00057 center += delta;
00058 radius += delta.Length();
00059 radiusSq = radius*radius;
00060 }
00061 }
00062
00063
00064
00065 bool BoundingSphere::Intersects(const AABB3 &b) const
00066 {
00067 float distance = 0.0f;
00068
00069 for (int t=0; t<3; t++)
00070 {
00071 if (center[t] < b.minVec[t])
00072 {
00073 distance += (center[t] - b.minVec[t]) * (center[t] - b.minVec[t]);
00074 if (distance>radiusSq) return false;
00075 }
00076 else
00077 if (center[t]>b.maxVec[t])
00078 {
00079 distance+=(center[t] - b.maxVec[t]) * (center[t] - b.maxVec[t]);
00080 if (distance>radiusSq) return false;
00081 }
00082 }
00083
00084 return true;
00085 }
00086
00087
00088
00089 bool BoundingSphere::Contains(const AABB3 &b) const
00090 {
00091 float distance = 0.0f;
00092
00093 for (int t=0; t<3; t++)
00094 {
00095 if (center[t]<b.maxVec[t])
00096 distance+=(center[t] - b.maxVec[t]) * (center[t] - b.maxVec[t]);
00097 else
00098 if (center[t]>b.minVec[t])
00099 distance+=(center[t] - b.minVec[t]) * (center[t] - b.minVec[t]);
00100
00101 if (distance>radiusSq) return false;
00102 }
00103 return true;
00104 }