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


multires_image
Author(s): Marc Alban
autogenerated on Thu Jun 6 2019 18:51:15