From 16e0763d88e39e1fe1f1506f5b9cfd219daf7e25 Mon Sep 17 00:00:00 2001 From: Lars Ivar Hatledal Date: Sat, 16 Mar 2024 10:45:04 +0100 Subject: [PATCH] misc and use file based cache --- examples/libs/CMakeLists.txt | 39 ++++++++++--------- .../libs/geo/geometries/MapNodeGeometry.hpp | 14 ++++--- examples/libs/geo/nodes/MapNode.cpp | 2 +- examples/libs/geo/nodes/MapNode.hpp | 6 +-- examples/libs/geo/nodes/MapPlaneNode.hpp | 2 +- examples/libs/geo/providers/MapProvider.hpp | 6 +-- .../geo/providers/OpenStreetMapsProvider.hpp | 19 +++++---- examples/projects/MapView/CMakeLists.txt | 4 +- examples/projects/MapView/main.cpp | 2 +- 9 files changed, 51 insertions(+), 43 deletions(-) diff --git a/examples/libs/CMakeLists.txt b/examples/libs/CMakeLists.txt index d567945c..4351fa65 100644 --- a/examples/libs/CMakeLists.txt +++ b/examples/libs/CMakeLists.txt @@ -29,27 +29,30 @@ add_library(pathfinding INTERFACE ) target_include_directories(pathfinding INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}") -add_library(geothreepp - "${CMAKE_CURRENT_SOURCE_DIR}/geo/lod/LODControl.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/lod/LODFrustum.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/lod/LODRadial.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/lod/LODRaycast.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/lod/LODRaycast.cpp" +if (CURL_FOUND) + add_library(geothreepp + "${CMAKE_CURRENT_SOURCE_DIR}/geo/lod/LODControl.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/lod/LODFrustum.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/lod/LODRadial.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/lod/LODRaycast.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/lod/LODRaycast.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/nodes/MapNode.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/nodes/MapNode.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/nodes/MapPlaneNode.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/nodes/MapNode.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/nodes/MapNode.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/nodes/MapPlaneNode.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/geometries/MapNodeGeometry.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/geometries/MapNodeGeometry.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/providers/MapProvider.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/providers/OpenStreetMapsProvider.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/providers/MapProvider.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/providers/OpenStreetMapsProvider.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/utils/UnitUtils.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/utils/UnitUtils.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/MapView.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/geo/MapView.cpp" -) + "${CMAKE_CURRENT_SOURCE_DIR}/geo/MapView.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/geo/MapView.cpp" + ) + + target_include_directories(geothreepp PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") + target_link_libraries(geothreepp PUBLIC threepp::threepp CURL::libcurl) -target_include_directories(geothreepp PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") -target_link_libraries(geothreepp PUBLIC threepp::threepp CURL::libcurl) +endif () diff --git a/examples/libs/geo/geometries/MapNodeGeometry.hpp b/examples/libs/geo/geometries/MapNodeGeometry.hpp index 10b147f5..cd0c1f2a 100644 --- a/examples/libs/geo/geometries/MapNodeGeometry.hpp +++ b/examples/libs/geo/geometries/MapNodeGeometry.hpp @@ -10,7 +10,9 @@ namespace threepp { class MapNodeGeometry: public BufferGeometry { public: - static std::shared_ptr create(int width = 1, int height = 1, int widthSegments = 1, int heightSegments = 1, bool skirt = true, int skirtDepth = 10) { + static std::shared_ptr create( + int width = 1, int height = 1, int widthSegments = 1, int heightSegments = 1, + bool skirt = true, int skirtDepth = 10) { return std::shared_ptr(new MapNodeGeometry(width, height, widthSegments, heightSegments, skirt, skirtDepth)); } @@ -111,13 +113,13 @@ namespace threepp { int gridZ = heightSegments + 1; // Width of each segment X - float segmentWidth = width / widthSegments; + float segmentWidth = width / static_cast(widthSegments); // Height of each segment Z - float segmentHeight = height / heightSegments; + float segmentHeight = height / static_cast(heightSegments); // Down X - int start = vertices.size() / 3; + auto start = vertices.size() / 3; for (int ix = 0; ix < gridX; ix++) { float x = ix * segmentWidth - widthHalf; float z = -heightHalf; @@ -195,7 +197,7 @@ namespace threepp { normals.push_back(0); uvs.push_back(0); - uvs.push_back(1 - static_cast(iz) / heightSegments); + uvs.push_back(1 - static_cast(iz) / static_cast(heightSegments)); } for (int iz = 0; iz < heightSegments; iz++) { @@ -226,7 +228,7 @@ namespace threepp { normals.push_back(0); uvs.push_back(1); - uvs.push_back(1 - static_cast(iz) / heightSegments); + uvs.push_back(1 - static_cast(iz) / static_cast(heightSegments)); } for (int iz = 0; iz < heightSegments; iz++) { diff --git a/examples/libs/geo/nodes/MapNode.cpp b/examples/libs/geo/nodes/MapNode.cpp index 47a8cbab..58ec0f0b 100644 --- a/examples/libs/geo/nodes/MapNode.cpp +++ b/examples/libs/geo/nodes/MapNode.cpp @@ -8,7 +8,7 @@ using namespace threepp; MapNode::MapNode( - MapNode* parentNode, MapView* mapView, int location, int level, float x, float y, + MapNode* parentNode, MapView* mapView, int location, int level, int x, int y, const std::shared_ptr& geometry, const std::shared_ptr& material) : Mesh(geometry, material), parentNode(parentNode), mapView(mapView), location(location), level(level), x(x), y(y) {} diff --git a/examples/libs/geo/nodes/MapNode.hpp b/examples/libs/geo/nodes/MapNode.hpp index 3a0ac331..aadcdc93 100644 --- a/examples/libs/geo/nodes/MapNode.hpp +++ b/examples/libs/geo/nodes/MapNode.hpp @@ -25,7 +25,7 @@ namespace threepp { MapNode(MapNode* parentNode, MapView* mapView, int location = QuadTreePosition::root, - int level = 0, float x = 0, float y = 0, + int level = 0, int x = 0, int y = 0, const std::shared_ptr& geometry = nullptr, const std::shared_ptr& material = nullptr); @@ -58,8 +58,8 @@ namespace threepp { int location; int level; - float x; - float y; + int x; + int y; bool subdivided = false; bool disposed = false; diff --git a/examples/libs/geo/nodes/MapPlaneNode.hpp b/examples/libs/geo/nodes/MapPlaneNode.hpp index ed3ea493..68fedc5e 100644 --- a/examples/libs/geo/nodes/MapPlaneNode.hpp +++ b/examples/libs/geo/nodes/MapPlaneNode.hpp @@ -20,7 +20,7 @@ namespace threepp { class MapPlaneNode: public MapNode { public: - MapPlaneNode(MapNode* parent, MapView* mapView, int location = QuadTreePosition::root, int level = 0, float x = 0, float y = 0) + MapPlaneNode(MapNode* parent, MapView* mapView, int location = QuadTreePosition::root, int level = 0, int x = 0, int y = 0) : MapNode(parent, mapView, location, level, x, y, baseGeom, MeshBasicMaterial::create({{"wireframe", false}})) { this->matrixAutoUpdate = false; diff --git a/examples/libs/geo/providers/MapProvider.hpp b/examples/libs/geo/providers/MapProvider.hpp index 56341ec4..f86cc9d3 100644 --- a/examples/libs/geo/providers/MapProvider.hpp +++ b/examples/libs/geo/providers/MapProvider.hpp @@ -13,10 +13,10 @@ namespace threepp { class MapProvider { public: - float minZoom = 0; - float maxZoom = 20; + int minZoom = 0; + int maxZoom = 20; - virtual Image fetchTile(float zoom, float x, float y) = 0; + virtual Image fetchTile(int zoom, int x, int y) = 0; virtual ~MapProvider() = default; }; diff --git a/examples/libs/geo/providers/OpenStreetMapsProvider.hpp b/examples/libs/geo/providers/OpenStreetMapsProvider.hpp index 9450b540..8762d7e1 100644 --- a/examples/libs/geo/providers/OpenStreetMapsProvider.hpp +++ b/examples/libs/geo/providers/OpenStreetMapsProvider.hpp @@ -8,8 +8,7 @@ #include "threepp/loaders/ImageLoader.hpp" -#include -#include +#include #include #include @@ -24,20 +23,24 @@ namespace threepp { this->maxZoom = 19; } - Image fetchTile(float zoom, float x, float y) override { + Image fetchTile(int zoom, int x, int y) override { std::stringstream ss; ss << address << zoom << '/' << x << '/' << y << '.' << format; const auto url = ss.str(); std::vector data; + std::string cacheFilePath = ".cache/openstreetmaps/" + std::to_string(zoom) + "_" + std::to_string(x) + "_" + std::to_string(y) + "." + format; - if (cache_.count(url)) { - data = cache_.at(url); - + if (std::filesystem::exists(cacheFilePath)) { + // Load from cache file + std::ifstream file(cacheFilePath, std::ios::binary); + data = std::vector((std::istreambuf_iterator(file)), std::istreambuf_iterator()); } else if (urlFetcher.fetch(url, data)) { - - cache_[url] = data; + // Save to cache file + std::filesystem::create_directories(".cache/openstreetmaps/"); + std::ofstream file(cacheFilePath, std::ios::binary); + file.write(reinterpret_cast(data.data()), data.size()); } return *loader.load(data, format == "png" ? 4 : 3, true); diff --git a/examples/projects/MapView/CMakeLists.txt b/examples/projects/MapView/CMakeLists.txt index 3dc962d6..a8963ad9 100644 --- a/examples/projects/MapView/CMakeLists.txt +++ b/examples/projects/MapView/CMakeLists.txt @@ -1,9 +1,9 @@ -if (CURL_FOUND) +if (TARGET geothreepp) add_example(NAME "MapView" SOURCES "main.cpp") if (TARGET "MapView") target_link_libraries("MapView" PRIVATE geothreepp) endif () -endif () \ No newline at end of file +endif () diff --git a/examples/projects/MapView/main.cpp b/examples/projects/MapView/main.cpp index 3464b123..65b1742b 100644 --- a/examples/projects/MapView/main.cpp +++ b/examples/projects/MapView/main.cpp @@ -25,7 +25,7 @@ int main() { OrbitControls controls{camera, canvas}; auto lodFunc = std::make_unique(); - lodFunc->subdivideDistance = 100; + lodFunc->subdivideDistance = 70; auto provider = std::make_unique(); MapView map(std::move(provider), std::move(lodFunc));