-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #358 from EveCharbie/model_creation_update
Model creation (BIS)
- Loading branch information
Showing
38 changed files
with
1,929 additions
and
649 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
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
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
40 changes: 33 additions & 7 deletions
40
binding/python3/model_creation/biomechanical_model_real.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Callable | ||
|
||
from .protocols import Data | ||
from .translations import Translations | ||
from .contact_real import ContactReal | ||
|
||
|
||
class Contact: | ||
def __init__( | ||
self, | ||
name: str, | ||
function: Callable | str = None, | ||
parent_name: str = None, | ||
axis: Translations = None, | ||
): | ||
""" | ||
Parameters | ||
---------- | ||
name | ||
The name of the new contact | ||
function | ||
The function (f(m) -> np.ndarray, where m is a dict of markers) that defines the contact with. | ||
parent_name | ||
The name of the parent the contact is attached to | ||
axis | ||
The axis of the contact | ||
""" | ||
self.name = name | ||
function = function if function is not None else self.name | ||
self.function = (lambda m, bio: m[function]) if isinstance(function, str) else function | ||
self.parent_name = parent_name | ||
self.axis = axis | ||
|
||
def to_contact(self, data: Data) -> ContactReal: | ||
return ContactReal.from_data( | ||
data, | ||
self.name, | ||
self.function, | ||
self.parent_name, | ||
self.axis, | ||
) |
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,80 @@ | ||
from typing import Callable | ||
|
||
import numpy as np | ||
|
||
from .protocols import Data | ||
from .translations import Translations | ||
|
||
|
||
class ContactReal: | ||
def __init__( | ||
self, | ||
name: str, | ||
parent_name: str, | ||
position: tuple[int | float, int | float, int | float] | np.ndarray = None, | ||
axis: Translations = None, | ||
): | ||
""" | ||
Parameters | ||
---------- | ||
name | ||
The name of the new contact | ||
parent_name | ||
The name of the parent the contact is attached to | ||
position | ||
The 3d position of the contact | ||
axis | ||
The axis of the contact | ||
""" | ||
self.name = name | ||
self.parent_name = parent_name | ||
if position is None: | ||
position = np.array((0, 0, 0, 1)) | ||
self.position = position if isinstance(position, np.ndarray) else np.array(position) | ||
self.axis = axis | ||
|
||
@staticmethod | ||
def from_data( | ||
data: Data, | ||
name: str, | ||
function: Callable, | ||
parent_name: str, | ||
axis: Translations = None, | ||
): | ||
""" | ||
This is a constructor for the Contact class. It evaluates the function that defines the contact to get an | ||
actual position | ||
Parameters | ||
---------- | ||
data | ||
The data to pick the data from | ||
name | ||
The name of the new contact | ||
function | ||
The function (f(m) -> np.ndarray, where m is a dict of markers (XYZ1 x time)) that defines the contacts in the local joint coordinates. | ||
parent_name | ||
The name of the parent the contact is attached to | ||
axis | ||
The axis of the contact | ||
""" | ||
|
||
# Get the position of the contact points and do some sanity checks | ||
p: np.ndarray = function(data.values) | ||
if not isinstance(p, np.ndarray): | ||
raise RuntimeError(f"The function {function} must return a np.ndarray of dimension 3xT (XYZ x time)") | ||
if p.shape == (3, 1): | ||
p = p.reshape((3,)) | ||
elif p.shape != (3,): | ||
raise RuntimeError(f"The function {function} must return a vector of dimension 3 (XYZ)") | ||
|
||
return ContactReal(name, parent_name, p, axis) | ||
|
||
def __str__(self): | ||
# Define the print function, so it automatically formats things in the file properly | ||
out_string = f"contact\t{self.name}\n" | ||
out_string += f"\tparent\t{self.parent_name}\n" | ||
out_string += f"\tposition\t{np.round(self.position[0], 4)}\t{np.round(self.position[1], 4)}\t{np.round(self.position[2], 4)}\n" | ||
out_string += f"\taxis\t{self.axis.value}\n" | ||
out_string += "endcontact\n" | ||
return out_string |
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
Oops, something went wrong.