OTB  6.7.0
Orfeo Toolbox
otbImageRegionNonUniformMultidimensionalSplitter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbImageRegionNonUniformMultidimensionalSplitter_hxx
23 #define otbImageRegionNonUniformMultidimensionalSplitter_hxx
25 
26 namespace otb
27 {
28 
32 template <unsigned int VImageDimension>
33 unsigned int
35 ::GetNumberOfSplits(const RegionType& region, unsigned int requestedNumber)
36 {
37  const SizeType& regionSize = region.GetSize();
38 
39  // requested number of splits per dimension
40  double splitsPerDimension[VImageDimension];
41 // ::ceil( std::pow((double) requestedNumber, 1.0/(double) VImageDimension));
42 
43  unsigned int numberOfPiecesLeft = requestedNumber;
44  unsigned int j, numPieces;
45  numPieces = 1;
46 
47  for (j = VImageDimension; j > 0; --j)
48  {
49  if (regionSize[j - 1] < numberOfPiecesLeft)
50  {
51  splitsPerDimension[j - 1] = regionSize[j - 1];
52  }
53  else
54  {
55  splitsPerDimension[j - 1] = numberOfPiecesLeft;
56  }
57  numberOfPiecesLeft = (unsigned int)
58  ::ceil(numberOfPiecesLeft / splitsPerDimension[j - 1]);
59  numPieces *= (unsigned int) splitsPerDimension[j - 1];
60  }
61 
62  // if a given dimension has fewer pixels that splitsPerDimension, then
63  // only split number of pixels times
64 // unsigned int i, numPieces;
65 // numPieces = 1;
66 // for (i=0; i < VImageDimension; ++i)
67 // {
68 // if (regionSize[i] < splitsPerDimension)
69 // {
70 // numPieces *= regionSize[i];
71 // }
72 // else
73 // {
74 // numPieces *= (unsigned int) splitsPerDimension;
75 // }
76 // }
77 
78  return numPieces;
79 }
80 
84 template <unsigned int VImageDimension>
87 ::GetSplit(unsigned int i, unsigned int numberOfPieces,
88  const RegionType& region)
89 {
90  RegionType splitRegion;
91  IndexType splitIndex;
92  SizeType splitSize, regionSize;
93 
94  // Initialize the splitRegion to the requested region
95  splitRegion = region;
96  splitIndex = splitRegion.GetIndex();
97  splitSize = splitRegion.GetSize();
98 
99  regionSize = region.GetSize();
100 
101  // requested number of splits per dimension
102  double splitsPerDimension[VImageDimension];
103 // ::ceil( std::pow((double) numberOfPieces, 1.0/(double) VImageDimension));
104 
105  unsigned int numberOfPiecesLeft = numberOfPieces;
106 
107  unsigned int j;
108  for (j = VImageDimension; j > 0; --j)
109  {
110  if (regionSize[j - 1] < numberOfPiecesLeft)
111  {
112  splitsPerDimension[j - 1] = regionSize[j - 1];
113  }
114  else
115  {
116  splitsPerDimension[j - 1] = numberOfPiecesLeft;
117  }
118  numberOfPiecesLeft = (unsigned int)
119  ::ceil(numberOfPiecesLeft / splitsPerDimension[j - 1]);
120  }
121 
122  // if a given dimension has fewer pixels that splitsPerDimension, then
123  // only split number of pixels times
124  unsigned int splits[VImageDimension], pixelsPerSplit[VImageDimension];
125  unsigned int numPieces;
126  unsigned int ijk[VImageDimension];
127  unsigned int offsetTable[VImageDimension];
128  numPieces = 1;
129  for (j = 0; j < VImageDimension; ++j)
130  {
131  offsetTable[j] = numPieces;
132  if (regionSize[j] < splitsPerDimension[j])
133  {
134  splits[j] = regionSize[j];
135  pixelsPerSplit[j] = 1;
136  numPieces *= regionSize[j];
137  }
138  else
139  {
140  splits[j] = (unsigned int) splitsPerDimension[j];
141  pixelsPerSplit[j] = (unsigned int) ::ceil(regionSize[j]
142  / (double) splits[j]);
143  numPieces *= (unsigned int) splitsPerDimension[j];
144  }
145  }
146 
147  // determine which split we are in
148  unsigned int offset = i;
149  for (j = VImageDimension - 1; j > 0; j--)
150  {
151  ijk[j] = offset / offsetTable[j];
152  offset -= (ijk[j] * offsetTable[j]);
153  }
154  ijk[0] = offset;
155 
156  // compute the split
157  for (j = 0; j < VImageDimension; ++j)
158  {
159  splitIndex[j] += ijk[j] * pixelsPerSplit[j];
160  if (ijk[j] < splits[j] - 1)
161  {
162  splitSize[j] = pixelsPerSplit[j];
163  }
164  else
165  {
166  // this dimension is falling off the edge of the image
167  splitSize[j] = splitSize[j] - ijk[j] * pixelsPerSplit[j];
168  }
169  }
170 
171  // set the split region ivars
172  splitRegion.SetIndex(splitIndex);
173  splitRegion.SetSize(splitSize);
174 
175  itkDebugMacro(" Split Piece: " << std::endl << splitRegion);
176 
177  return splitRegion;
178 }
179 
183 template <unsigned int VImageDimension>
184 void
186 ::PrintSelf(std::ostream& os, itk::Indent indent) const
187 {
188  Superclass::PrintSelf(os, indent);
189 }
190 
191 } // end namespace otb
192 
193 #endif
void SetSize(const SizeType &size)
const IndexType & GetIndex() const
RegionType GetSplit(unsigned int i, unsigned int numberOfPieces, const RegionType &region) override
unsigned int GetNumberOfSplits(const RegionType &region, unsigned int requestedNumber) override
const SizeType & GetSize() const
int ceil(const T x)
void SetIndex(const IndexType &index)
void PrintSelf(std::ostream &os, itk::Indent indent) const override