ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkCommandVnlIterationUpdate.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkCommandVnlIterationUpdate_h
00019 #define __itkCommandVnlIterationUpdate_h
00020 
00021 #include "itkCommand.h"
00022 #include "itkWeakPointer.h"
00023 
00024 namespace itk {
00025 
00031 template < class TOptimizer >
00032 class ITK_EXPORT CommandVnlIterationUpdate : public Command
00033 {
00034 public:
00035 
00039   typedef CommandVnlIterationUpdate   Self;
00040 
00041 
00045   typedef itk::Command  Superclass;
00046 
00047 
00051   typedef itk::SmartPointer<Self>  Pointer;
00052 
00056   typedef itk::SmartPointer<const Self>  ConstPointer;
00057 
00061   void Execute(itk::Object *caller, const itk::EventObject & event)
00062   {
00063     Execute( (const itk::Object *)caller, event);
00064   }
00065 
00066   void Execute(const itk::Object * itkNotUsed(caller), const itk::EventObject & event)
00067   {
00068     if( typeid( event ) == typeid( itk::StartEvent ) )
00069       {
00070       std::cout << std::endl << "Position              Value";
00071       std::cout << std::endl << std::endl;
00072       }
00073     else if( itk::IterationEvent().CheckEvent( &event ) )
00074       {
00075       std::cout << m_Optimizer->GetCurrentIteration() << " = ";
00076       std::cout << m_Optimizer->GetCurrentPosition() << std::endl;
00077       }
00078     else if( typeid( event ) == typeid( itk::EndEvent ) )
00079       {
00080       std::cout << std::endl << std::endl;
00081       std::cout << "After " << m_Optimizer->GetCurrentIteration();
00082       std::cout << "  iterations " << std::endl;
00083       std::cout << "Solution is    = " << m_Optimizer->GetCurrentPosition();
00084       std::cout << std::endl;
00085       std::cout << "vnl report = " << std::endl;
00086       m_Optimizer->GetOptimizer()->diagnose_outcome( std::cout );
00087       }
00088 
00089   }
00090 
00091 
00095   itkTypeMacro( CommandVnlIterationUpdate, ::itk::Command );
00096 
00097 
00101   itkNewMacro( Self );
00102 
00103 
00107   typedef    TOptimizer     OptimizerType;
00108 
00109 
00113   void SetOptimizer( OptimizerType * optimizer )
00114     {
00115     m_Optimizer = optimizer;
00116     m_Optimizer->AddObserver( itk::IterationEvent(), this );
00117     }
00119 
00120 protected:
00121 
00125   CommandVnlIterationUpdate() {};
00126 
00127 private:
00128 
00132   WeakPointer<OptimizerType>   m_Optimizer;
00133 };
00134 
00135 
00136 } // end namespace itk
00137 
00138 
00139 #endif
00140