9 Commits

Author SHA1 Message Date
David Allemang
5f9f4e568b Temporarily remove Nanogui window. 2021-01-15 19:45:29 -05:00
David Allemang
2d6dbc6804 Rename "hull" and "fill" to be more descriptive. 2021-01-15 19:44:33 -05:00
David Allemang
a28cc2d8be Add "center" offset to each slice 2020-10-29 20:43:06 -04:00
fbd23aea02 introduce set_union wrapper
also rename set_difference wrapper
2020-10-25 00:55:52 -04:00
a3233c2686 support multiple slices 2020-10-25 00:31:38 -04:00
c1f5163008 reintroduce each_tile and tile.
each_tile produces a vector of mesh parts, and tile produces the merged result.
2020-10-25 00:31:28 -04:00
David Allemang
01043e9bce Replace ComboIterator with std::set<std::vector<int>> via combinations.hpp
Since the number of generators never exceeds 6, the maximum number of combinations is (6, 3) = 20, so the space optimizations of using an iterator is mute.

Doing this way also allows to use set_difference and set_union to deal with collections of subgroups. This is not easily possible otherwise.
2020-10-24 23:06:52 -04:00
David Allemang
6e3ea1900b Convert hull to static method; inline all_sg_gens and exclude parameters for future testing. 2020-10-22 21:52:25 -04:00
David Allemang
9ce626ee64 introduce Mesh class with subgroup context 2020-10-14 17:14:25 -04:00
8 changed files with 279 additions and 279 deletions

2
vendor/toddcox vendored

View File

@@ -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();

View File

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

View File

@@ -1,98 +0,0 @@
#pragma once
#include <vector>
#include <array>
#include <algorithm>
#include <numeric>
#include <stdexcept>
size_t choose(size_t n, size_t k) {
if (k == 0) return 1;
return n * choose(n - 1, k - 1) / k;
}
template<class T>
class ComboIterator {
private:
const std::vector<T> &options;
std::vector<bool> bits;
std::vector<T> curr;
int at;
void set_curr() {
for (int i = 0, j = 0; i < bits.size(); ++i) {
if (bits[i]) curr[j++] = options[i];
}
}
public:
ComboIterator(const std::vector<T> &options, int k, int at = 0)
: options(options), bits(options.size()), curr(k), at(at) {
std::fill(bits.begin(), bits.begin() + k, true);
set_curr();
}
[[nodiscard]] bool operator==(const ComboIterator<T> &o) const {
return at == o.at;
}
[[nodiscard]] bool operator!=(const ComboIterator<T> &o) const {
return at != o.at;
}
auto operator*() const {
return curr;
}
const auto &operator->() const {
return &this;
}
auto operator++(int) {
std::prev_permutation(bits.begin(), bits.end());
set_curr();
++at;
return *this;
}
auto operator++() &{
auto res = *this;
(*this)++;
return res;
}
auto operator--(int) {
std::next_permutation(bits.begin(), bits.end());
set_curr();
--at;
return *this;
}
auto operator--() &{
auto res = *this;
(*this)--;
return res;
}
};
template<class T>
class Combos {
private:
const std::vector<T> options;
int k;
int size;
public:
Combos(const std::vector<T> &options, int k)
: options(options), k(k), size(choose(options.size(), k)) {
}
ComboIterator<T> begin() const {
return ComboIterator<T>(options, k);
}
ComboIterator<T> end() const {
return ComboIterator<T>(options, k, size);
}
};

View File

@@ -5,7 +5,6 @@
#include <optional> #include <optional>
#include <numeric> #include <numeric>
#include <iostream> #include <iostream>
#include "combo_iterator.hpp"
template<unsigned N> template<unsigned N>
using Prims = Eigen::Matrix<unsigned, N, Eigen::Dynamic>; using Prims = Eigen::Matrix<unsigned, N, Eigen::Dynamic>;

View File

