-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsvt_geometry.py
186 lines (163 loc) · 6.15 KB
/
nsvt_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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# TODO:
# A. Placeholder
# Library Imports
import math
import numpy
from numba import double, jit
from numba.extending import overload
# Local Imports
import nsvt_config as config
import numba_vectorized as nv
# REQUIRES:
# - vector is a 1-dimensional array of scalar values
@jit(nopython=True)
def calcMagnitude(vector):
vectorSum = 0
for val in vector:
vectorSum += val * val
return vectorSum
@jit(nopython=True)
def getUnitVector3D(vectorX, vectorY, vectorZ):
magnitude = calcMagnitude([vectorX, vectorY, vectorZ])
return (vectorX / magnitude, vectorY / magnitude, vectorZ / magnitude)
# REQUIRES:
# - theta is an angle in radians
@jit(nopython=True)
def getRotationMatrixX(theta):
if theta == 0:
return numpy.asarray([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]
], dtype=config.GEOMETRY_ROTATION_NUMPY_DTYPE)
else:
return numpy.asarray([
[1., 0., 0.],
[0., nv.cos(theta), (-1 * nv.sin(theta))],
[0., nv.sin(theta), nv.cos(theta)]
], dtype=config.GEOMETRY_ROTATION_NUMPY_DTYPE)
# REQUIRES:
# - theta is an angle in radians
@jit(nopython=True)
def getRotationMatrixY(theta):
if theta == 0:
return numpy.asarray([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]
], dtype=config.GEOMETRY_ROTATION_NUMPY_DTYPE)
else:
return numpy.array([
[nv.cos(theta), 0., nv.sin(theta)],
[0., 1., 0.],
[(-1 * nv.sin(theta)), 0., nv.cos(theta)]
], dtype=config.GEOMETRY_ROTATION_NUMPY_DTYPE)
# REQUIRES:
# - theta is an angle in radians
@jit(nopython=True)
def getRotationMatrixZ(theta):
if theta == 0:
return numpy.asarray([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]
], dtype=config.GEOMETRY_ROTATION_NUMPY_DTYPE)
else:
return numpy.array([
[nv.cos(theta), (-1. * nv.sin(theta)), 0.],
[nv.sin(theta), nv.cos(theta), 0.],
[0., 0., 1.]
], dtype=config.GEOMETRY_ROTATION_NUMPY_DTYPE)
# REQUIRES:
# - (uX, uY, uZ) forms a unit vector of length 1
# - theta is an angle in radians
@jit(nopython=True)
def getRotationMatrixAxisAngle(uX, uY, uZ, theta):
if theta == 0:
return numpy.asarray([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]
], dtype=config.GEOMETRY_ROTATION_NUMPY_DTYPE)
else:
return numpy.array([
[(nv.cos(theta) + (uX * uX * (1. - nv.cos(theta)))), ((uX * uY * (1. - nv.cos(theta))) - (uZ * nv.sin(theta))), ((uX * uZ * (1. - nv.cos(theta))) + (uY * nv.sin(theta)))],
[((uX * uY * (1. - nv.cos(theta))) + (uZ * nv.sin(theta))), (nv.cos(theta) + (uY * uY * (1. - nv.cos(theta)))), ((uY * uZ * (1. - nv.cos(theta))) - (uX * nv.sin(theta)))],
[((uX * uZ * (1. - nv.cos(theta))) - (uY * nv.sin(theta))), ((uY * uZ * (1. - nv.cos(theta))) + (uX * nv.sin(theta))), (nv.cos(theta) + (uZ * uZ * (1. - nv.cos(theta))))]
], dtype=config.GEOMETRY_ROTATION_NUMPY_DTYPE)
# MODIFIES:
# - triangleVertices
@jit(nopython=True)
def updateTriangleVertices(vertices, triangles, triangleVertices):
# Invariant Checks (NOT FULLY IMPLEMENTED)
# - Must have already built the base self.triangleVertices array
invariantFail = (triangleVertices.size == 0)
if invariantFail:
print("ERROR: Failed invariant checks in nsvt_geometry.updateTriangleVertices()")
return False
for t, triangle in enumerate(triangles):
for v, vertex in enumerate(triangle):
for i in range(3):
triangleVertices[t, 3 * v + i] = vertices[int(vertex)][i]
# MODIFIES:
# - vertices
@jit(nopython=True)
def translateVertices(vertices, moveX, moveY, moveZ):
# Invariant Checks (NOT YET IMPLEMENTED)
invariantFail = False
if invariantFail:
print("ERROR: Failed invariant checks in nsvt_geometry.translateVertices()")
return False
for vertex in vertices:
vertex[0] += moveX
vertex[1] += moveY
vertex[2] += moveZ
# Modify the X/Y/Z coordinates of each vertex
#vertices[:, 0] += moveX
#vertices[:, 1] += moveY
#vertices[:, 2] += moveZ
# REQUIRES:
# - rotateX/rotateY/rotateZ are angles in degrees
# MODIFIES:
# - vertices
# TODO: Make this function Numba-compatible
#@jit(nopython=True)
def rotateVertices(vertices, degreesX, degreesY, degreesZ):
# Invariant Checks (NOT YET IMPLEMENTED)
invariantFail = False
if invariantFail:
print("ERROR: Failed invariant checks in nsvt_geometry.rotateVertices()")
return False
verticesTransposed = numpy.transpose(vertices)
rMatX = getRotationMatrixX(math.radians(degreesX))
rMatY = getRotationMatrixY(math.radians(degreesY))
rMatZ = getRotationMatrixZ(math.radians(degreesZ))
#rMat = numpy.matmul(rMatX, rMatY)
#rMat = numpy.matmul(rMat, rMatZ)
#verticesTransposed = numpy.matmul(rMat, verticesTransposed)
rMat = rMatX @ rMatY
rMat = rMat @ rMatZ
verticesTransposed = rMat @ verticesTransposed
verticesTransposed = numpy.transpose(verticesTransposed)
vertices[:] = verticesTransposed[:]
return vertices
# REQUIRES:
# - rotateX/rotateY/rotateZ are angles in degrees
# MODIFIES:
# - vertices
# TODO: Make this function Numba-compatible
#@jit(nopython=True)
def rotateVerticesAxisAngle(vertices, axisX, axisY, axisZ, degrees):
# Invariant Checks (NOT YET IMPLEMENTED)
invariantFail = False
if invariantFail:
print("ERROR: Failed invariant checks in nsvt_geometry.rotateVerticesAxisAngle()")
return False
verticesTransposed = numpy.transpose(vertices)
unitVec = getUnitVector3D(axisX, axisY, axisZ)
rMat = getRotationMatrixAxisAngle(unitVec[0], unitVec[1], unitVec[2], math.radians(degrees))
#verticesTransposed = numpy.matmul(rMat, verticesTransposed)
verticesTransposed = rMat @ verticesTransposed
verticesTransposed = numpy.transpose(verticesTransposed)
vertices[:] = verticesTransposed[:]
return vertices