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


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