From 567651372bc46375b67b0c3fce9c6484d57d2735 Mon Sep 17 00:00:00 2001 From: Yixing Lao Date: Fri, 7 Jul 2023 13:15:20 +0800 Subject: [PATCH] Allow setting view status from json string (#6241) --- .../visualization/visualizer/Visualizer.h | 11 ++++- .../visualizer/VisualizerRender.cpp | 44 +++++++++++-------- cpp/pybind/visualization/visualizer.cpp | 9 +++- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/cpp/open3d/visualization/visualizer/Visualizer.h b/cpp/open3d/visualization/visualizer/Visualizer.h index a253b753f32..02126229bc3 100644 --- a/cpp/open3d/visualization/visualizer/Visualizer.h +++ b/cpp/open3d/visualization/visualizer/Visualizer.h @@ -209,11 +209,18 @@ class Visualizer { bool do_render = true, bool convert_to_world_coordinate = false); void CaptureRenderOption(const std::string &filename = ""); + /// Function to reset view point. void ResetViewPoint(bool reset_bounding_box = false); const std::string &GetWindowName() const { return window_name_; } + /// Get the current view status as a json string of ViewTrajectory. + std::string GetViewStatus(); + + /// Set the current view status from a json string of ViewTrajectory. + void SetViewStatus(const std::string &view_status_str); + protected: /// Function to initialize OpenGL virtual bool InitOpenGL(); @@ -229,11 +236,13 @@ class Visualizer { /// meshes individually). virtual void Render(bool render_screen = false); + /// Copy the current view status to clipboard. void CopyViewStatusToClipboard(); + /// Apply the view point from clipboard. void CopyViewStatusFromClipboard(); - // callback functions + /// Callback functions virtual void WindowRefreshCallback(GLFWwindow *window); virtual void WindowResizeCallback(GLFWwindow *window, int w, int h); virtual void MouseMoveCallback(GLFWwindow *window, double x, double y); diff --git a/cpp/open3d/visualization/visualizer/VisualizerRender.cpp b/cpp/open3d/visualization/visualizer/VisualizerRender.cpp index 9669f995249..175d11f99f8 100644 --- a/cpp/open3d/visualization/visualizer/VisualizerRender.cpp +++ b/cpp/open3d/visualization/visualizer/VisualizerRender.cpp @@ -143,34 +143,40 @@ void Visualizer::ResetViewPoint(bool reset_bounding_box /* = false*/) { } void Visualizer::CopyViewStatusToClipboard() { + glfwSetClipboardString(window_, GetViewStatus().c_str()); +} + +void Visualizer::CopyViewStatusFromClipboard() { + const char *clipboard_string_buffer = glfwGetClipboardString(window_); + if (clipboard_string_buffer != nullptr) { + std::string clipboard_string(clipboard_string_buffer); + SetViewStatus(clipboard_string); + } +} + +std::string Visualizer::GetViewStatus() { ViewParameters current_status; if (!view_control_ptr_->ConvertToViewParameters(current_status)) { - utility::LogWarning("Something is wrong copying view status."); + utility::LogWarning("Cannot convert to view parameters."); } ViewTrajectory trajectory; trajectory.view_status_.push_back(current_status); - std::string clipboard_string; - if (!io::WriteIJsonConvertibleToJSONString(clipboard_string, trajectory)) { - utility::LogWarning("Something is wrong copying view status."); + std::string view_status_str; + if (!io::WriteIJsonConvertibleToJSONString(view_status_str, trajectory)) { + utility::LogWarning("Cannot convert ViewTrajectory to json string."); } - glfwSetClipboardString(window_, clipboard_string.c_str()); + return view_status_str; } -void Visualizer::CopyViewStatusFromClipboard() { - const char *clipboard_string_buffer = glfwGetClipboardString(window_); - if (clipboard_string_buffer != NULL) { - std::string clipboard_string(clipboard_string_buffer); - ViewTrajectory trajectory; - if (!io::ReadIJsonConvertibleFromJSONString(clipboard_string, - trajectory)) { - utility::LogWarning("Something is wrong copying view status."); - } - if (trajectory.view_status_.size() != 1) { - utility::LogWarning("Something is wrong copying view status."); - } - view_control_ptr_->ConvertFromViewParameters( - trajectory.view_status_[0]); +void Visualizer::SetViewStatus(const std::string &view_status_str) { + ViewTrajectory trajectory; + if (!io::ReadIJsonConvertibleFromJSONString(view_status_str, trajectory)) { + utility::LogWarning("Cannot convert string to view status."); + } + if (trajectory.view_status_.size() != 1) { + utility::LogWarning("Cannot convert string to view status."); } + view_control_ptr_->ConvertFromViewParameters(trajectory.view_status_[0]); } std::shared_ptr Visualizer::CaptureScreenFloatBuffer( diff --git a/cpp/pybind/visualization/visualizer.cpp b/cpp/pybind/visualization/visualizer.cpp index 8d2521a4d7b..7b76396c7c3 100644 --- a/cpp/pybind/visualization/visualizer.cpp +++ b/cpp/pybind/visualization/visualizer.cpp @@ -117,7 +117,14 @@ void pybind_visualizer(py::module &m) { &Visualizer::CaptureDepthPointCloud, "Function to capture and save local point cloud", "filename"_a, "do_render"_a = false, "convert_to_world_coordinate"_a = false) - .def("get_window_name", &Visualizer::GetWindowName); + .def("get_window_name", &Visualizer::GetWindowName) + .def("get_view_status", &Visualizer::GetViewStatus, + "Get the current view status as a json string of " + "ViewTrajectory.") + .def("set_view_status", &Visualizer::SetViewStatus, + "Set the current view status from a json string of " + "ViewTrajectory.", + "view_status_str"_a); py::class_,