Shapeworks Studio  2.1
Shape analysis software suite
itkPSMShapeEntropyFunction.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 applicabcle 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 __itkPSMShapeEntropyFunction_h
19 #define __itkPSMShapeEntropyFunction_h
20 
21 #include "itkPSMShapeMatrixAttribute.h"
22 #include "itkPSMCostFunction.h"
23 #include <vector>
24 
25 namespace itk
26 {
27 
43 template <unsigned int VDimension>
44 class PSMShapeEntropyFunction : public PSMCostFunction<VDimension>
45 {
46 public:
49  typedef SmartPointer<Self> Pointer;
50  typedef SmartPointer<const Self> ConstPointer;
51  typedef PSMCostFunction<VDimension> Superclass;
53 
56 
58 
59  typedef typename ShapeMatrixType::DataType DataType;
60 
64  typedef vnl_vector<DataType> vnl_vector_type;
65  typedef vnl_matrix<DataType> vnl_matrix_type;
66 
68  itkNewMacro(Self);
69 
71  itkStaticConstMacro(Dimension, unsigned int, VDimension);
72 
77  virtual VectorType Evaluate(unsigned int, unsigned int,
78  const ParticleSystemType *, double &, double&) const;
79  virtual VectorType Evaluate(unsigned int a, unsigned int b,
80  const ParticleSystemType *c, double &d) const
81  {
82  double e;
83  return this->Evaluate(a,b,c,d,e);
84  }
85 
86  virtual double Energy(unsigned int a, unsigned int b,
87  const ParticleSystemType *c) const
88  {
89  double e, d;
90  this->Evaluate(a,b,c,d,e);
91  return e;
92  }
93 
97  // void WriteModes(const std::string &, int) const;
98 
100  void SetShapeMatrix( ShapeMatrixType *s)
101  { m_ShapeMatrix = s; }
102  ShapeMatrixType *GetShapeMatrix()
103  { return m_ShapeMatrix.GetPointer(); }
104  const ShapeMatrixType *GetShapeMatrix() const
105  { return m_ShapeMatrix.GetPointer(); }
106 
108  virtual void BeforeIteration()
109  {
110  m_ShapeMatrix->BeforeIteration();
111 
112  if (m_Counter == 0)
113  {
114  this->ComputeCovarianceMatrix();
115  }
116  }
117 
119  virtual void AfterIteration()
120  {
121  m_ShapeMatrix->AfterIteration();
122  // Update the annealing parameter.
123  if (m_HoldMinimumVariance != true)
124  {
125  m_Counter ++;
126  if (m_Counter >= m_RecomputeCovarianceInterval)
127  {
128  m_Counter = 0;
129  m_MinimumVariance *= m_MinimumVarianceDecayConstant;
130  }
131  }
132  }
133 
145  void SetMinimumVariance( double d)
146  { m_MinimumVariance = d;}
147  double GetMinimumVariance() const
148  { return m_MinimumVariance; }
149 
155  void SetMinimumVarianceDecay(double initial_value, double final_value, double time_period)
156  {
157  m_MinimumVarianceDecayConstant = exp(log(final_value / initial_value) / time_period);
158  m_MinimumVariance = initial_value;
159  m_HoldMinimumVariance = false;
160  }
161 
164  {
165  return m_MinimumVarianceDecayConstant;
166  }
167 
173  { return m_HoldMinimumVariance; }
174  void SetHoldMinimumVariance(bool b)
175  { m_HoldMinimumVariance = b; }
176 
185  { m_RecomputeCovarianceInterval = i; }
186  int GetRecomputeCovarianceInterval() const
187  { return m_RecomputeCovarianceInterval; }
188 
192  const std::vector<double> &GetShapePCAVariances() const
193  {
194  return m_ShapePCAVariances;
195  }
196 
197 protected:
199  {
200  // m_MinimumVarianceBase = 1.0;//exp(log(1.0e-5)/10000.0);
201  m_HoldMinimumVariance = true;
202  m_MinimumVariance = 1.0e-5;
203  m_MinimumEigenValue = 0.0;
204  m_MinimumVarianceDecayConstant = 1.0;//log(2.0) / 50000.0;
205  m_RecomputeCovarianceInterval = 1;
206  m_Counter = 0;
207  }
208  virtual ~PSMShapeEntropyFunction() {}
209  void operator=(const PSMShapeEntropyFunction &);
211  typename ShapeMatrixType::Pointer m_ShapeMatrix;
212 
213  std::vector<double> m_ShapePCAVariances;
214 
215  virtual void ComputeCovarianceMatrix();
216  vnl_matrix_type m_PointsUpdate;
217  double m_MinimumVariance;
218  double m_MinimumEigenValue;
219  double m_CurrentEnergy;
220  bool m_HoldMinimumVariance;
221  double m_MinimumVarianceDecayConstant;
222  int m_RecomputeCovarianceInterval;
223  int m_Counter;
224 
225 };
226 
227 
228 } //end namespace
229 
230 #ifndef ITK_MANUAL_INSTANTIATION
231 #include "itkPSMShapeEntropyFunction.hxx"
232 #endif
233 
234 #endif
235 
Each column describes a shape. A shape may be composed of m_DomainsPerShape domains (default 1)...
itkStaticConstMacro(Dimension, unsigned int, VDimension)
Superclass::ParticleSystemType ParticleSystemType
void SetMinimumVarianceDecay(double initial_value, double final_value, double time_period)
void SetShapeMatrix(ShapeMatrixType *s)
virtual VectorType Evaluate(unsigned int, unsigned int, const ParticleSystemType *, double &, double &) const
const std::vector< double > & GetShapePCAVariances() const
vnl_vector_fixed< double, VDimension > VectorType
PSMParticleSystem< VDimension > ParticleSystemType
virtual VectorType Evaluate(unsigned int a, unsigned int b, const ParticleSystemType *c, double &d) const
Definition: Shape.h:14