You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 indices of occupied voxels in the try.obj mesh
occupied_voxels_indices = np.array(try_voxel_grid.get_voxels())
# Select a random occupied voxel index
voxel_index = np.random.randint(0, len(occupied_voxels_indices))
selected_voxel_index = occupied_voxels_indices[voxel_index]
# Get the voxel center of the selected voxel
voxel_center = try_voxel_grid.get_voxel_center(selected_voxel_index)
# 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()
Then I tried to bypass get_voxel_center() with manual calculation but theres an error to this as well.
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 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.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
get_voxel_center() doesn't work.
Then I tried to bypass get_voxel_center() with manual calculation but theres an error to this as well.
Here are both the errors respectively:
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.
Beta Was this translation helpful? Give feedback.
All reactions