-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_transformation_matrix.py
156 lines (111 loc) · 4.05 KB
/
compute_transformation_matrix.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
148
149
150
151
152
153
154
155
156
from itertools import product
from math import cos, isinf, isnan, pi, sin, sqrt
def normalize_vector(vec):
magnitude = sqrt(sum(x**2 for x in vec))
return [x / magnitude for x in vec] if magnitude > 0.0 else [1.0, 0.0, 0.0]
def sanitize_value(value):
return value if not isnan(value) or not isinf(value) else 0.0
def calculate_matrix_product(mult_result, matrix1, matrix2):
# Get the dimensions of the input matrices
rows_matrix1 = len(matrix1)
cols_matrix1 = len(matrix1[0])
rows_matrix2 = len(matrix2)
cols_matrix2 = len(matrix2[0])
if cols_matrix1 != rows_matrix2:
raise ValueError("Matrix1 columns must equal Matrix2 rows for multiplication")
# Perform matrix multiplication
for row, col in product(range(rows_matrix1), range(cols_matrix2)):
value = sum(matrix1[row][k] * matrix2[k][col] for k in range(cols_matrix1))
mult_result[row][col] = sanitize_value(value)
# Convert the product matrix to a tuple of tuples
mult_result = tuple(tuple(row) for row in mult_result)
return mult_result
def rotate_and_transform_matrix(product, input_matrix, angle):
angle = min(angle, 65535)
angle_radians = angle * pi / 180.0
sine = sin(angle_radians)
cosine = cos(angle_radians)
rotation_matrix = [
[cosine, -sine, 0.0, 0.0],
[sine, cosine, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
]
product = calculate_matrix_product(product, input_matrix, rotation_matrix)
return product
def sanitize_vector(vector):
return [sanitize_value(x) for x in vector]
def cross_product(a, b):
return [
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
]
def get_direction(position_start, position_end):
direction = [position_end[i] - position_start[i] for i in range(3)]
return normalize_vector(direction)
def get_custom_params():
return [1, 2, 3]
def check_custom_params_availability(params):
return True
def get_custom_data(params):
return sum(params)
def get_direction_vector(data_ptr):
direction_vectors = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
return direction_vectors[data_ptr % 2]
def get_custom_data_from_context(context):
return context % 128
def compute_transformation_matrix(
context,
position_start,
position_end,
rotation_angle,
use_default_rotation,
use_custom_direction,
):
default_up = [0.0, 0.0, 1.0]
direction = get_direction(position_start, position_end)
if use_default_rotation:
params = get_custom_params()
params_available = check_custom_params_availability(params)
if params_available:
custom_data_ptr = get_custom_data(params)
direction_vector = get_direction_vector(custom_data_ptr)
default_up = sanitize_vector(direction_vector)
elif use_custom_direction:
custom_data_ptr = get_custom_data_from_context(context)
direction_vector = get_direction_vector(custom_data_ptr)
default_up = sanitize_vector(direction_vector)
right_vector = cross_product(default_up, direction)
right_vector = normalize_vector(right_vector)
up_vector = cross_product(direction, right_vector)
up_vector = normalize_vector(up_vector)
result_matrix = [
right_vector + [0.0],
up_vector + [0.0],
direction + [0.0],
position_start + [1.0],
]
result_matrix = rotate_and_transform_matrix(
result_matrix, result_matrix, rotation_angle
)
return result_matrix
def main():
position_start = [0.0, 0.0, 0.0]
position_end = [1.0, 1.0, 1.0]
rotation_angle = 256
use_default_rotation = True
use_custom_direction = False
result_matrix = compute_transformation_matrix(
12345,
position_start,
position_end,
rotation_angle,
use_default_rotation,
use_custom_direction,
)
# Print the resulting matrix
for row in result_matrix:
print(" ".join(f"{value:.6f}" for value in row))
if __name__ == "__main__":
main()