Medical Imaging Interaction Toolkit  2018.4.99-c0f884b2
Medical Imaging Interaction Toolkit
mitkPointSetFileIOTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkFileWriterRegistry.h"
14 #include "mitkIOUtil.h"
15 #include "mitkPointSet.h"
17 #include "mitkTestingMacros.h"
18 
19 #include <itksys/SystemTools.hxx>
20 #include <random>
21 #include <ctime>
22 #include <vector>
23 
24 // unsigned int numberOfTestPointSets = 1;
25 unsigned int numberOfTimeSeries = 5;
26 
27 // create one test PointSet
28 class mitkPointSetFileIOTestClass
29 {
30 public:
31  mitk::PointSet::Pointer m_SavedPointSet;
32  std::string m_FilePath;
33 
34  mitkPointSetFileIOTestClass() {}
35  ~mitkPointSetFileIOTestClass()
36  {
37  if (!m_FilePath.empty())
38  {
39  std::remove(m_FilePath.c_str());
40  }
41  }
42 
43  mitk::PointSet::Pointer CreateTestPointSet(mitk::BaseGeometry *geometry = nullptr)
44  {
46  std::uniform_real_distribution<> r(0, 1);
47  std::mt19937 gen;
48 
49  for (unsigned int t = 0; t < numberOfTimeSeries; t++)
50  {
51  for (unsigned int position = 0; position < 3; ++position)
52  {
53  mitk::Point3D point;
54  mitk::FillVector3D(point, r(gen), r(gen), r(gen));
55  pointSet->SetPoint(position, point, t);
56  }
57  }
58  m_SavedPointSet = pointSet;
59 
60  // we set the geometry AFTER adding points. Else all points added via SetPoint would already bee transformed
61  if (geometry != nullptr)
62  {
64  timeGeometry->Initialize(geometry, numberOfTimeSeries);
65  pointSet->SetTimeGeometry(timeGeometry);
66  }
67 
68  return pointSet;
69  }
70 
71  void PointSetCompare(mitk::PointSet::Pointer pointSet2, mitk::PointSet::Pointer pointSet1, bool & /*identical*/)
72  {
73  MITK_TEST_CONDITION(pointSet1->GetSize() == pointSet2->GetSize(), "Testing if PointSet size is correct");
74 
75  for (unsigned int t = 0; t < numberOfTimeSeries; t++)
76  {
77  for (unsigned int i = 0; i < (unsigned int)pointSet1->GetSize(t); ++i)
78  {
79  mitk::Point3D p1 = pointSet1->GetPoint(i);
80  mitk::Point3D p2 = pointSet2->GetPoint(i);
81 
82  // test
83  std::cout << "r point: " << p2 << std::endl;
84  std::cout << "w point: " << p1 << std::endl;
85 
86  // test end
87 
88  MITK_TEST_CONDITION((p1[0] - p2[0]) <= 0.0001,
89  "Testing if X coordinates of the Point are at the same Position");
90  MITK_TEST_CONDITION((p1[1] - p2[1]) <= 0.0001,
91  "Testing if Y coordinates of the Point are at the same Position");
92  MITK_TEST_CONDITION((p1[2] - p2[2]) <= 0.0001,
93  "Testing if Z coordinates of the Point are at the same Position");
94  }
95  }
96 
97  // testing geometry
98  MITK_TEST_CONDITION(mitk::Equal(*(pointSet1->GetGeometry()), *(pointSet2->GetGeometry()), 0.000001, true),
99  "Restored geometry must equal original one.");
100  }
101 
102  bool PointSetWrite(mitk::BaseGeometry *geometry = nullptr)
103  {
104  try
105  {
106  m_SavedPointSet = nullptr;
107 
108  std::ofstream tmpStream;
109  m_FilePath = mitk::IOUtil::CreateTemporaryFile(tmpStream) + ".mps";
110  MITK_INFO << "PointSet test file at " << m_FilePath;
111  mitk::IOUtil::Save(CreateTestPointSet(geometry), m_FilePath);
112  }
113  catch (std::exception &e)
114  {
115  MITK_ERROR << "Error during pointset creation: " << e.what();
116  return false;
117  }
118 
119  return true;
120  }
121 
122  void PointSetLoadAndCompareTest()
123  {
124  try
125  {
126  mitk::PointSet::Pointer pointSet = mitk::IOUtil::Load<mitk::PointSet>(m_FilePath);
127  MITK_TEST_CONDITION(pointSet.IsNotNull(), "Testing if the loaded Data are nullptr");
128 
129  bool identical(true);
130  PointSetCompare(pointSet.GetPointer(), m_SavedPointSet.GetPointer(), identical);
131  }
132  catch (std::exception &e)
133  {
134  MITK_ERROR << "Error during pointset creation: " << e.what();
135  }
136  }
137 
138 }; // mitkPointSetFileIOTestClass
139 
140 int mitkPointSetFileIOTest(int, char *[])
141 {
142  MITK_TEST_BEGIN("PointSet");
143 
144  // minimum test w/ identity geometry
145  {
146  mitkPointSetFileIOTestClass test;
147  MITK_TEST_CONDITION(test.PointSetWrite(), "Testing if the PointSetWriter writes Data");
148  test.PointSetLoadAndCompareTest(); // load - compare
149  }
150 
151  // case with a more complex geometry
152  {
153  mitkPointSetFileIOTestClass test;
154 
156 
157  // define arbitrary transformation matrix
158  // the number don't have much meaning - we just want them reproduced
159  // by the writer/reader cycle
161  bounds[0] = -918273645.18293746;
162  bounds[1] = -52.723;
163  bounds[2] = -1.002;
164  bounds[3] = 918273645.18293746;
165  bounds[4] = +1.002;
166  bounds[5] = +52.723;
167  g->SetBounds(bounds);
168 
169  mitk::ScalarType matrixCoeffs[9] = {0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8};
170 
171  mitk::AffineTransform3D::MatrixType matrix;
172  matrix.GetVnlMatrix().set(matrixCoeffs);
173 
174  mitk::AffineTransform3D::OffsetType offset;
175  offset[0] = -43.1829374;
176  offset[1] = 0.0;
177  offset[2] = +43.1829374;
178 
179  mitk::AffineTransform3D::Pointer transform = mitk::AffineTransform3D::New();
180  transform->SetMatrix(matrix);
181  transform->SetOffset(offset);
182  g->SetIndexToWorldTransform(transform);
183 
184  MITK_TEST_CONDITION(test.PointSetWrite(g), "Testing if the PointSetWriter writes Data _with_ geometry");
185  test.PointSetLoadAndCompareTest(); // load - compare
186  }
187 
188  MITK_TEST_END();
189 }
BoundingBoxType::BoundsArrayType BoundsArrayType
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
double ScalarType
Follow Up Storage - Class to facilitate loading/accessing structured follow-up data.
Definition: testcase.h:28
static Pointer New()
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
static Pointer New()
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:106
static Vector3D offset
#define MITK_TEST_CONDITION(COND, MSG)
unsigned int numberOfTimeSeries
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
static std::string CreateTemporaryFile(std::ofstream &tmpStream, const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:413
and MITK_TEST_END()
int mitkPointSetFileIOTest(int, char *[])
BaseGeometry Describes the geometry of a data object.