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) );
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));
70 dump_align_props<int>(std::cout,
"int");
71 dump_align_props<float>(std::cout,
"float");
72 dump_align_props<Vec2i>(std::cout,
"Vec2i");
73 dump_align_props<Vec2f>(std::cout,
"Vec2f");
74 dump_align_props<Vec3f>(std::cout,
"Vec3f");
75 dump_align_props<Vec4f>(std::cout,
"Vec4f");
76 dump_align_props<Mat4f>(std::cout,
"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() );
95 test_vec<Vector2I<int>,
int>(std::cout,
"Vector2I<int>");
96 test_vec<Vector2I<long>,
long>(std::cout,
"Vector2I<long>");
98 test_vec<Vector2F<float>,
float>(std::cout,
"Vector2F<float>");
99 test_vec<Vector2F<double>,
double>(std::cout,
"Vector2F<double>");
101 test_vec<Vector3F<float>,
float>(std::cout,
"Vector3F<float>");
102 test_vec<Vector3F<double>,
double>(std::cout,
"Vector3F<double>");
104 test_vec<Vector4F<float>,
float>(std::cout,
"Vector4F<float>");
105 test_vec<Vector4F<double>,
double>(std::cout,
"Vector4F<double>");
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));
Basic 4x4 value_type matrix implementation using fields for intensive use-cases (host operations).
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
4D vector using four value_type components.
std::string to_string(const alphabet &v) noexcept
std::enable_if< std::is_floating_point_v< T >, bool >::type 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.
2D vector using two integer components.
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)