Simbody
|
00001 #ifndef SimTK_SIMBODY_COLLISION_DETECTION_ALGORITHM_H_ 00002 #define SimTK_SIMBODY_COLLISION_DETECTION_ALGORITHM_H_ 00003 00004 /* -------------------------------------------------------------------------- * 00005 * SimTK Core: SimTK Simbody(tm) * 00006 * -------------------------------------------------------------------------- * 00007 * This is part of the SimTK Core biosimulation toolkit originating from * 00008 * Simbios, the NIH National Center for Physics-Based Simulation of * 00009 * Biological Structures at Stanford, funded under the NIH Roadmap for * 00010 * Medical Research, grant U54 GM072970. See https://simtk.org. * 00011 * * 00012 * Portions copyright (c) 2008 Stanford University and the Authors. * 00013 * Authors: Peter Eastman * 00014 * Contributors: * 00015 * * 00016 * Permission is hereby granted, free of charge, to any person obtaining a * 00017 * copy of this software and associated documentation files (the "Software"), * 00018 * to deal in the Software without restriction, including without limitation * 00019 * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 00020 * and/or sell copies of the Software, and to permit persons to whom the * 00021 * Software is furnished to do so, subject to the following conditions: * 00022 * * 00023 * The above copyright notice and this permission notice shall be included in * 00024 * all copies or substantial portions of the Software. * 00025 * * 00026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 00027 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 00028 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 00029 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 00030 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 00031 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 00032 * USE OR OTHER DEALINGS IN THE SOFTWARE. * 00033 * -------------------------------------------------------------------------- */ 00034 00035 #include "SimTKcommon.h" 00036 #include "simbody/internal/common.h" 00037 #include "simbody/internal/ContactGeometry.h" 00038 #include "simbody/internal/Contact.h" 00039 #include <map> 00040 00041 namespace SimTK { 00042 00051 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm { 00052 public: 00053 class HalfSpaceSphere; 00054 class SphereSphere; 00055 class HalfSpaceEllipsoid; 00056 class HalfSpaceTriangleMesh; 00057 class SphereTriangleMesh; 00058 class TriangleMeshTriangleMesh; 00059 virtual ~CollisionDetectionAlgorithm() {} 00073 virtual void processObjects 00074 (ContactSurfaceIndex index1, const ContactGeometry& object1, 00075 const Transform& transform1, 00076 ContactSurfaceIndex index2, const ContactGeometry& object2, 00077 const Transform& transform2, 00078 Array_<Contact>& contacts) const = 0; 00086 static void registerAlgorithm(const std::string& type1, const std::string& type2, CollisionDetectionAlgorithm* algorithm); 00094 static CollisionDetectionAlgorithm* getAlgorithm(int typeIndex1, int typeIndex2); 00095 private: 00096 struct AlgorithmMap : public std::map<std::pair<int, int>, CollisionDetectionAlgorithm*> { 00097 ~AlgorithmMap(); 00098 }; 00099 00100 // static std::map<std::pair<int, int>, CollisionDetectionAlgorithm*> algorithmMap; 00101 static AlgorithmMap algorithmMap; 00102 }; 00103 00107 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::HalfSpaceSphere : public CollisionDetectionAlgorithm { 00108 public: 00109 virtual ~HalfSpaceSphere() {} 00110 void processObjects 00111 (ContactSurfaceIndex index1, const ContactGeometry& object1, 00112 const Transform& transform1, 00113 ContactSurfaceIndex index2, const ContactGeometry& object2, 00114 const Transform& transform2, 00115 Array_<Contact>& contacts) const; 00116 }; 00117 00121 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::HalfSpaceEllipsoid : public CollisionDetectionAlgorithm { 00122 public: 00123 virtual ~HalfSpaceEllipsoid() {} 00124 void processObjects 00125 (ContactSurfaceIndex index1, const ContactGeometry& object1, 00126 const Transform& transform1, 00127 ContactSurfaceIndex index2, const ContactGeometry& object2, 00128 const Transform& transform2, 00129 Array_<Contact>& contacts) const; 00130 }; 00131 00135 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::SphereSphere : public CollisionDetectionAlgorithm { 00136 public: 00137 virtual ~SphereSphere() {} 00138 void processObjects 00139 (ContactSurfaceIndex index1, const ContactGeometry& object1, 00140 const Transform& transform1, 00141 ContactSurfaceIndex index2, const ContactGeometry& object2, 00142 const Transform& transform2, 00143 Array_<Contact>& contacts) const; 00144 }; 00145 00149 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::HalfSpaceTriangleMesh : public CollisionDetectionAlgorithm { 00150 public: 00151 virtual ~HalfSpaceTriangleMesh() {} 00152 void processObjects 00153 (ContactSurfaceIndex index1, const ContactGeometry& object1, 00154 const Transform& transform1, 00155 ContactSurfaceIndex index2, const ContactGeometry& object2, 00156 const Transform& transform2, 00157 Array_<Contact>& contacts) const; 00158 private: 00159 void processBox(const ContactGeometry::TriangleMesh& mesh, const ContactGeometry::TriangleMesh::OBBTreeNode& node, 00160 const Transform& transform, const Vec3& axisDir, Real xoffset, std::set<int>& insideFaces) const; 00161 void addAllTriangles(const ContactGeometry::TriangleMesh::OBBTreeNode& node, std::set<int>& insideFaces) const; 00162 }; 00163 00167 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::SphereTriangleMesh : public CollisionDetectionAlgorithm { 00168 public: 00169 virtual ~SphereTriangleMesh() {} 00170 void processObjects 00171 (ContactSurfaceIndex index1, const ContactGeometry& object1, 00172 const Transform& transform1, 00173 ContactSurfaceIndex index2, const ContactGeometry& object2, 00174 const Transform& transform2, 00175 Array_<Contact>& contacts) const; 00176 private: 00177 void processBox(const Vec3& center, Real radius2, const ContactGeometry::TriangleMesh& mesh, const ContactGeometry::TriangleMesh::OBBTreeNode& node, 00178 std::set<int>& insideFaces) const; 00179 }; 00180 00184 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::TriangleMeshTriangleMesh : public CollisionDetectionAlgorithm { 00185 public: 00186 virtual ~TriangleMeshTriangleMesh() {} 00187 void processObjects 00188 (ContactSurfaceIndex index1, const ContactGeometry& object1, 00189 const Transform& transform1, 00190 ContactSurfaceIndex index2, const ContactGeometry& object2, 00191 const Transform& transform2, 00192 Array_<Contact>& contacts) const; 00193 private: 00194 void processNodes(const ContactGeometry::TriangleMesh& mesh1, const ContactGeometry::TriangleMesh& mesh2, 00195 const ContactGeometry::TriangleMesh::OBBTreeNode& node1, const ContactGeometry::TriangleMesh::OBBTreeNode& node2, 00196 const OrientedBoundingBox& node2Bounds, const Transform& transform, std::set<int>& triangles1, std::set<int>& triangles2) const; 00197 void findInsideTriangles(const ContactGeometry::TriangleMesh& mesh, const ContactGeometry::TriangleMesh& otherMesh, 00198 const Transform& transform, std::set<int>& triangles) const; 00199 void tagFaces(const ContactGeometry::TriangleMesh& mesh, Array_<int>& faceType, std::set<int>& triangles, int index, int depth) const; 00200 static const int OUTSIDE = -1; 00201 static const int UNKNOWN = 0; 00202 static const int BOUNDARY = 1; 00203 static const int INSIDE = 2; 00204 }; 00205 00206 } // namespace SimTK 00207 00208 #endif // SimTK_SIMBODY_COLLISION_DETECTION_ALGORITHM_H_