1. Rotation matrix (right-handed coordinate system)
Rotate around the x-axis
Rotation matrix: The matrix on the right is the original coordinates of the point cloud, and the matrix on the left is the rotation matrix
Visualization: Rotate 90 degrees around the x-axis
Code:
import vtk import numpy as np import math def pointPolydataCreate(pointCloud): points = vtk.vtkPoints() cells = vtk.vtkCellArray() i = 0 for point in pointCloud: points.InsertPoint(i, point[0], point[1], point[2]) cells.InsertNextCell(1) cells.InsertCellPoint(i) i+=1 PolyData = vtk.vtkPolyData() PolyData.SetPoints(points) PolyData.SetVerts(cells) mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(PolyData) actor = vtk.vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetColor(0.0, 0.1, 1.0) return actor def visiualize(pointCloud, pointCloud2): colors = vtk.vtkNamedColors() actor1 = pointPolydataCreate(pointCloud) actor2 = pointPolydataCreate(pointCloud2) Axes = vtk.vtkAxesActor() # Visualization renderer1 = vtk.vtkRenderer() renderer1.SetViewport(0.0, 0.0, 0.5, 1) renderer1.AddActor(actor1) renderer1.AddActor(Axes) renderer1.SetBackground(colors.GetColor3d('skyblue')) renderer2 = vtk.vtkRenderer() renderer2.SetViewport(0.5, 0.0, 1.0, 1) renderer2.AddActor(actor2) renderer2.AddActor(Axes) renderer2.SetBackground(colors.GetColor3d('skyblue')) renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer1) renderWindow.AddRenderer(renderer2) renderWindow.SetSize(1040, 880) renderWindow.Render() renderWindow.SetWindowName('PointCloud') renderWindowInteractor = vtk.vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) renderWindowInteractor.Initialize() renderWindowInteractor.Start() pointCloud = np.loadtxt("C:/Users/A/Desktop/pointCloudData/model.txt") #Read point cloud data angel_x = 90 # rotation angle radian = angel_x * np.pi / 180 # Rotation radian Rotation_Matrix_1 = [ # Three-dimensional rotation matrix around the x-axis [1, 0, 0], [0, math.cos(radian), -math.sin(radian)], [0, math.sin(radian), math.cos(radian)]] Rotation_Matrix_1 = np.array(Rotation_Matrix_1) p = np.dot(Rotation_Matrix_1, pointCloud.T) # Calculation p = p.T visiualize(pointCloud, p)
Rotate around the y-axis
Rotation matrix:
Visualization: Rotate 180 degrees around the y-axis
Code:
angel_y = 180 # Rotation angle radian = angel_y * np.pi / 180 # Rotation radian Rotation_Matrix_2 = [ # Three-dimensional rotation matrix around the y-axis [math.cos(radian), 0, math.sin(radian)], [0, 1, 0], [-math.sin(radian), 0, math.cos(radian)]] Rotation_Matrix_1 = np.array(Rotation_Matrix_1) p = np.dot(Rotation_Matrix_1, pointCloud.T) # Calculation p = p.T visiualize(pointCloud, p)
Rotate around the z-axis
Rotation matrix:
Visualization: Rotate 90 degrees around the z-axis
Code:
angel_z = 90 # Rotation angle radian = angel_z * np.pi / 180 # Rotation radian Rotation_Matrix_1 = [ # Three-dimensional rotation matrix around the z-axis [math.cos(radian), -math.sin(radian), 0], [math.sin(radian), math.cos(radian), 0], [0, 0, 1]] Rotation_Matrix_1 = np.array(Rotation_Matrix_1) p = np.dot(Rotation_Matrix_1, pointCloud.T) # Calculation p = p.T visiualize(pointCloud, p)
The line rotates around the z-axis and then around the x-axis:
Rotation matrix: Which axis the line rotates around, the xyz matrix is calculated first and the rotation matrix of that axis is calculated first.
Visualization: first rotate 90 degrees around the z-axis, then rotate 90 degrees around the x-axis
Code:
angel_z = 90 # Rotation angle radian = angel_z * np.pi / 180 # Rotation radian Rotation_Matrix_z = [ # Three-dimensional rotation matrix around the z-axis [math.cos(radian), -math.sin(radian), 0], [math.sin(radian), math.cos(radian), 0], [0, 0, 1]] angel_x = 90 # rotation angle radian = angel_x * np.pi / 180 # Rotation radian Rotation_Matrix_x = [ # Three-dimensional rotation matrix around the x-axis [1, 0, 0], [0, math.cos(radian), -math.sin(radian)], [0, math.sin(radian), math.cos(radian)]] Rotation_Matrix_z = np.array(Rotation_Matrix_z) Rotation_Matrix_x = np.array(Rotation_Matrix_x) p = np.dot(Rotation_Matrix_z, pointCloud.T) # Calculation p = np.dot(Rotation_Matrix_x, p) # Calculation p = p.T visiualize(pointCloud, p)
2. Scaling matrix
Scaling matrix:
Calculation process: The three k are the scaling coefficients corresponding to xyz
The x coordinate becomes 1.5 times the original, y becomes 0.7 times, and z remains unchanged
< img alt="\begin{bmatrix} 1.5x\0.7 y \1z \end{bmatrix}" class="mathcode" src="//i2.wp.com /latex.csdn.net/eq?\begin{bmatrix} 1.5x\0.7 y \1z \end{bmatrix}">
Visualization:
3. Mirror matrix
3D mirror matrix:
Vector n is the unit vector perpendicular to the mirror plane
Mirror image of three-dimensional point cloud on xz plane:
①First, determine a unit vector perpendicular to the xz plane n=[0, 1, 0]
②Bring this unit vector into the above 3D mirror matrix
Visualization:
Code:
import vtk import numpy as np import math def pointPolydataCreate(pointCloud): points = vtk.vtkPoints() cells = vtk.vtkCellArray() i = 0 for point in pointCloud: points.InsertPoint(i, point[0], point[1], point[2]) cells.InsertNextCell(1) cells.InsertCellPoint(i) i+=1 PolyData = vtk.vtkPolyData() PolyData.SetPoints(points) PolyData.SetVerts(cells) mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(PolyData) actor = vtk.vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetColor(0.0, 0.1, 1.0) return actor def visiualize(pointCloud, pointCloud2): colors = vtk.vtkNamedColors() actor1 = pointPolydataCreate(pointCloud) actor2 = pointPolydataCreate(pointCloud2) Axes = vtk.vtkAxesActor() # Visualization renderer1 = vtk.vtkRenderer() renderer1.SetViewport(0.0, 0.0, 0.5, 1) renderer1.AddActor(actor1) renderer1.AddActor(Axes) renderer1.SetBackground(colors.GetColor3d('skyblue')) renderer2 = vtk.vtkRenderer() renderer2.SetViewport(0.5, 0.0, 1.0, 1) renderer2.AddActor(actor1) renderer2.AddActor(actor2) renderer2.AddActor(Axes) renderer2.SetBackground(colors.GetColor3d('skyblue')) renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer1) renderWindow.AddRenderer(renderer2) renderWindow.SetSize(1040, 880) renderWindow.Render() renderWindow.SetWindowName('PointCloud') renderWindowInteractor = vtk.vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) renderWindowInteractor.Initialize() renderWindowInteractor.Start() pointCloud = np.loadtxt("C:/Users/A/Desktop/pointCloudData/model.txt") #Read point cloud data nx = 0 n = 0 nz = 1 n = [nx, ny, nz] # Unit vector perpendicular to the xy plane # Mirror matrix Mirror_Matrix = [ [1-2*nx**2, -2*nx*ny, -2*nx*nz], [-2*nx*ny, 1-2*ny**2, -2*ny*nz], [-2*nx*nz, -2*ny*nz, 1-2*nz**2]] Mirror_Matrix = np.array(Mirror_Matrix) p = np.dot(Mirror_Matrix, pointCloud.T) # Calculation p = p.T visiualize(pointCloud, p)
4. Miscut matrix
Off-cut along the xy plane (z remains unchanged)
matrix calculation process
Off-cut along the xz plane (y remains unchanged)
matrix calculation process
Staggered cutting along the yz plane (x remains unchanged)
matrix calculation process
Visualization: staggered cutting along the yz plane
Code:
pointCloud = np.loadtxt("C:/Users/A/Desktop/pointCloudData/model.txt") #Read point cloud data s = 0.3 t=0.3 # Stagger the matrix along the yz plane Shear_Matrix = [ [1, 0, 0], [s, 1, 0], [t, 0, 1]] Shear_Matrix = np.array(Shear_Matrix) p = np.dot(Shear_Matrix, pointCloud.T) # Calculation p = p.T visiualize(pointCloud, p)
5. Orthographic projection
Orthogonal projection matrix (projected to any plane in three-dimensional space):
Vector n is the unit vector normal to the projection plane
Visualization: orthogonal projection of point cloud on xy plane
6. Translation matrix
The translation matrix requires the use of a homogeneous matrix (4*4 matrix). The following is a translation matrix
The rightmost column is the displacement of xyz
Calculation process:
Linear transformation + translation:
The added translation has no effect on the original linear transformation. You can combine the transformation matrix and translation introduced earlier.
For example: miscut + translation along the xy plane
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 386,908 people are learning the system