Skip to content

Commit 6c7346a

Browse files
authored
Merge pull request #209 from pariterre/master
Added a function to get the index of marker and segment from name
2 parents bd95083 + 1bfb283 commit 6c7346a

File tree

5 files changed

+70
-30
lines changed

5 files changed

+70
-30
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.0")
33
cmake_policy(SET CMP0042 NEW)
44
endif()
55

6-
project(biorbd VERSION 1.5.2)
6+
project(biorbd VERSION 1.5.3)
77
set (CMAKE_CXX_STANDARD 11)
88
set (BIORBD_NAME ${PROJECT_NAME})
99

binding/python3/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .biorbd import *
22
from ._version import __version__
3-
from .surface_max_torque_actuator import surface_max_torque_actuator
3+
from .surface_max_torque_actuator import *
4+
from .rigid_body import *
45

56
if biorbd.currentLinearAlgebraBackend() == 1:
67
from casadi import Function, MX, horzcat

binding/python3/rigid_body.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def marker_index(model, marker_name):
2+
try:
3+
return [n.to_string() for n in model.markerNames()].index(marker_name)
4+
except ValueError:
5+
raise ValueError(f"{marker_name} is not in the biorbd model")
6+
7+
8+
def segment_index(model, segment_name):
9+
try:
10+
return [model.segment(i).name().to_string() for i in range(model.nbSegment())].index(segment_name)
11+
except ValueError:
12+
raise ValueError(f"{segment_name} is not in the biorbd model")

examples/Test_longueur_wrapping.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# functions for cylinder rotation
99
def cyl2cart(r, theta, z):
10-
return (r*np.cos(theta), r*np.sin(theta), z)
10+
return (r * np.cos(theta), r * np.sin(theta), z)
1111

1212

1313
def roll(R, zi, zf, RT):
@@ -20,23 +20,26 @@ def roll(R, zi, zf, RT):
2020
x, y, z = cyl2cart(r, t, z)
2121

2222
# Euler rotation
23-
rot = np.dot(
24-
RT.rot().to_array(),
25-
np.array([x.ravel(), y.ravel(), z.ravel()])
26-
)
23+
rot = np.dot(RT.rot().to_array(), np.array([x.ravel(), y.ravel(), z.ravel()]))
2724

2825
x_rt = rot[0, :].reshape(x.shape) + RTw.trans().to_array()[0]
2926
y_rt = rot[1, :].reshape(y.shape) + RTw.trans().to_array()[1]
3027
z_rt = rot[2, :].reshape(z.shape) + RTw.trans().to_array()[2]
3128

32-
return x_rt, y_rt, z_rt,
29+
return (
30+
x_rt,
31+
y_rt,
32+
z_rt,
33+
)
34+
3335

3436
# model
3537
model = biorbd.Model("WrappingObjectExample.bioMod")
3638

37-
# wrapping RT matrix
38-
RTw = biorbd.WrappingCylinder(
39-
model.muscle(0).pathModifier().object(0)).RT(model, np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1]))
39+
# wrapping RT matrix
40+
RTw = biorbd.WrappingCylinder(model.muscle(0).pathModifier().object(0)).RT(
41+
model, np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1])
42+
)
4043
RTw_trans = RTw.transpose()
4144

4245
# Point in global base
@@ -47,27 +50,29 @@ def roll(R, zi, zf, RT):
4750

4851
# Plot the surface
4952
fig = plt.figure()
50-
ax = fig.add_subplot(111, projection='3d')
53+
ax = fig.add_subplot(111, projection="3d")
5154
x, y, z = roll(0.05, -0.2, 0.2, RTw)
5255
ax.plot_surface(x, y, z)
53-
plt.plot((Po[0], Po_wrap[0]), (Po[1], Po_wrap[1]), (Po[2], Po_wrap[2]), marker='o')
54-
plt.plot((Pi[0], Pi_wrap[0]), (Pi[1], Pi_wrap[1]), (Pi[2], Pi_wrap[2]), marker='o')
56+
plt.plot((Po[0], Po_wrap[0]), (Po[1], Po_wrap[1]), (Po[2], Po_wrap[2]), marker="o")
57+
plt.plot((Pi[0], Pi_wrap[0]), (Pi[1], Pi_wrap[1]), (Pi[2], Pi_wrap[2]), marker="o")
5558

5659
# To set Axe3D to (almost) equal
57-
max_range = np.array([x.max()-x.min(), y.max()-y.min(), z.max()-z.min()]).max()
58-
Xb = 0.5*max_range*np.mgrid[-1:2:2, -1:2:2, -1:2:2][0].flatten() + 0.5*(x.max()+x.min())
59-
Yb = 0.5*max_range*np.mgrid[-1:2:2, -1:2:2, -1:2:2][1].flatten() + 0.5*(y.max()+y.min())
60-
Zb = 0.5*max_range*np.mgrid[-1:2:2, -1:2:2, -1:2:2][2].flatten() + 0.5*(z.max()+z.min())
60+
max_range = np.array([x.max() - x.min(), y.max() - y.min(), z.max() - z.min()]).max()
61+
Xb = 0.5 * max_range * np.mgrid[-1:2:2, -1:2:2, -1:2:2][0].flatten() + 0.5 * (x.max() + x.min())
62+
Yb = 0.5 * max_range * np.mgrid[-1:2:2, -1:2:2, -1:2:2][1].flatten() + 0.5 * (y.max() + y.min())
63+
Zb = 0.5 * max_range * np.mgrid[-1:2:2, -1:2:2, -1:2:2][2].flatten() + 0.5 * (z.max() + z.min())
6164
for xb, yb, zb in zip(Xb, Yb, Zb):
62-
ax.plot([xb], [yb], [zb], 'w')
65+
ax.plot([xb], [yb], [zb], "w")
6366
plt.show()
6467

