|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import rerun as rr |
| 6 | + |
| 7 | +from .phase_rerun import PhaseRerun |
| 8 | +from .pyomarkers import PyoMarkers |
| 9 | +from .multi_frame_rate_phase_rerun import MultiFrameRatePhaseRerun |
| 10 | + |
| 11 | + |
| 12 | +def rrtrc( |
| 13 | + trc_filename: str, |
| 14 | + marker_trajectories: bool = False, |
| 15 | + show_floor: bool = True, |
| 16 | + notebook: bool = False, |
| 17 | +) -> None: |
| 18 | + """ |
| 19 | + Display a c3d file in rerun. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + trc_filename: str |
| 24 | + The path to the trc file. |
| 25 | + marker_trajectories: bool |
| 26 | + If True, show the marker trajectories. |
| 27 | + show_floor: bool |
| 28 | + If True, show the floor. |
| 29 | + notebook: bool |
| 30 | + If True, display the animation in the notebook. |
| 31 | + """ |
| 32 | + |
| 33 | + # Load a c3d file |
| 34 | + pyomarkers = PyoMarkers.from_trc(trc_filename) |
| 35 | + units = pyomarkers.units |
| 36 | + pyomarkers = adjust_position_unit_to_meters(pyomarkers, pyomarkers.units) |
| 37 | + pyomarkers.show_labels = False |
| 38 | + |
| 39 | + t_span = pyomarkers.time |
| 40 | + filename = Path(trc_filename).name |
| 41 | + |
| 42 | + phase_reruns = [] |
| 43 | + phase_rerun = PhaseRerun(t_span) |
| 44 | + phase_reruns.append(phase_rerun) |
| 45 | + phase_rerun.add_xp_markers(filename, pyomarkers) |
| 46 | + |
| 47 | + if show_floor: |
| 48 | + square_width = max_xy_coordinate_span_by_markers(pyomarkers) |
| 49 | + lowest_corner = 0 |
| 50 | + phase_rerun.add_floor(square_width, height_offset=lowest_corner - 0.0005) |
| 51 | + |
| 52 | + multi_phase_rerun = MultiFrameRatePhaseRerun(phase_reruns) |
| 53 | + multi_phase_rerun.rerun(filename, notebook=notebook) |
| 54 | + |
| 55 | + if marker_trajectories: |
| 56 | + # # todo: find a better way to display curves but hacky way ok for now |
| 57 | + marker_names = phase_rerun.xp_data.xp_data[0].marker_names |
| 58 | + for m in marker_names: |
| 59 | + for j, axis in enumerate(["X", "Y", "Z"]): |
| 60 | + rr.send_columns( |
| 61 | + f"markers_graphs/{m}/{axis}", |
| 62 | + indexes=[rr.TimeColumn("stable_time", duration=t_span)], |
| 63 | + columns=[ |
| 64 | + *rr.Scalars.columns( |
| 65 | + scalars=phase_rerun.xp_data.xp_data[0].markers_numpy[j, marker_names.index(m), :] |
| 66 | + ) |
| 67 | + ], |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def max_xy_coordinate_span_by_markers(pyomarkers: PyoMarkers) -> float: |
| 72 | + """Return the max span of the x and y coordinates of the markers.""" |
| 73 | + min_pyomarkers = np.nanmin(np.nanmin(pyomarkers.to_numpy(), axis=2), axis=1) |
| 74 | + max_pyomarkers = np.nanmax(np.nanmax(pyomarkers.to_numpy(), axis=2), axis=1) |
| 75 | + x_absolute_max = np.nanmax(np.abs([min_pyomarkers[0], max_pyomarkers[0]])) |
| 76 | + y_absolute_max = np.nanmax(np.abs([min_pyomarkers[1], max_pyomarkers[1]])) |
| 77 | + |
| 78 | + return np.max([x_absolute_max, y_absolute_max]) |
| 79 | + |
| 80 | + |
| 81 | +def adjust_pyomarkers_unit_to_meters(pyomarkers: PyoMarkers, unit: str) -> PyoMarkers: |
| 82 | + """Adjust the positions to meters for displaying purposes.""" |
| 83 | + pyomarkers = adjust_position_unit_to_meters(pyomarkers, unit) |
| 84 | + pyomarkers.attrs["units"] = "m" |
| 85 | + return pyomarkers |
| 86 | + |
| 87 | + |
| 88 | +def adjust_position_unit_to_meters(array: Any, unit: str) -> Any: |
| 89 | + conversion_factors = {"mm": 1000, "cm": 100, "m": 1} |
| 90 | + for u, factor in conversion_factors.items(): |
| 91 | + if u in unit: |
| 92 | + array /= factor |
| 93 | + break |
| 94 | + else: |
| 95 | + raise ValueError("The unit of the c3d file is not in meters, mm or cm.") |
| 96 | + return array |
0 commit comments