00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <multires_image/tile_set_layer.h>
00031
00032
00033 #include <cmath>
00034 #include <cstdio>
00035
00036
00037 #include <QString>
00038
00039 namespace multires_image
00040 {
00041 TileSetLayer::TileSetLayer(const swri_transform_util::GeoReference& geo,
00042 const std::string& path,
00043 int tileSize, int layer) :
00044 m_geo(geo),
00045 m_path(path),
00046 m_tileSize(tileSize),
00047 m_layer(layer),
00048 m_scale(std::pow(2.0, m_layer)),
00049 m_expectTiles(true)
00050 {
00051
00052 float width = std::ceil(m_geo.Width() / std::pow(2.0f, layer));
00053 float height = std::ceil(m_geo.Height() / std::pow(2.0f, layer));
00054
00055
00056 m_columns = std::ceil(width / tileSize);
00057 m_rows = std::ceil(height / tileSize);
00058
00059 m_tiles.reserve(m_columns);
00060 for (int c = 0; c < m_columns; c++)
00061 {
00062 m_tiles.push_back(std::vector<Tile*>());
00063 m_tiles[c].reserve(m_rows);
00064 }
00065 }
00066
00067 TileSetLayer::~TileSetLayer(void)
00068 {
00069 }
00070
00071 bool TileSetLayer::Load()
00072 {
00073 return Load("jpg");
00074 }
00075
00076 bool TileSetLayer::Load(const std::string extension)
00077 {
00078 bool needsTiles = false;
00079
00080 for (int32_t c = 0; c < m_columns; c++)
00081 {
00082 for (int32_t r = 0; r < m_rows; r++)
00083 {
00084 std::string rowString = QString::number(r).toStdString();
00085 while (rowString.length() < 5) rowString = '0' + rowString;
00086
00087 std::string columnString = QString::number(c).toStdString();
00088 while (columnString.length() < 5) columnString = '0' + columnString;
00089
00090
00091 int left = c * m_tileSize * m_scale;
00092 int top = r * m_tileSize * m_scale;
00093 int bottom = (r + 1) * m_tileSize * m_scale;
00094 int right = (c + 1) * m_tileSize * m_scale;
00095
00096 if (right > (int64_t)m_geo.Width())
00097 {
00098 right = m_geo.Width();
00099 }
00100 if (bottom > (int64_t)m_geo.Height())
00101 {
00102 bottom = m_geo.Height();
00103 }
00104
00105 double x, y;
00106 m_geo.GetCoordinate(left, top, x, y);
00107 tf::Point top_left(x, y, 0);
00108
00109 m_geo.GetCoordinate(right, top, x, y);
00110 tf::Point top_right(x, y, 0);
00111
00112 m_geo.GetCoordinate(left, bottom, x, y);
00113 tf::Point bottom_left(x, y, 0);
00114
00115 m_geo.GetCoordinate(right, bottom, x, y);
00116 tf::Point bottom_right(x, y, 0);
00117
00118 m_tiles[c].push_back(new Tile(
00119 m_path + "/tile" + rowString + "x" + columnString + "." + extension,
00120 c, r, m_layer, top_left, top_right, bottom_left, bottom_right));
00121
00122 needsTiles |= !m_tiles[c][r]->Exists();
00123 }
00124 }
00125
00126 if (needsTiles)
00127 {
00128 if (m_expectTiles)
00129 {
00130 printf("Error: Missing expected tiles\n");
00131 return false;
00132 }
00133 }
00134
00135 return true;
00136 }
00137
00138 void TileSetLayer::GetTileIndex(double x, double y, int& row, int& column) const
00139 {
00140 tf::Point position(x, y, 0);
00141 GetTileIndex(position, row, column);
00142 }
00143
00144 void TileSetLayer::GetTileIndex(const tf::Point& position, int& row, int& column) const
00145 {
00146 int x, y;
00147 m_geo.GetPixel(position.x(), position.y(), x, y);
00148
00149 column = static_cast<int>(x / (m_scale * m_tileSize));
00150 row = static_cast<int>(y / (m_scale * m_tileSize));
00151 }
00152
00153 void TileSetLayer::GetTileRange(
00154 const tf::Point& top_left,
00155 const tf::Point& bottom_right,
00156 int& startRow, int& startColumn,
00157 int& endRow, int& endColumn) const
00158 {
00159 GetTileIndex(top_left.x(), top_left.y(), startRow, startColumn);
00160 if (startColumn < 0)
00161 {
00162 startColumn = 0;
00163 }
00164 if ((uint32_t)startColumn >= m_tiles.size())
00165 {
00166 startColumn = m_tiles.size() - 1;
00167 }
00168 if (startRow < 0)
00169 {
00170 startRow = 0;
00171 }
00172 if ((uint32_t)startRow >= m_tiles[0].size())
00173 {
00174 startRow = m_tiles[0].size() - 1;
00175 }
00176
00177 GetTileIndex(bottom_right.x(), bottom_right.y(), endRow, endColumn);
00178 if (endColumn < 0)
00179 {
00180 endColumn = 0;
00181 }
00182 if ((uint32_t)endColumn >= m_tiles.size())
00183 {
00184 endColumn = m_tiles.size() - 1;
00185 }
00186 if (endRow < 0)
00187 {
00188 endRow = 0;
00189 }
00190 if ((uint32_t)endRow >= m_tiles[0].size())
00191 {
00192 endRow = m_tiles[0].size() - 1;
00193 }
00194 }
00195 }
00196