otbImageRegionAdaptativeSplitter.h 6.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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.
 */
20 21


22 23
#ifndef otbImageRegionAdaptativeSplitter_h
#define otbImageRegionAdaptativeSplitter_h
24 25 26 27 28

#include "itkRegion.h"
#include "itkImageRegionSplitter.h"
#include "itkIndex.h"
#include "itkSize.h"
29
#include "itkFastMutexLock.h"
30 31 32 33 34 35

namespace otb
{

/** \class ImageRegionSquareTileSplitter
   * \brief Divide a region into several pieces.
OTB Bot's avatar
STYLE  
OTB Bot committed
36
   *
Julien Michel's avatar
Julien Michel committed
37 38
   * This region splitter tries to adapt to the tiling scheme of the
   * input image using the TileHint parameter. It aims at
39 40 41
   * synchronizing the streaming with the tiling scheme (in a JPEG
   * 2000 situation for example) so as to avoid reading the same tile
   * multiple times in the standard pixel-based processing scheme.
OTB Bot's avatar
STYLE  
OTB Bot committed
42
   *
Julien Michel's avatar
Julien Michel committed
43 44 45 46 47 48 49 50
   * If the requested number of splits is lower than the number of
   * tiles in the image region, then the splitter will derive splits
   * that combine several tiles in one split. If the requested number
   * of splits is greater than the number of tiles in the image
   * region, the splitter will derive splits that divide exactly one
   * tile. All splits from one input tiles will be spawned before
   * changing to a new tile, ensuring the former tile will be only
   * read once.
51
   *
52
   * If the TileHint is empty, or is VImageDimension is not 2, the
Julien Michel's avatar
Julien Michel committed
53 54
   * splitter falls back to the behaviour of
   * otb::ImageRegionSquareTileSplitter.
55
   *
Julien Michel's avatar
Julien Michel committed
56
   * \sa ImageRegionSquareTileSplitter
57 58 59
   *
   * \ingroup ITKSystemObjects
   * \ingroup DataProcessing
60 61
 *
 * \ingroup OTBCommon
62 63 64 65 66 67 68
 */

template <unsigned int VImageDimension>
class ITK_EXPORT ImageRegionAdaptativeSplitter : public itk::ImageRegionSplitter<VImageDimension>
{
public:
  /** Standard class typedefs. */
69
  typedef ImageRegionAdaptativeSplitter             Self;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  typedef itk::ImageRegionSplitter<VImageDimension> Superclass;
  typedef itk::SmartPointer<Self>                   Pointer;
  typedef itk::SmartPointer<const Self>             ConstPointer;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** Run-time type information (and related methods). */
  itkTypeMacro(ImageRegionAdaptativeSplitter, itk::Object);

  /** Dimension of the image available at compile time. */
  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);

  /** Dimension of the image available at run time. */
  static unsigned int GetImageDimension()
  {
    return VImageDimension;
  }
88

89 90 91 92 93 94 95 96 97 98 99 100 101
  /** Index typedef support. An index is used to access pixel values. */
  typedef itk::Index<VImageDimension>        IndexType;
  typedef typename IndexType::IndexValueType IndexValueType;

  /** Size typedef support. A size is used to define region bounds. */
  typedef itk::Size<VImageDimension>       SizeType;
  typedef typename SizeType::SizeValueType SizeValueType;

  /** Region typedef support.   */
  typedef itk::ImageRegion<VImageDimension> RegionType;

  typedef std::vector<RegionType> StreamVectorType;

Julien Michel's avatar
Julien Michel committed
102
  /** Set the TileHint parameter */
OTB Bot's avatar
STYLE  
OTB Bot committed
103
  itkSetMacro(TileHint, SizeType);
Julien Michel's avatar
Julien Michel committed
104 105

  /** Get the TileHint parameter */
OTB Bot's avatar
STYLE  
OTB Bot committed
106
  itkGetConstReferenceMacro(TileHint, SizeType);
107

Julien Michel's avatar
Julien Michel committed
108
  /** Set the ImageRegion parameter */
OTB Bot's avatar
STYLE  
OTB Bot committed
109
  itkSetMacro(ImageRegion, RegionType);
Julien Michel's avatar
Julien Michel committed
110 111

  /** Get the ImageRegion parameter */
OTB Bot's avatar
STYLE  
OTB Bot committed
112
  itkGetConstReferenceMacro(ImageRegion, RegionType);
113

Julien Michel's avatar
Julien Michel committed
114
  /** Set the requested number of splits parameter */
OTB Bot's avatar
STYLE  
OTB Bot committed
115
  itkSetMacro(RequestedNumberOfSplits, unsigned int);
116

Julien Michel's avatar
Julien Michel committed
117
  /** Get the requested number of splits parameter */
OTB Bot's avatar
STYLE  
OTB Bot committed
118
  itkGetConstReferenceMacro(RequestedNumberOfSplits, unsigned int);
119

OTB Bot's avatar
STYLE  
OTB Bot committed
120
  /**
Julien Michel's avatar
Julien Michel committed
121 122 123
   * Calling this method will set the image region and the requested
   * number of splits, and call the EstimateSplitMap() method if
   * necessary.
124
   */
125
  unsigned int GetNumberOfSplits(const RegionType& region,
126
                                         unsigned int requestedNumber) override;
127

Julien Michel's avatar
Julien Michel committed
128 129 130
  /** Calling this method will set the image region and the requested
   * number of splits, and call the EstimateSplitMap() method if
   * necessary. */
131
  RegionType GetSplit(unsigned int i, unsigned int numberOfPieces,
132
                              const RegionType& region) override;
133

Julien Michel's avatar
Julien Michel committed
134
  /** Make the Modified() method update the IsUpToDate flag */
135
  void Modified() const override
136 137 138 139 140 141 142 143 144
  {
    // Call superclass implementation
    Superclass::Modified();

    // Invalidate up-to-date
    m_IsUpToDate = false;
  }

protected:
OTB Bot's avatar
STYLE  
OTB Bot committed
145 146 147
  ImageRegionAdaptativeSplitter() : m_TileHint(),
                                    m_ImageRegion(),
                                    m_RequestedNumberOfSplits(0),
148
                                    m_StreamVector(),
OTB Bot's avatar
STYLE  
OTB Bot committed
149
                                    m_IsUpToDate(false)
150 151
                                      {}

152 153
  ~ImageRegionAdaptativeSplitter() override {}
  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
154 155

private:
Julien Michel's avatar
Julien Michel committed
156 157
  /** This methods actually estimate the split map and stores it in a
  vector */
158 159
  void EstimateSplitMap();

160 161
  ImageRegionAdaptativeSplitter(const ImageRegionAdaptativeSplitter &) = delete;
  void operator =(const ImageRegionAdaptativeSplitter&) = delete;
162 163 164 165

  // This reflects the input image tiling
  SizeType   m_TileHint;

166
  // This contains the ImageRegion that is currently being split
167 168 169 170 171 172 173 174 175
  RegionType m_ImageRegion;

  // This contains the requested number of splits
  unsigned int m_RequestedNumberOfSplits;

  // This is a vector of all regions which will be split
  StreamVectorType m_StreamVector;

  // Is the splitter up-to-date ?
Julien Malik's avatar
Julien Malik committed
176
  mutable bool m_IsUpToDate;
177 178 179

  // Lock to ensure thread-safety
  itk::SimpleFastMutexLock m_Lock;
180 181 182 183 184 185 186 187 188
};

} // end namespace otb

#ifndef OTB_MANUAL_INSTANTIATION
# include "otbImageRegionAdaptativeSplitter.txx"
#endif

#endif