Skip to content

Commit

Permalink
Allow setting view status from json string (#6241)
Browse files Browse the repository at this point in the history
  • Loading branch information
yxlao authored Jul 7, 2023
1 parent 27ac413 commit 5676513
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
11 changes: 10 additions & 1 deletion cpp/open3d/visualization/visualizer/Visualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
44 changes: 25 additions & 19 deletions cpp/open3d/visualization/visualizer/VisualizerRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<geometry::Image> Visualizer::CaptureScreenFloatBuffer(
Expand Down
9 changes: 8 additions & 1 deletion cpp/pybind/visualization/visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_<VisualizerWithKeyCallback,
PyVisualizer<VisualizerWithKeyCallback>,
Expand Down

0 comments on commit 5676513

Please sign in to comment.