OTB  9.0.0
Orfeo Toolbox
otbVariadicConcatenateFunctor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otb_VariadicConcatenateFunctor_h
22 #define otb_VariadicConcatenateFunctor_h
23 
24 #include "itkVariableLengthVector.h"
25 #include <vector>
26 #include <numeric>
27 #include <array>
28 
29 namespace otb
30 {
31 
32 namespace Functor
33 {
34 
35 
36 namespace variadic_concatenate_details
37 {
38 
39 template <typename T>
40 size_t NumberOfElements(const T&)
41 {
42  static_assert(std::is_scalar<T>::value,
43  "variadic_concatenate_details::NumberOfElements<T> only works for T and itk::VariableLengthVector<T> where T is a scalar type.");
44  return 1;
45 }
46 
47 template <typename T>
48 size_t NumberOfElements(const itk::VariableLengthVector<T>& v)
49 {
50  static_assert(std::is_scalar<T>::value,
51  "variadic_concatenate_details::NumberOfElements<T> only works for T and itk::VariableLengthVector<T> where T is a scalar type.");
52  return v.GetSize();
53 }
54 
55 template <typename... T>
56 size_t NumberOfElements(const T&... t)
57 {
58  std::array<size_t, sizeof...(T)> sizes = {{NumberOfElements(t)...}};
59  return std::accumulate(sizes.begin(), sizes.end(), 0);
60 }
61 
62 template <typename Out, typename T>
63 size_t fillVector(itk::VariableLengthVector<Out>& out, size_t idx, const T& t)
64 {
65  assert(idx < out.GetSize());
66  out[idx] = static_cast<Out>(t);
67  return idx + 1;
68 }
69 
70 template <typename Out, typename T>
71 size_t fillVector(itk::VariableLengthVector<Out>& out, size_t idx, const itk::VariableLengthVector<T>& t)
72 {
73  assert(idx + t.GetSize() <= out.GetSize());
74  for (auto it = 0UL; it < t.GetSize(); ++it)
75  out[idx + it] = static_cast<Out>(t[it]);
76  return idx + t.GetSize();
77 }
78 
79 template <typename Out, typename Current, typename... T>
80 size_t fillVector(itk::VariableLengthVector<Out>& out, size_t idx, const Current& current, const T&... t)
81 {
82  size_t newIdx = fillVector(out, idx, current);
83  return fillVector(out, newIdx, t...);
84 }
85 } // end namespace variadic_concatenate_details
86 
87 // N images (all types) -> vector image
88 // This functor concatenates N images (N = variadic) of type
89 // VectorImage and or Image, into a single VectorImage
90 
97 template <typename TOut, typename... TIns>
99 {
100  auto operator()(const TIns&... ins) const
101  {
102  const size_t numberOfElements = variadic_concatenate_details::NumberOfElements(ins...);
103  itk::VariableLengthVector<TOut> out(numberOfElements);
105 
107 
108  return out;
109  }
110 
111  // Must define OutputSize because output pixel is vector
112  constexpr size_t OutputSize(const std::array<size_t, sizeof...(TIns)> inputsNbBands) const
113  {
114  return std::accumulate(inputsNbBands.begin(), inputsNbBands.end(), 0);
115  }
116 };
117 
118 } // end namespace Functor
119 
120 } // end namespace otb
121 
122 #endif
otb::Functor::variadic_concatenate_details::fillVector
vcl_size_t fillVector(itk::VariableLengthVector< Out > &out, vcl_size_t idx, const T &t)
Definition: otbVariadicConcatenateFunctor.h:63
otb::Functor::VariadicConcatenate
This functor concatenates any number of input of scalar type or VariableLengthVector.
Definition: otbVariadicConcatenateFunctor.h:98
otb::Functor::variadic_concatenate_details::NumberOfElements
vcl_size_t NumberOfElements(const T &)
Definition: otbVariadicConcatenateFunctor.h:40
otb::Functor::VariadicConcatenate::OutputSize
constexpr vcl_size_t OutputSize(const std::array< vcl_size_t, sizeof...(TIns)> inputsNbBands) const
Definition: otbVariadicConcatenateFunctor.h:112
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::VariadicConcatenate::operator()
auto operator()(const TIns &... ins) const
Definition: otbVariadicConcatenateFunctor.h:100