CollisionDetectionAlgorithm.h

Go to the documentation of this file.
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 HalfSpaceTriangleMesh;
00056     class SphereTriangleMesh;
00057     class TriangleMeshTriangleMesh;
00058     virtual ~CollisionDetectionAlgorithm() {}
00072     virtual void processObjects
00073        (ContactSurfaceIndex index1, const ContactGeometry& object1, 
00074         const Transform& transform1,
00075         ContactSurfaceIndex index2, const ContactGeometry& object2, 
00076         const Transform& transform2, 
00077         Array_<Contact>& contacts) const = 0;
00085     static void registerAlgorithm(const std::string& type1, const std::string& type2, CollisionDetectionAlgorithm* algorithm);
00093     static CollisionDetectionAlgorithm* getAlgorithm(int typeIndex1, int typeIndex2);
00094 private:
00095     struct AlgorithmMap : public  std::map<std::pair<int, int>, CollisionDetectionAlgorithm*> {
00096         ~AlgorithmMap();
00097     };
00098 
00099     // static std::map<std::pair<int, int>, CollisionDetectionAlgorithm*> algorithmMap;
00100     static AlgorithmMap algorithmMap;
00101 };
00102 
00106 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::HalfSpaceSphere : public CollisionDetectionAlgorithm {
00107 public:
00108     virtual ~HalfSpaceSphere() {}
00109     void processObjects
00110        (ContactSurfaceIndex index1, const ContactGeometry& object1, 
00111         const Transform& transform1,
00112         ContactSurfaceIndex index2, const ContactGeometry& object2, 
00113         const Transform& transform2, 
00114         Array_<Contact>& contacts) const;
00115 };
00116 
00120 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::SphereSphere : public CollisionDetectionAlgorithm {
00121 public:
00122     virtual ~SphereSphere() {}
00123     void processObjects
00124        (ContactSurfaceIndex index1, const ContactGeometry& object1, 
00125         const Transform& transform1,
00126         ContactSurfaceIndex index2, const ContactGeometry& object2, 
00127         const Transform& transform2, 
00128         Array_<Contact>& contacts) const;
00129 };
00130 
00134 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::HalfSpaceTriangleMesh : public CollisionDetectionAlgorithm {
00135 public:
00136     virtual ~HalfSpaceTriangleMesh() {}
00137     void processObjects
00138        (ContactSurfaceIndex index1, const ContactGeometry& object1, 
00139         const Transform& transform1,
00140         ContactSurfaceIndex index2, const ContactGeometry& object2, 
00141         const Transform& transform2, 
00142         Array_<Contact>& contacts) const;
00143 private:
00144     void processBox(const ContactGeometry::TriangleMesh& mesh, const ContactGeometry::TriangleMesh::OBBTreeNode& node,
00145             const Transform& transform, const Vec3& axisDir, Real xoffset, std::set<int>& insideFaces) const;
00146     void addAllTriangles(const ContactGeometry::TriangleMesh::OBBTreeNode& node, std::set<int>& insideFaces) const;
00147 };
00148 
00152 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::SphereTriangleMesh : public CollisionDetectionAlgorithm {
00153 public:
00154     virtual ~SphereTriangleMesh() {}
00155     void processObjects
00156        (ContactSurfaceIndex index1, const ContactGeometry& object1, 
00157         const Transform& transform1,
00158         ContactSurfaceIndex index2, const ContactGeometry& object2, 
00159         const Transform& transform2, 
00160         Array_<Contact>& contacts) const;
00161 private:
00162     void processBox(const Vec3& center, Real radius2, const ContactGeometry::TriangleMesh& mesh, const ContactGeometry::TriangleMesh::OBBTreeNode& node,
00163             std::set<int>& insideFaces) const;
00164 };
00165 
00169 class SimTK_SIMBODY_EXPORT CollisionDetectionAlgorithm::TriangleMeshTriangleMesh : public CollisionDetectionAlgorithm {
00170 public:
00171     virtual ~TriangleMeshTriangleMesh() {}
00172     void processObjects
00173        (ContactSurfaceIndex index1, const ContactGeometry& object1, 
00174         const Transform& transform1,
00175         ContactSurfaceIndex index2, const ContactGeometry& object2, 
00176         const Transform& transform2, 
00177         Array_<Contact>& contacts) const;
00178 private:
00179     void processNodes(const ContactGeometry::TriangleMesh& mesh1, const ContactGeometry::TriangleMesh& mesh2,
00180             const ContactGeometry::TriangleMesh::OBBTreeNode& node1, const ContactGeometry::TriangleMesh::OBBTreeNode& node2,
00181             const OrientedBoundingBox& node2Bounds, const Transform& transform, std::set<int>& triangles1, std::set<int>& triangles2) const;
00182     void findInsideTriangles(const ContactGeometry::TriangleMesh& mesh, const ContactGeometry::TriangleMesh& otherMesh,
00183         const Transform& transform, std::set<int>& triangles) const;
00184     void tagFaces(const ContactGeometry::TriangleMesh& mesh, Array_<int>& faceType, std::set<int>& triangles, int index, int depth) const;
00185     static const int OUTSIDE = -1;
00186     static const int UNKNOWN = 0;
00187     static const int BOUNDARY = 1;
00188     static const int INSIDE = 2;
00189 };
00190 
00191 } // namespace SimTK
00192 
00193 #endif // SimTK_SIMBODY_COLLISION_DETECTION_ALGORITHM_H_

Generated on Thu Aug 12 16:37:05 2010 for SimTKcore by  doxygen 1.6.1