otbFunctorImageFilter.hxx 10 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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 otbFunctorImageFilter_hxx
#define otbFunctorImageFilter_hxx

#include "otbFunctorImageFilter.h"
#include "itkProgressReporter.h"
Julien Michel's avatar
Julien Michel committed
26 27 28
#include "itkConstNeighborhoodIterator.h"
#include "itkImageScanlineIterator.h"
#include "itkImageRegionConstIterator.h"
29 30 31

namespace otb
{
Julien Michel's avatar
Julien Michel committed
32 33 34 35 36 37 38
namespace functor_filter_details
{
// Variadic SetRequestedRegion

// This function sets the requested region for one image
template<class T> int SetInputRequestedRegion(const T * img, const itk::ImageRegion<2> & region, const itk::Size<2>& radius)
{
39 40
  assert(img&&"Input image is a nullptr");

Julien Michel's avatar
Julien Michel committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  auto currentRegion = region;
  currentRegion.PadByRadius(radius);

  // The ugly cast in all ITK filters
  T * nonConstImg = const_cast<T*>(img);
  
  if(currentRegion.Crop(img->GetLargestPossibleRegion()))
    {
    nonConstImg->SetRequestedRegion(currentRegion);
    return 0;
    }
  else
    {
    nonConstImg->SetRequestedRegion(currentRegion);
        
    // build an exception
    itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
58
    e.SetLocation("::SetInputRequestedRegion<>()");
Julien Michel's avatar
Julien Michel committed
59 60 61 62 63 64
    e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
    e.SetDataObject(nonConstImg);
    throw e;
    }
}

Julien Michel's avatar
Julien Michel committed
65
// Will be easier to write in c++17 with std::apply and fold expressions
Julien Michel's avatar
Julien Michel committed
66 67 68 69 70
template <class Tuple, size_t...Is> auto SetInputRequestedRegionsImpl(Tuple & t, const itk::ImageRegion<2> & region, std::index_sequence<Is...>,const itk::Size<2> & radius)
{
  return std::make_tuple(SetInputRequestedRegion(std::get<Is>(t),region,radius)...);
}

Julien Michel's avatar
Julien Michel committed
71
// Will be easier to write in c++17 with std::apply and fold expressions
Julien Michel's avatar
Julien Michel committed
72 73 74 75 76
template <typename... T> auto SetInputRequestedRegions(std::tuple<T...> && t,const itk::ImageRegion<2> & region, const itk::Size<2> & radius)
{
  return SetInputRequestedRegionsImpl(t,region,std::make_index_sequence<sizeof...(T)>{},radius);
}

Julien Michel's avatar
Julien Michel committed
77
// Will be easier to write in c++17 with std::apply and fold expressions
Julien Michel's avatar
Julien Michel committed
78 79 80 81 82
template <class Tuple, size_t...Is> auto GetNumberOfComponentsPerInputImpl(Tuple & t, std::index_sequence<Is...>)
{
  return std::array<size_t,sizeof...(Is)>{{std::get<Is>(t)->GetNumberOfComponentsPerPixel()...}};
}

Julien Michel's avatar
Julien Michel committed
83
// Will be easier to write in c++17 with std::apply and fold expressions
Julien Michel's avatar
Julien Michel committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
template <typename ...T> auto GetNumberOfComponentsPerInput(std::tuple<T...> & t)
{
  return GetNumberOfComponentsPerInputImpl(t, std::make_index_sequence<sizeof...(T)>{});
}

template <typename N> struct MakeIterator {};

template <> struct MakeIterator<std::false_type>
{
  template <class T> static auto Make(const T * img, const itk::ImageRegion<2> & region, const itk::Size<2>&)
  {
    itk::ImageRegionConstIterator<T> it(img,region);
    return it;
  }
};

template <> struct MakeIterator<std::true_type>
{
  template <class T> static auto Make(const T * img, const itk::ImageRegion<2> & region, const itk::Size<2>& radius)
  {
    itk::ConstNeighborhoodIterator<T> it(radius,img,region);
    return it;
  }
};

Julien Michel's avatar
Julien Michel committed
109
// Will be easier to write in c++17 with std::apply and fold expressions
Julien Michel's avatar
Julien Michel committed
110 111 112 113 114
template <class TNeigh, class Tuple, size_t...Is> auto MakeIteratorsImpl(const Tuple& t, const itk::ImageRegion<2> & region, const itk::Size<2> & radius, std::index_sequence<Is...>, TNeigh)
{
  return std::make_tuple(MakeIterator<typename std::tuple_element<Is,TNeigh>::type >::Make(std::get<Is>(t),region,radius)...);
}

Julien Michel's avatar
Julien Michel committed
115
// Will be easier to write in c++17 with std::apply and fold expressions
Julien Michel's avatar
Julien Michel committed
116 117 118 119 120 121 122 123 124 125 126
template<class TNeigh, typename... T> auto MakeIterators(std::tuple<T...> &&t,const itk::ImageRegion<2> & region, const itk::Size<2> & radius, TNeigh n)
  {
    return MakeIteratorsImpl(t,region,radius,std::make_index_sequence<sizeof...(T)>{},n);
  }

// Variadic call of operator from iterator tuple
template <typename T> struct GetProxy{};


template <typename T> struct GetProxy<itk::ImageRegionConstIterator<T> >
{
127
  static decltype(auto) Get(const itk::ImageRegionConstIterator<T> & t)
Julien Michel's avatar
Julien Michel committed
128 129 130 131 132 133 134
{
  return t.Get();
}
};

template <typename T> struct GetProxy<itk::ConstNeighborhoodIterator<T> >
{
135
  static decltype(auto) Get(const itk::ConstNeighborhoodIterator<T> & t)
Julien Michel's avatar
Julien Michel committed
136 137 138 139 140
{
  return t.GetNeighborhood();
}
};

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
template <class Oper> struct OperProxy : public OperProxy<decltype(&Oper::operator())> {};

template<class Out, class ... In> struct OperProxy<Out(*)(In...)>
{
  template <class Oper> static void Compute(Oper& oper, Out& out, const In& ... in)
  {
    out = oper(in...);
  }
};

template<class C, class Out, class ... In> struct OperProxy<Out(C::*)(In...)>
{
  template<class Oper> static void Compute(Oper& oper, Out& out, const In& ... in)
  {
    out = oper(in...);
  }
};

template<class C, class Out, class ... In> struct OperProxy<Out(C::*)(In...) const>
{
  template<class Oper> static void Compute(Oper& oper, Out& out, const In& ... in)
  {
    out = oper(in...);
  }
};

template<class Out, class ... In> struct OperProxy<void(*)(Out&, In...)>
{
  template<class Oper> static void Compute(Oper& oper, Out& out, const In& ... in)
  {
    oper(out,in...);
  }
};

template<class C, class Out, class ... In> struct OperProxy<void(C::*)(Out&, In...)>
{
  template<class Oper> static void Compute(Oper& oper, Out& out, const In& ... in)
  {
    oper(out,in...);
  }
};

template<class C, class Out, class ... In> struct OperProxy<void(C::*)(Out&, In...) const>
{
  template template<class Oper> static void Compute(Oper& oper, Out& out, const In& ... in)
  {
    oper(out,in...);
  }
};


Julien Michel's avatar
Julien Michel committed
192
// Will be easier to write in c++17 with std::apply and fold expressions
193
template <class Tuple, class Out, class Oper, size_t...Is> auto CallOperatorImpl(Tuple& t, Out & out, const Oper & oper,std::index_sequence<Is...>)
Julien Michel's avatar
Julien Michel committed
194
{
195
  OperProxy<Oper>::Compute(oper,out,GetProxy<typename std::remove_reference<decltype(std::get<Is>(t))>::type>::Get(std::get<Is>(t))...);
Julien Michel's avatar
Julien Michel committed
196 197
}

Julien Michel's avatar
Julien Michel committed
198
// Will be easier to write in c++17 with std::apply and fold expressions
199
template <class Out, class Oper, typename ... Args> auto CallOperator(Out & out, const Oper& oper, std::tuple<Args...> & t)
Julien Michel's avatar
Julien Michel committed
200
{
201
  CallOperatorImpl(t,out,oper,std::make_index_sequence<sizeof...(Args)>{});
Julien Michel's avatar
Julien Michel committed
202 203 204
}

// Variadic move of iterators
Julien Michel's avatar
Julien Michel committed
205
// Will be easier to write in c++17 with std::apply and fold expressions
Julien Michel's avatar
Julien Michel committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
template<class Tuple,size_t...Is> auto MoveIteratorsImpl(Tuple & t, std::index_sequence<Is...>)
{
  return std::make_tuple(++(std::get<Is>(t) )...);
}

template<typename ... Args> void MoveIterators(std::tuple<Args...> & t)
{
  MoveIteratorsImpl(t,std::make_index_sequence<sizeof...(Args)>{});
}


// Default implementation does nothing
template <class F, class O, size_t N> struct NumberOfOutputComponents
{};

template <class F, class T, size_t N> struct NumberOfOutputComponents<F,otb::Image<T>,N>
  {
  // We can not be here if output type is VectorImage
    static void Set(const F&, otb::Image<T> *, std::array<size_t,N>){}
};

// O is a VectorImage AND F has a fixed OuptutSize static constexrp size_t;
template <class F, class T, size_t N> struct NumberOfOutputComponents<F,otb::VectorImage<T>,N>
{
  static void Set(const F & f, otb::VectorImage<T> * outputImage, std::array<size_t,N> inNbBands)
  {
    outputImage->SetNumberOfComponentsPerPixel(f.OutputSize(inNbBands));
  }
};

} // end namespace functor_filter_details

238 239 240 241 242
template <class TFunction>
void
FunctorImageFilter<TFunction>
::GenerateInputRequestedRegion()
{
243 244 245 246 247
  // Get requested region for output
  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
  auto requestedRegion = outputPtr->GetRequestedRegion();

  // Propagate to each variadic inputs, including possible radius
248 249
  // TODO: For now all inputs are padded with the radius, even if they
  // are not neighborhood based
250
  functor_filter_details::SetInputRequestedRegions(this->GetVariadicInputs(),requestedRegion, m_Radius);
251 252 253 254 255 256
}

template <class TFunction>
void
FunctorImageFilter<TFunction>::GenerateOutputInformation()
{
Julien Michel's avatar
Julien Michel committed
257 258 259 260
  // Call Superclass implementation
  Superclass::GenerateOutputInformation();

  // Get All variadic inputs
261
  auto inputs = this->GetVariadicInputs();
Julien Michel's avatar
Julien Michel committed
262 263 264 265 266 267

  // Retrieve an array of number of components per input
  auto inputNbComps = functor_filter_details::GetNumberOfComponentsPerInput(inputs);

  // Call the helper to set the number of components for the output image
  functor_filter_details::NumberOfOutputComponents<TFunction,OutputImageType,inputNbComps.size()>::Set(m_Functor,this->GetOutput(),inputNbComps);
268 269 270 271 272 273 274 275 276 277
}

/**
 * ThreadedGenerateData Performs the neighborhood-wise operation
 */
template <class TFunction>
void
FunctorImageFilter<TFunction>
::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
{
Julien Michel's avatar
Julien Michel committed
278
  // Build output iterator
279 280 281
  itk::ImageScanlineIterator<OutputImageType> outIt(this->GetOutput(),outputRegionForThread);
  itk::ProgressReporter p(this,threadId,outputRegionForThread.GetNumberOfPixels());

282
  auto inputIterators = functor_filter_details::MakeIterators(this->GetVariadicInputs(),outputRegionForThread, m_Radius,InputHasNeighborhood{});
283 284 285 286

  // Build a default value
  typename OutputImageType::PixelType outputValueHolder;
  itk::NumericTraits<typename OutputImageType::PixelType>::SetLength(outputValueHolder,this->GetOutput()->GetNumberOfComponentsPerPixel());
287 288 289 290 291
  
  while(!outIt.IsAtEnd())
    {
    for(;!outIt.IsAtEndOfLine();++outIt,functor_filter_details::MoveIterators(inputIterators))
      {
292 293
      functor_filter_details::CallOperator(outputValueHolder,m_Functor,inputIterators);
      outIt.Set(outputValueHolder);
294 295 296 297 298
      // Update progress
      p.CompletedPixel();
      }
    outIt.NextLine();
    }
299 300 301 302 303
}

} // end namespace otb

#endif