34 #include <boost/make_shared.hpp> 55 ImageCachePtr image_cache = boost::make_shared<ImageCache>(
"/tmp/tile_map");
56 tile_cache_ = boost::make_shared<TextureCache>(image_cache);
85 for (
size_t i = 0; i <
tiles_.size(); i++)
87 for (
size_t j = 0; j <
tiles_[i].points_t.size(); j++)
93 for (
size_t i = 0; i <
precache_.size(); i++)
95 for (
size_t j = 0; j <
precache_[i].points_t.size(); j++)
109 latitude = std::max(-90.0, std::min(90.0, latitude));
110 longitude = std::max(-180.0, std::min(180.0, longitude));
122 double lat_circumference =
125 std::max(
tile_source_->GetMinZoom(),
static_cast<int32_t
>(std::ceil(std::log(lat_circumference) / std::log(2) - 8))));
126 int64_t max_size = std::pow(2, level);
128 int64_t center_x = std::min(max_size - 1, static_cast<int64_t>(
129 std::floor(((longitude + 180.0) / 360.0) * std::pow(2.0, level))));
130 int64_t center_y = std::min(max_size - 1, static_cast<int64_t>(
131 std::floor((1.0 - std::log(std::tan(lat) + 1.0 / std::cos(lat)) /
swri_math_util::_pi) / 2.0 * std::pow(2.0, level))));
136 double max_dimension = std::max(width, height);
139 double tile_size = 256.0 * (meters_per_pixel / scale);
141 int64_t size = std::max(static_cast<int64_t>(1L), std::min(max_size, static_cast<int64_t>(
142 std::ceil(0.5 * max_dimension / tile_size) * 2 + 1)));
146 ROS_ERROR(
"Invalid map size: %ld", size);
157 int64_t top = std::max(static_cast<int64_t>(0L),
center_y_ -
size_ / 2);
158 int64_t left = std::max(static_cast<int64_t>(0L),
center_x_ -
size_ / 2);
160 int64_t right = std::min(max_size, left +
size_);
161 int64_t bottom = std::min(max_size, top +
size_);
163 for (
size_t i = 0; i <
tiles_.size(); i++)
169 for (int64_t i = top; i < bottom; i++)
171 for (int64_t j = left; j < right; j++)
179 for (
size_t i = 0; i <
precache_.size(); i++)
187 int64_t precache_x = std::floor(((longitude + 180.0) / 360.0) * std::pow(2.0, level - 1));
188 int64_t precache_y = std::floor((1.0 - std::log(std::tan(lat) + 1.0 / std::cos(lat)) /
swri_math_util::_pi) / 2.0 * std::pow(2.0, level - 1));
190 int64_t precache_max_size = std::pow(2, level - 1);
192 int64_t precache_top = std::max(static_cast<int64_t>(0L), precache_y - (
size_ - 1) / 2);
193 int64_t precache_left = std::max(static_cast<int64_t>(0L), precache_x - (
size_ - 1) / 2);
195 int64_t precache_right = std::min(precache_max_size, precache_left +
size_);
196 int64_t precache_bottom = std::min(precache_max_size, precache_top +
size_);
198 for (int64_t i = precache_top; i < precache_bottom; i++)
200 for (int64_t j = precache_left; j < precache_right; j++)
213 for (
size_t i = 0; i < tiles.size(); i++)
220 texture =
tile_cache_->GetTexture(tiles[i].url_hash, tiles[i].url, failed, priority);
225 glBindTexture(GL_TEXTURE_2D, texture->id);
227 glBegin(GL_TRIANGLES);
229 glColor4f(1.0
f, 1.0
f, 1.0
f, 1.0
f);
231 for (int32_t row = 0; row < tiles[i].subdiv_count; row++)
233 for (int32_t col = 0; col < tiles[i].subdiv_count; col++)
235 double u_0 = col * tiles[i].subwidth;
236 double v_0 = 1.0 - row * tiles[i].subwidth;
237 double u_1 = (col + 1.0) * tiles[i].subwidth;
238 double v_1 = 1.0 - (row + 1.0) * tiles[i].subwidth;
240 const tf::Vector3& tl = tiles[i].points_t[row * (tiles[i].subdiv_count + 1) + col];
241 const tf::Vector3& tr = tiles[i].points_t[row * (tiles[i].subdiv_count + 1) + col + 1];
242 const tf::Vector3& br = tiles[i].points_t[(row + 1) * (tiles[i].subdiv_count + 1) + col + 1];
243 const tf::Vector3& bl = tiles[i].points_t[(row + 1) * (tiles[i].subdiv_count + 1) + col];
246 glTexCoord2f(u_0, v_0); glVertex2d(tl.
x(), tl.
y());
247 glTexCoord2f(u_1, v_0); glVertex2d(tr.
x(), tr.
y());
248 glTexCoord2f(u_1, v_1); glVertex2d(br.
x(), br.
y());
251 glTexCoord2f(u_0, v_0); glVertex2d(tl.
x(), tl.
y());
252 glTexCoord2f(u_1, v_1); glVertex2d(br.
x(), br.
y());
253 glTexCoord2f(u_0, v_1); glVertex2d(bl.
x(), bl.
y());
259 glBindTexture(GL_TEXTURE_2D, 0);
271 glEnable(GL_TEXTURE_2D);
276 glDisable(GL_TEXTURE_2D);
281 double n = std::pow(2, level);
282 longitude = x / n * 360.0 - 180.0;
299 int32_t subdivs = std::max(0, 4 - level);
300 tile.
subwidth = 1.0 / (subdivs + 1.0);
313 for (
size_t i = 0; i < tile.
points_t.size(); i++)
void SetTileSource(const boost::shared_ptr< TileSource > &tile_source)
static const long double _rad_2_deg
std::vector< Tile > precache_
void ToLatLon(int32_t level, double x, double y, double &latitude, double &longitude)
void InitializeTile(int32_t level, int64_t x, int64_t y, Tile &tile, int priority)
TFSIMD_FORCE_INLINE const tfScalar & x() const
static const long double _2pi
swri_transform_util::Transform transform_
TFSIMD_FORCE_INLINE const tfScalar & y() const
std::vector< Tile > tiles_
void SetView(double latitude, double longitude, double scale, int32_t width, int32_t height)
void DrawTiles(std::vector< Tile > &tiles, int priority)
static const long double _pi
double ToRadians(double degrees)
TextureCachePtr tile_cache_
void SetTransform(const swri_transform_util::Transform &transform)
std::vector< tf::Vector3 > points
boost::shared_ptr< TileSource > tile_source_
std::vector< tf::Vector3 > points_t