reactphysics3d/src/collision/shapes/ConvexMeshShape.h

241 lines
9.8 KiB
C
Raw Normal View History

/********************************************************************************
2015-02-15 20:56:45 +00:00
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
2016-04-11 18:15:20 +00:00
* Copyright (c) 2010-2016 Daniel Chappuis *
2013-07-15 17:10:30 +00:00
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
#ifndef REACTPHYSICS3D_CONVEX_MESH_SHAPE_H
#define REACTPHYSICS3D_CONVEX_MESH_SHAPE_H
// Libraries
#include "ConvexPolyhedronShape.h"
2014-08-07 19:38:31 +00:00
#include "engine/CollisionWorld.h"
#include "mathematics/mathematics.h"
#include "collision/TriangleMesh.h"
#include "collision/PolyhedronMesh.h"
2014-08-07 19:38:31 +00:00
#include "collision/narrowphase/GJK/GJKAlgorithm.h"
2013-07-15 17:10:30 +00:00
#include <vector>
#include <set>
#include <map>
/// ReactPhysics3D namespace
namespace reactphysics3d {
2014-08-01 10:36:32 +00:00
// Declaration
class CollisionWorld;
2013-07-15 17:10:30 +00:00
// Class ConvexMeshShape
/**
* This class represents a convex mesh shape. In order to create a convex mesh shape, you
* need to indicate the local-space position of the mesh vertices. The center of mass
* of the shape will be at the origin of the local-space geometry that you use to create
* the mesh.
2013-07-15 17:10:30 +00:00
*/
class ConvexMeshShape : public ConvexPolyhedronShape {
2013-07-15 17:10:30 +00:00
protected :
2013-07-15 17:10:30 +00:00
// -------------------- Attributes -------------------- //
/// Polyhedron structure of the mesh
PolyhedronMesh* mPolyhedronMesh;
2013-07-15 17:10:30 +00:00
/// Mesh minimum bounds in the three local x, y and z directions
Vector3 mMinBounds;
/// Mesh maximum bounds in the three local x, y and z directions
Vector3 mMaxBounds;
// -------------------- Methods -------------------- //
/// Recompute the bounds of the mesh
void recalculateBounds();
/// Return a local support point in a given direction without the object margin.
virtual Vector3 getLocalSupportPointWithoutMargin(const Vector3& direction) const override;
/// Return true if a point is inside the collision shape
virtual bool testPointInside(const Vector3& localPoint, ProxyShape* proxyShape) const override;
/// Raycast method with feedback information
virtual bool raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* proxyShape) const override;
2015-02-12 21:31:26 +00:00
/// Return the number of bytes used by the collision shape
virtual size_t getSizeInBytes() const override;
2015-02-12 21:31:26 +00:00
2013-07-15 17:10:30 +00:00
public :
// -------------------- Methods -------------------- //
/// Constructor
ConvexMeshShape(PolyhedronMesh* polyhedronMesh);
2013-07-15 17:10:30 +00:00
/// Destructor
virtual ~ConvexMeshShape() override = default;
2013-07-15 17:10:30 +00:00
/// Deleted copy-constructor
ConvexMeshShape(const ConvexMeshShape& shape) = delete;
/// Deleted assignment operator
ConvexMeshShape& operator=(const ConvexMeshShape& shape) = delete;
2013-07-15 17:10:30 +00:00
/// Set the scaling vector of the collision shape
virtual void setLocalScaling(const Vector3& scaling) override;
2013-07-15 17:10:30 +00:00
/// Return the local bounds of the shape in x, y and z directions
virtual void getLocalBounds(Vector3& min, Vector3& max) const override;
2013-07-15 17:10:30 +00:00
/// Return the local inertia tensor of the collision shape.
virtual void computeLocalInertiaTensor(Matrix3x3& tensor, decimal mass) const override;
2013-07-15 17:10:30 +00:00
2017-03-30 20:39:06 +00:00
/// Return the number of faces of the polyhedron
virtual uint getNbFaces() const override;
/// Return a given face of the polyhedron
virtual HalfEdgeStructure::Face getFace(uint faceIndex) const override;
/// Return the number of vertices of the polyhedron
virtual uint getNbVertices() const override;
/// Return a given vertex of the polyhedron
virtual HalfEdgeStructure::Vertex getVertex(uint vertexIndex) const override;
/// Return the number of half-edges of the polyhedron
virtual uint getNbHalfEdges() const override;
/// Return a given half-edge of the polyhedron
virtual HalfEdgeStructure::Edge getHalfEdge(uint edgeIndex) const override;
/// Return the position of a given vertex
virtual Vector3 getVertexPosition(uint vertexIndex) const override;
/// Return the normal vector of a given face of the polyhedron
virtual Vector3 getFaceNormal(uint faceIndex) const override;
/// Return the centroid of the polyhedron
virtual Vector3 getCentroid() const override;
2013-07-15 17:10:30 +00:00
};
/// Set the scaling vector of the collision shape
inline void ConvexMeshShape::setLocalScaling(const Vector3& scaling) {
ConvexShape::setLocalScaling(scaling);
recalculateBounds();
}
2013-07-15 17:10:30 +00:00
// Return the number of bytes used by the collision shape
inline size_t ConvexMeshShape::getSizeInBytes() const {
return sizeof(ConvexMeshShape);
}
// Return the local bounds of the shape in x, y and z directions
2015-02-12 21:31:26 +00:00
/**
* @param min The minimum bounds of the shape in local-space coordinates
* @param max The maximum bounds of the shape in local-space coordinates
*/
2013-07-15 17:10:30 +00:00
inline void ConvexMeshShape::getLocalBounds(Vector3& min, Vector3& max) const {
min = mMinBounds;
max = mMaxBounds;
}
// Return the local inertia tensor of the collision shape.
/// The local inertia tensor of the convex mesh is approximated using the inertia tensor
/// of its bounding box.
2015-02-12 21:31:26 +00:00
/**
* @param[out] tensor The 3x3 inertia tensor matrix of the shape in local-space
* coordinates
* @param mass Mass to use to compute the inertia tensor of the collision shape
*/
2013-07-15 17:10:30 +00:00
inline void ConvexMeshShape::computeLocalInertiaTensor(Matrix3x3& tensor, decimal mass) const {
decimal factor = (decimal(1.0) / decimal(3.0)) * mass;
Vector3 realExtent = decimal(0.5) * (mMaxBounds - mMinBounds);
assert(realExtent.x > 0 && realExtent.y > 0 && realExtent.z > 0);
decimal xSquare = realExtent.x * realExtent.x;
decimal ySquare = realExtent.y * realExtent.y;
decimal zSquare = realExtent.z * realExtent.z;
tensor.setAllValues(factor * (ySquare + zSquare), 0.0, 0.0,
0.0, factor * (xSquare + zSquare), 0.0,
0.0, 0.0, factor * (xSquare + ySquare));
}
2014-08-01 10:36:32 +00:00
// Return true if a point is inside the collision shape
inline bool ConvexMeshShape::testPointInside(const Vector3& localPoint,
ProxyShape* proxyShape) const {
// Use the GJK algorithm to test if the point is inside the convex mesh
return proxyShape->mBody->mWorld.mCollisionDetection.
mNarrowPhaseGJKAlgorithm.testPointInside(localPoint, proxyShape);
2014-08-01 10:36:32 +00:00
}
2017-03-30 20:39:06 +00:00
// Return the number of faces of the polyhedron
inline uint ConvexMeshShape::getNbFaces() const {
return mPolyhedronMesh->getHalfEdgeStructure().getNbFaces();
}
// Return a given face of the polyhedron
inline HalfEdgeStructure::Face ConvexMeshShape::getFace(uint faceIndex) const {
assert(faceIndex < getNbFaces());
return mPolyhedronMesh->getHalfEdgeStructure().getFace(faceIndex);
}
// Return the number of vertices of the polyhedron
inline uint ConvexMeshShape::getNbVertices() const {
return mPolyhedronMesh->getHalfEdgeStructure().getNbVertices();
}
// Return a given vertex of the polyhedron
inline HalfEdgeStructure::Vertex ConvexMeshShape::getVertex(uint vertexIndex) const {
assert(vertexIndex < getNbVertices());
return mPolyhedronMesh->getHalfEdgeStructure().getVertex(vertexIndex);
}
// Return the number of half-edges of the polyhedron
inline uint ConvexMeshShape::getNbHalfEdges() const {
return mPolyhedronMesh->getHalfEdgeStructure().getNbHalfEdges();
}
// Return a given half-edge of the polyhedron
inline HalfEdgeStructure::Edge ConvexMeshShape::getHalfEdge(uint edgeIndex) const {
assert(edgeIndex < getNbHalfEdges());
return mPolyhedronMesh->getHalfEdgeStructure().getHalfEdge(edgeIndex);
}
// Return the position of a given vertex
inline Vector3 ConvexMeshShape::getVertexPosition(uint vertexIndex) const {
assert(vertexIndex < getNbVertices());
2017-08-18 15:50:27 +00:00
return mPolyhedronMesh->getVertex(vertexIndex) * mScaling;
2017-03-30 20:39:06 +00:00
}
// Return the normal vector of a given face of the polyhedron
inline Vector3 ConvexMeshShape::getFaceNormal(uint faceIndex) const {
assert(faceIndex < getNbFaces());
return mPolyhedronMesh->getFaceNormal(faceIndex);
}
// Return the centroid of the polyhedron
inline Vector3 ConvexMeshShape::getCentroid() const {
2017-08-18 15:50:27 +00:00
return mPolyhedronMesh->getCentroid() * mScaling;
}
2013-07-15 17:10:30 +00:00
}
#endif