tile_view.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
31 
32 // C++ standard libraries
33 #include <cmath>
34 #include <iostream>
35 
36 namespace multires_image
37 {
38  TileView::TileView(TileSet* tiles, QGLWidget* widget) :
39  m_tiles(tiles),
40  m_cache(tiles, widget),
41  m_currentLayer(tiles->LayerCount() - 1),
42  m_startRow(0),
43  m_startColumn(0),
44  m_endRow(0),
45  m_endColumn(0)
46  {
47  double top, left, bottom, right;
48  tiles->GeoReference().GetCoordinate(0, 0, left, top);
49  tiles->GeoReference().GetCoordinate(tiles->GeoReference().Width(),tiles->GeoReference().Height(), right, bottom);
50 
51  double scale_x = std::abs(right - left) / tiles->GeoReference().Width();
52  double scale_y = std::abs(top - bottom) / tiles->GeoReference().Height();
53 
54  min_scale_ = scale_x;
55  if (scale_y > scale_x)
56  min_scale_ = scale_y;
57  }
58 
60  {
61  }
62 
63  void TileView::SetView(double x, double y, double radius, double scale)
64  {
65  int layer = 0;
66  while (min_scale_ * std::pow(2.0, layer + 1) < scale) layer++;
67 
68  if (layer >= m_tiles->LayerCount())
69  layer = m_tiles->LayerCount() - 1;
70 
71  if (layer != m_currentLayer)
72  {
73  m_currentLayer = layer;
74  m_cache.SetCurrentLayer(layer);
75  }
76 
77  int row, column;
78  m_tiles->GetLayer(m_currentLayer)->GetTileIndex(x, y, row, column);
79 
80  m_startRow = row - 2;
81  if (m_startRow < 0)
82  m_startRow = 0;
85 
86  m_endRow = row + 2;
87  if (m_endRow < 0)
88  m_endRow = 0;
91 
92  m_startColumn = column - 2;
93  if (m_startColumn < 0)
94  m_startColumn = 0;
97 
98  m_endColumn = column + 2;
99  if (m_endColumn < 0)
100  m_endColumn = 0;
103 
104  m_cache.Precache(x, y);
105  }
106 
108  {
109  glEnable(GL_TEXTURE_2D);
110 
111  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
112 
113  // Always draw bottom layers
114  if (m_currentLayer != m_tiles->LayerCount() - 1)
115  {
116  TileSetLayer* baseLayer = m_tiles->GetLayer(m_tiles->LayerCount() - 1);
117  Tile* tile = baseLayer->GetTile(0, 0);
118  if (tile->TextureLoaded())
119  {
120  tile->Draw();
121  }
122  else
123  {
124  m_cache.Load(tile);
125  }
126  }
127 
128  if (m_tiles->LayerCount() >= 2 && m_currentLayer != m_tiles->LayerCount() - 2)
129  {
130  TileSetLayer* baseLayer = m_tiles->GetLayer(m_tiles->LayerCount() - 2);
131  for (int c = 0; c < baseLayer->ColumnCount(); c++)
132  {
133  for (int r = 0; r < baseLayer->RowCount(); r++)
134  {
135  Tile* tile = baseLayer->GetTile(c, r);
136  if (tile->TextureLoaded())
137  {
138  tile->Draw();
139  }
140  else
141  {
142  m_cache.Load(tile);
143  }
144  }
145  }
146  }
147 
149  if (m_endColumn < layer->ColumnCount() && m_endRow < layer->RowCount())
150  {
151  for (int c = m_startColumn; c <= m_endColumn; c++)
152  {
153  for (int r = m_startRow; r <= m_endRow; r++)
154  {
155  Tile* tile = layer->GetTile(c, r);
156  if (tile->TextureLoaded())
157  {
158  tile->Draw();
159  }
160  else
161  {
162  m_cache.Load(tile);
163  }
164  }
165  }
166  }
167 
168  glDisable(GL_TEXTURE_2D);
169  }
170 }
171 
multires_image::TileView::m_cache
TileCache m_cache
Definition: tile_view.h:57
multires_image
Definition: multires_view_node.h:51
multires_image::TileSetLayer::GetTileIndex
void GetTileIndex(const tf::Point &position, int &row, int &column) const
Definition: tile_set_layer.cpp:144
multires_image::TileView::Draw
void Draw()
Definition: tile_view.cpp:107
multires_image::TileSetLayer
Definition: tile_set_layer.h:45
multires_image::TileView::m_startColumn
int m_startColumn
Definition: tile_view.h:60
multires_image::Tile
Definition: tile.h:50
multires_image::TileSet::GetLayer
TileSetLayer * GetLayer(int layer)
Definition: tile_set.h:61
multires_image::TileCache::Precache
void Precache(const tf::Point &position)
Definition: tile_cache.cpp:125
multires_image::TileSet::GeoReference
swri_transform_util::GeoReference & GeoReference()
Definition: tile_set.h:59
multires_image::TileSetLayer::GetTile
Tile * GetTile(int column, int row)
Definition: tile_set_layer.h:58
multires_image::TileView::~TileView
~TileView(void)
Definition: tile_view.cpp:59
f
f
multires_image::TileView::m_startRow
int m_startRow
Definition: tile_view.h:59
multires_image::TileView::min_scale_
double min_scale_
Definition: tile_view.h:63
multires_image::TileSet::LayerCount
int LayerCount()
Definition: tile_set.h:56
multires_image::TileView::m_endRow
int m_endRow
Definition: tile_view.h:61
multires_image::TileView::m_tiles
TileSet * m_tiles
Definition: tile_view.h:56
swri_transform_util::GeoReference::GetCoordinate
void GetCoordinate(int x_pixel, int y_pixel, double &x_coordinate, double &y_coordinate) const
tile_view.h
multires_image::TileView::m_endColumn
int m_endColumn
Definition: tile_view.h:62
swri_transform_util::GeoReference::Height
unsigned int Height() const
multires_image::TileCache::SetCurrentLayer
void SetCurrentLayer(int layer)
Definition: tile_cache.h:64
multires_image::TileView::m_currentLayer
int m_currentLayer
Definition: tile_view.h:58
swri_transform_util::GeoReference::Width
unsigned int Width() const
multires_image::TileSetLayer::RowCount
int RowCount()
Definition: tile_set_layer.h:68
multires_image::TileCache::Load
void Load(Tile *tile)
Definition: tile_cache.cpp:97
multires_image::TileSet
Definition: tile_set.h:43
multires_image::TileSetLayer::ColumnCount
int ColumnCount()
Definition: tile_set_layer.h:69
multires_image::TileView::TileView
TileView(TileSet *tiles, QGLWidget *widget)
Definition: tile_view.cpp:38
multires_image::Tile::TextureLoaded
bool TextureLoaded() const
Definition: tile.h:63
multires_image::Tile::Draw
void Draw()
Definition: tile.cpp:191
multires_image::TileView::SetView
void SetView(double x, double y, double radius, double scale)
Definition: tile_view.cpp:63


multires_image
Author(s): Marc Alban
autogenerated on Sun Sep 8 2024 02:27:21