6568
# Compute muscle length
6669
r = biorbd.WrappingCylinder(model.muscle(0).pathModifier().object(0)).radius()
6770
l_muscle_bd = model.muscle(0).musculoTendonLength(model, np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1]))
6871

6972

70-
l_wt_arc = np.sqrt((Po_wrap[0]-Po[0])**2 + (Po_wrap[1]-Po[1])**2 + (Po_wrap[2]-Po[2])**2) + np.sqrt((Pi_wrap[0]-Pi[0])**2 + (Pi_wrap[1]-Pi[1])**2 + (Pi_wrap[2]-Pi[2])**2)
73+
l_wt_arc = np.sqrt((Po_wrap[0] - Po[0]) ** 2 + (Po_wrap[1] - Po[1]) ** 2 + (Po_wrap[2] - Po[2]) ** 2) + np.sqrt(
74+
(Pi_wrap[0] - Pi[0]) ** 2 + (Pi_wrap[1] - Pi[1]) ** 2 + (Pi_wrap[2] - Pi[2]) ** 2
75+
)
7176

7277
# Pi_wrap and Po_wrap provide in wrapping base to compute arc length
7378
Pi_wrap = biorbd.Vector3d(0.28997869688863276, 0.34739632020407846, 0.6636920365713757)
@@ -78,10 +83,14 @@ def roll(R, zi, zf, RT):
7883
Po_wrap.applyRT(RTw_trans)
7984
Po_wrap = Po_wrap.to_array()
8085

81-
arc = math.acos(
82-
((Po_wrap[0] * Pi_wrap[0]) + (Pi_wrap[1] * Po_wrap[1])) / (
83-
math.sqrt(((Pi_wrap[1]**2)+(Pi_wrap[0]**2)) * ((Po_wrap[1]**2) + (Po_wrap[0]**2))))) * r
86+
arc = (
87+
math.acos(
88+
((Po_wrap[0] * Pi_wrap[0]) + (Pi_wrap[1] * Po_wrap[1]))
89+
/ (math.sqrt(((Pi_wrap[1] ** 2) + (Pi_wrap[0] ** 2)) * ((Po_wrap[1] ** 2) + (Po_wrap[0] ** 2))))
90+
)
91+
* r
92+
)
8493

85-
l_arc = math.sqrt(arc**2 + (Po_wrap[2]-Pi_wrap[2])**2)
94+
l_arc = math.sqrt(arc ** 2 + (Po_wrap[2] - Pi_wrap[2]) ** 2)
8695

87-
l_m = l_wt_arc + l_arc # l_m = 0.582246096069153
96+
l_m = l_wt_arc + l_arc # l_m = 0.582246096069153

test/binding/Python3/test_binder_python_rigidbody.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ def test_set_vector3d():
218218
m.setGravity(np.array((0, 0, -2)))
219219
if biorbd.currentLinearAlgebraBackend() == 1:
220220
from casadi import MX
221+
221222
get_gravity = biorbd.to_casadi_func("Compute_Markers", m.getGravity)()["o0"]
222223
else:
223224
get_gravity = m.getGravity().to_array()
@@ -238,21 +239,22 @@ def check_value(target):
238239

239240
m.segment(0).characteristics().setMass(11.0)
240241
check_value(11.0)
241-
242+
242243
with pytest.raises(ValueError, match="Scalar must be a 1x1 array or a float"):
243244
m.segment(0).characteristics().setMass(np.array([]))
244-
245+
245246
m.segment(0).characteristics().setMass(np.array((12,)))
246247
check_value(12.0)
247-
248+
248249
m.segment(0).characteristics().setMass(np.array([[13]]))
249250
check_value(13.0)
250-
251+
251252
with pytest.raises(ValueError, match="Scalar must be a 1x1 array or a float"):
252253
m.segment(0).characteristics().setMass(np.array([[[14]]]))
253-
254+
254255
if biorbd.currentLinearAlgebraBackend() == 1:
255256
from casadi import MX
257+
256258
m.segment(0).characteristics().setMass(MX(15))
257259
check_value(15.0)
258260

@@ -358,3 +360,19 @@ def test_forward_dynamics_constraints_direct():
358360

359361
np.testing.assert_almost_equal(qddot.squeeze(), qddot_expected)
360362
np.testing.assert_almost_equal(cs_forces.squeeze(), contact_forces_expected)
363+
364+
365+
def test_name_to_index():
366+
m = biorbd.Model("../../models/pyomecaman.bioMod")
367+
368+
# Index of a segment
369+
np.testing.assert_equal(biorbd.segment_index(m, "Pelvis"), 0)
370+
np.testing.assert_equal(biorbd.segment_index(m, "PiedG"), 10)
371+
with pytest.raises(ValueError, match="dummy is not in the biorbd model"):
372+
biorbd.segment_index(m, "dummy")
373+
374+
# Index of a marker
375+
np.testing.assert_equal(biorbd.marker_index(m, "pelv1"), 0)
376+
np.testing.assert_equal(biorbd.marker_index(m, "piedg6"), 96)
377+
with pytest.raises(ValueError, match="dummy is not in the biorbd model"):
378+
biorbd.marker_index(m, "dummy")

0 commit comments

Comments
 (0)