#pragma once #include #include #include #include #include #include "combo_iterator.hpp" /** * An primitive with N indices. * @tparam N */ template struct Primitive { static_assert(N > 0, "Primitives must contain at least one point. Primitive<0> or lower is impossible."); std::array inds; Primitive() = default; Primitive(const Primitive &) = default; Primitive(const Primitive &sub, unsigned root) { std::copy(sub.inds.begin(), sub.inds.end(), inds.begin()); inds[N - 1] = root; } ~Primitive() = default; inline void flip() { if (N > 1) std::swap(inds[0], inds[1]); } void apply(const tc::Cosets &table, int gen) { for (auto &ind : inds) { ind = table.get(ind, gen); } flip(); } }; /** * Produce a list of all generators for the group context. The range [0..group.ngens). */ std::vector gens(const tc::Group &context) { std::vector g_gens(context.ngens); std::iota(g_gens.begin(), g_gens.end(), 0); return g_gens; } /** * Determine which of g_gens are the correct names for sg_gens within the current context */ std::vector recontext_gens( const tc::Group &context, std::vector g_gens, std::vector sg_gens) { std::sort(g_gens.begin(), g_gens.end()); int inv_gen_map[context.ngens]; for (size_t i = 0; i < g_gens.size(); i++) { inv_gen_map[g_gens[i]] = i; } std::vector s_sg_gens; s_sg_gens.reserve(sg_gens.size()); for (const auto gen : sg_gens) { s_sg_gens.push_back(inv_gen_map[gen]); } std::sort(s_sg_gens.begin(), s_sg_gens.end()); return s_sg_gens; } /** * Determine whether the orientation of the group sg_gens is reversed from the group g_gens within group context */ int get_parity( const tc::Group &context, const std::vector &g_gens, const std::vector &sg_gens ) { if (g_gens.size() != sg_gens.size() + 1) return 0; const auto proper_sg_gens = recontext_gens(context, g_gens, sg_gens); int i = 0; for (; i < sg_gens.size(); ++i) { if (proper_sg_gens[i] != i) { break; } } return i & 1; } /** * Solve the cosets generated by sg_gens within the subgroup generated by g_gens of the group context */ tc::Cosets solve( const tc::Group &context, const std::vector &g_gens, const std::vector &sg_gens ) { const auto proper_sg_gens = recontext_gens(context, g_gens, sg_gens); return context.subgroup(g_gens).solve(proper_sg_gens); } template struct Mesh { std::vector> prims; Mesh() : prims() {} Mesh(const Mesh &) = default; explicit Mesh(std::vector> &prims) : prims(prims) {} [[nodiscard]] size_t size() const { return prims.size(); } /** * Apply some context transformation to all primitives of this mesh. */ void apply(const tc::Cosets &table, int gen) { for (auto &prim : prims) { prim.apply(table, gen); } } /** * Reverse the orientation of all primitives in this mesh. */ void flip() { for (auto &prim : prims) { prim.flip(); } } /** * Convert the indexes of this mesh to those of a different context, using g_gens to build the parent context and sg_gens to build this context. */ [[nodiscard]] Mesh recontext( const tc::Group &context, const std::vector &g_gens, const std::vector &sg_gens ) const { const auto proper_sg_gens = recontext_gens(context, g_gens, sg_gens); const auto table = solve(context, g_gens, {}); const auto path = solve(context, sg_gens, {}).path; auto map = path.template walk(0, proper_sg_gens, [table](int coset, int gen) { return table.get(coset, gen); }); Mesh res = *this; for (Primitive &prim : res.prims) { for (auto &ind : prim.inds) { ind = map[ind]; } } if (get_parity(context, g_gens, sg_gens) == 1) res.flip(); return res; } [[nodiscard]] Mesh tile( const tc::Group &context, const std::vector &g_gens, const std::vector &sg_gens ) const { Mesh base = recontext(context, g_gens, sg_gens); const auto proper_sg_gens = recontext_gens(context, g_gens, sg_gens); const auto table = solve(context, g_gens, {}); const auto path = solve(context, g_gens, sg_gens).path; const auto all = path.template walk, int>(base, gens(context), [table](Mesh from, int gen) { from.apply(table, gen); return from; }); return merge(all); } /** * Produce a mesh of higher dimension by fanning a single point to all primitives in this mesh. */ [[nodiscard]] Mesh fan(int root) const { std::vector> res(prims.size()); std::transform(prims.begin(), prims.end(), res.begin(), [root](const Primitive &prim) { return Primitive(prim, root); } ); return Mesh(res); } }; /** * Union several meshes of the same dimension */ template Mesh merge(const std::vector> &meshes) { size_t size = 0; for (const auto &mesh : meshes) { size += mesh.size(); } std::vector> prims; prims.reserve(size); for (const auto &mesh : meshes) { prims.insert(prims.end(), mesh.prims.begin(), mesh.prims.end()); } return Mesh(prims); } /** * Produce a mesh of primitives that fill out the volume of the subgroup generated by generators g_gens within the group context */ template Mesh triangulate( const tc::Group &context, const std::vector &g_gens ) { if (g_gens.size() + 1 != N) // todo make static assert throw std::logic_error("g_gens size must be one less than N"); const auto &combos = Combos(g_gens, g_gens.size() - 1); std::vector> meshes; for (const auto &sg_gens : combos) { Mesh base = triangulate(context, sg_gens); Mesh raised = base.tile(context, g_gens, sg_gens); raised.prims.erase(raised.prims.begin(), raised.prims.begin() + base.size()); Mesh fan = raised.fan(0); meshes.push_back(fan); } return merge(meshes); } /** * Single-index primitives should not be further triangulated. */ template<> Mesh<1> triangulate<1>( const tc::Group &context, const std::vector &g_gens ) { if (not g_gens.empty()) // todo make static assert throw std::logic_error("g_gens must be empty for a trivial Mesh"); Mesh<1> res; res.prims.emplace_back(); return res; }