OTB  7.1.0
Orfeo Toolbox
otbImageRegionSquareTileSplitter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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 otbImageRegionSquareTileSplitter_hxx
22 #define otbImageRegionSquareTileSplitter_hxx
23 
25 #include "otbMath.h"
26 #include "otbMacro.h"
27 
28 namespace otb
29 {
30 
31 template <unsigned int VImageDimension>
32 unsigned int ImageRegionSquareTileSplitter<VImageDimension>::GetNumberOfSplits(const RegionType& region, unsigned int requestedNumber)
33 {
34  unsigned int theoricalNbPixelPerTile = region.GetNumberOfPixels() / requestedNumber;
35  unsigned int theoricalTileDimension = static_cast<unsigned int>(std::sqrt(static_cast<double>(theoricalNbPixelPerTile)));
36 
37  // Take the next multiple of m_TileSizeAlignment (eventually generate more splits than requested)
38  m_TileDimension = (theoricalTileDimension + m_TileSizeAlignment - 1) / m_TileSizeAlignment * m_TileSizeAlignment;
39 
40  // Minimal tile size is m_TileSizeAlignment * m_TileSizeAlignment
41  if (m_TileDimension < m_TileSizeAlignment)
42  {
43  otbMsgDevMacro(<< "Warning: clamping tile size to " << m_TileSizeAlignment << " * " << m_TileSizeAlignment);
44  m_TileDimension = m_TileSizeAlignment;
45  }
46 
47  unsigned int numPieces = 1;
48  const SizeType& regionSize = region.GetSize();
49  for (unsigned int j = 0; j < VImageDimension; ++j)
50  {
51  m_SplitsPerDimension[j] = (regionSize[j] + m_TileDimension - 1) / m_TileDimension;
52  numPieces *= m_SplitsPerDimension[j];
53  }
54 
55  otbMsgDevMacro(<< "Tile dimension : " << m_TileDimension)
56  otbMsgDevMacro(<< "Number of splits per dimension : " << m_SplitsPerDimension[0] << " " << m_SplitsPerDimension[1])
57 
58  return numPieces;
59 }
60 
61 template <unsigned int VImageDimension>
62 itk::ImageRegion<VImageDimension> ImageRegionSquareTileSplitter<VImageDimension>::GetSplit(unsigned int i, unsigned int itkNotUsed(numberOfPieces),
63  const RegionType& region)
64 {
65  RegionType splitRegion;
66  IndexType splitIndex;
67 
68  // Compute the actual number of splits
69  unsigned int numPieces = 1;
70  for (unsigned int j = 0; j < VImageDimension; ++j)
71  {
72  numPieces *= m_SplitsPerDimension[j];
73  }
74 
75  // Sanity check
76  if (i >= numPieces)
77  {
78  itkExceptionMacro("Asked for split number " << i << " but region contains only " << numPieces << " splits");
79  }
80 
81  // Compute the split index in the streaming grid
82  unsigned int remaining = i;
83  for (unsigned int j = VImageDimension - 1; j > 0; --j)
84  {
85  splitIndex[j] = remaining / m_SplitsPerDimension[VImageDimension - 1 - j];
86  remaining = remaining % m_SplitsPerDimension[VImageDimension - 1 - j];
87  }
88  splitIndex[0] = remaining;
89 
90  // Transform the split index to the actual coordinates
91  for (unsigned int j = 0; j < VImageDimension; ++j)
92  {
93  splitRegion.SetIndex(j, region.GetIndex(j) + m_TileDimension * splitIndex[j]);
94  splitRegion.SetSize(j, m_TileDimension);
95  }
96 
97  // Handle the borders
98  splitRegion.Crop(region);
99 
100  return splitRegion;
101 }
102 
106 template <unsigned int VImageDimension>
107 void ImageRegionSquareTileSplitter<VImageDimension>::PrintSelf(std::ostream& os, itk::Indent indent) const
108 {
109  Superclass::PrintSelf(os, indent);
110  os << indent << "SplitsPerDimension : " << m_SplitsPerDimension << std::endl;
111  os << indent << "TileDimension : " << m_TileDimension << std::endl;
112  os << indent << "TileSizeAlignment : " << m_TileSizeAlignment << std::endl;
113 }
115 
116 } // end namespace itk
117 
118 #endif
void PrintSelf(std::ostream &os, itk::Indent indent) const override
itk::ImageRegion< VImageDimension > RegionType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
RegionType GetSplit(unsigned int i, unsigned int numberOfPieces, const RegionType &region) override
unsigned int GetNumberOfSplits(const RegionType &region, unsigned int requestedNumber) override
#define otbMsgDevMacro(x)
Definition: otbMacro.h:64