Skip to content

Commit

Permalink
misc and use file based cache
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Mar 16, 2024
1 parent 0833248 commit 16e0763
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 43 deletions.
39 changes: 21 additions & 18 deletions examples/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
14 changes: 8 additions & 6 deletions examples/libs/geo/geometries/MapNodeGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ namespace threepp {
class MapNodeGeometry: public BufferGeometry {

public:
static std::shared_ptr<MapNodeGeometry> create(int width = 1, int height = 1, int widthSegments = 1, int heightSegments = 1, bool skirt = true, int skirtDepth = 10) {
static std::shared_ptr<MapNodeGeometry> create(
int width = 1, int height = 1, int widthSegments = 1, int heightSegments = 1,
bool skirt = true, int skirtDepth = 10) {

return std::shared_ptr<MapNodeGeometry>(new MapNodeGeometry(width, height, widthSegments, heightSegments, skirt, skirtDepth));
}
Expand Down Expand Up @@ -111,13 +113,13 @@ namespace threepp {
int gridZ = heightSegments + 1;

// Width of each segment X
float segmentWidth = width / widthSegments;
float segmentWidth = width / static_cast<float>(widthSegments);

// Height of each segment Z
float segmentHeight = height / heightSegments;
float segmentHeight = height / static_cast<float>(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;
Expand Down Expand Up @@ -195,7 +197,7 @@ namespace threepp {
normals.push_back(0);

uvs.push_back(0);
uvs.push_back(1 - static_cast<float>(iz) / heightSegments);
uvs.push_back(1 - static_cast<float>(iz) / static_cast<float>(heightSegments));
}

for (int iz = 0; iz < heightSegments; iz++) {
Expand Down Expand Up @@ -226,7 +228,7 @@ namespace threepp {
normals.push_back(0);

uvs.push_back(1);
uvs.push_back(1 - static_cast<float>(iz) / heightSegments);
uvs.push_back(1 - static_cast<float>(iz) / static_cast<float>(heightSegments));
}

for (int iz = 0; iz < heightSegments; iz++) {
Expand Down
2 changes: 1 addition & 1 deletion examples/libs/geo/nodes/MapNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BufferGeometry>& geometry, const std::shared_ptr<Material>& material)
: Mesh(geometry, material), parentNode(parentNode),
mapView(mapView), location(location), level(level), x(x), y(y) {}
Expand Down
6 changes: 3 additions & 3 deletions examples/libs/geo/nodes/MapNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BufferGeometry>& geometry = nullptr,
const std::shared_ptr<Material>& material = nullptr);

Expand Down Expand Up @@ -58,8 +58,8 @@ namespace threepp {
int location;
int level;

float x;
float y;
int x;
int y;

bool subdivided = false;
bool disposed = false;
Expand Down
2 changes: 1 addition & 1 deletion examples/libs/geo/nodes/MapPlaneNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions examples/libs/geo/providers/MapProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
19 changes: 11 additions & 8 deletions examples/libs/geo/providers/OpenStreetMapsProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

#include "threepp/loaders/ImageLoader.hpp"

#include <iostream>
#include <mutex>
#include <fstream>
#include <sstream>
#include <utility>

Expand All @@ -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<unsigned char> 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<unsigned char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
} 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<const char*>(data.data()), data.size());
}

return *loader.load(data, format == "png" ? 4 : 3, true);
Expand Down
4 changes: 2 additions & 2 deletions examples/projects/MapView/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 ()
endif ()
2 changes: 1 addition & 1 deletion examples/projects/MapView/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main() {
OrbitControls controls{camera, canvas};

auto lodFunc = std::make_unique<LODRadial>();
lodFunc->subdivideDistance = 100;
lodFunc->subdivideDistance = 70;
auto provider = std::make_unique<OpenStreetMapProvider>();

MapView map(std::move(provider), std::move(lodFunc));
Expand Down

0 comments on commit 16e0763

Please sign in to comment.