Skip to content

Add interface for get cropped geometry from VisualizerWithEditing class #5171

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

Merged
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
97 changes: 58 additions & 39 deletions cpp/open3d/visualization/visualizer/VisualizerWithEditing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ std::vector<size_t> &VisualizerWithEditing::GetPickedPoints() {
return pointcloud_picker_ptr_->picked_indices_;
}

std::shared_ptr<geometry::Geometry> VisualizerWithEditing::GetCroppedGeometry()
const {
if (editing_geometry_ptr_->GetGeometryType() ==
geometry::Geometry::GeometryType::PointCloud)
return std::make_shared<geometry::PointCloud>(
*std::dynamic_pointer_cast<geometry::PointCloud>(
editing_geometry_ptr_));
else {
return std::make_shared<geometry::TriangleMesh>(
*std::dynamic_pointer_cast<geometry::TriangleMesh>(
editing_geometry_ptr_));
}
}

bool VisualizerWithEditing::InitViewControl() {
view_control_ptr_ =
std::unique_ptr<ViewControlWithEditing>(new ViewControlWithEditing);
Expand Down Expand Up @@ -399,26 +413,6 @@ void VisualizerWithEditing::KeyPressCallback(
"nullptr.");
}
editing_geometry_renderer_ptr_->UpdateGeometry();
const char *filename;
const char *pattern[1] = {"*.ply"};
std::string default_filename =
default_directory_ + "cropped_" +
std::to_string(crop_action_count_ + 1) + ".ply";
if (use_dialog_) {
filename = tinyfd_saveFileDialog(
"geometry::PointCloud file",
default_filename.c_str(), 1, pattern,
"Polygon File Format (*.ply)");
} else {
filename = default_filename.c_str();
}
if (filename == NULL) {
utility::LogWarning(
"No filename is given. Abort saving.");
} else {
SaveCroppingResult(filename);
crop_action_count_++;
}
view_control.ToggleLocking();
InvalidateSelectionPolygon();
InvalidatePicking();
Expand All @@ -439,25 +433,6 @@ void VisualizerWithEditing::KeyPressCallback(
"nullptr.");
}
editing_geometry_renderer_ptr_->UpdateGeometry();
const char *filename;
const char *pattern[1] = {"*.ply"};
std::string default_filename =
default_directory_ + "cropped_" +
std::to_string(crop_action_count_ + 1) + ".ply";
if (use_dialog_) {
filename = tinyfd_saveFileDialog(
"Mesh file", default_filename.c_str(), 1,
pattern, "Polygon File Format (*.ply)");
} else {
filename = default_filename.c_str();
}
if (filename == NULL) {
utility::LogWarning(
"No filename is given. Abort saving.");
} else {
SaveCroppingResult(filename);
crop_action_count_++;
}
view_control.ToggleLocking();
InvalidateSelectionPolygon();
InvalidatePicking();
Expand All @@ -467,6 +442,50 @@ void VisualizerWithEditing::KeyPressCallback(
mods);
}
break;
case GLFW_KEY_S:
if (editing_geometry_ptr_ &&
editing_geometry_ptr_->GetGeometryType() ==
geometry::Geometry::GeometryType::PointCloud) {
const char *filename;
const char *pattern[1] = {"*.ply"};
std::string default_filename =
default_directory_ + "cropped_" +
std::to_string(crop_action_count_ + 1) + ".ply";
if (use_dialog_) {
filename = tinyfd_saveFileDialog(
"geometry::PointCloud file",
default_filename.c_str(), 1, pattern,
"Polygon File Format (*.ply)");
} else {
filename = default_filename.c_str();
}
if (filename == NULL) {
utility::LogWarning("No filename is given. Abort saving.");
} else {
SaveCroppingResult(filename);
crop_action_count_++;
}
} else {
const char *filename;
const char *pattern[1] = {"*.ply"};
std::string default_filename =
default_directory_ + "cropped_" +
std::to_string(crop_action_count_ + 1) + ".ply";
if (use_dialog_) {
filename = tinyfd_saveFileDialog(
"Mesh file", default_filename.c_str(), 1, pattern,
"Polygon File Format (*.ply)");
} else {
filename = default_filename.c_str();
}
if (filename == NULL) {
utility::LogWarning("No filename is given. Abort saving.");
} else {
SaveCroppingResult(filename);
crop_action_count_++;
}
}
break;
case GLFW_KEY_MINUS:
if (mods & GLFW_MOD_SHIFT) {
option.DecreaseSphereSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class VisualizerWithEditing : public Visualizer {
void BuildUtilities() override;
int PickPoint(double x, double y);
std::vector<size_t> &GetPickedPoints();
std::shared_ptr<geometry::Geometry> GetCroppedGeometry() const;

protected:
bool InitViewControl() override;
Expand Down
5 changes: 4 additions & 1 deletion cpp/pybind/visualization/visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ void pybind_visualizer(py::module &m) {
vis.GetWindowName();
})
.def("get_picked_points", &VisualizerWithEditing::GetPickedPoints,
"Function to get picked points");
"Function to get picked points")
.def("get_cropped_geometry",
&VisualizerWithEditing::GetCroppedGeometry,
"Function to get cropped geometry");

py::class_<VisualizerWithVertexSelection,
PyVisualizer<VisualizerWithVertexSelection>,
Expand Down
5 changes: 3 additions & 2 deletions examples/python/visualization/interactive_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ def demo_crop_geometry():
print("2) Press 'K' to lock screen and to switch to selection mode")
print("3) Drag for rectangle selection,")
print(" or use ctrl + left click for polygon selection")
print("4) Press 'C' to get a selected geometry and to save it")
print("5) Press 'F' to switch to freeview mode")
print("4) Press 'C' to get a selected geometry")
print("5) Press 'S' to save the selected geometry")
print("6) Press 'F' to switch to freeview mode")
pcd_data = o3d.data.DemoICPPointClouds()
pcd = o3d.io.read_point_cloud(pcd_data.paths[0])
o3d.visualization.draw_geometries_with_editing([pcd])
Expand Down