mirror of
https://github.com/allemangD/toddcox-visualize.git
synced 2025-11-10 12:02:47 -05:00
Replace Primitive vector with Eigen matrices.
template<unsigned N> Prims<N> = Eigen::Matrix<unsigned, N, Eigen::Dynamic> Replaces std::vector<Primitive<N>>
This commit is contained in:
@@ -54,7 +54,11 @@ namespace cgl {
|
||||
}
|
||||
|
||||
void put(const std::vector<T> &data, GLenum usage = GL_STATIC_DRAW) {
|
||||
glNamedBufferData(id, sizeof(T) * data.size(), &data[0], usage);
|
||||
put(&data[0], data.size(), usage);
|
||||
}
|
||||
|
||||
void put(const T *data, const size_t &size, GLenum usage = GL_STATIC_DRAW) {
|
||||
glNamedBufferData(id, sizeof(T) * size, data, usage);
|
||||
}
|
||||
|
||||
void bound(GLenum target, const std::function<void()> &action) const {
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
#include <iostream>
|
||||
#include "combo_iterator.hpp"
|
||||
|
||||
template<unsigned N>
|
||||
using Prims = Eigen::Matrix<unsigned, N, Eigen::Dynamic>;
|
||||
|
||||
template<int N>
|
||||
using vec = Eigen::Matrix<float, N, 1>;
|
||||
template<int N>
|
||||
@@ -33,35 +36,3 @@ mat4 ortho(float left, float right, float bottom, float top, float front, float
|
||||
0, 0, 0, 1;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* An primitive stage N indices.
|
||||
* @tparam N
|
||||
*/
|
||||
template<unsigned N>
|
||||
struct Primitive {
|
||||
static_assert(N > 0, "Primitives must contain at least one point. Primitive<0> or lower is impossible.");
|
||||
|
||||
std::array<unsigned, N> inds;
|
||||
|
||||
Primitive() = default;
|
||||
|
||||
Primitive(const Primitive<N> &) = default;
|
||||
|
||||
Primitive(const Primitive<N - 1> &sub, unsigned root) {
|
||||
std::copy(sub.inds.begin(), sub.inds.end(), inds.begin());
|
||||
inds[N - 1] = root;
|
||||
}
|
||||
|
||||
explicit Primitive(const std::vector<unsigned> &values) {
|
||||
std::copy(values.begin(), values.begin() + N, inds.begin());
|
||||
}
|
||||
|
||||
~Primitive() = default;
|
||||
|
||||
void apply(const tc::Cosets &table, int gen) {
|
||||
for (auto &ind : inds) {
|
||||
ind = table.get(ind, gen);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -38,17 +38,18 @@ private:
|
||||
const tc::Group group;
|
||||
|
||||
public:
|
||||
cgl::Buffer<Primitive<N>> ibo;
|
||||
cgl::Buffer<unsigned> ibo;
|
||||
cgl::Buffer<vec4> vbo;
|
||||
cgl::VertexArray vao;
|
||||
|
||||
template<class T>
|
||||
Slice(const tc::Group &g, T all_sg_gens, const std::vector<std::vector<int>> &exclude) : group(g) {
|
||||
ibo.put(merge<N>(hull<N>(g, all_sg_gens, exclude)));
|
||||
const Prims<N> &data = merge<N>(hull<N>(g, all_sg_gens, exclude));
|
||||
ibo.put(data.data(), data.size());
|
||||
vao.ipointer(0, ibo, 4, GL_UNSIGNED_INT);
|
||||
}
|
||||
|
||||
void setPoints(const vec5 &root, mat5 transform = mat5::Identity()) {
|
||||
void setPoints(const vec5 &root, const mat5 &transform = mat5::Identity()) {
|
||||
auto cosets = group.solve();
|
||||
auto mirrors = mirror<5>(group);
|
||||
|
||||
|
||||
@@ -60,11 +60,11 @@ tc::Cosets solve(
|
||||
* Apply some context transformation to all primitives of this mesh.
|
||||
*/
|
||||
template<unsigned N>
|
||||
std::vector<Primitive<N>> apply(std::vector<Primitive<N>> prims, const tc::Cosets &table, int gen) {
|
||||
for (auto &prim : prims) {
|
||||
prim.apply(table, gen);
|
||||
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);
|
||||
}
|
||||
return prims;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,8 +72,8 @@ std::vector<Primitive<N>> apply(std::vector<Primitive<N>> prims, const tc::Coset
|
||||
*/
|
||||
template<unsigned N>
|
||||
[[nodiscard]]
|
||||
std::vector<Primitive<N>> recontext(
|
||||
std::vector<Primitive<N>> prims,
|
||||
Prims<N> recontext(
|
||||
Prims<N> prims,
|
||||
const tc::Group &context,
|
||||
const std::vector<int> &g_gens,
|
||||
const std::vector<int> &sg_gens
|
||||
@@ -86,11 +86,10 @@ std::vector<Primitive<N>> recontext(
|
||||
return table.get(coset, gen);
|
||||
});
|
||||
|
||||
std::vector<Primitive<N>> res(prims);
|
||||
for (Primitive<N> &prim : res) {
|
||||
for (auto &ind : prim.inds) {
|
||||
ind = map[ind];
|
||||
}
|
||||
Prims<N> res(prims);
|
||||
auto data = res.data();
|
||||
for (int i = 0; i < prims.size(); ++i) {
|
||||
data[i] = map[data[i]];
|
||||
}
|
||||
|
||||
return res;
|
||||
@@ -100,16 +99,18 @@ std::vector<Primitive<N>> recontext(
|
||||
* Union several meshes of the same dimension
|
||||
*/
|
||||
template<unsigned N>
|
||||
std::vector<Primitive<N>> merge(const std::vector<std::vector<Primitive<N>>> &meshes) {
|
||||
size_t size = 0;
|
||||
Prims<N> merge(const std::vector<Prims<N>> &meshes) {
|
||||
size_t cols = 0;
|
||||
for (const auto &mesh : meshes) {
|
||||
size += mesh.size();
|
||||
cols += mesh.cols();
|
||||
}
|
||||
|
||||
std::vector<Primitive<N>> res;
|
||||
res.reserve(size);
|
||||
for (const auto &mesh : meshes) {
|
||||
res.insert(res.end(), mesh.begin(), mesh.end());
|
||||
Prims<N> res(N, cols);
|
||||
|
||||
size_t offset = 0;
|
||||
for (const Prims<N> &mesh : meshes) {
|
||||
res.middleCols(offset, mesh.cols()) = mesh;
|
||||
offset += mesh.cols();
|
||||
}
|
||||
|
||||
return res;
|
||||
@@ -117,52 +118,42 @@ std::vector<Primitive<N>> merge(const std::vector<std::vector<Primitive<N>>> &me
|
||||
|
||||
template<unsigned N>
|
||||
[[nodiscard]]
|
||||
std::vector<std::vector<Primitive<N>>> each_tile(
|
||||
std::vector<Primitive<N>> prims,
|
||||
std::vector<Prims<N>> tile(
|
||||
Prims<N> prims,
|
||||
const tc::Group &context,
|
||||
const std::vector<int> &g_gens,
|
||||
const std::vector<int> &sg_gens
|
||||
) {
|
||||
std::vector<Primitive<N>> base = recontext(prims, context, g_gens, 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;
|
||||
|
||||
auto _gens = generators(context);
|
||||
std::vector<int> _gens = generators(context);
|
||||
|
||||
auto res = path.walk<std::vector<Primitive<N>>, int>(base, generators(context), [&](auto from, auto gen) {
|
||||
return apply(from, table, gen);
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
template<unsigned N>
|
||||
[[nodiscard]]
|
||||
std::vector<Primitive<N>> tile(
|
||||
std::vector<Primitive<N>> prims,
|
||||
const tc::Group &context,
|
||||
const std::vector<int> &g_gens,
|
||||
const std::vector<int> &sg_gens
|
||||
) {
|
||||
auto res = each_tile<N>(prims, context, g_gens, sg_gens);
|
||||
|
||||
return merge(res);
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a mesh of higher dimension by fanning a single point to all primitives in this mesh.
|
||||
*/
|
||||
template<unsigned N>
|
||||
[[nodiscard]]
|
||||
std::vector<Primitive<N + 1>> fan(std::vector<Primitive<N>> prims, int root) {
|
||||
std::vector<Primitive<N + 1>> res(prims.size());
|
||||
std::transform(prims.begin(), prims.end(), res.begin(),
|
||||
[root](const Primitive<N> &prim) {
|
||||
return Primitive<N + 1>(prim, root);
|
||||
}
|
||||
);
|
||||
Prims<N + 1> fan(Prims<N> prims, int root) {
|
||||
Prims<N + 1> res(N + 1, prims.cols());
|
||||
|
||||
res.topRows(1) = Prims<1>::Constant(1, prims.cols(), root);
|
||||
res.bottomRows(N) = prims;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -170,7 +161,7 @@ std::vector<Primitive<N + 1>> fan(std::vector<Primitive<N>> prims, int root) {
|
||||
* 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>
|
||||
std::vector<Primitive<N>> triangulate(
|
||||
Prims<N> triangulate(
|
||||
const tc::Group &context,
|
||||
const std::vector<int> &g_gens
|
||||
) {
|
||||
@@ -179,37 +170,37 @@ std::vector<Primitive<N>> triangulate(
|
||||
|
||||
const auto &combos = Combos(g_gens, g_gens.size() - 1);
|
||||
|
||||
std::vector<std::vector<Primitive<N>>> meshes;
|
||||
std::vector<Prims<N>> meshes;
|
||||
|
||||
for (const auto &sg_gens : combos) {
|
||||
auto base = triangulate<N - 1>(context, sg_gens);
|
||||
auto raised = tile(base, context, g_gens, sg_gens);
|
||||
raised.erase(raised.begin(), raised.begin() + base.size());
|
||||
meshes.push_back(fan(raised, 0));
|
||||
auto parts = tile<N - 1>(base, context, g_gens, sg_gens);
|
||||
parts.erase(parts.begin(), parts.begin() + 1);
|
||||
auto raised = merge<N - 1>(parts);
|
||||
auto fanned = fan<N - 1>(raised, 0);
|
||||
meshes.push_back(fanned);
|
||||
}
|
||||
|
||||
return merge(meshes);
|
||||
return merge<N>(meshes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Single-index primitives should not be further triangulated.
|
||||
*/
|
||||
template<>
|
||||
std::vector<Primitive<1>> triangulate(
|
||||
Prims<1> triangulate<1>(
|
||||
const tc::Group &context,
|
||||
const std::vector<int> &g_gens
|
||||
) {
|
||||
if (not g_gens.empty()) // todo make static assert
|
||||
throw std::logic_error("g_gens must be empty for a trivial Mesh");
|
||||
|
||||
std::vector<Primitive<1>> res;
|
||||
res.emplace_back();
|
||||
return res;
|
||||
return Prims<1>::Zero(1, 1);
|
||||
}
|
||||
|
||||
template<unsigned N, class T>
|
||||
auto hull(const tc::Group &group, T all_sg_gens, const std::vector<std::vector<int>> &exclude) {
|
||||
std::vector<std::vector<Primitive<N>>> parts;
|
||||
std::vector<Prims<N>> parts;
|
||||
auto g_gens = generators(group);
|
||||
for (const std::vector<int> &sg_gens : all_sg_gens) {
|
||||
bool excluded = false;
|
||||
@@ -222,7 +213,7 @@ auto hull(const tc::Group &group, T all_sg_gens, const std::vector<std::vector<i
|
||||
if (excluded) continue;
|
||||
|
||||
const auto &base = triangulate<N>(group, sg_gens);
|
||||
const auto &tiles = each_tile(base, group, g_gens, sg_gens);
|
||||
const auto &tiles = tile<N>(base, group, g_gens, sg_gens);
|
||||
for (const auto &tile : tiles) {
|
||||
parts.push_back(tile);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user