-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_mesh_variables_creator.py
53 lines (41 loc) · 1.85 KB
/
model_mesh_variables_creator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import time
import open3d as o3d
import os
import trimesh
import pyglet
target_load = o3d.io.read_point_cloud('model.ply')
point_cloud = np.asarray(target_load.points)
meshlab_model_mesh = trimesh.load('model.stl')
vertices = np.asarray(meshlab_model_mesh.vertices)
faces = np.asarray(meshlab_model_mesh.faces)
indexes = []
for row in range(point_cloud.shape[0]):
found = False
for row2 in range(vertices.shape[0]):
if row == row2:
found = True
if found == False:
indexes.append(row)
indexes = np.asarray(indexes)
trimmed = np.delete(point_cloud, indexes, axis=0)
T = np.matmul(vertices, np.linalg.pinv(trimmed))
PCs = 5
# INITIALISE THE MODEL
pca_components = np.load(pca_components.npy')[0:PCs, :]
pca_mean = np.load(pca_mean.npy')
X_new = np.matmul(np.zeros([1, PCs]), np.transpose(np.linalg.pinv(pca_components))) + np.transpose(pca_mean)
model = np.zeros([int(X_new.size / 3), 3])
model[:, 0] = X_new[0, 0:int(X_new.size / 3)]
model[:, 1] = X_new[0, int(X_new.size / 3):int(2 * X_new.size / 3)]
model[:, 2] = X_new[0, int(2 * X_new.size / 3):int(X_new.size)]
trimmed_model = np.delete(model, indexes, axis=0)
reordered_model = np.matmul(T, trimmed_model)
mesh = trimesh.Trimesh(vertices=reordered_model,
faces=faces)
np.save('C:/Users/monre/OneDrive - Imperial College London/ME4/FYP/DATA/Depth_Camera_Data/Points_to_Surface/faces_connectivity_file.npy', faces)
np.save('C:/Users/monre/OneDrive - Imperial College London/ME4/FYP/DATA/Depth_Camera_Data/Points_to_Surface/mesh_trimming_indexes.npy', indexes)
np.save('C:/Users/monre/OneDrive - Imperial College London/ME4/FYP/DATA/Depth_Camera_Data/Points_to_Surface/reordering_mesh_transformation.npy', T)
mesh.show()