Shapeworks Studio  2.1
Shape analysis software suite
itkPSMPointTreeTest.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 "itkPSMPointTree.h"
20 #include "itkPoint.h"
21 #include "itkMacro.h"
22 
24 int itkPSMPointTreeTest(int, char* [])
25 {
26  typedef itk::Point<double, 3> PointType;
27  const unsigned int SZ = 10;
28 
29  bool passed = true;
30  std::string errstring = "";
31 
32  try
33  {
34  // Test instantiation of the object
35  itk::PSMPointTree<3>::Pointer P = itk::PSMPointTree<3>::New();
36 
37  // Construct a tree with the bounds (0,0,0) - (SZ,SZ,SZ) and 3 levels.
38  PointType lb, ub, pt;
39  lb[0] = 0.0f; lb[1] = 0.0f; lb[2] = 0.0f;
40  ub[0] = ub[1] = ub[2] = static_cast<double>(SZ);
41 
42  P->ConstructTree(lb,ub,3);
43 
44  // Add some points to the tree. Spread them out uniformly.
45  for (unsigned int x = 1; x < SZ; x++)
46  {
47  for (unsigned int y = 1; y < SZ; y++)
48  {
49  for (unsigned int z = 1; z < SZ; z++)
50  {
51  pt[0] = static_cast<double>(x);
52  pt[1] = static_cast<double>(y);
53  pt[2] = static_cast<double>(z);
54 
55  // Add position with Domain = 0
56  P->AddPoint(pt,0);
57  }
58  }
59  }
60 
61  // Test getting the points in a given region
62  ub[0] = static_cast<double>(SZ/2);
63  ub[1] = static_cast<double>(SZ/2);
64  ub[2] = static_cast<double>(SZ/2);
65 
66  itk::PSMPointTree<3>::PointIteratorListType l = P->FindPointsInRegion(lb,ub);
68  it != l.end(); it++)
69  {
70  // All points should be <= SZ/2,SZ/2,SZ/2; We must be in the 1st quadrant.
71  if ( (*it)->Point[0] > ub[0] || (*it)->Point[1] > ub[1] || (*it)->Point[2] > ub[2] )
72  {
73  passed = false;
74  errstring = "Points were sorted into the wrong tree bin.";
75  }
76  }
77  }
78  catch(itk::ExceptionObject &e)
79  {
80  errstring = "ITK exception with description: " + std::string(e.GetDescription())
81  + std::string("\n at location:") + std::string(e.GetLocation())
82  + std::string("\n in file:") + std::string(e.GetFile());
83  passed = false;
84  }
85  catch(...)
86  {
87  errstring = "Unknown exception thrown";
88  passed = false;
89  }
90 
91  if (passed)
92  {
93  std::cout << "All tests passed" << std::endl;
94  return EXIT_SUCCESS;
95  }
96  else
97  {
98  std::cout << "Test failed with the following error:" << std::endl;
99  std::cout << errstring << std::endl;
100  return EXIT_FAILURE;
101  }
102 }
PointListType::iterator AddPoint(const PointType &, unsigned int, NodePointerType &)
void ConstructTree(const PointType &, const PointType &, unsigned int)
PointIteratorListType FindPointsInRegion(const PointType &, const PointType &) const