Shapeworks Studio  2.1
Shape analysis software suite
itkPSMContainerArrayAttributeTest.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 "itkPSMContainerArrayAttribute.h"
20 #include "itkPSMParticleSystem.h"
21 #include "itkPSMRegionNeighborhood.h"
22 #include "itkPSMRegionDomain.h"
23 #include "itkMacro.h"
24 
25 namespace itk {
26 
27 template<class T, unsigned int VDimension>
29 {
30 public:
32  typedef T DataType;
35  typedef SmartPointer<Self> Pointer;
36  typedef SmartPointer<const Self> ConstPointer;
37  typedef WeakPointer<const Self> ConstWeakPointer;
38 
40  itkNewMacro(Self);
41 
44 
49  virtual void DomainAddEventCallback(Object *, const EventObject &)
50  {
51  this->resize( this->size() +1);
52  this->operator[](this->size() -1) = PSMContainer<T>::New();
53  m_AddDomainCount++;
54  }
55 
56  virtual void PositionAddEventCallback(Object *, const EventObject &e)
57  {
58  const itk::ParticlePositionAddEvent &event
59  = dynamic_cast<const itk::ParticlePositionAddEvent &>(e);
60  this->operator[](event.GetDomainIndex())->operator[](event.GetPositionIndex()) = 0.0;
61  m_AddPositionCount++;
62  }
63 
64  unsigned int GetNumberOfPositions() { return m_AddPositionCount; }
65  unsigned int GetNumberOfDomains() { return m_AddDomainCount; }
66 
67 protected:
68  TestContainerArrayAttribute() : m_AddPositionCount(0), m_AddDomainCount(0) {}
69  virtual ~TestContainerArrayAttribute() {}
70  void operator=(const TestContainerArrayAttribute &);
72 
73 private:
74  unsigned int m_AddPositionCount;
75  unsigned int m_AddDomainCount;
76 };
77 
78 } // end namespace
79 
81 int itkPSMContainerArrayAttributeTest(int, char* [] )
82 {
83  bool passed = true;
84  std::string errstring = "";
85 
86  try
87  {
88  typedef itk::Point<double, 3> PointType;
89 
90  itk::TestContainerArrayAttribute<double,3>::Pointer P = itk::TestContainerArrayAttribute<double,3>::New();
91 
92  itk::PSMParticleSystem<3>::Pointer ps = itk::PSMParticleSystem<3>::New();
93  itk::PSMRegionNeighborhood<3>::Pointer nb1 = itk::PSMRegionNeighborhood<3>::New();
94  itk::PSMRegionNeighborhood<3>::Pointer nb2 = itk::PSMRegionNeighborhood<3>::New();
95  itk::PSMRegionDomain<3>::Pointer d1 = itk::PSMRegionDomain<3>::New();
96  itk::PSMRegionDomain<3>::Pointer d2 = itk::PSMRegionDomain<3>::New();
97 
98  ps->RegisterAttribute(P);
99 
100  // Define bounding box
101  const unsigned int SZ = 100;
102  PointType ptl, ptu;
103  ptl[0] = 0.0f; ptl[1] = 0.0f; ptl[2] = 0.0f;
104  ptu[0] = static_cast<double>(SZ); ptu[1] = static_cast<double>(SZ); ptu[2] = static_cast<double>(SZ);
105 
106  // Add domains and neighborhoods
107  d1->SetRegion(ptl, ptu);
108  d2->SetRegion(ptl, ptu);
109  ps->AddDomain(d1);
110  ps->AddDomain(d2);
111  ps->SetNeighborhood(0, nb1);
112  ps->SetNeighborhood(1, nb2);
113 
114  // Add position
115  PointType pt;
116  for (unsigned int i = 0; i < SZ; i++)
117  {
118  pt[0] = static_cast<double>(i) + 0.1f;
119  pt[1] = static_cast<double>(i) + 0.2f;
120  pt[2] = static_cast<double>(i) + 0.3f;
121  ps->AddPosition(pt, 0);
122  ps->AddPosition(pt, 1);
123  }
124 
125  P->ZeroAllValues();
126  if (P->GetNumberOfPositions() != 2*SZ)
127  {
128  passed = false;
129  errstring += std::string("PositionAddEventCallback method error. ");
130  }
131  if (P->GetNumberOfDomains() != 2)
132  {
133  passed = false;
134  errstring += std::string("DomainAddEventCallback method error. ");
135  }
136  }
137  catch(itk::ExceptionObject &e)
138  {
139  errstring = "ITK exception with description: " + std::string(e.GetDescription())
140  + std::string("\n at location:") + std::string(e.GetLocation())
141  + std::string("\n in file:") + std::string(e.GetFile());
142  passed = false;
143  }
144  catch(...)
145  {
146  errstring = "Unknown exception thrown";
147  passed = false;
148  }
149 
150  if (passed)
151  {
152  std::cout << "All tests passed" << std::endl;
153  return EXIT_SUCCESS;
154  }
155  else
156  {
157  std::cout << "Test failed with the following error:" << std::endl;
158  std::cout << errstring << std::endl;
159  return EXIT_FAILURE;
160  }
161 }
itkTypeMacro(TestContainerArrayAttribute, PSMContainerArrayAttribute)
void AddDomain(DomainType *, int threadId=0)
void SetNeighborhood(unsigned int, NeighborhoodType *, int threadId=0)
void RegisterAttribute(PSMAttribute< VDimension > *)
const PointType & AddPosition(const PointType &, unsigned int d=0, int threadId=0)
A facade class that manages interactions with a particle system.
Base class for PSMParticleSystem attribute classes.
A container class that holds particle position information for the PSMParticleSystem class...
virtual void DomainAddEventCallback(Object *, const EventObject &)
void SetRegion(const PointType &l, const PointType &u)