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

namespace lsgrm
{
remicres's avatar
remicres committed
5

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

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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
template <class TInputImage>
void SplitOTBImage(TInputImage * imagePtr, // input image
    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,
    std::vector<ProcessingTile> &m_Tiles)
{

  // 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*/
  m_Tiles.assign(nbTilesX * nbTilesY, ProcessingTile());
  unsigned int i = 0;
  for(unsigned int row = 0; row < nbTilesY; ++row)
    {
    for(unsigned int col = 0; col < nbTilesX; ++col)
      {

remicres's avatar
remicres committed
50
      // Compute current tile start
remicres's avatar
remicres committed
51 52 53
      startX = col * tileWidth;
      startY = row * tileHeight;

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

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

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

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

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

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

      std::cout << "Tile " << i << ": ";
      region.Print(std::cout);

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

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

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

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

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

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

}
175
} // end of namespace lsgrm