Files
davinci2/test/unit/dvt_unit_math_util.cpp

102 lines
3.1 KiB
C++

#include <math/dvc_math_util.h>
#include <math/dvc_matrix4.h>
#include <math/dvc_vector3.h>
#include <math/dvc_vector4.h>
#include <gtest/gtest.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "dvt_unit_common.h"
using namespace davinci;
//-------------------------------------------------------------------------------------
TEST(MathUtil, Basic)
{
//lookat view matrix
{
float dEye[3] = { 0.0f, 4.0f, -10.0f };
float dAt[3] = { 0.0f, 1.0f, 0.0f };
float dUp[3] = { 0.0f, 1.0f, 0.0f };
fMatrix4 m1 = MathUtil::lookatRH(
fVector3(THREE_ELEMENT(dEye)),
fVector3(THREE_ELEMENT(dAt)),
fVector3(THREE_ELEMENT(dUp)));
glm::mat4 gm1 = glm::lookAtRH(glm::vec3(THREE_ELEMENT(dEye)), glm::vec3(THREE_ELEMENT(dAt)), glm::vec3(THREE_ELEMENT(dUp)));
EXPECT_EQ_4X4_T(m1, gm1);
}
//lookat view matrix(random)
{
for (int i = 0; i < 10; i++) {
fVector3 dEye, dAt, dUp;
do {
dEye[0] = _randomFloat(); dEye[1] = _randomFloat(); dEye[2] = _randomFloat();
dAt[0] = _randomFloat(); dAt[1] = _randomFloat(); dAt[2] = _randomFloat();
dUp[0] = _randomFloat(); dUp[1] = _randomFloat(); dUp[2] = _randomFloat();
} while ((dEye == dAt) || (dUp == fVector3::ZERO));
fMatrix4 m1 = MathUtil::lookatRH(
fVector3(THREE_ELEMENT(dEye)),
fVector3(THREE_ELEMENT(dAt)),
fVector3(THREE_ELEMENT(dUp)));
glm::mat4 gm1 = glm::lookAtRH(glm::vec3(THREE_ELEMENT(dEye)), glm::vec3(THREE_ELEMENT(dAt)), glm::vec3(THREE_ELEMENT(dUp)));
EXPECT_EQ_4X4_T_APPROX(m1, gm1, std::numeric_limits<float>::epsilon() * 1000);
}
}
//perspective
{
float width = 640.f, height = 480.f;
fMatrix4 m1 = MathUtil::perspectiveFovRH(MathUtil::PI_DIV4, width / height, 0.01f, 100.f);
glm::mat4 gm1 = glm::perspectiveRH(MathUtil::PI_DIV4, width / height, 0.01f, 100.f);
EXPECT_EQ_4X4_T_APPROX(m1, gm1, std::numeric_limits<float>::epsilon());
}
//perspective(random)
{
for (int i = 0; i < 10; i++) {
float fov = MathUtil::rangeRandom(0.1f, MathUtil::PI);
float aspect = MathUtil::rangeRandom(0.1f, 100.f);
float zNear = MathUtil::rangeRandom(0.00001f, 0.01f);
float zFar = MathUtil::rangeRandom(100.f, 10000.f);
fMatrix4 m1 = MathUtil::perspectiveFovRH(fov, aspect, zNear, zFar);
glm::mat4 gm1 = glm::perspectiveRH(fov, aspect, zNear, zFar);
EXPECT_EQ_4X4_T_APPROX(m1, gm1, std::numeric_limits<float>::epsilon() * 100);
}
}
//couting bit set
{
EXPECT_EQ(MathUtil::popcount((uint8_t)(0x0)), 0);
EXPECT_EQ(MathUtil::popcount((uint8_t)(0xFF)), 8);
EXPECT_EQ(MathUtil::popcount((uint16_t)(0x0)), 0);
EXPECT_EQ(MathUtil::popcount((uint16_t)(0xFFFF)), 16);
EXPECT_EQ(MathUtil::popcount((uint32_t)(0x0)), 0);
EXPECT_EQ(MathUtil::popcount((uint32_t)(0xFFFFFFFF)), 32);
for(int32_t i=0; i<100; i++)
{
uint8_t v8 = (uint8_t)MathUtil::rangeRandom(0, 0xFF);
EXPECT_EQ(MathUtil::popcount(v8), __popcnt(v8));
uint16_t v16 = (uint16_t)MathUtil::rangeRandom(0, 0xFFFF);
EXPECT_EQ(MathUtil::popcount(v16), __popcnt(v16));
uint32_t v32 = (uint32_t)MathUtil::rangeRandom(0, 0x7FFFFF);
EXPECT_EQ(MathUtil::popcount(v32), __popcnt(v32));
}
}
}