Trying to insert a sphere defect (.ply) into an object (.obj) using Voxel Method bypassing Airspaces and Vertices using the open3d. Please help :)
- Dominant language
- C++
- Stars
- 14k
- Forks
- 2.6k
- Avg merge
- 5d 18h
- Merged PRs (30d)
- 6
Description
### Checklist
- [X] I have searched for [similar issues](https://github.com/isl-org/Open3D/issues).
- [X] For Python issues, I have tested with the [latest development wheel](https://www.open3d.org/docs/latest/getting_started.html#development-version-pip).
- [X] I have checked the [release documentation](https://www.open3d.org/docs/release/) and the [latest documentation](https://www.open3d.org/docs/latest/) (for `main` branch).
### My Question
get_voxel_center() doesn't work in the code below:
```
import open3d as o3d
import numpy as np
def main():
# File paths for the try.obj and sphere.ply files
try_obj_path = '/Users/xd_anshul/Desktop/Research/try.obj'
defect_path = '/Users/xd_anshul/Desktop/Research/Project Defect/sphere.ply'
# Load meshes
try_mesh = o3d.io.read_triangle_mesh(try_obj_path)
defect_mesh = o3d.io.read_triangle_mesh(defect_path)
# Apply transformations if needed (scaling, translation, rotation)
scaled_defect = defect_mesh.scale(0.5, center=defect_mesh.get_center())
# Convert meshes to voxel grids
voxel_size = 0.2 # Adjust voxel size as needed
try_voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh(try_mesh, voxel_size)
defect_voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh(scaled_defect, voxel_size)
# Get coordinates of occupied voxels in the try.obj mesh
occupied_voxels_coords = np.array(try_voxel_grid.get_voxels())
# Select a random occupied voxel
voxel_index = np.random.randint(0, len(occupied_voxels_coords))
selected_voxel = occupied_voxels_coords[voxel_index]
# Translate the defect to the selected voxel's position
translation_vector = try_voxel_grid.get_voxel_center(selected_voxel)
scaled_defect.translate(translation_vector)
# Merge meshes
combined_mesh = try_mesh + scaled_defect
# Visualize combined mesh
o3d.visualization.draw_geometries([combined_mesh])
if __name__ == "__main__":
main()
```
To bypass get_voxel_center(), I manually calculated center but this error still persist:
```
import open3d as o3d
import numpy as np
def main():
# File paths for the try.obj and sphere.ply files
try_obj_path = '/Users/xd_anshul/Desktop/Research/try.obj'
defect_path = '/Users/xd_anshul/Desktop/Research/Project Defect/sphere.ply'
# Load meshes
try_mesh = o3d.io.read_triangle_mesh(try_obj_path)
defect_mesh = o3d.io.read_triangle_mesh(defect_path)
# Apply transformations if needed (scaling, translation, rotation)
scaled_defect = defect_mesh.scale(0.5, center=defect_mesh.get_center())
# Convert meshes to voxel grids
voxel_size = 0.2 # Adjust voxel size as needed
try_voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh(try_mesh, voxel_size)
defect_voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh(scaled_defect, voxel_size)
# Get coordinates of occupied voxels in the try.obj mesh
occupied_voxels_coords = np.array(try_voxel_grid.get_voxels())
# Select a random occupied voxel
voxel_index = np.random.randint(0, len(occupied_voxels_coords))
selected_voxel = occupied_voxels_coords[voxel_index]
# Compute the voxel center based on its index and size
voxel_center = np.array(selected_voxel) * voxel_size + voxel_size / 2
# Translate the defect to the selected voxel's position
scaled_defect.translate(voxel_center)
# Merge meshes
combined_mesh = try_mesh + scaled_defect
# Visualize combined mesh
o3d.visualization.draw_geometries([combined_mesh])
if __name__ == "__main__":
main()
```
Here are both the errors for the 2 codes above respectively:
```
(base) xd_anshul@MacBook-Air ~ % python -u "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py"
Traceback (most recent call last):
File "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py", line 39, in
main()
File "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py", line 29, in main
translation_vector = try_voxel_grid.get_voxel_center(selected_voxel)
AttributeError: 'open3d.cpu.pybind.geometry.VoxelGrid' object has no attribute 'get_voxel_center'
```
```
(base) xd_anshul@MacBook-Air ~ % python -u "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py"
Traceback (most recent call last):
File "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py", line 41, in
main()
File "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py", line 29, in main
voxel_center = np.array(selected_voxel) * voxel_size + voxel_size / 2
TypeError: unsupported operand type(s) for *: 'open3d.cpu.pybind.geometry.Voxel' and 'float'
```
Tried using trimesh as well as VTK library but voxelization is not happening which is necessary to insert the defect onto the solid region/part of the object and not in any of the airspace because the object is hollow and very dissimilar to the defect which is itself a sphere.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.