This is an example on how to use the utils.h file.
#include "Test.h"
class UtilsTestCase : public Test
{
bool TestDistance() {
b = { -2, 0, 0 };
return true;
}
bool TestNormalize() {
return true;
}
bool TestTranslate() {
AssertEqual(model, b);
return true;
}
bool TestLookAt() {
{ 0.7071068286895752, 0, 0.70710676908493042, 0 },
{ 0, 1, -0, 0 },
{ -0.7071068286895752, 0, 0.70710676908493042, 0 },
{ -0, -0, -1.4142135381698608, 1 });
AssertEqual(res, expected);
return true;
}
bool TestOrtho() {
auto res = Math::Utils::ortho<float>(0, 16, 0, 9);
mat4f expected = { { 0.125, 0, 0, 0 }, { 0, 0.222222222222222, 0, 0 }, { 0, 0, -1, 0 }, { -1, -1, 0, 1 } };
AssertEqual(res, expected);
return true;
}
bool TestPerspective() {
auto res = Math::Utils::perspective<float>(35, 800, 600, 0.1, 100000.0);
mat4f expected = { { -0.16869165003299713, 0, 0, 0 },
{ 0, -0.22492220997810364, 0, 0 },
{ 0, 0, -1.0000020265579224, -1 },
{ 0, 0, -0.20000019669532776, 0 } };
AssertEqual(res, expected);
return true;
}
bool TestAngleAxis() {
AssertEqual(res, expected);
return true;
}
bool TestScale() {
AssertEqual(model, res);
auto expected = model * 2.0f;
AssertEqual(res, expected);
expected[3][3] = 1.0f;
AssertEqual(res, expected);
return true;
}
bool TestRotate() {
mat4f expected({ { 1, 0, 0, 0 },
{ 0, -0.4480736255645752, 0.89399665594100952, 0 },
{ 0, -0.89399665594100952, -0.4480736255645752, 0 },
{ 0, 0, 0, 1 } });
AssertEqual(res, expected);
return true;
}
bool TestMax() {
return true;
}
bool TestLerp() {
AssertEqual(res, a);
AssertEqual(res, b);
AssertEqual(res, { 1.5, 0, 0 });
AssertEqual(res4, a4);
return true;
}
bool TestValuePtr() {
mat2f m({ 1, 2 }, { 3, 4 });
mat3f n({ 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 });
mat4f p({ 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 });
for(int i = 0; i < 2; ++i) { AssertEqual(a2[i], a[i]); }
for(int i = 0; i < 3; ++i) { AssertEqual(b2[i], b[i]); }
for(int i = 0; i < 4; ++i) { AssertEqual(c2[i], c[i]); }
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < 2; ++j) { AssertEqual(m2[i * 2 + j], m[i][j]); }
}
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) { AssertEqual(n2[i * 3 + j], n[i][j]); }
}
for(int i = 0; i < 4; ++i) {
for(int j = 0; j < 4; ++j) { AssertEqual(p2[i * 4 + j], p[i][j]); }
}
return true;
}
bool TestSign() {
AssertEqual(
sign(-1.), -1);
AssertEqual(
sign(1.), 1);
AssertEqual(
sign(0.), 1);
AssertEqual(sign<int>(-1.2), -1);
AssertEqual(sign<float>(1.), 1.);
AssertEqual(sign<int>(0), 1);
return true;
}
bool TestAbs() {
AssertEqual(
abs(1.), 1.);
AssertEqual(
abs(0.), 0.);
AssertEqual(abs<int>(-1.2), 1);
AssertEqual(abs<float>(1.), 1.);
AssertEqual(abs<int>(0), 0);
return true;
}
public:
void run() override {
TestDistance();
TestNormalize();
TestTranslate();
TestLookAt();
TestLerp();
TestOrtho();
TestPerspective();
TestAngleAxis();
TestScale();
TestRotate();
TestMax();
TestSign();
TestValuePtr();
}
};
int main() {
UtilsTestCase().run();
return 0;
}
float distance(const V &a, const V &b)
Definition: utils.h:27
vec2< T > normalize(const vec2< T > &a)
Definition: utils.h:38
mat4< T > rotate(const mat4< T > &m, const float &angle, vec3< T > u)
Definition: utils.h:247
mat4< T > lookAt(const vec3< T > &eye, const vec3< T > ¢er, const vec3< T > &up)
Definition: utils.h:91
mat4< T > translate(const mat4< T > &M, const vec3< T > &V)
Definition: utils.h:72
mat4< T > scale(const mat4< T > &mat, const float &factor)
Definition: utils.h:187
vec3< T > lerp(const vec3< T > &p1, const vec3< T > &p2, const float &v)
Definition: utils.h:295
vec3< T > max(const vec3< T > &a, const vec3< T > &b)
Definition: utils.h:282
mat4< T > angleAxis(const float &angle, const vec3< T > &axis)
Definition: utils.h:174
Matrix< double > eye(size_t rows, size_t columns=0)
vec2< T > normalize() const
Definition: vec2.h:91
vec3< T > normalize() const
Definition: vec3.h:141
vec4< T > normalize() const
Definition: vec4.h:118
T abs(const T &d)
Definition: utils.h:467
T sign(const T &d)
Definition: utils.h:453
void * value_ptr(vec2< T > &vec)
Definition: utils.h:327