HiPipe  0.7.0
C++17 data pipeline with Python bindings.
pad.hpp
1 /****************************************************************************
2  * hipipe library
3  * Copyright (c) 2017, Cognexa Solutions s.r.o.
4  * Copyright (c) 2018, Iterait a.s.
5  * Author(s) Filip Matzner
6  *
7  * This file is distributed under the MIT License.
8  * See the accompanying file LICENSE.txt for the complete license agreement.
9  ****************************************************************************/
10 
11 #ifndef HIPIPE_CORE_STREAM_PAD_HPP
12 #define HIPIPE_CORE_STREAM_PAD_HPP
13 
14 #include <hipipe/core/stream/transform.hpp>
15 #include <hipipe/core/utility/ndim.hpp>
16 
17 namespace hipipe::stream {
18 namespace detail {
19 
20  // Create a transformation function that can be sent to stream::transform.
21  template<typename FromColumn, typename MaskColumn, typename ValT>
22  struct wrap_pad_fun_for_transform {
23  ValT value;
24 
25  std::tuple<typename FromColumn::data_type, typename MaskColumn::data_type>
26  operator()(typename FromColumn::data_type& source)
27  {
28  using SourceVector = typename FromColumn::data_type;
29  using MaskVector = typename MaskColumn::data_type;
30  constexpr long SourceDims = utility::ndims<SourceVector>::value;
31  constexpr long MaskDims = utility::ndims<MaskVector>::value;
32  static_assert(MaskDims <= SourceDims, "stream::pad requires"
33  " the number of padded dimensions (i.e., the number of dimensions"
34  " of the mask) to be at most the number of dimensions of the source column.");
35  // create the positive mask
36  std::vector<std::vector<long>> source_size = utility::ndim_size<MaskDims>(source);
37  MaskVector mask;
38  utility::ndim_resize<MaskDims>(mask, source_size, true);
39  // pad the source
40  utility::ndim_pad<MaskDims>(source, value);
41  // create the negative mask
42  source_size = utility::ndim_size<MaskDims>(source);
43  utility::ndim_resize<MaskDims>(mask, source_size, false);
44  return {std::move(source), std::move(mask)};
45  }
46  };
47 
48 } // namespace detail
49 
82 template<
83  typename FromColumn, typename MaskColumn,
84  // The value type is automatically deduced as the type of the source column
85  // in the dimension of the mask column.
86  typename ValT =
87  typename utility::ndim_type_t<
88  typename FromColumn::data_type,
89  utility::ndims<typename MaskColumn::data_type>::value>>
90 auto pad(from_t<FromColumn> f, mask_t<MaskColumn> m, ValT value = ValT{})
91 {
92  detail::wrap_pad_fun_for_transform<FromColumn, MaskColumn, ValT>
93  trans_fun{std::move(value)};
94  return stream::transform(from<FromColumn>, to<FromColumn, MaskColumn>,
95  std::move(trans_fun), dim<0>);
96 }
97 
98 } // namespace hipipe::stream
99 #endif
hipipe::stream::pad
auto pad(from_t< FromColumn > f, mask_t< MaskColumn > m, ValT value=ValT{})
Pad the selected column to a rectangular size.
Definition: pad.hpp:96
hipipe::stream::transform
auto transform(from_t< FromColumns... > f, to_t< ToColumns... > t, Fun fun, dim_t< Dim > d=dim_t< 1 >{})
Transform a subset of hipipe columns to a different subset of hipipe columns.
Definition: transform.hpp:218