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);
80 for (
size_t i = 0; i <
tiles_.size(); i++)
82 for (
size_t j = 0; j <
tiles_[i].points_t.size(); j++)
88 for (
size_t i = 0; i <
precache_.size(); i++)
90 for (
size_t j = 0; j <
precache_[i].points_t.size(); j++)
104 latitude = std::max(-90.0, std::min(90.0, latitude));
105 longitude = std::max(-180.0, std::min(180.0, longitude));
117 double lat_circumference =
120 std::max(
tile_source_->GetMinZoom(),
static_cast<int32_t
>(std::ceil(std::log(lat_circumference) / std::log(2) - 8))));
121 int64_t max_size = std::pow(2, level);
123 int64_t center_x = std::min(max_size - 1, static_cast<int64_t>(
124 std::floor(((longitude + 180.0) / 360.0) * std::pow(2.0, level))));
125 int64_t center_y = std::min(max_size - 1, static_cast<int64_t>(
126 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))));
131 double max_dimension = std::max(width, height);
134 double tile_size = 256.0 * (meters_per_pixel / scale);
136 int64_t size = std::max(static_cast<int64_t>(1L), std::min(max_size, static_cast<int64_t>(
137 std::ceil(0.5 * max_dimension / tile_size) * 2 + 1)));
141 ROS_ERROR(
"Invalid map size: %ld", size);
152 int64_t top = std::max(static_cast<int64_t>(0L),
center_y_ -
size_ / 2);
153 int64_t left = std::max(static_cast<int64_t>(0L),
center_x_ -
size_ / 2);
155 int64_t right = std::min(max_size, left +
size_);
156 int64_t bottom = std::min(max_size, top +
size_);
158 for (
size_t i = 0; i <
tiles_.size(); i++)
164 for (int64_t i = top; i < bottom; i++)
166 for (int64_t j = left; j < right; j++)
174 for (
size_t i = 0; i <
precache_.size(); i++)
182 int64_t precache_x = std::floor(((longitude + 180.0) / 360.0) * std::pow(2.0, level - 1));
183 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));
185 int64_t precache_max_size = std::pow(2, level - 1);
187 int64_t precache_top = std::max(static_cast<int64_t>(0L), precache_y - (
size_ - 1) / 2);
188 int64_t precache_left = std::max(static_cast<int64_t>(0L), precache_x - (
size_ - 1) / 2);
190 int64_t precache_right = std::min(precache_max_size, precache_left +
size_);
191 int64_t precache_bottom = std::min(precache_max_size, precache_top +
size_);
193 for (int64_t i = precache_top; i < precache_bottom; i++)
195 for (int64_t j = precache_left; j < precache_right; j++)
208 for (
size_t i = 0; i < tiles.size(); i++)
215 texture =
tile_cache_->GetTexture(tiles[i].url_hash, tiles[i].url, failed, priority);
220 glBindTexture(GL_TEXTURE_2D, texture->id);
222 glBegin(GL_TRIANGLES);
224 glColor4f(1.0
f, 1.0
f, 1.0
f, 1.0
f);
226 for (int32_t row = 0; row < tiles[i].subdiv_count; row++)
228 for (int32_t col = 0; col < tiles[i].subdiv_count; col++)
230 double u_0 = col * tiles[i].subwidth;
231 double v_0 = 1.0 - row * tiles[i].subwidth;
232 double u_1 = (col + 1.0) * tiles[i].subwidth;
233 double v_1 = 1.0 - (row + 1.0) * tiles[i].subwidth;
235 const tf::Vector3& tl = tiles[i].points_t[row * (tiles[i].subdiv_count + 1) + col];
236 const tf::Vector3& tr = tiles[i].points_t[row * (tiles[i].subdiv_count + 1) + col + 1];
237 const tf::Vector3& br = tiles[i].points_t[(row + 1) * (tiles[i].subdiv_count + 1) + col + 1];
238 const tf::Vector3& bl = tiles[i].points_t[(row + 1) * (tiles[i].subdiv_count + 1) + col];
241 glTexCoord2f(u_0, v_0); glVertex2d(tl.
x(), tl.
y());
242 glTexCoord2f(u_1, v_0); glVertex2d(tr.
x(), tr.
y());
243 glTexCoord2f(u_1, v_1); glVertex2d(br.
x(), br.
y());
246 glTexCoord2f(u_0, v_0); glVertex2d(tl.
x(), tl.
y());
247 glTexCoord2f(u_1, v_1); glVertex2d(br.
x(), br.
y());
248 glTexCoord2f(u_0, v_1); glVertex2d(bl.
x(), bl.
y());
254 glBindTexture(GL_TEXTURE_2D, 0);
266 glEnable(GL_TEXTURE_2D);
271 glDisable(GL_TEXTURE_2D);
276 double n = std::pow(2, level);
277 longitude = x / n * 360.0 - 180.0;
294 int32_t subdivs = std::max(0, 4 - level);
295 tile.
subwidth = 1.0 / (subdivs + 1.0);
308 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