Shapeworks Studio  2.1
Shape analysis software suite
itkPSMParticleSystemTest.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 "itkPSMParticleSystem.h"
20 #include "itkPSMRegionNeighborhood.h"
21 #include "itkPSMRegionDomain.h"
22 #include "itkMacro.h"
23 
25 int itkPSMParticleSystemTest(int, char* [] )
26 {
27  bool passed = true;
28  std::string errstring = "";
29 
30  try
31  {
32  typedef itk::Point<double, 3> PointType;
33  itk::PSMParticleSystem<3>::Pointer P = itk::PSMParticleSystem<3>::New();
34  itk::PSMRegionNeighborhood<3>::Pointer nb1 = itk::PSMRegionNeighborhood<3>::New();
35  itk::PSMRegionNeighborhood<3>::Pointer nb2 = itk::PSMRegionNeighborhood<3>::New();
36  itk::PSMRegionDomain<3>::Pointer d1 = itk::PSMRegionDomain<3>::New();
37  itk::PSMRegionDomain<3>::Pointer d2 = itk::PSMRegionDomain<3>::New();
38 
39  // Define bounding box
40  const unsigned int SZ = 100;
41  PointType ptl, ptu;
42  ptl[0] = 0.0f; ptl[1] = 0.0f; ptl[2] = 0.0f;
43  ptu[0] = static_cast<double>(SZ); ptu[1] = static_cast<double>(SZ); ptu[2] = static_cast<double>(SZ);
44 
45  // Add domains and neighborhoods
46  d1->SetRegion(ptl, ptu);
47  d2->SetRegion(ptl, ptu);
48  P->AddDomain(d1);
49  P->AddDomain(d2);
50  P->SetNeighborhood(0, nb1);
51  P->SetNeighborhood(1, nb2);
52  if (P->GetNumberOfDomains() != 2)
53  {
54  passed = false;
55  errstring += std::string("AddDomain method failed. ");
56  }
57  // if (P->GetNeighborhood(0) != nb1 || P->GetNeighborhood(1) != nb2)
58  // {
59  // passed = false;
60  // errstring += std::string("SetNeighborhood method failed. ");
61  // }
62 
63  // Add position
64  PointType pt;
65  for (unsigned int i = 0; i < SZ; i++)
66  {
67  pt[0] = static_cast<double>(i) + 0.1f;
68  pt[1] = static_cast<double>(i) + 0.2f;
69  pt[2] = static_cast<double>(i) + 0.3f;
70  P->AddPosition(pt, 0);
71  P->AddPosition(pt, 1);
72  }
73  if (P->GetNumberOfParticles(0) != SZ || P->GetNumberOfParticles(1) != SZ)
74  {
75  passed = false;
76  errstring += std::string("AddPosition method failed. ");
77  }
78 
79 
80  // Split particles
81  P->SplitAllParticles(0.1);
82  if (P->GetNumberOfParticles(0) != SZ*2 || P->GetNumberOfParticles(1) != SZ*2)
83  {
84  passed = false;
85  errstring += std::string("SplitAllParticles method failed. ");
86  }
87 
88  for (unsigned int i = 0; i < 2; i++)
89  {
90  P->SplitParticle(0.1, 0, i);
91  if (P->GetNumberOfParticles(i) != SZ*2+1)
92  {
93  passed = false;
94  errstring += std::string("SplitParticle method failed. ");
95  }
96  }
97 
98  // Get/Set position
99  for (unsigned int i = 0; i < 2; i++)
100  {
101  PointType originalPt = P->GetPosition(0, i);
102  PointType modPt;
103  modPt[0] = originalPt[0] + 0.1;
104  modPt[1] = originalPt[1] + 0.2;
105  modPt[2] = originalPt[2] + 0.3;
106  P->SetPosition(modPt, 0, i);
107  if (P->GetPosition(0, i) != modPt)
108  {
109  passed = false;
110  errstring += std::string("Get/SetPosition methods failed. ");
111  }
112  }
113 
114  // remove position
115  for (unsigned int i = 0; i < 2; i++)
116  {
117  P->RemovePosition(0, i);
118  if (P->GetNumberOfParticles(i) != SZ*2)
119  {
120  passed = false;
121  errstring += std::string("RemovePosition method failed. ");
122  }
123  }
124 
125  // Find neighborhood
126  itk::PSMRegionNeighborhood<3>::PointVectorType vec;
127  for (unsigned int i = 0; i < 2; i++)
128  {
129  // get permuted position
130  PointType modPt = P->GetPosition(SZ/2, i);
131  modPt[0] += 0.0011;
132  vec = P->FindNeighborhoodPoints(modPt, 0.5, i);
133  if (vec.size() != 2)
134  {
135  passed = false;
136  errstring += std::string("FindNeighborhoodPoints method failed. ");
137  }
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  if (passed)
155  {
156  std::cout << "All tests passed" << std::endl;
157  return EXIT_SUCCESS;
158  }
159  else
160  {
161  std::cout << "Test failed with the following error:" << std::endl;
162  std::cout << errstring << std::endl;
163  return EXIT_FAILURE;
164  }
165 }
unsigned int GetNumberOfDomains() const
void AddDomain(DomainType *, int threadId=0)
void SetNeighborhood(unsigned int, NeighborhoodType *, int threadId=0)
PointType & GetPosition(unsigned long int k, unsigned int d=0)
const PointType & AddPosition(const PointType &, unsigned int d=0, int threadId=0)
unsigned long int GetNumberOfParticles(unsigned int d=0) const
A facade class that manages interactions with a particle system.
void SplitAllParticles(double epsilon, int threadId=0)
PointVectorType FindNeighborhoodPoints(const PointType &p, double r, unsigned int d=0) const
void SetRegion(const PointType &l, const PointType &u)