Skip to content

Commit

Permalink
fix and test transform_vector_cartesian_to_cylinder
Browse files Browse the repository at this point in the history
  • Loading branch information
christophlohrmann committed Jan 15, 2021
1 parent edf4d1d commit 6ce76ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/utils/include/utils/math/coordinate_transformation.hpp
Expand Up @@ -77,15 +77,14 @@ inline Vector3d transform_vector_cartesian_to_cylinder(Vector3d const &vec,
std::tie(theta, rotation_axis) = rotation_params(axis, z_axis);
auto const rotated_pos = vec_rotate(rotation_axis, theta, pos);
auto const rotated_vec = vec_rotate(rotation_axis, theta, vec);
auto const r = std::sqrt(rotated_pos[0] * rotated_pos[0] +
rotated_pos[1] * rotated_pos[1]);
// v_r = (x * v_x + y * v_y) / sqrt(x^2 + y^2)
auto const v_r =
(rotated_pos[0] * rotated_vec[0] + rotated_pos[1] * rotated_vec[1]) /
std::sqrt(rotated_pos[0] * rotated_pos[0] +
rotated_pos[1] * rotated_pos[1]);
// v_phi = (x * v_y - y * v_x ) / (x^2 + y^2)
(rotated_pos[0] * rotated_vec[0] + rotated_pos[1] * rotated_vec[1]) / r;
// v_phi = (x * v_y - y * v_x ) / sqrt(x^2 + y^2)
auto const v_phi =
(rotated_pos[0] * rotated_vec[1] - rotated_pos[1] * rotated_vec[0]) /
(rotated_pos[0] * rotated_pos[0] + rotated_pos[1] * rotated_pos[1]);
(rotated_pos[0] * rotated_vec[1] - rotated_pos[1] * rotated_vec[0]) / r;
return Vector3d{v_r, v_phi, rotated_vec[2]};
}

Expand Down
18 changes: 18 additions & 0 deletions src/utils/tests/coordinate_transformation.cpp
Expand Up @@ -85,3 +85,21 @@ BOOST_AUTO_TEST_CASE(cylinder_to_cartesian_test) {
BOOST_CHECK(transformed_z[i] == expected_z[i]);
}
}

BOOST_AUTO_TEST_CASE(vector_cart_to_cyl_test) {
constexpr auto eps = 1e-13;
Vector3d const pos{{1.1, 2.2, 3.3}};
auto const axis = (Vector3d{{4.4, 5.5, 6.6}}).normalized();
Vector3d const vec{{7.7, 8.8, 9.9}};

auto const vec_cyl = transform_vector_cartesian_to_cylinder(vec, axis, pos);

// cylindrical basis vectors at pos
auto const e_z = axis;
auto const e_r = (pos - (pos * axis) * axis).normalized();
auto const e_phi = Utils::vector_product(e_z, e_r);

BOOST_CHECK_SMALL(vec_cyl[0] - vec * e_r, eps);
BOOST_CHECK_SMALL(vec_cyl[1] - vec * e_phi, eps);
BOOST_CHECK_SMALL(vec_cyl[2] - vec * e_z, eps);
}

0 comments on commit 6ce76ae

Please sign in to comment.