Shapeworks Studio  2.1
Shape analysis software suite
itkPSMTwoCostFunctionTest.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 "itkPSMTwoCostFunction.h"
20 #include "itkMacro.h"
21 
22 namespace itk {
23 
24 template <unsigned int VDimension>
25 class TestTwoCostFunction : public PSMCostFunction<VDimension>
26 {
27 public:
30  typedef SmartPointer<Self> Pointer;
31  typedef SmartPointer<const Self> ConstPointer;
32  typedef PSMCostFunction<VDimension> Superclass;
33  itkTypeMacro( TestTwoCostFunction, 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 = m_Value;
57  energy = m_Value;
58  VectorType a;
59  a.fill(m_Value);
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  void SetValue(double v) {m_Value = v;}
83  double GetValue() const {return m_Value;}
84 
85 protected:
86  TestTwoCostFunction() : m_Value(0.0f) {}
87  virtual ~TestTwoCostFunction() {}
88  void operator=(const TestTwoCostFunction &);
90 
91  double m_Value;
92 };
93 
94 } //end namespace
95 
97 int itkPSMTwoCostFunctionTest(int, char* [] )
98 {
99  bool passed = true;
100  std::string errstring = "";
101  try
102  {
103  double aval = 1.0f;
104  double bval = 2.0f;
105 
109  avec.fill(aval);
110  bvec.fill(bval);
111 
112  itk::TestTwoCostFunction<3>::Pointer cfa = itk::TestTwoCostFunction<3>::New();
113  cfa->SetValue(aval);
114  itk::TestTwoCostFunction<3>::Pointer cfb = itk::TestTwoCostFunction<3>::New();
115  cfb->SetValue(bval);
116  itk::PSMParticleSystem<3>::Pointer ps = itk::PSMParticleSystem<3>::New();
117 
118  itk::PSMTwoCostFunction<3>::Pointer P = itk::PSMTwoCostFunction<3>::New();
119  P->SetFunctionA(cfa);
120  P->SetFunctionB(cfb);
121  P->SetParticleSystem(ps);
122 
123  // Test the evaluate method
124  double maxtimestep = aval;
125  double energy = aval;
126  res = P->Evaluate(0,0,ps,maxtimestep,energy);
127  // std::cout << "res = " << res << std::endl;
128  // std::cout << "maxtimestep = " << maxtimestep << std::endl;
129  // std::cout << "energy = " << energy << std::endl;
130 
131  // Both A and B functions should be enabled by default.
132  // Relative scaling should be 1.
133  if ( res != avec + bvec )
134  {
135  passed = false;
136  errstring = "Problem calling the Evaluate function";
137  }
138 
139  // Turn B function off
140  P->SetBOff();
141  res = P->Evaluate(0,0,ps,maxtimestep,energy);
142  if ( res != avec )
143  {
144  passed = false;
145  errstring = "Problem disabling B function";
146  }
147 
148  // Turn B function on and A function off
149  P->SetBOn();
150  P->SetAOff();
151  res = P->Evaluate(0,0,ps,maxtimestep,energy);
152  if ( res != bvec )
153  {
154  passed = false;
155  errstring = "Problem re-enabling B function or disabling A function";
156  }
157 
158  // Turn both functions on and scale the B part
159  P->SetBOn();
160  P->SetAOn();
161  P->SetRelativeGradientScaling(2.0);
162  res = P->Evaluate(0,0,ps,maxtimestep,energy);
163  if ( res != avec + (bvec * 2.0) )
164  {
165  passed = false;
166  errstring = "Problem calling the Evaluate function";
167  }
168 
169  // TEST ENERGY METHODS
170 
171  }
172  catch(itk::ExceptionObject &e)
173  {
174  errstring = "ITK exception with description: " + std::string(e.GetDescription())
175  + std::string("\n at location:") + std::string(e.GetLocation())
176  + std::string("\n in file:") + std::string(e.GetFile());
177  passed = false;
178  }
179  catch(...)
180  {
181  errstring = "Unknown exception thrown";
182  passed = false;
183  }
184 
185  if (passed)
186  {
187  std::cout << "All tests passed" << std::endl;
188  return EXIT_SUCCESS;
189  }
190  else
191  {
192  std::cout << "Test failed with the following error:" << std::endl;
193  std::cout << errstring << std::endl;
194  return EXIT_FAILURE;
195  }
196 }
itkStaticConstMacro(Dimension, unsigned int, VDimension)
virtual VectorType Evaluate(unsigned int, unsigned int, const ParticleSystemType *, double &maxtimestep, double &energy) const
virtual VectorType Evaluate(unsigned int idx, unsigned int d, const ParticleSystemType *system, double &maxmove) const
A facade class that manages interactions with a particle system.
virtual void SetParticleSystem(ParticleSystemType *p)
vnl_vector_fixed< double, VDimension > VectorType
virtual VectorType Evaluate(unsigned int a, unsigned int b, const ParticleSystemType *c, double &d) const
PSMParticleSystem< VDimension > ParticleSystemType
Superclass::ParticleSystemType ParticleSystemType
Definition: Shape.h:14