Merge remote-tracking branch 'refs/remotes/origin/main'

main
Brett 2025-07-14 00:06:54 -04:00
commit 8685f4ad10
5 changed files with 65 additions and 2 deletions

View File

@ -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)

View File

@ -41,6 +41,8 @@ void cleanup();
std::array<image_storage_t, 3>& get_reference_image();
std::array<size_t, 3> get_best_image_index();
std::array<image_pixel_t, IMAGE_DIMENSIONS * IMAGE_DIMENSIONS * 3> to_gl_image(const std::array<image_storage_t, 3>& image);
std::tuple<const std::vector<float>&, const std::vector<float>&, const std::vector<float>&, const std::vector<float>&> get_fitness_history();

View File

@ -27,6 +27,10 @@
#include <mutex>
#include <blt/std/hashmap.h>
#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;

View File

@ -21,6 +21,7 @@
#include <operations.h>
#include <random>
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include <stb_perlin.h>
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<int>(2*erosion_size + 1), static_cast<int>(2*erosion_size+1) ),
cv::Point( static_cast<int>(erosion_size), static_cast<int>(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<int>(2*dilate_size + 1), static_cast<int>(2*dilate_size+1) ),
cv::Point( static_cast<int>(dilate_size), static_cast<int>(dilate_size) ) );
cv::dilate( src, dst, element );
return ret;
}, "erode_image");
operator_builder builder{};
builder.build(op_image_ephemeral, make_add<image_t>(), make_sub<image_t>(), make_mul<image_t>(), make_div<image_t>(), op_image_x, op_image_y,
@ -373,6 +397,14 @@ void reset_programs()
program->reset_program(program->get_typesystem().get_type<image_t>().id());
}
std::array<size_t, 3> get_best_image_index()
{
std::array<size_t, 3> 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)
{}

View File

@ -7,6 +7,7 @@
#include <imgui.h>
#include <thread>
#include <implot.h>
#include <blt/gp/tree.h>
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<blt::u32>(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<float>(data.width) - side_bar_width, static_cast<float>(256) * 3) / 3;
const auto height = std::min(static_cast<float>(data.height) - top_bar_height, static_cast<float>(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())