Shapeworks Studio  2.1
Shape analysis software suite
itkPSMSurfaceNeighborhoodTest.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 "itkImage.h"
20 #include "itkImageFileReader.h"
21 #include "itkPSMSurfaceNeighborhood.h"
22 #include "itkPoint.h"
23 #include "itkImage.h"
24 #include "itkMacro.h"
25 
26 
28 int itkPSMSurfaceNeighborhoodTest(int argc, char* argv[])
29 {
30  bool passed = true;
31  std::string errstring = "";
32 
33  // Check for proper arguments
34  if (argc < 2)
35  {
36  std::cout << "Wrong number of arguments. You need one image file." << std::endl;
37  return EXIT_FAILURE;
38  }
39 
40  try
41  {
42  typedef itk::Point<double, 3> PointType;
43  typedef itk::Image<float, 3> ImageType;
44 
45  // Test instantiation of the object
46  itk::PSMSurfaceNeighborhood<ImageType>::Pointer P
48 
49  // Read image file
50  itk::ImageFileReader<ImageType>::Pointer reader
51  = itk::ImageFileReader<ImageType>::New();
52  reader->SetFileName(argv[1]);
53  reader->UpdateLargestPossibleRegion();
54 
55  // Setup Domain
56  itk::PSMImplicitSurfaceDomain<float, 3>::Pointer domain
58  domain->SetImage(reader->GetOutput());
59  P->SetDomain(domain);
60 
61  // Get Bounds
62  PointType ptl = domain->GetLowerBound();
63  PointType ptu = domain->GetUpperBound();
64 
65  // Test AddPosition
66  unsigned int SZ = 100;
67  PointType pt;
68  double range[3];
69  double maxDist = 0.0;
70  for (unsigned int i = 0; i < 3; i++)
71  {
72  range[i] = ptu[i]-ptl[i];
73  maxDist += range[i]*range[i];
74  }
75 
76  for (unsigned int i = 0; i < SZ; i++)
77  {
78  for (unsigned int j = 0; j < 3; j++)
79  {
80  pt[j] = static_cast<double>(i)/static_cast<double>(SZ)*range[j]+ptl[j]+0.1;
81  }
82  P->AddPosition(pt, i);
83  }
84 
85  for (unsigned int i = 0; i < 3; i++)
86  {
87  pt[i] = range[i]/2 + ptl[i];
88  }
89  itk::PSMSurfaceNeighborhood<ImageType>::PointVectorType vec;
90  std::vector<double> weights;
91 
92  vec = P->FindNeighborhoodPointsWithWeights(pt, weights, maxDist);
93  if (vec.size() != SZ)
94  {
95  passed = false;
96  errstring += std::string("AddPosition method failed. ");
97  }
98 
99  // Test SetPosition
100  P->SetPosition(pt, SZ/4);
101  pt[0] += 0.1;
102  vec = P->FindNeighborhoodPointsWithWeights(pt, weights, maxDist);
103  if (vec.size() != SZ)
104  {
105  passed = false;
106  errstring += std::string("SetPosition method failed. ");
107  }
108 
109  // Test FindNeighborhoodPoints
110  vec = P->FindNeighborhoodPointsWithWeights(pt, weights, 0.5);
111  if (vec.size() != 2)
112  {
113  passed = false;
114  errstring += std::string("FindNeighborhoodPoints size failed. ");
115  }
116  else if ((vec[0].Index != SZ/2 && vec[1].Index != SZ/2) ||
117  (vec[0].Index != SZ/4 && vec[1].Index != SZ/4))
118  {
119  passed = false;
120  errstring += std::string("FindNeightborhoodPoints index failed. ");
121  }
122 
123  // Test RemovePosition
124  P->RemovePosition(SZ/4);
125  vec = P->FindNeighborhoodPointsWithWeights(pt, weights, 0.5);
126  if (vec.size() != 1)
127  {
128  passed = false;
129  errstring += std::string("RemovePoints method failed. ");
130  }
131  }
132  catch(itk::ExceptionObject &e)
133  {
134  errstring = "ITK exception with description: " + std::string(e.GetDescription())
135  + std::string("\n at location:") + std::string(e.GetLocation())
136  + std::string("\n in file:") + std::string(e.GetFile());
137  passed = false;
138  }
139  catch(...)
140  {
141  errstring = "Unknown exception thrown";
142  passed = false;
143  }
144 
145  if (passed)
146  {
147  std::cout << "All tests passed" << std::endl;
148  return EXIT_SUCCESS;
149  }
150  else
151  {
152  std::cout << "Test failed with the following error:" << std::endl;
153  std::cout << errstring << std::endl;
154  return EXIT_FAILURE;
155  }
156 }
PSMSurfaceNeighborhood is a general purpose neighborhood object that computes neighborhoods based on ...
void AddPosition(const PointType &p, unsigned int idx, int threadId=0)
virtual const PointType & GetLowerBound() const
virtual PointVectorType FindNeighborhoodPointsWithWeights(const PointType &, std::vector< double > &, double) const