-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtransform3.py
More file actions
110 lines (92 loc) · 2.68 KB
/
Copy pathtransform3.py
File metadata and controls
110 lines (92 loc) · 2.68 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
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
import taichi as ti
@ti.func
def compute_frame_to_canonical_matrix(
axis0: ti.math.vec3, axis1: ti.math.vec3, axis2: ti.math.vec3, origin: ti.math.vec3
) -> ti.math.mat3:
"""
Compute the frame to canonical matrix that transforms points and vectors
expressed in the frame space to the same points expressed in the canonical
frame.
Parameters
----------
axis0, axis1, origin : ti.math.vec2
The axis 0 and 1, and the origin, of the frame expressed in the canonical matrix.
Notes
-----
Marschner and Shirley, Fundamentals of Computer Graphics, 5th edition, p. 153
"""
return ti.math.mat4(
[
[axis0.x, axis1.x, axis2.x, origin.x],
[axis0.y, axis1.y, axis2.y, origin.y],
[axis0.z, axis1.z, axis2.z, origin.z],
[0.0, 0.0, 0.0, 1.0],
]
)
@ti.func
def apply_to_point(T: ti.math.mat4, p: ti.math.vec3) -> ti.math.vec3:
p_homogeneous = ti.math.vec4(p, 1.0)
p_transformed = T @ p_homogeneous
p_transformed /= p_transformed[3]
return p_transformed[:3]
@ti.func
def apply_to_vector(T: ti.math.mat4, v: ti.math.vec3) -> ti.math.vec3:
v_homogeneous = ti.math.vec4(v, 0.0)
v_transformed = T @ v_homogeneous
return v_transformed.xyz
@ti.func
def scale_uniformly(s: float) -> ti.math.mat4:
return ti.math.mat4(
[
[s, 0.0, 0.0, 0.0],
[0.0, s, 0.0, 0.0],
[0.0, 0.0, s, 0.0],
[0.0, 0.0, 0.0, 1.0],
]
)
@ti.func
def translate(t: ti.math.vec3) -> ti.math.mat4:
return ti.math.mat4(
[
[1.0, 0.0, 0.0, t.x],
[0.0, 1.0, 0.0, t.y],
[0.0, 0.0, 1.0, t.z],
[0.0, 0.0, 0.0, 1.0],
]
)
@ti.func
def rotate_x(theta: float) -> ti.math.mat4:
cos_theta = ti.cos(theta)
sin_theta = ti.sin(theta)
return ti.math.mat4(
[
[1.0, 0.0, 0.0, 0.0],
[0.0, cos_theta, -sin_theta, 0.0],
[0.0, sin_theta, cos_theta, 0.0],
[0.0, 0.0, 0.0, 1.0],
]
)
@ti.func
def rotate_y(theta: float) -> ti.math.mat4:
cos_theta = ti.cos(theta)
sin_theta = ti.sin(theta)
return ti.math.mat4(
[
[cos_theta, 0.0, sin_theta, 0.0],
[0.0, 1.0, 0.0, 0.0],
[-sin_theta, 0.0, cos_theta, 0.0],
[0.0, 0.0, 0.0, 1.0],
]
)
@ti.func
def rotate_z(theta: float) -> ti.math.mat4:
cos_theta = ti.cos(theta)
sin_theta = ti.sin(theta)
return ti.math.mat4(
[
[cos_theta, -sin_theta, 0.0, 0.0],
[sin_theta, cos_theta, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
]
)