MRPT  2.0.4
TRenderMatrices_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, 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>
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 
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 = glm::perspective(
45  mrpt::DEG2RAD(fovy_deg), view_w / float(view_h), zmin, zmax);
46  std::cout << glm::to_string(Projection) << "\n";
47 #endif
48 
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 
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 
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 }
std::string to_string(T v)
Just like std::to_string(), but with an overloaded version for std::string arguments.
Definition: format.h:36
TEST(OpenGL, perspectiveMatrix)
void computeProjectionMatrix(float zmin, float zmax)
Uses is_projective , vw,vh, etc.
constexpr double DEG2RAD(const double x)
Degrees to radians.
bool fromMatlabStringFormat(const std::string &s, mrpt::optional_ref< std::ostream > dump_errors_here=std::nullopt)
Reads a matrix from a string in Matlab-like format, for example: "[1 0 2; 0 4 -1]" The string must st...
Rendering state related to the projection and model-view matrices.
void computeOrthoProjectionMatrix(float left, float right, float bottom, float top, float znear, float zfar)
Especial case for custom parameters of Orthographic projection.
size_t viewport_width
In pixels.
double FOV
Vertical FOV in degrees.
mrpt::math::CMatrixFloat44 p_matrix
Projection matrix, computed by renderNormalScene() from all the parameters above. ...
EXPECT_NEAR(out.cam_params.rightCameraPose.x, 0.1194, 0.005)
bool is_projective
true: projective, false: ortho



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020