lsgrmSplitter.txx 5.41 KB
Newer Older
1 2
#ifndef __LSGRM_SPLITTER_TXX
#define __LSGRM_SPLITTER_TXX
remicres's avatar
remicres committed
3
#include "lsgrmSplitter.h"
4 5 6

namespace lsgrm
{
remicres's avatar
remicres committed
7

remicres's avatar
remicres committed
8
enum RelativePosition{
remicres's avatar
remicres committed
9 10 11 12
  POS_TOP,
  POS_RIGHT,
  POS_BOTTOM,
  POS_LEFT
remicres's avatar
remicres committed
13 14 15 16 17 18 19 20 21 22 23 24 25
};

enum NeighborhoodRelativePosition{
  NBH_TOP,
  NBH_TOP_RIGHT,
  NBH_RIGHT,
  NBH_BOTTOM_RIGHT,
  NBH_BOTTOM,
  NBH_BOTTOM_LEFT,
  NBH_LEFT,
  NBH_TOP_LEFT
};

remicres's avatar
remicres committed
26
template <class TInputImage>
27
std::vector<ProcessingTile> SplitOTBImage(TInputImage * imagePtr, // input image
remicres's avatar
remicres committed
28 29 30 31 32
    unsigned int& tileWidth, // width of the tile
    unsigned int& tileHeight, // height of the tile
    const unsigned int margin, // stability margin
    unsigned int &nbTilesX,
    unsigned int &nbTilesY,
33
    std::string temporaryFilesPrefix)
remicres's avatar
remicres committed
34
    {
remicres's avatar
remicres committed
35

36 37
  std::vector<ProcessingTile> tiles;

remicres's avatar
remicres committed
38 39 40 41 42 43 44 45 46
  // Image size
  auto imageWidth = imagePtr->GetLargestPossibleRegion().GetSize()[0];
  auto imageHeight = imagePtr->GetLargestPossibleRegion().GetSize()[1];

  /* Local variables for the next loop */
  unsigned int startX, startY; // Upper left coordinates of the tile.
  unsigned int sizeX, sizeY; // Size of the tiles.

  /* Loop over the tiles*/
47
  tiles.assign(nbTilesX * nbTilesY, ProcessingTile());
remicres's avatar
remicres committed
48 49 50 51 52 53
  unsigned int i = 0;
  for(unsigned int row = 0; row < nbTilesY; ++row)
    {
    for(unsigned int col = 0; col < nbTilesX; ++col)
      {

54
      // Compute current tile start and size
remicres's avatar
remicres committed
55 56
      startX = col * tileWidth;
      startY = row * tileHeight;
57 58
      sizeX = tileWidth;
      sizeY = tileHeight;
remicres's avatar
remicres committed
59

remicres's avatar
remicres committed
60 61
      // Current tile size might be different for right and bottom borders
      if (col == nbTilesX -1)
remicres's avatar
remicres committed
62
        {
63
        sizeX += imageWidth % tileWidth;
remicres's avatar
remicres committed
64
        }
remicres's avatar
remicres committed
65
      if (row == nbTilesY -1)
remicres's avatar
remicres committed
66
        {
67
        sizeY += imageHeight % tileHeight;
remicres's avatar
remicres committed
68 69 70 71 72
        }

      /* Margin at the top ? */
      if( row > 0 )
        {
73 74 75
        tiles[i].margin[POS_TOP] = margin;
        tiles[i].rows[0] = row * tileHeight;
        tiles[i].tileNeighbors[NBH_TOP] = i - nbTilesX;
remicres's avatar
remicres committed
76 77 78
        }
      else
        {
remicres's avatar
remicres committed
79
        // Tile is on the top row --> no top margin
80 81 82
        tiles[i].margin[POS_TOP] = 0;
        tiles[i].rows[0] = 0;
        tiles[i].tileNeighbors[NBH_TOP] = -1;
remicres's avatar
remicres committed
83 84 85 86 87
        }

      /* Margin at the right */
      if( col < nbTilesX - 1 )
        {
88 89 90
        tiles[i].margin[POS_RIGHT] = margin;
        tiles[i].columns[1] = col * tileWidth + sizeX - 1; //sizeX
        tiles[i].tileNeighbors[NBH_RIGHT] = i+1;
remicres's avatar
remicres committed
91 92 93
        }
      else
        {
remicres's avatar
remicres committed
94
        // Tile is on the right column --> no right margin
95 96 97
        tiles[i].margin[POS_RIGHT] = 0;
        tiles[i].columns[1] = imageWidth - 1;
        tiles[i].tileNeighbors[NBH_RIGHT] = -1;
remicres's avatar
remicres committed
98 99 100 101 102
        }

      /* Margin at the bottom */
      if( row < nbTilesY - 1)
        {
103 104 105
        tiles[i].margin[POS_BOTTOM] = margin;
        tiles[i].rows[1] = row * tileHeight + sizeY - 1; // sizeY
        tiles[i].tileNeighbors[NBH_BOTTOM] = i + nbTilesX;
remicres's avatar
remicres committed
106 107 108
        }
      else
        {
remicres's avatar
remicres committed
109
        // Tile is on the bottom --> no bottom margin
110 111 112
        tiles[i].margin[POS_BOTTOM] = 0;
        tiles[i].rows[1] = imageHeight - 1;
        tiles[i].tileNeighbors[NBH_BOTTOM] = -1;
remicres's avatar
remicres committed
113 114 115 116 117
        }

      /* Margin at the left */
      if( col > 0 )
        {
118 119 120
        tiles[i].margin[POS_LEFT] = margin;
        tiles[i].columns[0] = col * tileWidth;
        tiles[i].tileNeighbors[NBH_LEFT] = i-1;
remicres's avatar
remicres committed
121 122 123
        }
      else
        {
remicres's avatar
remicres committed
124
        // Tile is on the left --> no left margin
125 126 127
        tiles[i].margin[POS_LEFT] = 0;
        tiles[i].columns[0] = 0;
        tiles[i].tileNeighbors[NBH_LEFT] = -1;
remicres's avatar
remicres committed
128 129
        }

remicres's avatar
remicres committed
130 131
      /* Store the tile region */
      typename TInputImage::IndexType index;
132 133
      index[0] = startX - tiles[i].margin[POS_LEFT];
      index[1] = startY - tiles[i].margin[POS_TOP];
remicres's avatar
remicres committed
134
      typename TInputImage::SizeType size;
135 136
      size[0] = sizeX + tiles[i].margin[POS_LEFT] + tiles[i].margin[POS_RIGHT];
      size[1] = sizeY + tiles[i].margin[POS_TOP] + tiles[i].margin[POS_BOTTOM];
remicres's avatar
remicres committed
137
      typename TInputImage::RegionType region(index, size);
138
      tiles[i].region = region;
remicres's avatar
remicres committed
139

remicres's avatar
remicres committed
140
      std::cout << "Tile " << i << ": start at " << index << " with size " << size << std::endl;
remicres's avatar
remicres committed
141

remicres's avatar
remicres committed
142 143
      /* Is there a neighbor at the rop right */
      if(row > 0 && col < nbTilesX - 1)
144
        tiles[i].tileNeighbors[NBH_TOP_RIGHT] = i - nbTilesX + 1;
remicres's avatar
remicres committed
145
      else
146
        tiles[i].tileNeighbors[NBH_TOP_RIGHT] = -1;
remicres's avatar
remicres committed
147 148 149

      /* Is there a neighbor at the bottom right */
      if(col < nbTilesX - 1 && row < nbTilesY - 1)
150
        tiles[i].tileNeighbors[NBH_BOTTOM_RIGHT] = i + nbTilesX + 1;
remicres's avatar
remicres committed
151
      else
152
        tiles[i].tileNeighbors[NBH_BOTTOM_RIGHT] = -1;
remicres's avatar
remicres committed
153 154 155

      /* Is there a neighbor at the bottom left */
      if(row < nbTilesY - 1 && col > 0)
156
        tiles[i].tileNeighbors[NBH_BOTTOM_LEFT] = i + nbTilesX - 1;
remicres's avatar
remicres committed
157
      else
158
        tiles[i].tileNeighbors[NBH_BOTTOM_LEFT] = -1;
remicres's avatar
remicres committed
159 160 161

      /* Is there a neighbor at the top left */
      if(col > 0 && row > 0)
162
        tiles[i].tileNeighbors[NBH_TOP_LEFT] = i - nbTilesX - 1;
remicres's avatar
remicres committed
163
      else
164 165 166 167 168 169 170
        tiles[i].tileNeighbors[NBH_TOP_LEFT] = -1;

      std::string suffix = std::to_string(row) + "_" + std::to_string(col) + ".bin";
      tiles[i].nodeFileName = temporaryFilesPrefix + "_node_" + suffix;
      tiles[i].edgeFileName = temporaryFilesPrefix + "_edge_" + suffix;
      tiles[i].nodeMarginFileName = temporaryFilesPrefix + "_nodeMargin_" + suffix;
      tiles[i].edgeMarginFileName = temporaryFilesPrefix + "_edgeMargin_" + suffix;
remicres's avatar
remicres committed
171 172 173 174 175 176

      i++;
      } // end for(unsigned int col = 0; col < nbTilesX; ++col)

    } // for(unsigned int row = 0; row < nbTilesY; ++row)

177
  return tiles;
remicres's avatar
remicres committed
178
    }
179
} // end of namespace lsgrm
180
#endif