-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate_model_from_c3d.py
More file actions
291 lines (262 loc) · 12.4 KB
/
create_model_from_c3d.py
File metadata and controls
291 lines (262 loc) · 12.4 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
This example shows how to create a personalized kinematic model from a C3D file containing a static trial.
Here, we generate a simple lower-body model with only a trunk segment.
The marker position and names are taken from Maldonado & al., 2018 (https://hal.science/hal-01841355/).
TODO: Collect functional trials to define joint centers more accurately, and then move the example to applied_examples.
"""
import os
import numpy as np
from biobuddy import (
Axis,
BiomechanicalModel,
C3dData,
Marker,
Mesh,
Segment,
SegmentCoordinateSystem,
Translations,
Rotations,
DeLevaTable,
Sex,
SegmentName,
ViewAs,
SegmentCoordinateSystemUtils,
RotationMatrix,
RotoTransMatrix,
JointCoordinateModifier,
)
def model_creation_from_measured_data(
static_trial: C3dData, align_scs: bool = False, remove_temporary: bool = True, animate_model: bool = True
):
total_mass = 66
total_height = 1.70
output_model_filepath = f"lower_body.bioMod"
de_leva = DeLevaTable(total_mass=total_mass, sex=Sex.FEMALE)
de_leva.from_measurements(
total_height=total_height,
ankle_height=SegmentCoordinateSystemUtils.mean_markers(["RSPH", "RLM", "LLM", "LSPH"])(static_trial, None)[2],
knee_height=SegmentCoordinateSystemUtils.mean_markers(["RLFE", "RMFE", "LLFE", "LMFE"])(static_trial, None)[2],
hip_height=SegmentCoordinateSystemUtils.mean_markers(["LPSIS", "RPSIS", "LASIS", "RASIS"])(static_trial, None)[
2
],
shoulder_height=SegmentCoordinateSystemUtils.mean_markers(["LA", "RA"])(static_trial, None)[2],
finger_span=total_height,
wrist_span=total_height * 0.9, # TODO: find data from literature for these % to set default values
elbow_span=total_height * 0.5,
shoulder_span=total_height * 0.2,
foot_length=total_height * 0.2,
hip_width=0,
)
# Generate the personalized kinematic model
reduced_model = BiomechanicalModel()
reduced_model.add_segment(Segment(name="Ground"))
reduced_model.add_segment(
Segment(
name="Pelvis",
parent_name="Ground",
translations=Translations.XYZ,
rotations=Rotations.XYZ,
inertia_parameters=de_leva[SegmentName.TRUNK],
segment_coordinate_system=SegmentCoordinateSystem(
origin=SegmentCoordinateSystemUtils.mean_markers(["LPSIS", "RPSIS", "LASIS", "RASIS"]),
first_axis=Axis(
name=Axis.Name.X,
start=SegmentCoordinateSystemUtils.mean_markers(["LPSIS", "LASIS"]),
end=SegmentCoordinateSystemUtils.mean_markers(["RPSIS", "RASIS"]),
),
second_axis=Axis(name=Axis.Name.Z),
axis_to_keep=Axis.Name.Z,
),
mesh=Mesh(("LPSIS", "RPSIS", "RASIS", "LASIS", "LPSIS"), is_local=False),
)
)
reduced_model.segments["Pelvis"].add_marker(Marker("LPSIS", is_technical=True, is_anatomical=True))
reduced_model.segments["Pelvis"].add_marker(Marker("RPSIS", is_technical=True, is_anatomical=True))
reduced_model.segments["Pelvis"].add_marker(Marker("LASIS", is_technical=True, is_anatomical=True))
reduced_model.segments["Pelvis"].add_marker(Marker("RASIS", is_technical=True, is_anatomical=True))
reduced_model.segments["Pelvis"].add_marker(Marker("RA", is_technical=True, is_anatomical=True))
reduced_model.segments["Pelvis"].add_marker(Marker("LA", is_technical=True, is_anatomical=True))
reduced_model.add_segment(
Segment(
name="RFemur",
parent_name="Pelvis",
rotations=Rotations.XY,
inertia_parameters=de_leva[SegmentName.THIGH],
segment_coordinate_system=SegmentCoordinateSystem(
origin=lambda m, bio: SegmentCoordinateSystemUtils.mean_markers(["RPSIS", "RASIS"])(static_trial, None)
- np.array([0.0, 0.0, 0.05 * total_height, 0.0]),
first_axis=Axis(name=Axis.Name.X, start="RMFE", end="RLFE"),
second_axis=Axis(
name=Axis.Name.Z,
start=SegmentCoordinateSystemUtils.mean_markers(["RMFE", "RLFE"]),
end=SegmentCoordinateSystemUtils.mean_markers(["RPSIS", "RASIS"]),
),
axis_to_keep=Axis.Name.Z,
),
mesh=Mesh(
(
lambda m, bio: SegmentCoordinateSystemUtils.mean_markers(["RPSIS", "RASIS"])(static_trial, None)
- np.array([0.0, 0.0, 0.05 * total_height, 0.0]),
"RMFE",
"RLFE",
lambda m, bio: SegmentCoordinateSystemUtils.mean_markers(["RPSIS", "RASIS"])(static_trial, None)
- np.array([0.0, 0.0, 0.05 * total_height, 0.0]),
),
is_local=False,
),
)
)
reduced_model.segments["RFemur"].add_marker(Marker("RLFE", is_technical=True, is_anatomical=True))
reduced_model.segments["RFemur"].add_marker(Marker("RMFE", is_technical=True, is_anatomical=True))
reduced_model.add_segment(
Segment(
name="RTibia",
parent_name="RFemur",
rotations=Rotations.X,
inertia_parameters=de_leva[SegmentName.SHANK],
segment_coordinate_system=SegmentCoordinateSystem(
origin=SegmentCoordinateSystemUtils.mean_markers(["RMFE", "RLFE"]),
first_axis=Axis(name=Axis.Name.X, start="RSPH", end="RLM"),
second_axis=Axis(
name=Axis.Name.Z,
start=SegmentCoordinateSystemUtils.mean_markers(["RSPH", "RLM"]),
end=SegmentCoordinateSystemUtils.mean_markers(["RMFE", "RLFE"]),
),
axis_to_keep=Axis.Name.Z,
),
mesh=Mesh(("RMFE", "RSPH", "RLM", "RLFE"), is_local=False),
)
)
reduced_model.segments["RTibia"].add_marker(Marker("RLM", is_technical=True, is_anatomical=True))
reduced_model.segments["RTibia"].add_marker(Marker("RSPH", is_technical=True, is_anatomical=True))
# The foot is a special case since the position of the ankle relatively to the foot length is not given in De Leva
# So here we assume that the foot com is in the middle of the three foot markers
foot_inertia_parameters = de_leva[SegmentName.FOOT]
rt_matrix = RotoTransMatrix.from_euler_angles_and_translation(
angle_sequence="y",
angles=np.array([-np.pi / 2]),
translation=np.array([0.0, 0.0, 0.0]),
)
foot_inertia_parameters.center_of_mass = lambda m, bio: rt_matrix.rt_matrix @ np.nanmean(
m.markers_center_position(["LSPH", "LLM", "LTT2"]) - m.markers_center_position(["LSPH", "LLM"]),
axis=1,
)
reduced_model.add_segment(
Segment(
name="RFoot",
parent_name="RTibia",
rotations=Rotations.X,
segment_coordinate_system=SegmentCoordinateSystem(
origin=SegmentCoordinateSystemUtils.mean_markers(["RSPH", "RLM"]),
first_axis=Axis(
Axis.Name.Z, start=SegmentCoordinateSystemUtils.mean_markers(["RSPH", "RLM"]), end="RTT2"
),
second_axis=Axis(Axis.Name.X, start="RSPH", end="RLM"),
axis_to_keep=Axis.Name.Z,
),
inertia_parameters=foot_inertia_parameters,
mesh=Mesh(("RLM", "RTT2", "RSPH", "RLM"), is_local=False),
)
)
reduced_model.segments["RFoot"].add_marker(Marker("RTT2", is_technical=True, is_anatomical=True))
reduced_model.add_segment(
Segment(
name="LFemur",
parent_name="Pelvis",
rotations=Rotations.XY,
inertia_parameters=de_leva[SegmentName.THIGH],
segment_coordinate_system=SegmentCoordinateSystem(
origin=lambda m, bio: SegmentCoordinateSystemUtils.mean_markers(["LPSIS", "LASIS"])(static_trial, None)
- np.array([0.0, 0.0, 0.05 * total_height, 0.0]),
first_axis=Axis(name=Axis.Name.X, start="LLFE", end="LMFE"),
second_axis=Axis(
name=Axis.Name.Z,
start=SegmentCoordinateSystemUtils.mean_markers(["LMFE", "LLFE"]),
end=SegmentCoordinateSystemUtils.mean_markers(["LPSIS", "LASIS"]),
),
axis_to_keep=Axis.Name.Z,
),
mesh=Mesh(
(
lambda m, bio: SegmentCoordinateSystemUtils.mean_markers(["LPSIS", "LASIS"])(static_trial, None)
- np.array([0.0, 0.0, 0.05 * total_height, 0.0]),
"LMFE",
"LLFE",
lambda m, bio: SegmentCoordinateSystemUtils.mean_markers(["LPSIS", "LASIS"])(static_trial, None)
- np.array([0.0, 0.0, 0.05 * total_height, 0.0]),
),
is_local=False,
),
)
)
reduced_model.segments["LFemur"].add_marker(Marker("LLFE", is_technical=True, is_anatomical=True))
reduced_model.segments["LFemur"].add_marker(Marker("LMFE", is_technical=True, is_anatomical=True))
reduced_model.add_segment(
Segment(
name="LTibia",
parent_name="LFemur",
rotations=Rotations.X,
inertia_parameters=de_leva[SegmentName.SHANK],
segment_coordinate_system=SegmentCoordinateSystem(
origin=SegmentCoordinateSystemUtils.mean_markers(["LMFE", "LLFE"]),
first_axis=Axis(name=Axis.Name.X, start="LLM", end="LSPH"),
second_axis=Axis(
name=Axis.Name.Z,
start=SegmentCoordinateSystemUtils.mean_markers(["LSPH", "LLM"]),
end=SegmentCoordinateSystemUtils.mean_markers(["LMFE", "LLFE"]),
),
axis_to_keep=Axis.Name.Z,
),
mesh=Mesh(("LMFE", "LSPH", "LLM", "LLFE"), is_local=False),
)
)
reduced_model.segments["LTibia"].add_marker(Marker("LLM", is_technical=True, is_anatomical=True))
reduced_model.segments["LTibia"].add_marker(Marker("LSPH", is_technical=True, is_anatomical=True))
foot_inertia_parameters = de_leva[SegmentName.FOOT]
rt_matrix = RotoTransMatrix.from_euler_angles_and_translation(
angle_sequence="y",
angles=np.array([-np.pi / 2]),
translation=np.array([0.0, 0.0, 0.0]),
)
foot_inertia_parameters.center_of_mass = lambda m, bio: rt_matrix.rt_matrix @ np.nanmean(
m.markers_center_position(["LSPH", "LLM", "LTT2"]) - m.markers_center_position(["LSPH", "LLM"]),
axis=1,
)
reduced_model.add_segment(
Segment(
name="LFoot",
parent_name="LTibia",
rotations=Rotations.X,
segment_coordinate_system=SegmentCoordinateSystem(
origin=SegmentCoordinateSystemUtils.mean_markers(["LSPH", "LLM"]),
first_axis=Axis(
Axis.Name.Z, start=SegmentCoordinateSystemUtils.mean_markers(["LLM", "LSPH"]), end="LTT2"
),
second_axis=Axis(Axis.Name.X, start="LLM", end="LSPH"),
axis_to_keep=Axis.Name.Z,
),
inertia_parameters=foot_inertia_parameters,
mesh=Mesh(("LLM", "LTT2", "LSPH", "LLM"), is_local=False),
)
)
reduced_model.segments["LFoot"].add_marker(Marker("LTT2", is_technical=True, is_anatomical=True))
# Put the model together, print it and print it to a bioMod file
model_real = reduced_model.to_real(static_trial)
if align_scs:
# If you'd like the RTs to all be aligned, you can use the following step on the real model
joint_center_modifier = JointCoordinateModifier(model_real)
# In this case, the global reference frame is rotated of 90 degrees around the z axis
rotation_to_align_with = RotationMatrix.from_euler_angles("z", np.array([np.pi / 2]))
model_real = joint_center_modifier.align_all_scs(rotation_to_align_with)
model_real.to_biomod(output_model_filepath)
if animate_model:
model_real.animate(view_as=ViewAs.BIORBD, model_path=output_model_filepath)
if remove_temporary:
os.remove(output_model_filepath)
return model_real
def main():
# Load the static trial
static_trial = C3dData(f"data/static_lower_body.c3d")
model_creation_from_measured_data(static_trial)
if __name__ == "__main__":
main()