Click here to Skip to main content
16,020,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have STL file where I want to apply 4x4 matrix.
The matrix I want to apply is:
[[2.7282, 0, 0, 0], [0, 4.0014, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]


Here is my STL file: https://we.tl/t-v6SHprAeUZ[^]

I have used Python code to apply matrix to the mesh (STL file) but the code didn't help me. It gives me runtime error:
Traceback (most recent call last):
  File "E:\STL\matrix.py", line 17, in <module>
    transformed_point = np.dot(matrix, homogeneous_point)  # Apply the transformation
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: shapes (4,4) and (10,1) not aligned: 4 (dim 1) != 10 (dim 0)


What I have tried:

The code:
Python
import numpy as np
from stl import mesh

# Define your 4x4 transformation matrix
matrix = np.array([[2.7282, 0, 0, 0],
                   [0, 4.0014, 0, 0],
                   [0, 0, 1, 0],
                   [0, 0, 0, 1]])

# Load your STL file and its vertices into a data structure
your_mesh = mesh.Mesh.from_file('E:\\STL\\Mesh4.stl')

# Apply the transformation to each vertex
transformed_vertices = []
for vertex in your_mesh.vectors:
    homogeneous_point = np.append(vertex, 1)[:, np.newaxis]
    transformed_point = np.dot(matrix, homogeneous_point)  # Apply the transformation
    transformed_vertices.append(transformed_point[:3, 0])  # Extract the transformed 3D coordinates

# Create a new mesh with the transformed vertices
new_mesh = mesh.Mesh(np.array(transformed_vertices), remove_empty_areas=False)

# Save the modified mesh to an STL file
new_mesh.save('E:\\STL\\output_mesh.stl')
print("Transformation applied and saved successfully.")
Posted
Updated 2-Oct-23 5:20am
v3
Comments
0x01AA 25-Sep-23 6:59am    
It us much more simple to use transform(matrix)
sahil ajmeri 2022 25-Sep-23 7:28am    
how do I use can you give me more specific detail about How to use It ?
0x01AA 26-Sep-23 17:29pm    
And, any success?
0x01AA 27-Sep-23 15:38pm    
?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900