Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkFileWriterRegistry.h"
18 #include "mitkIOUtil.h"
19 #include "mitkPointSet.h"
21 #include "mitkTestingMacros.h"
22 
23 #include <itksys/SystemTools.hxx>
24 #include <random>
25 #include <time.h>
26 #include <vector>
27 
28 // unsigned int numberOfTestPointSets = 1;
29 unsigned int numberOfTimeSeries = 5;
30 
31 // create one test PointSet
32 class mitkPointSetFileIOTestClass
33 {
34 public:
35  mitk::PointSet::Pointer m_SavedPointSet;
36  std::string m_FilePath;
37 
38  mitkPointSetFileIOTestClass() {}
39  ~mitkPointSetFileIOTestClass()
40  {
41  if (!m_FilePath.empty())
42  {
43  std::remove(m_FilePath.c_str());
44  }
45  }
46 
47  mitk::PointSet::Pointer CreateTestPointSet(mitk::BaseGeometry *geometry = nullptr)
48  {
50  std::uniform_real_distribution<> r(0, 1);
51  std::mt19937 gen;
52 
53  for (unsigned int t = 0; t < numberOfTimeSeries; t++)
54  {
55  for (unsigned int position = 0; position < 3; ++position)
56  {
57  mitk::Point3D point;
58  mitk::FillVector3D(point, r(gen), r(gen), r(gen));
59  pointSet->SetPoint(position, point, t);
60  }
61  }
62  m_SavedPointSet = pointSet;
63 
64  // we set the geometry AFTER adding points. Else all points added via SetPoint would already bee transformed
65  if (geometry != nullptr)
66  {
68  timeGeometry->Initialize(geometry, numberOfTimeSeries);
69  pointSet->SetTimeGeometry(timeGeometry);
70  }
71 
72  return pointSet;
73  }
74 
75  void PointSetCompare(mitk::PointSet::Pointer pointSet2, mitk::PointSet::Pointer pointSet1, bool & /*identical*/)
76  {
77  MITK_TEST_CONDITION(pointSet1->GetSize() == pointSet2->GetSize(), "Testing if PointSet size is correct");
78 
79  for (unsigned int t = 0; t < numberOfTimeSeries; t++)
80  {
81  for (unsigned int i = 0; i < (unsigned int)pointSet1->GetSize(t); ++i)
82  {
83  mitk::Point3D p1 = pointSet1->GetPoint(i);
84  mitk::Point3D p2 = pointSet2->GetPoint(i);
85 
86  // test
87  std::cout << "r point: " << p2 << std::endl;
88  std::cout << "w point: " << p1 << std::endl;
89 
90  // test end
91 
92  MITK_TEST_CONDITION((p1[0] - p2[0]) <= 0.0001,
93  "Testing if X coordinates of the Point are at the same Position");
94  MITK_TEST_CONDITION((p1[1] - p2[1]) <= 0.0001,
95  "Testing if Y coordinates of the Point are at the same Position");
96  MITK_TEST_CONDITION((p1[2] - p2[2]) <= 0.0001,
97  "Testing if Z coordinates of the Point are at the same Position");
98  }
99  }
100 
101  // testing geometry
102  MITK_TEST_CONDITION(mitk::Equal(*(pointSet1->GetGeometry()), *(pointSet2->GetGeometry()), 0.000001, true),
103  "Restored geometry must equal original one.");
104  }
105 
106  bool PointSetWrite(mitk::BaseGeometry *geometry = nullptr)
107  {
108  try
109  {
110  m_SavedPointSet = NULL;
111 
112  std::ofstream tmpStream;
113  m_FilePath = mitk::IOUtil::CreateTemporaryFile(tmpStream) + ".mps";
114  MITK_INFO << "PointSet test file at " << m_FilePath;
115  mitk::IOUtil::Save(CreateTestPointSet(geometry), m_FilePath);
116  }
117  catch (std::exception &e)
118  {
119  MITK_ERROR << "Error during pointset creation: " << e.what();
120  return false;
121  }
122 
123  return true;
124  }
125 
126  void PointSetLoadAndCompareTest()
127  {
128  try
129  {
131  MITK_TEST_CONDITION(pointSet.IsNotNull(), "Testing if the loaded Data are NULL");
132 
133  bool identical(true);
134  PointSetCompare(pointSet.GetPointer(), m_SavedPointSet.GetPointer(), identical);
135  }
136  catch (std::exception &e)
137  {
138  MITK_ERROR << "Error during pointset creation: " << e.what();
139  }
140  }
141 
142 }; // mitkPointSetFileIOTestClass
143 
144 int mitkPointSetFileIOTest(int, char *[])
145 {
146  MITK_TEST_BEGIN("PointSet");
147 
148  // minimum test w/ identity geometry
149  {
150  mitkPointSetFileIOTestClass test;
151  MITK_TEST_CONDITION(test.PointSetWrite(), "Testing if the PointSetWriter writes Data");
152  test.PointSetLoadAndCompareTest(); // load - compare
153  }
154 
155  // case with a more complex geometry
156  {
157  mitkPointSetFileIOTestClass test;
158 
160 
161  // define arbitrary transformation matrix
162  // the number don't have much meaning - we just want them reproduced
163  // by the writer/reader cycle
165  bounds[0] = -918273645.18293746;
166  bounds[1] = -52.723;
167  bounds[2] = -1.002;
168  bounds[3] = 918273645.18293746;
169  bounds[4] = +1.002;
170  bounds[5] = +52.723;
171  g->SetBounds(bounds);
172 
173  mitk::ScalarType matrixCoeffs[9] = {0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8};
174 
175  mitk::AffineTransform3D::MatrixType matrix;
176  matrix.GetVnlMatrix().set(matrixCoeffs);
177 
178  mitk::AffineTransform3D::OffsetType offset;
179  offset[0] = -43.1829374;
180  offset[1] = 0.0;
181  offset[2] = +43.1829374;
182 
184  transform->SetMatrix(matrix);
185  transform->SetOffset(offset);
186  g->SetIndexToWorldTransform(transform);
187 
188  MITK_TEST_CONDITION(test.PointSetWrite(g), "Testing if the PointSetWriter writes Data _with_ geometry");
189  test.PointSetLoadAndCompareTest(); // load - compare
190  }
191 
192  MITK_TEST_END();
193 }
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
itk::SmartPointer< Self > Pointer
BoundingBoxType::BoundsArrayType BoundsArrayType
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
double ScalarType
Follow Up Storage - Class to facilitate loading/accessing structured follow-up data.
Definition: testcase.h:32
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:110
static Vector3D offset
#define MITK_TEST_CONDITION(COND, MSG)
static mitk::PointSet::Pointer LoadPointSet(const std::string &path)
LoadPointSet Convenience method to load an arbitrary mitkPointSet.
Definition: mitkIOUtil.cpp:619
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 std::string CreateTemporaryFile(std::ofstream &tmpStream, const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:407
and MITK_TEST_END()
int mitkPointSetFileIOTest(int, char *[])
BaseGeometry Describes the geometry of a data object.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.