mirror of
https://github.com/BlackMATov/vmath.hpp.git
synced 2025-12-16 22:19:51 +07:00
rotate vec4 by qua
This commit is contained in:
@@ -646,13 +646,23 @@ namespace vmath_hpp
|
|||||||
}
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
[[nodiscard]] vec<T, 3> rotate(const vec<T, 3>& v, T angle, const vec<T, 3>& normal) {
|
[[nodiscard]] vec<T, 3> rotate(const vec<T, 3>& v, const qua<T>& q) {
|
||||||
return v * mat<T, 3>(rotate(angle, normal));
|
return v * q;
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
[[nodiscard]] vec<T, 4> rotate(const vec<T, 4>& v, T angle, const vec<T, 3>& normal) {
|
[[nodiscard]] vec<T, 3> rotate(const vec<T, 3>& v, T angle, const vec<T, 3>& axis) {
|
||||||
return v * rotate(angle, normal);
|
return v * qrotate(angle, axis);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
[[nodiscard]] vec<T, 4> rotate(const vec<T, 4>& v, const qua<T>& q) {
|
||||||
|
return v * q;
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
[[nodiscard]] vec<T, 4> rotate(const vec<T, 4>& v, T angle, const vec<T, 3>& axis) {
|
||||||
|
return v * qrotate(angle, axis);
|
||||||
}
|
}
|
||||||
|
|
||||||
// project
|
// project
|
||||||
|
|||||||
@@ -80,6 +80,11 @@ namespace vmath_hpp
|
|||||||
return xs + qv2 * ys.s + cross(ys.v, qv2);
|
return xs + qv2 * ys.s + cross(ys.v, qv2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
[[nodiscard]] constexpr vec<T, 4> operator*(const vec<T, 4>& xs, const qua<T>& ys) {
|
||||||
|
return {vec<T, 3>{xs} * ys, xs.w};
|
||||||
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
[[nodiscard]] constexpr qua<T> operator*(const qua<T>& xs, const qua<T>& ys) {
|
[[nodiscard]] constexpr qua<T> operator*(const qua<T>& xs, const qua<T>& ys) {
|
||||||
return {
|
return {
|
||||||
@@ -99,6 +104,11 @@ namespace vmath_hpp
|
|||||||
return (xs = (xs * ys));
|
return (xs = (xs * ys));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
constexpr vec<T, 4>& operator*=(vec<T, 4>& xs, const qua<T>& ys) {
|
||||||
|
return (xs = (xs * ys));
|
||||||
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
constexpr qua<T>& operator*=(qua<T>& xs, const qua<T>& ys) {
|
constexpr qua<T>& operator*=(qua<T>& xs, const qua<T>& ys) {
|
||||||
return (xs = (xs * ys));
|
return (xs = (xs * ys));
|
||||||
|
|||||||
@@ -285,7 +285,10 @@ TEST_CASE("vmath/ext") {
|
|||||||
REQUIRE(rotate(float2(2.f,0.f), radians(90.f)) == uapprox2(0.f,2.f));
|
REQUIRE(rotate(float2(2.f,0.f), radians(90.f)) == uapprox2(0.f,2.f));
|
||||||
REQUIRE(rotate(float2(1.5f,0.f), radians(-90.f)) == uapprox2(0.f,-1.5f));
|
REQUIRE(rotate(float2(1.5f,0.f), radians(-90.f)) == uapprox2(0.f,-1.5f));
|
||||||
|
|
||||||
|
REQUIRE(rotate(float3(1.5f,0.f,0.f), qrotate_z(radians(90.f))) == uapprox3(0.f,1.5f,0.f));
|
||||||
REQUIRE(rotate(float3(1.5f,0.f,0.f), radians(90.f), float3(0,0,1)) == uapprox3(0.f,1.5f,0.f));
|
REQUIRE(rotate(float3(1.5f,0.f,0.f), radians(90.f), float3(0,0,1)) == uapprox3(0.f,1.5f,0.f));
|
||||||
|
|
||||||
|
REQUIRE(rotate(float4(1.5f,0.f,0.f,1.f), qrotate_z(radians(90.f))) == uapprox4(0.f,1.5f,0.f,1.f));
|
||||||
REQUIRE(rotate(float4(1.5f,0.f,0.f,1.f), radians(90.f), float3(0,0,1)) == uapprox4(0.f,1.5f,0.f,1.f));
|
REQUIRE(rotate(float4(1.5f,0.f,0.f,1.f), radians(90.f), float3(0,0,1)) == uapprox4(0.f,1.5f,0.f,1.f));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,9 +59,17 @@ TEST_CASE("vmath/qua_fun") {
|
|||||||
REQUIRE(v == uapprox3(0.f,1.f,0.f));
|
REQUIRE(v == uapprox3(0.f,1.f,0.f));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
float4 v{1,0,0,2};
|
||||||
|
REQUIRE(&v == &(v *= fqua{0,0,0.7071067812f,0.7071067812f}));
|
||||||
|
REQUIRE(v == uapprox4(0.f,1.f,0.f,2.f));
|
||||||
|
}
|
||||||
|
|
||||||
STATIC_REQUIRE(fqua{} * fqua{} == fqua{});
|
STATIC_REQUIRE(fqua{} * fqua{} == fqua{});
|
||||||
STATIC_REQUIRE(float3{1,2,3} * fqua{} == uapprox3(1.f,2.f,3.f));
|
STATIC_REQUIRE(float3{1,2,3} * fqua{} == uapprox3(1.f,2.f,3.f));
|
||||||
|
STATIC_REQUIRE(float4{1,2,3,4} * fqua{} == uapprox4(1.f,2.f,3.f,4.f));
|
||||||
STATIC_REQUIRE(float3{1,0,0} * fqua{0,0,0.7071067812f,0.7071067812f} == uapprox3(0.f,1.f,0.f));
|
STATIC_REQUIRE(float3{1,0,0} * fqua{0,0,0.7071067812f,0.7071067812f} == uapprox3(0.f,1.f,0.f));
|
||||||
|
STATIC_REQUIRE(float4{1,0,0,2} * fqua{0,0,0.7071067812f,0.7071067812f} == uapprox4(0.f,1.f,0.f,2.f));
|
||||||
}
|
}
|
||||||
|
|
||||||
SUBCASE("Common Functions") {
|
SUBCASE("Common Functions") {
|
||||||
|
|||||||
Reference in New Issue
Block a user