diff --git a/Modules/Core/Functor/CMakeLists.txt b/Modules/Core/Functor/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..b22e90e314ec765d319649b8fca0eb9220d835fc --- /dev/null +++ b/Modules/Core/Functor/CMakeLists.txt @@ -0,0 +1,22 @@ +# +# 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. +# + +project(OTBFunctor) +otb_module_impl() diff --git a/Modules/Core/Functor/include/otbVariadicAddFunctor.h b/Modules/Core/Functor/include/otbVariadicAddFunctor.h new file mode 100644 index 0000000000000000000000000000000000000000..61d687146c253d89ee6aaf12f641b2be9cdbfd81 --- /dev/null +++ b/Modules/Core/Functor/include/otbVariadicAddFunctor.h @@ -0,0 +1,46 @@ +/* + * 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_VariadicAddFunctor_h +#define otb_VariadicAddFunctor_h + +#include <numeric> + +namespace otb +{ + +namespace Functor +{ + +template <typename TOut, typename ...TIns> struct VariadicAdd +{ + auto operator()(TIns... ins) const + { + std::vector<TOut> outVector{static_cast<TOut>(ins)...}; + + return std::accumulate(outVector.begin(), outVector.end(),0); + } +}; + +} // end namespace Functor + +} // end namespace otb + +#endif diff --git a/Modules/Core/Functor/include/otbVariadicConcatenateFunctor.h b/Modules/Core/Functor/include/otbVariadicConcatenateFunctor.h new file mode 100644 index 0000000000000000000000000000000000000000..fc247623611c8c7fb1d5a9dfb8fa3fb980370ddb --- /dev/null +++ b/Modules/Core/Functor/include/otbVariadicConcatenateFunctor.h @@ -0,0 +1,97 @@ +/* + * 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_VariadicConcatenateFunctor_h +#define otb_VariadicConcatenateFunctor_h + +#include <vector> +#include <numeric> + +namespace otb +{ + +namespace Functor +{ + + +namespace internal +{ +// helper function to implement next functor (convert a scalar value +// to a VariableLengthVector) +template <typename T> itk::VariableLengthVector<T> toVector(const T & in) +{ + itk::VariableLengthVector<T> out; + out.SetSize(1); + out[0] = in; + return out; +} + +// helper function to implement next functor, VariableLengthVectorVersion (returns in) +template <typename T> const itk::VariableLengthVector<T> & toVector(const itk::VariableLengthVector<T> & in) +{ + return in; +} + +// helper function to implement next functor, Merge two VariableLengthVector in-place +template <typename v1, typename v2> void concatenateVectors(v1 & a, const v2 & b) +{ + const size_t previousSizeOfA = a.GetSize(); + + a.SetSize(previousSizeOfA+b.GetSize()); + + for(size_t it = 0; it<b.Size();++it) + { + a[previousSizeOfA+it] = static_cast<typename v1::ValueType>(b[it]); + } +} + +// helper function to implement next functor, Merge N VariableLengthVector in-place +template <typename v1, typename v2, typename ...vn> void concatenateVectors(v1 & a, const v2 & b, const vn&... z) +{ + concatenateVectors(a,b); + concatenateVectors(a,z...); +} +} // end namespace internal + +// N images (all types) -> vector image +// This functor concatenates N images (N = variadic) of type +// VectorImage and or Image, into a single VectorImage +template<typename TOut, typename ...TIns> struct VariadicConcatenate +{ + auto operator()(const TIns &... ins) const + { + itk::VariableLengthVector<TOut> out; + internal::concatenateVectors(out, internal::toVector(ins)...); + + return out; + } + + // Must define OutputSize because output pixel is vector + constexpr size_t OutputSize(const std::array<size_t, sizeof...(TIns)> inputsNbBands) const + { + return std::accumulate(inputsNbBands.begin(),inputsNbBands.end(),0); + } +}; + +} // end namespace Functor + +} // end namespace otb + +#endif diff --git a/Modules/Core/Functor/otb-module.cmake b/Modules/Core/Functor/otb-module.cmake new file mode 100644 index 0000000000000000000000000000000000000000..01d7f0cf94dc980a08e61ba9514e9792c4876d90 --- /dev/null +++ b/Modules/Core/Functor/otb-module.cmake @@ -0,0 +1,34 @@ +# +# 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. +# + +set(DOCUMENTATION "This module contains a set of classes that allow parsing +metadata files from different types of sensor (both optical and radar sensor types +are supported. for instance: Pleiades, SPOT6, TerraSar, and so on).") + +otb_module(OTBFunctor + DEPENDS + OTBITK + OTBCommon + OTBImageBase + TEST_DEPENDS + OTBTestKernel +DESCRIPTION + "${DOCUMENTATION}" +) diff --git a/Modules/Core/Functor/test/CMakeLists.txt b/Modules/Core/Functor/test/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..b51af1d22e27c3345ed3a69c2ea98485b918f31f --- /dev/null +++ b/Modules/Core/Functor/test/CMakeLists.txt @@ -0,0 +1,33 @@ +# +# 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. +# + +otb_module_test() + +set(OTBFunctorTests +otbFunctorTestDriver.cxx +otbFunctorImageFilter.cxx +) + +add_executable(otbFunctorTestDriver ${OTBFunctorTests}) +target_link_libraries(otbFunctorTestDriver ${OTBFunctor-Test_LIBRARIES}) +otb_module_target_label(otbFunctorTestDriver) + +otb_add_test(NAME bfTvFunctorImageFilter COMMAND otbImageManipulationTestDriver + otbFunctorImageFilter) diff --git a/Modules/Core/Functor/test/otbFunctorImageFilter.cxx b/Modules/Core/Functor/test/otbFunctorImageFilter.cxx index 814e07a8112117fca3141cdd734861aa328e07d5..34b2b6aed1cd712af350370c584980128e9f30bb 100644 --- a/Modules/Core/Functor/test/otbFunctorImageFilter.cxx +++ b/Modules/Core/Functor/test/otbFunctorImageFilter.cxx @@ -23,6 +23,8 @@ #include "otbImage.h" #include "otbVectorImage.h" #include "itkNeighborhood.h" +#include "otbVariadicAddFunctor.h" +#include "otbVariadicConcatenateFunctor.h" #include <tuple> #include <numeric> @@ -116,74 +118,6 @@ auto checksComplex = TypesCheck<std::complex<double>>{}; // Example functors -// N scalar images -> image -// This functor takes N scalar image (variadic N) and returns the sum -// of all pixels -template <typename TOut, typename ...TIns> struct VariadicAdd -{ - auto operator()(TIns... ins) const - { - std::vector<TOut> outVector{static_cast<TOut>(ins)...}; - - return std::accumulate(outVector.begin(), outVector.end(),0); - } -}; - -// helper function to implement next functor (convert a scalar value -// to a VariableLengthVector) -template <typename T> itk::VariableLengthVector<T> toVector(const T & in) -{ - itk::VariableLengthVector<T> out; - out.SetSize(1); - out[0] = in; - return out; -} - -// helper function to implement next functor, VariableLengthVectorVersion (returns in) -template <typename T> const itk::VariableLengthVector<T> & toVector(const itk::VariableLengthVector<T> & in) -{ - return in; -} - -// helper function to implement next functor, Merge two VariableLengthVector in-place -template <typename v1, typename v2> void concatenateVectors(v1 & a, const v2 & b) -{ - const size_t previousSizeOfA = a.GetSize(); - - a.SetSize(previousSizeOfA+b.GetSize()); - - for(size_t it = 0; it<b.Size();++it) - { - a[previousSizeOfA+it] = static_cast<typename v1::ValueType>(b[it]); - } -} - -// helper function to implement next functor, Merge N VariableLengthVector in-place -template <typename v1, typename v2, typename ...vn> void concatenateVectors(v1 & a, const v2 & b, const vn&... z) -{ - concatenateVectors(a,b); - concatenateVectors(a,z...); -} - -// N images (all types) -> vector image -// This functor concatenates N images (N = variadic) of type -// VectorImage and or Image, into a single VectorImage -template<typename TOut, typename ...TIns> struct VariadicConcatenate -{ - auto operator()(const TIns &... ins) const - { - itk::VariableLengthVector<TOut> out; - concatenateVectors(out, toVector(ins)...); - - return out; - } - - // Must define OutputSize because output pixel is vector - constexpr size_t OutputSize(const std::array<size_t, sizeof...(TIns)> inputsNbBands) const - { - return std::accumulate(inputsNbBands.begin(),inputsNbBands.end(),0); - } -}; // 1 VectorImage -> 1 VectorImage with a different size depending on a @@ -361,13 +295,13 @@ int otbFunctorImageFilter(int itkNotUsed(argc), char * itkNotUsed(argv) []) filterLambda2->Update(); // Test FunctorImageFilter with the VariadicConcatenate operator - using ConcatFunctorType = VariadicConcatenate<double, double, itk::VariableLengthVector<double> >; + using ConcatFunctorType = Functor::VariadicConcatenate<double, double, itk::VariableLengthVector<double> >; auto concatenate = NewFunctorFilter(ConcatFunctorType{}); concatenate->SetVInputs(image,vimage); concatenate->Update(); // Test FunctorImageFilter With VariadicAdd functor - using AddFunctorType = VariadicAdd<double, double, double>; + using AddFunctorType = Functor::VariadicAdd<double, double, double>; auto add = NewFunctorFilter(AddFunctorType{}); add->SetVInputs(image,image); add->Update(); diff --git a/Modules/Core/Functor/test/otbFunctorTestDriver.cxx b/Modules/Core/Functor/test/otbFunctorTestDriver.cxx new file mode 100644 index 0000000000000000000000000000000000000000..860a5e38ddbbdea1b8bed4a94dd4ca09b4667643 --- /dev/null +++ b/Modules/Core/Functor/test/otbFunctorTestDriver.cxx @@ -0,0 +1,26 @@ +/* + * 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. + */ + +#include "otbTestMain.h" + +void RegisterTests() +{ + REGISTER_TEST(otbFunctorImageFilter); +}