diff --git a/cmake-build-release/.ninja_deps b/cmake-build-release/.ninja_deps index 6c02fe1..1bf6c5f 100644 Binary files a/cmake-build-release/.ninja_deps and b/cmake-build-release/.ninja_deps differ diff --git a/cmake-build-release/.ninja_log b/cmake-build-release/.ninja_log index e08b7ac..5044a47 100644 --- a/cmake-build-release/.ninja_log +++ b/cmake-build-release/.ninja_log @@ -210,3 +210,5 @@ 2237 2427 1705433088022195202 libraries/BLT-With-Graphics-Template/libraries/BLT/libBLT.a 5e3491f3bb42050d 9 3115 1705433088714186351 CMakeFiles/gp_image_test.dir/src/main.cpp.o d2247c8eaef04c80 3116 3265 1705433088862184459 gp_image_test 627b40e9bcf815e3 +22 1261 1705448034056787170 CMakeFiles/gp_image_test.dir/src/main.cpp.o d2247c8eaef04c80 +1261 1331 1705448034128783930 gp_image_test 627b40e9bcf815e3 diff --git a/cmake-build-release/CMakeFiles/gp_image_test.dir/src/main.cpp.o b/cmake-build-release/CMakeFiles/gp_image_test.dir/src/main.cpp.o index 60cdfbe..7650a50 100644 Binary files a/cmake-build-release/CMakeFiles/gp_image_test.dir/src/main.cpp.o and b/cmake-build-release/CMakeFiles/gp_image_test.dir/src/main.cpp.o differ diff --git a/cmake-build-release/Testing/Temporary/LastTest.log b/cmake-build-release/Testing/Temporary/LastTest.log index 2b1c83a..55f63ee 100644 --- a/cmake-build-release/Testing/Temporary/LastTest.log +++ b/cmake-build-release/Testing/Temporary/LastTest.log @@ -1,3 +1,3 @@ -Start testing: Jan 16 14:24 EST +Start testing: Jan 16 18:33 EST ---------------------------------------------------------- -End testing: Jan 16 14:24 EST +End testing: Jan 16 18:33 EST diff --git a/cmake-build-release/gp_image_test b/cmake-build-release/gp_image_test index 65570b7..935c56d 100755 Binary files a/cmake-build-release/gp_image_test and b/cmake-build-release/gp_image_test differ diff --git a/include/image.h b/include/image.h index 9d36447..282bc22 100644 --- a/include/image.h +++ b/include/image.h @@ -31,16 +31,16 @@ inline constexpr i32 width = 1024, height = 1024; class image { private: - std::array data; + std::array data; public: image() = default; - u8 get(i32 x, i32 y) + const blt::vec4& get(i32 x, i32 y) { return data[y * height + x]; } - void set(u8 c, i32 x, i32 y) + void set(const blt::vec4& c, i32 x, i32 y) { data[y * height + x] = c; } @@ -92,23 +92,23 @@ inline i32 inputs(function_t op) struct op_con { - function_t op; - std::optional i1, i2; + const function_t op; + const std::optional i1, i2; op_con(function_t op, const std::optional& i1, const std::optional& i2): op(op), i1(i1), i2(i2) {} - [[nodiscard]] bool has_both() const + [[nodiscard]] bool has_both() const noexcept { return i1.has_value() && i2.has_value(); } - [[nodiscard]] bool has_one() const + [[nodiscard]] bool has_one() const noexcept { return i1.has_value() || i2.has_value(); } - [[nodiscard]] image* getOne() + [[nodiscard]] image* getOne() const { if (i1) return i1.value(); diff --git a/src/main.cpp b/src/main.cpp index 97491e8..fafba70 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,8 @@ struct node; blt::area_allocator node_allocator; blt::area_allocator img_allocator; +using data_t = std::array; + node* createNode() { auto* n = node_allocator.allocate(1); @@ -47,90 +49,6 @@ void destroyImage(image* img) img_allocator.deallocate(img, 1); } -image* apply_operator(op_con operation) -{ - auto* ret = createImage(); - for (i32 y = 0; y < height; y++) - { - for (i32 x = 0; x < width; x++) - { - switch (operation.op) - { - case function_t::ADD: - { - BLT_ASSERT(operation.has_both()); - ret->set((operation.i1.value()->get(x, y) + operation.i2.value()->get(x, y)) % std::numeric_limits::max(), x, y); - break; - } - case function_t::SUB: - { - BLT_ASSERT(operation.has_both()); - ret->set((operation.i1.value()->get(x, y) - operation.i2.value()->get(x, y)) % std::numeric_limits::max(), x, y); - break; - } - case function_t::MUL: - { - BLT_ASSERT(operation.has_both()); - ret->set((operation.i1.value()->get(x, y) * operation.i2.value()->get(x, y)) % std::numeric_limits::max(), x, y); - break; - } - case function_t::DIV: - { - BLT_ASSERT(operation.has_both()); - auto den = operation.i2.value()->get(x, y); - if (den == 0) - ret->set(0, x, y); - else - ret->set((operation.i1.value()->get(x, y) / den) % std::numeric_limits::max(), x, y); - break; - } - case function_t::EXP: - { - BLT_ASSERT(operation.has_both()); - ret->set(static_cast(std::pow(operation.i1.value()->get(x, y), operation.i2.value()->get(x, y))), x, y); - break; - } - case function_t::LOG: - { - BLT_ASSERT(operation.has_one()); - ret->set(static_cast(std::log(static_cast(operation.getOne()->get(x, y)))), x, y); - break; - } - case function_t::SQRT: - { - BLT_ASSERT(operation.has_one()); - ret->set(static_cast(std::sqrt(static_cast(operation.getOne()->get(x, y)))), x, y); - break; - } - case function_t::QUAD: - { - BLT_ASSERT(operation.has_one()); - auto v = operation.getOne()->get(x, y); - ret->set(static_cast(v * v), x, y); - break; - } - case function_t::RANDOM: - { - - break; - } - case function_t::NOISE: - { - break; - } - case function_t::COLOR: - { - break; - } - case function_t::SCALAR: - { - break; - } - } - } - } -} - static constexpr int OPERATOR_COUNT = 13; class tree; @@ -142,7 +60,28 @@ struct node node* left; node* right; function_t type; - std::array data; + data_t data; + image* img = nullptr; + + [[nodiscard]] bool has_both() const + { + return left->img != nullptr && right->img != nullptr; + } + + [[nodiscard]] bool has_one() const + { + return left->img != nullptr || right->img != nullptr; + } + + [[nodiscard]] image* get_one() const + { + if (left->img != nullptr) + return left->img; + if (right->img != nullptr) + return right->img; + BLT_THROW(std::runtime_error("Unable to get when one is missing! You have an error in your GA!")); + } + public: static node* construct_random() @@ -161,6 +100,105 @@ struct node return n; } + void evaluate() + { + img = createImage(); + for (i32 y = 0; y < height; y++) + { + for (i32 x = 0; x < width; x++) + { + switch (type) + { + case function_t::ADD: + { + BLT_ASSERT(has_both()); + img->set((left->img->get(x, y) + right->img->get(x, y)) % std::numeric_limits::max(), x, y); + break; + } + case function_t::SUB: + { + BLT_ASSERT(has_both()); + img->set((left->img->get(x, y) - right->img->get(x, y)) % std::numeric_limits::max(), x, y); + break; + } + case function_t::MUL: + { + BLT_ASSERT(has_both()); + img->set((left->img->get(x, y) * right->img->get(x, y)) % std::numeric_limits::max(), x, y); + break; + } + case function_t::DIV: + { + BLT_ASSERT(has_both()); + auto den = right->img->get(x, y); + if (den == 0) + img->set(0, x, y); + else + img->set((left->img->get(x, y) / den) % std::numeric_limits::max(), x, y); + break; + } + case function_t::EXP: + { + BLT_ASSERT(has_both()); + img->set(static_cast(std::pow(left->img->get(x, y), right->img->get(x, y))), x, y); + break; + } + case function_t::LOG: + { + BLT_ASSERT(has_one()); + const auto& v = get_one()->get(x, y); + auto v_x = v.x() == 0 ? 0.000001f : v.x(); + auto v_y = v.y() == 0 ? 0.000001f : v.y(); + auto v_z = v.z() == 0 ? 0.000001f : v.z(); + img->set({std::log(v_x), std::log(v_y), std::log(v_z)}, x, y); + break; + } + case function_t::SQRT: + { + BLT_ASSERT(has_one()); + const auto& v = get_one()->get(x, y); + img->set({std::sqrt(v.x()), std::sqrt(v.y()), std::sqrt(v.z())}, x, y); + break; + } + case function_t::QUAD: + { + BLT_ASSERT(has_one()); + auto v = get_one()->get(x, y); + img->set(v * v, x, y); + break; + } + case function_t::RANDOM: + { + static std::random_device dev; + static std::mt19937_64 engine(dev()); + static std::uniform_real_distribution dist(0, 1); + img->set({dist(engine), dist(engine), dist(engine), 1.0f}, x, y); + break; + } + case function_t::NOISE: + { + auto v = std::numeric_limits::max() * + stb_perlin_noise3(static_cast(x) / ((256.0f * (data[0] + 1.0f) / 2.0f)), + static_cast(y) / ((256 * (data[1] + 1) / 2)), data[2] / 2.0f + 0.5f, 0, + 0, 0); + img->set(blt::vec4{v,v,v,1.0f}, x, y); + break; + } + case function_t::COLOR: + { + img->set(blt::vec4{data[0], data[1], data[2], 1.0f}, x, y); + break; + } + case function_t::SCALAR: + { + img->set(blt::vec4{data[0], data[0], data[0], 1.0f}, x, y); + break; + } + } + } + } + } + ~node() { destroyNode(left);