diff --git a/weldx/transformations/cs_manager.py b/weldx/transformations/cs_manager.py index 1cb4ff002..82917fcb3 100644 --- a/weldx/transformations/cs_manager.py +++ b/weldx/transformations/cs_manager.py @@ -17,10 +17,11 @@ from weldx.exceptions import WeldxDeprecationWarning, WeldxException from weldx.geometry import SpatialData from weldx.time import Time, types_time_like, types_timestamp_like +from weldx.types import QuantityLike from weldx.util import check_matplotlib_available, dataclass_nested_eq from .local_cs import LocalCoordinateSystem -from .types import types_coordinates, types_orientation +from .types import types_coordinates, types_homogeneous, types_orientation # only import heavy-weight packages on type checking if TYPE_CHECKING: # pragma: no cover @@ -830,6 +831,51 @@ def create_cs_from_axis_vectors( coordinate_system_name, reference_system_name, lcs, lcs_child_in_parent ) + def create_cs_from_homogenous_transformation( + self, + coordinate_system_name: str, + reference_system_name: str, + transformation_matrix: types_homogeneous, + translation_unit: QuantityLike, + time: types_time_like = None, + time_ref: types_timestamp_like = None, + lcs_child_in_parent: bool = True, + ): + """Create a coordinate system from a homogeneous transformation matrix and add + it to the coordinate system manager. + + This function uses the `LocalCoordinateSystem.from_homogeneous_transformation` + method of the `LocalCoordinateSystem` class. + + Parameters + ---------- + coordinate_system_name : + Name of the new coordinate system. + reference_system_name : + Name of the parent system. This must have been already added. + transformation_matrix : + Describes the homogeneous transformation matrix that includes the rotation + and the translation (coordinates). + translation_unit : + Unit describing the value of the translation. Necessary, because the + homogeneous transformation matrix is unitless. + time : + Time data for time dependent coordinate systems. + time_ref : + Reference time for time dependent coordinate systems + lcs_child_in_parent : + If set to `True`, the passed `LocalCoordinateSystem` instance describes + the new system orientation towards is parent. If `False`, it describes + how the parent system is positioned in its new child system. + + """ + lcs = LocalCoordinateSystem.from_homogenous_transformation( + transformation_matrix, translation_unit, time, time_ref + ) + self.add_cs( + coordinate_system_name, reference_system_name, lcs, lcs_child_in_parent + ) + def delete_cs(self, coordinate_system_name: str, delete_children: bool = False): """Delete a coordinate system from the coordinate system manager. diff --git a/weldx/transformations/local_cs.py b/weldx/transformations/local_cs.py index ac976d776..9076a6cbe 100644 --- a/weldx/transformations/local_cs.py +++ b/weldx/transformations/local_cs.py @@ -18,8 +18,13 @@ from weldx.core import TimeSeries from weldx.exceptions import WeldxException from weldx.time import Time, TimeDependent, types_time_like, types_timestamp_like -from weldx.transformations.types import types_coordinates, types_orientation +from weldx.transformations.types import ( + types_coordinates, + types_homogeneous, + types_orientation, +) from weldx.transformations.util import normalize +from weldx.types import QuantityLike __all__ = ("LocalCoordinateSystem",) @@ -540,6 +545,39 @@ def from_axis_vectors( t_axes = (1, 0) if mat.ndim == 2 else (1, 2, 0) return cls(mat.transpose(t_axes), coordinates, time, time_ref) + @classmethod + def from_homogeneous_transformation( + cls, + transformation_matrix: types_homogeneous, + translation_unit: QuantityLike, + time: types_time_like = None, + time_ref: types_timestamp_like = None, + ) -> LocalCoordinateSystem: + """Construct a local coordinate system from a homogeneous transformation matrix. + + Parameters + ---------- + transformation_matrix : + Describes the homogeneous transformation matrix that includes the rotation + and the translation (coordinates). + translation_unit : + Unit describing the value of the translation. Necessary, because the + homogeneous transformation matrix is unitless. + time : + Time data for time dependent coordinate systems (Default value = None) + time_ref : + Optional reference timestamp if ``time`` is a time delta. + + Returns + ------- + LocalCoordinateSystem + Local coordinate system + + """ + orientation = transformation_matrix[:, :3, :3] + coordinates = Q_(transformation_matrix[:, :3, 3], translation_unit) + return cls(orientation, coordinates=coordinates, time=time, time_ref=time_ref) + @property def orientation(self) -> xr.DataArray: """Get the coordinate systems orientation matrix. diff --git a/weldx/transformations/types.py b/weldx/transformations/types.py index 2e724913d..668bc6bd7 100644 --- a/weldx/transformations/types.py +++ b/weldx/transformations/types.py @@ -9,9 +9,11 @@ types_coordinates = Union[xr.DataArray, npt.ArrayLike, pint.Quantity] types_orientation = Union[xr.DataArray, npt.ArrayLike, Rotation] +types_homogeneous = Union[xr.DataArray, npt.ArrayLike] __all__ = [ "types_coordinates", "types_orientation", + "types_homogeneous", ]