Skip to content

Commit

Permalink
correct array layout during normalization and interpolation (#25)
Browse files Browse the repository at this point in the history
* correct array layout during normalization and interpolation
* remove duplicate checks in check_coordinate_system
* Add test case for normalization during LCS.__init__

Co-authored-by: Hirthammer <volker.hirthammer@bam.de>
  • Loading branch information
CagtayFabry and vhirtham authored Apr 28, 2020
1 parent 96be7fa commit 329f8e3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
36 changes: 32 additions & 4 deletions tests/test_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest
import random
import math
from copy import deepcopy
from typing import Union, List, Any


Expand Down Expand Up @@ -444,12 +445,8 @@ def check_coordinate_system(
check_coordinate_system_orientation(
cs_p.orientation, orientation_expected, positive_orientation_expected
)
check_coordinate_system_orientation(
cs_p.orientation, orientation_expected, positive_orientation_expected
)

assert np.allclose(cs_p.coordinates.values, coordinates_expected, atol=1e-9)
assert np.allclose(cs_p.coordinates.values, coordinates_expected, atol=1e-9)


def test_coordinate_system_init():
Expand Down Expand Up @@ -554,6 +551,34 @@ def test_coordinate_system_init():
)
check_coordinate_system(lcs, orientation_exp, coordinates_exp, True, time_exp)

# matrix normalization ----------------------

# no time dependency
orientation_exp = tf.rotation_matrix_z(np.pi / 3)
orientation_fix_2 = deepcopy(orientation_exp)
orientation_fix_2[:, 0] *= 10
orientation_fix_2[:, 1] *= 3
orientation_fix_2[:, 2] *= 4

lcs = tf.LocalCoordinateSystem(
orientation=orientation_fix_2, coordinates=coordinates_fix
)

check_coordinate_system(lcs, orientation_exp, coordinates_fix, True)

# time dependent
orientation_exp = tf.rotation_matrix_z(np.pi / 3 * ut.to_float_array([1, 2, 4]))
orientation_tdp_2 = deepcopy(orientation_exp)
orientation_tdp_2[:, :, 0] *= 10
orientation_tdp_2[:, :, 1] *= 3
orientation_tdp_2[:, :, 2] *= 4

lcs = tf.LocalCoordinateSystem(
orientation=orientation_tdp_2, coordinates=coordinates_fix, time=time_0
)

check_coordinate_system(lcs, orientation_exp, coordinates_fix, True, time_0)

# exceptions --------------------------------
# invalid inputs
with pytest.raises(Exception):
Expand All @@ -573,6 +598,9 @@ def test_coordinate_system_init():
# TODO: implement


test_coordinate_system_init()


def test_coordinate_system_factories():
"""
Test construction of coordinate system class.
Expand Down
20 changes: 9 additions & 11 deletions weldx/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,17 @@ def scale_matrix(scale_x, scale_y, scale_z):
return np.diag([scale_x, scale_y, scale_z]).astype(float)


def normalize(vec):
def normalize(a):
"""
Normalize a vector.
Normalize (l2 norm) an ndarray along the last dimension.
:param vec: Vector
:return: Normalized vector
:param a: data in ndarray
:return: Normalized ndarray
"""
norm = np.linalg.norm(vec, axis=(-1))
norm = np.linalg.norm(a, axis=(-1), keepdims=True)
if not np.all(norm):
raise ValueError("Vector length is 0.")
if vec.ndim > 1:
return vec / norm[..., np.newaxis]
return vec / norm
raise ValueError("Length 0 encountered during normalization.")
return a / norm


def orientation_point_plane_containing_origin(point, p_a, p_b):
Expand Down Expand Up @@ -277,8 +275,8 @@ def __init__(
orientation = xr.apply_ufunc(
normalize,
orientation,
input_core_dims=[["c", "v"]],
output_core_dims=[["c", "v"]],
input_core_dims=[["c"]],
output_core_dims=[["c"]],
)

# unify time axis
Expand Down
2 changes: 1 addition & 1 deletion weldx/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def xr_interp_orientation_in_time(
]

# interpolate rotations in the intersecting time range
rotations_key = Rot.from_matrix(dsx.data)
rotations_key = Rot.from_matrix(dsx.transpose(..., "c", "v").data)
times_key = dsx.time.astype(np.int64)
rotations_interp = Slerp(times_key, rotations_key)(
times_intersect.astype(np.int64)
Expand Down

0 comments on commit 329f8e3

Please sign in to comment.