Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Tiling Stage Fixes and Improvements #65

Draft
wants to merge 14 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions modules/realm_core/src/cv_grid_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,21 @@ void CvGridMap::add(const CvGridMap &submap, int flag_overlap_handle, bool do_ex
}
}

// Get the data in the overlapping area of both mat
// Hack, don't access larger area than we have
// This should be handled above, but something is a few pixels off
if (m_layers[idx_layer].data.cols < dst_roi.x + dst_roi.width) {
dst_roi.width = submap_layer.data.cols - dst_roi.x;
}
if (m_layers[idx_layer].data.rows < dst_roi.y + dst_roi.height) {
dst_roi.height = submap_layer.data.rows - dst_roi.y;
}
if (submap_layer.data.cols < src_roi.x + src_roi.width) {
src_roi.width = submap_layer.data.cols - src_roi.x;
}
if (submap_layer.data.rows < src_roi.y + src_roi.height) {
src_roi.height = submap_layer.data.rows - src_roi.y;
}

cv::Mat src_data_roi = submap_layer.data(src_roi);
cv::Mat dst_data_roi = m_layers[idx_layer].data(dst_roi);

Expand Down Expand Up @@ -480,10 +494,15 @@ void CvGridMap::mergeMatrices(const cv::Mat &from, cv::Mat &to, int flag_merge_h
break;
case REALM_OVERWRITE_ZERO:
cv::Mat mask;
if (to.type() == CV_32F || to.type() == CV_64F)
mask = (to != to) & (from == from);
else
if (to.type() == CV_32F || to.type() == CV_64F) {
cv::Mat to_mask = to.clone();
cv::Mat from_mask = from.clone();
cv::patchNaNs(to_mask, 0);
cv::patchNaNs(from_mask, 0);
mask = (to_mask == 0) & (from_mask != 0);
} else {
mask = (to == 0) & (from > 0);
}
from.copyTo(to, mask);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/realm_io/include/realm_io/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ std::vector<std::string> split(const char *str, char c = ' ');
* @param dir Directory to grab the filenames
* @return Vector of all files with absolute path
*/
std::vector<std::string> getFileList(const std::string& dir, const std::string &suffix = "");
std::vector<std::string> getFileList(const std::string& dir, const std::string &suffix = "", const std::function<bool(std::string, std::string)>& sort = std::less<>());

/*! TODO: Einbaurichtung implementieren?
* @brief Function to compute a 3x3 rotation matrix based on heading data. It is assumed, that the camera is pointing
Expand Down
17 changes: 10 additions & 7 deletions modules/realm_io/src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,27 @@ std::vector<std::string> io::split(const char *str, char c)
return result;
}

std::vector<std::string> io::getFileList(const std::string& dir, const std::string &suffix)
std::vector<std::string> io::getFileList(const std::string& dir, const std::string &suffix, const std::function<bool(std::string, std::string)>& sort)
{
std::vector<std::string> filenames;
if (!dir.empty())
{
boost::filesystem::path apk_path(dir);
boost::filesystem::recursive_directory_iterator end;

for (boost::filesystem::recursive_directory_iterator it(apk_path); it != end; ++it)
{
for (boost::filesystem::recursive_directory_iterator it(apk_path); it != end; ++it) {
const boost::filesystem::path cp = (*it);

const std::string &filepath = cp.string();
if (suffix.empty() || filepath.substr(filepath.size() - suffix.size(), filepath.size()) == suffix)
filenames.push_back(cp.string());
auto canon_path = boost::filesystem::canonical(cp, "/");
const std::string &filepath = canon_path.string();
if (suffix.empty() || filepath.substr(filepath.size() - suffix.size(), suffix.size()) == suffix) {
filenames.push_back(filepath);
}
}
}
std::sort(filenames.begin(), filenames.end());

// By default, return in reverse sort order, useful for zoom level creating / deletion
std::sort(filenames.begin(), filenames.end(), sort);
return filenames;
}

Expand Down
2 changes: 0 additions & 2 deletions modules/realm_ortho/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ set(HEADER_FILES
${root}/include/realm_ortho/nearest_neighbor.h
${root}/include/realm_ortho/rectification.h
${root}/include/realm_ortho/tile.h
${root}/include/realm_ortho/tile_cache.h
)

set(SOURCE_FILES
Expand All @@ -63,7 +62,6 @@ set(SOURCE_FILES
${root}/src/map_tiler.cpp
${root}/src/rectification.cpp
${root}/src/tile.cpp
${root}/src/tile_cache.cpp
)

# delaunay relies on CGAL to work
Expand Down
59 changes: 38 additions & 21 deletions modules/realm_ortho/include/realm_ortho/map_tiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <realm_ortho/rectification.h>
#include <realm_ortho/gdal_warper.h>
#include <realm_ortho/tile_cache.h>
#include <realm_ortho/tile.h>
#include <realm_core/conversions.h>
#include <realm_core/loguru.h>

Expand Down Expand Up @@ -38,8 +38,9 @@ class MapTiler
/*!
* @brief Besides member initialization the required lookup tables to map zoom level to image resolution are created.
* @param verbosity Flag to set verbose output
* @param verbosity Flag to use TMS standard for tile, false to use Google/OSM
*/
explicit MapTiler(bool verbosity);
explicit MapTiler(bool verbosity, bool use_tms);

~MapTiler() = default;
MapTiler(const MapTiler &other) = default;
Expand All @@ -61,6 +62,38 @@ class MapTiler

double getResolution(int zoom_level);

/*!
* @brief Computes one lat-lon coordinate for a given tile. It represents the upper left corner of the tile.
* @param x Coordinate of tile in x-direction
* @param y Coordinate of tile in y-direction
* @param zoom_level Zoom level of the map
* @param tms Whether TMS or Google standard is used (inverts y axis)
* @return (longitude, latitude) of upper left tile corner
*/
static WGSPose computeLatLonForTile(int x, int y, int zoom_level, bool tms);

/*!
* @brief Computes one lat-lon coordinate for a given tile. It represents the center of the tile. This can be useful
* when converting from tiles to CoG as it can prevent including edge tiles along the boundary.
* @param x Coordinate of tile in x-direction
* @param y Coordinate of tile in y-direction
* @param zoom_level Zoom level of the map
* @param tms Whether TMS or Google standard is used (inverts y axis)
* @return (longitude, latitude) of the center of the tile
*/
static WGSPose computeCenterLatLonForTile(int x, int y, int zoom_level, bool tms);

/*!
* @brief Computes the slippy tile index for a given zoom level that contains the requested coordinate in WGS84. The
* specifications are documented: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Pseudo-code
* @param lat Latitude in WGS84
* @param lon Longitude in WGS84
* @param zoom_level Zoom level of the map
* @param tms Whether TMS or Google standard is used (inverts y axis)
* @return Tile coordinate according to slippy tile standard
*/
static cv::Point2i computeTileFromLatLon(double lat, double lon, int zoom_level, bool tms) ;

private:

/// Flag to set verbose output
Expand All @@ -75,6 +108,9 @@ class MapTiler
/// Shift of the coordinate frame origin
double m_origin_shift;

/// Whether to use TMS or Google/OSM standards for the y origin
bool m_use_tms;

/// Size of the tiles in [pix], usually this is 256
int m_tile_size;

Expand All @@ -84,16 +120,6 @@ class MapTiler
/// Lookup table to map zoom levels to an absolute number of tiles
std::map<int, long> m_lookup_nrof_tiles_from_zoom;

/*!
* @brief Computes the slippy tile index for a given zoom level that contains the requested coordinate in WGS84. The
* specifications are documented: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Pseudo-code
* @param lat Latitude in WGS84
* @param lon Longitude in WGS84
* @param zoom_level Zoom level 0 - 35
* @return Tile coordinate according to slippy tile standard
*/
cv::Point2i computeTileFromLatLon(double lat, double lon, int zoom_level) const;

/*!
* @brief Each tile is indexed with (tx, ty). Together with the corresponding tile size this coordinate can be
* transformed into a global pixel coordinate with tx * tile size, ty * tile size. If the resolution is known in
Expand Down Expand Up @@ -173,15 +199,6 @@ class MapTiler
*/
cv::Rect2d computeTileBoundsMeters(const cv::Rect2i &idx_roi, int zoom_level);

/*!
* @brief Computes one lat-lon coordinate for a given tile. It represents the upper left corner of the tile.
* @param x Coordinate of tile in x-direction
* @param y Coordinate of tile in y-direction
* @param zoom_level Zoon level of the map
* @return (longitude, latitude) of upper left tile corner
*/
WGSPose computeLatLonForTile(int x, int y, int zoom_level) const;

/*!
* @brief Maximal scale down zoom of the pyramid closest to the pixelSize.
* @param GSD Ground sampling distance in m/pix
Expand Down
24 changes: 17 additions & 7 deletions modules/realm_ortho/include/realm_ortho/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace realm

/*!
* @brief Tile is a container class that is defined by a coordinate (x,y) in a specific zoom level following the
* Tiled Map Service specification and a multi-layered grid map holding the data.
* Tiled Map Service or Google/OSM specification and a multi-layered grid map holding the data.
*/
class Tile
{
Expand All @@ -24,12 +24,13 @@ class Tile
public:
/*!
* @brief Non-default constructor
* @param zoom_level Zoom level or z-coordinate according to TMS standard
* @param tx Tile index in x-direction according to TMS standard
* @param ty Tile index in y-direction according to TMS standard
* @param zoom_level Zoom level or z-coordinate according to TMS or Google/OSM specification
* @param tx Tile index in x-direction according to TMS or Google/OSM specification
* @param ty Tile index in y-direction according to TMS or Google/OSM specification
* @param map Multi-layered grid map holding the data for the tile
* @param is_tms Indicates this is a TMS rather than google tile
*/
Tile(int zoom_level, int tx, int ty, const CvGridMap &map);
Tile(int zoom_level, int tx, int ty, const CvGridMap &map, bool is_tms);

/*!
* @brief Locks the tile when being accessed or modified to prevent multi-threading problems.
Expand All @@ -41,6 +42,12 @@ class Tile
*/
void unlock();

/*!
* @brief Indicates if this is a TMS or Google/OSM tile
* @return True if tms
*/
bool is_tms() const;

/*!
* @brief Getter for the zoom level
* @return Zoom level of the data
Expand Down Expand Up @@ -69,15 +76,18 @@ class Tile

private:

/// Zoom level according to TMS standard
/// Zoom level according to TMS or Google/OSM specification
int m_zoom_level;

/// Tile index according to TMS standard
/// Tile index according to TMS or Google/OSM specification
cv::Point2i m_index;

/// Multi-layered grid map container
CvGridMap::Ptr m_data;

/// Indicates this is a tms tiles
bool m_tms;

/// Main mutex to prevent simultaneous access from different threads
std::mutex m_mutex_data;
};
Expand Down
98 changes: 0 additions & 98 deletions modules/realm_ortho/include/realm_ortho/tile_cache.h

This file was deleted.

Loading