| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include <gtest/gtest.h> |
| 11 |
#include <mrpt/opengl/TRenderMatrices.h> |
| 12 |
|
| 13 |
#include <Eigen/Dense> |
| 14 |
|
| 15 |
//#define USE_GLM_GROUND_TRUTH |
| 16 |
|
| 17 |
#if defined(USE_GLM_GROUND_TRUTH) |
| 18 |
#define GLM_ENABLE_EXPERIMENTAL |
| 19 |
#include <glm/gtc/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale, glm::perspective |
| 20 |
#include <glm/gtc/type_ptr.hpp> // glm::value_ptr |
| 21 |
#include <glm/gtx/string_cast.hpp> |
| 22 |
#include <glm/mat4x4.hpp> // glm::mat4 |
| 23 |
#include <glm/vec3.hpp> // glm::vec3 |
| 24 |
#include <glm/vec4.hpp> // glm::vec4, glm::ivec4 |
| 25 |
#endif |
| 26 |
|
| 27 |
TEST(OpenGL, perspectiveMatrix) |
| 28 |
{ |
| 29 |
float zmin = 0.1f, zmax = 100.0f; |
| 30 |
float fovy_deg = 45.0f; |
| 31 |
size_t view_w = 640, view_h = 480; |
| 32 |
|
| 33 |
mrpt::opengl::TRenderMatrices rm; |
| 34 |
rm.viewport_width = view_w; |
| 35 |
rm.viewport_height = view_h; |
| 36 |
rm.FOV = fovy_deg; |
| 37 |
rm.is_projective = true; |
| 38 |
|
| 39 |
rm.computeProjectionMatrix(zmin, zmax); |
| 40 |
|
| 41 |
// Expected value: |
| 42 |
|
| 43 |
#if defined(USE_GLM_GROUND_TRUTH) |
| 44 |
glm::mat4 Projection = |
| 45 |
glm::perspective(mrpt::DEG2RAD(fovy_deg), view_w / float(view_h), zmin, zmax); |
| 46 |
std::cout << glm::to_string(Projection) << "\n"; |
| 47 |
#endif |
| 48 |
|
| 49 |
mrpt::math::CMatrixFloat44 P_GT; |
| 50 |
P_GT.fromMatlabStringFormat( |
| 51 |
"[1.810660 0.000000 0.000000 0.000000;" |
| 52 |
"0.000000 2.414213 0.000000 0.000000;" |
| 53 |
"0.000000 0.000000 -1.002002 -1.000000;" |
| 54 |
"0.000000 0.000000 -0.200200 0.000000" |
| 55 |
"]"); |
| 56 |
P_GT = P_GT.transpose().eval(); // GT was in column major |
| 57 |
|
| 58 |
EXPECT_NEAR((P_GT - rm.p_matrix).array().abs().maxCoeff(), 0, 0.001f) |
| 59 |
<< "P=\n" |
| 60 |
<< rm.p_matrix << "\nExpected=\n" |
| 61 |
<< P_GT << "\n"; |
| 62 |
} |
| 63 |
|
| 64 |
TEST(OpenGL, orthoMatrix) |
| 65 |
{ |
| 66 |
float left = -4.0f, right = 6.0f; |
| 67 |
float top = 10.0f, bottom = -2.0f; |
| 68 |
float zmin = 0.1f, zmax = 100.0f; |
| 69 |
|
| 70 |
mrpt::opengl::TRenderMatrices rm; |
| 71 |
rm.computeOrthoProjectionMatrix(left, right, bottom, top, zmin, zmax); |
| 72 |
|
| 73 |
// Expected value: |
| 74 |
|
| 75 |
#if defined(USE_GLM_GROUND_TRUTH) |
| 76 |
glm::mat4 Projection = glm::ortho(left, right, bottom, top, zmin, zmax); |
| 77 |
std::cout << glm::to_string(Projection) << "\n"; |
| 78 |
#endif |
| 79 |
|
| 80 |
mrpt::math::CMatrixFloat44 P_GT; |
| 81 |
P_GT.fromMatlabStringFormat( |
| 82 |
"[0.200000 0.000000 0.000000 0.000000;" |
| 83 |
"0.000000 0.166667 0.000000 0.000000;" |
| 84 |
"0.000000 0.000000 -0.020020 0.000000;" |
| 85 |
"-0.200000 -0.666667 -1.002002 1.000000" |
| 86 |
"]"); |
| 87 |
P_GT = P_GT.transpose().eval(); // GT was in column major |
| 88 |
|
| 89 |
EXPECT_NEAR((P_GT - rm.p_matrix).array().abs().maxCoeff(), 0, 0.001f) |
| 90 |
<< "P=\n" |
| 91 |
<< rm.p_matrix << "\nExpected=\n" |
| 92 |
<< P_GT << "\n"; |
| 93 |
} |
| 94 |
|
| 95 |
TEST(OpenGL, perspectiveMatrixFromPinhole) |
| 96 |
{ |
| 97 |
float zmin = 0.1f, zmax = 100.0f; |
| 98 |
|
| 99 |
mrpt::img::TCamera c1; |
| 100 |
c1.ncols = 800; |
| 101 |
c1.nrows = 600; |
| 102 |
c1.fx(350); |
| 103 |
c1.fy(340); |
| 104 |
c1.cx(400); |
| 105 |
c1.cy(300); |
| 106 |
|
| 107 |
mrpt::opengl::TRenderMatrices rm; |
| 108 |
rm.viewport_width = c1.ncols; |
| 109 |
rm.viewport_height = c1.nrows; |
| 110 |
rm.pinhole_model = c1; |
| 111 |
rm.is_projective = true; |
| 112 |
|
| 113 |
rm.computeProjectionMatrix(zmin, zmax); |
| 114 |
|
| 115 |
// Camera axes: +Z forward, +X right, +Y down. |
| 116 |
{ |
| 117 |
// Project point test: |
| 118 |
mrpt::math::CMatrixFixed<float, 4, 1> pt1; |
| 119 |
pt1.fromMatlabStringFormat("[0 ; 0; 10.0; 1.0]"); |
| 120 |
const Eigen::Vector4f pt1h = (rm.p_matrix * pt1); |
| 121 |
const Eigen::Vector3f pt1v = pt1h.head<3>() * (1.0f / pt1h[3]); |
| 122 |
|
| 123 |
EXPECT_NEAR(pt1v[0], 0, 1e-3f); |
| 124 |
EXPECT_NEAR(pt1v[1], 0, 1e-3f); |
| 125 |
} |
| 126 |
|
| 127 |
{ |
| 128 |
// Project point test: |
| 129 |
mrpt::math::CMatrixFixed<float, 4, 1> pt1; |
| 130 |
const float fov_x = 2 * atan(c1.ncols / (2 * c1.fx())); |
| 131 |
const float fov_y = 2 * atan(c1.nrows / (2 * c1.fy())); |
| 132 |
|
| 133 |
const float z = 10.0f; |
| 134 |
pt1[0] = tan(fov_x / 2) * z; |
| 135 |
pt1[1] = tan(fov_y / 2) * z; |
| 136 |
pt1[2] = z; |
| 137 |
pt1[3] = 1.0; |
| 138 |
const Eigen::Vector4f pt1h = (rm.p_matrix * pt1); |
| 139 |
const Eigen::Vector3f pt1v = pt1h.head<3>() * (1.0f / pt1h[3]); |
| 140 |
|
| 141 |
EXPECT_NEAR(pt1v[0], -1, 1e-3f); |
| 142 |
EXPECT_NEAR(pt1v[1], -1, 1e-3f); |
| 143 |
} |
| 144 |
} |