ENH: Simplify solving logic

This is a combination of multiple WIP commits:

- Semantic group names - G, H
- Simplify fan<>()
- Template int deduction
- Remove _recontext_gens
- Move g_gens logic into caller
- Move subgroup logic into caller
- Move recontext logic into coller
- Move _generators, _recontext_gens to anonymous namespace
- Simplify hull, tile
This commit is contained in:
David Allemang
2023-02-07 16:15:19 -05:00
parent 47a78027bd
commit 92a55f8c74
7 changed files with 91 additions and 147 deletions

View File

@@ -92,6 +92,7 @@ namespace tc {
private: private:
size_t _rank; size_t _rank;
std::vector<size_t> _mults; std::vector<size_t> _mults;
std::vector<size_t> _gens;
public: public:
Group(Group const &) = default; Group(Group const &) = default;
@@ -108,7 +109,11 @@ namespace tc {
[[nodiscard]] size_t rank() const; [[nodiscard]] size_t rank() const;
[[nodiscard]] Group sub(std::vector<size_t> const &idxs) const; [[nodiscard]] std::vector<size_t> gens() const;
[[nodiscard]] Group sub(std::vector<size_t> const &gens) const;
[[nodiscard]] std::vector<Group> subs(size_t rank) const;
[[nodiscard]] Cosets solve(std::vector<size_t> const &idxs = {}, size_t bound = SIZE_MAX) const; [[nodiscard]] Cosets solve(std::vector<size_t> const &idxs = {}, size_t bound = SIZE_MAX) const;
}; };

View File

