Shapeworks Studio  2.1
Shape analysis software suite
itkPSMGradientDescentOptimizerTest.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 "itkMacro.h"
20 #include "itkPSMGradientDescentOptimizer.h"
21 #include "itkPSMShapeEntropyFunction.h"
22 #include "itkPSMRegionNeighborhood.h"
23 #include "itkPSMRegionDomain.h"
24 
26 int itkPSMGradientDescentOptimizerTest(int, char* [] )
27 {
28  bool passed = true;
29  std::string errstring = "";
30 
31  try
32  {
33  itk::PSMGradientDescentOptimizer<float, 3>::Pointer P
35 
36  typedef itk::Point<double, 3> PointType;
37 
38  itk::PSMShapeEntropyFunction<3>::Pointer cf
40  itk::PSMShapeMatrixAttribute<double, 3>::Pointer sm =
42  sm->SetDomainsPerShape(1);
43  cf->SetShapeMatrix(sm);
44 
45  // setup particle system
46  itk::PSMParticleSystem<3>::Pointer ps = itk::PSMParticleSystem<3>::New();
47  ps->RegisterAttribute(sm);
48 
49  // fill in particle system with points
50  itk::PSMRegionNeighborhood<3>::Pointer nb1 = itk::PSMRegionNeighborhood<3>::New();
51  itk::PSMRegionNeighborhood<3>::Pointer nb2 = itk::PSMRegionNeighborhood<3>::New();
52  itk::PSMRegionDomain<3>::Pointer d1 = itk::PSMRegionDomain<3>::New();
53  itk::PSMRegionDomain<3>::Pointer d2 = itk::PSMRegionDomain<3>::New();
54 
55  // Define bounding box
56  const unsigned int SZ = 100;
57  PointType ptl, ptu;
58  ptl[0] = 0.0f; ptl[1] = 0.0f; ptl[2] = 0.0f;
59  ptu[0] = static_cast<double>(SZ); ptu[1] = static_cast<double>(SZ); ptu[2] = static_cast<double>(SZ);
60 
61  // Add domains and neighborhoods
62  d1->SetRegion(ptl, ptu);
63  d2->SetRegion(ptl, ptu);
64  ps->AddDomain(d1);
65  ps->AddDomain(d2);
66  ps->SetNeighborhood(0, nb1);
67  ps->SetNeighborhood(1, nb2);
68 
69  // Add position
70  PointType pt;
71  for (unsigned int i = 0; i < SZ; i++)
72  {
73  pt[0] = static_cast<double>(i) + 0.1f;
74  pt[1] = static_cast<double>(i) + 0.2f;
75  pt[2] = static_cast<double>(i) + 0.3f;
76  ps->AddPosition(pt, 0);
77  ps->AddPosition(pt, 1);
78  }
79 
80  // Setup optimizer
81  P->SetCostFunction(cf);
82  P->SetParticleSystem(ps);
83 
84  // FIXME: We should probably run optimization here!
85  //P->StartOptimization();
86 
87  }
88  catch(itk::ExceptionObject &e)
89  {
90  errstring = "ITK exception with description: " + std::string(e.GetDescription())
91  + std::string("\n at location:") + std::string(e.GetLocation())
92  + std::string("\n in file:") + std::string(e.GetFile());
93  passed = false;
94  }
95  catch(...)
96  {
97  errstring = "Unknown exception thrown";
98  passed = false;
99  }
100 
101  if (passed)
102  {
103  std::cout << "All tests passed" << std::endl;
104  return EXIT_SUCCESS;
105  }
106  else
107  {
108  std::cout << "Test failed with the following error:" << std::endl;
109  std::cout << errstring << std::endl;
110  return EXIT_FAILURE;
111  }
112 }
Each column describes a shape. A shape may be composed of m_DomainsPerShape domains (default 1)...
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)
void SetShapeMatrix(ShapeMatrixType *s)
A facade class that manages interactions with a particle system.
void SetRegion(const PointType &l, const PointType &u)