Fix vfs load for mesh gmeshs type
This commit is contained in:
@@ -99,7 +99,7 @@ mjCFlexcomp::mjCFlexcomp(void) {
|
|||||||
|
|
||||||
|
|
||||||
// make flexcomp object
|
// make flexcomp object
|
||||||
bool mjCFlexcomp::Make(mjsBody* body, char* error, int error_sz) {
|
bool mjCFlexcomp::Make(mjsBody* body, char* error, int error_sz, const mjVFS* vfs) {
|
||||||
mjCModel* model = static_cast<mjCBody*>(body->element)->model;
|
mjCModel* model = static_cast<mjCBody*>(body->element)->model;
|
||||||
mjsCompiler* compiler = static_cast<mjCBody*>(body->element)->compiler;
|
mjsCompiler* compiler = static_cast<mjCBody*>(body->element)->compiler;
|
||||||
mjsFlex* dflex = def.spec.flex;
|
mjsFlex* dflex = def.spec.flex;
|
||||||
@@ -170,11 +170,11 @@ bool mjCFlexcomp::Make(mjsBody* body, char* error, int error_sz) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case mjFCOMPTYPE_MESH:
|
case mjFCOMPTYPE_MESH:
|
||||||
res = MakeMesh(model, compiler, error, error_sz);
|
res = MakeMesh(model, compiler, error, error_sz, vfs);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case mjFCOMPTYPE_GMSH:
|
case mjFCOMPTYPE_GMSH:
|
||||||
res = MakeGMSH(model, compiler, error, error_sz);
|
res = MakeGMSH(model, compiler, error, error_sz, vfs);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case mjFCOMPTYPE_DIRECT:
|
case mjFCOMPTYPE_DIRECT:
|
||||||
@@ -1129,7 +1129,7 @@ template <typename T> static T* VecToArray(std::vector<T>& vector, bool clear =
|
|||||||
|
|
||||||
|
|
||||||
// make mesh
|
// make mesh
|
||||||
bool mjCFlexcomp::MakeMesh(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz) {
|
bool mjCFlexcomp::MakeMesh(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz, const mjVFS* vfs) {
|
||||||
// strip path
|
// strip path
|
||||||
if (!file.empty() && model->spec.strippath) {
|
if (!file.empty() && model->spec.strippath) {
|
||||||
file = mjuu_strippath(file);
|
file = mjuu_strippath(file);
|
||||||
@@ -1156,7 +1156,7 @@ bool mjCFlexcomp::MakeMesh(mjCModel* model, mjsCompiler* compiler, char* error,
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
resource = mjCBase::LoadResource(mjs_getString(model->spec.modelfiledir),
|
resource = mjCBase::LoadResource(mjs_getString(model->spec.modelfiledir),
|
||||||
filename, 0);
|
filename, vfs);
|
||||||
} catch (mjCError err) {
|
} catch (mjCError err) {
|
||||||
return comperr(error, err.message, error_sz);
|
return comperr(error, err.message, error_sz);
|
||||||
}
|
}
|
||||||
@@ -1248,7 +1248,7 @@ static int findstring(const char* buffer, int buffer_sz, const char* str) {
|
|||||||
|
|
||||||
|
|
||||||
// load points and elements from GMSH file
|
// load points and elements from GMSH file
|
||||||
bool mjCFlexcomp::MakeGMSH(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz) {
|
bool mjCFlexcomp::MakeGMSH(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz, const mjVFS* vfs) {
|
||||||
// strip path
|
// strip path
|
||||||
if (!file.empty() && model->spec.strippath) {
|
if (!file.empty() && model->spec.strippath) {
|
||||||
file = mjuu_strippath(file);
|
file = mjuu_strippath(file);
|
||||||
@@ -1264,7 +1264,7 @@ bool mjCFlexcomp::MakeGMSH(mjCModel* model, mjsCompiler* compiler, char* error,
|
|||||||
try {
|
try {
|
||||||
std::string filename = mjuu_combinePaths(mjs_getString(compiler->meshdir), file);
|
std::string filename = mjuu_combinePaths(mjs_getString(compiler->meshdir), file);
|
||||||
resource = mjCBase::LoadResource(mjs_getString(model->spec.modelfiledir),
|
resource = mjCBase::LoadResource(mjs_getString(model->spec.modelfiledir),
|
||||||
filename, 0);
|
filename, vfs);
|
||||||
} catch (mjCError err) {
|
} catch (mjCError err) {
|
||||||
return comperr(error, err.message, error_sz);
|
return comperr(error, err.message, error_sz);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,13 +53,15 @@ typedef enum _mjtDof {
|
|||||||
class mjCFlexcomp {
|
class mjCFlexcomp {
|
||||||
public:
|
public:
|
||||||
mjCFlexcomp(void);
|
mjCFlexcomp(void);
|
||||||
bool Make(mjsBody* body, char* error, int error_sz);
|
bool Make(mjsBody* body, char* error, int error_sz, const mjVFS* vfs = nullptr);
|
||||||
|
|
||||||
bool MakeGrid(char* error, int error_sz);
|
bool MakeGrid(char* error, int error_sz);
|
||||||
bool MakeBox(char* error, int error_sz, int dim, bool open = true);
|
bool MakeBox(char* error, int error_sz, int dim, bool open = true);
|
||||||
bool MakeSquare(char* error, int error_sz);
|
bool MakeSquare(char* error, int error_sz);
|
||||||
bool MakeMesh(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz);
|
bool MakeMesh(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz,
|
||||||
bool MakeGMSH(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz);
|
const mjVFS* vfs = nullptr);
|
||||||
|
bool MakeGMSH(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz,
|
||||||
|
const mjVFS* vfs = nullptr);
|
||||||
void LoadGMSH(mjCModel* model, mjResource* resource);
|
void LoadGMSH(mjCModel* model, mjResource* resource);
|
||||||
void LoadGMSH41(char* buffer, int binary, int nodeend, int nodebegin,
|
void LoadGMSH41(char* buffer, int binary, int nodeend, int nodebegin,
|
||||||
int elemend, int elembegin);
|
int elemend, int elembegin);
|
||||||
|
|||||||
@@ -2849,7 +2849,7 @@ void mjXReader::OneFlexcomp(XMLElement* elem, mjsBody* body, const mjVFS* vfs) {
|
|||||||
|
|
||||||
// make flexcomp
|
// make flexcomp
|
||||||
char error[200];
|
char error[200];
|
||||||
bool res = fcomp.Make(body, error, 200);
|
bool res = fcomp.Make(body, error, 200, vfs);
|
||||||
|
|
||||||
// throw error
|
// throw error
|
||||||
if (!res) {
|
if (!res) {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
// Tests for user/user_model.cc.
|
// Tests for user/user_model.cc.
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cstdio>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <gmock/gmock.h>
|
#include <gmock/gmock.h>
|
||||||
@@ -876,5 +877,47 @@ TEST_F(UserFlexTest, MeshNodePinning) {
|
|||||||
mj_deleteModel(m);
|
mj_deleteModel(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(UserFlexTest, FlexcompMeshLoadsFromVFS) {
|
||||||
|
// read cube.stl from testdata into a buffer
|
||||||
|
const std::string stl_path =
|
||||||
|
GetTestDataFilePath("user/testdata/cube.stl");
|
||||||
|
FILE* f = fopen(stl_path.c_str(), "rb");
|
||||||
|
ASSERT_THAT(f, NotNull()) << "Could not open " << stl_path;
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
long stl_size = ftell(f);
|
||||||
|
fseek(f, 0, SEEK_SET);
|
||||||
|
std::string stl_data(stl_size, '\0');
|
||||||
|
fread(stl_data.data(), 1, stl_size, f);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
// add the STL data to a VFS
|
||||||
|
mjVFS vfs;
|
||||||
|
mj_defaultVFS(&vfs);
|
||||||
|
mj_addBufferVFS(&vfs, "cube.stl", stl_data.data(), stl_size);
|
||||||
|
|
||||||
|
// XML that uses flexcomp type="mesh" referencing the VFS file
|
||||||
|
static constexpr char xml[] = R"(
|
||||||
|
<mujoco>
|
||||||
|
<worldbody>
|
||||||
|
<body name="flex_body" pos="0 0 1">
|
||||||
|
<flexcomp name="flex_object" type="mesh" file="cube.stl" rigid="true">
|
||||||
|
<contact contype="1" conaffinity="1"/>
|
||||||
|
</flexcomp>
|
||||||
|
</body>
|
||||||
|
</worldbody>
|
||||||
|
</mujoco>
|
||||||
|
)";
|
||||||
|
|
||||||
|
// cleanup
|
||||||
|
std::array<char, 1024> error;
|
||||||
|
mjModel* m = LoadModelFromString(xml, error.data(), error.size(), &vfs);
|
||||||
|
ASSERT_THAT(m, NotNull()) << error.data();
|
||||||
|
mjData* d = mj_makeData(m);
|
||||||
|
mj_step(m, d);
|
||||||
|
mj_deleteData(d);
|
||||||
|
mj_deleteModel(m);
|
||||||
|
mj_deleteVFS(&vfs);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace mujoco
|
} // namespace mujoco
|
||||||
|
|||||||
Reference in New Issue
Block a user