-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclass_3d_model.py
More file actions
67 lines (61 loc) · 2.83 KB
/
class_3d_model.py
File metadata and controls
67 lines (61 loc) · 2.83 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""
@author: Anton Wang
"""
# 3D model class
import numpy as np
class a_3d_model:
def __init__(self, filepath):
self.model_filepath=filepath
self.load_obj_file()
self.calculate_plane_equations()
self.calculate_Q_matrices()
def load_obj_file(self):
with open(self.model_filepath) as file:
self.points = []
self.faces = []
while 1:
line = file.readline()
if not line:
break
strs = line.split(" ")
if strs[0] == "v":
self.points.append((float(strs[1]), float(strs[2]), float(strs[3])))
if strs[0] == "f":
self.faces.append((int(strs[1]), int(strs[2]), int(strs[3])))
self.points=np.array(self.points)
self.faces=np.array(self.faces)
self.number_of_points=self.points.shape[0]
self.number_of_faces=self.faces.shape[0]
edge_1=self.faces[:,0:2]
edge_2=self.faces[:,1:]
edge_3=np.concatenate([self.faces[:,:1], self.faces[:,-1:]], axis=1)
self.edges=np.concatenate([edge_1, edge_2, edge_3], axis=0)
unique_edges_trans, unique_edges_locs=np.unique(self.edges[:,0]*(10**10)+self.edges[:,1], return_index=True)
self.edges=self.edges[unique_edges_locs,:]
def calculate_plane_equations(self):
self.plane_equ_para = []
for i in range(0, self.number_of_faces):
# solving equation ax+by+cz+d=0, a^2+b^2+c^2=1
# set d=-1, give three points (x1, y1 ,z1), (x2, y2, z2), (x3, y3, z3)
point_1=self.points[self.faces[i,0]-1, :]
point_2=self.points[self.faces[i,1]-1, :]
point_3=self.points[self.faces[i,2]-1, :]
point_mat=np.array([point_1, point_2, point_3])
abc=np.matmul(np.linalg.inv(point_mat), np.array([[1],[1],[1]]))
self.plane_equ_para.append(np.concatenate([abc.T, np.array(-1).reshape(1, 1)], axis=1)/(np.sum(abc**2)**0.5))
self.plane_equ_para=np.array(self.plane_equ_para)
self.plane_equ_para=self.plane_equ_para.reshape(self.plane_equ_para.shape[0], self.plane_equ_para.shape[2])
def calculate_Q_matrices(self):
self.Q_matrices = []
for i in range(0, self.number_of_points):
point_index=i+1
# each point is the solution of the intersection of a set of planes
# find the planes for point_index
face_set_index=np.where(self.faces==point_index)[0]
Q_temp=np.zeros((4,4))
for j in face_set_index:
p=self.plane_equ_para[j,:]
p=p.reshape(1, len(p))
Q_temp=Q_temp+np.matmul(p.T, p)
self.Q_matrices.append(Q_temp)