From 9095ef398d399ce190d3907daa5383210ffa08b7 Mon Sep 17 00:00:00 2001
From: Aurelien Bricier <aurelien.bricier@c-s.fr>
Date: Mon, 8 Nov 2010 17:22:48 +0100
Subject: [PATCH] ENH: added new ImageFunctionAdaptor class

---
 .../otbImageFunctionAdaptor.h                 | 122 ++++++++++++++++++
 .../otbImageFunctionAdaptor.txx               |  58 +++++++++
 Testing/Code/FeatureExtraction/CMakeLists.txt |  10 +-
 .../otbFeatureExtractionTests16.cxx           |   4 +-
 4 files changed, 187 insertions(+), 7 deletions(-)
 create mode 100644 Code/FeatureExtraction/otbImageFunctionAdaptor.h
 create mode 100644 Code/FeatureExtraction/otbImageFunctionAdaptor.txx

diff --git a/Code/FeatureExtraction/otbImageFunctionAdaptor.h b/Code/FeatureExtraction/otbImageFunctionAdaptor.h
new file mode 100644
index 0000000000..bf392fddfc
--- /dev/null
+++ b/Code/FeatureExtraction/otbImageFunctionAdaptor.h
@@ -0,0 +1,122 @@
+/*=========================================================================
+
+  Program:   ORFEO Toolbox
+  Language:  C++
+  Date:      $Date$
+  Version:   $Revision$
+
+
+  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
+  See OTBCopyright.txt for details.
+
+
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+
+=========================================================================*/
+#ifndef __otbImageFunctionAdaptor_h
+#define __otbImageFunctionAdaptor_h
+
+#include "itkImageFunction.h"
+#include "itkNumericTraits.h"
+
+#include "otbVariableLengthVectorConverter.h"
+
+
+#include <complex>
+
+namespace otb
+{
+
+
+template< class TInternalImageFunctionType >
+class ITK_EXPORT ImageFunctionAdaptor :
+    public itk::ImageFunction< typename TInternalImageFunctionType::InputImageType,
+                               itk::VariableLengthVector<
+                               ITK_TYPENAME itk::NumericTraits<typename TInternalImageFunctionType::InputImageType::PixelType>
+                               ::RealType >,
+                               typename TInternalImageFunctionType::CoordRepType >
+{
+  public:
+  // Standard class typedefs. //
+  typedef ImageFunctionAdaptor                                          Self;
+  typedef itk::ImageFunction< typename TInternalImageFunctionType::InputImageType,
+                              itk::VariableLengthVector<
+                              ITK_TYPENAME itk::NumericTraits<typename TInternalImageFunctionType::InputImageType::PixelType>
+                              ::RealType >,
+                              typename TInternalImageFunctionType::CoordRepType >
+                                                                        Superclass;
+  typedef itk::SmartPointer<Self>                                       Pointer;
+  typedef itk::SmartPointer<const Self>                                 ConstPointer;
+
+  // Run-time type information (and related methods). //
+  itkTypeMacro(ImageFunctionAdaptor, ImageFunction);
+
+  // Method for creation through the object factory. //
+  itkNewMacro(Self);
+
+  // InputImageType typedef support. //
+  typedef typename TInternalImageFunctionType::InputImageType  InputImageType;
+  typedef typename TInternalImageFunctionType::CoordRepType    CoordRepType;
+  typedef typename Superclass::IndexType                       IndexType;
+  typedef typename Superclass::ContinuousIndexType             ContinuousIndexType;
+  typedef typename Superclass::PointType                       PointType;
+  typedef typename Superclass::OutputType                      OutputType;
+  typedef typename OutputType::ValueType                       OutputValueType;
+  
+  // InternalImageFunction related typedefs //
+  typedef TInternalImageFunctionType                        InternalImageFunctionType;
+  typedef typename InternalImageFunctionType::OutputType    InternalImageFunctionOutputType;
+
+  // Converter related typedefs //
+  typedef VariableLengthVectorConverter<InternalImageFunctionOutputType, double> ConverterType;
+
+
+  // Dimension of the underlying image. //
+  itkStaticConstMacro(ImageDimension, unsigned int, InputImageType::ImageDimension);
+
+  // Evalulate the function at specified index //
+  virtual OutputType EvaluateAtIndex(const IndexType& index) const;
+
+  // Evaluate the function at non-integer positions //
+  virtual OutputType Evaluate(const PointType& point) const
+  {
+    IndexType index;
+    this->ConvertPointToNearestIndex(point, index);
+    return this->EvaluateAtIndex(index);
+  }
+  virtual OutputType EvaluateAtContinuousIndex(
+    const ContinuousIndexType& cindex) const
+  {
+    IndexType index;
+    this->ConvertContinuousIndexToNearestIndex(cindex, index);
+    return this->EvaluateAtIndex(index);
+  }
+
+  // Accessors //
+  itkGetConstMacro(InternalImageFunction, typename InternalImageFunctionType::Pointer);
+  itkSetMacro(InternalImageFunction, typename InternalImageFunctionType::Pointer);
+  
+protected:
+  ImageFunctionAdaptor();
+  virtual ~ImageFunctionAdaptor() {}
+  void PrintSelf(std::ostream& os, itk::Indent indent) const;
+  
+private:
+  ImageFunctionAdaptor(const Self &);  //purposely not implemented
+  void operator =(const Self&);  //purposely not implemented
+
+  // Internal Image Function //
+  typename InternalImageFunctionType::Pointer    m_InternalImageFunction;
+  // Converter //
+  typename ConverterType::Pointer                m_Converter; 
+};
+
+} // end namespace otb
+
+#ifndef OTB_MANUAL_INSTANTIATION
+#include "otbImageFunctionAdaptor.txx"
+#endif
+
+#endif
diff --git a/Code/FeatureExtraction/otbImageFunctionAdaptor.txx b/Code/FeatureExtraction/otbImageFunctionAdaptor.txx
new file mode 100644
index 0000000000..62a1437d14
--- /dev/null
+++ b/Code/FeatureExtraction/otbImageFunctionAdaptor.txx
@@ -0,0 +1,58 @@
+/*=========================================================================
+
+  Program:   ORFEO Toolbox
+  Language:  C++
+  Date:      $Date$
+  Version:   $Revision$
+
+
+  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
+  See OTBCopyright.txt for details.
+
+
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+
+=========================================================================*/
+#ifndef __otbImageFunctionAdaptor_txx
+#define __otbImageFunctionAdaptor_txx
+
+#include "otbImageFunctionAdaptor.h"
+
+namespace otb
+{
+template< class TInternalImageFunctionType >
+ImageFunctionAdaptor< TInternalImageFunctionType >
+::ImageFunctionAdaptor()
+{
+  m_InternalImageFunction = InternalImageFunctionType::New();
+  m_Converter = ConverterType::New(); 
+}
+
+template< class TInternalImageFunctionType >
+void
+ImageFunctionAdaptor< TInternalImageFunctionType >
+::PrintSelf(std::ostream& os, itk::Indent indent) const
+{
+  Superclass::PrintSelf(os, indent);
+  os << indent << "Internal Image Function: " << m_InternalImageFunction << std::endl;
+}
+
+template< class TInternalImageFunctionType >
+typename ImageFunctionAdaptor< TInternalImageFunctionType >::OutputType
+ImageFunctionAdaptor< TInternalImageFunctionType >
+::EvaluateAtIndex(const IndexType& index) const
+{
+  OutputType result;
+  this->GetInternalImageFunction()->SetInputImage(this->GetInputImage());
+  InternalImageFunctionOutputType tmpResult = this->GetInternalImageFunction()->EvaluateAtIndex(index);
+  result = m_Converter->Convert(tmpResult);
+
+  return result;
+}
+
+
+} // end namespace otb
+
+#endif
diff --git a/Testing/Code/FeatureExtraction/CMakeLists.txt b/Testing/Code/FeatureExtraction/CMakeLists.txt
index 560bcf8695..12b13c2b26 100644
--- a/Testing/Code/FeatureExtraction/CMakeLists.txt
+++ b/Testing/Code/FeatureExtraction/CMakeLists.txt
@@ -1504,12 +1504,12 @@ ADD_TEST(feTuLocalHistogramImageFunctionTest ${FEATUREEXTRACTION_TESTS16}
 
 # -------   otb::ImageFunctionAdapter   -------------
 
-ADD_TEST(feTuImageFunctionAdapterNew ${FEATUREEXTRACTION_TESTS16}
-        otbImageFunctionAdapterNew
+ADD_TEST(feTuImageFunctionAdaptorNew ${FEATUREEXTRACTION_TESTS16}
+        otbImageFunctionAdaptorNew
 )
 
-ADD_TEST(feTvImageFunctionAdapter ${FEATUREEXTRACTION_TESTS16}
-        otbImageFunctionAdapter
+ADD_TEST(feTvImageFunctionAdaptor ${FEATUREEXTRACTION_TESTS16}
+        otbImageFunctionAdaptor
                 ${INPUTDATA}/poupees.png
 	        )
 	        
@@ -1722,7 +1722,7 @@ otbFeatureExtractionTests16.cxx
 otbFourierMellinDescriptors.cxx
 otbLocalHistogramImageFunctionNew.cxx
 otbLocalHistogramImageFunctionTest.cxx
-otbImageFunctionAdapter.cxx
+otbImageFunctionAdaptor.cxx
 otbMetaImageFunction.cxx
 )
 
diff --git a/Testing/Code/FeatureExtraction/otbFeatureExtractionTests16.cxx b/Testing/Code/FeatureExtraction/otbFeatureExtractionTests16.cxx
index 0b1ddb8e18..4ce2d662b6 100644
--- a/Testing/Code/FeatureExtraction/otbFeatureExtractionTests16.cxx
+++ b/Testing/Code/FeatureExtraction/otbFeatureExtractionTests16.cxx
@@ -32,8 +32,8 @@ void RegisterTests()
   REGISTER_TEST(otbFourierMellinDescriptorsRotationInvariant);
   REGISTER_TEST(otbLocalHistogramImageFunctionNew);
   REGISTER_TEST(otbLocalHistogramImageFunctionTest);
-  REGISTER_TEST(otbImageFunctionAdapterNew);
-  REGISTER_TEST(otbImageFunctionAdapter);
+  REGISTER_TEST(otbImageFunctionAdaptorNew);
+  REGISTER_TEST(otbImageFunctionAdaptor);
   REGISTER_TEST(otbMetaImageFunctionNew);
   REGISTER_TEST(otbMetaImageFunction);
 }
-- 
GitLab