Debian Codegraph @

sdk/viewers/transformationexplorer/testing/TestTransformationExplorer.h

raw
1 /*****************************************************************************
2 * $CAMITK_LICENCE_BEGIN$
3 *
4 * CamiTK - Computer Assisted Medical Intervention ToolKit
5 * (c) 2001-2025 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
6 *
7 * Visit http://camitk.imag.fr for more information
8 *
9 * This file is part of CamiTK.
10 *
11 * CamiTK is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * CamiTK is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
22 *
23 * $CAMITK_LICENCE_END$
24 ****************************************************************************/
25
26 #ifndef CAMITK_TESTTRANSFORMATIONEXPLORER_H
27 #define CAMITK_TESTTRANSFORMATIONEXPLORER_H
28
29 #include <QTest>
30
31 #include <ranges>
32
33 #include "FrameOfReference.h"
34 #include "Transformation.h"
35 #include "TransformationManager.h"
36 #include "MeshComponent.h"
37 #include "Application.h"
38 #include "ExtensionManager.h"
39 #include "TransformationExplorer.h"
40 #include "Log.h"
41
42 #include <vtkPolyData.h>
43 #include <vtkSphereSource.h>
44 #include <vtkMatrix4x4.h>
45 #include <vtkTransform.h>
46
47
48 using namespace camitk;
49
50 class TestTransformationExplorer: public QObject {
51 Q_OBJECT
52
53 TransformationExplorer* explorer;
54
55 /**
56 * Create two frames and a Transformation linking them for testing
57 */
58 auto createFramesAndTransformation(QString frame1, QString frame2) {
59 auto fr1 = TransformationManager::addFrameOfReference(frame1);
60 auto fr2 = TransformationManager::addFrameOfReference(frame2);
61
62 auto tr = TransformationManager::addTransformation(fr1, fr2);
63 return std::make_tuple(fr1, fr2, tr);
64 }
65
66 /**
67 * Create a basic MeshComponent for tests
68 */
69 Component* createSphere(float radius = 1.0) {
70 auto sphereSource = vtkSmartPointer<vtkSphereSource>::New();
71 sphereSource->SetRadius(radius);
72 vtkSmartPointer<vtkPointSet> sphere(sphereSource->GetOutput());
73 return new MeshComponent(sphere, "Sphere");
74 }
75
76 private slots:
77
78 /// Before all tests, create a TransformationManager
79 void initTestCase() {
80 // show all log messages in the console
81 Log::getLogger()->setLogLevel(InterfaceLogger::TRACE);
82 // no log messages will generate a QMessageBox
83 Log::getLogger()->setMessageBoxLevel(InterfaceLogger::NONE);
84 // no time stamp for reproducible log diff
85 Log::getLogger()->setTimeStampInformation(false);
86
87 // Make sure we load the TransformationExplorer viewer
88 // Load all the viewer extensions in order to use the TransformationExplorer factory (Application::getNewViewer)
89 ExtensionManager::autoload(ExtensionManager::VIEWER);
90 // Create a new custom Geometry Viewer
91 explorer = dynamic_cast<TransformationExplorer*>(Application::getNewViewer("Explorer test", "TransformationExplorer"));
92 QVERIFY(explorer != nullptr);
93
94 // Initialize the widget and show the widget in order to build the widget (and its frame)
95 // and force the new viewer frame to be truly visible so that calling setFrame(..)
96 // (that itself calls refresh(..)) really updates of the actor transformation, the aim of this test
97 explorer->getWidget();
98
99 // Register the viewer so that it can be used in comp->setVisibility(...)
100 // and be automatically called during Application::refresh()
101 Application::registerViewer(explorer);
102 }
103
104 /// Run after testing (failed or not)
105 void cleanupTestCase() {
106 }
107
108 /// Cleanup after each test
109 void cleanup() {
110
111 }
112
113 /// Test FrameOfReference constructors
114 void playWithFramesTransformationsAndComponents() {
115 explorer->refresh();
116 auto [fr1, fr2, tr] = createFramesAndTransformation("Frame1", "Frame2");
117 explorer->refresh();
118 Component* c1 = createSphere();
119 QVERIFY(c1 != nullptr);
120 explorer->refresh();
121 delete c1;
122 explorer->refresh();
123 fr1 = nullptr;
124 fr2 = nullptr;
125 tr = nullptr;
126 TransformationManager::cleanupFramesAndTransformations();
127 explorer->refresh();
128 QCOMPARE(TransformationManager::getFramesOfReference().size(), 1);
129 }
130
131 };
132
133 // } // namespace camitk
134
135 #endif // CAMITK_TESTTRANSFORMATIONEXPLORER_H