@@ -5,7 +5,8 @@
#include <cgl/pipeline.hpp> #include <cgl/pipeline.hpp>
#include <geometry.hpp> #include <geometry.hpp>
#include "mirror.hpp" #include <mirror.hpp>
#include <combinations.hpp>
struct Matrices { struct Matrices {
mat4 proj = mat4::Identity(); mat4 proj = mat4::Identity();
@@ -45,14 +46,21 @@ public:
cgl::Buffer<vec4> vbo; cgl::Buffer<vec4> vbo;
cgl::VertexArray vao; cgl::VertexArray vao;
template<class T> vec5 root = vec5::Ones().normalized();
Slice(const tc::Group &g, T all_sg_gens, const std::vector<std::vector<int>> &exclude) : group(g) { vec5 center = vec5::Zero();
const auto &data = merge<N>(hull<N>(g, all_sg_gens, exclude)); mat5 transform = mat5::Identity();
ibo.put(data);
vec3 color = vec3::Ones();
explicit Slice(const tc::Group &g) : group(g) {
vao.ipointer(0, ibo, 4, GL_UNSIGNED_INT); vao.ipointer(0, ibo, 4, GL_UNSIGNED_INT);
} }
void setPoints(const vec5 &root, const mat5 &transform = mat5::Identity()) { void setMesh(const Mesh<N> &mesh) {
ibo.put(mesh);
}
void setPoints() {
auto cosets = group.solve(); auto cosets = group.solve();
auto mirrors = mirror<5>(group); auto mirrors = mirror<5>(group);
@@ -63,7 +71,7 @@ public:
std::transform( std::transform(
higher.begin(), higher.end(), higher.begin(), higher.begin(), higher.end(), higher.begin(),
[&](const vec5& v) { return transform * v; } [&](const vec5& v) { return center + transform * v; }
); );
std::vector<vec4> lower(higher.size()); std::vector<vec4> lower(higher.size());
@@ -97,7 +105,7 @@ public:
void draw(const Slice<N> &prop) const { void draw(const Slice<N> &prop) const {
glBindProgramPipeline(pipe); glBindProgramPipeline(pipe);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, prop.vbo); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, prop.vbo);
glProgramUniform3f(solid, 2, 1.f, 1.f, 1.f); glProgramUniform3fv(solid, 2, 1, prop.color.data());
glBindVertexArray(prop.vao); glBindVertexArray(prop.vao);
glDrawArrays(GL_POINTS, 0, prop.ibo.count() * N); glDrawArrays(GL_POINTS, 0, prop.ibo.count() * N);
} }

View File

@@ -7,8 +7,9 @@
#include <iostream> #include <iostream>
#include <geometry.hpp> #include <geometry.hpp>
#include <utility>
#include "combo_iterator.hpp" #include <combinations.hpp>
/** /**
* Produce a list of all generators for the group context. The range [0..group.ngens). * Produce a list of all generators for the group context. The range [0..group.ngens).
@@ -19,204 +20,237 @@ 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);
}
}
} }
/**
* 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<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);
}
/**
* 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);
static Mesh<N> fill(const tc::Group &g, const std::vector<int> &ctx);
// template<class SC>
// static Mesh<N> hull(const tc::Group &g, const std::vector<int> &ctx, const SC &sub_ctxs);
Mesh<N> recontext(std::vector<int> ctx_);
Mesh<N> tile(const std::vector<int> &ctx_);
std::vector<Mesh<N>> each_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) { Mesh<N> Mesh<N>::tile(const std::vector<int> &ctx_) {
size_t cols = 0; return merge(each_tile(ctx_));
for (const auto &mesh : meshes) {
cols += mesh.cols();
}
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;
} }
template<unsigned N> template<unsigned N>
[[nodiscard]] std::vector<Mesh<N>> Mesh<N>::each_tile(const std::vector<int> &ctx_) {
std::vector<Prims<N>> tile( auto base = recontext(ctx_);
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, {}); auto table = solve(*g, base.ctx, {});
const auto path = solve(context, g_gens, sg_gens).path; auto path = solve(*g, base.ctx, ctx).path;
std::vector<int> _gens = generators(context); std::vector<Mesh<N>> res = path.template walk<Mesh<N>, int>(
base, generators(*g),
std::vector<Prims<N>> res = path.walk<Prims<N>, int>( [&](Mesh<N> mesh, int gen) {
base, _gens, apply<N>(table, gen, mesh.prims);
[&](Prims<N> from, int gen) { return mesh;
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<N>::fill(const tc::Group &g, const std::vector<int> &ctx) {
if (ctx.size() + 1 != N)
throw std::logic_error("ctx size must be one less than N");
std::vector<Prims<N>> meshes; // const auto &combos = Combos(ctx, (int)ctx.size() - 1);
const auto &combos = combinations(ctx, (int) 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 &sub_ctx : combos) {
auto base = Mesh<N - 1>::fill(g, sub_ctx);
auto parts = base.each_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); return merge(meshes);
} }
/**
* Single-index primitives should not be further triangulated.
*/
template<> template<>
Prims<1> triangulate<1>( Mesh<1> Mesh<1>::fill(const tc::Group &g, const std::vector<int> &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); return Mesh<1>(g, ctx, 1);
} }
template<unsigned N, class T> template<unsigned N, class C, class SC>
auto hull(const tc::Group &group, T all_sg_gens, const std::vector<std::vector<int>> &exclude) { Mesh<N> fill_each_tile_merge(const tc::Group &g, const C &ctx, const SC &sub_ctxs) {
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) {
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 (const auto &sub_ctx : sub_ctxs) {
const auto &tiles = tile<N>(base, group, g_gens, sg_gens); auto root = Mesh<N>::fill(g, sub_ctx);
for (const auto &tile : tiles) { auto faces = root.each_tile(ctx);
parts.push_back(tile); parts.insert(parts.end(), faces.begin(), faces.end());
}
} }
return parts;
return merge(parts);
}
template<unsigned N, class SC>
Mesh<N> fill_each_tile_merge(const tc::Group &g, const SC &sub_ctxs) {
return fill_each_tile_merge<N>(g, generators(g), sub_ctxs);
}
template<unsigned N, class C, class SC>
Mesh<N> fill_each_recontext_merge(const tc::Group &g, const C &ctx, const SC &sub_ctxs) {
std::vector<Mesh<N>> parts;
for (const auto &sub_ctx : sub_ctxs) {
auto root = Mesh<N>::fill(g, sub_ctx);
auto face = root.recontext(ctx);
parts.insert(parts.end(), face);
}
return merge(parts);
}
template<unsigned N, class SC>
Mesh<N> fill_each_recontext_merge(const tc::Group &g, const SC &sub_ctxs) {
return fill_each_recontext_merge<N>(g, generators(g), sub_ctxs);
} }

