{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Transformations Tutorial #1: Coordinate Systems\n",
"## Introduction\n",
"\n",
"This tutorial is about the transformation packages `LocalCoordinateSystem` class which describes the orientation and position of a Cartesian coordinate system towards another reference coordinate system.\n",
"The reference coordinate systems origin is always at $(0, 0, 0)$ and its orientation is described by the basis: $e_x = (1, 0, 0)$, $e_y = (0, 1, 0)$, $e_z = (0, 0, 1)$.\n",
"All coordinate systems used by WelDX are positively oriented and follow the [right hand rule](https://en.wikipedia.org/wiki/Right-hand_rule).\n",
"\n",
"Note that this tutorial contains some chapters that address low-level features that will be seldom required when working with WelDX because there are better alternatives.\n",
"The purpose of those chapters is mainly to give a complete overview over the capabilities of the `LocalCoordinateSystem` class.\n",
"We will inform you at the beginning of a chapter if you can skip it without missing out on some important information.\n",
"\n",
"## Imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbsphinx": "hidden"
},
"outputs": [],
"source": [
"# enable interactive plots on Jupyterlab with ipympl and jupyterlab-matplotlib installed\n",
"# %matplotlib widget"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# interactive plots\n",
"import numpy as np\n",
"import pandas as pd\n",
"import xarray as xr\n",
"\n",
"import weldx.visualization as vs\n",
"from weldx import Q_, LocalCoordinateSystem, MathematicalExpression, TimeSeries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Construction\n",
"\n",
"The initializer of the `LocalCoordinateSystem` class takes 2 parameters, the `orientation` and the `coordinates`. \n",
"`orientation` requires a 3x3 matrix. \n",
"It can either be viewed as a rotation/reflection matrix or a set of normalized column vectors that represent the 3 basis vectors of the coordinate system inside of the reference system. \n",
"The matrix needs to be [orthogonal](https://en.wikipedia.org/wiki/Orthogonal_matrix), otherwise, an exception is raised. \n",
"`coordinates` is the position of the `LocalCoordinateSystem`'s origin inside the reference coordinate system.\n",
"The default parameters are the identity matrix and the zero vector. \n",
"Hence, we get a system that is identical to the reference system if no parameter is passed to the constructor:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_ref = LocalCoordinateSystem()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now create some additional coordinate systems.\n",
"Note that coordinates can not be provided as pure numbers but need to be quantities.\n",
"This ensures that coordinates always have a unit.\n",
"If quantities are new to you, checkout [the documentation of the pint package](https://pint.readthedocs.io/en/0.10.1/tutorial.html)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_01 = LocalCoordinateSystem(coordinates=Q_([2, 4, -1], \"mm\"))\n",
"\n",
"rotation = [[0, 1, 0], [-1, 0, 0], [0, 0, 1]]\n",
"lcs_02 = LocalCoordinateSystem(orientation=rotation, coordinates=Q_([0, 0, 3], \"mm\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`lcs_01` has the same orientation as the reference system but a different position.\n",
"`lcs_02` also risides at a position different from the origin.\n",
"Additionally, it is rotated around the z-axis by 90 degrees.\n",
"Below, a plot of the 3 coordinate systems is shown.\n",
"\n",
">Note that the corresponding code cell that produces the plot might be hidden since its content is not relevant for this tutorial.\n",
"This is also the case for all further plots. If those cells are not hidden, just ignore the code and focus on the plots."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_ref, {\"color\": \"r\", \"label\": \"reference_system\"}),\n",
" (lcs_01, {\"color\": \"g\", \"label\": \"system 1\"}),\n",
" (lcs_02, {\"color\": \"c\", \"label\": \"system 2\"}),\n",
" ]\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> **HINT:** Enabled interactive plots in the jupyter notebook version of this tutorial by uncommenting the code in the first cell. \n",
"If you do so, you can rotate the plot by pressing the left mouse button and moving the mouse. \n",
"This helps to understand how the different coordinate systems are positioned in the 3d space.\n",
"\n",
"Since writing down a rotation matrix is usually not as straight forward as a translation vector, there are some additional methods to create a `LocalCoordinateSystem` that let you describe the systems orientation in a more intuitive way.\n",
"With the `from_axis_vectors` method, you specify orthogonal vectors that point into the same directions as the basis vectors of the new coordinate system.\n",
"2 vectors are usually sufficient since the third one can be computed automatically but you can also provide all three.\n",
"\n",
"As an example take these 3 orthogonal vectors of arbitrary length:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = [1, 2, 0]\n",
"b = [-6, 3, 0]\n",
"c = [0, 0, 5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If they are orthogonal, we can use them to specify the orientation of a new `LocalCoordinateSystem`.\n",
"So let's try that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_03 = LocalCoordinateSystem.from_axis_vectors(\n",
" x=a, y=b, z=c, coordinates=Q_([1, 1, 0], \"mm\")\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Seems like it worked.\n",
"If those vectors weren't orthogoal, an exception would have been raised as the following example demonstrates:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"w = [1, 0, 1]\n",
"try:\n",
" LocalCoordinateSystem.from_axis_vectors(\n",
" x=a, y=b, z=w, coordinates=Q_([1, 1, 0], \"mm\")\n",
" )\n",
"except ValueError as e:\n",
" print(f\"The following exception was raised:\\n{e}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we want the third vector to be calculated automatically, we pass just the two vectors we know:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_04 = LocalCoordinateSystem.from_axis_vectors(\n",
" x=a, z=c, coordinates=Q_([1, 1, 2], \"mm\")\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because we used the vectors from before, the orientations of both created coordinate systems should be identical.\n",
"Let's check that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.allclose(lcs_03.orientation, lcs_04.orientation)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that order matters here.\n",
"If we flipped `a` and `c` as x- and z-axis, the calculated third axis would point into the opposite direction because WelDX always calculates the missing one in a way that the resulting system is positively oriented:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_flipped_y = LocalCoordinateSystem.from_axis_vectors(\n",
" x=c, z=a, coordinates=Q_([3, 3, -2], \"mm\")\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a plot of the created coordinate systems:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_ref, {\"color\": \"r\", \"label\": \"reference_system\"}),\n",
" (lcs_03, {\"color\": \"g\", \"label\": \"constructed with 3 vectors\"}),\n",
" (lcs_04, {\"color\": \"c\", \"label\": \"constructed with 2 vectors\"}),\n",
" (\n",
" lcs_flipped_y,\n",
" {\"color\": \"y\", \"label\": \"constructed with 2 vectors (flipped)\"},\n",
" ),\n",
" ]\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another method to create a `LocalCoordinateSystem` is `from_euler`. \n",
"It utilizes a series of rotations around the reference systems' coordinate axis to describe the new systems orientation. \n",
"Its `sequence` parameter expects a string that determines the rotation sequence around the coordinate axes from left to right. \n",
"For example, `\"xyz\"` expresses that the first rotation is around the x-axis, the second around the y-axis, and the last around the z-axis.\n",
"`angles` is either a scalar for a single rotation or a list for a series of rotations.\n",
"As the name suggests, it defines the rotation angles in the same order as the given sequence.\n",
"The parameter `degrees` should be set to `True` if the provided `angles` are in degrees. \n",
"Here is a short example for a single rotation:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_05 = LocalCoordinateSystem.from_euler(\n",
" sequence=\"x\", angles=90, degrees=True, coordinates=Q_([1, -1, -3], \"mm\")\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A rotation sequence would be defined as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_06 = LocalCoordinateSystem.from_euler(\n",
" sequence=\"xy\", angles=[90, -45], degrees=True, coordinates=Q_([-1.5, 2, 0], \"mm\")\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plot looks like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# create 3d plot\n",
"vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_ref, {\"color\": \"r\", \"label\": \"reference_system\"}),\n",
" (lcs_05, {\"color\": \"g\", \"label\": \"single rotation\"}),\n",
" (lcs_06, {\"color\": \"c\", \"label\": \"rotation sequence\"}),\n",
" ]\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Coordinate transformations\n",
"\n",
"> **Note:** This chapter covers rather low-level details. \n",
"Coordinate transformations in WelDX should generally be done using the `CoordinateSystemManager` class ([click to open tutorial](transformations_02_coordinate_system_manager.ipynb)) since it is much easier to use and you do not need to remember some conventions mentioned below.\n",
"You won't have any problems in understanding WelDX if you skip this chapter entirely.\n",
"\n",
"It is quite common that there exists a chain or tree-like dependency between coordinate systems. \n",
"We might have a moving object with a local coordinate system that describes its position and orientation towards a fixed reference coordinate system. \n",
"This object can have another object attached to it, with its position and orientation given in relation to its parent objects coordinate system. \n",
"If we want to know the attached object coordinate system in relation to the reference coordinate system, we have to perform a coordinate transformation.\n",
"\n",
"To avoid confusion about the reference systems of each coordinate system, we will use the following naming convention for the coordinate systems: `lcs_NAME_in_REFERENCE`. \n",
"This is a coordinate system with the name \"NAME\" and its reference system has the name \"REFERENCE\". \n",
"The only exception to this convention will be the root coordinate system `lcs_ref`, which has no reference system.\n",
"\n",
"The `LocalCoordinateSystem` class provides the `+` and `-` operators to change the reference system easily. \n",
"The `+` operator will transform a coordinate system to the reference coordinate system of its current reference system:\n",
"\n",
"~~~ python\n",
"lcs_child_in_ref = lcs_child_in_parent + lcs_parent_in_ref\n",
"~~~\n",
"As the naming of the variables already implies, the `+` operator should only be used if there exists a **child-parent relation** between the left-hand side and right-hand side system.\n",
"If two coordinate systems share a **common reference system**, the `-` operator transforms one of those systems into the other:\n",
"\n",
"~~~ python\n",
"lcs_child_in_parent = lcs_child_in_ref - lcs_parent_in_ref\n",
"~~~\n",
"\n",
"It is important to remember that this operation is in general not commutative since it involves matrix multiplication which is also not commutative. \n",
"During those operations, the local system that should be transformed into another coordinate system is always located to the left of the `+` or `-` operator. \n",
"You can also chain multiple transformations, like this:\n",
"\n",
"~~~ python\n",
"lcs_A_in_C = lcs_A_in_B + lcs_B_in_ref - lcs_C_in_ref\n",
"~~~\n",
"\n",
"Pythons operator associativity ([link](https://www.faceprep.in/python/python-operator-precedence-associativity/)) for the `+` and `-` operator ensures, that all operations are performed from left to right. \n",
"So in the previously shown example, we first calculate an intermediate coordinate system `lcs_A_in_ref` (`lcs_A_in_B + lcs_B_in_ref`) without actually storing it to a variable and subsequently transform it to the reference coordinate system C (`lcs_A_in_ref - lcs_C_in_ref`). \n",
"Keep in mind, that the intermediate results and the coordinate system on the right-hand side of the next operator must either have a child-parent relation (`+` operator) or share a common coordinate system (`-` operator), otherwise the transformation chain produces invalid results.\n",
"\n",
"You can think about both operators in the context of a tree-like graph structure where all dependency chains lead to a common root coordinate system. \n",
"The `+` operator moves a coordinate system 1 level higher and closer to the root. \n",
"Since its way to the root leads over its parent coordinate system, the parent is the only valid system than can be used on the right-hand side of the `+` operator. \n",
"The `-` operator pushes a coordinate system one level lower and further away from the root. It can only be pushed one level deeper if there is another coordinate system connected to its parent system."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Invert method\n",
"\n",
"The `invert` method calculates how a parent coordinate system is positioned and oriented in its child coordinate system:\n",
"\n",
"~~~ python\n",
"lcs_child_in_parent = lcs_parent_in_child.invert()\n",
"~~~\n",
"\n",
"Here is a short example with visualization:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_child_in_parent = LocalCoordinateSystem.from_euler(\n",
" sequence=\"xy\", angles=[90, 45], degrees=True, coordinates=Q_([2, 3, 0], \"mm\")\n",
")\n",
"\n",
"lcs_parent_in_child = lcs_child_in_parent.invert()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"_, (ax_invert_0, ax_invert_1) = vs.new_3d_figure_and_axes(num_subplots=2, width=1000)\n",
"\n",
"# left plot\n",
"vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_ref, {\"color\": \"r\", \"label\": \"parent\"}),\n",
" (lcs_child_in_parent, {\"color\": \"g\", \"label\": \"child\"}),\n",
" ],\n",
" axes=ax_invert_0,\n",
" limits=(-3, 3),\n",
" title=\"child in parent\",\n",
")\n",
"\n",
"# right plot\n",
"vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_parent_in_child, {\"color\": \"r\", \"label\": \"parent\"}),\n",
" (lcs_ref, {\"color\": \"g\", \"label\": \"child\"}),\n",
" ],\n",
" axes=ax_invert_1,\n",
" limits=(-3, 3),\n",
" title=\"parent in child\",\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Time dependency\n",
"\n",
"The orientation and position of a `LocalCoordinateSystem` towards their reference system might vary in time.\n",
"For example, in a welding process the position of the torch towards the specimen is changing constantly. \n",
"The `LocalCoordinateSystem` provides an interface for such cases. \n",
"All previously shown construction methods also provide the option to pass a `time` parameter.\n",
"\n",
"To create a time-dependent system, you have to provide a list of time values. \n",
"WelDX supports several time formats.\n",
"A list of the supported formats can be found in the [documentation of the generalized `Time` class](https://weldx.readthedocs.io/en/latest/_autosummary/weldx.Time.html#weldx.Time).\n",
"\n",
"If you use the `time` parameter, you also need to provide the extra data for the `orientation` and/or `coordinates` to the construction method. \n",
"One way to do this is by providing an array of coordinate vectors or orientation matrices with the same number of elements as there are time values. \n",
"For example: If you want to create a moving coordinate system with 2 timestamps, you can do it by like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"time = [\"2010-02-01\", \"2010-02-02\"]\n",
"\n",
"coordinates_mov = Q_([[-3, 0, 0], [0, 0, 2]], \"mm\")\n",
"\n",
"lcs_mov_in_ref = LocalCoordinateSystem(coordinates=coordinates_mov, time=time)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the coordinates are now a 2-dimensional array with two coordinate vectors while the orientation is still a single matrix (the default unit matrix) and therefore constant.\n",
"\n",
"A coordinate system with varying orientation between 2 timestamps using the `from_axis_vectors` can be defined very similar:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x_vecs = [[1, 0, 0], [0, -1, 0]]\n",
"y_vecs = [[0, 1, 0], [1, 0, 0]]\n",
"coordinates_rot = Q_([1, 0, 2], \"mm\")\n",
"\n",
"lcs_rot_in_ref = LocalCoordinateSystem.from_axis_vectors(\n",
" x=x_vecs, y=y_vecs, coordinates=coordinates_rot, time=time\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"Here the individual vectors are arrays.\n",
"\n",
"A rotating and moving coordinate system defined with the `from_euler` method is shown in the next code section:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"angles = [[25, 45], [135, 90]]\n",
"coordinates_movrot = Q_([[0, 3, 0], [-2, 3, 2]], \"mm\")\n",
"\n",
"lcs_movrot_in_ref = LocalCoordinateSystem.from_euler(\n",
" sequence=\"xy\",\n",
" angles=angles,\n",
" degrees=True,\n",
" coordinates=coordinates_movrot,\n",
" time=time,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a visualization of the created coordinate systems at the two different times:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# plot coordinate systems\n",
"_, axes_array = vs.new_3d_figure_and_axes(num_subplots=2, width=1000)\n",
"\n",
"for i, ax in enumerate(axes_array):\n",
" vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_ref, {\"color\": \"r\", \"label\": \"reference system\"}),\n",
" (lcs_mov_in_ref, {\"color\": \"g\", \"label\": \"moving system\"}),\n",
" (lcs_rot_in_ref, {\"color\": \"c\", \"label\": \"rotating system\"}),\n",
" (lcs_movrot_in_ref, {\"color\": \"y\", \"label\": \"moving and rotating system\"}),\n",
" ],\n",
" axes=ax,\n",
" limits=(-3, 3),\n",
" title=f\"timestep {i}\",\n",
" time_index=i,\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Time interpolation\n",
"\n",
"It is also possible, to interpolate a coordinate system's orientations and coordinates in time by using the `interp_time` function.\n",
"You have to pass it a single or multiple target times for the interpolation.\n",
"The same time formats that are compatible with the different construction methods can be used here too.\n",
"\n",
"Alternatively, you can pass another `LocalCoordinateSystem` which provides the target timestamps.\n",
"The return value of this function is a new `LocalCoordinateSystem` with interpolated orientations and coordinates.\n",
"In case that a target time for the interpolation lies outside of the `LocalCoordinateSystem`s' time range, the boundary value is broadcasted.\n",
"\n",
"Here is an example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"time = [\"2010-02-02\", \"2010-02-07\"]\n",
"time_interp = [\n",
" \"2010-02-01\",\n",
" \"2010-02-03\",\n",
" \"2010-02-04\",\n",
" \"2010-02-05\",\n",
" \"2010-02-06\",\n",
" \"2010-02-11\",\n",
"]\n",
"\n",
"coordinates_tdp = Q_([[0, 3, 0], [-2, 3, 2]], \"mm\")\n",
"angles_tdp = [0, 90]\n",
"\n",
"lcs_tdp_in_ref = LocalCoordinateSystem.from_euler(\n",
" sequence=\"z\",\n",
" angles=angles_tdp,\n",
" degrees=True,\n",
" coordinates=coordinates_tdp,\n",
" time=time,\n",
")\n",
"\n",
"lcs_interp_in_ref = lcs_tdp_in_ref.interp_time(time_interp)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a visual representation of the original and the interpolated system:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# plot coordinate systems\n",
"_, (ax_0_interp, ax_1_interp) = vs.new_3d_figure_and_axes(num_subplots=2, width=1000)\n",
"\n",
"# original systems\n",
"vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_ref, {\"color\": \"r\", \"label\": \"reference\"}),\n",
" (lcs_tdp_in_ref, {\"color\": \"g\", \"label\": \"2010-02-01\", \"time_index\": 0}),\n",
" (lcs_tdp_in_ref, {\"color\": \"b\", \"label\": \"2010-02-07\", \"time_index\": 1}),\n",
" ],\n",
" axes=ax_0_interp,\n",
" limits=(-3, 3),\n",
" title=\"original system\",\n",
")\n",
"\n",
"\n",
"# interpolated systems\n",
"colors = [[0, 1, 0], [0, 1, 0.5], [0, 1, 1], [0, 0.5, 1], [0, 0, 1], [0.5, 0, 1]]\n",
"plot_data = [\n",
" (lcs_interp_in_ref, {\"color\": colors[i], \"label\": time_interp[i], \"time_index\": i})\n",
" for i in range(len(time_interp))\n",
"]\n",
"plot_data = [(lcs_ref, {\"color\": \"r\", \"label\": \"reference\"}), *plot_data]\n",
"\n",
"vs.plot_coordinate_systems(\n",
" plot_data, axes=ax_1_interp, limits=(-3, 3), title=\"interpolated system\"\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, the time values `\"2010-02-01\"` and `\"2010-02-11\"`, which lie outside the original range from `\"2010-02-02\"` and `\"2010-02-07\"`, still get valid values due to the broadcasting across time range boundaries. \n",
"The intermediate coordinates and orientations are interpolated as expected.\n",
"\n",
"> **Note:** \n",
"Using the `+` and `-` operators with time dependent systems obeys the following rules:\n",
">- If the left-hand side system has a time component, the data of the right-hand side system will be interpolated to the same times \n",
">- In case, that the left-hand side system has no time component, but the right-hand side does, the resulting system has the same time components as the right-hand side system"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mathematical Expressions for time dependent coordinates\n",
"\n",
"Another type that can be used when creating a `LocalCoordinateSystem` is the `TimeSeries`.\n",
"This class represents time-dependent data and can either be created with explicit values or a `MathematicalExpression` ([click to get to API doc](https://weldx.readthedocs.io/en/latest/_autosummary/weldx.MathematicalExpression.html)).\n",
"Therefore, we can describe time dependent coordinate systems also with mathematical expressions (Note that only coordinates support the `TimeSeries` class at the moment).\n",
"\n",
"We will give you just a short example without much explanation here, but if you want to learn how to create a valid `TimeSeries` using mathematical expressions, checkout [the tutorial about this class](timeseries_01.ipynb). \n",
"\n",
"The following systems z-position will change quadratically with time while it moves at a constant speed into the x-direction: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"expression = \"a*t^2 + b*t + c\"\n",
"parameters = dict(\n",
" a=Q_([0, 0, 0.2], \"mm/s^2\"), b=Q_([1, 0, 0], \"mm/s\"), c=Q_([-2, -2, -2], \"mm\")\n",
")\n",
"\n",
"me = MathematicalExpression(expression=expression, parameters=parameters)\n",
"ts = TimeSeries(me)\n",
"\n",
"lcs_expr = LocalCoordinateSystem(coordinates=ts)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# plot coordinate systems\n",
"_, ax_dsx = vs.new_3d_figure_and_axes()\n",
"\n",
"lcs_expr_int = lcs_expr.interp_time(Q_(list(range(5)), \"s\"))\n",
"# first timestep\n",
"vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_ref, {\"color\": \"r\", \"label\": \"reference\"}),\n",
" (lcs_expr_int, {\"color\": \"g\", \"label\": \"expression based coordinates\"}),\n",
" ],\n",
" axes=ax_dsx,\n",
" limits=(-3, 3),\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transformation of spatial data\n",
"\n",
"> **Note** The things covered in this section are not necessary to work with WelDX and can be skipped.\n",
"Data transformations can be done much easier using the `CoordinateSystemManager` class.\n",
"\n",
"The `LocalCoordinateSystem` only defines how the different coordinate systems are oriented towards each other. \n",
"If you want to transform spatial data that is defined in one coordinate system (for example specimen geometry/point cloud) you have to use the `CoordinateSystemManager`, which is discussed in the next tutorial, or do the transformation manually.\n",
"For the manual transformation, you can get all you need from the `LocalCoordinateSystem` using its accessor properties:\n",
"\n",
"~~~\n",
"orientation = lcs_a_in_b.orientation\n",
"coordinates = lcs_a_in_b.coordinates\n",
"~~~\n",
"\n",
"The returned data is an `xarray.DataFrame`. \n",
"In case you are not used to work with this data type, you can get a `numpy.ndarray` by simply using their `data` property:\n",
"\n",
"~~~\n",
"orientation_numpy = lcs_a_in_b.orientation.data\n",
"coordinates_numpy = lcs_a_in_b.coordinates.data\n",
"~~~\n",
"\n",
"Keep in mind, that you actually get an array of matrices (`orientation`) and vectors (`coordinates`) if the corresponding component is time dependent. \n",
"The transformation itself is done by the equation:\n",
"\n",
"$$v_b = O_{ab} \\cdot v_a + c_{ab}$$\n",
"\n",
"where $v_a$ is a data point defined in coordinate system `a`, $O_{ab}$ is the orientation matrix of `a` in `b`, $c_{ab}$ the coordinates of `a` in `b` and $v_b$ the transformed data point.\n",
"\n",
"\n",
"Here is a short example that transforms the points of a square from one coordinate system to another.\n",
"First we create a set of points with the coordinates meant to be defined in the reference system:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = np.array(\n",
" [[-1, 1, 0], [1, 1, 0], [1, -1, 0], [-1, -1, 0], [-1, 1, 0]], dtype=float\n",
")\n",
"\n",
"points_in_ref = Q_(data.transpose(), \"mm\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we create the target system:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_target_in_ref = LocalCoordinateSystem.from_euler(\n",
" sequence=\"zy\", angles=[90, -45], degrees=True, coordinates=Q_([2, -2, 0], \"mm\")\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For the transformation from the reference system to the target system we actually need the orientation and coordinates of the inverted target system:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_ref_in_target = lcs_target_in_ref.invert()\n",
"t_mat = lcs_ref_in_target.orientation.data\n",
"t_vec = lcs_ref_in_target.coordinates.data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we use the formula we discussed earlier:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"points_in_target = np.matmul(t_mat, points_in_ref) + t_vec[:, np.newaxis]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that we needed to broadcast the values of `t_vec` using `[:, np.newaxis]` because we are not working with a single point but an array of points.\n",
"Since we now have the data available in both coordinate systems, we can create the following plots:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# plot coordinate systems\n",
"_, (ax_0_trans, ax_1_trans) = vs.new_3d_figure_and_axes(num_subplots=2, width=1000)\n",
"\n",
"# first plot\n",
"vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_ref, {\"color\": \"r\", \"label\": \"original system\"}),\n",
" (lcs_target_in_ref, {\"color\": \"g\", \"label\": \"target system\"}),\n",
" ],\n",
" axes=ax_0_trans,\n",
" limits=(-3, 3),\n",
" title=\"Data in original system\",\n",
")\n",
"ax_0_trans.plot(points_in_ref[0].m, points_in_ref[1].m, points_in_ref[2].m)\n",
"\n",
"\n",
"# second plot\n",
"vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_ref_in_target, {\"color\": \"r\", \"label\": \"original system\"}),\n",
" (lcs_ref, {\"color\": \"g\", \"label\": \"target system\"}),\n",
" ],\n",
" axes=ax_1_trans,\n",
" limits=(-3, 3),\n",
" title=\"Data in target system\",\n",
")\n",
"ax_1_trans.plot(points_in_target[0].m, points_in_target[1].m, points_in_target[2].m);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Internal xarray structure\n",
"\n",
"The local coordinate system and many other components of the WelDX package use xarray data frames internally. \n",
"So it is also possible to pass `xarray.DataArray`s to a lot of methods. \n",
"However, they need a certain structure which will be described here. \n",
"If you are not familiar with the xarray package, you should first read the [documentation](http://xarray.pydata.org/en/stable/).\n",
"\n",
"To pass a `xarray.DataArray` as coordinates to a `LocalCoordinateSystem`, it must at least have a dimension `c`. \n",
"It represents the location in 3d space of the coordinate system and must always be of length 3. \n",
"Those components must be named coordinates of the data frame (`coords={\"c\": [\"x\", \"y\", \"z\"]}`). \n",
"An optional dimension is `time`. \n",
"It can be of arbitrary length, but the timestamps must be added as coordinates.\n",
"\n",
"The same conventions that are used for the coordinates also apply to the orientations. \n",
"Additionally, they must have another dimension `v` of length 3, which are enumerated (`\"v\": [0, 1, 2]`). \n",
"`c` and `v` are the rows and columns of the orientation matrix.\n",
"\n",
"Here is an example.\n",
"Time dependent coordinates are defined as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"time = pd.TimedeltaIndex([0, 5], \"D\")\n",
"coordinates_q = Q_([[0, 3, 0], [-2, 3, 2]], \"mm\")\n",
"\n",
"coordinates_da = xr.DataArray(\n",
" data=coordinates_q,\n",
" dims=[\"time\", \"c\"],\n",
" coords={\"time\": time, \"c\": [\"x\", \"y\", \"z\"]},\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The definition of time dependent orientations is quite similar:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"orientation_q = [\n",
" [[1, 0, 0], [0, 1, 0], [0, 0, 1]],\n",
" [[0, -1, 0], [1, 0, 0], [0, 0, 1]],\n",
"]\n",
"\n",
"orientation_da = xr.DataArray(\n",
" data=orientation_q,\n",
" dims=[\"time\", \"c\", \"v\"],\n",
" coords={\"time\": time, \"c\": [\"x\", \"y\", \"z\"], \"v\": [0, 1, 2]},\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can create a new `LocalCoordinateSystem`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lcs_xr = LocalCoordinateSystem(orientation=orientation_da, coordinates=coordinates_da)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is the resulting plot:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# plot coordinate systems\n",
"_, ax_dsx = vs.new_3d_figure_and_axes()\n",
"\n",
"# first timestep\n",
"vs.plot_coordinate_systems(\n",
" [\n",
" (lcs_ref, {\"color\": \"r\", \"label\": \"reference\"}),\n",
" (lcs_xr, {\"color\": \"g\", \"label\": str(lcs_xr.time[0]), \"time_index\": 0}),\n",
" (lcs_xr, {\"color\": \"b\", \"label\": str(lcs_xr.time[1]), \"time_index\": 1}),\n",
" ],\n",
" axes=ax_dsx,\n",
" limits=(-3, 3),\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `weldx.utility` package contains two utility functions to create xarray data frames that can be passed as `orientation` and `coordinates` to an `LocalCoordinateSystem`.\n",
"They are named `xr_3d_vector` and `xr_3d_matrix`. \n",
"Here are the links to the corresponding API documentation [for vectors](https://weldx.readthedocs.io/en/latest/_autosummary/weldx.util.xr_3d_vector.html#weldx.util.xr_3d_vector) and [matrices](https://weldx.readthedocs.io/en/latest/_autosummary/weldx.util.xr_3d_matrix.html#weldx.util.xr_3d_matrix)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "",
"language": "python",
"name": ""
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}