Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

bounds.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++ -*-
00002    
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2003 Koblenz University
00006    $Id: bounds.cpp,v 1.2 2003/05/19 21:37:49 fruit Exp $
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; version 2 of the License.
00011   
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016  
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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     // TODO : check if this is correct
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 }

Generated on Thu Apr 6 15:25:37 2006 for rcssserver3d by  doxygen 1.4.4