Merge pull request #801 from hartikainen:fix-mj_sizeModel

PiperOrigin-RevId: 522013801
Change-Id: Iff5e930fb7c86ae4296beb40f266ed02aabb6a7f
This commit is contained in:
Copybara-Service
2023-04-05 04:20:11 -07:00
2 changed files with 47 additions and 2 deletions
+13 -2
View File
@@ -803,8 +803,19 @@ void mj_deleteModel(mjModel* m) {
// size of buffer needed to hold model
int mj_sizeModel(const mjModel* m) {
return sizeof(int)*(4+getnint()) + sizeof(mjOption) +
sizeof(mjVisual) + sizeof(mjStatistic) + m->nbuffer;
int size = (
sizeof(int)*(4+getnint())
+ sizeof(mjOption)
+ sizeof(mjVisual)
+ sizeof(mjStatistic));
MJMODEL_POINTERS_PREAMBLE(m)
#define X(type, name, nr, nc) \
size += sizeof(type)*(m->nr)*(nc);
MJMODEL_POINTERS
#undef X
return size;
}
+34
View File
@@ -18,7 +18,11 @@
#include <array>
#include <climits>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <gmock/gmock.h>
@@ -47,6 +51,36 @@ mjModel PartialModel(const mjModel* m) {
return partial_model;
}
TEST_F(EngineIoTest, VerifySizeModel) {
constexpr char xml[] = R"(
<mujoco>
<worldbody>
<body>
<joint/>
<geom size="1"/>
</body>
</worldbody>
</mujoco>
)";
std::array<char, 1024> error;
mjModel* model = LoadModelFromString(xml, error.data(), error.size());
ASSERT_THAT(model, NotNull()) << "Failed to load model: " << error.data();
std::filesystem::path temp_file = (
std::filesystem::temp_directory_path() / "model.mjb");
mj_saveModel(model, temp_file.string().c_str(), NULL, 0);
std::uintmax_t file_size = std::filesystem::file_size(temp_file);
int model_size = mj_sizeModel(model);
std::filesystem::remove(temp_file);
mj_deleteModel(model);
EXPECT_EQ(file_size, model_size);
}
TEST_F(EngineIoTest, MakeDataFromPartialModel) {
constexpr char xml[] = R"(
<mujoco>