From 8b4f2f28654f6e98eefe640dcd28e69dc4bf1ebd Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 23 Jun 2017 23:21:45 +0200 Subject: [PATCH] [DEV] Rework can be good --- ephysics/collision/TriangleVertexArray.cpp | 79 ++------- ephysics/collision/TriangleVertexArray.hpp | 77 ++++---- .../collision/shapes/ConcaveMeshShape.cpp | 165 ++++-------------- .../collision/shapes/ConcaveMeshShape.hpp | 19 +- ephysics/collision/shapes/ConvexMeshShape.cpp | 78 ++------- ephysics/collision/shapes/ConvexMeshShape.hpp | 8 +- ephysics/engine/ConstraintSolver.hpp | 24 +-- 7 files changed, 138 insertions(+), 312 deletions(-) diff --git a/ephysics/collision/TriangleVertexArray.cpp b/ephysics/collision/TriangleVertexArray.cpp index 2497b72..0ee569b 100644 --- a/ephysics/collision/TriangleVertexArray.cpp +++ b/ephysics/collision/TriangleVertexArray.cpp @@ -7,76 +7,33 @@ // Libraries #include -using namespace ephysics; -// Constructor -/// Note that your data will not be copied int32_to the TriangleVertexArray and -/// therefore, you need to make sure that those data are always valid during -/// the lifetime of the TriangleVertexArray. -/** - * @param nbVertices Number of vertices in the array - * @param verticesStart Pointer to the first vertices of the array - * @param verticesStride Number of bytes between the beginning of two consecutive vertices - * @param nbTriangles Number of triangles in the array - * @param indexesStart Pointer to the first triangle index - * @param indexesStride Number of bytes between the beginning of two consecutive triangle indices - * @param vertexDataType Type of data for the vertices (float, double) - * @param indexDataType Type of data for the indices (short, int32_t) - */ -TriangleVertexArray::TriangleVertexArray(uint32_t nbVertices, void* verticesStart, int32_t verticesStride, - uint32_t nbTriangles, void* indexesStart, int32_t indexesStride, - VertexDataType vertexDataType, IndexDataType indexDataType) { - m_numberVertices = nbVertices; - m_verticesStart = reinterpret_cast(verticesStart); - m_verticesStride = verticesStride; - m_numberTriangles = nbTriangles; - m_indicesStart = reinterpret_cast(indexesStart); - m_indicesStride = indexesStride; - m_vertexDataType = vertexDataType; - m_indexDataType = indexDataType; +ephysics::TriangleVertexArray::TriangleVertexArray(const std::vector& _vertices, std::vector _triangles): + m_vertices(_vertices), + m_triangles(_triangles) { + } -// Destructor -TriangleVertexArray::~TriangleVertexArray() { - +size_t ephysics::TriangleVertexArray::getNbVertices() const { + return m_vertices.size(); } -// Return the vertex data type -TriangleVertexArray::VertexDataType TriangleVertexArray::getVertexDataType() const { - return m_vertexDataType; +size_t ephysics::TriangleVertexArray::getNbTriangles() const { + return m_triangles.size()/3; } -// Return the index data type -TriangleVertexArray::IndexDataType TriangleVertexArray::getIndexDataType() const { - return m_indexDataType; +const std::vector& ephysics::TriangleVertexArray::getVertices() const { + return m_vertices; } -// Return the number of vertices -uint32_t TriangleVertexArray::getNbVertices() const { - return m_numberVertices; +const std::vector& ephysics::TriangleVertexArray::getIndices() const{ + return m_triangles; } -// Return the number of triangles -uint32_t TriangleVertexArray::getNbTriangles() const { - return m_numberTriangles; -} - -// Return the vertices stride (number of bytes) -int32_t TriangleVertexArray::getVerticesStride() const { - return m_verticesStride; -} - -// Return the indices stride (number of bytes) -int32_t TriangleVertexArray::getIndicesStride() const { - return m_indicesStride; -} - -// Return the pointer to the start of the vertices array -unsigned char* TriangleVertexArray::getVerticesStart() const { - return m_verticesStart; -} - -// Return the pointer to the start of the indices array -unsigned char* TriangleVertexArray::getIndicesStart() const { - return m_indicesStart; +ephysics::Triangle ephysics::TriangleVertexArray::getTriangle(size_t _id) const { + ephysics::Triangle out; + out[0] = m_vertices[m_triangles[_id*3]]; + out[1] = m_vertices[m_triangles[_id*3+1]]; + out[2] = m_vertices[m_triangles[_id*3+2]]; + return out; } \ No newline at end of file diff --git a/ephysics/collision/TriangleVertexArray.hpp b/ephysics/collision/TriangleVertexArray.hpp index dfcbf86..282f2a0 100644 --- a/ephysics/collision/TriangleVertexArray.hpp +++ b/ephysics/collision/TriangleVertexArray.hpp @@ -6,8 +6,16 @@ #pragma once #include +#include namespace ephysics { + class Triangle { + public: + vec3 value[3]; + vec3& operator[] (size_t _id) { + return value[_id]; + } + }; /** * This class is used to describe the vertices and faces of a triangular mesh. * A TriangleVertexArray represents a continuous array of vertices and indexes @@ -18,43 +26,42 @@ namespace ephysics { * remains valid during the TriangleVertexArray life. */ class TriangleVertexArray { - public: - /// Data type for the vertices in the array - enum VertexDataType {VERTEX_FLOAT_TYPE, VERTEX_DOUBLE_TYPE}; - /// Data type for the indices in the array - enum IndexDataType {INDEX_INTEGER_TYPE, INDEX_SHORT_TYPE}; protected: - uint32_t m_numberVertices; //!< Number of vertices in the array - unsigned char* m_verticesStart; //!< Pointer to the first vertex value in the array - int32_t m_verticesStride; //!< Stride (number of bytes) between the beginning of two vertices values in the array - uint32_t m_numberTriangles; //!< Number of triangles in the array - unsigned char* m_indicesStart; //!< Pointer to the first vertex index of the array - int32_t m_indicesStride; //!< Stride (number of bytes) between the beginning of two indices in the array - VertexDataType m_vertexDataType; //!< Data type of the vertices in the array - IndexDataType m_indexDataType; //!< Data type of the indices in the array + std::vector m_vertices; //!< Vertice list + std::vector m_triangles; //!< List of triangle (3 pos for each triangle) public: - /// Constructor - TriangleVertexArray(uint32_t nbVertices, void* verticesStart, int32_t verticesStride, - uint32_t nbTriangles, void* indexesStart, int32_t indexesStride, - VertexDataType vertexDataType, IndexDataType indexDataType); - /// Destructor - virtual ~TriangleVertexArray(); - /// Return the vertex data type - VertexDataType getVertexDataType() const; - /// Return the index data type - IndexDataType getIndexDataType() const; - /// Return the number of vertices - uint32_t getNbVertices() const; - /// Return the number of triangles - uint32_t getNbTriangles() const; - /// Return the vertices stride (number of bytes) - int32_t getVerticesStride() const; - /// Return the indices stride (number of bytes) - int32_t getIndicesStride() const; - /// Return the pointer to the start of the vertices array - unsigned char* getVerticesStart() const; - /// Return the pointer to the start of the indices array - unsigned char* getIndicesStart() const; + /** + * @brief Constructor + * @param[in] _vertices List Of all vertices + * @param[in] _triangles List of all linked points + */ + TriangleVertexArray(const std::vector& _vertices, + std::vector _triangles); + /** + * @brief Get the number of vertices + * @return Number of vertices + */ + size_t getNbVertices() const; + /** + * @brief Get the number of triangle + * @return Number of triangles + */ + size_t getNbTriangles() const; + /** + * @brief Get The table of the vertices + * @return reference on the vertices + */ + const std::vector& getVertices() const; + /** + * @brief Get The table of the triangle indice + * @return reference on the triangle indice + */ + const std::vector& getIndices() const; + /** + * @brief Get a triangle at the specific ID + * @return Buffer of 3 points + */ + ephysics::Triangle getTriangle(size_t _id) const; }; diff --git a/ephysics/collision/shapes/ConcaveMeshShape.cpp b/ephysics/collision/shapes/ConcaveMeshShape.cpp index a63c0ae..461e20b 100644 --- a/ephysics/collision/shapes/ConcaveMeshShape.cpp +++ b/ephysics/collision/shapes/ConcaveMeshShape.cpp @@ -4,153 +4,80 @@ * @license BSD 3 clauses (see license file) */ -// Libraries #include +#include #include using namespace ephysics; -// Constructor -ConcaveMeshShape::ConcaveMeshShape(TriangleMesh* triangleMesh): +ConcaveMeshShape::ConcaveMeshShape(TriangleMesh* _triangleMesh): ConcaveShape(CONCAVE_MESH) { - m_triangleMesh = triangleMesh; + m_triangleMesh = _triangleMesh; m_raycastTestType = FRONT; - // Insert all the triangles int32_to the dynamic AABB tree initBVHTree(); } -// Destructor -ConcaveMeshShape::~ConcaveMeshShape() { - -} - -// Insert all the triangles int32_to the dynamic AABB tree void ConcaveMeshShape::initBVHTree() { - // TODO : Try to randomly add the triangles int32_to the tree to obtain a better tree + // TODO : Try to randomly add the triangles into the tree to obtain a better tree // For each sub-part of the mesh for (uint32_t subPart=0; subPartgetNbSubparts(); subPart++) { // Get the triangle vertex array of the current sub-part TriangleVertexArray* triangleVertexArray = m_triangleMesh->getSubpart(subPart); - TriangleVertexArray::VertexDataType vertexType = triangleVertexArray->getVertexDataType(); - TriangleVertexArray::IndexDataType indexType = triangleVertexArray->getIndexDataType(); - unsigned char* verticesStart = triangleVertexArray->getVerticesStart(); - unsigned char* indicesStart = triangleVertexArray->getIndicesStart(); - int32_t vertexStride = triangleVertexArray->getVerticesStride(); - int32_t indexStride = triangleVertexArray->getIndicesStride(); // For each triangle of the concave mesh - for (uint32_t triangleIndex=0; triangleIndexgetNbTriangles(); triangleIndex++) { - void* vertexIndexPointer = (indicesStart + triangleIndex * 3 * indexStride); - vec3 trianglePoints[3]; - // For each vertex of the triangle - for (int32_t k=0; k < 3; k++) { - // Get the index of the current vertex in the triangle - int32_t vertexIndex = 0; - if (indexType == TriangleVertexArray::INDEX_INTEGER_TYPE) { - vertexIndex = ((uint32_t*)vertexIndexPointer)[k]; - } else if (indexType == TriangleVertexArray::INDEX_SHORT_TYPE) { - vertexIndex = ((unsigned short*)vertexIndexPointer)[k]; - } else { - assert(false); - } - // Get the vertices components of the triangle - if (vertexType == TriangleVertexArray::VERTEX_FLOAT_TYPE) { - const float* vertices = (float*)(verticesStart + vertexIndex * vertexStride); - trianglePoints[k][0] = float(vertices[0]) * m_scaling.x(); - trianglePoints[k][1] = float(vertices[1]) * m_scaling.y(); - trianglePoints[k][2] = float(vertices[2]) * m_scaling.z(); - } else if (vertexType == TriangleVertexArray::VERTEX_DOUBLE_TYPE) { - const double* vertices = (double*)(verticesStart + vertexIndex * vertexStride); - trianglePoints[k][0] = float(vertices[0]) * m_scaling.x(); - trianglePoints[k][1] = float(vertices[1]) * m_scaling.y(); - trianglePoints[k][2] = float(vertices[2]) * m_scaling.z(); - } else { - assert(false); - } - } + for (size_t iii=0; iiigetNbTriangles(); ++iii) { + ephysics::Triangle trianglePoints = triangleVertexArray->getTriangle(iii); + vec3 trianglePoints2[3]; + trianglePoints2[0] = trianglePoints[0]; + trianglePoints2[1] = trianglePoints[1]; + trianglePoints2[2] = trianglePoints[2]; // Create the AABB for the triangle - AABB aabb = AABB::createAABBForTriangle(trianglePoints); + AABB aabb = AABB::createAABBForTriangle(trianglePoints2); aabb.inflate(m_triangleMargin, m_triangleMargin, m_triangleMargin); // Add the AABB with the index of the triangle int32_to the dynamic AABB tree - m_dynamicAABBTree.addObject(aabb, subPart, triangleIndex); + m_dynamicAABBTree.addObject(aabb, subPart, iii); } } } -// Return the three vertices coordinates (in the array outTriangleVertices) of a triangle -// given the start vertex index pointer of the triangle -void ConcaveMeshShape::getTriangleVerticesWithIndexPointer(int32_t subPart, int32_t triangleIndex, vec3* outTriangleVertices) const { +void ConcaveMeshShape::getTriangleVerticesWithIndexPointer(int32_t _subPart, int32_t _triangleIndex, vec3* _outTriangleVertices) const { + EPHY_ASSERT(_outTriangleVertices != nullptr, "Input check error"); + // Get the triangle vertex array of the current sub-part - TriangleVertexArray* triangleVertexArray = m_triangleMesh->getSubpart(subPart); + TriangleVertexArray* triangleVertexArray = m_triangleMesh->getSubpart(_subPart); if (triangleVertexArray == nullptr) { std::cout << "get nullptr ..." << std::endl; } - TriangleVertexArray::VertexDataType vertexType = triangleVertexArray->getVertexDataType(); - TriangleVertexArray::IndexDataType indexType = triangleVertexArray->getIndexDataType(); - unsigned char* verticesStart = triangleVertexArray->getVerticesStart(); - unsigned char* indicesStart = triangleVertexArray->getIndicesStart(); - int32_t vertexStride = triangleVertexArray->getVerticesStride(); - int32_t indexStride = triangleVertexArray->getIndicesStride(); - void* vertexIndexPointer = (indicesStart + triangleIndex * 3 * indexStride); - // For each vertex of the triangle - for (int32_t k=0; k < 3; k++) { - // Get the index of the current vertex in the triangle - int32_t vertexIndex = 0; - if (indexType == TriangleVertexArray::INDEX_INTEGER_TYPE) { - vertexIndex = ((uint32_t*)vertexIndexPointer)[k]; - } else if (indexType == TriangleVertexArray::INDEX_SHORT_TYPE) { - vertexIndex = ((unsigned short*)vertexIndexPointer)[k]; - } else { - std::cout << "wrong type of array : " << int32_t(indexType) << std::endl; - assert(false); - } - // Get the vertices components of the triangle - if (vertexType == TriangleVertexArray::VERTEX_FLOAT_TYPE) { - const float* vertices = (float*)(verticesStart + vertexIndex * vertexStride); - outTriangleVertices[k][0] = float(vertices[0]) * m_scaling.x(); - outTriangleVertices[k][1] = float(vertices[1]) * m_scaling.y(); - outTriangleVertices[k][2] = float(vertices[2]) * m_scaling.z(); - } else if (vertexType == TriangleVertexArray::VERTEX_DOUBLE_TYPE) { - const double* vertices = (double*)(verticesStart + vertexIndex * vertexStride); - outTriangleVertices[k][0] = float(vertices[0]) * m_scaling.x(); - outTriangleVertices[k][1] = float(vertices[1]) * m_scaling.y(); - outTriangleVertices[k][2] = float(vertices[2]) * m_scaling.z(); - } else { - assert(false); - } - } + ephysics::Triangle trianglePoints = triangleVertexArray->getTriangle(_triangleIndex); + _outTriangleVertices[0] = trianglePoints[0] * m_scaling; + _outTriangleVertices[1] = trianglePoints[1] * m_scaling; + _outTriangleVertices[2] = trianglePoints[2] * m_scaling; } -// Use a callback method on all triangles of the concave shape inside a given AABB -void ConcaveMeshShape::testAllTriangles(TriangleCallback& callback, const AABB& localAABB) const { - ConvexTriangleAABBOverlapCallback overlapCallback(callback, *this, m_dynamicAABBTree); +void ConcaveMeshShape::testAllTriangles(TriangleCallback& _callback, const AABB& _localAABB) const { + ConvexTriangleAABBOverlapCallback overlapCallback(_callback, *this, m_dynamicAABBTree); // Ask the Dynamic AABB Tree to report all the triangles that are overlapping // with the AABB of the convex shape. - m_dynamicAABBTree.reportAllShapesOverlappingWithAABB(localAABB, overlapCallback); + m_dynamicAABBTree.reportAllShapesOverlappingWithAABB(_localAABB, overlapCallback); } -// Raycast method with feedback information -/// Note that only the first triangle hit by the ray in the mesh will be returned, even if -/// the ray hits many triangles. -bool ConcaveMeshShape::raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* proxyShape) const { +bool ConcaveMeshShape::raycast(const Ray& _ray, RaycastInfo& _raycastInfo, ProxyShape* _proxyShape) const { PROFILE("ConcaveMeshShape::raycast()"); // Create the callback object that will compute ray casting against triangles - ConcaveMeshRaycastCallback raycastCallback(m_dynamicAABBTree, *this, proxyShape, raycastInfo, ray); + ConcaveMeshRaycastCallback raycastCallback(m_dynamicAABBTree, *this, _proxyShape, _raycastInfo, _ray); // Ask the Dynamic AABB Tree to report all AABB nodes that are hit by the ray. // The raycastCallback object will then compute ray casting against the triangles // in the hit AABBs. - m_dynamicAABBTree.raycast(ray, raycastCallback); + m_dynamicAABBTree.raycast(_ray, raycastCallback); raycastCallback.raycastTriangles(); return raycastCallback.getIsHit(); } -// Collect all the AABB nodes that are hit by the ray in the Dynamic AABB Tree -float ConcaveMeshRaycastCallback::raycastBroadPhaseShape(int32_t nodeId, const Ray& ray) { +float ConcaveMeshRaycastCallback::raycastBroadPhaseShape(int32_t _nodeId, const Ray& _ray) { // Add the id of the hit AABB node int32_to - m_hitAABBNodes.push_back(nodeId); - return ray.maxFraction; + m_hitAABBNodes.push_back(_nodeId); + return _ray.maxFraction; } -// Raycast all collision shapes that have been collected void ConcaveMeshRaycastCallback::raycastTriangles() { std::vector::const_iterator it; float smallestHitFraction = m_ray.maxFraction; @@ -183,46 +110,24 @@ void ConcaveMeshRaycastCallback::raycastTriangles() { } } -// Return the number of bytes used by the collision shape size_t ConcaveMeshShape::getSizeInBytes() const { return sizeof(ConcaveMeshShape); } -// Return the local bounds of the shape in x, y and z directions. -// This method is used to compute the AABB of the box -/** - * @param min The minimum bounds of the shape in local-space coordinates - * @param max The maximum bounds of the shape in local-space coordinates - */ -void ConcaveMeshShape::getLocalBounds(vec3& min, vec3& max) const { - +void ConcaveMeshShape::getLocalBounds(vec3& _min, vec3& _max) const { // Get the AABB of the whole tree AABB treeAABB = m_dynamicAABBTree.getRootAABB(); - - min = treeAABB.getMin(); - max = treeAABB.getMax(); + _min = treeAABB.getMin(); + _max = treeAABB.getMax(); } -// Set the local scaling vector of the collision shape -void ConcaveMeshShape::setLocalScaling(const vec3& scaling) { - - CollisionShape::setLocalScaling(scaling); - - // Reset the Dynamic AABB Tree +void ConcaveMeshShape::setLocalScaling(const vec3& _scaling) { + CollisionShape::setLocalScaling(_scaling); m_dynamicAABBTree.reset(); - - // Rebuild Dynamic AABB Tree here initBVHTree(); } -// Return the local inertia tensor of the shape -/** - * @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 - */ void ConcaveMeshShape::computeLocalInertiaTensor(etk::Matrix3x3& _tensor, float _mass) const { - // Default inertia tensor // Note that this is not very realistic for a concave triangle mesh. // However, in most cases, it will only be used static bodies and therefore, @@ -232,8 +137,6 @@ void ConcaveMeshShape::computeLocalInertiaTensor(etk::Matrix3x3& _tensor, float 0, 0, _mass); } -// Called when a overlapping node has been found during the call to -// DynamicAABBTree:reportAllShapesOverlappingWithAABB() void ConvexTriangleAABBOverlapCallback::notifyOverlappingNode(int32_t _nodeId) { // Get the node data (triangle index and mesh subpart index) diff --git a/ephysics/collision/shapes/ConcaveMeshShape.hpp b/ephysics/collision/shapes/ConcaveMeshShape.hpp index 5d3b25f..96ccf7a 100644 --- a/ephysics/collision/shapes/ConcaveMeshShape.hpp +++ b/ephysics/collision/shapes/ConcaveMeshShape.hpp @@ -79,10 +79,8 @@ namespace ephysics { ConcaveMeshShape(const ConcaveMeshShape& _shape) = delete; /// Private assignment operator ConcaveMeshShape& operator=(const ConcaveMeshShape& _shape) = delete; - /// Raycast method with feedback information - virtual bool raycast(const Ray& _ray, RaycastInfo& _raycastInfo, ProxyShape* _proxyShape) const; - /// Return the number of bytes used by the collision shape - virtual size_t getSizeInBytes() const; + virtual bool raycast(const Ray& _ray, RaycastInfo& _raycastInfo, ProxyShape* _proxyShape) const override; + virtual size_t getSizeInBytes() const override; /// Insert all the triangles int32_to the dynamic AABB tree void initBVHTree(); /// Return the three vertices coordinates (in the array outTriangleVertices) of a triangle @@ -93,16 +91,11 @@ namespace ephysics { public: /// Constructor ConcaveMeshShape(TriangleMesh* triangleMesh); - /// Destructor - ~ConcaveMeshShape(); - /// Return the local bounds of the shape in x, y and z directions. - virtual void getLocalBounds(vec3& min, vec3& max) const; - /// Set the local scaling vector of the collision shape - virtual void setLocalScaling(const vec3& scaling); - /// Return the local inertia tensor of the collision shape - virtual void computeLocalInertiaTensor(etk::Matrix3x3& tensor, float mass) const; + virtual void getLocalBounds(vec3& min, vec3& max) const override; + virtual void setLocalScaling(const vec3& scaling) override; + virtual void computeLocalInertiaTensor(etk::Matrix3x3& tensor, float mass) const override; /// Use a callback method on all triangles of the concave shape inside a given AABB - virtual void testAllTriangles(TriangleCallback& callback, const AABB& localAABB) const; + virtual void testAllTriangles(TriangleCallback& callback, const AABB& localAABB) const override; friend class ConvexTriangleAABBOverlapCallback; friend class ConcaveMeshRaycastCallback; }; diff --git a/ephysics/collision/shapes/ConvexMeshShape.cpp b/ephysics/collision/shapes/ConvexMeshShape.cpp index de517ea..3efa5e9 100644 --- a/ephysics/collision/shapes/ConvexMeshShape.cpp +++ b/ephysics/collision/shapes/ConvexMeshShape.cpp @@ -38,76 +38,29 @@ ConvexMeshShape::ConvexMeshShape(const float* arrayVertices, uint32_t nbVertices recalculateBounds(); } -// Constructor to initialize with a triangle mesh -/// This method creates an int32_ternal copy of the input vertices. -/** - * @param triangleVertexArray Array with the vertices and indices of the vertices and triangles of the mesh - * @param isEdgesInformationUsed True if you want to use edges information for collision detection (faster but requires more memory) - * @param margin Collision margin (in meters) around the collision shape - */ -ConvexMeshShape::ConvexMeshShape(TriangleVertexArray* triangleVertexArray, bool isEdgesInformationUsed, float margin) - : ConvexShape(CONVEX_MESH, margin), m_minBounds(0, 0, 0), - m_maxBounds(0, 0, 0), m_isEdgesInformationUsed(isEdgesInformationUsed) { - - TriangleVertexArray::VertexDataType vertexType = triangleVertexArray->getVertexDataType(); - TriangleVertexArray::IndexDataType indexType = triangleVertexArray->getIndexDataType(); - unsigned char* verticesStart = triangleVertexArray->getVerticesStart(); - unsigned char* indicesStart = triangleVertexArray->getIndicesStart(); - int32_t vertexStride = triangleVertexArray->getVerticesStride(); - int32_t indexStride = triangleVertexArray->getIndicesStride(); - +ConvexMeshShape::ConvexMeshShape(TriangleVertexArray* _triangleVertexArray, bool _isEdgesInformationUsed, float _margin): + ConvexShape(CONVEX_MESH, _margin), + m_minBounds(0, 0, 0), + m_maxBounds(0, 0, 0), + m_isEdgesInformationUsed(_isEdgesInformationUsed) { // For each vertex of the mesh - for (uint32_t v = 0; v < triangleVertexArray->getNbVertices(); v++) { - - // Get the vertices components of the triangle - if (vertexType == TriangleVertexArray::VERTEX_FLOAT_TYPE) { - const float* vertices = (float*)(verticesStart + v * vertexStride); - - vec3 vertex(vertices[0], vertices[1], vertices[2] ); - vertex = vertex * m_scaling; - m_vertices.push_back(vertex); - } - else if (vertexType == TriangleVertexArray::VERTEX_DOUBLE_TYPE) { - const double* vertices = (double*)(verticesStart + v * vertexStride); - - vec3 vertex(vertices[0], vertices[1], vertices[2] ); - vertex = vertex * m_scaling; - m_vertices.push_back(vertex); - } + for (auto &it: _triangleVertexArray->getVertices()) { + m_vertices.push_back(it*m_scaling); } - // If we need to use the edges information of the mesh if (m_isEdgesInformationUsed) { - // For each triangle of the mesh - for (uint32_t triangleIndex=0; triangleIndexgetNbTriangles(); triangleIndex++) { - - void* vertexIndexPointer = (indicesStart + triangleIndex * 3 * indexStride); - + for (size_t iii=0; iii<_triangleVertexArray->getNbTriangles(); iii++) { uint32_t vertexIndex[3] = {0, 0, 0}; - - // For each vertex of the triangle - for (int32_t k=0; k < 3; k++) { - - // Get the index of the current vertex in the triangle - if (indexType == TriangleVertexArray::INDEX_INTEGER_TYPE) { - vertexIndex[k] = ((uint32_t*)vertexIndexPointer)[k]; - } - else if (indexType == TriangleVertexArray::INDEX_SHORT_TYPE) { - vertexIndex[k] = ((unsigned short*)vertexIndexPointer)[k]; - } - else { - assert(false); - } - } - + vertexIndex[0] = _triangleVertexArray->getIndices()[iii*3]; + vertexIndex[1] = _triangleVertexArray->getIndices()[iii*3+1]; + vertexIndex[2] = _triangleVertexArray->getIndices()[iii*3+2]; // Add information about the edges addEdge(vertexIndex[0], vertexIndex[1]); addEdge(vertexIndex[0], vertexIndex[2]); addEdge(vertexIndex[1], vertexIndex[2]); } } - m_numberVertices = m_vertices.size(); recalculateBounds(); } @@ -115,9 +68,12 @@ ConvexMeshShape::ConvexMeshShape(TriangleVertexArray* triangleVertexArray, bool // Constructor. /// If you use this constructor, you will need to set the vertices manually one by one using /// the addVertex() method. -ConvexMeshShape::ConvexMeshShape(float margin) - : ConvexShape(CONVEX_MESH, margin), m_numberVertices(0), m_minBounds(0, 0, 0), - m_maxBounds(0, 0, 0), m_isEdgesInformationUsed(false) { +ConvexMeshShape::ConvexMeshShape(float _margin): + ConvexShape(CONVEX_MESH, _margin), + m_numberVertices(0), + m_minBounds(0, 0, 0), + m_maxBounds(0, 0, 0), + m_isEdgesInformationUsed(false) { } // Return a local support point in a given direction without the object margin. diff --git a/ephysics/collision/shapes/ConvexMeshShape.hpp b/ephysics/collision/shapes/ConvexMeshShape.hpp index b2ddd2d..aa178e5 100644 --- a/ephysics/collision/shapes/ConvexMeshShape.hpp +++ b/ephysics/collision/shapes/ConvexMeshShape.hpp @@ -56,7 +56,13 @@ namespace ephysics { uint32_t _nbVertices, int32_t _stride, float _margin = OBJECT_MARGIN); - /// Constructor to initialize with a triangle vertex array + /** + * @brief Constructor to initialize with a triangle mesh + * This method creates an internal copy of the input vertices. + * @param _triangleVertexArray Array with the vertices and indices of the vertices and triangles of the mesh + * @param _isEdgesInformationUsed True if you want to use edges information for collision detection (faster but requires more memory) + * @param _margin Collision margin (in meters) around the collision shape + */ ConvexMeshShape(TriangleVertexArray* _triangleVertexArray, bool _isEdgesInformationUsed = true, float _margin = OBJECT_MARGIN); diff --git a/ephysics/engine/ConstraintSolver.hpp b/ephysics/engine/ConstraintSolver.hpp index f36e553..2875730 100644 --- a/ephysics/engine/ConstraintSolver.hpp +++ b/ephysics/engine/ConstraintSolver.hpp @@ -111,23 +111,27 @@ namespace ephysics { ConstraintSolverData m_constraintSolverData; //!< Constraint solver data used to initialize and solve the constraints public : /// Constructor - ConstraintSolver(const std::map& mapBodyToVelocityIndex); + ConstraintSolver(const std::map& _mapBodyToVelocityIndex); /// Initialize the constraint solver for a given island - void initializeForIsland(float dt, Island* island); + void initializeForIsland(float _dt, Island* _island); /// Solve the constraints - void solveVelocityConstraints(Island* island); + void solveVelocityConstraints(Island* _island); /// Solve the position constraints - void solvePositionConstraints(Island* island); + void solvePositionConstraints(Island* _island); /// Return true if the Non-Linear-Gauss-Seidel position correction technique is active - bool getIsNonLinearGaussSeidelPositionCorrectionActive() const; + bool getIsNonLinearGaussSeidelPositionCorrectionActive() const { + return m_isWarmStartingActive; + } /// Enable/Disable the Non-Linear-Gauss-Seidel position correction technique. - void setIsNonLinearGaussSeidelPositionCorrectionActive(bool isActive); + void setIsNonLinearGaussSeidelPositionCorrectionActive(bool _isActive) { + m_isWarmStartingActive = _isActive; + } /// Set the constrained velocities arrays - void setConstrainedVelocitiesArrays(vec3* constrainedLinearVelocities, - vec3* constrainedAngularVelocities); + void setConstrainedVelocitiesArrays(vec3* _constrainedLinearVelocities, + vec3* _constrainedAngularVelocities); /// Set the constrained positions/orientations arrays - void setConstrainedPositionsArrays(vec3* constrainedPositions, - etk::Quaternion* constrainedOrientations); + void setConstrainedPositionsArrays(vec3* _constrainedPositions, + etk::Quaternion* _constrainedOrientations); }; }