tile.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.h>
00031 
00032 // C++ standard libraries
00033 #include <cmath>
00034 #include <algorithm>
00035 #include <exception>
00036 #include <iostream>
00037 
00038 // QT libraries
00039 #include <QGLWidget>
00040 #include <QFile>
00041 
00042 #include <swri_math_util/math_util.h>
00043 
00044 namespace multires_image
00045 {
00046   Tile::Tile(
00047          const std::string& path, int column, int row, int level,
00048          const tf::Point& topLeft, const tf::Point& topRight,
00049          const tf::Point& bottomLeft, const tf::Point& bottomRight) :
00050     m_path(path),
00051     m_column(column),
00052     m_row(row),
00053     m_level(level),
00054     m_top_left(topLeft),
00055     m_top_right(topRight),
00056     m_bottom_right(bottomRight),
00057     m_bottom_left(bottomLeft),
00058     m_transformed_top_left(topLeft),
00059     m_transformed_top_right(topRight),
00060     m_transformed_bottom_right(bottomRight),
00061     m_transformed_bottom_left(bottomLeft),
00062     m_failed(false),
00063     m_textureLoaded(false),
00064     m_dimension(0),
00065     m_textureId(0),
00066     m_tileId(1000000 * level + 1000 * column + row),
00067     m_memorySize(0)
00068   {
00069   }
00070 
00071   Tile::~Tile(void)
00072   {
00073   }
00074 
00075   bool Tile::Exists()
00076   {
00077     return QFile::exists(m_path.c_str());
00078   }
00079 
00080   bool Tile::LoadImageToMemory(bool gl)
00081   {
00082     if (!m_failed)
00083     {
00084       m_mutex.lock();
00085 
00086       try
00087       {
00088         QImage nullImage;
00089         m_image = nullImage;
00090 
00091         if (m_image.load(m_path.c_str()))
00092         {
00093           if (gl)
00094           {
00095             int width = m_image.width();
00096             int height = m_image.height();
00097 
00098             float max_dim = std::max(width, height);
00099             m_dimension = swri_math_util::Round(
00100               std::pow(2.0f, std::ceil(std::log(max_dim)/std::log(2.0f))));
00101 
00102             if (width != m_dimension || height != m_dimension)
00103             {
00104               m_image = m_image.scaled(m_dimension, m_dimension, Qt::IgnoreAspectRatio, Qt::FastTransformation);
00105             }
00106 
00107             m_memorySize = m_dimension * m_dimension * 4;
00108 
00109             m_image = QGLWidget::convertToGLFormat(m_image);
00110           }
00111         }
00112         else
00113         {
00114           m_failed = true;
00115         }
00116       }
00117       catch(std::exception& e)
00118       {
00119         std::cout << "An exception occurred loading image: " << e.what() << std::endl;
00120         m_failed = true;
00121       }
00122 
00123       m_mutex.unlock();
00124     }
00125 
00126     return !m_failed;
00127   }
00128 
00129   void Tile::UnloadImage()
00130   {
00131     m_mutex.lock();
00132 
00133     QImage nullImage;
00134     m_image = nullImage;
00135 
00136     m_mutex.unlock();
00137   }
00138 
00139   bool Tile::LoadTexture()
00140   {
00141     if (!m_textureLoaded && !m_failed)
00142     {
00143       m_mutex.lock();
00144 
00145       try
00146       {
00147         GLuint ids[1];
00148         glGenTextures(1, &ids[0]);
00149         m_textureId = ids[0];
00150 
00151         glBindTexture(GL_TEXTURE_2D, m_textureId);
00152         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_dimension, m_dimension, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_image.bits());
00153 
00154         // TODO(malban): check for GL error
00155 
00156         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00157         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00158 
00159         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00160         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00161 
00162         m_textureLoaded = true;
00163       }
00164       catch (const std::exception& e)
00165       {
00166         std::cout << "An exception occured loading texture: " << e.what() << std::endl;
00167         m_failed = true;
00168       }
00169 
00170       m_mutex.unlock();
00171     }
00172 
00173     return m_textureLoaded;
00174   }
00175 
00176   void Tile::UnloadTexture()
00177   {
00178     m_mutex.lock();
00179 
00180     if (m_textureLoaded)
00181     {
00182       m_textureLoaded = false;
00183       GLuint ids[1];
00184       ids[0] = m_textureId;
00185       glDeleteTextures(1, &ids[0]);
00186     }
00187 
00188     m_mutex.unlock();
00189   }
00190 
00191   void Tile::Draw()
00192   {
00193     if (!m_failed)
00194     {
00195       if (m_textureLoaded)
00196       {
00197         glBindTexture(GL_TEXTURE_2D, m_textureId);
00198 
00199         glBegin(GL_QUADS);
00200 
00201         glTexCoord2f(0, 1); glVertex2f(m_transformed_top_left.x(), m_transformed_top_left.y());
00202         glTexCoord2f(1, 1); glVertex2f(m_transformed_top_right.x(), m_transformed_top_right.y());
00203         glTexCoord2f(1, 0); glVertex2f(m_transformed_bottom_right.x(), m_transformed_bottom_right.y());
00204         glTexCoord2f(0, 0); glVertex2f(m_transformed_bottom_left.x(), m_transformed_bottom_left.y());
00205 
00206         glEnd();
00207       }
00208     }
00209   }
00210 
00211   void Tile::Transform(const swri_transform_util::Transform& transform)
00212   {
00213     m_transformed_top_left = transform * m_top_left;
00214     m_transformed_top_right = transform * m_top_right;
00215     m_transformed_bottom_left = transform * m_bottom_left;
00216     m_transformed_bottom_right = transform * m_bottom_right;
00217   }
00218 }
00219 


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