pythonvtkvoxelopen3dtrimesh

Trying to insert a sphere defect (.ply) into an object (.obj) using Voxel Method bypassing Airspaces and Vertices using the open3d


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 <module>
    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 <module>
    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.


Solution

  • You should be using get_voxel_center_coordinate instead of get_voxel_center. See open3d docs here.

    translation_vector = try_voxel_grid.get_voxel_center_coordinate(selected_voxel.grid_index)