ContactGeometry.h
Go to the documentation of this file.00001 #ifndef SimTK_SIMBODY_CONTACT_GEOMETRY_H_
00002 #define SimTK_SIMBODY_CONTACT_GEOMETRY_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "SimTKcommon.h"
00037
00038 #include "simbody/internal/common.h"
00039 #include "simbody/internal/OrientedBoundingBox.h"
00040
00041 #include <cassert>
00042
00043 namespace SimTK {
00044
00045 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ContactGeometryTypeId);
00046
00047 class ContactGeometryImpl;
00048 class OBBTreeNodeImpl;
00049
00054 class SimTK_SIMBODY_EXPORT ContactGeometry {
00055 public:
00056 class HalfSpace;
00057 class Sphere;
00058 class TriangleMesh;
00059 class HalfSpaceImpl;
00060 class SphereImpl;
00061 class TriangleMeshImpl;
00062 ContactGeometry() : impl(0) {
00063 }
00064 ContactGeometry(const ContactGeometry& src);
00065 explicit ContactGeometry(ContactGeometryImpl* impl);
00066 virtual ~ContactGeometry();
00076 Vec3 findNearestPoint(const Vec3& position, bool& inside, UnitVec3& normal) const;
00088 bool intersectsRay(const Vec3& origin, const UnitVec3& direction, Real& distance, UnitVec3& normal) const;
00095 void getBoundingSphere(Vec3& center, Real& radius) const;
00096 bool isOwnerHandle() const;
00097 bool isEmptyHandle() const;
00098
00102 ContactGeometry& operator=(const ContactGeometry& src);
00103 bool hasImpl() const {
00104 return impl != 0;
00105 }
00106 const ContactGeometryImpl& getImpl() const {
00107 assert(impl);
00108 return *impl;
00109 }
00110 ContactGeometryImpl& updImpl() {
00111 assert(impl);
00112 return *impl;
00113 }
00118 const std::string& getType() const;
00123 int getTypeIndex() const;
00124
00125 ContactGeometryTypeId getTypeId() const;
00126
00127
00128 protected:
00129 ContactGeometryImpl* impl;
00130 };
00131
00136 class SimTK_SIMBODY_EXPORT ContactGeometry::HalfSpace : public ContactGeometry {
00137 public:
00138 HalfSpace();
00139
00141 static bool isInstance(const ContactGeometry& geo)
00142 { return geo.getTypeId()==classTypeId(); }
00144 static const HalfSpace& getAs(const ContactGeometry& geo)
00145 { assert(isInstance(geo)); return static_cast<const HalfSpace&>(geo); }
00147 static HalfSpace& updAs(ContactGeometry& geo)
00148 { assert(isInstance(geo)); return static_cast<HalfSpace&>(geo); }
00149
00151 static ContactGeometryTypeId classTypeId();
00152 };
00153
00157 class SimTK_SIMBODY_EXPORT ContactGeometry::Sphere : public ContactGeometry {
00158 public:
00159 explicit Sphere(Real radius);
00160 Real getRadius() const;
00161 void setRadius(Real radius);
00162
00164 static bool isInstance(const ContactGeometry& geo)
00165 { return geo.getTypeId()==classTypeId(); }
00167 static const Sphere& getAs(const ContactGeometry& geo)
00168 { assert(isInstance(geo)); return static_cast<const Sphere&>(geo); }
00170 static Sphere& updAs(ContactGeometry& geo)
00171 { assert(isInstance(geo)); return static_cast<Sphere&>(geo); }
00172
00174 static ContactGeometryTypeId classTypeId();
00175
00176 const SphereImpl& getImpl() const;
00177 SphereImpl& updImpl();
00178 };
00179
00200 class SimTK_SIMBODY_EXPORT ContactGeometry::TriangleMesh : public ContactGeometry {
00201 public:
00202 class OBBTreeNode;
00214 TriangleMesh(const ArrayViewConst_<Vec3>& vertices, const ArrayViewConst_<int>& faceIndices, bool smooth=false);
00224 explicit TriangleMesh(const PolygonalMesh& mesh, bool smooth=false);
00228 int getNumEdges() const;
00232 int getNumFaces() const;
00236 int getNumVertices() const;
00243 const Vec3& getVertexPosition(int index) const;
00252 int getFaceEdge(int face, int edge) const;
00260 int getFaceVertex(int face, int vertex) const;
00268 int getEdgeFace(int edge, int face) const;
00276 int getEdgeVertex(int edge, int vertex) const;
00283 void findVertexEdges(int vertex, Array_<int>& edges) const;
00289 const UnitVec3& getFaceNormal(int face) const;
00295 Real getFaceArea(int face) const;
00303 Vec3 findPoint(int face, const Vec2& uv) const;
00312 Vec3 findCentroid(int face) const;
00319 UnitVec3 findNormalAtPoint(int face, const Vec2& uv) const;
00329 Vec3 findNearestPoint(const Vec3& position, bool& inside, UnitVec3& normal) const;
00341 Vec3 findNearestPoint(const Vec3& position, bool& inside, int& face, Vec2& uv) const;
00353 bool intersectsRay(const Vec3& origin, const UnitVec3& direction, Real& distance, UnitVec3& normal) const;
00367 bool intersectsRay(const Vec3& origin, const UnitVec3& direction, Real& distance, int& face, Vec2& uv) const;
00371 OBBTreeNode getOBBTreeNode() const;
00372
00378 PolygonalMesh createPolygonalMesh() const;
00379
00381 static bool isInstance(const ContactGeometry& geo)
00382 { return geo.getTypeId()==classTypeId(); }
00384 static const TriangleMesh& getAs(const ContactGeometry& geo)
00385 { assert(isInstance(geo)); return static_cast<const TriangleMesh&>(geo); }
00387 static TriangleMesh& updAs(ContactGeometry& geo)
00388 { assert(isInstance(geo)); return static_cast<TriangleMesh&>(geo); }
00389
00391 static ContactGeometryTypeId classTypeId();
00392
00393 const TriangleMeshImpl& getImpl() const;
00394 TriangleMeshImpl& updImpl();
00395 };
00396
00404 class SimTK_SIMBODY_EXPORT ContactGeometry::TriangleMesh::OBBTreeNode {
00405 public:
00406 OBBTreeNode(const OBBTreeNodeImpl& impl);
00410 const OrientedBoundingBox& getBounds() const;
00414 bool isLeafNode() const;
00418 const OBBTreeNode getFirstChildNode() const;
00422 const OBBTreeNode getSecondChildNode() const;
00426 const Array_<int>& getTriangles() const;
00431 int getNumTriangles() const;
00432 private:
00433 const OBBTreeNodeImpl* impl;
00434 };
00435
00436 }
00437
00438 #endif // SimTK_SIMBODY_CONTACT_GEOMETRY_H_