Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPhotoacoustic3dVolumeTest.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 <mitkTestFixture.h>
14 #include <mitkTestingMacros.h>
15 
16 #include "mitkPAVolume.h"
17 #include <mitkImage.h>
18 #include <mitkImageReadAccessor.h>
19 
20 class mitkPhotoacoustic3dVolumeTestSuite : public mitk::TestFixture
21 {
22  CPPUNIT_TEST_SUITE(mitkPhotoacoustic3dVolumeTestSuite);
23  MITK_TEST(TestCorrectGetDataAndSetDataBehavior);
24  MITK_TEST(TestCallingConstructorWithNullParameter);
25  MITK_TEST(TestCallingConstructorWithCorrectParameters);
26  MITK_TEST(TestModifyImage);
27  MITK_TEST(TestModifyComplexImage);
28  MITK_TEST(TestConvertToMitkImage);
29  MITK_TEST(TestDeepCopy);
30  MITK_TEST(TestCatchException);
31  CPPUNIT_TEST_SUITE_END();
32 
33 private:
34 
35  mitk::pa::Volume::Pointer m_Photoacoustic3dVolume;
36 
37 public:
38 
39  void setUp() override
40  {
41  }
42 
43  void TestCallingConstructorWithNullParameter()
44  {
45  bool exceptionEncountered = false;
46  try
47  {
48  m_Photoacoustic3dVolume = mitk::pa::Volume::New(nullptr, 3, 3, 3, 1);
49  }
50  catch (...)
51  {
52  exceptionEncountered = true;
53  }
54  CPPUNIT_ASSERT(exceptionEncountered);
55  }
56 
57  void TestCallingConstructorWithCorrectParameters()
58  {
59  auto* data = new double[1];
60  data[0] = 3;
61  m_Photoacoustic3dVolume = mitk::pa::Volume::New(data, 1, 1, 1, 1);
62  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 0, 0) == 3);
63  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetXDim() == 1);
64  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetYDim() == 1);
65  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetZDim() == 1);
66  }
67 
68  void TestModifyImage()
69  {
70  auto* data = new double[1];
71  data[0] = 3;
72  m_Photoacoustic3dVolume = mitk::pa::Volume::New(data, 1, 1, 1, 1);
73  CPPUNIT_ASSERT_MESSAGE(std::to_string(m_Photoacoustic3dVolume->GetData(0, 0, 0)), m_Photoacoustic3dVolume->GetData(0, 0, 0) == 3);
74  m_Photoacoustic3dVolume->SetData(17, 0, 0, 0);
75  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 0, 0) == 17);
76  }
77 
78  void TestModifyComplexImage()
79  {
80  unsigned int xDim = 4;
81  unsigned int yDim = 7;
82  unsigned int zDim = 12;
83  unsigned int length = xDim * yDim * zDim;
84  auto* data = new double[length];
85  for (unsigned int i = 0; i < length; i++)
86  data[i] = 5;
87 
88  m_Photoacoustic3dVolume = mitk::pa::Volume::New(data, xDim, yDim, zDim, 1);
89 
90  for (unsigned int z = 0; z < zDim; z++)
91  for (unsigned int y = 0; y < yDim; y++)
92  for (unsigned int x = 0; x < xDim; x++)
93  {
94  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(x, y, z) == 5);
95  m_Photoacoustic3dVolume->SetData((x + y)*(z + 1), x, y, z);
96  CPPUNIT_ASSERT(std::abs(m_Photoacoustic3dVolume->GetData(x, y, z) - (x + y)*(z + 1)) < mitk::eps);
97  }
98  }
99 
100  void TestCorrectGetDataAndSetDataBehavior()
101  {
102  unsigned int xDim = 40;
103  unsigned int yDim = 7;
104  unsigned int zDim = 12;
105  unsigned int length = xDim * yDim * zDim;
106  auto* data = new double[length];
107  for (unsigned int i = 0; i < length; i++)
108  data[i] = 0;
109 
110  m_Photoacoustic3dVolume = mitk::pa::Volume::New(data, xDim, yDim, zDim, 1);
111 
112  for (unsigned int z = 0; z < zDim; z++)
113  for (unsigned int y = 0; y < yDim; y++)
114  for (unsigned int x = 0; x < xDim; x++)
115  {
116  int index = z*xDim*yDim + x*yDim + y;
117  m_Photoacoustic3dVolume->SetData(index, x, y, z);
118  CPPUNIT_ASSERT_MESSAGE(std::to_string(index), m_Photoacoustic3dVolume->GetData(x, y, z) == index);
119  }
120  }
121 
122  void TestConvertToMitkImage()
123  {
124  auto* data = new double[6];
125  data[0] = 3;
126  data[1] = 3;
127  data[2] = 3;
128  data[3] = 3;
129  data[4] = 3;
130  data[5] = 3;
131  m_Photoacoustic3dVolume = mitk::pa::Volume::New(data, 1, 2, 3, 1);
132  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 0, 0) == 3);
133  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 0, 1) == 3);
134  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 0, 2) == 3);
135  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 1, 0) == 3);
136  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 1, 1) == 3);
137  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 1, 2) == 3);
138 
139  m_Photoacoustic3dVolume->SetData(17, 0, 0, 0);
140  m_Photoacoustic3dVolume->SetData(17, 0, 1, 0);
141  m_Photoacoustic3dVolume->SetData(17, 0, 1, 2);
142  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 0, 0) == 17);
143  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 0, 1) == 3);
144  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 0, 2) == 3);
145  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 1, 0) == 17);
146  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 1, 1) == 3);
147  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 1, 2) == 17);
148 
149  mitk::Image::Pointer mitkImage = m_Photoacoustic3dVolume->AsMitkImage();
150  CPPUNIT_ASSERT(mitkImage->GetDimensions()[0] == 2);
151  CPPUNIT_ASSERT(mitkImage->GetDimensions()[1] == 1);
152  CPPUNIT_ASSERT(mitkImage->GetDimensions()[2] == 3);
153 
154  mitk::ImageReadAccessor readAccess(mitkImage, mitkImage->GetVolumeData());
155  auto* copyData = (double*)readAccess.GetData();
156  CPPUNIT_ASSERT_MESSAGE(std::to_string(copyData[0]), copyData[0] == 17);
157  CPPUNIT_ASSERT_MESSAGE(std::to_string(copyData[1]), copyData[1] == 17);
158  CPPUNIT_ASSERT_MESSAGE(std::to_string(copyData[2]), copyData[2] == 3);
159  CPPUNIT_ASSERT_MESSAGE(std::to_string(copyData[3]), copyData[3] == 3);
160  CPPUNIT_ASSERT_MESSAGE(std::to_string(copyData[4]), copyData[4] == 3);
161  CPPUNIT_ASSERT_MESSAGE(std::to_string(copyData[5]), copyData[5] == 17);
162  }
163 
164  void TestDeepCopy()
165  {
166  auto* data = new double[1];
167  data[0] = 3;
168  m_Photoacoustic3dVolume = mitk::pa::Volume::New(data, 1, 1, 1, 1);
169  mitk::pa::Volume::Pointer copiedVolume = m_Photoacoustic3dVolume->DeepCopy();
170 
171  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetXDim() == copiedVolume->GetXDim());
172  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetYDim() == copiedVolume->GetYDim());
173  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetZDim() == copiedVolume->GetZDim());
174  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 0, 0) == 3);
175  CPPUNIT_ASSERT(copiedVolume->GetData(0, 0, 0) == 3);
176  m_Photoacoustic3dVolume->SetData(17, 0, 0, 0);
177  CPPUNIT_ASSERT(m_Photoacoustic3dVolume->GetData(0, 0, 0) == 17);
178  CPPUNIT_ASSERT(copiedVolume->GetData(0, 0, 0) == 3);
179  }
180 
181  void AssertIndexException(unsigned int x, unsigned int y, unsigned int z)
182  {
183  bool exceptionCaught = false;
184  try
185  {
186  double thisIsIrrelevant = m_Photoacoustic3dVolume->GetData(x, y, z);
187  thisIsIrrelevant += 1;
188  }
189  catch (...)
190  {
191  exceptionCaught = true;
192  if (exceptionCaught)
193  exceptionCaught = true;
194  }
195 #ifdef _DEBUG
196 
197  CPPUNIT_ASSERT(exceptionCaught);
198 
199 #endif
200  }
201 
202  void TestCatchException()
203  {
204  auto* data = new double[1];
205  data[0] = 3;
206  m_Photoacoustic3dVolume = mitk::pa::Volume::New(data, 1, 1, 1, 1);
207 
208  AssertIndexException(1, 0, 0);
209  AssertIndexException(0, 1, 0);
210  AssertIndexException(0, 0, 1);
211  AssertIndexException(18, 1, 222);
212  }
213 
214  void tearDown() override
215  {
216  m_Photoacoustic3dVolume = nullptr;
217  }
218 };
219 
220 MITK_TEST_SUITE_REGISTRATION(mitkPhotoacoustic3dVolume)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
Test fixture for parameterized tests.
MITKCORE_EXPORT const ScalarType eps
ImageReadAccessor class to get locked read access for a particular image part.
static Volume::Pointer New(double *data, unsigned int xDim, unsigned int yDim, unsigned int zDim, double spacing)
returns smartpointer reference to a new instance of this objects. The given data array will be freed ...
const void * GetData() const
Gives const access to the data.