-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate_model.py
More file actions
141 lines (122 loc) · 4.45 KB
/
create_model.py
File metadata and controls
141 lines (122 loc) · 4.45 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
"""
This example shows how to add different components to a model from scratch.
"""
import os
from pathlib import Path
import numpy as np
import biorbd
from biobuddy import (
BiomechanicalModel,
MeshFile,
Segment,
Contact,
MuscleGroup,
Muscle,
MuscleType,
MuscleStateType,
Translations,
Rotations,
RangeOfMotion,
Ranges,
ViaPoint,
)
def complex_model_from_scratch(mesh_path, remove_temporary: bool = True):
"""
We define a new model by feeding in the actual dimension and position of the model.
Please note that this model is not a human, it is only used to show the functionalities of the model creation module.
"""
kinematic_model_filepath = "temporary_complex.bioMod"
# Create a model holder
bio_model = BiomechanicalModel()
# The ground segment
bio_model.add_segment(Segment(name="GROUND"))
# The pendulum segment
bio_model.add_segment(
Segment(
name="PENDULUM",
translations=Translations.XYZ,
rotations=Rotations.X,
q_ranges=RangeOfMotion(range_type=Ranges.Q, min_bound=[-1, -1, -1, -np.pi], max_bound=[1, 1, 1, np.pi]),
qdot_ranges=RangeOfMotion(
range_type=Ranges.Qdot, min_bound=[-10, -10, -10, -np.pi * 10], max_bound=[10, 10, 10, np.pi * 10]
),
mesh_file=MeshFile(
mesh_file_name="pendulum.STL",
mesh_file_directory=mesh_path,
mesh_color=np.array([0, 0, 1]),
scaling_function=lambda m, model: np.array([1, 1, 10]),
rotation_function=lambda m, model: np.array([np.pi / 2, 0, 0]),
translation_function=lambda m, model: np.array([0.1, 0, 0]),
),
)
)
# The pendulum segment contact point
bio_model.segments["PENDULUM"].add_contact(
Contact(
name="PENDULUM_CONTACT",
function=lambda m, model: np.array([0, 0, 0]),
parent_name="PENDULUM",
axis=Translations.XYZ,
)
)
# The pendulum muscle group
bio_model.add_muscle_group(
MuscleGroup(name="PENDULUM_MUSCLE_GROUP", origin_parent_name="GROUND", insertion_parent_name="PENDULUM")
)
# The pendulum muscle
bio_model.muscle_groups["PENDULUM_MUSCLE_GROUP"].add_muscle(
Muscle(
"PENDULUM_MUSCLE",
muscle_type=MuscleType.HILL_THELEN,
state_type=MuscleStateType.DEGROOTE,
muscle_group="PENDULUM_MUSCLE_GROUP",
origin_position=ViaPoint(
name="PENDULUM_ORIGIN",
parent_name="GROUND",
muscle_name="PENDULUM_MUSCLE",
muscle_group="PENDULUM_MUSCLE_GROUP",
position_function=lambda m, model: np.array([0, 0, 0]),
),
insertion_position=ViaPoint(
name="PENDULUM_ORIGIN",
parent_name="PENDULUM",
muscle_name="PENDULUM_MUSCLE",
muscle_group="PENDULUM_MUSCLE_GROUP",
position_function=lambda m, model: np.array([0, 0, 1]),
),
optimal_length_function=lambda m, model: 0.1,
maximal_force_function=lambda m, model: 100.0,
tendon_slack_length_function=lambda m, model: 0.05,
pennation_angle_function=lambda m, model: 0.05,
maximal_velocity_function=lambda m, model: 10.0,
maximal_excitation=1,
)
)
bio_model.muscle_groups["PENDULUM_MUSCLE_GROUP"].muscles["PENDULUM_MUSCLE"].add_via_point(
ViaPoint(
"PENDULUM_MUSCLE",
parent_name="PENDULUM",
muscle_name="PENDULUM_MUSCLE",
muscle_group="PENDULUM_MUSCLE_GROUP",
position_function=lambda m, model: np.array([0, 0, 0.5]),
)
)
# Put the model together
real_model = bio_model.to_real({}, None)
# Print it to a bioMod file
real_model.to_biomod(kinematic_model_filepath)
model = biorbd.Model(kinematic_model_filepath)
assert model.nbQ() == 4
assert model.nbSegment() == 3
assert model.nbMarkers() == 0
assert model.nbMuscles() == 1
assert model.nbMuscleGroups() == 1
assert model.nbContacts() == 3
if remove_temporary:
os.remove(kinematic_model_filepath)
return real_model
def main():
current_path_file = Path(__file__).parent
complex_model_from_scratch(mesh_path=f"{current_path_file}/meshFiles")
if __name__ == "__main__":
main()