| 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/CBox.h> |
| 12 |
#include <mrpt/opengl/CFBORender.h> |
| 13 |
#include <mrpt/opengl/CGridPlaneXY.h> |
| 14 |
#include <mrpt/opengl/CMesh.h> |
| 15 |
#include <mrpt/opengl/CMeshFast.h> |
| 16 |
#include <mrpt/opengl/CSphere.h> |
| 17 |
#include <mrpt/opengl/CText3D.h> |
| 18 |
#include <mrpt/opengl/stock_objects.h> |
| 19 |
#include <mrpt/system/filesystem.h> |
| 20 |
// |
| 21 |
#include <Eigen/Dense> |
| 22 |
// |
| 23 |
#include <mrpt/config.h> // for MRPT_HAS_* |
| 24 |
#include <test_mrpt_common.h> |
| 25 |
|
| 26 |
#if MRPT_HAS_OPENCV && MRPT_HAS_OPENGL_GLUT && MRPT_HAS_EGL |
| 27 |
#define RUN_OFFSCREEN_RENDER_TESTS |
| 28 |
#endif |
| 29 |
|
| 30 |
// In MIPS, these tests crash in autobuilders, skip them: |
| 31 |
#if defined(__mips__) || defined(__mips) |
| 32 |
#undef RUN_OFFSCREEN_RENDER_TESTS |
| 33 |
#endif |
| 34 |
// Idem with PowerPC: |
| 35 |
#if defined(__powerpc) || defined(__powerpc__) || defined(__powerpc64__) || \ |
| 36 |
defined(__POWERPC__) || defined(__ppc__) || defined(__PPC__) || defined(_ARCH_PPC) || \ |
| 37 |
defined(__PPC64__) || defined(__ppc64__) || defined(_ARCH_PPC64) |
| 38 |
#undef RUN_OFFSCREEN_RENDER_TESTS |
| 39 |
#endif |
| 40 |
// Idem with 32bit ARM archs: |
| 41 |
#if defined(__arm__) && MRPT_WORD_SIZE == 32 |
| 42 |
#undef RUN_OFFSCREEN_RENDER_TESTS |
| 43 |
#endif |
| 44 |
// Idem with RISCV64 arch: |
| 45 |
#if defined(__riscv) |
| 46 |
#undef RUN_OFFSCREEN_RENDER_TESTS |
| 47 |
#endif |
| 48 |
// Idem with Loongarch arch: |
| 49 |
#if defined(__loongarch__) |
| 50 |
#undef RUN_OFFSCREEN_RENDER_TESTS |
| 51 |
#endif |
| 52 |
|
| 53 |
namespace |
| 54 |
{ |
| 55 |
float imageDiff(const mrpt::img::CImage& im1, const mrpt::img::CImage& im2) |
| 56 |
{ |
| 57 |
mrpt::math::CMatrixFloat r1, g1, b1; |
| 58 |
im1.getAsRGBMatrices(r1, g1, b1); |
| 59 |
|
| 60 |
mrpt::math::CMatrixFloat r2, g2, b2; |
| 61 |
im2.getAsRGBMatrices(r2, g2, b2); |
| 62 |
|
| 63 |
return (r1 - r2).asEigen().array().abs().sum() + (g1 - g2).asEigen().array().abs().sum() + |
| 64 |
(b1 - b2).asEigen().array().abs().sum(); |
| 65 |
} |
| 66 |
|
| 67 |
void test_opengl_CFBORender(const bool useCameraFromIntrinsics) |
| 68 |
{ |
| 69 |
using namespace mrpt; // _deg |
| 70 |
using namespace std::string_literals; // s |
| 71 |
using namespace mrpt::opengl; |
| 72 |
|
| 73 |
const std::string expected_RGB_img_file = |
| 74 |
UNITTEST_BASEDIR() + "/tests/CFBORender_expected_rgb_"s + |
| 75 |
(useCameraFromIntrinsics ? "camInt"s : "camFOV"s) + ".png"s; |
| 76 |
|
| 77 |
const std::string expected_depth_img_file = |
| 78 |
UNITTEST_BASEDIR() + "/tests/CFBORender_expected_depth_"s + |
| 79 |
(useCameraFromIntrinsics ? "camInt"s : "camFOV"s) + ".png"s; |
| 80 |
|
| 81 |
Scene scene; |
| 82 |
|
| 83 |
{ |
| 84 |
auto obj = mrpt::opengl::CGridPlaneXY::Create(-20, 20, -20, 20, 0, 5); |
| 85 |
obj->setColor(0.4f, 0.4f, 0.4f); |
| 86 |
scene.insert(obj); |
| 87 |
} |
| 88 |
{ |
| 89 |
auto obj = |
| 90 |
mrpt::opengl::CBox::Create(mrpt::math::TPoint3D(0, 0, 0), mrpt::math::TPoint3D(.1, .1, .1)); |
| 91 |
obj->setColor(1.0f, 0.f, 0.f); |
| 92 |
obj->setName("x"); |
| 93 |
obj->enableShowName(true); |
| 94 |
obj->setLocation(1.0, 0, 0); |
| 95 |
scene.insert(obj); |
| 96 |
} |
| 97 |
{ |
| 98 |
auto obj = mrpt::opengl::CTexturedPlane::Create(); |
| 99 |
obj->setPlaneCorners(-10, 10, -10, 10); |
| 100 |
obj->setColor_u8(0x00, 0xff, 0xff, 0xff); |
| 101 |
obj->setLocation(0, 0, -14); |
| 102 |
scene.insert(obj); |
| 103 |
} |
| 104 |
{ |
| 105 |
auto obj = mrpt::opengl::CSphere::Create(); |
| 106 |
obj->setColor(0, 0, 1); |
| 107 |
obj->setRadius(1.0f); |
| 108 |
obj->setLocation(0, 1, 0); |
| 109 |
obj->setName("ball_1"); |
| 110 |
scene.insert(obj); |
| 111 |
} |
| 112 |
|
| 113 |
{ |
| 114 |
auto obj = mrpt::opengl::CText3D::Create("Hi there!"); |
| 115 |
obj->setLocation(5.0, 3.0, 1.0); |
| 116 |
scene.insert(obj); |
| 117 |
} |
| 118 |
{ |
| 119 |
auto obj = opengl::stock_objects::CornerXYZ(2.0f); |
| 120 |
obj->setLocation(-3, -2, 0.5); |
| 121 |
scene.insert(obj); |
| 122 |
} |
| 123 |
|
| 124 |
// CMeshFast, CMesh: |
| 125 |
{ |
| 126 |
double off_x = -5.0, STEP_X = 1.0; |
| 127 |
opengl::CMeshFast::Ptr obj1 = opengl::CMeshFast::Create(); |
| 128 |
opengl::CMeshFast::Ptr obj2 = opengl::CMeshFast::Create(); |
| 129 |
opengl::CMesh::Ptr obj3 = opengl::CMesh::Create(); |
| 130 |
opengl::CMesh::Ptr obj4 = opengl::CMesh::Create(); |
| 131 |
|
| 132 |
obj1->setXBounds(-1, 1); |
| 133 |
obj1->setYBounds(-1, 1); |
| 134 |
|
| 135 |
const int W = 128, H = 128; |
| 136 |
|
| 137 |
mrpt::math::CMatrixDynamic<float> Z(H, W); |
| 138 |
|
| 139 |
for (int r = 0; r < H; r++) |
| 140 |
for (int c = 0; c < W; c++) Z(r, c) = sin(0.05 * (c + r) - 0.5) * cos(0.9 - 0.03 * r); |
| 141 |
|
| 142 |
const std::string texture_file = |
| 143 |
mrpt::system::getShareMRPTDir() + "datasets/sample-texture-terrain.jpg"s; |
| 144 |
|
| 145 |
mrpt::img::CImage im; |
| 146 |
|
| 147 |
// obj1: |
| 148 |
obj1->setZ(Z); |
| 149 |
obj1->enableColorFromZ(true); |
| 150 |
obj1->setPointSize(2.0); |
| 151 |
obj1->setLocation(off_x, 0, 0); |
| 152 |
scene.insert(obj1); |
| 153 |
|
| 154 |
// obj 2: |
| 155 |
if (im.loadFromFile(texture_file)) |
| 156 |
{ |
| 157 |
obj2->assignImageAndZ(im, Z); |
| 158 |
obj2->setPointSize(2.0); |
| 159 |
obj2->setLocation(off_x, 3, 0); |
| 160 |
scene.insert(obj2); |
| 161 |
} |
| 162 |
|
| 163 |
off_x += STEP_X; |
| 164 |
|
| 165 |
// obj 3: |
| 166 |
obj3->setZ(Z); |
| 167 |
obj3->enableColorFromZ(true, mrpt::img::cmJET); |
| 168 |
obj3->enableWireFrame(true); |
| 169 |
obj3->setLocation(off_x, 0, 0); |
| 170 |
obj3->cullFaces(mrpt::opengl::TCullFace::BACK); |
| 171 |
scene.insert(obj3); |
| 172 |
|
| 173 |
// obj 4: |
| 174 |
if (im.getWidth() > 1) |
| 175 |
{ |
| 176 |
obj4->assignImageAndZ(im, Z); |
| 177 |
obj4->setLocation(off_x, 3, 0); |
| 178 |
obj4->cullFaces(mrpt::opengl::TCullFace::BACK); |
| 179 |
scene.insert(obj4); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
int width = 500, height = 400; |
| 184 |
const double cameraFOVdeg = 120.0; |
| 185 |
|
| 186 |
::setenv("MRPT_FBORENDER_SHOW_DEVICES", "1", 1); |
| 187 |
|
| 188 |
CFBORender renderer(width, height); |
| 189 |
mrpt::img::CImage frame(width, height, mrpt::img::CH_RGB); |
| 190 |
mrpt::math::CMatrixFloat depth; |
| 191 |
|
| 192 |
scene.getViewport()->setCustomBackgroundColor({0.3f, 0.3f, 0.3f, 1.0f}); |
| 193 |
const float clipMax = 25.0f; |
| 194 |
scene.getViewport()->setViewportClipDistances(0.1, clipMax); |
| 195 |
|
| 196 |
{ |
| 197 |
CCamera& camera = renderer.getCamera(scene); |
| 198 |
|
| 199 |
if (useCameraFromIntrinsics) |
| 200 |
{ |
| 201 |
mrpt::img::TCamera c1; |
| 202 |
c1.ncols = width; |
| 203 |
c1.nrows = height; |
| 204 |
c1.fx(width * 0.5); |
| 205 |
c1.fy(width * 0.5); |
| 206 |
c1.cx(width / 2); |
| 207 |
c1.cy(height / 2); |
| 208 |
|
| 209 |
camera.setProjectiveFromPinhole(c1); |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
camera.setProjectiveFOVdeg(cameraFOVdeg); |
| 214 |
} |
| 215 |
|
| 216 |
// Defined by setPose() instead of orbit values: |
| 217 |
camera.set6DOFMode(true); |
| 218 |
|
| 219 |
// Reference camera pose: |
| 220 |
auto robotPose = |
| 221 |
mrpt::poses::CPose3D(0, 0, 10.0, 0.0_deg /*yaw*/, 90.0_deg /*pitch*/, 0.0_deg /*roll*/); |
| 222 |
|
| 223 |
// Convert to +Z pointing forward camera axes: |
| 224 |
auto camPose = robotPose + mrpt::poses::CPose3D::FromYawPitchRoll( |
| 225 |
90.0_deg /*yaw*/, 0.0_deg /*pitch*/, 90.0_deg /*roll*/); |
| 226 |
|
| 227 |
camera.setPose(camPose); |
| 228 |
} |
| 229 |
|
| 230 |
renderer.render_RGBD(scene, frame, depth); |
| 231 |
|
| 232 |
mrpt::img::CImage gt_frame; |
| 233 |
bool readOk_rgb = gt_frame.loadFromFile(expected_RGB_img_file); |
| 234 |
|
| 235 |
EXPECT_TRUE(readOk_rgb); |
| 236 |
|
| 237 |
const float rgb_diff = imageDiff(gt_frame, frame); |
| 238 |
std::cout << "rgb_diff=" << rgb_diff << "\n"; |
| 239 |
EXPECT_LT(rgb_diff, 5000.0f); |
| 240 |
|
| 241 |
{ |
| 242 |
mrpt::img::CImage imDepth; |
| 243 |
depth *= (1.0f / clipMax); |
| 244 |
imDepth.setFromMatrix(depth, true); |
| 245 |
|
| 246 |
mrpt::img::CImage gt_imDepth; |
| 247 |
bool readOk_depth = gt_imDepth.loadFromFile(expected_depth_img_file); |
| 248 |
|
| 249 |
EXPECT_TRUE(readOk_depth); |
| 250 |
|
| 251 |
const float depth_diff = imageDiff(gt_imDepth, imDepth); |
| 252 |
std::cout << "depth_diff=" << depth_diff << "\n"; |
| 253 |
EXPECT_LT(depth_diff, 3000.0f); |
| 254 |
} |
| 255 |
} |
| 256 |
} // namespace |
| 257 |
|
| 258 |
#if defined(RUN_OFFSCREEN_RENDER_TESTS) |
| 259 |
TEST(OpenGL, CFBORender_camera_intrinsics) |
| 260 |
#else |
| 261 |
TEST(OpenGL, DISABLED_CFBORender_camera_intrinsics) |
| 262 |
#endif |
| 263 |
{ |
| 264 |
try |
| 265 |
{ |
| 266 |
test_opengl_CFBORender(true); |
| 267 |
} |
| 268 |
catch (const std::exception& e) |
| 269 |
{ |
| 270 |
std::cerr << "***** WARNING ****: Ignoring exception in test, likely due to limited rendering " |
| 271 |
"capabilities on this device (?):\n" |
| 272 |
<< e.what() << std::endl; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
#if defined(RUN_OFFSCREEN_RENDER_TESTS) |
| 277 |
TEST(OpenGL, CFBORender_camera_fov) |
| 278 |
#else |
| 279 |
TEST(OpenGL, DISABLED_CFBORender_camera_fov) |
| 280 |
#endif |
| 281 |
{ |
| 282 |
try |
| 283 |
{ |
| 284 |
test_opengl_CFBORender(false); |
| 285 |
} |
| 286 |
catch (const std::exception& e) |
| 287 |
{ |
| 288 |
std::cerr << "***** WARNING ****: Ignoring exception in test, likely due to limited rendering " |
| 289 |
"capabilities on this device (?):\n" |
| 290 |
<< e.what() << std::endl; |
| 291 |
} |
| 292 |
} |