diff --git a/libs/cg/include/psemek/cg/body/body.hpp b/libs/cg/include/psemek/cg/body/body.hpp index 2dba99ea..382ef98a 100644 --- a/libs/cg/include/psemek/cg/body/body.hpp +++ b/libs/cg/include/psemek/cg/body/body.hpp @@ -12,7 +12,13 @@ namespace psemek::cg { - /* The basic interface of a 3D body is + /* The basic interface of a 2D body is + * constexpr dimension(body) -> 2 + * vertices(body) -> [point] + * edges(body) -> [(index, index)] + * + * The basic interface of a 3D body is + * constexpr dimension(body) -> 3 * vertices(body) -> [point] * edges(body) -> [(index, index)] * faces(body) -> [[index]] @@ -24,7 +30,11 @@ namespace psemek::cg * However, it is possible that edges(body).size() != edge_directions(body).size, * if there are many collinear edges. * - * Minimal set of required functions: + * Minimal set of required functions for 2D body: + * vertices + * edges + * + * Minimal set of required functions for 3D body: * vertices * edges * faces/triangles diff --git a/libs/cg/include/psemek/cg/body/box.hpp b/libs/cg/include/psemek/cg/body/box.hpp index 200235fb..f15894a4 100644 --- a/libs/cg/include/psemek/cg/body/box.hpp +++ b/libs/cg/include/psemek/cg/body/box.hpp @@ -13,6 +13,56 @@ namespace psemek::cg template box(geom::box) -> box; + template + constexpr std::size_t dimension(box const &) + { + return N; + } + + template + struct box + { + box() = default; + box(geom::box const & b); + + std::array, 4> vertices; + }; + + template + box::box(geom::box const & b) + { + for (std::size_t y = 0; y < 2; ++y) + { + for (std::size_t x = 0; x < 2; ++x) + { + std::size_t i = y * 2 + x; + + vertices[i][0] = (x == 0) ? b[0].min : b[0].max; + vertices[i][1] = (y == 0) ? b[1].min : b[1].max; + } + } + } + + template + auto const & vertices(box const & b) + { + return b.vertices; + } + + template + auto const & edges(box const &) + { + static const std::array, 12> result = + {{ + { 0b00, 0b01 }, + { 0b01, 0b11 }, + { 0b11, 0b10 }, + { 0b10, 0b00 }, + }}; + + return result; + } + template struct box { diff --git a/libs/cg/include/psemek/cg/body/frustum.hpp b/libs/cg/include/psemek/cg/body/frustum.hpp index e503c75a..f6e36a16 100644 --- a/libs/cg/include/psemek/cg/body/frustum.hpp +++ b/libs/cg/include/psemek/cg/body/frustum.hpp @@ -24,6 +24,12 @@ namespace psemek::cg std::array, 8> vertices; }; + template + constexpr std::size_t dimension(frustum const &) + { + return N; + } + template frustum::frustum(geom::matrix const & m) { diff --git a/libs/cg/include/psemek/cg/body/icosahedron.hpp b/libs/cg/include/psemek/cg/body/icosahedron.hpp index 00b7d2e1..1f40d67d 100644 --- a/libs/cg/include/psemek/cg/body/icosahedron.hpp +++ b/libs/cg/include/psemek/cg/body/icosahedron.hpp @@ -14,6 +14,12 @@ namespace psemek::cg std::array, 12> vertices; }; + template + constexpr std::size_t dimension(icosahedron const &) + { + return 3; + } + template icosahedron::icosahedron(geom::point const & origin, T radius) { diff --git a/libs/cg/include/psemek/cg/body/prism.hpp b/libs/cg/include/psemek/cg/body/prism.hpp index 0c317aa0..cb982131 100644 --- a/libs/cg/include/psemek/cg/body/prism.hpp +++ b/libs/cg/include/psemek/cg/body/prism.hpp @@ -15,6 +15,12 @@ namespace psemek::cg std::array, 4> edge_directions; }; + template + constexpr std::size_t dimension(triangular_prism const &) + { + return 3; + } + template triangular_prism::triangular_prism(geom::triangle> const & t, geom::vector const & d) {