@@ -1,9 +1,12 @@
#include <tc/core.hpp> #include <tc/core.hpp>
#include <cassert> #include <cassert>
#include <numeric>
namespace tc { namespace tc {
Group::Group(size_t rank) : _rank(rank), _mults(_rank * _rank, 2) { Group::Group(size_t rank) : _rank(rank), _mults(_rank * _rank, 2), _gens(_rank) {
std::iota(_gens.begin(), _gens.end(), 0);
for (int idx = 0; idx < rank; ++idx) { for (int idx = 0; idx < rank; ++idx) {
set(idx, idx, 1); set(idx, idx, 1);
} }
@@ -28,15 +31,42 @@ namespace tc {
return _rank; return _rank;
} }
[[nodiscard]] Group Group::sub(std::vector<size_t> const &idxs) const { [[nodiscard]] std::vector<size_t> Group::gens() const {
Group res(idxs.size()); return _gens;
}
for (int i = 0; i < idxs.size(); ++i) { [[nodiscard]] Group Group::sub(std::vector<size_t> const &gens) const {
for (int j = i; j < idxs.size(); ++j) { Group res(gens.size());
res.set(i, j, get(idxs[i], idxs[j])); res._gens = gens;
for (int i = 0; i < gens.size(); ++i) {
for (int j = i; j < gens.size(); ++j) {
res.set(i, j, get(gens[i], gens[j]));
} }
} }
return res; return res;
} }
[[nodiscard]] std::vector<Group> Group::subs(size_t rank) const {
std::vector<bool> mask(_rank, false);
std::fill(mask.begin(), mask.begin() + rank, true);
std::vector<size_t> sub_gens(rank);
std::vector<Group> res;
do {
for (int i = 0, j = 0; i < _rank; ++i) {
if (mask[i]) sub_gens[j++] = i;
}
res.push_back(sub(sub_gens));
} while (std::next_permutation(
mask.begin(),
mask.end(),
std::greater<>()
));
return res;
}
} }

View File

@@ -1,30 +0,0 @@
#pragma once
#include <set>
#include <algorithm>
template<typename V, typename M>
V select(const V &data, const M &mask, size_t count) {
V result;
result.reserve(count);
for (int i = 0; i < mask.size(); ++i) {
if (mask[i]) result.push_back(data[i]);
}
return result;
}
template<typename V>
std::vector<V> combinations(const V &data, const size_t count) {
std::vector<V> result;
std::vector<bool> mask(data.size(), false);
std::fill(mask.begin(), mask.begin() + count, true);
do {
result.push_back(select(data, mask, count));
} while (std::next_permutation(mask.begin(), mask.end(), std::greater<>()));
return result;
}

View File

@@ -8,8 +8,6 @@
#include <Eigen/Eigen> #include <Eigen/Eigen>
#include "combo_iterator.hpp"
template<unsigned N> template<unsigned N>
using Indices = Eigen::Array<unsigned, N, Eigen::Dynamic>; using Indices = Eigen::Array<unsigned, N, Eigen::Dynamic>;

View File

@@ -8,55 +8,20 @@
#include <geometry.hpp> #include <geometry.hpp>
#include "combo_iterator.hpp"
/**
* Produce a list of all generators for the group context. The range [0..group.rank()).
*/
std::vector<size_t> _generators(
const tc::Group &context
) {
std::vector<size_t> g_gens(context.rank());
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
*
* For example if g_gens contains {a, b, c, d} and sg_gens contains {b, d, a} then the result is {1, 3, 0}
*/
std::vector<size_t> _recontext_gens(
std::vector<size_t> g_gens,
std::vector<size_t> sg_gens
) {
auto orig = sg_gens;
for (size_t &gen: sg_gens) {
gen = std::distance(
g_gens.begin(),
std::find(g_gens.begin(), g_gens.end(), gen)
);
}
return sg_gens;
}
/** /**
* 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. * 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.
*/ */
template<unsigned N> template<int N>
[[nodiscard]] [[nodiscard]]
Indices<N> recontext( Indices<N> recontext(
Indices<N> prims, Indices<N> prims,
const tc::Group &context, const tc::Group &G,
const std::vector<size_t> &g_gens, const tc::Group &H
const std::vector<size_t> &sg_gens
) { ) {
const auto proper_sg_gens = _recontext_gens(g_gens, sg_gens); const auto table = G.solve({});
const auto table = context.sub(g_gens).solve({}); const auto cosets = H.solve({});
const auto cosets = context.sub(sg_gens).solve({});
tc::Path<size_t> path(cosets, proper_sg_gens); tc::Path<size_t> path(cosets, H.gens());
std::vector<size_t> map(path.order()); std::vector<size_t> map(path.order());
path.walk(0, [&table](size_t coset, size_t gen) { path.walk(0, [&table](size_t coset, size_t gen) {
@@ -75,7 +40,7 @@ Indices<N> recontext(
/** /**
* Union several meshes of the same dimension * Union several meshes of the same dimension
*/ */
template<unsigned N> template<int N>
Indices<N> merge( Indices<N> merge(
const std::vector<Indices<N>> &meshes const std::vector<Indices<N>> &meshes
) { ) {
@@ -95,25 +60,21 @@ Indices<N> merge(
return res; return res;
} }
template<unsigned N> template<int N>
[[nodiscard]] [[nodiscard]]
std::vector<Indices<N>> tile( std::vector<Indices<N>> tile(
Indices<N> prims, Indices<N> prims,
const tc::Group &context, const tc::Group &G,
const std::vector<size_t> &g_gens, const tc::Group &H
const std::vector<size_t> &sg_gens
) { ) {
Indices<N> base = recontext<N>(prims, context, g_gens, sg_gens); const auto &table = G.solve({});
const auto proper_sg_gens = _recontext_gens(g_gens, sg_gens); const auto &cosets = G.solve(H.gens());
const auto &table = context.sub(g_gens).solve({});
const auto &cosets = context.sub(g_gens).solve(proper_sg_gens);
tc::Path<> path(cosets); tc::Path<> path(cosets);
std::vector<Indices<N>> res(path.order()); std::vector<Indices<N>> res(path.order());
path.walk(base, [&](Indices<N> from, auto gen) { path.walk(prims, [&](Indices<N> from, auto gen) {
for (int i = 0; i < from.size(); ++i) { for (int i = 0; i < from.size(); ++i) {
from(i) = table.get(from(i), gen); from(i) = table.get(from(i), gen);
} }
@@ -126,7 +87,7 @@ std::vector<Indices<N>> tile(
/** /**
* Produce a mesh of higher dimension by fanning a single point to all primitives in this mesh. * Produce a mesh of higher dimension by fanning a single point to all primitives in this mesh.
*/ */
template<unsigned N> template<int N>
[[nodiscard]] [[nodiscard]]
Indices<N + 1> fan( Indices<N + 1> fan(
Indices<N> prims, Indices<N> prims,
@@ -134,8 +95,8 @@ Indices<N + 1> fan(
) { ) {
Indices<N + 1> res(N + 1, prims.cols()); Indices<N + 1> res(N + 1, prims.cols());
res.topRows(1) = Indices<1>::Constant(1, prims.cols(), root); res.topRows(N) = prims;
res.bottomRows(N) = prims; res.bottomRows(1) = root;
return res; return res;
} }
@@ -143,67 +104,53 @@ Indices<N + 1> fan(
/** /**
* Produce a mesh of primitives that fill out the volume of the subgroup generated by generators g_gens within the group context * Produce a mesh of primitives that fill out the volume of the subgroup generated by generators g_gens within the group context
*/ */
template<unsigned N> template<int N>
Indices<N> triangulate( Indices<N> cell(
const tc::Group &context, const tc::Group &G
const std::vector<size_t> &g_gens
) { ) {
if (g_gens.size() + 1 != N) // todo make static assert assert(G.rank() + 1 == N); // g_gens size must be one less than N
throw std::logic_error("g_gens size must be one less than N");
const auto &combos = combinations(g_gens, g_gens.size() - 1); std::vector<Indices<N - 1>> facets;
std::vector<Indices<N>> meshes; for (auto H : G.subs(N - 2)) {
auto sub_base = cell<N - 1>(H);
auto base = recontext(sub_base, G, H);
auto tiles = tile(base, G, H);
for (const auto &sg_gens: combos) { facets.insert(
auto base = triangulate<N - 1>(context, sg_gens); facets.end(),
auto parts = tile<N - 1>(base, context, g_gens, sg_gens); tiles.begin() + 1, // skip "near" facet
parts.erase(parts.begin(), parts.begin() + 1); tiles.end()
auto raised = merge<N - 1>(parts); );
auto fanned = fan<N - 1>(raised, 0);
meshes.push_back(fanned);
} }
return merge<N>(meshes); return fan(merge(facets), 0);
} }
/** /**
* Single-index primitives should not be further triangulated. * Single-index primitives should not be further triangulated.
*/ */
template<> template<>
Indices<1> triangulate<1>( Indices<1> cell<1>(
const tc::Group &context, const tc::Group &G
const std::vector<size_t> &g_gens
) { ) {
if (not g_gens.empty()) // todo make static assert assert(G.rank() == 0); // rank must be 0 for trivial Mesh
throw std::logic_error("g_gens must be empty for a trivial Mesh");
return Indices<1>::Zero(1, 1); return Indices<1>::Zero(1, 1);
} }
template<unsigned N, class T> template<int N>
auto hull( auto hull(
const tc::Group &group, const tc::Group &G
T all_sg_gens,
const std::vector<std::vector<size_t>> &exclude
) { ) {
std::vector<Indices<N>> parts; std::vector<Indices<N>> parts;
auto g_gens = _generators(group);
for (const std::vector<size_t> &sg_gens: all_sg_gens) {
bool excluded = false;
for (const auto &test: exclude) {
if (sg_gens == test) {
excluded = true;
break;
}
}
if (excluded) continue;
const auto &base = triangulate<N>(group, sg_gens); for (auto H: G.subs(N - 1)) {
const auto &tiles = tile<N>(base, group, g_gens, sg_gens); auto sub_base = cell<N>(H);
for (const auto &tile: tiles) { auto base = recontext(sub_base, G, H);
parts.push_back(tile); auto tiles = tile(base, G, H);
} parts.push_back(merge(tiles));
} }
return parts; return parts;
} }

View File

@@ -22,9 +22,6 @@ namespace vis {
tc::Group group; tc::Group group;
vec5 root; vec5 root;
vec3 color; vec3 color;
std::vector<std::vector<size_t>> exclude {{0, 1, 2}};
std::vector<std::vector<size_t>> include = combinations(_generators(group), 3);
}; };
struct VBOs { struct VBOs {
@@ -62,9 +59,13 @@ namespace vis {
vbos.verts.put(lower.colwise().begin(), lower.colwise().end()); vbos.verts.put(lower.colwise().begin(), lower.colwise().end());
// todo generate all, then mask using glMultiDraw. // todo generate all, then mask using glMultiDraw.
const size_t N = 4; const Eigen::Index N = 4;
auto inds = merge<N>(hull<N>(group.group, group.include, group.exclude)); auto tiles = hull<N>(group.group);
tiles.erase(tiles.begin()); // remove {0, 1, 2} cells
auto inds = merge<N>(tiles);
vbos.ibo.put(inds.colwise().begin(), inds.colwise().end()); vbos.ibo.put(inds.colwise().begin(), inds.colwise().end());
} }

View File

@@ -140,14 +140,7 @@ int run(GLFWwindow* window, ImGuiContext* ctx) {
entity, entity,
tc::schlafli({5, 3, 3, 2}), tc::schlafli({5, 3, 3, 2}),
vec5{0.80, 0.09, 0.09, 0.09, 0.09}, vec5{0.80, 0.09, 0.09, 0.09, 0.09},
vec3{0.90, 0.90, 0.90}, vec3{0.90, 0.90, 0.90}
std::vector<std::vector<size_t>>{
{0, 1, 2},
{0, 3, 4},
{1, 3, 4},
{2, 3, 4},
}
); );
registry.emplace<vis::VBOs>(entity); registry.emplace<vis::VBOs>(entity);