-
Notifications
You must be signed in to change notification settings - Fork 0
/
haptics.py
127 lines (109 loc) · 4.45 KB
/
haptics.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
import pyhaptics3 as ph
import taichi as ti
import numpy as np
@ti.data_oriented
class haptices:
def __init__(self, verts, direct, init_rota):
self.mat_row = np.zeros([4, 4])
self.verts = verts
self.mat = ti.Matrix.field(4, 4, dtype=ti.f32, shape=1)
self.rota_mat = ti.Matrix.field(3, 3, dtype=ti.f32, shape=1)
self.rota(direct, init_rota)
@ti.kernel
def rota(self, direction: ti.i32, alpha: ti.f32):
if direction == 0:
self.rota_mat[0] = ([[1, 0, 0],
[0, ti.cos(alpha), ti.sin(alpha)],
[0, -ti.sin(alpha), ti.cos(alpha)]
])
elif direction == 1:
self.rota_mat[0] = ([[ti.cos(alpha), 0, -ti.sin(alpha)],
[0, 1, 0],
[ti.sin(alpha), 0, ti.cos(alpha)],
])
else:
self.rota_mat[0] = ([[ti.cos(alpha), ti.sin(alpha), 0],
[-ti.sin(alpha), ti.cos(alpha), 0],
[0, 0, 1]
])
for vert in self.verts.ox:
T = self.verts.ox[vert] @ self.rota_mat[0]
self.verts.ox[vert] = T
def get_mat(self):
self.mat_row = np.array(ph.get_transform()).reshape(4, -1)
@ti.kernel
def model_transpose(self, mat: ti.types.ndarray(dtype=ti.f32, ndim=2)):
for i in range(4):
for j in range(4):
if i == 3 and j != 3:
self.mat[0][i, j] = mat[i, j] * 0.02
else:
self.mat[0][i, j] = mat[i, j]
for vert in self.verts.ox:
X = ti.Vector([self.verts.ox[vert].x, self.verts.ox[vert].y, self.verts.ox[vert].z, 1])
T = X @ self.mat[0]
# print(T)
self.verts.x[vert].x = T[0]
self.verts.x[vert].y = T[1] + 0.5
self.verts.x[vert].z = T[2]
self.verts.rx[vert] = self.verts.x[vert]
@staticmethod
def set_force(x: ti.f32, y: ti.f32, z: ti.f32):
ph.set_force(x, y, z)
def run(self, x, y, z):
self.get_mat()
self.model_transpose(self.mat_row)
self.set_force(x, y, z)
@ti.data_oriented
class haptices1:
def __init__(self, verts, direct, init_rota):
self.mat_row = np.zeros([4, 4])
self.verts = verts
self.mat = ti.Matrix.field(4, 4, dtype=ti.f32, shape=1)
self.rota_mat = ti.Matrix.field(3, 3, dtype=ti.f32, shape=1)
self.rota(direct, init_rota)
@ti.kernel
def rota(self, direction: ti.i32, alpha: ti.f32):
if direction == 0:
self.rota_mat[0] = ([[1, 0, 0],
[0, ti.cos(alpha), ti.sin(alpha)],
[0, -ti.sin(alpha), ti.cos(alpha)]
])
elif direction == 1:
self.rota_mat[0] = ([[ti.cos(alpha), 0, -ti.sin(alpha)],
[0, 1, 0],
[ti.sin(alpha), 0, ti.cos(alpha)],
])
else:
self.rota_mat[0] = ([[ti.cos(alpha), ti.sin(alpha), 0],
[-ti.sin(alpha), ti.cos(alpha), 0],
[0, 0, 1]
])
for vert in self.verts.ox:
T = self.verts.ox[vert] @ self.rota_mat[0]
self.verts.ox[vert] = T
def get_mat(self):
self.mat_row = np.array(ph.get_transform1()).reshape(4, -1)
@ti.kernel
def model_transpose(self, mat: ti.types.ndarray(dtype=ti.f32, ndim=2)):
for i in range(4):
for j in range(4):
if i == 3 and j != 3:
self.mat[0][i, j] = mat[i, j] * 0.02
else:
self.mat[0][i, j] = mat[i, j]
for vert in self.verts.ox:
X = ti.Vector([self.verts.ox[vert].x, self.verts.ox[vert].y, self.verts.ox[vert].z, 1])
T = X @ self.mat[0]
# print(T)
self.verts.x[vert].x = T[0]
self.verts.x[vert].y = T[1] + 0.5
self.verts.x[vert].z = T[2]
self.verts.rx[vert] = self.verts.x[vert]
@staticmethod
def set_force(x: ti.f32, y: ti.f32, z: ti.f32):
ph.set_force1(x, y, z)
def run(self, x, y, z):
self.get_mat()
self.model_transpose(self.mat_row)
self.set_force(x, y, z)