Commit 5de2770a authored by Julien Michel's avatar Julien Michel

Merge branch 'functor_filter_named_inputs' into 'functorfilter'

Named inputs in FunctorImageFilter

See merge request !290
parents 72b6a700 54e30088
......@@ -238,9 +238,9 @@ template <class F, class T, size_t N> struct NumberOfOutputComponents<F,otb::Vec
} // end namespace functor_filter_details
template <class TFunction>
template <class TFunction, class TNameMap>
void
FunctorImageFilter<TFunction>
FunctorImageFilter<TFunction, TNameMap>
::GenerateInputRequestedRegion()
{
// Get requested region for output
......@@ -253,9 +253,9 @@ FunctorImageFilter<TFunction>
functor_filter_details::SetInputRequestedRegions<InputHasNeighborhood>(this->GetVariadicInputs(),requestedRegion, m_Radius);
}
template <class TFunction>
template <class TFunction, class TNameMap>
void
FunctorImageFilter<TFunction>::GenerateOutputInformation()
FunctorImageFilter<TFunction, TNameMap>::GenerateOutputInformation()
{
// Call Superclass implementation
Superclass::GenerateOutputInformation();
......@@ -273,9 +273,9 @@ FunctorImageFilter<TFunction>::GenerateOutputInformation()
/**
* ThreadedGenerateData Performs the neighborhood-wise operation
*/
template <class TFunction>
template <class TFunction, class TNameMap>
void
FunctorImageFilter<TFunction>
FunctorImageFilter<TFunction, TNameMap>
::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
{
// Build output iterator
......
/*
* Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otb_VariadicNamedInputsImageFilter_h
#define otb_VariadicNamedInputsImageFilter_h
#include "otbVariadicInputsImageFilter.h"
namespace otb {
/// TODO: Documentation
namespace internal
{
template<typename Arg, typename Tuple> struct tuple_index;
template<typename Arg, typename...Args> struct tuple_index<Arg, std::tuple<Arg,Args...> >
{
static constexpr std::size_t value = 0;
};
template<typename Arg, typename NotMatching, typename...Args> struct tuple_index<Arg, std::tuple<NotMatching,Args...>>
{
static_assert(sizeof...(Args)>0,"Could not find requested type in tuple");
static constexpr std::size_t value = 1 + tuple_index<Arg,std::tuple<Args...>>::value;
};
}
template<class TOuptut, class TInputNameMap, class ... TInputs> class VariadicNamedInputsImageFilter : public VariadicInputsImageFilter<TOuptut,TInputs...>
{
public:
using Self = VariadicNamedInputsImageFilter<TOuptut,TInputNameMap, TInputs...>;
using Pointer = itk::SmartPointer<Self>;
using ConstPointer = itk::SmartPointer<const Self>;
using Superclass = VariadicInputsImageFilter<TOuptut, TInputs...>;;
using InputTypesTupleType = typename Superclass::InputTypesTupleType;
template <size_t I> using InputImageType = typename Superclass::template InputImageType<I>;
static constexpr size_t NumberOfInputs = Superclass::NumberOfInputs;
static_assert(std::tuple_size<TInputNameMap>::value == NumberOfInputs,"Tuple for input name does not match the size of ... TInputs");
itkNewMacro(Self);
template <typename Tag> void SetVariadicNamedInput(const InputImageType<internal::tuple_index<Tag, TInputNameMap>::value> * inputPtr)
{
constexpr size_t idx = internal::tuple_index<Tag, TInputNameMap>::value;
this->SetNthInput(idx,const_cast<InputImageType<idx> *>(inputPtr));
}
template <typename Tag> const InputImageType<internal::tuple_index<Tag,TInputNameMap>::value> * GetVariadicNamedInput()
{
constexpr size_t idx = internal::tuple_index<Tag, TInputNameMap>::value;
return dynamic_cast<const InputImageType<idx> *>(this->GetInput(idx));
}
protected:
VariadicNamedInputsImageFilter() = default;
~VariadicNamedInputsImageFilter() = default;
private:
VariadicNamedInputsImageFilter(const Self&) = delete;
void operator=(const Self&) = delete;
};
}
#endif
......@@ -25,6 +25,7 @@
#include "itkNeighborhood.h"
#include "otbVariadicAddFunctor.h"
#include "otbVariadicConcatenateFunctor.h"
#include "otbVariadicNamedInputsImageFilter.h"
#include <tuple>
#include <numeric>
......@@ -144,13 +145,23 @@ template <typename TOut,typename TIn> struct TestOperatorVoidReturn
static_assert(std::is_same<typename FilterType::template InputImageType<0>, InputImageType>::value, "");
filter->SetVariadicInputs(in);
filter->SetInput1(in);
filter->template SetVariadicInput<0>(in); // template keyword to avoid C++ parse ambiguity
filter->template SetVariadicInput<0>(in); // template keyword to
// avoid C++ parse
// ambiguity
auto res = filter->template GetVariadicInput<0>();
filter->Update();
// Test named input version
struct tag{};
using inputNames = std::tuple<tag>;
auto filter1 = NewFunctorFilter<decltype(functor),inputNames>(functor);
filter1->template SetVariadicNamedInput<tag>(in);
res = filter1->template GetVariadicNamedInput<tag>();
filter1->Update();
// Test with void return
auto functorWithVoidReturn = TestOperatorVoidReturn<TOut,TIn>{};
auto filterWithVoidReturn = NewFunctorFilter(functorWithVoidReturn);
using FilterWithVoidReturnType = typename decltype(filter)::ObjectType;
static_assert(FilterWithVoidReturnType::NumberOfInputs == 1,"");
static_assert(std::is_same<typename FilterWithVoidReturnType::template InputImageType<0>, InputImageType>::value, "");
......@@ -346,12 +357,24 @@ int otbFunctorImageFilter(int itkNotUsed(argc), char * itkNotUsed(argv) [])
filter->SetVariadicInputs(vimage,image);
std::cout<<filter->GetVariadicInput<0>()<< filter->GetVariadicInput<1>()<<std::endl;
// Test VariadicNamedInputsImageFilter
struct xs {};
struct pan {};
using Names = std::tuple<xs,pan>;
auto filterWithNames = otb::VariadicNamedInputsImageFilter<VectorImageType, Names, VectorImageType,ImageType>::New();
filterWithNames->SetVariadicNamedInput<xs>(vimage);
// Test FunctorImageFilter with a built-in math function
using CosType = double(double);
auto filterCos = NewFunctorFilter(static_cast<CosType *>(std::cos));
filterCos->SetVariadicInputs(image);
filterCos->Update();
filterWithNames->SetVariadicNamedInput<pan>(image);
std::cout<<filterWithNames->GetVariadicNamedInput<xs>()<< filterWithNames->GetVariadicNamedInput<pan>()<<std::endl;
// test FunctorImageFilter with a lambda
double scale = 10.;
......@@ -392,7 +415,8 @@ int otbFunctorImageFilter(int itkNotUsed(argc), char * itkNotUsed(argv) [])
// Test FunctorImageFilter With VariadicAdd functor
using AddFunctorType = Functor::VariadicAdd<double, double, double>;
auto add = NewFunctorFilter(AddFunctorType{});
add->SetVariadicInputs(image,image);
add->SetVariadicInput<0>(image);
add->SetVariadicInput<1>(image);
add->Update();
// Test FunctorImageFilter with BandExtraction functor
......@@ -424,7 +448,7 @@ int otbFunctorImageFilter(int itkNotUsed(argc), char * itkNotUsed(argv) [])
auto argFilter = NewFunctorFilter(LambdaComplex);
argFilter->SetVariadicInputs(cimage);
argFilter->Update();
return EXIT_SUCCESS;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment