-
Notifications
You must be signed in to change notification settings - Fork 8
/
eye_reconstructor.py
147 lines (127 loc) · 4.76 KB
/
eye_reconstructor.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import openmesh as om
import numpy as np
from scipy.spatial.transform import Rotation as R
class Surface():
def __init__(self):
self.a = None
self.b = None
self.c = None
def fit_surface(self, points):
mean = np.mean(points, axis=0)
points = points - mean
u, s, v = np.linalg.svd(points)
a, b, c = v[-1]
d = -np.sum(v[-1]*mean)
# ax + by + cz + d=0
self.a = a
self.b = b
self.c = c
self.d = d
if abs(self.d) < 1e-4:
print("small d!!!")
else:
self.a /= self.d
self.b /= self.d
self.c /= self.d
self.d = 1
return
def reflect_one_point(self, point):
x, y, z = point
t = (self.a * x + self.b * y + self.c * z + self.d) / (self.a**2 + self.b**2 + self.c**2)
x -= self.a * t
y -= self.b * t
z -= self.c * t
new_point = np.array([x, y, z])
dist = t * np.sqrt(self.a**2 + self.b**2 + self.c**2)
return new_point, t
def reflect(self, points):
nlist = []
tlist = []
for point in points:
new_point, t = self.reflect_one_point(point)
nlist.append(new_point)
tlist.append(t)
return np.array(nlist), np.array(tlist)
# return points
def fit_circle(self, points): # indep func
# cite https://blog.csdn.net/jiangjjp2812/article/details/106937333/
if self.a is None:
self.fit_surface(points)
M, _ = self.reflect(points)
num, dim = points.shape
L1 = np.ones((num, 1))
A = np.linalg.inv(M.T.dot(M)).dot(M.T).dot(L1).reshape(3, -1)
Mshape = M.shape[0]
tril_idx = np.triu(np.ones((Mshape, Mshape)) - np.eye(Mshape)).flatten().nonzero()
M_minus = M.reshape(Mshape, 1, 3) - M.reshape(1, Mshape, 3)
B = -M_minus.reshape(-1, 3)[tril_idx]
M_sq = (M**2).sum(-1)
Msq_minus = (M_sq.reshape(Mshape, 1) - M_sq.reshape(1, Mshape)) / 2
L2 = -Msq_minus.flatten()[tril_idx]
L2 = L2.reshape(L2.shape[0], 1)
D = np.zeros((4, 4))
D[:3, :3] = B.T.dot(B)
D[3:4, :3] = A.T
D[:3, 3:4] = A
temp = B.T.dot(L2)
L3 = np.zeros((temp.shape[0]+1, 1))
L3[:-1, 0:1] = temp
C = np.linalg.inv(D).dot(L3)
C = C[0:3]
C = self.reflect_one_point(C)[0]
r = 0
for x in M:
r += np.linalg.norm(x-C.T[0])
r /= num
return [C.T[0], r]
class Eye_reconstructor():
def __init__(self):
self.eyeidx = self.get_orbit_idx()
self.one_eye_points = om.read_polymesh("./rabit_data/eyes/one_eye.obj").points() # copy when use
eyemesh = om.read_polymesh("./rabit_data/eyes/template_eyes.obj")
self.eyeface = eyemesh.face_vertex_indices()
def get_orbit_idx(self):
mesh = om.read_polymesh("./rabit_data/eyes/orbit_annotation.ply", vertex_color=True)
colors = mesh.vertex_colors()
eye_idx = []
for i, color in enumerate(colors):
if color[0] < 0.3 and color[1] < 0.3:
eye_idx.append(i)
return eye_idx
def get_ball_info(self, points):
eye_max = points.max(axis=0)
eye_min = points.min(axis=0)
eye_c = (eye_max + eye_min) / 2
eye_r = abs(eye_max[0] - eye_min[0]) / 2
return eye_c, eye_r
def generate_one_eye(self, mesh_points, rC, rR):
orbit = mesh_points[self.eyeidx]
surface = Surface()
circle = surface.fit_circle(orbit)
vec = np.array([surface.a, surface.b, surface.c])
if surface.c < 0:
vec = -vec
vec = vec/np.linalg.norm(vec) # norm vectore
rotation = R.align_vectors(np.reshape(vec, (1, -1)), np.array([[0, 0, 1]]))
rotation = rotation[0].as_matrix()
bC = circle[0] - vec * rC * circle[1]
bR = circle[1] * rR
template_points = self.one_eye_points.copy()
template_points = template_points.dot(rotation.T)
#faces = template_eye.face_vertex_indices()
tc, tr = self.get_ball_info(template_points)
template_points = template_points / tr * bR
tc, tr = self.get_ball_info(template_points) # now tr==bR
template_points = template_points - tc + bC
return template_points
def reconstruct(self, meshpoints, rC=None, rR=None):
meshpoints = meshpoints.detach().cpu().numpy()
eye1 = self.generate_one_eye(meshpoints, rC, rR)
eye2 = eye1.copy() # utilze symmetric
eye2[:, 0] = -eye2[:, 0]
eyes = np.concatenate([eye1, eye2], axis=0)
# reconstruct
new_mesh = om.PolyMesh(points=eyes, face_vertex_indices=self.eyeface)
return new_mesh
if __name__ == '__main__':
pass