diff --git a/CMakeLists.txt b/CMakeLists.txt index bf9501c..a9065ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(image-gp-2 VERSION 0.0.11) +project(image-gp-2 VERSION 0.0.12) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) @@ -14,6 +14,13 @@ add_subdirectory(lib/FastNoise2) find_package(OpenCV REQUIRED) +include(CheckCXXCompilerFlag) +CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE) +if(COMPILER_SUPPORTS_MARCH_NATIVE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") +endif() + + include_directories( ${OpenCV_INCLUDE_DIRS} ) include_directories(lib/stb) diff --git a/include/gp_system.h b/include/gp_system.h index 0646a3d..3648e2c 100644 --- a/include/gp_system.h +++ b/include/gp_system.h @@ -41,6 +41,8 @@ void cleanup(); std::array& get_reference_image(); +std::array get_best_image_index(); + std::array to_gl_image(const std::array& image); std::tuple&, const std::vector&, const std::vector&, const std::vector&> get_fitness_history(); diff --git a/include/image_storage.h b/include/image_storage.h index 90163d1..5ca7933 100644 --- a/include/image_storage.h +++ b/include/image_storage.h @@ -27,6 +27,10 @@ #include #include +#ifndef BLT_IMAGE_SIZE +#define BLT_IMAGE_SIZE 256 +#endif + using image_pixel_t = float; using image_ipixel_t = blt::u32; constexpr blt::i32 IMAGE_DIMENSIONS = 256; diff --git a/src/gp_system.cpp b/src/gp_system.cpp index d77a314..11884a5 100644 --- a/src/gp_system.cpp +++ b/src/gp_system.cpp @@ -21,6 +21,7 @@ #include #include #include "opencv2/imgcodecs.hpp" +#include "opencv2/imgproc.hpp" #include using namespace blt::gp; @@ -236,6 +237,29 @@ void setup_operations(gp_program* program) static auto lit = operation_t([program]() { return program->get_random().get_float(-1.0f, 1.0f); }, "lit_float").set_ephemeral(); + static operation_t op_erode([](const image_t a, float erosion_size) { + image_t ret{}; + erosion_size = std::min(std::max(erosion_size, 0.0f), 21.0f); + const cv::Mat src{IMAGE_DIMENSIONS, IMAGE_DIMENSIONS, CV_32F, a.as_void_const()}; + cv::Mat dst{IMAGE_DIMENSIONS, IMAGE_DIMENSIONS, CV_32F, ret.get_data().data.data()}; + const cv::Mat element = cv::getStructuringElement( cv::MORPH_CROSS, + cv::Size( static_cast(2*erosion_size + 1), static_cast(2*erosion_size+1) ), + cv::Point( static_cast(erosion_size), static_cast(erosion_size) ) ); + cv::erode( src, dst, element ); + return ret; + }, "erode_image"); + + static operation_t op_dilate([](const image_t a, float dilate_size) { + image_t ret{}; + dilate_size = std::min(std::max(dilate_size, 0.0f), 21.0f); + const cv::Mat src{IMAGE_DIMENSIONS, IMAGE_DIMENSIONS, CV_32F, a.as_void_const()}; + cv::Mat dst{IMAGE_DIMENSIONS, IMAGE_DIMENSIONS, CV_32F, ret.get_data().data.data()}; + const cv::Mat element = cv::getStructuringElement( cv::MORPH_CROSS, + cv::Size( static_cast(2*dilate_size + 1), static_cast(2*dilate_size+1) ), + cv::Point( static_cast(dilate_size), static_cast(dilate_size) ) ); + cv::dilate( src, dst, element ); + return ret; + }, "erode_image"); operator_builder builder{}; builder.build(op_image_ephemeral, make_add(), make_sub(), make_mul(), make_div(), op_image_x, op_image_y, @@ -373,6 +397,14 @@ void reset_programs() program->reset_program(program->get_typesystem().get_type().id()); } +std::array get_best_image_index() +{ + std::array best_index{}; + for (const auto [slot, program] : blt::zip(best_index, programs)) + slot = program->get_best_indexes<1>()[0]; + return best_index; +} + void regenerate_image(blt::size_t index, float& image_storage, blt::i32 width, blt::i32 height) {} diff --git a/src/main.cpp b/src/main.cpp index ef24b47..32f0e7f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include blt::gfx::matrix_state_manager global_matrices; blt::gfx::resource_manager resources; @@ -97,6 +98,7 @@ void update(const blt::gfx::window_data& data) static blt::i32 image_to_enlarge = -1; bool clicked_on_image = false; + static bool show_best = false; // Create the tab bar if (ImGui::BeginTabBar("MainTabs")) @@ -108,6 +110,7 @@ void update(const blt::gfx::window_data& data) static int images_y = 6; static bool run_gp = false; static int generation_limit = 0; + static int min_between_runs = 100; if (ImGui::BeginTabItem("Run GP")) { @@ -121,8 +124,10 @@ void update(const blt::gfx::window_data& data) if (ImGui::InputInt("Images X", &images_x) || ImGui::InputInt("Images Y", &images_y)) update_population_size(images_x * images_y); ImGui::InputInt("Generation Limit", &generation_limit); - if (run_gp && (generation_limit == 0 || get_generation() < generation_limit)) + if (run_gp && (generation_limit == 0 || get_generation() < static_cast(generation_limit))) run_generation = true; + ImGui::InputInt("Min Time Between Runs (ms)", &min_between_runs); + ImGui::Checkbox("Show Best", &show_best); } ImGui::EndChild(); @@ -247,6 +252,19 @@ void update(const blt::gfx::window_data& data) if ((blt::gfx::isMousePressed(0) && blt::gfx::mousePressedLastFrame() && !clicked_on_image) || (blt::gfx::isKeyPressed(GLFW_KEY_ESCAPE) && blt::gfx::keyPressedLastFrame())) image_to_enlarge = -1; + if (show_best) + { + auto best_images = get_best_image_index(); + for (const auto [i, best_image] : blt::enumerate(best_images)) + { + const auto width = std::min(static_cast(data.width) - side_bar_width, static_cast(256) * 3) / 3; + const auto height = std::min(static_cast(data.height) - top_bar_height, static_cast(256) * 3) / 3; + renderer_2d.drawRectangle(blt::gfx::rectangle2d_t{blt::gfx::anchor_t::BOTTOM_LEFT, + side_bar_width + 256 + i * width, 64, width, height + }, std::to_string(best_image), 1); + } + } + if (image_to_enlarge != -1) { if (blt::gfx::isKeyPressed(GLFW_KEY_R) && blt::gfx::keyPressedLastFrame())