otbImageRegionAdaptativeSplitter.h 6 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
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#ifndef __otbImageRegionAdaptativeSplitter_h
#define __otbImageRegionAdaptativeSplitter_h

#include "itkRegion.h"
#include "itkImageRegionSplitter.h"
#include "itkIndex.h"
#include "itkSize.h"
26
#include "itkFastMutexLock.h"
27 28 29 30 31 32

namespace otb
{

/** \class ImageRegionSquareTileSplitter
   * \brief Divide a region into several pieces.
OTB Bot's avatar
STYLE  
OTB Bot committed
33
   *
Julien Michel's avatar
Julien Michel committed
34 35
   * This region splitter tries to adapt to the tiling scheme of the
   * input image using the TileHint parameter. It aims at
36 37 38
   * 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
39
   *
Julien Michel's avatar
Julien Michel committed
40 41 42 43 44 45 46 47
   * 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.
48
   *
49
   * If the TileHint is empty, or is VImageDimension is not 2, the
Julien Michel's avatar
Julien Michel committed
50 51
   * splitter falls back to the behaviour of
   * otb::ImageRegionSquareTileSplitter.
52
   *
Julien Michel's avatar
Julien Michel committed
53
   * \sa ImageRegionSquareTileSplitter
54 55 56 57 58 59 60 61 62 63
   *
   * \ingroup ITKSystemObjects
   * \ingroup DataProcessing
 */

template <unsigned int VImageDimension>
class ITK_EXPORT ImageRegionAdaptativeSplitter : public itk::ImageRegionSplitter<VImageDimension>
{
public:
  /** Standard class typedefs. */
64
  typedef ImageRegionAdaptativeSplitter             Self;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  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;
  }
83

84 85 86 87 88 89 90 91 92 93 94 95 96
  /** 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
97
  /** Set the TileHint parameter */
OTB Bot's avatar
STYLE  
OTB Bot committed
98
  itkSetMacro(TileHint, SizeType);
Julien Michel's avatar
Julien Michel committed
99 100

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

Julien Michel's avatar
Julien Michel committed
103
  /** Set the ImageRegion parameter */
OTB Bot's avatar
STYLE  
OTB Bot committed
104
  itkSetMacro(ImageRegion, RegionType);
Julien Michel's avatar
Julien Michel committed
105 106

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

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

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

OTB Bot's avatar
STYLE  
OTB Bot committed
115
  /**
Julien Michel's avatar
Julien Michel committed
116 117 118
   * Calling this method will set the image region and the requested
   * number of splits, and call the EstimateSplitMap() method if
   * necessary.
119 120 121 122
   */
  virtual unsigned int GetNumberOfSplits(const RegionType& region,
                                         unsigned int requestedNumber);

Julien Michel's avatar
Julien Michel committed
123 124 125
  /** Calling this method will set the image region and the requested
   * number of splits, and call the EstimateSplitMap() method if
   * necessary. */
126 127 128
  virtual RegionType GetSplit(unsigned int i, unsigned int numberOfPieces,
                              const RegionType& region);

Julien Michel's avatar
Julien Michel committed
129
  /** Make the Modified() method update the IsUpToDate flag */
Julien Malik's avatar
Julien Malik committed
130
  virtual void Modified() const
131 132 133 134 135 136 137 138 139
  {
    // Call superclass implementation
    Superclass::Modified();

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

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

  virtual ~ImageRegionAdaptativeSplitter() {}
  void PrintSelf(std::ostream& os, itk::Indent indent) const;

private:
Julien Michel's avatar
Julien Michel committed
151 152
  /** This methods actually estimate the split map and stores it in a
  vector */
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  void EstimateSplitMap();

  ImageRegionAdaptativeSplitter(const ImageRegionAdaptativeSplitter &); //purposely not implemented
  void operator =(const ImageRegionAdaptativeSplitter&); //purposely not implemented

  // This reflects the input image tiling
  SizeType   m_TileHint;

  // This contains the ImageRegion that is currently beeing split
  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
171
  mutable bool m_IsUpToDate;
172 173 174

  // Lock to ensure thread-safety
  itk::SimpleFastMutexLock m_Lock;
175 176 177 178 179 180 181 182 183
};

} // end namespace otb

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

#endif