tile_set_layer.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 
00030 #include <multires_image/tile_set_layer.h>
00031 
00032 // C++ standard libraries
00033 #include <cmath>
00034 #include <cstdio>
00035 
00036 // QT libraries
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     // Calculate the width and height in pixels of this layer
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     // Calculate the number for tile rows and columns for this layer
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         // Get 4 corners of this tile
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 


multires_image
Author(s): Marc Alban
autogenerated on Thu Aug 24 2017 02:46:18