python-plyfile icon indicating copy to clipboard operation
python-plyfile copied to clipboard

Incorrect rotation problem

Open pesa2000 opened this issue 1 year ago • 1 comments

Hi, i am using PlyFile to load and rotate a scene, the output position is indeed correct but the orientation of the point clouds are not.

Using this code:

import numpy as np
from plyfile import PlyData, PlyElement

def rotate_point_cloud(points, rotation_matrix):
    return points.dot(rotation_matrix.T)

# Load the PLY file
input_filename = "input.ply"
output_filename = "rotated_output.ply"

plydata = PlyData.read(input_filename)
vertex_data = plydata['vertex'].data

# Extract points
points = np.vstack([vertex_data['x'], vertex_data['y'], vertex_data['z']]).T

# Define the rotation matrix for a 90 degree rotation around the y-axis (to the right)
rotation_angle = np.pi / 2  # 90 degrees
rotation_matrix = np.array([[np.cos(rotation_angle), 0, np.sin(rotation_angle)],
                            [0, 1, 0],
                            [-np.sin(rotation_angle), 0, np.cos(rotation_angle)]])

# Rotate the points
rotated_points = rotate_point_cloud(points, rotation_matrix)

# Update the vertex data with rotated points
vertex_data['x'] = rotated_points[:, 0]
vertex_data['y'] = rotated_points[:, 1]
vertex_data['z'] = rotated_points[:, 2]

# Create a new PlyElement for the rotated vertices
vertex_element = PlyElement.describe(vertex_data, 'vertex')

# Write the rotated point cloud to a new PLY file in binary little-endian format
PlyData([vertex_element], text=False, byte_order='<').write(output_filename)

print(f"Rotated point cloud saved to {output_filename}")

I get the following output:

image

Any idea of what could be? Thank you very much in advance.

pesa2000 avatar Jun 08 '24 12:06 pesa2000

I guess it theoretically could be a plyfile bug, but from what you wrote, I think it's more likely that the bug is somewhere else. However, if you think it really is something in plyfile, it would help me a lot if you could minimize your failing example so that I and others could reproduce it.

dranjan avatar Jun 09 '24 11:06 dranjan

Closing due to lack of information. Feel free to reopen if you can confirm it's a plyfile bug or can provide enough data for me to debug this.

dranjan avatar Aug 05 '24 00:08 dranjan