tile_set_layer.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 <cstdio>
35 
36 // QT libraries
37 #include <QString>
38 
39 namespace multires_image
40 {
42  const std::string& path,
43  int tileSize, int layer) :
44  m_geo(geo),
45  m_path(path),
46  m_tileSize(tileSize),
47  m_layer(layer),
48  m_scale(std::pow(2.0, m_layer)),
49  m_expectTiles(true)
50  {
51  // Calculate the width and height in pixels of this layer
52  float width = std::ceil(m_geo.Width() / std::pow(2.0f, layer));
53  float height = std::ceil(m_geo.Height() / std::pow(2.0f, layer));
54 
55  // Calculate the number for tile rows and columns for this layer
56  m_columns = std::ceil(width / tileSize);
57  m_rows = std::ceil(height / tileSize);
58 
59  m_tiles.reserve(m_columns);
60  for (int c = 0; c < m_columns; c++)
61  {
62  m_tiles.push_back(std::vector<Tile*>());
63  m_tiles[c].reserve(m_rows);
64  }
65  }
66 
68  {
69  }
70 
72  {
73  return Load("jpg");
74  }
75 
76  bool TileSetLayer::Load(const std::string extension)
77  {
78  bool needsTiles = false;
79 
80  for (int32_t c = 0; c < m_columns; c++)
81  {
82  for (int32_t r = 0; r < m_rows; r++)
83  {
84  std::string rowString = QString::number(r).toStdString();
85  while (rowString.length() < 5) rowString = '0' + rowString;
86 
87  std::string columnString = QString::number(c).toStdString();
88  while (columnString.length() < 5) columnString = '0' + columnString;
89 
90  // Get 4 corners of this tile
91  int left = c * m_tileSize * m_scale;
92  int top = r * m_tileSize * m_scale;
93  int bottom = (r + 1) * m_tileSize * m_scale;
94  int right = (c + 1) * m_tileSize * m_scale;
95 
96  if (right > (int64_t)m_geo.Width())
97  {
98  right = m_geo.Width();
99  }
100  if (bottom > (int64_t)m_geo.Height())
101  {
102  bottom = m_geo.Height();
103  }
104 
105  double x, y;
106  m_geo.GetCoordinate(left, top, x, y);
107  tf::Point top_left(x, y, 0);
108 
109  m_geo.GetCoordinate(right, top, x, y);
110  tf::Point top_right(x, y, 0);
111 
112  m_geo.GetCoordinate(left, bottom, x, y);
113  tf::Point bottom_left(x, y, 0);
114 
115  m_geo.GetCoordinate(right, bottom, x, y);
116  tf::Point bottom_right(x, y, 0);
117 
118  m_tiles[c].push_back(new Tile(
119  m_path + "/tile" + rowString + "x" + columnString + "." + extension,
120  c, r, m_layer, top_left, top_right, bottom_left, bottom_right));
121 
122  needsTiles |= !m_tiles[c][r]->Exists();
123  }
124  }
125 
126  if (needsTiles)
127  {
128  if (m_expectTiles)
129  {
130  printf("Error: Missing expected tiles\n");
131  return false;
132  }
133  }
134 
135  return true;
136  }
137 
138  void TileSetLayer::GetTileIndex(double x, double y, int& row, int& column) const
139  {
140  tf::Point position(x, y, 0);
141  GetTileIndex(position, row, column);
142  }
143 
144  void TileSetLayer::GetTileIndex(const tf::Point& position, int& row, int& column) const
145  {
146  int x, y;
147  m_geo.GetPixel(position.x(), position.y(), x, y);
148 
149  column = static_cast<int>(x / (m_scale * m_tileSize));
150  row = static_cast<int>(y / (m_scale * m_tileSize));
151  }
152 
154  const tf::Point& top_left,
155  const tf::Point& bottom_right,
156  int& startRow, int& startColumn,
157  int& endRow, int& endColumn) const
158  {
159  GetTileIndex(top_left.x(), top_left.y(), startRow, startColumn);
160  if (startColumn < 0)
161  {
162  startColumn = 0;
163  }
164  if ((uint32_t)startColumn >= m_tiles.size())
165  {
166  startColumn = m_tiles.size() - 1;
167  }
168  if (startRow < 0)
169  {
170  startRow = 0;
171  }
172  if ((uint32_t)startRow >= m_tiles[0].size())
173  {
174  startRow = m_tiles[0].size() - 1;
175  }
176 
177  GetTileIndex(bottom_right.x(), bottom_right.y(), endRow, endColumn);
178  if (endColumn < 0)
179  {
180  endColumn = 0;
181  }
182  if ((uint32_t)endColumn >= m_tiles.size())
183  {
184  endColumn = m_tiles.size() - 1;
185  }
186  if (endRow < 0)
187  {
188  endRow = 0;
189  }
190  if ((uint32_t)endRow >= m_tiles[0].size())
191  {
192  endRow = m_tiles[0].size() - 1;
193  }
194  }
195 }
196 
f
void GetTileIndex(const tf::Point &position, int &row, int &column) const
const swri_transform_util::GeoReference & m_geo
void GetTileRange(const tf::Point &top_left, const tf::Point &bottom_right, int &startRow, int &startColumn, int &endRow, int &endColumn) const
tf::Vector3 Point
void GetPixel(double x_coordinate, double y_coordinate, int &x_pixel, int &y_pixel) const
TileSetLayer(const swri_transform_util::GeoReference &geo, const std::string &path, int tileSize, int layer)
void GetCoordinate(int x_pixel, int y_pixel, double &x_coordinate, double &y_coordinate) const
std::vector< std::vector< Tile * > > m_tiles


multires_image
Author(s): Marc Alban
autogenerated on Fri Dec 16 2022 03:59:39