From 57db942f9e936f94e66374d980f40a3bb1dd1d93 Mon Sep 17 00:00:00 2001 From: Madeline Busig Date: Wed, 7 May 2025 02:06:15 -0700 Subject: [PATCH] Implement generation --- include/fractal.hpp | 2 +- src/fractal.cpp | 92 +++++++++++++++++++++++++++++++++++++++++---- src/main.cpp | 14 ++++--- 3 files changed, 95 insertions(+), 13 deletions(-) diff --git a/include/fractal.hpp b/include/fractal.hpp index 097d13b..69e8f4b 100644 --- a/include/fractal.hpp +++ b/include/fractal.hpp @@ -280,9 +280,9 @@ class generator_t { void clear_previous_buffers(); void clear_all_buffers(); + bool generate_group(group_id_t gid, mtl::vec2 pos, mtl::mat<2, 2> orient, etl::ivector& out_markers); bool generate_axiom(etl::ivector& out_markers); bool generate_leafs(etl::ivector& out_markers); - bool generate_wleafs(etl::ivector& out_markers); public: generator_t() : m_axiom(0) { } diff --git a/src/fractal.cpp b/src/fractal.cpp index b883687..1742f84 100644 --- a/src/fractal.cpp +++ b/src/fractal.cpp @@ -226,17 +226,89 @@ void generator_t::clear_all_buffers() { } bool generator_t::generate_axiom(etl::ivector& out_markers) { - return false; + const group_output_t& axiom_output = m_group_outputs[m_axiom]; + + if (m_leafs_cur->capacity() < axiom_output.m_child_leafs.size() || + m_wleafs_cur->capacity() < axiom_output.m_child_wleafs.size() || + out_markers.capacity() < axiom_output.m_child_markers.size()) { + mlog::debug << "Reached limit" << endl; + return false; + } + + for (const leaf_t& leaf : axiom_output.m_child_leafs) { + m_leafs_cur->push_back(leaf); + } + for (const weighted_leaf_t& wleaf : axiom_output.m_child_wleafs) { + m_wleafs_cur->push_back(wleaf); + } + for (const marker_t& mark : axiom_output.m_child_markers) { + out_markers.push_back(mark); + } + + return true; } + +bool generator_t::generate_group(group_id_t gid, vec2 pos, mat<2, 2> orient, etl::ivector& out_markers) { + const group_output_t& group_output = m_group_outputs[gid]; + + if (m_leafs_cur->capacity() < group_output.m_child_leafs.size() || + m_wleafs_cur->capacity() < group_output.m_child_wleafs.size() || + out_markers.capacity() < group_output.m_child_markers.size()) { + mlog::debug << "Reached limit" << endl; + return false; + } + + fixed cur_scale = 1; + for (uint32_t i = 1; i < m_gen_num; ++i) { // start @ 1, don't scale axiom + cur_scale *= m_scale; + } + + for (const leaf_t& leaf : group_output.m_child_leafs) { + leaf_t child; + + child.m_group_id = leaf.m_group_id; + child.m_position = pos + orient * cur_scale * leaf.m_position; + child.m_orientation = leaf.m_orientation * orient; + + m_leafs_cur->push_back(child); + } + for (const weighted_leaf_t& wleaf : group_output.m_child_wleafs) { + weighted_leaf_t child; + + child.m_weighted_group_id = wleaf.m_weighted_group_id; + child.m_position = pos + orient * cur_scale * m_gen_num * wleaf.m_position; + child.m_orientation = wleaf.m_orientation * orient; + + m_wleafs_cur->push_back(child); + } + for (const marker_t& mark : group_output.m_child_markers) { + marker_t child; + + child.m_id = mark.m_id; + child.m_pos = orient * cur_scale * mark.m_pos; + + out_markers.push_back(child); + } + + return true; +} + bool generator_t::generate_leafs(etl::ivector& out_markers) { - return false; -} -bool generator_t::generate_wleafs(etl::ivector& out_markers) { - return false; + bool good = true; + + for (const leaf_t& leaf : *m_leafs_prev) { + good = good && generate_group(leaf.m_group_id, leaf.m_position, leaf.m_orientation, out_markers); + + if (!good) { + mlog::debug << "Failed to generate leafs" << endl; + return false; + } + } + + return true; } bool generator_t::step_generation(etl::ivector& out_markers) { - swap_buffers(); clear_current_buffers(); bool good = true; @@ -244,7 +316,13 @@ bool generator_t::step_generation(etl::ivector& out_markers) { if (m_leafs_prev->empty() && m_leafs_cur->empty()) { good = generate_axiom(out_markers); } else { - good = generate_leafs(out_markers) && generate_wleafs(out_markers); + good = generate_leafs(out_markers); + } + + swap_buffers(); + + if (good) { + ++m_gen_num; } return good; diff --git a/src/main.cpp b/src/main.cpp index bdbaf25..d2daaf5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,15 +73,13 @@ int main(void) { size_t gen_num = 0; try { + constexpr fixed scale = 0.5; gen.parse_ruleset(rules); + gen.set_scale(scale); - while (gen.step_generation(output)) { + while (gen.step_generation(output) && gen.generation_num() < 4) { mlog::debug << "Stepped generation" << gen.generation_num() << endl; gen_num = gen.generation_num(); - - for (const marker_t& m : output) { - mlog::debug << ' ' << m.m_id << ", " << m.m_pos << endl; - } } } catch (etl::exception& e) { mlog::error << "Caught ETL exception: " << e.what() << endl; @@ -92,6 +90,12 @@ int main(void) { mlog::info << "Stepped " << gen.generation_num() << " generations" << mtl::endl; mlog::debug << "End" << mtl::flush; + mlog::info << "Output " << output.size() << " markers: " << endl; + + for (const marker_t& marker : output) { + mlog::info << " " << marker.m_pos << endl; + } + while (true) { vid_vsync(); }