View File

@@ -29,13 +29,10 @@ mat5 wander(float time) {
class ExampleApplication : public nanogui::Screen { class ExampleApplication : public nanogui::Screen {
public: public:
vec5 root;
// std::unique_ptr<tc::Group> group;
std::unique_ptr<SliceRenderer<4>> ren; std::unique_ptr<SliceRenderer<4>> ren;
std::unique_ptr<cgl::Buffer<Matrices>> ubo; std::unique_ptr<cgl::Buffer<Matrices>> ubo;
std::unique_ptr<Slice<4>> slice; std::vector<Slice<4>> slices;
float glfw_time = 0; float glfw_time = 0;
float last_frame = 0; float last_frame = 0;
@@ -53,29 +50,36 @@ public:
4, 5) { 4, 5) {
using namespace nanogui; using namespace nanogui;
Window *window = new Window(this, "Sample Window"); // auto *window = new Window(this, "Sample Window");
window->setPosition(Vector2i(15, 15)); // window->setPosition(Vector2i(15, 15));
window->setFixedWidth(250); // window->setFixedWidth(250);
window->setLayout(new BoxLayout(Orientation::Vertical)); // window->setLayout(new BoxLayout(Orientation::Vertical));
auto pause = new ToolButton(window, ENTYPO_ICON_CONTROLLER_PAUS); // auto pause = new ToolButton(window, ENTYPO_ICON_CONTROLLER_PAUS);
pause->setFlags(Button::ToggleButton); // pause->setFlags(Button::ToggleButton);
pause->setChangeCallback([&](bool value) { this->paused = value; }); // pause->setChangeCallback([&](bool value) { this->paused = value; });
performLayout(); performLayout();
std::cout << utilInfo(); std::cout << utilInfo();
std::vector<int> symbol = {3, 4, 3, 2}; {
root << .80, .02, .02, .02, .02; std::vector<int> symbol = {3, 4, 3, 2};
auto group = tc::schlafli(symbol);
auto ctx = generators(group);
auto selected_ctxs = set_difference(
combinations(ctx, 3),
{
{0, 1, 2},
}
);
auto mesh = fill_each_tile_merge<4>(group, selected_ctxs);
auto group = tc::schlafli(symbol); auto &slice = slices.emplace_back(group);
slice.setMesh(mesh);
slice.root << .80, .02, .02, .02, .02;
}
auto gens = generators(group);
auto combos = Combos<int>(gens, 3);
std::vector<std::vector<int>> exclude = {{0, 1, 2}};
slice = std::make_unique<Slice<4>>(group, combos, exclude);
ren = std::make_unique<SliceRenderer<4>>(); ren = std::make_unique<SliceRenderer<4>>();
ubo = std::make_unique<cgl::Buffer<Matrices>>(); ubo = std::make_unique<cgl::Buffer<Matrices>>();
@@ -98,16 +102,21 @@ public:
if (!paused) time += frame_time; if (!paused) time += frame_time;
auto rotation = wander(time); auto rotation = wander(time);
slice->setPoints(root, rotation); for (auto &slice : slices) {
slice.transform = rotation;
slice.setPoints();
}
Matrices mats = Matrices::build(*this); Matrices mats = Matrices::build(*this);
glBindBufferBase(GL_UNIFORM_BUFFER, 1, *ubo); glBindBufferBase(GL_UNIFORM_BUFFER, 1, *ubo);
ubo->put(mats); ubo->put(mats);
ren->draw(*slice); for (const auto &slice : slices) {
ren->draw(slice);
}
} }
}; };
int main(int argc, char ** argv) { int main(int argc, char **argv) {
try { try {
nanogui::init(); nanogui::init();
@@ -126,4 +135,4 @@ int main(int argc, char ** argv) {
} }
return 0; return 0;
} }