Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPhotoacousticVectorTest.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 "mitkPAVector.h"
17 
18 class mitkPhotoacousticVectorTestSuite : public mitk::TestFixture
19 {
20  CPPUNIT_TEST_SUITE(mitkPhotoacousticVectorTestSuite);
21 
22  MITK_TEST(TestNormalizeVector);
23  MITK_TEST(TestRotateVectorZeroDegrees);
24  MITK_TEST(TestRotatedVectorPositiveDegrees);
25  MITK_TEST(TestRotateVectorZeroDegrees);
26  MITK_TEST(TestScaleVector);
27  MITK_TEST(TestCloneVector);
28 
29  CPPUNIT_TEST_SUITE_END();
30 
31 private:
32 
33  mitk::pa::Vector::Pointer m_TestVector;
34  mitk::pa::Vector::Pointer m_TestReturnVector;
35 
36  const double DIF_VAL = 0.001;
37  const double TWO_PI = 6.283185;
38 
39 public:
40 
41  void setUp() override
42  {
43  m_TestVector = mitk::pa::Vector::New();
44  m_TestReturnVector = mitk::pa::Vector::New();
45  }
46 
47  void TestNormalizeVector()
48  {
49  std::stringstream output;
50  int a = 2;
51  int b = 3;
52  int c = 4;
53 
54  m_TestVector->SetElement(0, a);
55  m_TestVector->SetElement(1, b);
56  m_TestVector->SetElement(2, c);
57 
58  output << "The vectorlength should be";
59  output << sqrt(a*a + b*b + c*c);
60  CPPUNIT_ASSERT_EQUAL_MESSAGE(output.str(), sqrt(a*a + b*b + c*c), m_TestVector->GetNorm());
61  output.flush();
62 
63  m_TestVector->Normalize();
64 
65  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vectorlength should be 1.", true, m_TestVector->GetNorm() - 1 < DIF_VAL);
66  }
67 
68  void TestRotateVectorZeroDegrees()
69  {
70  int a = 1;
71  int b = 2;
72  int c = 3;
73 
74  double length;
75 
76  m_TestVector->SetElement(0, a);
77  m_TestVector->SetElement(1, b);
78  m_TestVector->SetElement(2, c);
79 
80  length = m_TestVector->GetNorm();
81 
82  m_TestVector->Rotate(0, 0);
83 
84  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector length should be equal", length, m_TestVector->GetNorm());
85 
86  CPPUNIT_ASSERT_MESSAGE("The vector value at index0 should be 1.0", m_TestVector->GetElement(0) - 1 < DIF_VAL);
87  CPPUNIT_ASSERT_MESSAGE("The vector value at index1 should be 2.0", m_TestVector->GetElement(1) - 2 < DIF_VAL);
88  CPPUNIT_ASSERT_MESSAGE("The vector value at index2 should be 3.0", m_TestVector->GetElement(2) - 3 < DIF_VAL);
89  }
90 
91  void TestRotatedVectorPositiveDegrees()
92  {
93  MITK_INFO << atan2(0, 0);
94 
95  for (int r = 0; r < 10; r++)
96  {
97  for (double phi = 0.1; phi < 3; phi += 0.1)
98  {
99  for (double theta = 0.1; theta < 3; theta += 0.1)
100  {
101  double rotateTheta = 0.1;
102  double rotatePhi = 0.1;
103 
104  m_TestVector->SetElement(0, r * sin(theta) * cos(phi));
105  m_TestVector->SetElement(1, r * sin(theta) * sin(phi));
106  m_TestVector->SetElement(2, r * cos(theta));
107 
108  m_TestVector->Rotate(rotateTheta, rotatePhi);
109 
110  double newTheta = fmod(theta + rotateTheta, TWO_PI);
111  double newPhi = fmod(phi + rotatePhi, TWO_PI);
112 
113  double expectedX = r * sin(newTheta) * cos(newPhi);
114  double expectedY = r * sin(newTheta) * sin(newPhi);
115  double expectedZ = r * cos(newTheta);
116 
117  CPPUNIT_ASSERT_MESSAGE("The vector value at index0 should be " + std::to_string(expectedX) + " but was " + std::to_string(m_TestVector->GetElement(0))
118  + " at r=" + std::to_string(r) + " phi=" + std::to_string(phi) + " theta=" + std::to_string(theta),
119  m_TestVector->GetElement(0) - expectedX < DIF_VAL);
120  CPPUNIT_ASSERT_MESSAGE("The vector value at index1 should be " + std::to_string(expectedY) + " but was " + std::to_string(m_TestVector->GetElement(0))
121  + " at r=" + std::to_string(r) + " phi=" + std::to_string(phi) + " theta=" + std::to_string(theta),
122  m_TestVector->GetElement(1) - expectedY < DIF_VAL);
123  CPPUNIT_ASSERT_MESSAGE("The vector value at index2 should be " + std::to_string(expectedZ) + " but was " + std::to_string(m_TestVector->GetElement(0))
124  + " at r=" + std::to_string(r) + " phi=" + std::to_string(phi) + " theta=" + std::to_string(theta),
125  m_TestVector->GetElement(2) - expectedZ < DIF_VAL);
126  }
127  }
128  }
129  }
130 
131  void TestRotatedVectorNegativeDegrees()
132  {
133  for (int r = 0; r < 10; r++)
134  {
135  for (double phi = -0.1; phi > -3; phi -= 0.1)
136  {
137  for (double theta = -0.1; theta > -3; theta -= 0.1)
138  {
139  double rotateTheta = -0.1;
140  double rotatePhi = -0.1;
141 
142  m_TestVector->SetElement(0, r * sin(theta) * cos(phi));
143  m_TestVector->SetElement(1, r * sin(theta) * sin(phi));
144  m_TestVector->SetElement(2, r * cos(theta));
145 
146  m_TestVector->Rotate(rotateTheta, rotatePhi);
147 
148  double newTheta = fmod(theta + rotateTheta, TWO_PI);
149  double newPhi = fmod(phi + rotatePhi, TWO_PI);
150 
151  double expectedX = r * sin(newTheta) * cos(newPhi);
152  double expectedY = r * sin(newTheta) * sin(newPhi);
153  double expectedZ = r * cos(newTheta);
154 
155  CPPUNIT_ASSERT_MESSAGE("The vector value at index0 should be " + std::to_string(expectedX) + " but was " + std::to_string(m_TestVector->GetElement(0))
156  + " at r=" + std::to_string(r) + " phi=" + std::to_string(phi) + " theta=" + std::to_string(theta),
157  m_TestVector->GetElement(0) - expectedX < DIF_VAL);
158  CPPUNIT_ASSERT_MESSAGE("The vector value at index1 should be " + std::to_string(expectedY) + " but was " + std::to_string(m_TestVector->GetElement(0))
159  + " at r=" + std::to_string(r) + " phi=" + std::to_string(phi) + " theta=" + std::to_string(theta),
160  m_TestVector->GetElement(1) - expectedY < DIF_VAL);
161  CPPUNIT_ASSERT_MESSAGE("The vector value at index2 should be " + std::to_string(expectedZ) + " but was " + std::to_string(m_TestVector->GetElement(0))
162  + " at r=" + std::to_string(r) + " phi=" + std::to_string(phi) + " theta=" + std::to_string(theta),
163  m_TestVector->GetElement(2) - expectedZ < DIF_VAL);
164  }
165  }
166  }
167  }
168 
169  void TestScaleVector()
170  {
171  double a = 1.0;
172  double b = 2.0;
173  double c = 3.0;
174 
175  for (double testFactor = -2.0; testFactor <= 2.0; testFactor += 0.3)
176  {
177  double potElement0Fctr;
178  double potElement1Fctr;
179  double potElement2Fctr;
180 
181  std::stringstream output;
182 
183  m_TestVector->SetElement(0, a);
184  m_TestVector->SetElement(1, b);
185  m_TestVector->SetElement(2, c);
186 
187  potElement0Fctr = (m_TestVector->GetElement(0)*testFactor)*(m_TestVector->GetElement(0)*testFactor);
188  potElement1Fctr = (m_TestVector->GetElement(1)*testFactor)*(m_TestVector->GetElement(1)*testFactor);
189  potElement2Fctr = (m_TestVector->GetElement(2)*testFactor)*(m_TestVector->GetElement(2)*testFactor);
190 
191  m_TestVector->Scale(testFactor);
192 
193  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector length should not be equal",
194  sqrt(potElement0Fctr + potElement1Fctr + potElement2Fctr), m_TestVector->GetNorm());
195 
196  output << "The vector value at index0 should be";
197  output << a*testFactor;
198  CPPUNIT_ASSERT_EQUAL_MESSAGE(output.str(), a*testFactor, m_TestVector->GetElement(0));
199  output.flush();
200 
201  output << "The vector value at index1 should be";
202  output << b*testFactor;
203  CPPUNIT_ASSERT_EQUAL_MESSAGE(output.str(), b*testFactor, m_TestVector->GetElement(1));
204  output.flush();
205 
206  output << "The vector value at index2 should be";
207  output << c*testFactor;
208  CPPUNIT_ASSERT_EQUAL_MESSAGE(output.str(), c*testFactor, m_TestVector->GetElement(2));
209  output.flush();
210  }
211  }
212 
213  void TestCloneVector()
214  {
215  int a = 1;
216  int b = 2;
217  int c = 3;
218 
219  m_TestVector->SetElement(0, a);
220  m_TestVector->SetElement(1, b);
221  m_TestVector->SetElement(2, c);
222 
223  m_TestReturnVector = m_TestVector->Clone();
224 
225  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector length should be equal", (m_TestVector->GetNorm()), m_TestReturnVector->GetNorm());
226 
227  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector value at index0 should be equal", m_TestVector->GetElement(0), m_TestReturnVector->GetElement(0));
228  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector value at index1 should be equal", m_TestVector->GetElement(1), m_TestReturnVector->GetElement(1));
229  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector value at index2 should be equal", m_TestVector->GetElement(2), m_TestReturnVector->GetElement(2));
230 
231  m_TestReturnVector->Rotate(itk::Math::pi / 4, itk::Math::pi / 4);
232 
233  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector value at index0 should be not equal", true, m_TestVector->GetElement(0) != m_TestReturnVector->GetElement(0));
234  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector value at index0 should be not equal", true, m_TestVector->GetElement(1) != m_TestReturnVector->GetElement(1));
235  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector value at index0 should be not equal", true, m_TestVector->GetElement(2) != m_TestReturnVector->GetElement(2));
236 
237  for (double testFactor = -2.0; testFactor <= 2.0; testFactor += 0.3)
238  {
239  m_TestReturnVector->Scale(testFactor);
240 
241  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector value at index0 should be not equal", true, m_TestVector->GetElement(0) != m_TestReturnVector->GetElement(0));
242  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector value at index0 should be not equal", true, m_TestVector->GetElement(1) != m_TestReturnVector->GetElement(1));
243  CPPUNIT_ASSERT_EQUAL_MESSAGE("The vector value at index0 should be not equal", true, m_TestVector->GetElement(2) != m_TestReturnVector->GetElement(2));
244  }
245  }
246 
247  void tearDown() override
248  {
249  m_TestVector = nullptr;
250  m_TestReturnVector = nullptr;
251  }
252 };
253 
254 MITK_TEST_SUITE_REGISTRATION(mitkPhotoacousticVector)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
Test fixture for parameterized tests.
static Pointer New()