From 5bd2a40a226dfa07e163a668cad7c7827d2da568 Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Tue, 26 May 2020 12:51:03 -0400 Subject: [PATCH 01/17] support for polyhedron --- src/csg/levelset_3d.cpp | 7 +++++++ src/csg/meson.build | 1 + src/csg/parser.cpp | 29 +++++++++++++++++++++++++++-- src/csg/polyhedron.cpp | 5 +++++ src/csg_polyhedron.hpp | 12 ++++++++++++ src/csg_types.hpp | 14 ++++++++++---- 6 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 src/csg/polyhedron.cpp create mode 100644 src/csg_polyhedron.hpp diff --git a/src/csg/levelset_3d.cpp b/src/csg/levelset_3d.cpp index 1eee5aa..83209da 100644 --- a/src/csg/levelset_3d.cpp +++ b/src/csg/levelset_3d.cpp @@ -27,6 +27,7 @@ double signed_distance_3d(const Sphere &, double, double, double); double signed_distance_3d(const Type3D &, double, double, double); double signed_distance_3d(const LinearExtrude &, double, double, double); double signed_distance_3d(const RotateExtrude &, double, double, double); +double signed_distance_3d(const Polyhedron &, double, double, double); double signed_distance_2d(const Union2D &, double, double); @@ -149,6 +150,12 @@ double signed_distance_3d(const RotateExtrude &rot_ext, double xx, double yy, return signed_distance_2d(rot_ext.group, XX, YY); } +double signed_distance_3d(const Polyhedron &polyhedron, double xx, double yy, + double zz) { + // TODO: Implement + return 0.0; +} + double signed_distance_3d(const Type3D &obj, double xx, double yy, double zz) { return std::visit( diff --git a/src/csg/meson.build b/src/csg/meson.build index c9f7140..22a3aee 100644 --- a/src/csg/meson.build +++ b/src/csg/meson.build @@ -5,6 +5,7 @@ lib_csg_parser = static_library( 'levelset_2d.cpp', 'parser.cpp', 'matrix_functions.cpp', + 'polyhedron.cpp', include_directories: parser_inc, dependencies: [pegtl, cgal], install : true) diff --git a/src/csg/parser.cpp b/src/csg/parser.cpp index 3e1429b..7f91ef2 100644 --- a/src/csg/parser.cpp +++ b/src/csg/parser.cpp @@ -8,12 +8,15 @@ // includes #include "csg_types.hpp" +#include "csg_polyhedron.hpp" using namespace tao::pegtl; namespace { -using Attr = std::variant, std::string>; +using Attr = std::variant, std::string, + std::vector>, + std::vector>>; using AttrMap = std::map; struct parser_state { @@ -102,6 +105,10 @@ struct cube : seq, L_FUN, attr_list, R_FUN> {}; struct cylinder : seq, L_FUN, attr_list, R_FUN> {}; +struct polyhedron + : seq, L_FUN, + attr_list, R_FUN> {}; + struct circle : seq, L_FUN, attr_list, R_FUN> {}; @@ -111,7 +118,8 @@ struct square template struct shape; template <> -struct shape : seq, opt> {}; +struct shape + : seq, opt> {}; template <> struct shape : seq, opt> {}; @@ -562,6 +570,23 @@ template <> struct action { } }; +template <> struct action { + template + static void apply(const Input &in, parser_state &st) { + std::stringstream ss(in.string()); + csg::Polyhedron p; + auto &curr_attr = st.curr_attrs.back(); + p.points = std::get>>( + curr_attr["points"]); + p.faces = + std::get>>(curr_attr["edges"]); + + Polyhedron::create(); + st.current_3d_objs.back().push_back(p); + st.curr_attrs.pop_back(); + } +}; + template <> struct action { template static void apply(const Input &in, parser_state &st) { diff --git a/src/csg/polyhedron.cpp b/src/csg/polyhedron.cpp new file mode 100644 index 0000000..f235c54 --- /dev/null +++ b/src/csg/polyhedron.cpp @@ -0,0 +1,5 @@ +#include "csg_matrix_functions.hpp" + +namespace Polyhedron { + void create() {}; +} diff --git a/src/csg_polyhedron.hpp b/src/csg_polyhedron.hpp new file mode 100644 index 0000000..185e906 --- /dev/null +++ b/src/csg_polyhedron.hpp @@ -0,0 +1,12 @@ +#ifndef POLYHEDRON_H_ +#define POLYHEDRON_H_ + +#include +#include +#include + +namespace Polyhedron { +void create(); +} + +#endif diff --git a/src/csg_types.hpp b/src/csg_types.hpp index 5077100..2b2863a 100644 --- a/src/csg_types.hpp +++ b/src/csg_types.hpp @@ -49,6 +49,12 @@ struct Cone { bool center; }; +struct Polyhedron { + std::optional name; + std::vector> points; + std::vector> faces; +}; + enum Dimension { D2, D3 }; template struct Mulmatrix; @@ -68,10 +74,10 @@ template <> struct TypeHelper { }; template <> struct TypeHelper { - using Type = - std::variant, - Intersection, Difference, - Mulmatrix, LinearExtrude, RotateExtrude>; + using Type = std::variant, + Intersection, + Difference, Mulmatrix, + LinearExtrude, RotateExtrude, Polyhedron>; }; template struct Union { -- GitLab From 99e23ad412cab4d764bb7d5bb9832e1d3a19d775 Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Tue, 26 May 2020 14:21:24 -0400 Subject: [PATCH 02/17] more work --- src/csg/cgal_helper.cpp | 54 +++++++++++++++++++++++++++++++++++++++++ src/csg/meson.build | 2 +- src/csg/parser.cpp | 4 +-- src/csg/polyhedron.cpp | 5 ---- src/csg_cgal_helper.hpp | 20 +++++++++++++++ src/csg_polyhedron.hpp | 12 --------- 6 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 src/csg/cgal_helper.cpp delete mode 100644 src/csg/polyhedron.cpp create mode 100644 src/csg_cgal_helper.hpp delete mode 100644 src/csg_polyhedron.hpp diff --git a/src/csg/cgal_helper.cpp b/src/csg/cgal_helper.cpp new file mode 100644 index 0000000..5bcd0c4 --- /dev/null +++ b/src/csg/cgal_helper.cpp @@ -0,0 +1,54 @@ +#include "csg_cgal_helper.hpp" + +namespace cgal_helper { + +namespace { + +typedef typename HalfedgeDS::Vertex Vertex; +typedef typename Vertex::Point Point; + +// A modifier creating a polyhedron with the incremental builder. +template class Build_polyhedron : public CGAL::Modifier_base { +private: + const std::vector> &d_points; + const std::vector> &d_faces; + +public: + Build_polyhedron( + const std::vector> &points, + const std::vector> &faces) + : d_points(points), d_faces(faces) {} + + void operator()(HDS &hds) { + for (const auto &face : d_faces) { + CGAL::Polyhedron_incremental_builder_3 B(hds, true); + B.begin_surface(face.size(), 1); + B.begin_facet(); + + int f = 0; + for (const auto &point : face) { + const auto &p = d_points[point]; + auto [px, py, pz] = p; + B.add_vertex(Point(px, py, pz)); + B.add_vertex_to_facet(f); + ++f; + } + B.end_facet(); + B.end_surface(); + } + } +}; + +} // namespace + +Polyhedron +create_polyhedron(const std::vector> &points, + const std::vector> &faces) { + Polyhedron p; + Build_polyhedron bp(points, faces); + p.delegate(bp); + CGAL_assertion(p.is_valid()); + return p; +} + +} // namespace cgal_helper diff --git a/src/csg/meson.build b/src/csg/meson.build index 22a3aee..dafffc1 100644 --- a/src/csg/meson.build +++ b/src/csg/meson.build @@ -5,7 +5,7 @@ lib_csg_parser = static_library( 'levelset_2d.cpp', 'parser.cpp', 'matrix_functions.cpp', - 'polyhedron.cpp', + 'cgal_helper.cpp', include_directories: parser_inc, dependencies: [pegtl, cgal], install : true) diff --git a/src/csg/parser.cpp b/src/csg/parser.cpp index 7f91ef2..5a9c22f 100644 --- a/src/csg/parser.cpp +++ b/src/csg/parser.cpp @@ -7,8 +7,8 @@ #include // includes +#include "csg_cgal_helper.hpp" #include "csg_types.hpp" -#include "csg_polyhedron.hpp" using namespace tao::pegtl; @@ -581,7 +581,7 @@ template <> struct action { p.faces = std::get>>(curr_attr["edges"]); - Polyhedron::create(); + cgal_helper::create_polyhedron(p.points, p.faces); st.current_3d_objs.back().push_back(p); st.curr_attrs.pop_back(); } diff --git a/src/csg/polyhedron.cpp b/src/csg/polyhedron.cpp deleted file mode 100644 index f235c54..0000000 --- a/src/csg/polyhedron.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "csg_matrix_functions.hpp" - -namespace Polyhedron { - void create() {}; -} diff --git a/src/csg_cgal_helper.hpp b/src/csg_cgal_helper.hpp new file mode 100644 index 0000000..a665ccb --- /dev/null +++ b/src/csg_cgal_helper.hpp @@ -0,0 +1,20 @@ +#ifndef POLYHEDRON_H_ +#define POLYHEDRON_H_ + +#include +#include +#include + +namespace cgal_helper { + +typedef CGAL::Simple_cartesian Kernel; +typedef CGAL::Polyhedron_3 Polyhedron; +typedef Polyhedron::HalfedgeDS HalfedgeDS; + +Polyhedron +create_polyhedron(const std::vector> &points, + const std::vector> &faces); + +} // namespace cgal_helper + +#endif diff --git a/src/csg_polyhedron.hpp b/src/csg_polyhedron.hpp deleted file mode 100644 index 185e906..0000000 --- a/src/csg_polyhedron.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef POLYHEDRON_H_ -#define POLYHEDRON_H_ - -#include -#include -#include - -namespace Polyhedron { -void create(); -} - -#endif -- GitLab From 5f3b21c1ca9e6e10c044a89e448f4c249757f6bd Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Tue, 26 May 2020 17:26:48 -0400 Subject: [PATCH 03/17] more work --- src/csg/cgal_helper.cpp | 1 + src/csg/parser.cpp | 49 +++++++++++++++++++-------- src/csg/tests/parser/primitives.t.cpp | 11 +++++- src/csg_cgal_helper.hpp | 1 - src/csg_types.hpp | 2 ++ 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/csg/cgal_helper.cpp b/src/csg/cgal_helper.cpp index 5bcd0c4..d455d16 100644 --- a/src/csg/cgal_helper.cpp +++ b/src/csg/cgal_helper.cpp @@ -4,6 +4,7 @@ namespace cgal_helper { namespace { +typedef Polyhedron::HalfedgeDS HalfedgeDS; typedef typename HalfedgeDS::Vertex Vertex; typedef typename Vertex::Point Point; diff --git a/src/csg/parser.cpp b/src/csg/parser.cpp index 5a9c22f..f825b72 100644 --- a/src/csg/parser.cpp +++ b/src/csg/parser.cpp @@ -15,8 +15,7 @@ using namespace tao::pegtl; namespace { using Attr = std::variant, std::string, - std::vector>, - std::vector>>; + std::vector>>; using AttrMap = std::map; struct parser_state { @@ -86,16 +85,18 @@ struct vector : seq>, R_ARR> {}; struct vector_attr : vector {}; -struct value - : sor {}; +struct row : vector {}; -struct keyval : seq, string<'='>, pad> {}; +struct matrix : seq>, R_ARR> {}; -struct attr_list : list> {}; +struct matrix_attr : matrix {}; -struct row : vector {}; +struct value : sor {}; -struct matrix : seq>, R_ARR> {}; +struct keyval : seq, string<'='>, pad> {}; + +struct attr_list : list> {}; struct sphere : seq, L_FUN, attr_list, R_FUN> {}; @@ -241,6 +242,7 @@ template <> struct action { template <> struct action { template static void apply(const Input &in, parser_state &st) { + std::cout << "parsing matrix" << std::endl; std::stringstream ss(in.string()); st.current_matrices.push_back(st.current_matrix); st.current_matrix.clear(); @@ -574,15 +576,23 @@ template <> struct action { template static void apply(const Input &in, parser_state &st) { std::stringstream ss(in.string()); - csg::Polyhedron p; + csg::Polyhedron polyh; auto &curr_attr = st.curr_attrs.back(); - p.points = std::get>>( - curr_attr["points"]); - p.faces = - std::get>>(curr_attr["edges"]); + auto points = + std::get>>(curr_attr["points"]); + auto faces = std::get>>(curr_attr["faces"]); + + for (const auto &p : points) { + polyh.points.push_back({p[0], p[1], p[2]}); + } + + for (const auto &f : faces) { + polyh.faces.push_back(std::vector(f.begin(), f.end())); + } - cgal_helper::create_polyhedron(p.points, p.faces); - st.current_3d_objs.back().push_back(p); + polyh.cgal_polyhedron = + cgal_helper::create_polyhedron(polyh.points, polyh.faces); + st.current_3d_objs.back().push_back(polyh); st.curr_attrs.pop_back(); } }; @@ -640,6 +650,15 @@ template <> struct action { } }; +template <> struct action { + template + static void apply(const Input &in, parser_state &st) { + std::stringstream ss(in.string()); + st.curr_attr[st.current_name] = st.current_matrix; + st.current_matrix.clear(); + } +}; + std::optional do_parse(std::string str) { parser_state st; std::vector new_3d_group; diff --git a/src/csg/tests/parser/primitives.t.cpp b/src/csg/tests/parser/primitives.t.cpp index ca2175c..e2a0e2c 100644 --- a/src/csg/tests/parser/primitives.t.cpp +++ b/src/csg/tests/parser/primitives.t.cpp @@ -54,4 +54,13 @@ sphere(r = 10) CHECK(sph.radius == 10); } -// TODO: polyhedron() +TEST_CASE("polyhedron", "[csg]") { + auto st = *csg::parse_csg(R"( +polyhedron( +points = [[10, 10, 0], [10, -10, 0], [-10, -10, 0], [-10, 10, 0], [0, 0, 10]], +faces = [[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4], [1, 0, 3], [2, 1, 3]], +$name="my_polyhedron"); +)"); + // auto polyh = std::get(st.top.objs.at(0)); + // CHECK(polyh.name == "my_polyhedron"); +} diff --git a/src/csg_cgal_helper.hpp b/src/csg_cgal_helper.hpp index a665ccb..5f58649 100644 --- a/src/csg_cgal_helper.hpp +++ b/src/csg_cgal_helper.hpp @@ -9,7 +9,6 @@ namespace cgal_helper { typedef CGAL::Simple_cartesian Kernel; typedef CGAL::Polyhedron_3 Polyhedron; -typedef Polyhedron::HalfedgeDS HalfedgeDS; Polyhedron create_polyhedron(const std::vector> &points, diff --git a/src/csg_types.hpp b/src/csg_types.hpp index 2b2863a..1af4c37 100644 --- a/src/csg_types.hpp +++ b/src/csg_types.hpp @@ -1,6 +1,7 @@ #ifndef CSG_TYPES_H_ #define CSG_TYPES_H_ +#include "csg_cgal_helper.hpp" #include "csg_matrix_functions.hpp" #include @@ -53,6 +54,7 @@ struct Polyhedron { std::optional name; std::vector> points; std::vector> faces; + cgal_helper::Polyhedron cgal_polyhedron; }; enum Dimension { D2, D3 }; -- GitLab From bcdc78bc88d79a50095b68d61077535418c33f81 Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Tue, 26 May 2020 17:28:10 -0400 Subject: [PATCH 04/17] more work --- src/csg/parser.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/csg/parser.cpp b/src/csg/parser.cpp index f825b72..737e312 100644 --- a/src/csg/parser.cpp +++ b/src/csg/parser.cpp @@ -242,7 +242,6 @@ template <> struct action { template <> struct action { template static void apply(const Input &in, parser_state &st) { - std::cout << "parsing matrix" << std::endl; std::stringstream ss(in.string()); st.current_matrices.push_back(st.current_matrix); st.current_matrix.clear(); -- GitLab From 708b6a66f1120ab8b3ef79e53b5067f3ce984234 Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Tue, 26 May 2020 18:03:43 -0400 Subject: [PATCH 05/17] more work --- src/csg/cgal_helper.cpp | 12 ++++++------ src/csg/parser.cpp | 3 +++ src/csg/tests/parser/primitives.t.cpp | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/csg/cgal_helper.cpp b/src/csg/cgal_helper.cpp index d455d16..35cb650 100644 --- a/src/csg/cgal_helper.cpp +++ b/src/csg/cgal_helper.cpp @@ -23,16 +23,16 @@ public: void operator()(HDS &hds) { for (const auto &face : d_faces) { CGAL::Polyhedron_incremental_builder_3 B(hds, true); - B.begin_surface(face.size(), 1); - B.begin_facet(); - - int f = 0; + B.begin_surface(face.size(), 1, 2 * face.size()); for (const auto &point : face) { const auto &p = d_points[point]; auto [px, py, pz] = p; B.add_vertex(Point(px, py, pz)); - B.add_vertex_to_facet(f); - ++f; + } + + B.begin_facet(); + for (size_t i = 0; i < face.size(); ++i) { + B.add_vertex_to_facet(i); } B.end_facet(); B.end_surface(); diff --git a/src/csg/parser.cpp b/src/csg/parser.cpp index 737e312..4e0e345 100644 --- a/src/csg/parser.cpp +++ b/src/csg/parser.cpp @@ -576,7 +576,10 @@ template <> struct action { static void apply(const Input &in, parser_state &st) { std::stringstream ss(in.string()); csg::Polyhedron polyh; + auto &curr_attr = st.curr_attrs.back(); + polyh.name = get_name(curr_attr); + auto points = std::get>>(curr_attr["points"]); auto faces = std::get>>(curr_attr["faces"]); diff --git a/src/csg/tests/parser/primitives.t.cpp b/src/csg/tests/parser/primitives.t.cpp index e2a0e2c..225e6bb 100644 --- a/src/csg/tests/parser/primitives.t.cpp +++ b/src/csg/tests/parser/primitives.t.cpp @@ -61,6 +61,6 @@ points = [[10, 10, 0], [10, -10, 0], [-10, -10, 0], [-10, 10, 0], [0, 0, 10]], faces = [[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4], [1, 0, 3], [2, 1, 3]], $name="my_polyhedron"); )"); - // auto polyh = std::get(st.top.objs.at(0)); - // CHECK(polyh.name == "my_polyhedron"); + auto polyh = std::get(st.top.objs.at(0)); + CHECK(polyh.name == "my_polyhedron"); } -- GitLab From 05f7572e5b03df09c2f280c7fe263da988211c7a Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Tue, 26 May 2020 21:55:57 -0400 Subject: [PATCH 06/17] more work --- src/csg/cgal_helper.cpp | 27 +++++++++++++++---------- src/csg/tests/levelset/primitives.t.cpp | 26 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/csg/cgal_helper.cpp b/src/csg/cgal_helper.cpp index 35cb650..044b324 100644 --- a/src/csg/cgal_helper.cpp +++ b/src/csg/cgal_helper.cpp @@ -1,5 +1,7 @@ #include "csg_cgal_helper.hpp" +#include + namespace cgal_helper { namespace { @@ -21,22 +23,25 @@ public: : d_points(points), d_faces(faces) {} void operator()(HDS &hds) { - for (const auto &face : d_faces) { - CGAL::Polyhedron_incremental_builder_3 B(hds, true); - B.begin_surface(face.size(), 1, 2 * face.size()); - for (const auto &point : face) { - const auto &p = d_points[point]; - auto [px, py, pz] = p; - B.add_vertex(Point(px, py, pz)); - } + CGAL::Polyhedron_incremental_builder_3 B(hds, true); + B.begin_surface(d_points.size(), d_faces.size()); + // Add all the vertices first + for (const auto &p : d_points) { + auto [px, py, pz] = p; + B.add_vertex(Point(px, py, pz)); + } + + // Add facets next + for (const auto &face : d_faces) { B.begin_facet(); - for (size_t i = 0; i < face.size(); ++i) { - B.add_vertex_to_facet(i); + for (const auto &p_index : face) { + B.add_vertex_to_facet(p_index); } B.end_facet(); - B.end_surface(); } + + B.end_surface(); } }; diff --git a/src/csg/tests/levelset/primitives.t.cpp b/src/csg/tests/levelset/primitives.t.cpp index b2b3b01..bb84b5b 100644 --- a/src/csg/tests/levelset/primitives.t.cpp +++ b/src/csg/tests/levelset/primitives.t.cpp @@ -545,4 +545,30 @@ TEST_CASE("Cone", "[Levelset Primitives]") { } } +TEST_CASE("Polyhedron", "[Levelset Primitives]") { + + csg::Polyhedron my_cub{.name = std::nullopt, + .points = {{0, 0, 0}, + {10, 0, 0}, + {10, 7, 0}, + {0, 7, 0}, + {0, 0, 5}, + {10, 0, 5}, + {10, 7, 5}, + {0, 7, 5}}, + .faces = {{0, 1, 2, 3}, + {4, 5, 1, 0}, + {7, 6, 5, 4}, + {5, 6, 2, 1}, + {6, 7, 3, 2}, + {7, 4, 0, 3}}}; + + auto my_tree = std::make_shared(); + my_tree->top.objs.push_back(my_cub); + csg::CsgIF my_levelset(my_tree); + + SECTION("Outside") { CHECK_FALSE(0 < my_levelset(10.1, 1, 1)); } + SECTION("Inside") { CHECK(0 < my_levelset(5, 5, 2.5)); } +} + } // namespace -- GitLab From c62e3d76b95cc66fc1e89a8496363ca198a3a1df Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Tue, 26 May 2020 23:24:15 -0400 Subject: [PATCH 07/17] more work --- src/csg/cgal_helper.cpp | 25 ++++++++++++++++++++- src/csg/levelset_3d.cpp | 6 +++-- src/csg/parser.cpp | 15 ++----------- src/csg/tests/levelset/primitives.t.cpp | 29 ++++++++++++------------- src/csg_cgal_helper.hpp | 8 ++++--- src/csg_types.hpp | 23 +++++++++++++++++--- 6 files changed, 69 insertions(+), 37 deletions(-) diff --git a/src/csg/cgal_helper.cpp b/src/csg/cgal_helper.cpp index 044b324..c3b07b4 100644 --- a/src/csg/cgal_helper.cpp +++ b/src/csg/cgal_helper.cpp @@ -1,6 +1,12 @@ #include "csg_cgal_helper.hpp" -#include +#include +#include +#include +#include +#include +#include +#include namespace cgal_helper { @@ -9,6 +15,10 @@ namespace { typedef Polyhedron::HalfedgeDS HalfedgeDS; typedef typename HalfedgeDS::Vertex Vertex; typedef typename Vertex::Point Point; +typedef CGAL::AABB_face_graph_triangle_primitive Primitive; +typedef CGAL::AABB_traits Traits; +typedef CGAL::AABB_tree Tree; +typedef CGAL::Side_of_triangle_mesh Point_inside; // A modifier creating a polyhedron with the incremental builder. template class Build_polyhedron : public CGAL::Modifier_base { @@ -57,4 +67,17 @@ create_polyhedron(const std::vector> &points, return p; } +bool inside_polyhedron(const Polyhedron &polyhedron, double xx, double yy, + double zz) { + Kernel::Point_3 p(xx, yy, zz); + // Construct AABB tree with a KdTree + Tree tree(faces(polyhedron).first, faces(polyhedron).second, polyhedron); + tree.accelerate_distance_queries(); + // Initialize the point-in-polyhedron tester + Point_inside inside_tester(tree); + + // Determine the side and return true if inside! + return inside_tester(p) == CGAL::ON_BOUNDED_SIDE; +} + } // namespace cgal_helper diff --git a/src/csg/levelset_3d.cpp b/src/csg/levelset_3d.cpp index 83209da..da38da2 100644 --- a/src/csg/levelset_3d.cpp +++ b/src/csg/levelset_3d.cpp @@ -1,3 +1,4 @@ +#include "csg_cgal_helper.hpp" #include "csg_types.hpp" #include @@ -152,8 +153,9 @@ double signed_distance_3d(const RotateExtrude &rot_ext, double xx, double yy, double signed_distance_3d(const Polyhedron &polyhedron, double xx, double yy, double zz) { - // TODO: Implement - return 0.0; + // TODO: support signed distance instead of -1.0/1.0 + return cgal_helper::inside_polyhedron(polyhedron.cgal_polyhedron(), xx, yy, + zz); } double signed_distance_3d(const Type3D &obj, double xx, double yy, double zz) { diff --git a/src/csg/parser.cpp b/src/csg/parser.cpp index 4e0e345..ec6209a 100644 --- a/src/csg/parser.cpp +++ b/src/csg/parser.cpp @@ -7,7 +7,6 @@ #include // includes -#include "csg_cgal_helper.hpp" #include "csg_types.hpp" using namespace tao::pegtl; @@ -575,25 +574,15 @@ template <> struct action { template static void apply(const Input &in, parser_state &st) { std::stringstream ss(in.string()); - csg::Polyhedron polyh; - auto &curr_attr = st.curr_attrs.back(); - polyh.name = get_name(curr_attr); auto points = std::get>>(curr_attr["points"]); auto faces = std::get>>(curr_attr["faces"]); - for (const auto &p : points) { - polyh.points.push_back({p[0], p[1], p[2]}); - } - - for (const auto &f : faces) { - polyh.faces.push_back(std::vector(f.begin(), f.end())); - } + csg::Polyhedron polyh(points, faces); + polyh.name = get_name(curr_attr); - polyh.cgal_polyhedron = - cgal_helper::create_polyhedron(polyh.points, polyh.faces); st.current_3d_objs.back().push_back(polyh); st.curr_attrs.pop_back(); } diff --git a/src/csg/tests/levelset/primitives.t.cpp b/src/csg/tests/levelset/primitives.t.cpp index bb84b5b..8e1fd0f 100644 --- a/src/csg/tests/levelset/primitives.t.cpp +++ b/src/csg/tests/levelset/primitives.t.cpp @@ -547,21 +547,20 @@ TEST_CASE("Cone", "[Levelset Primitives]") { TEST_CASE("Polyhedron", "[Levelset Primitives]") { - csg::Polyhedron my_cub{.name = std::nullopt, - .points = {{0, 0, 0}, - {10, 0, 0}, - {10, 7, 0}, - {0, 7, 0}, - {0, 0, 5}, - {10, 0, 5}, - {10, 7, 5}, - {0, 7, 5}}, - .faces = {{0, 1, 2, 3}, - {4, 5, 1, 0}, - {7, 6, 5, 4}, - {5, 6, 2, 1}, - {6, 7, 3, 2}, - {7, 4, 0, 3}}}; + csg::Polyhedron my_cub({{0, 0, 0}, + {10, 0, 0}, + {10, 7, 0}, + {0, 7, 0}, + {0, 0, 5}, + {10, 0, 5}, + {10, 7, 5}, + {0, 7, 5}}, + {{0, 1, 2, 3}, + {4, 5, 1, 0}, + {7, 6, 5, 4}, + {5, 6, 2, 1}, + {6, 7, 3, 2}, + {7, 4, 0, 3}}); auto my_tree = std::make_shared(); my_tree->top.objs.push_back(my_cub); diff --git a/src/csg_cgal_helper.hpp b/src/csg_cgal_helper.hpp index 5f58649..7973aab 100644 --- a/src/csg_cgal_helper.hpp +++ b/src/csg_cgal_helper.hpp @@ -1,8 +1,7 @@ -#ifndef POLYHEDRON_H_ -#define POLYHEDRON_H_ +#ifndef CGAL_HELPER_H_ +#define CGAL_HELPER_H_ #include -#include #include namespace cgal_helper { @@ -14,6 +13,9 @@ Polyhedron create_polyhedron(const std::vector> &points, const std::vector> &faces); +bool inside_polyhedron(const Polyhedron &polyhedron, double xx, double yy, + double zz); + } // namespace cgal_helper #endif diff --git a/src/csg_types.hpp b/src/csg_types.hpp index 1af4c37..f7f698c 100644 --- a/src/csg_types.hpp +++ b/src/csg_types.hpp @@ -51,10 +51,27 @@ struct Cone { }; struct Polyhedron { +private: + std::vector> d_points; + std::vector> d_faces; + cgal_helper::Polyhedron d_cgal_polyhedron; + +public: std::optional name; - std::vector> points; - std::vector> faces; - cgal_helper::Polyhedron cgal_polyhedron; + Polyhedron(const std::vector> &points, + const std::vector> &faces) { + for (const auto &p : points) { + d_points.push_back({p[0], p[1], p[2]}); + } + + for (const auto &f : faces) { + d_faces.push_back(std::vector(f.begin(), f.end())); + } + d_cgal_polyhedron = cgal_helper::create_polyhedron(d_points, d_faces); + } + const cgal_helper::Polyhedron &cgal_polyhedron() const { + return d_cgal_polyhedron; + } }; enum Dimension { D2, D3 }; -- GitLab From 471a868cc9c8f0a4d45779c8a5b1fe09ad81dde7 Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Wed, 27 May 2020 08:40:56 -0400 Subject: [PATCH 08/17] more work --- src/csg/cgal_helper.cpp | 1 + src/csg_types.hpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/csg/cgal_helper.cpp b/src/csg/cgal_helper.cpp index c3b07b4..c28b467 100644 --- a/src/csg/cgal_helper.cpp +++ b/src/csg/cgal_helper.cpp @@ -69,6 +69,7 @@ create_polyhedron(const std::vector> &points, bool inside_polyhedron(const Polyhedron &polyhedron, double xx, double yy, double zz) { + std::cout << "#halfedges = " << polyhedron.size_of_halfedges() << std::endl; Kernel::Point_3 p(xx, yy, zz); // Construct AABB tree with a KdTree Tree tree(faces(polyhedron).first, faces(polyhedron).second, polyhedron); diff --git a/src/csg_types.hpp b/src/csg_types.hpp index f7f698c..b630ca9 100644 --- a/src/csg_types.hpp +++ b/src/csg_types.hpp @@ -63,12 +63,12 @@ public: for (const auto &p : points) { d_points.push_back({p[0], p[1], p[2]}); } - for (const auto &f : faces) { d_faces.push_back(std::vector(f.begin(), f.end())); } d_cgal_polyhedron = cgal_helper::create_polyhedron(d_points, d_faces); } + const cgal_helper::Polyhedron &cgal_polyhedron() const { return d_cgal_polyhedron; } -- GitLab From f8d3e9178365754003c310c53217b77ee1f532fb Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Wed, 27 May 2020 09:04:17 -0400 Subject: [PATCH 09/17] more work --- src/csg/cgal_helper.cpp | 8 ++++++ src/csg/levelset_3d.cpp | 4 ++- src/csg/tests/levelset/primitives.t.cpp | 35 ++++++++++++++++++------- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/csg/cgal_helper.cpp b/src/csg/cgal_helper.cpp index c28b467..5006212 100644 --- a/src/csg/cgal_helper.cpp +++ b/src/csg/cgal_helper.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -61,9 +62,16 @@ Polyhedron create_polyhedron(const std::vector> &points, const std::vector> &faces) { Polyhedron p; + + // Build incrementally Build_polyhedron bp(points, faces); p.delegate(bp); CGAL_assertion(p.is_valid()); + + // Triangulate faces + CGAL::Polygon_mesh_processing::triangulate_faces(p); + CGAL_assertion(p.is_valid()); + return p; } diff --git a/src/csg/levelset_3d.cpp b/src/csg/levelset_3d.cpp index da38da2..df377a2 100644 --- a/src/csg/levelset_3d.cpp +++ b/src/csg/levelset_3d.cpp @@ -155,7 +155,9 @@ double signed_distance_3d(const Polyhedron &polyhedron, double xx, double yy, double zz) { // TODO: support signed distance instead of -1.0/1.0 return cgal_helper::inside_polyhedron(polyhedron.cgal_polyhedron(), xx, yy, - zz); + zz) + ? 1.0 + : -1.0; } double signed_distance_3d(const Type3D &obj, double xx, double yy, double zz) { diff --git a/src/csg/tests/levelset/primitives.t.cpp b/src/csg/tests/levelset/primitives.t.cpp index 8e1fd0f..b950f82 100644 --- a/src/csg/tests/levelset/primitives.t.cpp +++ b/src/csg/tests/levelset/primitives.t.cpp @@ -546,15 +546,16 @@ TEST_CASE("Cone", "[Levelset Primitives]") { } TEST_CASE("Polyhedron", "[Levelset Primitives]") { - + double Lx = 10.0, Ly = 7.0, Lz = 5.0; + // A 10 x 7 x 5 cuboid in positive quandrant formed using polyhedron csg::Polyhedron my_cub({{0, 0, 0}, - {10, 0, 0}, - {10, 7, 0}, - {0, 7, 0}, - {0, 0, 5}, - {10, 0, 5}, - {10, 7, 5}, - {0, 7, 5}}, + {Lx, 0, 0}, + {Lx, Ly, 0}, + {0, Ly, 0}, + {0, 0, Lz}, + {Lx, 0, Lz}, + {Lx, Ly, Lz}, + {0, Ly, Lz}}, {{0, 1, 2, 3}, {4, 5, 1, 0}, {7, 6, 5, 4}, @@ -566,8 +567,22 @@ TEST_CASE("Polyhedron", "[Levelset Primitives]") { my_tree->top.objs.push_back(my_cub); csg::CsgIF my_levelset(my_tree); - SECTION("Outside") { CHECK_FALSE(0 < my_levelset(10.1, 1, 1)); } - SECTION("Inside") { CHECK(0 < my_levelset(5, 5, 2.5)); } + SECTION("Outside") { + CHECK_FALSE(0 < my_levelset(1.01 * Lx, 0.1 * Ly, 0.1 * Lz)); + CHECK_FALSE(0 < my_levelset(-0.01 * Lx, 0.5 * Ly, 0.5 * Lz)); + CHECK_FALSE(0 < my_levelset(0.3 * Lx, 1.01 * Ly, 0.2 * Lz)); + CHECK_FALSE(0 < my_levelset(0.7 * Lx, -0.01 * Ly, 0.7 * Lz)); + CHECK_FALSE(0 < my_levelset(0.3 * Lx, 0.9 * Ly, 1.01 * Lz)); + CHECK_FALSE(0 < my_levelset(0.7 * Lx, 0.1 * Ly, -0.01 * Lz)); + } + SECTION("Inside") { + CHECK(0 < my_levelset(0.99 * Lx, 0.1 * Ly, 0.1 * Lz)); + CHECK(0 < my_levelset(0.01 * Lx, 0.5 * Ly, 0.5 * Lz)); + CHECK(0 < my_levelset(0.3 * Lx, 0.99 * Ly, 0.2 * Lz)); + CHECK(0 < my_levelset(0.7 * Lx, 0.01 * Ly, 0.7 * Lz)); + CHECK(0 < my_levelset(0.3 * Lx, 0.9 * Ly, 0.99 * Lz)); + CHECK(0 < my_levelset(0.7 * Lx, 0.1 * Ly, 0.01 * Lz)); + } } } // namespace -- GitLab From 2a2383324fde0ab97e6c2bb219339d4c376c98ea Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Wed, 27 May 2020 09:06:21 -0400 Subject: [PATCH 10/17] more work --- src/csg/cgal_helper.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/csg/cgal_helper.cpp b/src/csg/cgal_helper.cpp index 5006212..aa31c3c 100644 --- a/src/csg/cgal_helper.cpp +++ b/src/csg/cgal_helper.cpp @@ -77,7 +77,6 @@ create_polyhedron(const std::vector> &points, bool inside_polyhedron(const Polyhedron &polyhedron, double xx, double yy, double zz) { - std::cout << "#halfedges = " << polyhedron.size_of_halfedges() << std::endl; Kernel::Point_3 p(xx, yy, zz); // Construct AABB tree with a KdTree Tree tree(faces(polyhedron).first, faces(polyhedron).second, polyhedron); -- GitLab From 7cc5871b206fa7e09e8a0e3532e63dffe5c9c4e9 Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Wed, 27 May 2020 09:09:09 -0400 Subject: [PATCH 11/17] more work --- src/csg/cgal_helper.cpp | 5 ++--- src/csg/levelset_3d.cpp | 6 ++---- src/csg_cgal_helper.hpp | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/csg/cgal_helper.cpp b/src/csg/cgal_helper.cpp index aa31c3c..00a2d70 100644 --- a/src/csg/cgal_helper.cpp +++ b/src/csg/cgal_helper.cpp @@ -68,15 +68,14 @@ create_polyhedron(const std::vector> &points, p.delegate(bp); CGAL_assertion(p.is_valid()); - // Triangulate faces + // Triangulate faces - needed for levelset CGAL::Polygon_mesh_processing::triangulate_faces(p); CGAL_assertion(p.is_valid()); return p; } -bool inside_polyhedron(const Polyhedron &polyhedron, double xx, double yy, - double zz) { +bool inside(const Polyhedron &polyhedron, double xx, double yy, double zz) { Kernel::Point_3 p(xx, yy, zz); // Construct AABB tree with a KdTree Tree tree(faces(polyhedron).first, faces(polyhedron).second, polyhedron); diff --git a/src/csg/levelset_3d.cpp b/src/csg/levelset_3d.cpp index df377a2..3e7ce17 100644 --- a/src/csg/levelset_3d.cpp +++ b/src/csg/levelset_3d.cpp @@ -154,10 +154,8 @@ double signed_distance_3d(const RotateExtrude &rot_ext, double xx, double yy, double signed_distance_3d(const Polyhedron &polyhedron, double xx, double yy, double zz) { // TODO: support signed distance instead of -1.0/1.0 - return cgal_helper::inside_polyhedron(polyhedron.cgal_polyhedron(), xx, yy, - zz) - ? 1.0 - : -1.0; + return cgal_helper::inside(polyhedron.cgal_polyhedron(), xx, yy, zz) ? 1.0 + : -1.0; } double signed_distance_3d(const Type3D &obj, double xx, double yy, double zz) { diff --git a/src/csg_cgal_helper.hpp b/src/csg_cgal_helper.hpp index 7973aab..52d9aed 100644 --- a/src/csg_cgal_helper.hpp +++ b/src/csg_cgal_helper.hpp @@ -13,7 +13,7 @@ Polyhedron create_polyhedron(const std::vector> &points, const std::vector> &faces); -bool inside_polyhedron(const Polyhedron &polyhedron, double xx, double yy, +bool inside(const Polyhedron &polyhedron, double xx, double yy, double zz); } // namespace cgal_helper -- GitLab From 530242244dbd76ecf83f59ad169e046299cfe358 Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Wed, 27 May 2020 09:43:03 -0400 Subject: [PATCH 12/17] more work --- src/csg/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/csg/CMakeLists.txt b/src/csg/CMakeLists.txt index 0efaa7b..ed0bd36 100644 --- a/src/csg/CMakeLists.txt +++ b/src/csg/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(csg levelset_3d.cpp matrix_functions.cpp parser.cpp + cgal_helper.cpp ) target_link_libraries(csg PRIVATE -- GitLab From 1cdb0e3e7ba077e6cab002010cd62dab316aa70a Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Wed, 27 May 2020 10:01:41 -0400 Subject: [PATCH 13/17] more work --- src/csg/tests/CMakeLists.txt | 1 + src/csg/tests/parser/primitives.t.cpp | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/csg/tests/CMakeLists.txt b/src/csg/tests/CMakeLists.txt index f3fcb55..a5ee6f4 100644 --- a/src/csg/tests/CMakeLists.txt +++ b/src/csg/tests/CMakeLists.txt @@ -23,6 +23,7 @@ target_include_directories(unit_tests_csg target_link_libraries(unit_tests_csg csg CONAN_PKG::catch2 + CONAN_PKG::cgal ) catch_discover_tests(unit_tests_csg) diff --git a/src/csg/tests/parser/primitives.t.cpp b/src/csg/tests/parser/primitives.t.cpp index 225e6bb..805fc26 100644 --- a/src/csg/tests/parser/primitives.t.cpp +++ b/src/csg/tests/parser/primitives.t.cpp @@ -63,4 +63,6 @@ $name="my_polyhedron"); )"); auto polyh = std::get(st.top.objs.at(0)); CHECK(polyh.name == "my_polyhedron"); + CHECK(polyh.cgal_polyhedron().size_of_vertices() == 5); + CHECK(polyh.cgal_polyhedron().size_of_facets() == 6); } -- GitLab From 7df588648b6c285fe3a6049c00a40d37e74e3137 Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Wed, 27 May 2020 12:13:16 -0400 Subject: [PATCH 14/17] review changes --- src/csg/cgal_helper.cpp | 22 +++++++++++----------- src/csg/tests/parser/primitives.t.cpp | 2 +- src/csg_types.hpp | 17 +++++++++-------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/csg/cgal_helper.cpp b/src/csg/cgal_helper.cpp index 00a2d70..72378cc 100644 --- a/src/csg/cgal_helper.cpp +++ b/src/csg/cgal_helper.cpp @@ -22,29 +22,29 @@ typedef CGAL::AABB_tree Tree; typedef CGAL::Side_of_triangle_mesh Point_inside; // A modifier creating a polyhedron with the incremental builder. -template class Build_polyhedron : public CGAL::Modifier_base { +template class PolyhedronBuilder : public CGAL::Modifier_base { private: - const std::vector> &d_points; - const std::vector> &d_faces; + const std::vector> &m_points; + const std::vector> &m_faces; public: - Build_polyhedron( + PolyhedronBuilder( const std::vector> &points, const std::vector> &faces) - : d_points(points), d_faces(faces) {} + : m_points(points), m_faces(faces) {} void operator()(HDS &hds) { CGAL::Polyhedron_incremental_builder_3 B(hds, true); - B.begin_surface(d_points.size(), d_faces.size()); + B.begin_surface(m_points.size(), m_faces.size()); // Add all the vertices first - for (const auto &p : d_points) { + for (const auto &p : m_points) { auto [px, py, pz] = p; B.add_vertex(Point(px, py, pz)); } // Add facets next - for (const auto &face : d_faces) { + for (const auto &face : m_faces) { B.begin_facet(); for (const auto &p_index : face) { B.add_vertex_to_facet(p_index); @@ -64,7 +64,7 @@ create_polyhedron(const std::vector> &points, Polyhedron p; // Build incrementally - Build_polyhedron bp(points, faces); + PolyhedronBuilder bp(points, faces); p.delegate(bp); CGAL_assertion(p.is_valid()); @@ -76,7 +76,7 @@ create_polyhedron(const std::vector> &points, } bool inside(const Polyhedron &polyhedron, double xx, double yy, double zz) { - Kernel::Point_3 p(xx, yy, zz); + Kernel::Point_3 pt(xx, yy, zz); // Construct AABB tree with a KdTree Tree tree(faces(polyhedron).first, faces(polyhedron).second, polyhedron); tree.accelerate_distance_queries(); @@ -84,7 +84,7 @@ bool inside(const Polyhedron &polyhedron, double xx, double yy, double zz) { Point_inside inside_tester(tree); // Determine the side and return true if inside! - return inside_tester(p) == CGAL::ON_BOUNDED_SIDE; + return inside_tester(pt) == CGAL::ON_BOUNDED_SIDE; } } // namespace cgal_helper diff --git a/src/csg/tests/parser/primitives.t.cpp b/src/csg/tests/parser/primitives.t.cpp index 805fc26..3d1ece9 100644 --- a/src/csg/tests/parser/primitives.t.cpp +++ b/src/csg/tests/parser/primitives.t.cpp @@ -54,7 +54,7 @@ sphere(r = 10) CHECK(sph.radius == 10); } -TEST_CASE("polyhedron", "[csg]") { +TEST_CASE("square pyramid polyhedron", "[csg]") { auto st = *csg::parse_csg(R"( polyhedron( points = [[10, 10, 0], [10, -10, 0], [-10, -10, 0], [-10, 10, 0], [0, 0, 10]], diff --git a/src/csg_types.hpp b/src/csg_types.hpp index b630ca9..0ffe415 100644 --- a/src/csg_types.hpp +++ b/src/csg_types.hpp @@ -52,25 +52,26 @@ struct Cone { struct Polyhedron { private: - std::vector> d_points; - std::vector> d_faces; - cgal_helper::Polyhedron d_cgal_polyhedron; + std::vector> m_points; + std::vector> m_faces; + cgal_helper::Polyhedron m_cgal_polyhedron; public: std::optional name; Polyhedron(const std::vector> &points, const std::vector> &faces) { - for (const auto &p : points) { - d_points.push_back({p[0], p[1], p[2]}); + for (const auto &pt : points) { + assert(pt.size() == 3); + m_points.push_back({pt[0], pt[1], pt[2]}); } for (const auto &f : faces) { - d_faces.push_back(std::vector(f.begin(), f.end())); + m_faces.push_back(std::vector(f.begin(), f.end())); } - d_cgal_polyhedron = cgal_helper::create_polyhedron(d_points, d_faces); + m_cgal_polyhedron = cgal_helper::create_polyhedron(m_points, m_faces); } const cgal_helper::Polyhedron &cgal_polyhedron() const { - return d_cgal_polyhedron; + return m_cgal_polyhedron; } }; -- GitLab From e82fe42b9e2ce3704ac160534c8dbcd3d5860fbf Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Wed, 27 May 2020 13:09:19 -0400 Subject: [PATCH 15/17] add test --- src/csg/tests/parser/primitives.t.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/csg/tests/parser/primitives.t.cpp b/src/csg/tests/parser/primitives.t.cpp index 3d1ece9..eac811a 100644 --- a/src/csg/tests/parser/primitives.t.cpp +++ b/src/csg/tests/parser/primitives.t.cpp @@ -66,3 +66,13 @@ $name="my_polyhedron"); CHECK(polyh.cgal_polyhedron().size_of_vertices() == 5); CHECK(polyh.cgal_polyhedron().size_of_facets() == 6); } + +TEST_CASE("bad vs good polyhedron openscad example", "[csg]") { + // https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Primitive_Solids#polyhedron + CHECK_THROWS(csg::parse_csg(R"( +polyhedron(points = [[0, -10, 60], [0, 10, 60], [0, 10, 0], [0, -10, 0], [60, -10, 60], [60, 10, 60], [10, -10, 50], [10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], [30, 10, 50]], faces = [[0, 2, 3], [0, 1, 2], [0, 4, 5], [0, 5, 1], [5, 4, 2], [2, 4, 3], [6, 8, 9], [6, 7, 8], [6, 10, 11], [6, 11, 7], [10, 8, 11], [10, 9, 8], [0, 3, 9], [9, 0, 6], [10, 6, 0], [0, 4, 10], [3, 9, 10], [3, 10, 4], [1, 7, 11], [1, 11, 5], [1, 7, 8], [1, 8, 2], [2, 8, 11], [2, 11, 5]], convexity = 1); +)")); + CHECK_NOTHROW(csg::parse_csg(R"( +polyhedron(points = [[0, -10, 60], [0, 10, 60], [0, 10, 0], [0, -10, 0], [60, -10, 60], [60, 10, 60], [10, -10, 50], [10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], [30, 10, 50]], faces = [[0, 3, 2], [0, 2, 1], [4, 0, 5], [5, 0, 1], [5, 2, 4], [4, 2, 3], [6, 8, 9], [6, 7, 8], [6, 10, 11], [6, 11, 7], [10, 8, 11], [10, 9, 8], [3, 0, 9], [9, 0, 6], [10, 6, 0], [0, 4, 10], [3, 9, 10], [3, 10, 4], [1, 7, 11], [1, 11, 5], [1, 8, 7], [2, 8, 1], [8, 2, 11], [5, 11, 2]], convexity = 1); +)")); +} -- GitLab From d1681965034df2be0d99972f30907d2143e34a75 Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Wed, 27 May 2020 13:17:43 -0400 Subject: [PATCH 16/17] update test --- src/csg/tests/parser/primitives.t.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/csg/tests/parser/primitives.t.cpp b/src/csg/tests/parser/primitives.t.cpp index eac811a..32e2260 100644 --- a/src/csg/tests/parser/primitives.t.cpp +++ b/src/csg/tests/parser/primitives.t.cpp @@ -72,7 +72,9 @@ TEST_CASE("bad vs good polyhedron openscad example", "[csg]") { CHECK_THROWS(csg::parse_csg(R"( polyhedron(points = [[0, -10, 60], [0, 10, 60], [0, 10, 0], [0, -10, 0], [60, -10, 60], [60, 10, 60], [10, -10, 50], [10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], [30, 10, 50]], faces = [[0, 2, 3], [0, 1, 2], [0, 4, 5], [0, 5, 1], [5, 4, 2], [2, 4, 3], [6, 8, 9], [6, 7, 8], [6, 10, 11], [6, 11, 7], [10, 8, 11], [10, 9, 8], [0, 3, 9], [9, 0, 6], [10, 6, 0], [0, 4, 10], [3, 9, 10], [3, 10, 4], [1, 7, 11], [1, 11, 5], [1, 7, 8], [1, 8, 2], [2, 8, 11], [2, 11, 5]], convexity = 1); )")); - CHECK_NOTHROW(csg::parse_csg(R"( + auto st = *csg::parse_csg(R"( polyhedron(points = [[0, -10, 60], [0, 10, 60], [0, 10, 0], [0, -10, 0], [60, -10, 60], [60, 10, 60], [10, -10, 50], [10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], [30, 10, 50]], faces = [[0, 3, 2], [0, 2, 1], [4, 0, 5], [5, 0, 1], [5, 2, 4], [4, 2, 3], [6, 8, 9], [6, 7, 8], [6, 10, 11], [6, 11, 7], [10, 8, 11], [10, 9, 8], [3, 0, 9], [9, 0, 6], [10, 6, 0], [0, 4, 10], [3, 9, 10], [3, 10, 4], [1, 7, 11], [1, 11, 5], [1, 8, 7], [2, 8, 1], [8, 2, 11], [5, 11, 2]], convexity = 1); -)")); +)"); + auto polyh = std::get(st.top.objs.at(0)); + CHECK(polyh.cgal_polyhedron().size_of_vertices() == 12); } -- GitLab From de32a718a27521ce904d4665dc8ecd2d7c04d8f2 Mon Sep 17 00:00:00 2001 From: Deepak Rangarajan Date: Wed, 27 May 2020 14:32:49 -0400 Subject: [PATCH 17/17] reformat --- src/csg/tests/parser/primitives.t.cpp | 29 +++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/csg/tests/parser/primitives.t.cpp b/src/csg/tests/parser/primitives.t.cpp index 32e2260..7847718 100644 --- a/src/csg/tests/parser/primitives.t.cpp +++ b/src/csg/tests/parser/primitives.t.cpp @@ -70,10 +70,35 @@ $name="my_polyhedron"); TEST_CASE("bad vs good polyhedron openscad example", "[csg]") { // https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Primitive_Solids#polyhedron CHECK_THROWS(csg::parse_csg(R"( -polyhedron(points = [[0, -10, 60], [0, 10, 60], [0, 10, 0], [0, -10, 0], [60, -10, 60], [60, 10, 60], [10, -10, 50], [10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], [30, 10, 50]], faces = [[0, 2, 3], [0, 1, 2], [0, 4, 5], [0, 5, 1], [5, 4, 2], [2, 4, 3], [6, 8, 9], [6, 7, 8], [6, 10, 11], [6, 11, 7], [10, 8, 11], [10, 9, 8], [0, 3, 9], [9, 0, 6], [10, 6, 0], [0, 4, 10], [3, 9, 10], [3, 10, 4], [1, 7, 11], [1, 11, 5], [1, 7, 8], [1, 8, 2], [2, 8, 11], [2, 11, 5]], convexity = 1); +polyhedron( +points = [[0, -10, 60], [0, 10, 60], [0, 10, 0], +[0, -10, 0], [60, -10, 60], [60, 10, 60], [10, -10, 50], +[10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], +[30, 10, 50]], + +faces = [[0, 2, 3], [0, 1, 2], [0, 4, 5], [0, 5, 1], +[5, 4, 2], [2, 4, 3], [6, 8, 9], [6, 7, 8], [6, 10, 11], +[6, 11, 7], [10, 8, 11], [10, 9, 8], [0, 3, 9], [9, 0, 6], +[10, 6, 0], [0, 4, 10], [3, 9, 10], [3, 10, 4], [1, 7, 11], +[1, 11, 5], [1, 7, 8], [1, 8, 2], [2, 8, 11], [2, 11, 5]], + +convexity = 1); )")); + auto st = *csg::parse_csg(R"( -polyhedron(points = [[0, -10, 60], [0, 10, 60], [0, 10, 0], [0, -10, 0], [60, -10, 60], [60, 10, 60], [10, -10, 50], [10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], [30, 10, 50]], faces = [[0, 3, 2], [0, 2, 1], [4, 0, 5], [5, 0, 1], [5, 2, 4], [4, 2, 3], [6, 8, 9], [6, 7, 8], [6, 10, 11], [6, 11, 7], [10, 8, 11], [10, 9, 8], [3, 0, 9], [9, 0, 6], [10, 6, 0], [0, 4, 10], [3, 9, 10], [3, 10, 4], [1, 7, 11], [1, 11, 5], [1, 8, 7], [2, 8, 1], [8, 2, 11], [5, 11, 2]], convexity = 1); +polyhedron( +points = [[0, -10, 60], [0, 10, 60], [0, 10, 0], +[0, -10, 0], [60, -10, 60], [60, 10, 60], [10, -10, 50], +[10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], +[30, 10, 50]], + +faces = [[0, 3, 2], [0, 2, 1], [4, 0, 5], [5, 0, 1], +[5, 2, 4], [4, 2, 3], [6, 8, 9], [6, 7, 8], [6, 10, 11], +[6, 11, 7], [10, 8, 11], [10, 9, 8], [3, 0, 9], [9, 0, 6], +[10, 6, 0], [0, 4, 10], [3, 9, 10], [3, 10, 4], [1, 7, 11], +[1, 11, 5], [1, 8, 7], [2, 8, 1], [8, 2, 11], [5, 11, 2]], + +convexity = 1); )"); auto polyh = std::get(st.top.objs.at(0)); CHECK(polyh.cgal_polyhedron().size_of_vertices() == 12); -- GitLab