Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting view status from json string #6241

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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