Debian Codegraph @

libs/opengl/src/serializations_unittest.cpp

raw
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 #define MRPT_NO_WARN_BIG_HDR
11 #include <gtest/gtest.h>
12 #include <mrpt/io/CMemoryStream.h>
13 #include <mrpt/opengl.h>
14 #include <mrpt/serialization/CArchive.h>
15 #include <mrpt/system/filesystem.h>
16 #include <test_mrpt_common.h>
17 //
18 #include <mrpt/config.h>
19 #ifndef MRPT_BUILT_AS_DLL
20 #include <mrpt/opengl/registerAllClasses.h>
21 #endif
22
23 using namespace mrpt;
24 using namespace mrpt::opengl;
25 using namespace mrpt::serialization;
26 using namespace std;
27
28 // Create a set of classes, then serialize and deserialize to test possible
29 // bugs:
30 TEST(SerializeTestOpenGL, WriteReadToMem)
31 {
32 #ifndef MRPT_BUILT_AS_DLL
33 mrpt::opengl::registerAllClasses_mrpt_opengl();
34 #endif
35
36 const mrpt::rtti::TRuntimeClassId* lstClasses[] = {
37 CLASS_ID(CAxis),
38 CLASS_ID(CBox),
39 CLASS_ID(CFrustum),
40 CLASS_ID(CDisk),
41 CLASS_ID(CGridPlaneXY),
42 #if MRPT_HAS_OPENCV // These classes need CImage serialization
43 CLASS_ID(CMesh),
44 CLASS_ID(CTexturedPlane),
45 CLASS_ID(CSkyBox),
46 #endif
47 CLASS_ID(Viewport),
48 CLASS_ID(CPointCloud),
49 CLASS_ID(CPointCloudColoured),
50 CLASS_ID(CSetOfLines),
51 CLASS_ID(CSetOfTriangles),
52 CLASS_ID(CSphere),
53 CLASS_ID(CCylinder),
54 CLASS_ID(CPolyhedron),
55 CLASS_ID(CArrow),
56 CLASS_ID(CCamera),
57 CLASS_ID(CEllipsoid3D),
58 CLASS_ID(CGridPlaneXZ),
59 CLASS_ID(Scene),
60 CLASS_ID(CSetOfObjects),
61 CLASS_ID(CSimpleLine),
62 CLASS_ID(CText),
63 CLASS_ID(CText3D),
64 CLASS_ID(CEllipsoidInverseDepth2D),
65 CLASS_ID(CEllipsoidInverseDepth3D),
66 CLASS_ID(CEllipsoidRangeBearing2D),
67 CLASS_ID(COctoMapVoxels)
68 };
69
70 for (auto& cl : lstClasses)
71 {
72 try
73 {
74 mrpt::io::CMemoryStream buf;
75 {
76 auto o = mrpt::ptr_cast<CSerializable>::from(cl->createObject());
77 mrpt::serialization::archiveFrom(buf) << *o;
78 o.reset();
79 }
80 CSerializable::Ptr recons;
81 buf.Seek(0);
82 mrpt::serialization::archiveFrom(buf) >> recons;
83 recons.reset();
84 }
85 catch (const std::exception& e)
86 {
87 GTEST_FAIL() << "Exception during serialization test for class '" << cl->className << "':\n"
88 << e.what() << endl;
89 }
90 }
91 }
92
93 TEST(SerializeTestOpenGL, PredefinedSceneFile)
94 {
95 using namespace std::string_literals;
96
97 //! JS_PRELOAD_FILE <tests/default-scene.3Dscene>
98 const std::string fil = mrpt::UNITTEST_BASEDIR() + "/tests/default-scene.3Dscene"s;
99
100 mrpt::opengl::Scene scene;
101
102 ASSERT_FILE_EXISTS_(fil);
103 bool readOk = scene.loadFromFile(fil);
104 EXPECT_TRUE(readOk);
105
106 // scene.asYAML().printAsYAML();
107
108 EXPECT_EQ(scene.viewportsCount(), 2U);
109 size_t count = 0;
110 for (const auto& obj : *scene.getViewport())
111 {
112 (void)obj;
113 count++;
114 }
115
116 EXPECT_EQ(count, 2U);
117 }