tile_view.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_view.h>
00031 
00032 // C++ standard libraries
00033 #include <cmath>
00034 #include <iostream>
00035 
00036 namespace multires_image
00037 {
00038   TileView::TileView(TileSet* tiles, QGLWidget* widget) :
00039       m_tiles(tiles),
00040       m_cache(tiles, widget),
00041       m_currentLayer(tiles->LayerCount() - 1),
00042       m_startRow(0),
00043       m_startColumn(0),
00044       m_endRow(0),
00045       m_endColumn(0)
00046   {
00047     double top, left, bottom, right;
00048     tiles->GeoReference().GetCoordinate(0, 0, left, top);
00049     tiles->GeoReference().GetCoordinate(tiles->GeoReference().Width(),tiles->GeoReference().Height(), right, bottom);
00050 
00051     double scale_x = std::abs(right - left) / tiles->GeoReference().Width();
00052     double scale_y = std::abs(top - bottom) / tiles->GeoReference().Height();
00053 
00054     min_scale_ = scale_x;
00055     if (scale_y > scale_x)
00056       min_scale_ = scale_y;
00057   }
00058 
00059   TileView::~TileView(void)
00060   {
00061   }
00062 
00063   void TileView::SetView(double x, double y, double radius, double scale)
00064   {
00065     int layer = 0;
00066     while (min_scale_ * std::pow(2.0, layer + 1) < scale) layer++;
00067 
00068     if (layer >= m_tiles->LayerCount())
00069       layer = m_tiles->LayerCount() - 1;
00070 
00071     if (layer != m_currentLayer)
00072     {
00073       m_currentLayer = layer;
00074       m_cache.SetCurrentLayer(layer);
00075     }
00076 
00077     int row, column;
00078     m_tiles->GetLayer(m_currentLayer)->GetTileIndex(x, y, row, column);
00079 
00080     m_startRow = row - 2;
00081     if (m_startRow < 0)
00082       m_startRow = 0;
00083     if (m_startRow >= m_tiles->GetLayer(m_currentLayer)->RowCount())
00084       m_startRow = m_tiles->GetLayer(m_currentLayer)->RowCount() - 1;
00085 
00086     m_endRow = row + 2;
00087     if (m_endRow < 0)
00088       m_endRow = 0;
00089     if (m_endRow >= m_tiles->GetLayer(m_currentLayer)->RowCount())
00090       m_endRow = m_tiles->GetLayer(m_currentLayer)->RowCount() - 1;
00091 
00092     m_startColumn = column - 2;
00093     if (m_startColumn < 0)
00094       m_startColumn = 0;
00095     if (m_startColumn >= m_tiles->GetLayer(m_currentLayer)->ColumnCount())
00096       m_startColumn = m_tiles->GetLayer(m_currentLayer)->ColumnCount() - 1;
00097 
00098     m_endColumn = column + 2;
00099     if (m_endColumn < 0)
00100       m_endColumn = 0;
00101     if (m_endColumn >= m_tiles->GetLayer(m_currentLayer)->ColumnCount())
00102       m_endColumn = m_tiles->GetLayer(m_currentLayer)->ColumnCount() - 1;
00103 
00104     m_cache.Precache(x, y);
00105   }
00106 
00107   void TileView::Draw()
00108   {
00109     glEnable(GL_TEXTURE_2D);
00110 
00111     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
00112 
00113     // Always draw bottom layers
00114     if (m_currentLayer != m_tiles->LayerCount() - 1)
00115     {
00116       TileSetLayer* baseLayer = m_tiles->GetLayer(m_tiles->LayerCount() - 1);
00117       Tile* tile = baseLayer->GetTile(0, 0);
00118       if (tile->TextureLoaded())
00119       {
00120         tile->Draw();
00121       }
00122       else
00123       {
00124         m_cache.Load(tile);
00125       }
00126     }
00127 
00128     if (m_tiles->LayerCount() >= 2 && m_currentLayer != m_tiles->LayerCount() - 2)
00129     {
00130       TileSetLayer* baseLayer = m_tiles->GetLayer(m_tiles->LayerCount() - 2);
00131       for (int c = 0; c < baseLayer->ColumnCount(); c++)
00132       {
00133         for (int r = 0; r <  baseLayer->RowCount(); r++)
00134         {
00135           Tile* tile = baseLayer->GetTile(c, r);
00136           if (tile->TextureLoaded())
00137           {
00138             tile->Draw();
00139           }
00140           else
00141           {
00142             m_cache.Load(tile);
00143           }
00144         }
00145       }
00146     }
00147 
00148     TileSetLayer* layer = m_tiles->GetLayer(m_currentLayer);
00149     if (m_endColumn < layer->ColumnCount() && m_endRow < layer->RowCount())
00150     {
00151       for (int c = m_startColumn; c <= m_endColumn; c++)
00152       {
00153         for (int r = m_startRow; r <= m_endRow; r++)
00154         {
00155           Tile* tile = layer->GetTile(c, r);
00156           if (tile->TextureLoaded())
00157           {
00158             tile->Draw();
00159           }
00160           else
00161           {
00162             m_cache.Load(tile);
00163           }
00164         }
00165       }
00166     }
00167 
00168     glDisable(GL_TEXTURE_2D);
00169   }
00170 }
00171 


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