Skip to content

Commit 7fc45cc

Browse files
committed
Register MeshInertialCalculator when loading sim from an SDF string (#2754)
Signed-off-by: Ian Chen <ichen@openrobotics.org>
1 parent e2c84db commit 7fc45cc

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

src/ServerPrivate.cc

+7-11
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,13 @@ sdf::Errors ServerPrivate::LoadSdfRootHelper(const ServerConfig &_config)
606606
{
607607
sdf::Errors errors;
608608

609+
sdf::ParserConfig sdfParserConfig = sdf::ParserConfig::GlobalConfig();
610+
sdfParserConfig.SetStoreResolvedURIs(true);
611+
sdfParserConfig.SetCalculateInertialConfiguration(
612+
sdf::ConfigureResolveAutoInertials::SKIP_CALCULATION_IN_LOAD);
613+
MeshInertiaCalculator meshInertiaCalculator;
614+
sdfParserConfig.RegisterCustomInertiaCalc(meshInertiaCalculator);
615+
609616
switch (_config.Source())
610617
{
611618
// Load a world if specified. Check SDF string first, then SDF file
@@ -628,10 +635,6 @@ sdf::Errors ServerPrivate::LoadSdfRootHelper(const ServerConfig &_config)
628635
msg += "File path [" + _config.SdfFile() + "].\n";
629636
}
630637
gzmsg << msg;
631-
sdf::ParserConfig sdfParserConfig = sdf::ParserConfig::GlobalConfig();
632-
sdfParserConfig.SetStoreResolvedURIs(true);
633-
sdfParserConfig.SetCalculateInertialConfiguration(
634-
sdf::ConfigureResolveAutoInertials::SKIP_CALCULATION_IN_LOAD);
635638
errors = this->sdfRoot.LoadSdfString(
636639
_config.SdfString(), sdfParserConfig);
637640
this->sdfRoot.ResolveAutoInertials(errors, sdfParserConfig);
@@ -655,13 +658,6 @@ sdf::Errors ServerPrivate::LoadSdfRootHelper(const ServerConfig &_config)
655658
gzmsg << "Loading SDF world file[" << filePath << "].\n";
656659

657660
sdf::Root sdfRootLocal;
658-
sdf::ParserConfig sdfParserConfig = sdf::ParserConfig::GlobalConfig();
659-
sdfParserConfig.SetStoreResolvedURIs(true);
660-
sdfParserConfig.SetCalculateInertialConfiguration(
661-
sdf::ConfigureResolveAutoInertials::SKIP_CALCULATION_IN_LOAD);
662-
663-
MeshInertiaCalculator meshInertiaCalculator;
664-
sdfParserConfig.RegisterCustomInertiaCalc(meshInertiaCalculator);
665661
// \todo(nkoenig) Async resource download.
666662
// This call can block for a long period of time while
667663
// resources are downloaded. Blocking here causes the GUI to block with

test/integration/mesh_inertia_calculation.cc

+47-19
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,39 @@ class MeshInertiaCalculationTest : public InternalFixture<::testing::Test>
5151
{
5252
};
5353

54-
TEST(MeshInertiaCalculationTest, CylinderColladaMeshInertiaCalculation)
54+
/// \brief Load an SDF world and run mesh inertia tests. Two tests are run
55+
/// one after another: 1) the server is launched with path to SDF file, and
56+
/// 2) ther server is launched from an sdf string.
57+
/// \param[in] _path Path to SDF
58+
/// \param[in] _testFunc Test function that checks mesh inertia values
59+
void loadSdfAndTest(const std::string &_path,
60+
std::function<void(const gz::sim::ServerConfig &)> _testFunc)
5561
{
56-
size_t kIter = 100u;
57-
58-
// Start server and run.
62+
// Test mesh inertial calculator with sdf loaded from file
5963
gz::sim::ServerConfig serverConfig;
60-
serverConfig.SetSdfFile(common::joinPaths(
61-
PROJECT_SOURCE_PATH, "test", "worlds", "mesh_inertia_calculation.sdf"));
62-
64+
serverConfig.SetSdfFile(_path);
6365
common::setenv(
6466
"GZ_SIM_RESOURCE_PATH",
6567
common::joinPaths(PROJECT_SOURCE_PATH, "test", "worlds", "models"));
68+
_testFunc(serverConfig);
6669

67-
gz::sim::Server server(serverConfig);
70+
71+
// Test mesh inertial calculator with sdf loaded from string
72+
std::ifstream sdfFile(_path);
73+
std::stringstream buffer;
74+
buffer << sdfFile.rdbuf();
75+
gz::sim::ServerConfig serverConfigSdfStr;
76+
serverConfigSdfStr.SetSdfString(buffer.str());
77+
_testFunc(serverConfigSdfStr);
78+
}
79+
80+
void cylinderColladaMeshInertiaCalculation(
81+
const gz::sim::ServerConfig &_serverConfig)
82+
{
83+
size_t kIter = 100u;
84+
85+
// Start server and run.
86+
gz::sim::Server server(_serverConfig);
6887

6988
// Create a system just to get the ECM
7089
EntityComponentManager *ecm;
@@ -127,21 +146,20 @@ TEST(MeshInertiaCalculationTest, CylinderColladaMeshInertiaCalculation)
127146
EXPECT_EQ(link.WorldInertialPose(*ecm).value(), gz::math::Pose3d::Zero);
128147
}
129148

130-
TEST(MeshInertiaCalculationTest,
131-
CylinderColladaMeshWithNonCenterOriginInertiaCalculation)
149+
TEST(MeshInertiaCalculationTest, CylinderColladaMeshInertiaCalculation)
150+
{
151+
std::string sdfFilePath = common::joinPaths(
152+
PROJECT_SOURCE_PATH, "test", "worlds", "mesh_inertia_calculation.sdf");
153+
loadSdfAndTest(sdfFilePath, cylinderColladaMeshInertiaCalculation);
154+
}
155+
156+
void cylinderColladaMeshWithNonCenterOriginInertiaCalculation(
157+
const gz::sim::ServerConfig &_serverConfig)
132158
{
133159
size_t kIter = 100u;
134160

135161
// Start server and run.
136-
gz::sim::ServerConfig serverConfig;
137-
serverConfig.SetSdfFile(common::joinPaths(
138-
PROJECT_SOURCE_PATH, "test", "worlds", "mesh_inertia_calculation.sdf"));
139-
140-
common::setenv(
141-
"GZ_SIM_RESOURCE_PATH",
142-
common::joinPaths(PROJECT_SOURCE_PATH, "test", "worlds", "models"));
143-
144-
gz::sim::Server server(serverConfig);
162+
gz::sim::Server server(_serverConfig);
145163

146164
// Create a system just to get the ECM
147165
EntityComponentManager *ecm;
@@ -207,6 +225,16 @@ TEST(MeshInertiaCalculationTest,
207225
// the center of mass (inertial pose) will be 1m above the ground
208226
EXPECT_EQ(link.WorldInertialPose(*ecm).value(),
209227
gz::math::Pose3d(0, 0, 1, 0, 0, 0));
228+
229+
}
230+
231+
TEST(MeshInertiaCalculationTest,
232+
CylinderColladaMeshWithNonCenterOriginInertiaCalculation)
233+
{
234+
std::string sdfFilePath = common::joinPaths(
235+
PROJECT_SOURCE_PATH, "test", "worlds", "mesh_inertia_calculation.sdf");
236+
237+
loadSdfAndTest(sdfFilePath, cylinderColladaMeshInertiaCalculation);
210238
}
211239

212240
TEST(MeshInertiaCalculationTest, CylinderColladaOptimizedMeshInertiaCalculation)

0 commit comments

Comments
 (0)