From 1cd528e7ef3f465c88531cbcab32451cc089f580 Mon Sep 17 00:00:00 2001 From: Madeline Busig Date: Wed, 7 May 2025 00:11:12 -0700 Subject: [PATCH] Add parsing ruleset --- include/fractal.hpp | 15 +++++-- src/fractal.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/include/fractal.hpp b/include/fractal.hpp index 3541799..8d4829b 100644 --- a/include/fractal.hpp +++ b/include/fractal.hpp @@ -251,12 +251,21 @@ class generator_t { etl::vector m_weighted_groups; etl::vector m_group_outputs; etl::vector m_token_matches; - group_id_t m_axiom; - uint32_t m_gen_num; + group_id_t m_axiom; + bool m_empty; + + uint32_t m_gen_num = 0; + + void parse_group_characteristic(group_id_t gid, group_output_t& output, const ruleset_t& ruleset); public: - generator_t(const ruleset_t& ruleset); + generator_t() : m_empty(true), m_axiom(0) { } + generator_t(const ruleset_t& ruleset) : m_empty(false), m_axiom(0) { + parse_ruleset(ruleset); + } + + void parse_ruleset(const ruleset_t& ruleset); /** * @brief Steps one generation, outputting generated markers diff --git a/src/fractal.cpp b/src/fractal.cpp index e9eb45b..b04d20d 100644 --- a/src/fractal.cpp +++ b/src/fractal.cpp @@ -110,6 +110,101 @@ branch_rule_id_t ruleset_t::add_branch_rule_weighted(token_id_t match, weighted_ return m_branch_rules.size() - 1; } +// ---------- GENERATOR ---------- + +void generator_t::parse_ruleset(const ruleset_t& ruleset) { + mlog::debug << "Parsing ruleset" << endl; + + m_axiom = ruleset.get_axiom(); + + for (weighted_group_id_t wgid = 0; wgid < ruleset.num_weighted_groups(); ++wgid) { + m_weighted_groups.push_back(ruleset.get_weighted_group(wgid)); + } + + mlog::debug << "Copied " << m_weighted_groups.size() << " weighted groups" << endl; + + for (token_id_t tok = 0; tok < ruleset.num_tokens(); ++tok) { + m_token_matches.push_back(token_match_t()); + token_match_t& cur_match = m_token_matches.back(); + + ruleset.find_token_matches(tok, cur_match.m_groups, cur_match.m_wgroups); + } + + for (group_id_t gid = 0; gid < ruleset.num_groups(); ++gid) { + m_group_outputs.push_back(group_output_t()); + + parse_group_characteristic(gid, m_group_outputs.back(), ruleset); + } + + + m_empty = false; +} + +void generator_t::parse_group_characteristic(group_id_t id, group_output_t& output, const ruleset_t& ruleset) { + mlog::debug << "Parsing group characteristic for group " << id << endl; + + vec2 cur_pos(0, 0); + mat<2, 2> cur_orient = create_rotation(fixed(1), fixed(0)); + + const group_characteristic_t& charac = ruleset.get_group_characteristic(id); + + for (uint32_t i = 0; i < charac.m_factor; ++i) { + for (token_id_t tokid : charac.m_token_ids) { + const token_t& tok = ruleset.get_token(tokid); + + // Process token marking + if (tok.m_mark != 0) { + marker_t marker { cur_pos, tok.m_mark }; + + output.m_child_markers.push_back(marker); + mlog::debug << "Group " << id << " has marker ID: " << tok.m_mark << endl; + mlog::debug << " at position " << cur_pos << ", cos(angle) = " << cur_orient.e[0][0] << ", sin(angle) = " << cur_orient.e[1][0] << endl; + } + + // Process token groups + const token_match_t& match = m_token_matches.at(tokid); + + // Regular groups + for (group_id_t groupid : match.m_groups) { + leaf_t leaf; + leaf.m_group_id = groupid; + leaf.m_orientation = cur_orient; + leaf.m_position = cur_pos; + + output.m_child_leafs.push_back(leaf); + + mlog::debug << "Group " << id << " has child leaf w/ group id " << groupid << endl; + mlog::debug << " at position " << cur_pos << ", cos(angle) = " << cur_orient.e[0][0] << ", sin(angle) = " << cur_orient.e[1][0] << endl; + } + + // Weighted groups + for (weighted_group_id_t wgroupid : match.m_wgroups) { + weighted_leaf_t wleaf; + wleaf.m_weighted_group_id = wgroupid; + wleaf.m_orientation = cur_orient; + wleaf.m_position = cur_pos; + + output.m_child_wleafs.push_back(wleaf); + + mlog::debug << "Group " << id << " has weighted child leaf w/ wgroup " << wgroupid << endl; + mlog::debug << " at position " << cur_pos << ", cos(angle) = " << cur_orient.e[0][0] << ", sin(angle) = " << cur_orient.e[1][0] << endl; + } + + // Apply the token's transformation + switch (tok.m_type) { + case token_type_e::walk: + cur_pos = cur_pos + cur_orient * vec2(0, tok.m_value); + mlog::debug << "Transforming with walk of " << tok.m_value << endl; + break; + case token_type_e::rotate: + cur_orient = create_rotation(tok.m_value, tok.m_value2) * cur_orient; + mlog::debug << "Transforming with rotation of " << tok.m_value << endl; + break; + default:; + } + } + } +} #if 0 token_id_t generator_t::add_token(token_type_e type, mtl::fixed value) {