Update spec.from_string() and spec.from_file() to match the respective mjModel functions.

PiperOrigin-RevId: 678230403
Change-Id: I7f3d8c5fd44d30b5695eee1f8513cc27f4158d94
This commit is contained in:
Alessio Quaglino
2024-09-24 06:56:27 -07:00
committed by Copybara-Service
parent 910cd89cc7
commit 5bca7876c4
4 changed files with 138 additions and 79 deletions
-41
View File
@@ -87,21 +87,6 @@ inline std::size_t NConMax(const mjData* d) {
return d->narena / sizeof(mjContact);
}
// strip path prefix from filename and make lowercase
std::string StripPath(const char* name) {
std::string filename(name);
size_t start = filename.find_last_of("/\\");
// get name without path
if (start != std::string::npos) {
filename = filename.substr(start + 1, filename.size() - start - 1);
}
// make lowercase
std::transform(filename.begin(), filename.end(), filename.begin(),
[](unsigned char c) { return std::tolower(c); });
return filename;
}
} // namespace
// ==================== MJOPTION ===============================================
@@ -318,16 +303,6 @@ MjModelWrapper::~MjWrapper() {
}
}
namespace {
struct VfsAsset {
VfsAsset(const char* name, const void* content, std::size_t content_size)
: name(name), content(content), content_size(content_size) {}
const char* name;
const void* content;
std::size_t content_size;
};
}
// Helper function for both LoadXMLFile and LoadBinaryFile.
// Creates a temporary MJB from the assets dictionary if one is supplied.
template <typename LoadFunc>
@@ -365,22 +340,6 @@ static raw::MjModel* LoadModelFileImpl(
return model;
}
// Converts a dict with py::bytes value to a vector of standard C++ types.
// This allows us to release the GIL early. Note that the vector consists only
// of pointers to existing data so no substantial data copies are being made.
static std::vector<VfsAsset>
ConvertAssetsDict(
const std::optional<std::unordered_map<std::string, py::bytes>>& assets) {
std::vector<VfsAsset> out;
if (assets.has_value()) {
for (const auto& [name, content] : *assets) {
out.emplace_back(name.c_str(), PYBIND11_BYTES_AS_STRING(content.ptr()),
py::len(content));
}
}
return out;
}
MjModelWrapper MjModelWrapper::LoadXMLFile(
const std::string& filename,
const std::optional<std::unordered_map<std::string, py::bytes>>& assets) {