00001 /**************************************************************************** 00002 Copyright (C)1996 David Jung <opensim@pobox.com> 00003 00004 This program/file is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. (http://www.gnu.org) 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 $Id: GJKCollisionModel 1031 2004-02-11 20:46:36Z jungd $ 00019 $Revision: 1.4 $ 00020 $Date: 2004-02-11 15:46:36 -0500 (Wed, 11 Feb 2004) $ 00021 $Author: jungd $ 00022 00023 ****************************************************************************/ 00024 00025 #ifndef _PHYSICS_GJKCOLLISIONMODEL_ 00026 #define _PHYSICS_GJKCOLLISIONMODEL_ 00027 00028 #include <physics/physics> 00029 #include <physics/CollisionModel> 00030 00031 #include <physics/Box> 00032 #include <physics/Sphere> 00033 #include <physics/Polyhedron> 00034 00035 #include <gfx/TriangleContainer> 00036 00037 00038 namespace physics { 00039 00040 00041 class GJKCollisionModel : public CollisionModel 00042 { 00043 public: 00044 GJKCollisionModel(const GJKCollisionModel& cm); 00045 GJKCollisionModel(const gfx::TriangleContainer& triangles); 00046 GJKCollisionModel(ref<const Shape> shape); 00047 virtual ~GJKCollisionModel(); 00048 00049 virtual String className() const { return String("GJKCollisionModel"); } 00050 virtual Object& clone() const { return *NewNamedObj(className()) GJKCollisionModel(*this); } 00051 00052 virtual CollisionModelType getType() const { return GJKModel; } 00053 00054 00055 class GJKModelState : public ModelState 00056 { 00057 public: 00058 GJKModelState() {} 00059 GJKModelState(ref<const Polyhedron::Vertex> v) 00060 : lastSupport(v) {} 00061 virtual ~GJKModelState() {} 00062 00063 virtual String className() const { return String("GJKCollisionModel::GJKModelState"); } 00064 virtual base::Object& clone() const { return *NewNamedObj(className()) GJKModelState(); } 00065 00066 ref<const Polyhedron::Vertex> lastSupport; /// last support vertex of the GJK algorithm 00067 Point3 lastSupportPoint; /// actual local coords. of lastSupport 00068 }; 00069 00070 00071 protected: 00072 ref<const Shape> shape; // model is based on this shape 00073 00074 00075 // GJK algorithm Support mapping functions 00076 void initSupportFunction(); 00077 base::Point3 support(ref<GJKModelState> s, const base::Vector3& v) const 00078 { 00079 Assert(supportFunction); 00080 s->lastSupportPoint = (*supportFunction)(s,v); 00081 return s->lastSupportPoint; 00082 } 00083 00084 struct SupportFunction : public std::binary_function<ref<GJKModelState>, const base::Vector3&, base::Point3> 00085 { 00086 SupportFunction() {} 00087 virtual ~SupportFunction() {} 00088 virtual Point3 operator()(ref<GJKModelState> s, const Vector3& v) const = 0; 00089 }; 00090 00091 00092 struct BoxSupport : public SupportFunction 00093 { 00094 BoxSupport(ref<const Shape> s) : box(base::dynamic_cast_ref<const Box>(s)) {} 00095 virtual Point3 operator()(ref<GJKModelState> s, const Vector3& v) const; 00096 ref<const Box> box; 00097 }; 00098 00099 struct SphereSupport : public SupportFunction 00100 { 00101 SphereSupport(ref<const Shape> s) : sphere(base::dynamic_cast_ref<const Sphere>(s)) {} 00102 virtual Point3 operator()(ref<GJKModelState> s, const Vector3& v) const; 00103 ref<const Sphere> sphere; 00104 }; 00105 00106 struct PolyhedronSupport : public SupportFunction 00107 { 00108 PolyhedronSupport(ref<const Shape> s) : poly(base::dynamic_cast_ref<const Polyhedron>(s)) {} 00109 virtual Point3 operator()(ref<GJKModelState> s, const Vector3& v) const; 00110 ref<const Polyhedron> poly; 00111 }; 00112 00113 00114 SupportFunction* supportFunction; // instantiated support function from above 00115 00116 00117 friend class GJKCollisionDetector; 00118 }; 00119 00120 00121 } // physics 00122 00123 #endif