Skip to content

Commit

Permalink
Update Mesh Inertia Calculator to incorporate mesh scale in the calcu…
Browse files Browse the repository at this point in the history
…lations (#2171)

Signed-off-by: Jasmeet Singh <[email protected]>
  • Loading branch information
jasmeet0915 authored Sep 27, 2023
1 parent 2e58d61 commit 5ef84a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/MeshInertiaCalculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ using namespace sim;

//////////////////////////////////////////////////
void MeshInertiaCalculator::GetMeshTriangles(
std::vector<Triangle>& _triangles,
std::vector<Triangle> &_triangles,
const gz::math::Vector3d &_meshScale,
const gz::common::Mesh* _mesh)
{
// Get the vertices & indices of the mesh
Expand All @@ -60,6 +61,12 @@ void MeshInertiaCalculator::GetMeshTriangles(
triangle.v2.X() = vertArray[static_cast<ptrdiff_t>(3 * indArray[i+2])];
triangle.v2.Y() = vertArray[static_cast<ptrdiff_t>(3 * indArray[i+2] + 1)];
triangle.v2.Z() = vertArray[static_cast<ptrdiff_t>(3 * indArray[i+2] + 2)];

// Apply mesh scale to the triangle coordinates
triangle.v0 = triangle.v0 * _meshScale;
triangle.v1 = triangle.v1 * _meshScale;
triangle.v2 = triangle.v2 * _meshScale;

triangle.centroid = (triangle.v0 + triangle.v1 + triangle.v2) / 3;
_triangles.push_back(triangle);
}
Expand Down Expand Up @@ -245,7 +252,7 @@ std::optional<gz::math::Inertiald> MeshInertiaCalculator::operator()
gz::math::Pose3d inertiaOrigin;

// Create a list of Triangle objects from the mesh vertices and indices
this->GetMeshTriangles(meshTriangles, mesh);
this->GetMeshTriangles(meshTriangles, sdfMesh->Scale(), mesh);

// Calculate the mesh centroid and set is as centre of mass
this->CalculateMeshCentroid(centreOfMass, meshTriangles);
Expand Down
5 changes: 4 additions & 1 deletion src/MeshInertiaCalculator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ namespace gz
/// \param[out] _triangles A vector to hold all the Triangle
/// instances obtained
/// from the vertices & indices of the mesh
/// \param[in] _meshScale A vector with the scaling factor
/// of all the 3 axes
/// \param[in] _mesh Mesh object
public: void GetMeshTriangles(
std::vector<Triangle>& _triangles,
std::vector<Triangle> &_triangles,
const gz::math::Vector3d &_meshScale,
const gz::common::Mesh* _mesh);

/// \brief Function to calculate the centroid of the mesh. Since
Expand Down

0 comments on commit 5ef84a9

Please sign in to comment.