Orfeo Toolbox  3.16
itkFEMFiniteDifferenceFunctionLoad.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkFEMFiniteDifferenceFunctionLoad.txx,v $
5  Language: C++
6  Date: $Date: 2008-12-21 19:13:11 $
7  Version: $Revision: 1.14 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkFEMFiniteDifferenceFunctionLoad_txx
18 #define __itkFEMFiniteDifferenceFunctionLoad_txx
19 
21 
22 
23 namespace itk
24 {
25 namespace fem
26 {
27 
28 template<class TMoving,class TFixed>
30 {
31  m_SolutionIndex=1;
32  m_SolutionIndex2=0;
33  m_Sign=1.0;
34 
35  for (unsigned int i=0; i<ImageDimension; i++)
36  {
37  m_MetricRadius[i] = 1;
38  }
39 
40  m_DifferenceFunction=NULL;
41  m_DeformationField=NULL;
42 
43 }
44 
45 
46 template<class TMoving,class TFixed>
47 void
49 {
50 
51  typedef MeanSquareRegistrationFunctionType defaultRegistrationFunctionType;
52 
53  if (!m_DifferenceFunction)
54  {
55  typename defaultRegistrationFunctionType::Pointer drfp
56  = defaultRegistrationFunctionType::New();
57  this->SetMetric(static_cast<FiniteDifferenceFunctionType *>(drfp));
58  }
59  std::cout << " load sizes " << m_DeformationField->GetLargestPossibleRegion().GetSize()
60  << " image " << m_FixedImage->GetLargestPossibleRegion().GetSize() << std::endl;
61 
62  m_DifferenceFunction->InitializeIteration();
63 
64 }
65 
66 
67 template<class TMoving,class TFixed>
68 void
70 {
71  this->InitializeIteration();
72 }
73 
74 template<class TMoving,class TFixed>
75 void
77 {
78  if ( m_DifferenceFunction )
79  std::cout << " energy " << m_DifferenceFunction->GetEnergy() << std::endl;
80 }
81 
82 template<class TMoving,class TFixed>
83 double
85 {
86  if ( m_DifferenceFunction )
87  return m_DifferenceFunction->GetEnergy();
88  else return 0.0;
89 }
90 
91 template<class TMoving,class TFixed>
92 void
94 {
95  if ( m_DifferenceFunction ) m_DifferenceFunction->SetEnergy(e);
96 }
97 
98 template<class TMoving,class TFixed>
101 {
102  return 10.0; //FIXME
103 }
104 
105 #if __DEFINED__FIXME__THIS_IS_NEVER_REACHED_BECAUSE_OF_OVERRIDING_RETURN_STATEMENT__
106 template<class TMoving,class TFixed>
109 {
110  return 10.0; //FIXME
111  Float energy=0.0,defe=0.0;
112 
113 
114  vnl_vector_fixed<Float,2*ImageDimension> InVec(0.0);
115 
116  typename Element::VectorType ip,shapef;
117  typename Element::MatrixType solmat;
118  typename Element::Float w;
119 
120  typedef typename Element::ArrayType ArrayType;
121 
122  ArrayType::iterator elt = el->begin();
123 
124  const unsigned int Nnodes=(*elt)->GetNumberOfNodes();
125 
126  FEMVectorType Gpos,Gsol;
127  Gpos.set_size(ImageDimension); Gpos.fill(0.0);
128  Gsol.set_size(ImageDimension); Gsol.fill(0.0);
129 
130  solmat.set_size(Nnodes*ImageDimension,1);
131 
132  for(; elt!=el->end(); elt++)
133  {
134  for(unsigned int i=0; i<m_NumberOfIntegrationPoints; i++)
135  {
136  dynamic_cast<Element*>(&*(*elt))->GetIntegrationPointAndWeight(i,ip,w,m_NumberOfIntegrationPoints); // FIXME REMOVE WHEN ELEMENT NEW IS BASE CLASS
137  shapef = (*elt)->ShapeFunctions(ip);
138 
139  float solval,posval;
140  Float detJ=(*elt)->JacobianDeterminant(ip);
141 
142  for(unsigned int f=0; f<ImageDimension; f++)
143  {
144  solval=0.0;
145  posval=0.0;
146  for(unsigned int n=0; n<Nnodes; n++)
147  {
148  posval += shapef[n]*(((*elt)->GetNodeCoordinates(n))[f]);
149  float nodeval=( (m_Solution)->GetSolutionValue( (*elt)->GetNode(n)->GetDegreeOfFreedom(f) , m_SolutionIndex)
150  +(m_Solution)->GetSolutionValue( (*elt)->GetNode(n)->GetDegreeOfFreedom(f) , m_SolutionIndex2)*step);
151 
152  solval += shapef[n] * nodeval;
153  solmat[(n*ImageDimension)+f][0]=nodeval;
154  }
155  InVec[f]=posval;
156  Gpos[f]=posval;
157  InVec[f+ImageDimension]=solval;
158  Gsol[f]=solval;
159  }
160 
161  float tempe=0.0;
162  try
163  {
164  this->Fe(Gpos,Gsol); // FIXME
165  tempe=vcl_fabs(0.0);
166  }
167  catch( ... )
168  {
169  // do nothing we dont care if the metric region is outside the image
170  //std::cerr << e << std::endl;
171  }
172  for(unsigned int n=0; n<Nnodes; n++)
173  {
174  itk::fem::Element::Float temp=shapef[n]*tempe*w*detJ;
175  energy += temp;
176  }
177  }
178 
179  defe += 0.0;//(double)(*elt)->GetElementDeformationEnergy( solmat );
180  }
181 
182  //std::cout << " def e " << defe << " sim e " << energy*m_Gamma << std::endl;
183  return vcl_fabs((double)energy*(double)m_Gamma-(double)defe);
184 }
185 #endif
186 
187 template<class TMoving,class TFixed>
188 typename FiniteDifferenceFunctionLoad<TMoving , TFixed>::FEMVectorType
191  FEMVectorType Gsol)
192 {
193 
194  // We assume the vector input is of size 2*ImageDimension.
195  // The 0 to ImageDimension-1 elements contain the position, p,
196  // in the reference image. The next ImageDimension to 2*ImageDimension-1
197  // elements contain the value of the vector field at that point, v(p).
198  //
199  // Thus, we evaluate the derivative at the point p+v(p) with respect to
200  // some region of the target (fixed) image by calling the metric with
201  // the translation parameters as provided by the vector field at p.
202  //------------------------------------------------------------
203 
204  VectorType OutVec;
205  FEMVectorType femVec;
206  femVec.set_size(ImageDimension);
207  femVec.fill(0.0);
208 
209  if (!m_DifferenceFunction || !m_DeformationField || !m_FixedImage || !m_MovingImage)
210  {
211  std::cout << " initializing FE() ";
212  this->InitializeIteration();
213  std::cout << " done " << std::endl;
214  if (!m_DeformationField || !m_FixedImage || !m_MovingImage )
215  {
216  std::cout << " input data {field,fixed/moving image} are not set ";
217  return femVec;
218  }
219  std::cout << " sizes " << m_DeformationField->GetLargestPossibleRegion().GetSize()
220  << " image " << m_FixedImage->GetLargestPossibleRegion().GetSize() << std::endl;
221  }
222 
223  typedef typename TMoving::IndexType::IndexValueType OIndexValueType;
224  typename TMoving::IndexType oindex;
225 
226  unsigned int k;
227  bool inimage=true;
228  for(k = 0; k < ImageDimension; k++ )
229  {
230 
231  if ( vnl_math_isnan(Gpos[k]) || vnl_math_isinf(Gpos[k]) ||
232  vnl_math_isnan(Gsol[k]) || vnl_math_isinf(Gsol[k]) ||
233  vcl_fabs(Gpos[k]) > 1.e33 || vcl_fabs(Gsol[k]) > 1.e33 )
234  {
235  return femVec;
236  }
237  else oindex[k]=(long) (Gpos[k]+0.5);
238  if (oindex[k] > static_cast<OIndexValueType>(m_FixedSize[k]-1) || oindex[k] < 0) inimage=false;
239  // FIXME : resized images not same as vect field from expand image filter
240  // expandimagefilter does only dyadic size!!!
241 
242  }
243  if (!inimage)
244  {
245  return femVec;
246  }
247 
248 // std::cout << " index " << oindex << std::endl;
249 
250  FieldIteratorType nD(m_MetricRadius, m_DeformationField, m_DeformationField->GetLargestPossibleRegion());
251  nD.SetLocation(oindex);
252 
253  void* globalData=NULL;
254  OutVec = m_DifferenceFunction->ComputeUpdate(nD, globalData);
255 
256  for (k=0;k<ImageDimension;k++)
257  {
258  if ( vnl_math_isnan(OutVec[k]) || vnl_math_isinf(OutVec[k] )) femVec[k]=0.0;
259  else femVec[k]=OutVec[k];
260  }
261  return femVec;
262 }
263 
264 template<class TMoving,class TFixed>
266 {
267 
268  std::string clsnm = std::string("FiniteDifferenceFunctionLoad(")+typeid(TMoving).name()+","+typeid(TFixed).name()+")";
269  static const int CLID_ = FEMOF::Register( FiniteDifferenceFunctionLoad::NewB,clsnm.c_str());
270 
271  return CLID_;
272 }
273 
274 
275 template<class TMoving,class TFixed>
276 const int FiniteDifferenceFunctionLoad<TMoving,TFixed>::m_DummyCLID=FiniteDifferenceFunctionLoad<TMoving,TFixed>::CLID();
277 
278 
279 } // end namespace fem
280 } // end namespace itk
281 
282 #endif

Generated at Sat May 18 2013 23:37:56 for Orfeo Toolbox with doxygen 1.8.3.1