forked from polmorenoc/opendr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.py
98 lines (69 loc) · 3.09 KB
/
geometry.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
from chumpy import Ch
import numpy as np
class RotateZ(Ch):
dterms = 'a'
def compute_r(self):
return np.array([[np.cos(self.a.r), -np.sin(self.a.r), 0, 0], [np.sin(self.a.r), np.cos(self.a.r), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
def compute_dr_wrt(self, wrt):
if wrt is not self.a:
return
if wrt is self.a:
return np.array([[-np.sin(self.a.r)[0], -np.cos(self.a.r)[0], 0, 0], [np.cos(self.a.r)[0], -np.sin(self.a.r)[0], 0, 0], [0, 0, 0, 0],
[0, 0, 0, 0]]).reshape(16, 1)
class RotateX(Ch):
dterms = 'a'
def compute_r(self):
return np.array([[1, 0, 0, 0], [0, np.cos(self.a.r), -np.sin(self.a.r), 0], [0, np.sin(self.a.r), np.cos(self.a.r), 0], [0, 0, 0, 1]])
def compute_dr_wrt(self, wrt):
# if wrt is not self.a:
# return
if wrt is self.a:
return np.array([[0, 0, 0, 0], [0, -np.sin(self.a.r)[0], -np.cos(self.a.r)[0], 0], [0, np.cos(self.a.r)[0], -np.sin(self.a.r)[0], 0],
[0, 0, 0, 0]]).reshape(16, 1)
class Scale(Ch):
dterms = 'x', 'y', 'z'
def compute_r(self):
return np.array([[self.x.r, 0, 0, 0], [0, self.y.r, 0, 0], [0, 0, self.z.r, 0], [0, 0, 0, 1]])
def compute_dr_wrt(self, wrt):
return
def compute_dr_wrt(self, wrt):
if wrt is not self.x and wrt is not self.y and wrt is not self.z:
return
if wrt is self.x:
return np.array([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]).reshape(16, 1)
if wrt is self.y:
return np.array([[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]).reshape(16, 1)
if wrt is self.z:
return np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]).reshape(16, 1)
class Translate(Ch):
dterms = 'x', 'y', 'z'
def compute_r(self):
return np.array([[1, 0, 0, self.x.r], [0, 1, 0, self.y.r], [0, 0, 1, self.z.r], [0, 0, 0, 1]])
def compute_dr_wrt(self, wrt):
if wrt is not self.x and wrt is not self.y and wrt is not self.z:
return
if wrt is self.x:
return np.array([[0, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]).reshape(16, 1)
if wrt is self.y:
return np.array([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]]).reshape(16, 1)
if wrt is self.z:
return np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]).reshape(16, 1)
def normalize_v3(arr):
lens = np.sqrt(arr[:, 0] ** 2 + arr[:, 1] ** 2 + arr[:, 2] ** 2)
arr[:, 0] /= lens
arr[:, 1] /= lens
arr[:, 2] /= lens
return arr
def getNormals(vertices, faces):
norm = np.zeros(vertices.shape, dtype=vertices.dtype)
tris = vertices[faces]
n = np.cross(tris[::, 1] - tris[::, 0], tris[::, 2] - tris[::, 0])
normalize_v3(n)
norm[faces[:, 0]] += n
norm[faces[:, 1]] += n
norm[faces[:, 2]] += n
normalize_v3(norm)
return norm
def chGetNormals(vertices, faces):
import opendr.geometry
return opendr.geometry.VertNormals(vertices, faces).reshape((-1, 3))