Computer Assisted Medical Intervention Tool Kit version 6.0
 
Loading...
Searching...
No Matches
TestPersistence.h
Go to the documentation of this file.
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_TESTPERSISTENCE_H
27#define CAMITK_TESTPERSISTENCE_H
28
29#include <QTest>
30#include <QDir>
31#include <QTemporaryFile>
32
33#include "Application.h"
34#include "ExtensionManager.h"
35#include "Action.h"
37#include "Transformation.h"
38#include "FrameOfReference.h"
39#include "Component.h"
40
44camitk::Component* createNamedSphere(float radius = 1.0, QString name = "No name");
45
46namespace camitk {
47
48class TestPersistence: public QObject {
49 Q_OBJECT
50
51
52private slots:
53
54 // Before all tests
55 void initTestCase() {
56 }
57
58 // Run after testing (failed or not)
59 void cleanupTestCase() {
60
61 }
62
63
64 void saveLoadEmptyWorkspace() {
65 // Change some settings
66
67 // Save the empty workspace
68
69 // Change the settings
70
71 // Load the workspace
72
73 // Check if the settings are back to their previous values
74 }
75
76 void saveLoadWorkspaceComponents() { // Checks that components are reloaded, that the frames and transformations are identical
77 // Load extensions to be able to read and save msh files
79 // Create two components
80 Component* component1 = createNamedSphere(1.0, "SphereMeshComponent");
81 Component* component2 = createNamedSphere(2.0, "SphereMeshComponent2");
82
83 // Add a Transformation between their frames
84 std::shared_ptr<Transformation> tr = TransformationManager::addTransformation(component1->getFrame(), component2->getFrame());
85 QVERIFY(tr != nullptr);
86 QUuid transformationId = tr->getUuid();
87
88 // Save the components
89 QTemporaryFile file1(QDir::tempPath() + QDir::separator() + "Component-XXXXXX.vtk");
90 QTemporaryFile file2(QDir::tempPath() + QDir::separator() + "Component-XXXXXX.vtk");
91 QTemporaryFile fileWorkspace(QDir::tempPath() + QDir::separator() + "Workspace-XXXXXX.camitk");
92 QVERIFY(file1.open());
93 QVERIFY(file2.open());
94 QVERIFY(fileWorkspace.open());
95 component1->setFileName(file1.fileName());
96 component2->setFileName(file2.fileName());
97 Application::save(component1);
98 Application::save(component2);
99
100 // Save the workspace
101 Application::saveWorkspace(fileWorkspace.fileName());
102
103 // Keep some data for later checks
104 QUuid frameId1 = component1->getFrame()->getUuid();
105 QUuid frameId2 = component2->getFrame()->getUuid();
106
107 // Cleanup unused frames and transformations
109
110 // Count frames and transformations before closing components
111 int nbTrBefore = TransformationManager::getTransformations().size();
112 int nbFrBefore = TransformationManager::getFramesOfReference().size();
113
114 // Reset our shared_ptr
115 tr = nullptr;
116
117 // Close the components
118 Application::close(component1);
119 Application::close(component2);
120
121 // Check that frames and transformations were cleaned up (two frames removed, one transformation and its inverse removed)
122 QCOMPARE(TransformationManager::getFramesOfReference().size(), nbFrBefore - 2);
123 QCOMPARE(TransformationManager::getTransformations().size(), nbTrBefore - 2);
124
125 // Load the workspace
126 Application::loadWorkspace(fileWorkspace.fileName());
127
128 // Check that frames and transformation were added again
129 QCOMPARE(nbFrBefore, TransformationManager::getFramesOfReference().size());
130 QCOMPARE(nbTrBefore, TransformationManager::getTransformations().size());
131
132 // Check that the components are there and that their frames and transformations have the same UUIDs
133 component1 = nullptr;
134 component2 = nullptr;
136 if (comp->getName() == "SphereMeshComponent") {
137 component1 = comp;
138 }
139 else {
140 if (comp->getName() == "SphereMeshComponent2") {
141 component2 = comp;
142 }
143 }
144 }
145 QVERIFY(component1 != nullptr);
146 QVERIFY(component2 != nullptr);
147 QVERIFY(component1->getFrame()->getUuid() == frameId1);
148 QVERIFY(component2->getFrame()->getUuid() == frameId2);
149 QVERIFY(TransformationManager::getTransformation(component1->getFrame(), component2->getFrame())->getUuid() == transformationId);
150
151 // Update data to be checked later
152 nbTrBefore = TransformationManager::getTransformations().size();
154
155 // Close all components
156 Application::getAction("Close All")->apply();
157
158 // Clean up the transformationManager (remove all frames and Transformations)
159 // The Transformation should be detected as useless so the frames should not be in use either
162
163 QVERIFY(TransformationManager::getFrameOfReferenceOwnership(frameId1) == nullptr);
164 QVERIFY(TransformationManager::getFrameOfReferenceOwnership(frameId2) == nullptr);
165 QVERIFY(TransformationManager::getTransformationOwnership(transformationId) == nullptr);
166
167 QCOMPARE(nbFrBefore - 2, TransformationManager::getFramesOfReference().size());
168 QCOMPARE(nbTrBefore - 2, TransformationManager::getTransformations().size());
169
170 // Load the workspace again
171 Application::loadWorkspace(fileWorkspace.fileName());
172
173 // Check that the right number of frames and transformations were loaded
174 QCOMPARE(nbFrBefore, TransformationManager::getFramesOfReference().size());
175 QCOMPARE(nbTrBefore, TransformationManager::getTransformations().size());
176
177 // Check they have the same UUIDs as before
178 QVERIFY(TransformationManager::getFrameOfReferenceOwnership(frameId1) != nullptr);
179 QVERIFY(TransformationManager::getFrameOfReferenceOwnership(frameId2) != nullptr);
180 QVERIFY(TransformationManager::getTransformationOwnership(transformationId) != nullptr);
181 }
182
183};
184
185} // namespace camitk
186
187#endif // CAMITK_TESTPERSISTENCE_H
camitk::Component * createNamedSphere(float radius, QString name)
Create a basic MeshComponent for tests.
Definition TestPersistence.cpp:34
camitk::Component * createNamedSphere(float radius=1.0, QString name="No name")
Create a basic MeshComponent for tests.
Definition TestPersistence.cpp:34
virtual camitk::Action::ApplyStatus apply()=0
This method is called when the action has to be applied on the target list (get the target lists usin...
static const ComponentList & getAllComponents()
get the current application wide list of all Components.
Definition Application.cpp:1273
static Action * getAction(QString)
get a registered action given its name
Definition Application.cpp:967
static bool loadWorkspace(const QString &filepath)
load a camitk file and its content into the application
Definition Application.cpp:728
static bool close(Component *component, bool blockRefresh=false)
Close a Component: if it has been changed, ask the user for more information, then if everything is o...
Definition Application.cpp:755
static bool saveWorkspace(const QString &filepath)
save the current workspace to a .camitk file.
Definition Application.cpp:868
static bool save(Component *component)
save a component to its file (as given by component->getFileName()).
Definition Application.cpp:804
A Component represents something that could be included in the explorer view, the interactive 3D view...
Definition sdk/libraries/core/component/Component.h:304
virtual const FrameOfReference * getFrame() const override
Get the pointer to this object's FrameOfReference.
Definition sdk/libraries/core/component/Component.h:795
void setFileName(const QString &)
set the file name where the data have to be stored
Definition sdk/libraries/core/component/Component.cpp:425
static void autoload()
Autoload component, action and viewer extensions (dlls) as well as registered CamiTK extension file.
Definition ExtensionManager.cpp:46
QUuid getUuid() const override
Get the unique identifier of the Frame.
Definition FrameOfReference.h:339
Definition TestPersistence.h:48
static std::shared_ptr< FrameOfReference > getFrameOfReferenceOwnership(const QUuid &uuid)
FrameOfReference Management.
Definition TransformationManager.cpp:137
static QVector< FrameOfReference * > getFramesOfReference()
Get a list of all stored FrameOfReference.
Definition TransformationManager.cpp:204
static std::shared_ptr< Transformation > addTransformation(const QVariant &)
Create and register a new Transformation from a QVariant (usually from a JSON representation in a ....
Definition TransformationManager.cpp:533
static QVector< Transformation * > getTransformations()
Returns the list of all transformations managed in the system, independents or not.
Definition TransformationManager.cpp:128
static std::shared_ptr< Transformation > getTransformationOwnership(const Transformation *)
Get the shared_ptr that owns the given Transformation.
Definition TransformationManager.cpp:356
static Transformation * getTransformation(const FrameOfReference *from, const FrameOfReference *to)
Get a transformation if it exists or compute it if a path exists between the frames.
Definition TransformationManager.cpp:325
static void cleanupFramesAndTransformations()
Remove transformations and frames that are unused (i.e.
Definition TransformationManager.cpp:630
static bool removeTransformation(std::shared_ptr< Transformation > &tr)
Remove an existing transformation between the two frames.
Definition TransformationManager.cpp:671
QUuid getUuid() const override
Get the unique identifier of this Transformation.
Definition Transformation.h:267
Definition Action.cpp:40