Shapeworks Studio  2.1
Shape analysis software suite
itkPSMRegionNeighborhoodTest.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 "itkPSMRegionNeighborhood.h"
20 #include "itkPSMRegionDomain.h"
21 #include "itkPoint.h"
22 #include "itkMacro.h"
23 
25 int itkPSMRegionNeighborhoodTest(int, char* [])
26 {
27  bool passed = true;
28  std::string errstring = "";
29  // First try 3D
30  try
31  {
32  typedef itk::Point<double, 3> PointType;
33  // Test instantiation of the object
34  itk::PSMRegionNeighborhood<3>::Pointer P = itk::PSMRegionNeighborhood<3>::New();
35  itk::PSMRegionDomain<3>::Pointer domain = itk::PSMRegionDomain<3>::New();
36  itk::PSMRegionNeighborhood<3>::PointVectorType vec;
37  const unsigned int SZ = 100;
38  PointType ptl;
39  ptl[0] = 0.0f;
40  ptl[1] = 0.0f;
41  ptl[2] = 0.0f;
42  PointType ptu;
43  ptu[0] = static_cast<double>(SZ);
44  ptu[1] = static_cast<double>(SZ);
45  ptu[2] = static_cast<double>(SZ);
46  domain->SetRegion(ptl, ptu);
47  P->SetDomain(domain);
48 
49  // Test AddPosition
50  PointType pt;
51  for (unsigned int i = 0; i < SZ; i++)
52  {
53  pt[0] = static_cast<double>(i) + 0.1f;
54  pt[1] = static_cast<double>(i) + 0.2f;
55  pt[2] = static_cast<double>(i) + 0.3f;
56  P->AddPosition(pt, i);
57  }
58  pt[0] = 0.0;
59  pt[1] = 0.0;
60  pt[2] = 0.0;
61  vec = P->FindNeighborhoodPoints(pt, static_cast<double>(SZ*SZ*SZ));
62  if (vec.size() != SZ)
63  {
64  passed = false;
65  errstring += std::string("AddPosition 3D method failed. ");
66  }
67 
68  // Test SetPosition
69  pt[0] = 1.1f;
70  pt[1] = 1.2f;
71  pt[2] = 1.3f;
72  P->SetPosition(pt, SZ/2);
73  pt[0] += 0.1;
74  vec = P->FindNeighborhoodPoints(pt, static_cast<double>(SZ*SZ*SZ));
75  if (vec.size() != SZ)
76  {
77  passed = false;
78  errstring += std::string("SetPosition 3D method failed. ");
79  }
80 
81  // Test FindNeighborhoodPoints
82  vec = P->FindNeighborhoodPoints(pt, 0.5);
83  if (vec.size() != 2)
84  {
85  passed = false;
86  errstring += std::string("FindNeighborhoodPoints 3D size failed . ");
87  }
88  else if ((vec[0].Index != 1 && vec[1].Index != 1) ||
89  (vec[0].Index != SZ/2 && vec[1].Index != SZ/2))
90  {
91  passed = false;
92  errstring += std::string("FindNeightborhoodPoints 3D index failed. ");
93 
94  }
95 
96  // Test RemovePosition
97  P->RemovePosition(SZ/2);
98  vec = P->FindNeighborhoodPoints(pt, 0.5);
99  if (vec.size() != 1)
100  {
101  passed = false;
102  errstring += std::string("RemovePoints 3D method failed. ");
103  }
104 
105  }
106  catch(itk::ExceptionObject &e)
107  {
108  errstring = "ITK exception with description: " + std::string(e.GetDescription())
109  + std::string("\n at location:") + std::string(e.GetLocation())
110  + std::string("\n in file:") + std::string(e.GetFile());
111  passed = false;
112  }
113  catch(...)
114  {
115  errstring = "Unknown exception thrown";
116  passed = false;
117  }
118 
119  // Next try 2D
120  try
121  {
122  typedef itk::Point<double, 2> PointType;
123  // Test instantiation of the object
124  itk::PSMRegionNeighborhood<2>::Pointer P = itk::PSMRegionNeighborhood<2>::New();
125  itk::PSMRegionDomain<2>::Pointer domain = itk::PSMRegionDomain<2>::New();
126  itk::PSMRegionNeighborhood<2>::PointVectorType vec;
127  const unsigned int SZ = 100;
128  PointType ptl;
129  ptl[0] = 0.0f;
130  ptl[1] = 0.0f;
131  PointType ptu;
132  ptu[0] = static_cast<double>(SZ);
133  ptu[1] = static_cast<double>(SZ);
134  domain->SetRegion(ptl, ptu);
135  P->SetDomain(domain);
136 
137  // Test AddPosition
138  PointType pt;
139  for (unsigned int i = 0; i < SZ; i++)
140  {
141  pt[0] = static_cast<double>(i) + 0.1f;
142  pt[1] = static_cast<double>(i) + 0.2f;
143  P->AddPosition(pt, i);
144  }
145  pt[0] = 0.0;
146  pt[1] = 0.0;
147  vec = P->FindNeighborhoodPoints(pt, static_cast<double>(SZ*SZ));
148  if (vec.size() != SZ)
149  {
150  passed = false;
151  errstring += std::string("AddPosition 2D method failed. ");
152  }
153 
154  // Test SetPosition
155  pt[0] = 1.1f;
156  pt[1] = 1.2f;
157  P->SetPosition(pt, SZ/2);
158  pt[0] += 0.1;
159  vec = P->FindNeighborhoodPoints(pt, static_cast<double>(SZ*SZ));
160  if (vec.size() != SZ)
161  {
162  passed = false;
163  errstring += std::string("SetPosition 2D method failed. ");
164  }
165 
166  // Test FindNeighborhoodPoints
167  vec = P->FindNeighborhoodPoints(pt, 0.5);
168  if (vec.size() != 2)
169  {
170  passed = false;
171  errstring += std::string("FindNeighborhoodPoints 2D size failed . ");
172  }
173  else if ((vec[0].Index != 1 && vec[1].Index != 1) ||
174  (vec[0].Index != SZ/2 && vec[1].Index != SZ/2))
175  {
176  passed = false;
177  errstring += std::string("FindNeightborhoodPoints 2D index failed. ");
178 
179  }
180 
181  // Test RemovePosition
182  P->RemovePosition(SZ/2);
183  vec = P->FindNeighborhoodPoints(pt, 0.5);
184  if (vec.size() != 1)
185  {
186  passed = false;
187  errstring += std::string("RemovePoints 2D method failed. ");
188  }
189 
190  }
191  catch(itk::ExceptionObject &e)
192  {
193  errstring = "ITK exception with description: " + std::string(e.GetDescription())
194  + std::string("\n at location:") + std::string(e.GetLocation())
195  + std::string("\n in file:") + std::string(e.GetFile());
196  passed = false;
197  }
198  catch(...)
199  {
200  errstring = "Unknown exception thrown";
201  passed = false;
202  }
203 
204 
205  if (passed)
206  {
207  std::cout << "All tests passed" << std::endl;
208  return EXIT_SUCCESS;
209  }
210  else
211  {
212  std::cout << "Test failed with the following error:" << std::endl;
213  std::cout << errstring << std::endl;
214  return EXIT_FAILURE;
215  }
216 }
void AddPosition(const PointType &p, unsigned int idx, int threadId=0)
virtual void SetDomain(DomainType *p)
virtual PointVectorType FindNeighborhoodPoints(const PointType &, double) const
void SetRegion(const PointType &l, const PointType &u)