Shapeworks Studio  2.1
Shape analysis software suite
itkPSMCostFunctionTest.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 "itkPSMCostFunction.h"
20 #include "itkMacro.h"
21 
22 namespace itk {
23 
24 template <unsigned int VDimension>
25 class TestCostFunction : public PSMCostFunction<VDimension>
26 {
27 public:
30  typedef SmartPointer<Self> Pointer;
31  typedef SmartPointer<const Self> ConstPointer;
32  typedef PSMCostFunction<VDimension> Superclass;
33  itkTypeMacro( TestCostFunction, PSMCostFunction);
34 
37 
41 
43  itkNewMacro(Self);
44 
46  itkStaticConstMacro(Dimension, unsigned int, VDimension);
47 
52  virtual VectorType Evaluate(unsigned int, unsigned int,
53  const ParticleSystemType *, double &maxtimestep,
54  double &energy) const
55  {
56  maxtimestep = 1.0f;
57  energy = 1.0f;
58  VectorType a;
59  a.fill(1.0f);
60  return a;
61  }
62  virtual VectorType Evaluate(unsigned int a, unsigned int b,
63  const ParticleSystemType *c, double &d) const
64  {
65  double e;
66  return this->Evaluate(a,b,c,d,e);
67  }
68  virtual double Energy(unsigned int a, unsigned int b,
69  const ParticleSystemType *c) const
70  {
71  double e, d;
72  this->Evaluate(a,b,c,d,e);
73  return e;
74  }
75 
77  virtual void BeforeIteration() {}
78 
80  virtual void AfterIteration() {}
81 
82 protected:
84  { }
85  virtual ~TestCostFunction() {}
86  void operator=(const TestCostFunction &);
88 };
89 
90 
91 } //end namespace
92 
94 int itkPSMCostFunctionTest(int, char* [] )
95 {
96  bool passed = true;
97  std::string errstring = "";
98 
99  try
100  {
101  itk::TestCostFunction<3>::Pointer p = itk::TestCostFunction<3>::New();
103  double maxtimestep = 0.0f;
104  double energy = 0.0f;
105  a.fill(1.0f);
106 
107  itk::PSMParticleSystem<3>::Pointer ps = itk::PSMParticleSystem<3>::New();
108 
109  if (a != p->Evaluate(0,0,ps,maxtimestep,energy) || maxtimestep != 1.0f || energy != 1.0f)
110  {
111  passed = false;
112  errstring = "Problem calling the Evaluate function";
113  }
114  }
115  catch(itk::ExceptionObject &e)
116  {
117  errstring = "ITK exception with description: " + std::string(e.GetDescription())
118  + std::string("\n at location:") + std::string(e.GetLocation())
119  + std::string("\n in file:") + std::string(e.GetFile());
120  passed = false;
121  }
122  catch(...)
123  {
124  errstring = "Unknown exception thrown";
125  passed = false;
126  }
127 
128  if (passed)
129  {
130  std::cout << "All tests passed" << std::endl;
131  return EXIT_SUCCESS;
132  }
133  else
134  {
135  std::cout << "Test failed with the following error:" << std::endl;
136  std::cout << errstring << std::endl;
137  return EXIT_FAILURE;
138  }
139 }
virtual VectorType Evaluate(unsigned int, unsigned int, const ParticleSystemType *, double &maxtimestep, double &energy) const
Superclass::ParticleSystemType ParticleSystemType
virtual VectorType Evaluate(unsigned int a, unsigned int b, const ParticleSystemType *c, double &d) const
A facade class that manages interactions with a particle system.
Superclass::VectorType VectorType
vnl_vector_fixed< double, VDimension > VectorType
itkStaticConstMacro(Dimension, unsigned int, VDimension)
PSMParticleSystem< VDimension > ParticleSystemType
Definition: Shape.h:14