glm/test/gtx/gtx_matrix_interpolation.cpp

118 lines
3.8 KiB
C++
Raw Normal View History

2016-11-12 13:19:22 +00:00
#define GLM_ENABLE_EXPERIMENTAL
2017-06-24 10:43:00 +00:00
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/component_wise.hpp>
2012-09-19 13:34:23 +00:00
#include <glm/gtx/matrix_interpolation.hpp>
2017-06-24 10:43:00 +00:00
#include <iostream>
#include <limits>
2017-06-24 10:43:00 +00:00
2018-09-27 19:22:07 +00:00
static int test_axisAngle()
2017-06-24 10:43:00 +00:00
{
int Error = 0;
glm::mat4 m1(-0.9946f, 0.0f, -0.104531f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.104531f, 0.0f, -0.9946f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
glm::mat4 m2(-0.992624f, 0.0f, -0.121874f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.121874f, 0.0f, -0.992624f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
glm::mat4 const m1rot = glm::extractMatrixRotation(m1);
glm::mat4 const dltRotation = m2 * glm::transpose(m1rot);
glm::vec3 dltAxis(0.0f);
float dltAngle = 0.0f;
glm::axisAngle(dltRotation, dltAxis, dltAngle);
2017-12-19 13:47:02 +00:00
std::cout << "dltAxis: (" << dltAxis.x << ", " << dltAxis.y << ", " << dltAxis.z << "), dltAngle: " << dltAngle << std::endl;
2017-06-24 10:43:00 +00:00
2018-08-07 11:55:13 +00:00
glm::quat q = glm::quat_cast(dltRotation);
2017-06-24 10:43:00 +00:00
std::cout << "q: (" << q.x << ", " << q.y << ", " << q.z << ", " << q.w << ")" << std::endl;
float yaw = glm::yaw(q);
std::cout << "Yaw: " << yaw << std::endl;
return Error;
}
template <class T>
int testForAxisAngle(glm::tvec3<T> const axisTrue, T const angleTrue)
{
T const eps = sqrt(std::numeric_limits<T>::epsilon());
glm::tmat4x4<T> const matTrue = glm::axisAngleMatrix(axisTrue, angleTrue);
glm::tvec3<T> axis;
T angle;
glm::axisAngle(matTrue, axis, angle);
glm::tmat4x4<T> const matRebuilt = glm::axisAngleMatrix(axis, angle);
glm::tmat4x4<T> const errMat = matTrue - matRebuilt;
T const maxErr = glm::compMax(glm::tvec4<T>(
glm::compMax(glm::abs(errMat[0])),
glm::compMax(glm::abs(errMat[1])),
glm::compMax(glm::abs(errMat[2])),
glm::compMax(glm::abs(errMat[3]))
));
return maxErr < eps ? 0 : 1;
}
static int test_axisAngle2()
{
int Error = 0;
Error += testForAxisAngle(glm::vec3(0.0f, 1.0f, 0.0f), 0.0f);
Error += testForAxisAngle(glm::vec3(0.358f, 0.0716f, 0.9309f), 0.00001f);
Error += testForAxisAngle(glm::vec3(1.0f, 0.0f, 0.0f), 0.0001f);
Error += testForAxisAngle(glm::vec3(0.0f, 0.0f, 1.0f), 0.001f);
Error += testForAxisAngle(glm::vec3(0.0f, 0.0f, 1.0f), 0.001f);
Error += testForAxisAngle(glm::vec3(0.0f, 1.0f, 0.0f), 0.005f);
Error += testForAxisAngle(glm::vec3(0.0f, 0.0f, 1.0f), 0.005f);
Error += testForAxisAngle(glm::vec3(0.358f, 0.0716f, 0.9309f), 0.03f);
Error += testForAxisAngle(glm::vec3(0.358f, 0.0716f, 0.9309f), 0.0003f);
Error += testForAxisAngle(glm::vec3(0.0f, 0.0f, 1.0f), 0.01f);
Error += testForAxisAngle(glm::dvec3(0.0f, 1.0f, 0.0f), 0.00005);
Error += testForAxisAngle(glm::dvec3(-1.0f, 0.0f, 0.0f), 0.000001);
Error += testForAxisAngle(glm::dvec3(0.7071f, 0.7071f, 0.0f), 0.5);
Error += testForAxisAngle(glm::dvec3(0.7071f, 0.0f, 0.7071f), 0.0002);
Error += testForAxisAngle(glm::dvec3(0.7071f, 0.0f, 0.7071f), 0.00002);
Error += testForAxisAngle(glm::dvec3(0.7071f, 0.0f, 0.7071f), 0.000002);
Error += testForAxisAngle(glm::dvec3(0.7071f, 0.0f, 0.7071f), 0.0000002);
Error += testForAxisAngle(glm::vec3(0.0f, 0.7071f, 0.7071f), 1.3f);
Error += testForAxisAngle(glm::vec3(0.0f, 0.7071f, 0.7071f), 6.3f);
Error += testForAxisAngle(glm::vec3(1.0f, 0.0f, 0.0f), -0.23456f);
Error += testForAxisAngle(glm::vec3(1.0f, 0.0f, 0.0f), glm::pi<float>());
Error += testForAxisAngle(glm::vec3(0.0f, 1.0f, 0.0f), -glm::pi<float>());
Error += testForAxisAngle(glm::vec3(0.358f, 0.0716f, 0.9309f), -glm::pi<float>());
return Error;
}
2018-09-27 19:22:07 +00:00
static int test_rotate()
{
glm::mat4 m2(1.0);
float myAngle = 1.0f;
m2 = glm::rotate(m2, myAngle, glm::vec3(1.0f, 0.0f, 0.0f));
glm::vec3 m2Axis;
float m2Angle;
glm::axisAngle(m2, m2Axis, m2Angle);
return 0;
}
2012-09-19 13:34:23 +00:00
int main()
{
2017-06-24 10:43:00 +00:00
int Error = 0;
2017-06-24 10:43:00 +00:00
Error += test_axisAngle();
Error += test_axisAngle2();
2018-09-27 19:22:07 +00:00
Error += test_rotate();
2012-09-19 13:34:23 +00:00
return Error;
}