forked from alextac98/AIM-ViconDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
182 lines (146 loc) · 6.25 KB
/
example.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
#!/usr/bin/env python
# //==============================================================================
# /*
# Software License Agreement (BSD License)
# Copyright (c) 2020, AIMVicon
# (www.aimlab.wpi.edu)
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of authors nor the names of its contributors may
# be used to endorse or promote products derived from this software
# without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# \author <http://www.aimlab.wpi.edu>
# \author <[email protected]>
# \author Nathaniel Goldfarb
# \version 0.1
# */
# //==============================================================================
import numpy as np
import GaitCore.Core as core
from Vicon.Mocap import Vicon
from Vicon import Markers
import matplotlib.pyplot as plt
import os
import matplotlib.animation as animation
cloud = [core.Point.Point(0.0, 0.0, 0.0),
core.Point.Point(70.0, 0.0, 0.0),
core.Point.Point(0.0, 49.0, 0.0),
core.Point.Point(70.0, 63.0, 0.0)]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_autoscale_on(False)
def animate(frame, x, y, z, centers=None, axis =None):
"""
:param frame:
:param x:
:param y:
:param z:
:param centers:
:return:
"""
ax.clear()
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.axis([-500, 500, -200, 3000])
ax.set_zlim3d(0, 1250)
ax.scatter(x[frame], y[frame], z[frame], c='r', marker='o')
ax.scatter(centers[frame][0], centers[frame][1], centers[frame][2], c='g', marker='o')
axis_x = [(centers[frame][0] - axis[0] * 1000).item(0), (centers[frame][0]).item(0), (centers[frame][0] + axis[0] * 1000).item(0)]
axis_y = [(centers[frame][1] - axis[1] * 1000).item(0), (centers[frame][1]).item(0), (centers[frame][1] + axis[1] * 1000).item(0)]
axis_z = [(centers[frame][2] - axis[2] * 1000).item(0), (centers[frame][2]).item(0), (centers[frame][2] + axis[2] * 1000).item(0)]
ax.plot(axis_x, axis_y, axis_z, 'b')
def get_right_knee(file, start, end):
vicon = Vicon.Vicon(file)
markers = vicon.get_markers()
markers.smart_sort()
shank = markers.get_rigid_body("R_Tibia")
thigh = markers.get_rigid_body("R_Femur")
transforms = []
error = []
m1 = shank[0][start:end]
m2 = shank[1][start:end]
m3 = shank[2][start:end]
m4 = shank[3][start:end]
data = [m1, m2, m3, m4]
core = Markers.calc_CoR(data)
axis = Markers.calc_AoR(data)
core = [[core[0][0]], [core[1][0]], [core[2][0]],[1.0]]
core = np.array(core)
vect = np.array([[0.0], [0.0], [0.0], [0.0]])
max_error = 10000000000
for frame in range(start, end):
f = [shank[0][frame], shank[1][frame], shank[2][frame], shank[3][frame]]
T, err = Markers.cloud_to_cloud(cloud, f)
if err < max_error:
max_error = err
vect = np.dot(np.linalg.pinv(T), core)
error.append(err)
transforms.append(T)
#vect = vect/(end - start)
centers = []
for frame in range(len(shank[0])):
f = [shank[0][frame], shank[1][frame], shank[2][frame], shank[3][frame]]
T, err = Markers.cloud_to_cloud(cloud, f)
point = np.dot(T, vect)[0:3]
_thigh = Markers.calc_mass_vect([thigh[0][frame],
thigh[1][frame],
thigh[2][frame],
thigh[3][frame]])
_shank = Markers.calc_mass_vect([shank[0][frame],
shank[1][frame],
shank[2][frame],
shank[3][frame]])
sol = Markers.minimize_center([_thigh, _shank], axis=axis, initial=(point[0][0], point[1][0], point[2][0]))
#centers.append( sol.x )
centers.append(point)
keys = markers._filtered_markers.keys()
nfr = len(markers._filtered_markers[keys[0]]) # Number of frames
x_total = []
y_total = []
z_total= []
for frame in xrange(nfr):
x = []
y = []
z = []
for key in keys:
point = markers._filtered_markers[key][frame]
x += [point.x]
y += [point.y]
z += [point.z]
x_total.append(x)
y_total.append(y)
z_total.append(z)
fps = 100 # Frame per sec
keys = markers._filtered_markers.keys()
nfr = len(markers._filtered_markers[keys[0]]) # Number of frames
ani = animation.FuncAnimation(fig,
animate, nfr,
fargs=(x_total, y_total, z_total, centers, axis),
interval=100 / fps)
plt.show()
return centers
if __name__ == "__main__":
script_dir = os.path.dirname(__file__) # <-- absolute dir the script is in
file = "TestData.csv"
right_knee = get_right_knee(os.path.join(script_dir,file), 875, 930) # 950