29#include <jau/test/catch2_ext.hpp>
40template<
class T,
class U>
41void test_vec(std::ostream& out,
const char* prefix) {
42 out <<
"Test: " << std::string(prefix) <<
", sizeof(U) = " <<
sizeof(U) << std::endl;
43 T a( U(1) ), b( U(2) );
44 out <<
"- a: " << a <<
", len = " << std::to_string(a.length()) <<
", len(normal(a)) = " << std::to_string(T(a).normalize().length()) << std::endl;
45 out <<
"- b: " << b <<
", len = " << std::to_string(b.length()) <<
", len(normal(b)) = " << std::to_string(T(b).normalize().length()) << std::endl;
47 REQUIRE( T(U(2)) == a * U(2) );
48 REQUIRE( T(U(1)) == b / U(2) );
59 out << std::string(prefix) <<
"{size " << std::to_string(
sizeof(T) ) <<
", alignment " << std::to_string(
alignof(T) )
63TEST_CASE(
"Math Vec Test 00",
"[vec][linear_algebra][math]" ) {
64 static_assert(
alignof(int) ==
alignof(
Vec2i));
65 static_assert(
alignof(float) ==
alignof(
Vec2f));
66 static_assert(
alignof(float) ==
alignof(
Vec3f));
67 static_assert(
alignof(float) ==
alignof(
Vec4f));
68 static_assert(
alignof(float) ==
alignof(
Mat4f));
78 std::cout <<
"A v2 " <<
Vec2f(1, 2) << std::endl;
79 std::cout <<
"A v3 " <<
Vec3f(1, 2, 3) << std::endl;
80 std::cout <<
"A v4 " <<
Vec4f(1, 2, 3, 4) << std::endl;
82 const float mf[] = { 1.0f, 2.0f, 3.0f, 4.0f,
83 5.0f, 6.0f, 7.0f, 8.0f,
84 9.0f, 10.0f, 11.0f, 12.0f,
85 13.0f, 14.0f, 15.0f, 16.0f
87 std::cout <<
"A mat4 " <<
Mat4f(mf) << std::endl;
92 REQUIRE( 0.0f ==
Vec2f().length() );
93 REQUIRE( 0.0f ==
Vec3f().length() );
108TEST_CASE(
"Math Vec Normalize Test 01",
"[vec][linear_algebra][math]" ) {
109 const Vec3f v0(1, 0, 0);
112 REQUIRE( 1 < v1.
length() );
116TEST_CASE(
"Math Vec Angle Test 02",
"[vec][linear_algebra][math]" ) {
119 std::cout <<
"Test 0-deg, UNIT_X vecs" << std::endl;
122 std::cout <<
"v0 " << v0 << std::endl;
123 std::cout <<
"v1 " << v1 << std::endl;
125 const float a0_v0_v1 = v0.
angle(v1);
126 std::cout <<
"a0(v0, v1) = " << a0_v0_v1 <<
" rad, " <<
jau::rad_to_adeg(a0_v0_v1) <<
" deg, via dot, acos" << std::endl;
131 std::cout <<
"Test 0-deg, free vecs" << std::endl;
132 const Vec3f v0(0.14f, 0.07f, 0.0f);
133 const Vec3f v1(0.33f, 0.07f, 0.0f);
134 const Vec3f v0_1 = v1 - v0;
135 std::cout <<
"v0 " << v0 << std::endl;
136 std::cout <<
"v1 " << v1 << std::endl;
137 std::cout <<
"v0_1 " << v0_1 << std::endl;
139 const float a0_x_v0_1 =
Vec3f(1, 0, 0).
angle(v0_1);
140 std::cout <<
"a0(X, v0_1) = " << a0_x_v0_1 <<
" rad, " <<
jau::rad_to_adeg(a0_x_v0_1) <<
" deg, via dot, acos" << std::endl;
145 std::cout <<
"Test 180-deg, free vecs" << std::endl;
146 const Vec3f v0(0.33f, 0.07f, 0.0f);
147 const Vec3f v1(0.14f, 0.07f, 0.0f);
148 const Vec3f v0_1 = v1 - v0;
149 std::cout <<
"v0 " << v0 << std::endl;
150 std::cout <<
"v1 " << v1 << std::endl;
151 std::cout <<
"v0_1 " << v0_1 << std::endl;
153 const float a0_x_v0_1 =
Vec3f(1, 0, 0).
angle(v0_1);
154 std::cout <<
"a0(X, v0_1) = " << a0_x_v0_1 <<
" rad, " <<
jau::rad_to_adeg(a0_x_v0_1) <<
" deg, via dot, acos" << std::endl;
155 REQUIRE(
true ==
jau::equals((
float)M_PI, a0_x_v0_1));
159 std::cout <<
"Test 90-deg, UNIT_X, UNIT_Y vecs" << std::endl;
160 const Vec3f v0(1, 0, 0);
161 const Vec3f v1(0, 1, 0);
162 std::cout <<
"v0 " << v0 << std::endl;
163 std::cout <<
"v1 " << v1 << std::endl;
164 const float a0_v0_v1 = v0.
angle(v1);
165 std::cout <<
"a0(v0, v1) = " << a0_v0_v1 <<
" rad, " <<
jau::rad_to_adeg(a0_v0_v1) <<
" deg, via dot, acos" << std::endl;
166 REQUIRE(
true ==
jau::equals((
float)M_PI_2, a0_v0_v1));
170 std::cout <<
"Test 180-deg, UNIT_X, UNIT_X_NEG vecs" << std::endl;
171 const Vec3f v0(1, 0, 0);
172 const Vec3f v1(-1, 0, 0);
173 std::cout <<
"v0 " << v0 << std::endl;
174 std::cout <<
"v1 " << v1 << std::endl;
175 const float a0_v0_v1 = v0.
angle(v1);
176 std::cout <<
"a0(v0, v1) = " << a0_v0_v1 <<
" rad, " <<
jau::rad_to_adeg(a0_v0_v1) <<
" deg, via dot, acos" << std::endl;
177 REQUIRE(
true ==
jau::equals((
float)M_PI, a0_v0_v1));
constexpr_cxx26 value_type angle(const Vector3F &o) const noexcept
Return the angle between to vectors in radians.
constexpr Vector3F & normalize() noexcept
Normalize this vector in place.
constexpr value_type length() const noexcept
Return the length of a vector, a.k.a the norm or magnitude
std::enable_if_t< std::is_floating_point_v< T >, bool > constexpr equals(const T &a, const T &b, const T &epsilon=std::numeric_limits< T >::epsilon()) noexcept
Returns true if both values are equal, i.e.
constexpr T rad_to_adeg(const T rad) noexcept
Converts radians to arc-degree.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
TEST_CASE("Math Vec Test 00", "[vec][linear_algebra][math]")
void dump_align_props(std::ostream &out, const char *prefix)
void test_vec(std::ostream &out, const char *prefix)