mirror of
https://github.com/allemangD/toddcox-visualize.git
synced 2025-11-10 03:52:48 -05:00
introduce Mesh class with subgroup context
This commit is contained in:
2
vendor/toddcox
vendored
2
vendor/toddcox
vendored
Submodule vendor/toddcox updated: 16c9d7d62f...265de59917
@@ -62,11 +62,6 @@ namespace cgl {
|
|||||||
put(data.data(), data.size(), usage);
|
put(data.data(), data.size(), usage);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
|
||||||
void put<std::vector<T>>(const std::vector<T> &data, GLenum usage = GL_STATIC_DRAW) {
|
|
||||||
put(data.data, data.size(), usage);
|
|
||||||
}
|
|
||||||
|
|
||||||
void bound(GLenum target, const std::function<void()> &action) const {
|
void bound(GLenum target, const std::function<void()> &action) const {
|
||||||
glBindBuffer(target, id);
|
glBindBuffer(target, id);
|
||||||
action();
|
action();
|
||||||
|
|||||||
@@ -47,8 +47,9 @@ public:
|
|||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
Slice(const tc::Group &g, T all_sg_gens, const std::vector<std::vector<int>> &exclude) : group(g) {
|
Slice(const tc::Group &g, T all_sg_gens, const std::vector<std::vector<int>> &exclude) : group(g) {
|
||||||
const auto &data = merge<N>(hull<N>(g, all_sg_gens, exclude));
|
auto mesh = merge(hull<N>(g, exclude));
|
||||||
ibo.put(data);
|
ibo.put(mesh);
|
||||||
|
|
||||||
vao.ipointer(0, ibo, 4, GL_UNSIGNED_INT);
|
vao.ipointer(0, ibo, 4, GL_UNSIGNED_INT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <geometry.hpp>
|
#include <geometry.hpp>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include "combo_iterator.hpp"
|
#include "combo_iterator.hpp"
|
||||||
|
|
||||||
@@ -19,204 +20,235 @@ std::vector<int> generators(const tc::Group &context) {
|
|||||||
return g_gens;
|
return g_gens;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
namespace {
|
||||||
* Determine which of g_gens are the correct names for sg_gens within the current context
|
/**
|
||||||
*/
|
* Determine which of g_gens are the correct names for sg_gens within the current context
|
||||||
std::vector<int> recontext_gens(
|
*/
|
||||||
const tc::Group &context,
|
std::vector<int> recontext_gens(
|
||||||
std::vector<int> g_gens,
|
const tc::Group &context,
|
||||||
std::vector<int> sg_gens) {
|
std::vector<int> g_gens,
|
||||||
|
std::vector<int> sg_gens) {
|
||||||
|
|
||||||
std::sort(g_gens.begin(), g_gens.end());
|
std::sort(g_gens.begin(), g_gens.end());
|
||||||
|
|
||||||
int inv_gen_map[context.ngens];
|
int inv_gen_map[context.ngens];
|
||||||
for (size_t i = 0; i < g_gens.size(); i++) {
|
for (size_t i = 0; i < g_gens.size(); i++) {
|
||||||
inv_gen_map[g_gens[i]] = i;
|
inv_gen_map[g_gens[i]] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<int> s_sg_gens;
|
/**
|
||||||
s_sg_gens.reserve(sg_gens.size());
|
* Solve the cosets generated by sg_gens within the subgroup generated by g_gens of the group context
|
||||||
for (const auto gen : sg_gens) {
|
*/
|
||||||
s_sg_gens.push_back(inv_gen_map[gen]);
|
tc::Cosets solve(
|
||||||
|
const tc::Group &context,
|
||||||
|
const std::vector<int> &g_gens,
|
||||||
|
const std::vector<int> &sg_gens
|
||||||
|
) {
|
||||||
|
const auto proper_sg_gens = recontext_gens(context, g_gens, sg_gens);
|
||||||
|
return context.subgroup(g_gens).solve(proper_sg_gens);
|
||||||
}
|
}
|
||||||
std::sort(s_sg_gens.begin(), s_sg_gens.end());
|
|
||||||
|
|
||||||
return s_sg_gens;
|
/**
|
||||||
|
* Apply some context transformation to all primitives of this mesh.
|
||||||
|
*/
|
||||||
|
template<unsigned N>
|
||||||
|
void apply(const tc::Cosets &table, int gen, Prims<N> &mat) {
|
||||||
|
auto data = mat.data();
|
||||||
|
for (int i = 0; i < mat.size(); ++i) {
|
||||||
|
data[i] = table.get(data[i], gen);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//template<unsigned N, class T>
|
||||||
* Solve the cosets generated by sg_gens within the subgroup generated by g_gens of the group context
|
//auto hull(const tc::Group &group, T all_sg_gens, const std::vector<std::vector<int>> &exclude) {
|
||||||
*/
|
// std::vector<Prims<N>> parts;
|
||||||
tc::Cosets solve(
|
// auto g_gens = generators(group);
|
||||||
const tc::Group &context,
|
// for (const std::vector<int> &sg_gens : all_sg_gens) {
|
||||||
const std::vector<int> &g_gens,
|
// bool excluded = false;
|
||||||
const std::vector<int> &sg_gens
|
// for (const auto &test : exclude) {
|
||||||
) {
|
// if (sg_gens == test) {
|
||||||
const auto proper_sg_gens = recontext_gens(context, g_gens, sg_gens);
|
// excluded = true;
|
||||||
return context.subgroup(g_gens).solve(proper_sg_gens);
|
// break;
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
|
// if (excluded) continue;
|
||||||
|
//
|
||||||
|
// const auto &base = triangulate<N>(group, sg_gens);
|
||||||
|
// const auto &tiles = tile<N>(base, group, g_gens, sg_gens);
|
||||||
|
// for (const auto &tile : tiles) {
|
||||||
|
// parts.push_back(tile);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return parts;
|
||||||
|
//}
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply some context transformation to all primitives of this mesh.
|
|
||||||
*/
|
|
||||||
template<unsigned N>
|
template<unsigned N>
|
||||||
void apply(const tc::Cosets &table, int gen, Prims<N> &mat) {
|
class Mesh {
|
||||||
auto data = mat.data();
|
public:
|
||||||
for (int i = 0; i < mat.size(); ++i) {
|
const tc::Group *g; // todo this needs to be handled more consistently
|
||||||
data[i] = table.get(data[i], gen);
|
std::vector<int> ctx;
|
||||||
|
Prims<N> prims;
|
||||||
|
|
||||||
|
Mesh(const tc::Group &g_, std::vector<int> ctx_, size_t cols);
|
||||||
|
|
||||||
|
Mesh(const tc::Group &g_, std::vector<int> ctx_);
|
||||||
|
|
||||||
|
Mesh<N> recontext(std::vector<int> ctx_);
|
||||||
|
|
||||||
|
std::vector<Mesh<N>> tile(const std::vector<int> &ctx_);
|
||||||
|
|
||||||
|
Mesh<N + 1> fan(unsigned root);
|
||||||
|
|
||||||
|
[[nodiscard]] size_t size() const { return prims.size(); }
|
||||||
|
|
||||||
|
[[nodiscard]] size_t rows() const { return prims.rows(); }
|
||||||
|
|
||||||
|
[[nodiscard]] size_t cols() const { return prims.cols(); }
|
||||||
|
|
||||||
|
[[nodiscard]] unsigned *data() { return prims.data(); }
|
||||||
|
|
||||||
|
[[nodiscard]] const unsigned *data() const { return prims.data(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class M>
|
||||||
|
M merge(const std::vector<M> &meshes) {
|
||||||
|
if (meshes.empty()) throw std::logic_error("cannot merge an empty list of meshes");
|
||||||
|
|
||||||
|
auto g = meshes[0].g;
|
||||||
|
auto ctx = meshes[0].ctx;
|
||||||
|
|
||||||
|
size_t cols = 0;
|
||||||
|
for (const auto &mesh : meshes) {
|
||||||
|
cols += mesh.prims.cols();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
M res(*g, ctx, cols);
|
||||||
|
|
||||||
|
size_t offset = 0;
|
||||||
|
for (const auto &mesh : meshes) {
|
||||||
|
res.prims.middleCols(offset, mesh.prims.cols()) = mesh.prims;
|
||||||
|
offset += mesh.prims.cols();
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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<unsigned N>
|
||||||
[[nodiscard]]
|
Mesh<N> Mesh<N>::recontext(std::vector<int> ctx_) {
|
||||||
Prims<N> recontext(
|
Mesh<N> res = *this;
|
||||||
Prims<N> prims,
|
res.ctx = ctx_;
|
||||||
const tc::Group &context,
|
|
||||||
const std::vector<int> &g_gens,
|
const auto proper_sg_gens = recontext_gens(*g, res.ctx, ctx);
|
||||||
const std::vector<int> &sg_gens
|
const auto table = solve(*g, res.ctx, {});
|
||||||
) {
|
const auto path = solve(*g, ctx, {}).path;
|
||||||
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<int, int>(0, proper_sg_gens, [table](int coset, int gen) {
|
auto map = path.template walk<int, int>(0, proper_sg_gens, [table](int coset, int gen) {
|
||||||
return table.get(coset, gen);
|
return table.get(coset, gen);
|
||||||
});
|
});
|
||||||
|
|
||||||
Prims<N> res(prims);
|
auto data = res.prims.data();
|
||||||
auto data = res.data();
|
for (int i = 0; i < res.prims.size(); ++i) {
|
||||||
for (int i = 0; i < prims.size(); ++i) {
|
|
||||||
data[i] = map[data[i]];
|
data[i] = map[data[i]];
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Union several meshes of the same dimension
|
|
||||||
*/
|
|
||||||
template<unsigned N>
|
template<unsigned N>
|
||||||
Prims<N> merge(const std::vector<Prims<N>> &meshes) {
|
std::vector<Mesh<N>> Mesh<N>::tile(const std::vector<int> &ctx_) {
|
||||||
size_t cols = 0;
|
auto base = recontext(ctx_);
|
||||||
for (const auto &mesh : meshes) {
|
|
||||||
cols += mesh.cols();
|
|
||||||
}
|
|
||||||
|
|
||||||
Prims<N> res(N, cols);
|
auto table = solve(*g, base.ctx, {});
|
||||||
|
auto path = solve(*g, base.ctx, ctx).path;
|
||||||
|
|
||||||
size_t offset = 0;
|
std::vector<Mesh<N>> res = path.template walk<Mesh<N>, int>(
|
||||||
for (const Prims<N> &mesh : meshes) {
|
base, generators(*g),
|
||||||
res.middleCols(offset, mesh.cols()) = mesh;
|
[&](Mesh<N> mesh, int gen) {
|
||||||
offset += mesh.cols();
|
apply<N>(table, gen, mesh.prims);
|
||||||
}
|
return mesh;
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<unsigned N>
|
|
||||||
[[nodiscard]]
|
|
||||||
std::vector<Prims<N>> tile(
|
|
||||||
Prims<N> prims,
|
|
||||||
const tc::Group &context,
|
|
||||||
const std::vector<int> &g_gens,
|
|
||||||
const std::vector<int> &sg_gens
|
|
||||||
) {
|
|
||||||
Prims<N> base = recontext<N>(prims, 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;
|
|
||||||
|
|
||||||
std::vector<int> _gens = generators(context);
|
|
||||||
|
|
||||||
std::vector<Prims<N>> res = path.walk<Prims<N>, int>(
|
|
||||||
base, _gens,
|
|
||||||
[&](Prims<N> from, int gen) {
|
|
||||||
apply<N>(table, gen, from);
|
|
||||||
return from;
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Produce a mesh of higher dimension by fanning a single point to all primitives in this mesh.
|
|
||||||
*/
|
|
||||||
template<unsigned N>
|
template<unsigned N>
|
||||||
[[nodiscard]]
|
Mesh<N + 1> Mesh<N>::fan(unsigned root) {
|
||||||
Prims<N + 1> fan(Prims<N> prims, int root) {
|
Mesh<N + 1> res(*g, ctx, prims.cols());
|
||||||
Prims<N + 1> res(N + 1, prims.cols());
|
|
||||||
|
|
||||||
res.topRows(1) = Prims<1>::Constant(1, prims.cols(), root);
|
res.prims.topRows(1) = Prims<1>::Constant(1, prims.cols(), root);
|
||||||
res.bottomRows(N) = prims;
|
res.prims.bottomRows(N) = prims;
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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<unsigned N>
|
||||||
Prims<N> triangulate(
|
Mesh<N>::Mesh(const tc::Group &g_, std::vector<int> ctx_, size_t cols)
|
||||||
const tc::Group &context,
|
: g(&g_), ctx(std::move(ctx_)) {
|
||||||
const std::vector<int> &g_gens
|
prims.setZero(N, cols);
|
||||||
) {
|
}
|
||||||
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);
|
template<unsigned N>
|
||||||
|
Mesh<N>::Mesh(const tc::Group &g_, std::vector<int> ctx_)
|
||||||
|
: g(&g_), ctx(std::move(ctx_)) {
|
||||||
|
if (ctx.size() + 1 != N) // todo make static assert
|
||||||
|
throw std::logic_error("ctx size must be one less than N");
|
||||||
|
|
||||||
std::vector<Prims<N>> meshes;
|
const auto &combos = Combos(ctx, ctx.size() - 1);
|
||||||
|
|
||||||
for (const auto &sg_gens : combos) {
|
std::vector<Mesh<N>> meshes;
|
||||||
auto base = triangulate<N - 1>(context, sg_gens);
|
|
||||||
auto parts = tile<N - 1>(base, context, g_gens, sg_gens);
|
for (const auto &sctx : combos) {
|
||||||
|
Mesh<N - 1> base(*g, sctx);
|
||||||
|
auto parts = base.tile(ctx);
|
||||||
parts.erase(parts.begin(), parts.begin() + 1);
|
parts.erase(parts.begin(), parts.begin() + 1);
|
||||||
auto raised = merge<N - 1>(parts);
|
|
||||||
auto fanned = fan<N - 1>(raised, 0);
|
if (parts.empty()) continue;
|
||||||
|
auto raised = merge(parts);
|
||||||
|
|
||||||
|
auto fanned = raised.fan(0);
|
||||||
meshes.push_back(fanned);
|
meshes.push_back(fanned);
|
||||||
}
|
}
|
||||||
|
|
||||||
return merge<N>(meshes);
|
prims = merge(meshes).prims;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Single-index primitives should not be further triangulated.
|
|
||||||
*/
|
|
||||||
template<>
|
template<>
|
||||||
Prims<1> triangulate<1>(
|
Mesh<1>::Mesh(const tc::Group &g_, std::vector<int> ctx_) : g(&g_), ctx(std::move(ctx_)) {
|
||||||
const tc::Group &context,
|
if (not ctx.empty())
|
||||||
const std::vector<int> &g_gens
|
throw std::logic_error("ctx must be empty for a trivial Mesh.");
|
||||||
) {
|
|
||||||
if (not g_gens.empty()) // todo make static assert
|
|
||||||
throw std::logic_error("g_gens must be empty for a trivial Mesh");
|
|
||||||
|
|
||||||
return Prims<1>::Zero(1, 1);
|
prims.setZero(1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<unsigned N, class T>
|
template<unsigned N>
|
||||||
auto hull(const tc::Group &group, T all_sg_gens, const std::vector<std::vector<int>> &exclude) {
|
auto hull(const tc::Group &g, const std::vector<std::vector<int>> &exclude) {
|
||||||
std::vector<Prims<N>> parts;
|
std::vector<Mesh<N>> parts;
|
||||||
auto g_gens = generators(group);
|
|
||||||
for (const std::vector<int> &sg_gens : all_sg_gens) {
|
auto ctx = generators(g);
|
||||||
bool excluded = false;
|
auto sub_ctxs = Combos(ctx, N - 1);
|
||||||
for (const auto &test : exclude) {
|
|
||||||
if (sg_gens == test) {
|
for (const auto &sub_ctx : sub_ctxs) {
|
||||||
excluded = true;
|
bool excluded = std::any_of(
|
||||||
break;
|
exclude.begin(), exclude.end(),
|
||||||
}
|
[&](auto e) { return e == sub_ctx; }
|
||||||
}
|
);
|
||||||
if (excluded) continue;
|
if (excluded) continue;
|
||||||
|
|
||||||
const auto &base = triangulate<N>(group, sg_gens);
|
auto sub_parts = Mesh<N>(g, sub_ctx).tile(ctx);
|
||||||
const auto &tiles = tile<N>(base, group, g_gens, sg_gens);
|
parts.insert(parts.end(), sub_parts.begin(), sub_parts.end());
|
||||||
for (const auto &tile : tiles) {
|
|
||||||
parts.push_back(tile);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return parts;
|
return parts;
|
||||||
}
|
}
|
||||||
@@ -18,9 +18,9 @@ mat5 wander(float time) {
|
|||||||
r *= rot<5>(1, 2, time * .13f);
|
r *= rot<5>(1, 2, time * .13f);
|
||||||
r *= rot<5>(0, 1, time * .20f);
|
r *= rot<5>(0, 1, time * .20f);
|
||||||
|
|
||||||
r *= rot<5>(0, 3, time * .17f);
|
// r *= rot<5>(0, 3, time * .17f);
|
||||||
r *= rot<5>(1, 3, time * .25f);
|
// r *= rot<5>(1, 3, time * .25f);
|
||||||
r *= rot<5>(2, 3, time * .12f);
|
// r *= rot<5>(2, 3, time * .12f);
|
||||||
|
|
||||||
// r *= rot<5>(1, 4, time * .27f);
|
// r *= rot<5>(1, 4, time * .27f);
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ public:
|
|||||||
|
|
||||||
std::cout << utilInfo();
|
std::cout << utilInfo();
|
||||||
|
|
||||||
std::vector<int> symbol = {3, 4, 3, 2};
|
std::vector<int> symbol = {5, 3, 2, 2};
|
||||||
root << .80, .02, .02, .02, .02;
|
root << .80, .02, .02, .02, .02;
|
||||||
|
|
||||||
auto group = tc::schlafli(symbol);
|
auto group = tc::schlafli(symbol);
|
||||||
|
|||||||
Reference in New Issue
Block a user