-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpositionFuncs.py
More file actions
executable file
·189 lines (133 loc) · 5.42 KB
/
positionFuncs.py
File metadata and controls
executable file
·189 lines (133 loc) · 5.42 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
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
187
188
189
from copy import deepcopy
from math import cos, sin
import math as m
import numpy as np
def doRotationMatrixes(inPts, rotations, transposed = False):
A = rotations[0]
B = rotations[1]
C = rotations[2]
rotationMags = np.array([
[ cos(B)*cos(C), sin(A)*sin(B)*cos(C)-cos(A)*sin(C), cos(A)*sin(B)*cos(C)+sin(A)*sin(C) ],
[ cos(B)*sin(C), sin(A)*sin(B)*sin(C) +cos(A)*cos(C), cos(A)*sin(B)*sin(C) -sin(A)*cos(C) ],
[ -sin(B), sin(A)*cos(B), cos(A)*cos(B) ],
])
if transposed: rotationMags = np.matrix.transpose(rotationMags)
# if type(pos[0]) in (np.ndarray, list, set):
xPts = sum([inPts[ii]*rotationMags[0][ii] for ii in range(3)])
yPts = sum([inPts[ii]*rotationMags[1][ii] for ii in range(3)])
zPts = sum([inPts[ii]*rotationMags[2][ii] for ii in range(3)])
return([xPts, yPts, zPts])
# else:
# xPts = [inPts[ii]*rotationMags[0][ii] for ii in range(3)]
# yPts = [inPts[ii]*rotationMags[1][ii] for ii in range(3)]
# zPts = [inPts[ii]*rotationMags[2][ii] for ii in range(3)]
# return([xPts, yPts, zPts])
# for foo in rotationMags: print(foo)
def motionToZAng(inMotion):
return np.arctan2(-sin(inMotion[4]), cos(inMotion[4])*cos(inMotion[5]))
def completeMotion(inPts, motion):
offSets = motion[:3]
rotations = motion[3:6]
ptSet = doRotationMatrixes(inPts, rotations)
for ii in range(3):
ptSet[ii] += offSets[ii]
return(ptSet)
def undoMotion(inPts, motion):
offSets = motion[:3]
rotations = motion[3:6]
for ii in range(3):
inPts[ii] -= offSets[ii]
ptSet = doRotationMatrixes(inPts, rotations, transposed=True)
return(ptSet)
# def applyMotion(pos, motion):
# mat = transformationMatrix(motion)
# return(
# pos[ii]*rotationMags[0][ii] for ii in range(3),
# pos[ii]*rotationMags[1][ii] for ii in range(3),
# pos[ii]*rotationMags[2][ii] for ii in range(3),
# )
def transformationMatrix(motion):
A = motion[3]
B = motion[4]
C = motion[5]
return(np.array([
[ cos(B)*cos(C), sin(A)*sin(B)*cos(C)-cos(A)*sin(C), cos(A)*sin(B)*cos(C)+sin(A)*sin(C), motion[0] ],
[ cos(B)*sin(C), sin(A)*sin(B)*sin(C) +cos(A)*cos(C), cos(A)*sin(B)*sin(C) -sin(A)*cos(C), motion[1] ],
[ -sin(B), sin(A)*cos(B), cos(A)*cos(B), motion[2] ],
[0, 0, 0, 1]
]))
def addMotions(motion1, motion2, transpose1 = False):
if not transpose1: outMatrix = np.matmul( transformationMatrix(motion1), transformationMatrix(motion2) )
else: outMatrix = np.matmul( np.linalg.inv(transformationMatrix(motion1)), transformationMatrix(motion2) )
return(outMatrix)
def getMotionBetween(motion1, motion2):
outMat = np.matmul( np.linalg.inv(transformationMatrix(motion2)), transformationMatrix(motion1))
outMotion = [
outMat[0][3],
outMat[1][3],
outMat[2][3],
np.arctan2(outMat[2][1], outMat[2][2]),
np.arctan2(-outMat[2][0], m.sqrt(pow(outMat[3][1], 2) + pow(outMat[2][2], 2))),
np.arctan2(outMat[1][0], outMat[0][0]),
]
return(outMotion)
def normalizeMotion(inMotion):
outMat = transformationMatrix(inMotion)
outMotion = [
outMat[0][3],
outMat[1][3],
outMat[2][3],
np.arctan2(outMat[2][1], outMat[2][2]),
np.arctan2(-outMat[2][0], m.sqrt(pow(outMat[3][1], 2) + pow(outMat[2][2], 2))),
np.arctan2(outMat[1][0], outMat[0][0]),
]
return(outMotion)
def magnitude(inVals):
return(m.sqrt(sum(pow(inVals,2))))
def getClosestPts(inVectors, inPts):
inPts = np.column_stack(inPts)
inVectors = np.column_stack(inVectors)
tSet = np.sum(inPts*inVectors, axis=1) / np.sum(inVectors*inVectors, axis=1)
# print(tSet)
# print(tSet[:, None])
outPts = inVectors * tSet[:, None] # Converts NP array to 2d with only one element in row
# print(outPts)
return(outPts)
def crossFixed(a:np.ndarray,b:np.ndarray)->np.ndarray: # Fix code unreachable error in some IDES
return np.cross(a,b)
def ptVectDistSquared(pt, line):
dVect = line - pt # Get vector from arbitrary point on line to target
distance = np.sum(np.square(crossFixed(line, dVect)), axis=1)/np.sum(np.square(line), axis=1)
return(distance)
def getError(inVectors, inPts, ptSize):
inPts = np.column_stack(inPts)
inVectors = np.column_stack(inVectors)
distances = ptVectDistSquared(inVectors, inPts)
# distances *= ptSize
return( np.sum(distances) )
def testError(inVectors, inPts, motion, ptSize):
adjPts = completeMotion(inPts, motion)
sumError = getError(inVectors, adjPts, ptSize)
return(sumError)
def set_axes_equal(ax):
'''Make axes of 3D plot have equal scale so that spheres appear as spheres,
cubes as cubes, etc.. This is one possible solution to Matplotlib's
ax.set_aspect('equal') and ax.axis('equal') not working for 3D.
Input
ax: a matplotlib axis, e.g., as output from plt.gca().
'''
x_limits = ax.get_xlim3d()
y_limits = ax.get_ylim3d()
z_limits = ax.get_zlim3d()
x_range = abs(x_limits[1] - x_limits[0])
x_middle = np.mean(x_limits)
y_range = abs(y_limits[1] - y_limits[0])
y_middle = np.mean(y_limits)
z_range = abs(z_limits[1] - z_limits[0])
z_middle = np.mean(z_limits)
# The plot bounding box is a sphere in the sense of the infinity
# norm, hence I call half the max range the plot radius.
plot_radius = 0.5*max([x_range, y_range, z_range])
ax.set_xlim3d([x_middle - plot_radius, x_middle + plot_radius])
ax.set_ylim3d([y_middle - plot_radius, y_middle + plot_radius])
ax.set_zlim3d([z_middle - plot_radius, z_middle + plot_radius])