|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Open Source Robotics Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | +*/ |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +#include <gz/math/MassMatrix3.hh> |
| 21 | + |
| 22 | +#include "MeshInertiaCalculator.hh" |
| 23 | + |
| 24 | +using namespace gz; |
| 25 | +using namespace sim; |
| 26 | + |
| 27 | +///////////////////////////////////////////////// |
| 28 | +TEST(MeshInertiaCalculator, CorrectMassMatrix) |
| 29 | +{ |
| 30 | + // Verify a mass matrix with a small error can be corrected |
| 31 | + math::MassMatrix3d massMatrix(55.0, |
| 32 | + math::Vector3d(7, 15, 23), |
| 33 | + math::Vector3d::Zero); |
| 34 | + EXPECT_FALSE(massMatrix.IsValid()); |
| 35 | + EXPECT_TRUE(massMatrix.IsPositive()); |
| 36 | + // Set a small tolerance for the triangle inequality test and expect that |
| 37 | + // the mass matrix can not be corrected. |
| 38 | + EXPECT_FALSE(MeshInertiaCalculator::CorrectMassMatrix(massMatrix, 0.01)); |
| 39 | + // Verify successful correction with default tolerance. |
| 40 | + EXPECT_TRUE(MeshInertiaCalculator::CorrectMassMatrix(massMatrix)); |
| 41 | + EXPECT_TRUE(massMatrix.IsValid()); |
| 42 | + |
| 43 | + // Verify a mass matrix with unordered diagonal moments and a small error |
| 44 | + // can be corrected, and that the elements in the corrected principal moments |
| 45 | + // maintain the same order. |
| 46 | + math::Vector3d ixxyyzz(15, 7, 23); |
| 47 | + massMatrix = math::MassMatrix3d(55.0, |
| 48 | + ixxyyzz, |
| 49 | + math::Vector3d::Zero); |
| 50 | + EXPECT_FALSE(massMatrix.IsValid()); |
| 51 | + EXPECT_TRUE(massMatrix.IsPositive()); |
| 52 | + EXPECT_TRUE(MeshInertiaCalculator::CorrectMassMatrix(massMatrix)); |
| 53 | + EXPECT_TRUE(massMatrix.IsValid()); |
| 54 | + EXPECT_NE(ixxyyzz, massMatrix.DiagonalMoments()); |
| 55 | + EXPECT_LT(massMatrix.PrincipalMoments()[1], massMatrix.PrincipalMoments()[0]); |
| 56 | + EXPECT_LT(massMatrix.PrincipalMoments()[0], massMatrix.PrincipalMoments()[2]); |
| 57 | + |
| 58 | + // Verify a mass matrix with non-zero off-diagonal moments can be corrected |
| 59 | + massMatrix = math::MassMatrix3d(1.0, |
| 60 | + math::Vector3d(2.0, 2.0, 2.0), |
| 61 | + math::Vector3d(-1, 0, -0.1)); |
| 62 | + EXPECT_FALSE(massMatrix.IsValid()); |
| 63 | + EXPECT_TRUE(massMatrix.IsPositive()); |
| 64 | + EXPECT_TRUE(MeshInertiaCalculator::CorrectMassMatrix(massMatrix)); |
| 65 | + EXPECT_TRUE(massMatrix.IsValid()); |
| 66 | + |
| 67 | + // Verify a mass matrix with a large error can not be corrected. |
| 68 | + massMatrix = math::MassMatrix3d(15.0, |
| 69 | + math::Vector3d(1, 2, 23), |
| 70 | + math::Vector3d::Zero); |
| 71 | + EXPECT_FALSE(massMatrix.IsValid()); |
| 72 | + EXPECT_TRUE(massMatrix.IsPositive()); |
| 73 | + EXPECT_FALSE(MeshInertiaCalculator::CorrectMassMatrix(massMatrix)); |
| 74 | + EXPECT_FALSE(massMatrix.IsValid()); |
| 75 | + |
| 76 | + // Verify a mass matrix with non positive-definite inertia matrix can not |
| 77 | + // be corrected. |
| 78 | + massMatrix = math::MassMatrix3d(15.0, |
| 79 | + math::Vector3d(12, -15, 23), |
| 80 | + math::Vector3d::Zero); |
| 81 | + EXPECT_FALSE(massMatrix.IsValid()); |
| 82 | + EXPECT_FALSE(massMatrix.IsPositive()); |
| 83 | + EXPECT_FALSE(MeshInertiaCalculator::CorrectMassMatrix(massMatrix)); |
| 84 | + EXPECT_FALSE(massMatrix.IsPositive()); |
| 85 | + EXPECT_FALSE(massMatrix.IsValid()); |
| 86 | + |
| 87 | + // Verify that CorrectMassMatrix returns true when given a valid mass matrix |
| 88 | + // and does not change the mass matrix data. |
| 89 | + massMatrix = math::MassMatrix3d(15.0, |
| 90 | + math::Vector3d(12, 15, 23), |
| 91 | + math::Vector3d::Zero); |
| 92 | + math::MassMatrix3d originalMassMatrix = massMatrix; |
| 93 | + EXPECT_TRUE(massMatrix.IsValid()); |
| 94 | + EXPECT_TRUE(MeshInertiaCalculator::CorrectMassMatrix(massMatrix)); |
| 95 | + EXPECT_TRUE(massMatrix.IsValid()); |
| 96 | + EXPECT_DOUBLE_EQ(originalMassMatrix.Mass(), |
| 97 | + massMatrix.Mass()); |
| 98 | + EXPECT_EQ(originalMassMatrix.DiagonalMoments(), |
| 99 | + massMatrix.DiagonalMoments()); |
| 100 | + EXPECT_EQ(originalMassMatrix.OffDiagonalMoments(), |
| 101 | + massMatrix.OffDiagonalMoments()); |
| 102 | +} |
0 commit comments