diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b0e622..dcd672f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,15 +8,15 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) string(APPEND resources_dir "${CMAKE_CURRENT_SOURCE_DIR}/data@data") if (EMSCRIPTEN) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Oz -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY --preload-file ${resources_dir} --shell-file=\"${CMAKE_CURRENT_SOURCE_DIR}/shell.html\"") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Oz -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY --preload-file ${resources_dir} --shell-file=\"${CMAKE_CURRENT_SOURCE_DIR}/shell.html\"") set(CMAKE_EXECUTABLE_SUFFIX ".html") endif () file(GLOB_RECURSE PROJECT_SRC src/*.cpp src/*.hpp src/*.h) find_package(raylib) -#find_package(spdlog) +find_package(glm) find_package(nlohmann_json) add_executable(${PROJECT_NAME} ${PROJECT_SRC}) -target_link_libraries(${PROJECT_NAME} raylib nlohmann_json::nlohmann_json) +target_link_libraries(${PROJECT_NAME} raylib nlohmann_json::nlohmann_json glm::glm) diff --git a/conanfile.txt b/conanfile.txt index 2496da6..0d9a6c2 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -4,7 +4,7 @@ raylib/5.5 #spdlog/1.15.0 nlohmann_json/3.11.3 -#glm/cci.20230113 +glm/cci.20230113 #glfw/3.4 #glew/2.2.0 #stb/cci.20240531 diff --git a/data/img2.jpg b/data/img2.jpg new file mode 100644 index 0000000..4dee48f Binary files /dev/null and b/data/img2.jpg differ diff --git a/data/img3.jpg b/data/img3.jpg new file mode 100644 index 0000000..c756eac Binary files /dev/null and b/data/img3.jpg differ diff --git a/data/img4.jpg b/data/img4.jpg new file mode 100644 index 0000000..a08a6bb Binary files /dev/null and b/data/img4.jpg differ diff --git a/src/main.cpp b/src/main.cpp index 9f92a98..f37a363 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,128 @@ +#include +#include +#include +#include +#include +#include #include +#include -int main (int argc, char *argv[]) +#define MAX_PTS_PER_NODE 1 + +struct kdtree +{ + kdtree(std::vector &pts) + { + calc_aabb(pts); + + if (pts.size() > MAX_PTS_PER_NODE) + { + int split_index = aabb_size.x > aabb_size.y ? 0 : 1; + + std::sort(pts.begin(), pts.end(), + [split_index](const glm::uvec2 &a, const glm::uvec2 &b) + { + return a[split_index] < b[split_index]; + }); + + { + std::vector lpts(pts.begin(), pts.begin() + pts.size() / 2); + left = new kdtree(lpts); + } + + { + std::vector rpts(pts.begin() + pts.size() / 2, pts.end()); + right = new kdtree(rpts); + } + + } + } + + void draw() + { + DrawRectangleLines(aabb_min.x, aabb_min.y, aabb_size.x, aabb_size.y, BLACK); + if(left && right) + { + left->draw(); + right->draw(); + } + } + + void calc_aabb(const std::vector &pts) + { + for (const auto &p : pts) + { + aabb_min = glm::min(aabb_min, p); + aabb_max = glm::max(aabb_max, p); + } + + aabb_size = aabb_max - aabb_min; + } + + kdtree *left = nullptr; + kdtree *right = nullptr; + glm::uvec2 aabb_min{999999}; + glm::uvec2 aabb_max{000000}; + glm::uvec2 aabb_size; +}; + +int main (int argc, char *argv[]) { auto img = LoadImage("data/img.jpg"); - InitWindow(img.width, img.height, ""); + InitWindow(img.width, img.height, ""); SetTargetFPS(60); - while (!WindowShouldClose()) + std::vector pts; + + for (int i = 0; i < img.width * img.height; ++i) + { + int x = i % img.width; + int y = i / img.width; + + auto val = ((glm::u8vec3 *)img.data + i)->r < 10; + + bool found = false; + for(int dy = -1; dy <= 1; ++dy) + { + for(int dx = -1; dx <= 1; ++dx) + { + auto x2 = x + dx; + auto y2 = y + dy; + auto index = y2 * img.width + x2; + + if(index >= 0 && index < img.width * img.height) + { + auto v2 = ((glm::u8vec3 *)img.data + index)->r < 10; + if(val != v2) + { + found = true; + break; + } + } + } + + if(found) + { + break; + } + } + if(found) + { + pts.emplace_back(x, y); + } + } + + kdtree tree(pts); + + while (!WindowShouldClose()) { BeginDrawing(); ClearBackground(WHITE); + tree.draw(); EndDrawing(); } - + CloseWindow(); return 0;