Shapeworks Studio  2.1
Shape analysis software suite
itkPSMTwoCostFunction.h
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 #ifndef __itkParticleTwoCostFunction_h
19 #define __itkParticleTwoCostFunction_h
20 
21 #include "itkPSMCostFunction.h"
22 
23 namespace itk
24 {
25 
50 template <unsigned int VDimension>
51 class PSMTwoCostFunction : public PSMCostFunction<VDimension>
52 {
53 public:
56  typedef SmartPointer<Self> Pointer;
57  typedef SmartPointer<const Self> ConstPointer;
58  typedef PSMCostFunction<VDimension> Superclass;
59  itkTypeMacro( PSMTwoCostFunction, PSMCostFunction);
60 
63 
66 
68  itkNewMacro(Self);
69 
71  itkStaticConstMacro(Dimension, unsigned int, VDimension);
72 
77  virtual VectorType Evaluate(unsigned int idx, unsigned int d,
78  const ParticleSystemType *system,
79  double &maxmove) const;
80  virtual VectorType Evaluate(unsigned int idx, unsigned int d,
81  const ParticleSystemType *system,
82  double &maxmove, double &energy) const;
83  virtual double Energy(unsigned int idx, unsigned int d,
84  const ParticleSystemType *system) const;
85 
87  virtual void BeforeEvaluate(unsigned int idx, unsigned int d,
88  const ParticleSystemType *system)
89  {
90  if (m_AOn) m_FunctionA->BeforeEvaluate(idx, d, system);
91  if (m_BOn) m_FunctionB->BeforeEvaluate(idx, d, system);
92  }
93 
96  virtual void AfterIteration()
97  {
98  if (m_AOn) m_FunctionA->AfterIteration();
99  if (m_BOn) m_FunctionB->AfterIteration();
100  }
101 
104  virtual void BeforeIteration()
105  {
106  if (m_AOn) m_FunctionA->BeforeIteration();
107  if (m_BOn) m_FunctionB->BeforeIteration();
108 
109  m_AverageGradMagA = 0.0;
110  m_AverageGradMagB = 0.0;
111 
112  m_AverageEnergyA = 0.0;
113  m_AverageEnergyB = 0.0;
114 
115  m_Counter = 0.0;
116  }
117 
120  virtual void SetParticleSystem( ParticleSystemType *p)
121  {
123  if (m_FunctionA.GetPointer() != 0) m_FunctionA->SetParticleSystem(p);
124  if (m_FunctionB.GetPointer() != 0) m_FunctionB->SetParticleSystem(p);
125  }
126 
127  void SetDomainNumber( unsigned int i)
128  {
129  Superclass::SetDomainNumber(i);
130  if (m_FunctionA.GetPointer() != 0) m_FunctionA->SetDomainNumber(i);
131  if (m_FunctionB.GetPointer() != 0) m_FunctionB->SetDomainNumber(i);
132  }
133 
134  void SetFunctionA( PSMCostFunction<VDimension> *o)
135  {
136  m_FunctionA = o;
137  m_FunctionA->SetDomainNumber(this->GetDomainNumber());
138  m_FunctionA->SetParticleSystem(this->GetParticleSystem());
139  }
140 
141  void SetFunctionB( PSMCostFunction<VDimension> *o)
142  {
143  m_FunctionB = o;
144  m_FunctionB->SetDomainNumber(this->GetDomainNumber());
145  m_FunctionB->SetParticleSystem(this->GetParticleSystem());
146  }
147 
149  void SetAOn() { m_AOn = true; }
150  void SetAOff() { m_AOn = false; }
151  void SetAOn(bool s) { m_AOn = s; }
152  bool GetAOn() const { return m_AOn; }
153  void SetBOn() { m_BOn = true; }
154  void SetBOff() { m_BOn = false; }
155  void SetBOn(bool s) { m_BOn = s; }
156  bool GetBOn() const { return m_BOn; }
157 
161  {
162  m_RelativeEnergyScaling = r;
163  }
164  double GetRelativeEnergyScaling() const
165  {
166  return m_RelativeEnergyScaling;
167  }
168 
169  void SetRelativeGradientScaling(double r)
170  {
171  m_RelativeGradientScaling = r;
172  }
173  double GetRelativeGradientScaling() const
174  {
175  return m_RelativeGradientScaling;
176  }
177 
178  double GetAverageGradMagA() const
179  {
180  if (m_Counter != 0.0) return m_AverageGradMagA / m_Counter;
181  else return 0.0;
182  }
183  double GetAverageGradMagB() const
184  {
185  if (m_Counter != 0.0) return m_AverageGradMagB / m_Counter;
186  else return 0.0;
187  }
188 
189  double GetAverageEnergyA() const
190  {
191  if (m_Counter != 0.0) return m_AverageEnergyA / m_Counter;
192  else return 0.0;
193  }
194  double GetAverageEnergyB() const
195  {
196  if (m_Counter != 0.0) return m_AverageEnergyB / m_Counter;
197  else return 0.0;
198  }
199 
200 protected:
201  PSMTwoCostFunction() : m_AOn(true), m_BOn(true),
202  m_RelativeGradientScaling(1.0), m_RelativeEnergyScaling(1.0) {}
203 
204  virtual ~PSMTwoCostFunction() {}
205  void operator=(const PSMTwoCostFunction &);
207 
208  bool m_AOn;
209  bool m_BOn;
210  double m_RelativeGradientScaling;
211  double m_RelativeEnergyScaling;
212  double m_AverageGradMagA;
213  double m_AverageGradMagB;
214  double m_AverageEnergyA;
215  double m_AverageEnergyB;
216  double m_Counter;
217 
218  typename PSMCostFunction<VDimension>::Pointer m_FunctionA;
219  typename PSMCostFunction<VDimension>::Pointer m_FunctionB;
220 };
221 
222 } //end namespace
223 
224 #ifndef ITK_MANUAL_INSTANTIATION
225 #include "itkPSMTwoCostFunction.hxx"
226 #endif
227 
228 
229 #endif
virtual void BeforeEvaluate(unsigned int idx, unsigned int d, const ParticleSystemType *system)
Superclass::ParticleSystemType ParticleSystemType
virtual VectorType Evaluate(unsigned int idx, unsigned int d, const ParticleSystemType *system, double &maxmove) const
itkStaticConstMacro(Dimension, unsigned int, VDimension)
virtual void SetParticleSystem(ParticleSystemType *p)
virtual void SetParticleSystem(ParticleSystemType *p)
vnl_vector_fixed< double, VDimension > VectorType
Superclass::VectorType VectorType
PSMParticleSystem< VDimension > ParticleSystemType
void SetRelativeEnergyScaling(double r)