Reputation: 51
I have 2 different pointclouds stored in 2 different numpy arrays and I can visualize them combined in open3d. But what I want to do is that one pointcloud stays constant and the other moves in the z direction (that is updating the coordinates of numpy array) without closing the window.
Below is the code in which I pass these two numpy arrays (npa and npa_test) to visualize in open3d. Please note that in the initial lines I store numpy arrays to a .pcd files (since they were extracted from different types of files .asc which open3d doesnot recognize) and then read them using open3d read function. I want to update the one pointcloud geomtry by moving it in z direction and then visualizing it on the same window without closing it. This can be done by updating the z cordinates by 1 in for loop as mentioned below. Please let me know if you have a solution regarding this. Thank you.
def draw(npa,npa_test):
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(npa)
o3d.io.write_point_cloud("C:/Users/Junaid/Desktop/test.ply", pcd)
pcd_load = o3d.io.read_point_cloud("C:/Users/Junaid/Desktop/test.ply")
pcd1 = o3d.geometry.PointCloud()
pcd1.points = o3d.utility.Vector3dVector(npa_test)
o3d.io.write_point_cloud("C:/Users/Junaid/Desktop/test1.ply", pcd1)
pcd_load1 = o3d.io.read_point_cloud("C:/Users/Junaid/Desktop/test1.ply")
o3d.utility.set_verbosity_level(o3d.utility.VerbosityLevel.Debug)
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(pcd_load)
vis.add_geometry(pcd_load1)
npa.tolist()
for i in range(len(npa)):
npa[i][2] = npa[i][2] + 1
npa=np.asarray(npa, dtype=np.float32)
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(npa)
o3d.io.write_point_cloud("C:/Users/Junaid/Desktop/test.ply", pcd)
pcd_load = o3d.io.read_point_cloud("C:/Users/Junaid/Desktop/test.ply")
vis.update_geometry(pcd_load)
vis.poll_events()
vis.update_renderer()
vis.destroy_window()
Upvotes: 1
Views: 4144
Reputation: 725
I see that you're using npa[i][2] = npa[i][2] + 1
to modify the value which is overkill when you can just use Transformation Matrix to transform the geometry like pcd.transform(np.array([[1,0,0,0],[0,1,0,0],[0,0,1,z_val],[0,0,0,1]]))
.
Here's easy solution of moving sphere from z=0.5 until 15.0 with increment of 0.005 using open3d.
import open3d as o3d
import numpy as np
def create_transform_matrix_from_z(z):
""" Return transform 4x4 transformation matrix given a Z value """
result = np.identity(4)
result[2,3] = z # Change the z
return result
# Create Open3d visualization window
vis = o3d.visualization.Visualizer()
vis.create_window()
# create sphere geometry
sphere1 = o3d.geometry.TriangleMesh.create_sphere()
vis.add_geometry(sphere1)
# create coordinate frame
coordinate_frame = o3d.geometry.TriangleMesh.create_coordinate_frame()
vis.add_geometry(coordinate_frame)
prev_tf = None
for curr_z in np.arange(0.5, 15.0, 0.005):
# return sphere1 to original position (0,0,0)
if prev_tf is not None:
sphere1.transform(np.linalg.inv(prev_tf))
# transform bazed on curr_z tf
curr_tf = create_transform_matrix_from_z(curr_z)
sphere1.transform(curr_tf)
prev_tf = curr_tf
vis.update_geometry(sphere1)
vis.poll_events()
vis.update_renderer()
Upvotes: 4