add handles for text and image overlays
This commit is contained in:
@@ -148,6 +148,50 @@ class SimulateWrapper {
|
||||
|
||||
void ClearFigures() { simulate_->user_figures_.clear(); }
|
||||
|
||||
void SetOverlayText(
|
||||
const std::vector<std::tuple<int, int, std::string, std::string>>& overlay_texts) {
|
||||
// Collection of [font, gridpos, text1, text2] tuples for overlay text
|
||||
std::vector<std::tuple<int, int, std::string, std::string>> user_overlay_text;
|
||||
for (const auto& [font, gridpos, text1, text2] : overlay_texts) {
|
||||
user_overlay_text.push_back(std::make_tuple(font, gridpos, text1, text2));
|
||||
}
|
||||
|
||||
// Set them all at once to prevent overlay text flickering.
|
||||
simulate_->user_text_ = user_overlay_text;
|
||||
}
|
||||
|
||||
void ClearOverlayText() { simulate_->user_text_.clear(); }
|
||||
|
||||
void SetImages(
|
||||
const std::vector<std::tuple<mjrRect, pybind11::array_t<unsigned char>>>& viewport_images
|
||||
) {
|
||||
// Clear previous images to prevent memory leaks
|
||||
simulate_->user_images_.clear();
|
||||
|
||||
for (const auto& [viewport, image] : viewport_images) {
|
||||
auto buf = image.request();
|
||||
if (static_cast<int>(buf.shape[2]) != 3) {
|
||||
throw std::invalid_argument("image must have 3 channels");
|
||||
}
|
||||
if (buf.ndim != 3) {
|
||||
throw std::invalid_argument("image must have 3 dimensions (H, W, C)");
|
||||
}
|
||||
|
||||
// Calculate size of the image data
|
||||
size_t height = buf.shape[0];
|
||||
size_t width = buf.shape[1];
|
||||
size_t size = height * width * 3;
|
||||
|
||||
// Make a copy of the image data to prevent flickering
|
||||
unsigned char* image_copy = new unsigned char[size];
|
||||
std::memcpy(image_copy, buf.ptr, size);
|
||||
|
||||
simulate_->user_images_.push_back(std::make_tuple(viewport, image_copy));
|
||||
}
|
||||
}
|
||||
|
||||
void ClearImages() { simulate_->user_images_.clear(); }
|
||||
|
||||
private:
|
||||
mujoco::Simulate* simulate_;
|
||||
std::atomic_int destroyed_ = 0;
|
||||
@@ -249,6 +293,12 @@ PYBIND11_MODULE(_simulate, pymodule) {
|
||||
.def("set_figures", &SimulateWrapper::SetFigures,
|
||||
py::arg("viewports_figures"))
|
||||
.def("clear_figures", &SimulateWrapper::ClearFigures)
|
||||
.def("overlay_text", &SimulateWrapper::SetOverlayText,
|
||||
py::arg("overlay_texts"))
|
||||
.def("clear_overlay_text", &SimulateWrapper::ClearOverlayText)
|
||||
.def("set_images", &SimulateWrapper::SetImages,
|
||||
py::arg("viewports_images"))
|
||||
.def("clear_images", &SimulateWrapper::ClearImages)
|
||||
.def_property_readonly("m", &SimulateWrapper::GetModel)
|
||||
.def_property_readonly("d", &SimulateWrapper::GetData)
|
||||
.def_property_readonly("viewport", &SimulateWrapper::GetViewport)
|
||||
|
||||
Reference in New Issue
Block a user