-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
231 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import numpy as np | ||
|
||
|
||
def human_center_of_mass( | ||
joints_spine: np.array, | ||
joints_left_arm: np.array, | ||
joints_right_arm: np.array, | ||
joints_left_leg: np.array, | ||
joints_right_leg: np.array, | ||
) -> np.array: | ||
""" | ||
Compute the center of mass of a human body definedy by the standard human weight distribution. | ||
Each arm accounts for a 5% of the body weight, each leg accounts for a 15% of the body weight | ||
and the spine accounts for a 60% of the body weight. | ||
Parameters | ||
---------- | ||
joints_spine : np.array[..., n_joints, 3] | ||
Joint positions of the spine. | ||
joints_left_arm : np.array[..., n_joints, 3] | ||
Joint positions of the left arm. | ||
joints_right_arm : np.array[..., n_joints, 3] | ||
Joint positions of the right arm. | ||
joints_left_leg : np.array[..., n_joints, 3] | ||
Joint positions of the left leg. | ||
joints_right_leg : np.array[..., n_joints, 3] | ||
Joint positions of the right leg. | ||
Returns | ||
------- | ||
center_of_mass : np.array[..., 3] | ||
Center of mass. | ||
""" | ||
n_joints_spine = joints_spine.shape[-2] | ||
n_joints_left_arm = joints_left_arm.shape[-2] | ||
n_joints_right_arm = joints_right_arm.shape[-2] | ||
n_joints_left_leg = joints_left_leg.shape[-2] | ||
n_joints_right_leg = joints_right_leg.shape[-2] | ||
weights = np.array( | ||
[0.6 / n_joints_spine] * n_joints_spine | ||
+ [0.05 / n_joints_left_arm] * n_joints_left_arm | ||
+ [0.05 / n_joints_right_arm] * n_joints_right_arm | ||
+ [0.15 / n_joints_left_leg] * n_joints_left_leg | ||
+ [0.15 / n_joints_right_leg] * n_joints_right_leg | ||
) | ||
joints = np.concatenate( | ||
[joints_spine, joints_left_arm, joints_right_arm, joints_left_leg, joints_right_leg], axis=-2 | ||
) | ||
return center_of_mass(joints, weights) | ||
|
||
|
||
def center_of_mass(joints: np.array, weights: np.array) -> np.array: | ||
""" | ||
Compute the center of mass of a set of joints. | ||
Parameters | ||
---------- | ||
joints : np.array[..., n_joints, 3] | ||
Joint positions. | ||
weights : np.array[..., n_joints] | ||
Weights of the joints. The weights should sum to 1 along the last dimension. | ||
Returns | ||
------- | ||
center_of_mass : np.array[..., 3] | ||
Center of mass. | ||
""" | ||
return np.sum(joints * weights[..., np.newaxis], axis=-2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import torch | ||
|
||
|
||
def human_center_of_mass( | ||
joints_spine: torch.Tensor, | ||
joints_left_arm: torch.Tensor, | ||
joints_right_arm: torch.Tensor, | ||
joints_left_leg: torch.Tensor, | ||
joints_right_leg: torch.Tensor, | ||
) -> torch.Tensor: | ||
""" | ||
Compute the center of mass of a human body definedy by the standard human weight distribution. | ||
Each arm accounts for a 5% of the body weight, each leg accounts for a 15% of the body weight | ||
and the spine accounts for a 60% of the body weight. | ||
Parameters | ||
---------- | ||
joints_spine : torch.Tensor[..., n_joints, 3] | ||
Joint positions of the spine. | ||
joints_left_arm : torch.Tensor[..., n_joints, 3] | ||
Joint positions of the left arm. | ||
joints_right_arm : torch.Tensor[..., n_joints, 3] | ||
Joint positions of the right arm. | ||
joints_left_leg : torch.Tensor[..., n_joints, 3] | ||
Joint positions of the left leg. | ||
joints_right_leg : torch.Tensor[..., n_joints, 3] | ||
Joint positions of the right leg. | ||
Returns | ||
------- | ||
center_of_mass : torch.Tensor[..., 3] | ||
Center of mass. | ||
""" | ||
n_joints_spine = joints_spine.shape[-2] | ||
n_joints_left_arm = joints_left_arm.shape[-2] | ||
n_joints_right_arm = joints_right_arm.shape[-2] | ||
n_joints_left_leg = joints_left_leg.shape[-2] | ||
n_joints_right_leg = joints_right_leg.shape[-2] | ||
weights = torch.tensor( | ||
[0.6 / n_joints_spine] * n_joints_spine | ||
+ [0.05 / n_joints_left_arm] * n_joints_left_arm | ||
+ [0.05 / n_joints_right_arm] * n_joints_right_arm | ||
+ [0.15 / n_joints_left_leg] * n_joints_left_leg | ||
+ [0.15 / n_joints_right_leg] * n_joints_right_leg, | ||
device=joints_spine.device, | ||
) | ||
joints = torch.concatenate( | ||
[joints_spine, joints_left_arm, joints_right_arm, joints_left_leg, joints_right_leg], axis=-2 | ||
) | ||
return center_of_mass(joints, weights) | ||
|
||
|
||
def center_of_mass(joints: torch.Tensor, weights: torch.Tensor) -> torch.Tensor: | ||
""" | ||
Compute the center of mass of a set of joints. | ||
Parameters | ||
---------- | ||
joints : torch.Tensor[..., n_joints, 3] | ||
Joint positions. | ||
weights : torch.Tensor[..., n_joints] | ||
Weights of the joints. The weights should sum to 1 along the last dimension. | ||
Returns | ||
------- | ||
center_of_mass : torch.Tensor[..., 3] | ||
Center of mass. | ||
""" | ||
return torch.sum(joints * weights[..., None], dim=-2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import numpy as np | ||
from pymotion.ops.center_of_mass import center_of_mass, human_center_of_mass | ||
from pymotion.ops.center_of_mass_torch import ( | ||
center_of_mass as center_of_mass_torch, | ||
human_center_of_mass as human_center_of_mass_torch, | ||
) | ||
from numpy.testing import assert_allclose | ||
import torch | ||
|
||
|
||
class TestCoM: | ||
atol = 1e-6 | ||
low_atol = 1e-3 # for those operations that are not as precise | ||
|
||
def test_com(self): | ||
assert_allclose( | ||
center_of_mass( | ||
np.array([[[0, 0, 0]]]), | ||
np.array([[1]]), | ||
), | ||
np.array([[0, 0, 0]]), | ||
atol=self.atol, | ||
) | ||
assert_allclose( | ||
center_of_mass( | ||
np.array([[[0, 0, 0], [1, 0, 0]]]), | ||
np.array([[0.5, 0.5]]), | ||
), | ||
np.array([[0.5, 0, 0]]), | ||
atol=self.atol, | ||
) | ||
assert_allclose( | ||
center_of_mass( | ||
np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]), | ||
np.array([[0.3, 0.3, 0.4]]), | ||
), | ||
np.array([[4.3, 5.3, 6.3]]), | ||
atol=self.atol, | ||
) | ||
assert_allclose( | ||
center_of_mass_torch( | ||
torch.tensor([[[0, 0, 0]]]), | ||
torch.tensor([[1]]), | ||
), | ||
torch.tensor([[0, 0, 0]]), | ||
atol=self.atol, | ||
) | ||
assert_allclose( | ||
center_of_mass_torch( | ||
torch.tensor([[[0, 0, 0], [1, 0, 0]]]), | ||
torch.tensor([[0.5, 0.5]]), | ||
), | ||
torch.tensor([[0.5, 0, 0]]), | ||
atol=self.atol, | ||
) | ||
assert_allclose( | ||
center_of_mass_torch( | ||
torch.tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]), | ||
torch.tensor([[0.3, 0.3, 0.4]]), | ||
), | ||
torch.tensor([[4.3, 5.3, 6.3]]), | ||
atol=self.atol, | ||
) | ||
assert_allclose( | ||
human_center_of_mass( | ||
np.array([[[2, 1, 5]]]), | ||
np.array([[[1, 3, 4]]]), | ||
np.array([[[1, 1, 1]]]), | ||
np.array([[[0, 7, 6]]]), | ||
np.array([[[2, 8, 1]]]), | ||
), | ||
np.array([[1.6, 3.05, 4.3]]), | ||
) | ||
assert_allclose( | ||
human_center_of_mass_torch( | ||
torch.tensor([[[2, 1, 5]]]), | ||
torch.tensor([[[1, 3, 4]]]), | ||
torch.tensor([[[1, 1, 1]]]), | ||
torch.tensor([[[0, 7, 6]]]), | ||
torch.tensor([[[2, 8, 1]]]), | ||
), | ||
torch.tensor([[1.6, 3.05, 4.3]]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters