Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkMcxyzXmlTest.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 <mitkPAProbe.h>
17 
18 // us
19 #include <usModule.h>
20 #include <usModuleResource.h>
21 #include <usGetModuleContext.h>
22 #include <usModuleContext.h>
23 #include <usModuleResourceStream.h>
24 #include <mitkLocaleSwitch.h>
25 #include <random>
26 #include <chrono>
27 
28 class mitkMcxyzXmlTestSuite : public mitk::TestFixture
29 {
30  CPPUNIT_TEST_SUITE(mitkMcxyzXmlTestSuite);
31  MITK_TEST(TestCreatePointSourceProbe);
32  MITK_TEST(TestCreateCircleSourceProbe);
33  MITK_TEST(TestCreateRectangleSourceProbe);
34  MITK_TEST(TestCreateTwoPointSourcesProbe);
35  MITK_TEST(TestCreateAllSourcesProbe);
36  MITK_TEST(TestValuesAreInBoundsUniformRectangle);
37  MITK_TEST(TestValuesAreInBoundsGaussianRectangle);
38  CPPUNIT_TEST_SUITE_END();
39 
40 private:
41 
42  mitk::pa::LightSource::Pointer m_LightSource;
43  mitk::pa::Probe::Pointer m_Probe;
44  std::string m_XmlProbePointSource;
45  std::string m_XmlProbeCircleSource;
46  std::string m_XmlProbeRectangleSource;
47  std::string m_XmlProbeTwoPointSources;
48  std::string m_XmlProbeAllSources;
49 
50 public:
51 
52  void setUp() override
53  {
54  m_LightSource = mitk::pa::LightSource::New();
55  LoadXmlFile("pointsource.xml", &m_XmlProbePointSource);
56  LoadXmlFile("circlesource.xml", &m_XmlProbeCircleSource);
57  LoadXmlFile("rectanglesource.xml", &m_XmlProbeRectangleSource);
58  LoadXmlFile("twopointsources.xml", &m_XmlProbeTwoPointSources);
59  LoadXmlFile("allsources.xml", &m_XmlProbeAllSources);
60  }
61 
62  void LoadXmlFile(std::string filename, std::string* lines)
63  {
64  us::ModuleResource pointSourceXml = us::GetModuleContext()->GetModule()->GetResource(filename);
65  std::string line;
66  if (pointSourceXml.IsValid() && pointSourceXml.IsFile())
67  {
68  us::ModuleResourceStream stream(pointSourceXml);
69  stream.std::istream::imbue(std::locale("C"));
70  while (std::getline(stream, line))
71  {
72  *lines = *lines + line + " ";
73  }
74  }
75  else
76  {
77  MITK_ERROR << "Xml file was not valid";
78  }
79  }
80 
81  void TestCreatePointSourceProbe()
82  {
83  m_Probe = mitk::pa::Probe::New(m_XmlProbePointSource.c_str(), true);
84  CPPUNIT_ASSERT(true == m_Probe->IsValid());
85  }
86 
87  void TestCreateCircleSourceProbe()
88  {
89  m_Probe = mitk::pa::Probe::New(m_XmlProbeCircleSource.c_str(), true);
90  CPPUNIT_ASSERT(true == m_Probe->IsValid());
91  }
92 
93  void TestCreateRectangleSourceProbe()
94  {
95  m_Probe = mitk::pa::Probe::New(m_XmlProbeRectangleSource.c_str(), true);
96  CPPUNIT_ASSERT(true == m_Probe->IsValid());
97  }
98 
99  void TestCreateTwoPointSourcesProbe()
100  {
101  m_Probe = mitk::pa::Probe::New(m_XmlProbeTwoPointSources.c_str(), true);
102  CPPUNIT_ASSERT(true == m_Probe->IsValid());
103  }
104 
105  void TestCreateAllSourcesProbe()
106  {
107  m_Probe = mitk::pa::Probe::New(m_XmlProbeAllSources.c_str(), true);
108  CPPUNIT_ASSERT(true == m_Probe->IsValid());
109  }
110 
111  void TestValuesAreInBoundsUniformRectangle()
112  {
113  int MAXIMUM = 2;
114  int MINIMUM = -2;
115 
116  int ANGLE_MAXIMUM = 1;
117  int ANGLE_MINIMUM = -1;
118 
119  m_LightSource->SetAngleXMode(mitk::pa::LightSource::DistributionMode::UNIFORM);
120  m_LightSource->SetAngleYMode(mitk::pa::LightSource::DistributionMode::UNIFORM);
121 
122  m_LightSource->SetAngleXMaximum(ANGLE_MAXIMUM);
123  m_LightSource->SetAngleXMinimum(ANGLE_MINIMUM);
124 
125  m_LightSource->SetAngleYMaximum(ANGLE_MAXIMUM);
126  m_LightSource->SetAngleYMinimum(ANGLE_MINIMUM);
127 
128  m_LightSource->SetSpawnLocationX(MINIMUM);
129  m_LightSource->SetSpawnLocationXLength(2 * MAXIMUM);
130 
131  m_LightSource->SetSpawnLocationY(MINIMUM);
132  m_LightSource->SetSpawnLocationYLength(2 * MAXIMUM);
133 
134  m_LightSource->SetSpawnLocationZ(MINIMUM);
135  m_LightSource->SetSpawnLocationZLength(2 * MAXIMUM);
136 
137  m_LightSource->SetSpawnLocationRadius(MAXIMUM);
138 
139  m_LightSource->SetVerbose(false);
140 
141  m_LightSource->SetSpawnType(mitk::pa::LightSource::SpawnType::RECTANGLE);
142 
143  std::mt19937 rng;
144  rng.seed(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::
145  now().time_since_epoch()).count());
146  std::uniform_real_distribution<> realDist(0, 1);
147 
148  for (int i = 0, length = 10000; i < length; i++)
149  {
150  auto result =
151  m_LightSource->GetNextPhoton(realDist(rng), realDist(rng), realDist(rng), realDist(rng),
152  realDist(rng), realDist(rng), realDist(rng));
153 
154  CPPUNIT_ASSERT(result.xAngle >= ANGLE_MINIMUM);
155  CPPUNIT_ASSERT(result.xAngle <= ANGLE_MAXIMUM);
156  CPPUNIT_ASSERT(result.yAngle >= ANGLE_MINIMUM);
157  CPPUNIT_ASSERT(result.yAngle <= ANGLE_MAXIMUM);
158  CPPUNIT_ASSERT(result.zAngle >= 0);
159  CPPUNIT_ASSERT(result.zAngle <= ANGLE_MAXIMUM);
160  CPPUNIT_ASSERT(result.xPosition >= MINIMUM);
161  CPPUNIT_ASSERT(result.xPosition <= MAXIMUM);
162  CPPUNIT_ASSERT(result.yPosition >= MINIMUM);
163  CPPUNIT_ASSERT(result.yPosition <= MAXIMUM);
164  CPPUNIT_ASSERT(result.zPosition >= MINIMUM);
165  CPPUNIT_ASSERT(result.zPosition <= MAXIMUM);
166  }
167  }
168 
169  void TestValuesAreInBoundsGaussianRectangle()
170  {
171  int MAXIMUM = 2;
172  int MINIMUM = -2;
173 
174  int ANGLE_MAXIMUM = 1;
175  int ANGLE_MINIMUM = -1;
176 
177  m_LightSource->SetAngleXMode(mitk::pa::LightSource::DistributionMode::GAUSSIAN);
178  m_LightSource->SetAngleYMode(mitk::pa::LightSource::DistributionMode::GAUSSIAN);
179 
180  m_LightSource->SetAngleXMaximum(ANGLE_MAXIMUM);
181  m_LightSource->SetAngleXMinimum(ANGLE_MINIMUM);
182 
183  m_LightSource->SetAngleYMaximum(ANGLE_MAXIMUM);
184  m_LightSource->SetAngleYMinimum(ANGLE_MINIMUM);
185 
186  m_LightSource->SetSpawnLocationX(MINIMUM);
187  m_LightSource->SetSpawnLocationXLength(2 * MAXIMUM);
188 
189  m_LightSource->SetSpawnLocationY(MINIMUM);
190  m_LightSource->SetSpawnLocationYLength(2 * MAXIMUM);
191 
192  m_LightSource->SetSpawnLocationZ(MINIMUM);
193  m_LightSource->SetSpawnLocationZLength(2 * MAXIMUM);
194 
195  m_LightSource->SetSpawnLocationRadius(MAXIMUM);
196 
197  m_LightSource->SetVerbose(false);
198 
199  m_LightSource->SetSpawnType(mitk::pa::LightSource::SpawnType::RECTANGLE);
200 
201  std::mt19937 rng;
202  rng.seed(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::
203  now().time_since_epoch()).count());
204  std::uniform_real_distribution<> realDist(0, 1);
205 
206  for (int i = 0, length = 10000; i < length; i++)
207  {
208  auto result =
209  m_LightSource->GetNextPhoton(realDist(rng), realDist(rng), realDist(rng), realDist(rng),
210  realDist(rng), realDist(rng), realDist(rng));
211 
212  CPPUNIT_ASSERT(result.xAngle >= ANGLE_MINIMUM);
213  CPPUNIT_ASSERT(result.xAngle <= ANGLE_MAXIMUM);
214  CPPUNIT_ASSERT(result.yAngle >= ANGLE_MINIMUM);
215  CPPUNIT_ASSERT(result.yAngle <= ANGLE_MAXIMUM);
216  CPPUNIT_ASSERT(result.zAngle >= 0);
217  CPPUNIT_ASSERT(result.zAngle <= ANGLE_MAXIMUM);
218  CPPUNIT_ASSERT(result.xPosition >= MINIMUM);
219  CPPUNIT_ASSERT(result.xPosition <= MAXIMUM);
220  CPPUNIT_ASSERT(result.yPosition >= MINIMUM);
221  CPPUNIT_ASSERT(result.yPosition <= MAXIMUM);
222  CPPUNIT_ASSERT(result.zPosition >= MINIMUM);
223  CPPUNIT_ASSERT(result.zPosition <= MAXIMUM);
224  }
225  }
226 
227  void tearDown() override
228  {
229  m_XmlProbePointSource = "";
230  m_XmlProbeCircleSource = "";
231  m_XmlProbeRectangleSource = "";
232  m_XmlProbeTwoPointSources = "";
233  m_XmlProbeAllSources = "";
234  m_Probe = nullptr;
235  }
236 };
237 
238 MITK_TEST_SUITE_REGISTRATION(mitkMcxyzXml)
static char * line
Definition: svm.cpp:2870
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_ERROR
Definition: mitkLogMacros.h:20
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static Pointer New()
Module * GetModule() const
Test fixture for parameterized tests.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.