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

Fix minimal oriented bounding box of MeshBase derived classes and add new unit tests #6898

Merged

Conversation

nicolaloi
Copy link
Contributor

@nicolaloi nicolaloi commented Jul 28, 2024

Type

  • Bug fix (non-breaking change which fixes an issue): Fixes Minimal oriented bounding box is not minimal #6866
  • New feature (non-breaking change which adds functionality). Resolves #
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) Resolves #

Motivation and Context

Fixing the MeshBase::GetMinimalOrientedBoundingBox method, which did not return the minimal oriented bounding box. Also adding new unit tests for the derived classes TriangleMesh and TetraMesh for the different bounding box generation algorithms, since they were not covered.

Checklist:

  • I have run python util/check_style.py --apply to apply Open3D code style
    to my code.
  • This PR changes Open3D behavior or adds new functionality.
    • Both C++ (Doxygen) and Python (Sphinx / Google style) documentation is
      updated accordingly.
    • I have added or updated C++ and / or Python unit tests OR included test
      results
      (e.g. screenshots or numbers) here.
  • I will follow up and update the code if CI fails.
  • For fork PRs, I have selected Allow edits from maintainers.

Description

The MeshBase::GetMinimalOrientedBoundingBox method was calling OrientedBoundingBox::CreateFromPoints (so same functionality as MeshBase::GetOrientedBoundingBox) instead of OrientedBoundingBox::CreateFromPointsMinimal (as done in PointCloud::GetMinimalOrientedBoundingBox). I have also added new unit tests for the minimal oriented bounding box, oriented bounding box, axis-aligned bounding box, and get center for both TriangleMesh and TetraMesh objects.

Testing

Testing code (in the visualization the bounding boxes of the meshes are slightly enlarged for better visibility)

import numpy as np
import open3d as o3d

# Create a point cloud
points = np.array(
    [[0.866, 0.474, 0.659],
     [0.943, 0.025, 0.789],
     [0.386, 0.264, 0.691],
     [0.938, 0.588, 0.496],
     [0.221, 0.116, 0.257],
     [0.744, 0.182, 0.052],
     [0.019, 0.525, 0.699],
     [0.722, 0.134, 0.668]]
)

box_pcl = o3d.geometry.PointCloud()
box_pcl.points = o3d.utility.Vector3dVector(points)
box_pcl.colors = o3d.utility.Vector3dVector(np.random.rand(8, 3))

# Create a triangle mesh from the point cloud
box_triangle_mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_alpha_shape(
    pcd=box_pcl, alpha=1.0
)

# Create a tetra mesh from the point cloud
box_tetra_mesh, size = o3d.geometry.TetraMesh.create_from_point_cloud(box_pcl)

# Get the minimal oriented bounding box of the point cloud
obbox_pcd = box_pcl.get_minimal_oriented_bounding_box(robust=True)
obbox_pcd.color = [0.2, 0.9, 0.2] # green
print(f"Minimal OBB pcd volume (green): {obbox_pcd.volume():.4f}")

# Get the minimal oriented bounding box of the triangle mesh
obbox_triangle_mesh = box_triangle_mesh.get_minimal_oriented_bounding_box(robust=True)
obbox_triangle_mesh.color = [1.0, 0.2, 0.2] # red
print(f"Minimal OBB triangle_mesh volume (red): {obbox_triangle_mesh.volume():.4f}")
obbox_triangle_mesh.scale(1.01, center=obbox_triangle_mesh.get_center())

# Get the minimal oriented bounding box of the tetra mesh
obbox_tetra_mesh = box_tetra_mesh.get_minimal_oriented_bounding_box(robust=True)
obbox_tetra_mesh.color = [0.2, 0.2, 1.0] # blue
print(f"Minimal OBB tetra_mesh volume (blue): {obbox_tetra_mesh.volume():.4f}")
obbox_tetra_mesh.scale(1.02, center=obbox_tetra_mesh.get_center())

# Visualization
o3d.visualization.draw_geometries(
    [box_pcl, obbox_pcd, obbox_triangle_mesh, obbox_tetra_mesh],
    window_name="minimal_obboxes",
    lookat=[0, 0.2, 0.5],
    up=[0, 0, 1],
    front=[1, 0, 0],
    zoom=1,
    point_show_normal=False,
    mesh_show_wireframe=False,
    mesh_show_back_face=False,
)
Before the fix

Before Minimal OBB fix

Minimal OBB pcd volume (green): 0.3591
Minimal OBB triangle_mesh volume (red): 0.4390
Minimal OBB tetra_mesh volume (blue): 0.4390
After the fix

After Minimal OBB fix

Minimal OBB pcd volume (green): 0.3591
Minimal OBB triangle_mesh volume (red): 0.3591
Minimal OBB tetra_mesh volume (blue): 0.3591

@ssheorey ssheorey requested a review from benjaminum July 31, 2024 15:24
@benjaminum
Copy link
Contributor

Thanks @nicolaloi !

@benjaminum benjaminum merged commit 8f5d3b4 into isl-org:main Aug 7, 2024
36 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Minimal oriented bounding box is not minimal
2 participants