Skip to content

Commit

Permalink
update rotate3d_normal
Browse files Browse the repository at this point in the history
  • Loading branch information
YGXXD committed Jun 24, 2024
1 parent 8d578f6 commit 878a2a8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
16 changes: 7 additions & 9 deletions ktm/detail/function/matrix_transform3d.inl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include "../../type/basic.h"

template<typename T>
KTM_NOINLINE std::enable_if_t<std::is_floating_point_v<T>, ktm::mat<4, 4, T>> ktm::detail::matrix_transform3d_implement::
rotate3d_normal(T sin_theta, T cos_theta, const vec<3, T>& normal, const vec<3, T>* normal_start_ptr) noexcept
KTM_NOINLINE std::enable_if_t<std::is_floating_point_v<T>> ktm::detail::matrix_transform3d_implement::
rotate3d_normal(ktm::mat<4, 4, T>& out, T sin_theta, T cos_theta, const vec<3, T>& normal, const vec<3, T>* normal_start_ptr) noexcept
{
T one_minus_cos_theta = one<T> - cos_theta;
T xx_one_minus_cos = normal[0] * normal[0] * one_minus_cos_theta;
Expand All @@ -23,23 +23,21 @@ KTM_NOINLINE std::enable_if_t<std::is_floating_point_v<T>, ktm::mat<4, 4, T>> kt
T yz_one_minus_cos = normal[1] * normal[2] * one_minus_cos_theta;
T zz_one_minus_cos = normal[2] * normal[2] * one_minus_cos_theta;
T x_sin = normal[0] * sin_theta, y_sin = normal[1] * sin_theta, z_sin = normal[2] * sin_theta;
mat<4, 4, T> ret;
ret[0] = { xx_one_minus_cos + cos_theta, xy_one_minus_cos + z_sin, xz_one_minus_cos - y_sin, zero<T> };
ret[1] = { xy_one_minus_cos - z_sin, yy_one_minus_cos + cos_theta, yz_one_minus_cos + x_sin, zero<T> };
ret[2] = { xz_one_minus_cos + y_sin, yz_one_minus_cos - x_sin, zz_one_minus_cos + cos_theta, zero<T> };
out[0] = { xx_one_minus_cos + cos_theta, xy_one_minus_cos + z_sin, xz_one_minus_cos - y_sin, zero<T> };
out[1] = { xy_one_minus_cos - z_sin, yy_one_minus_cos + cos_theta, yz_one_minus_cos + x_sin, zero<T> };
out[2] = { xz_one_minus_cos + y_sin, yz_one_minus_cos - x_sin, zz_one_minus_cos + cos_theta, zero<T> };
if(normal_start_ptr)
{
T a = (*normal_start_ptr)[0], b = (*normal_start_ptr)[1], c = (*normal_start_ptr)[2];
ret[3] = { a * (one_minus_cos_theta - xx_one_minus_cos) + b * (z_sin - xy_one_minus_cos) - c * (y_sin + xz_one_minus_cos),
out[3] = { a * (one_minus_cos_theta - xx_one_minus_cos) + b * (z_sin - xy_one_minus_cos) - c * (y_sin + xz_one_minus_cos),
b * (one_minus_cos_theta - yy_one_minus_cos) + c * (x_sin - yz_one_minus_cos) - a * (z_sin + xy_one_minus_cos),
c * (one_minus_cos_theta - zz_one_minus_cos) + a * (y_sin - xz_one_minus_cos) - b * (x_sin + yz_one_minus_cos),
one<T> };
}
else
{
ret[3] = { zero<T>, zero<T>, zero<T>, one<T> };
out[3] = { zero<T>, zero<T>, zero<T>, one<T> };
}
return ret;
}

#endif
2 changes: 1 addition & 1 deletion ktm/detail/function/matrix_transform3d_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace matrix_transform3d_implement
{

template<typename T>
KTM_NOINLINE std::enable_if_t<std::is_floating_point_v<T>, mat<4, 4, T>> rotate3d_normal(
KTM_NOINLINE std::enable_if_t<std::is_floating_point_v<T>> rotate3d_normal(mat<4, 4, T>& out,
T sin_theta, T cos_theta, const vec<3, T>& normal, const vec<3, T>* normal_start_ptr = nullptr) noexcept;

}
Expand Down
12 changes: 9 additions & 3 deletions ktm/function/matrix_transform3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,27 @@ KTM_INLINE std::enable_if_t<std::is_floating_point_v<T>, mat<4, 4, T>> rotate3d_
template<typename T>
KTM_INLINE std::enable_if_t<std::is_floating_point_v<T>, mat<4, 4, T>> rotate3d_axis(T angle, const vec<3, T>& axis) noexcept
{
return detail::matrix_transform3d_implement::rotate3d_normal(sin(angle), cos(angle), axis);
mat<4, 4, T> ret;
detail::matrix_transform3d_implement::rotate3d_normal(ret, sin(angle), cos(angle), axis);
return ret;
}

template<typename T>
KTM_INLINE std::enable_if_t<std::is_floating_point_v<T>, mat<4, 4, T>> rotate3d_from_to(const vec<3, T>& from, const vec<3, T>& to) noexcept
{
T cos_theta = dot(from, to);
T sin_theta = sqrt(one<T> - cos_theta * cos_theta);
return detail::matrix_transform3d_implement::rotate3d_normal(sin_theta, cos_theta, normalize(cross(from, to)));
mat<4, 4, T> ret;
detail::matrix_transform3d_implement::rotate3d_normal(ret, sin_theta, cos_theta, normalize(cross(from, to)));
return ret;
}

template<typename T>
KTM_INLINE std::enable_if_t<std::is_floating_point_v<T>, mat<4, 4, T>> rotate3d_any_axis(T angle, const vec<3, T>& axis_start, const vec<3, T>& axis) noexcept
{
return detail::matrix_transform3d_implement::rotate3d_normal(sin(angle), cos(angle), axis, &axis_start);
mat<4, 4, T> ret;
detail::matrix_transform3d_implement::rotate3d_normal(ret, sin(angle), cos(angle), axis, &axis_start);
return ret;
}

template<typename T>
Expand Down

0 comments on commit 878a2a8

Please sign in to comment.