Shapeworks Studio  2.1
Shape analysis software suite
itkPSMContainerTest.cxx
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #include <iostream>
19 #include "itkPoint.h"
20 #include "itkMacro.h"
21 #include "itkPSMContainer.h"
22 
24 int itkPSMContainerTest(int, char* [] )
25 {
26  bool passed = true;
27  std::string errstring = "";
28 
29  const unsigned int SZ = 100;
30  // First do 3D Point
31  try
32  {
33  // Define the type of point (particle position) stored in this
34  // container.
35  typedef itk::Point<double, 3> PointType;
36  itk::PSMContainer<PointType>::Pointer P = itk::PSMContainer<PointType>::New();
37  PointType pt;
38 
39  // Test storing values
40  for (unsigned int i = 0; i < SZ; i++)
41  {
42  pt[0] = static_cast<double>(i) + 0.1f;
43  pt[1] = static_cast<double>(i) + 0.2f;
44  pt[2] = static_cast<double>(i) + 0.3f;
45 
46  P->operator[](i) = pt;
47  }
48 
49  // Test Size method
50  if (P->GetSize() != SZ)
51  {
52  passed = false;
53  errstring += std::string("GetSize method failed");
54  }
55 
56  // Test HasIndex method
57  if (P->HasIndex(0) != true || P->HasIndex(SZ+1) != false)
58  {
59  passed = false;
60  errstring += std::string("HasIndex method failed");
61  }
62 
63  // Test accessor methods
64  if (P->operator[](SZ-1)[0] != static_cast<double>(SZ-1) + 0.1f ||
65  P->operator[](SZ-1)[1] != static_cast<double>(SZ-1) + 0.2f ||
66  P->operator[](SZ-1)[2] != static_cast<double>(SZ-1) + 0.3f)
67  {
68  passed = false;
69  errstring += std::string("Accessor method failed or values were stored incorrectly.");
70  }
71 
72  // Test Erase method
73  P->Erase(SZ /2);
74  if (P->HasIndex(SZ/2) == true)
75  {
76  passed = false;
77  errstring += std::string("Erase method failed");
78  }
79 
80  }
81  catch(itk::ExceptionObject &e)
82  {
83  errstring = "ITK exception with description: " + std::string(e.GetDescription())
84  + std::string("\n at location:") + std::string(e.GetLocation())
85  + std::string("\n in file:") + std::string(e.GetFile());
86  passed = false;
87  }
88  catch(...)
89  {
90  errstring = "Unknown exception thrown";
91  passed = false;
92  }
93 
94  // Next do 2D Point
95  try
96  {
97  typedef itk::Point<double, 2> PointType;
98  itk::PSMContainer<PointType>::Pointer P = itk::PSMContainer<PointType>::New();
99  PointType pt;
100 
101  // Test storing values
102  for (unsigned int i = 0; i < SZ; i++)
103  {
104  pt[0] = static_cast<double>(i) + 0.1f;
105  pt[1] = static_cast<double>(i) + 0.2f;
106 
107  P->operator[](i) = pt;
108  }
109 
110  // Test Size method
111  if (P->GetSize() != SZ)
112  {
113  passed = false;
114  errstring += std::string("GetSize method failed");
115  }
116 
117  // Test HasIndex method
118  if (P->HasIndex(0) != true || P->HasIndex(SZ+1) != false)
119  {
120  passed = false;
121  errstring += std::string("HasIndex method failed");
122  }
123 
124  // Test accessor methods
125  if (P->operator[](SZ-1)[0] != static_cast<double>(SZ-1) + 0.1f ||
126  P->operator[](SZ-1)[1] != static_cast<double>(SZ-1) + 0.2f)
127  {
128  passed = false;
129  errstring += std::string("Accessor method failed or values were stored incorrectly.");
130  }
131 
132  // Test Erase method
133  P->Erase(SZ /2);
134  if (P->HasIndex(SZ/2) == true)
135  {
136  passed = false;
137  errstring += std::string("Erase method failed");
138  }
139 
140  }
141  catch(itk::ExceptionObject &e)
142  {
143  errstring = "ITK exception with description: " + std::string(e.GetDescription())
144  + std::string("\n at location:") + std::string(e.GetLocation())
145  + std::string("\n in file:") + std::string(e.GetFile());
146  passed = false;
147  }
148  catch(...)
149  {
150  errstring = "Unknown exception thrown";
151  passed = false;
152  }
153 
154 
155 
156  if (passed)
157  {
158  std::cout << "All tests passed" << std::endl;
159  return EXIT_SUCCESS;
160  }
161  else
162  {
163  std::cout << "Test failed with the following error:" << std::endl;
164  std::cout << errstring << std::endl;
165  return EXIT_FAILURE;
166  }
167 }
MapType::size_type Erase(const unsigned int &k)
bool HasIndex(unsigned long int k) const
unsigned long int GetSize() const
A container class that holds particle position information for the PSMParticleSystem class...