Commit d5348a1f authored by Deepak Rangarajan's avatar Deepak Rangarajan Committed by Mark Meredith
Browse files

allow empty union();, difference();, etc.

parent 5313fa5a
Loading
Loading
Loading
Loading
Loading
+19 −10
Original line number Diff line number Diff line
@@ -135,17 +135,21 @@ struct shape<Dimension::D2> : seq<sor<circle, square, polygon>, opt<S_CLN>> {};
template <Dimension dim> struct obj_list;

template <Dimension dim>
struct bool_union : seq<string<'u', 'n', 'i', 'o', 'n'>, L_FUN, R_FUN,
                        L_BLK<dim>, obj_list<dim>, R_BLK<dim>> {};
struct bool_union
    : seq<string<'u', 'n', 'i', 'o', 'n'>, L_FUN, R_FUN,
          opt<seq<L_BLK<dim>, obj_list<dim>, R_BLK<dim>>>, opt<S_CLN>> {};

template <Dimension dim>
struct bool_intersection
    : seq<string<'i', 'n', 't', 'e', 'r', 's', 'e', 'c', 't', 'i', 'o', 'n'>,
          L_FUN, R_FUN, L_BLK<dim>, obj_list<dim>, R_BLK<dim>> {};
          L_FUN, R_FUN, opt<seq<L_BLK<dim>, obj_list<dim>, R_BLK<dim>>>,
          opt<S_CLN>> {};

template <Dimension dim>
struct bool_diff : seq<string<'d', 'i', 'f', 'f', 'e', 'r', 'e', 'n', 'c', 'e'>,
                       L_FUN, R_FUN, L_BLK<dim>, obj_list<dim>, R_BLK<dim>> {};
struct bool_diff
    : seq<string<'d', 'i', 'f', 'f', 'e', 'r', 'e', 'n', 'c', 'e'>, L_FUN,
          R_FUN, opt<seq<L_BLK<dim>, obj_list<dim>, R_BLK<dim>>>, opt<S_CLN>> {
};

template <Dimension dim>
struct bool_exp : sor<bool_union<dim>, bool_intersection<dim>, bool_diff<dim>> {
@@ -154,7 +158,8 @@ struct bool_exp : sor<bool_union<dim>, bool_intersection<dim>, bool_diff<dim>> {
template <Dimension dim>
struct mulmat
    : seq<string<'m', 'u', 'l', 't', 'm', 'a', 't', 'r', 'i', 'x'>, L_FUN,
          matrix, R_FUN, L_BLK<dim>, obj_list<dim>, R_BLK<dim>> {};
          matrix, R_FUN, opt<seq<L_BLK<dim>, obj_list<dim>, R_BLK<dim>>>,
          opt<S_CLN>> {};

template <Dimension dim>
struct group
@@ -163,13 +168,17 @@ struct group

struct extrude_rot : seq<string<'r', 'o', 't', 'a', 't', 'e', '_', 'e', 'x',
                                't', 'r', 'u', 'd', 'e'>,
                         L_FUN, attr_list, R_FUN, L_BLK<Dimension::D2>,
                         obj_list<Dimension::D2>, R_BLK<Dimension::D2>> {};
                         L_FUN, attr_list, R_FUN,
                         opt<seq<L_BLK<Dimension::D2>, obj_list<Dimension::D2>,
                                 R_BLK<Dimension::D2>>>,
                         opt<S_CLN>> {};

struct extrude_lin : seq<string<'l', 'i', 'n', 'e', 'a', 'r', '_', 'e', 'x',
                                't', 'r', 'u', 'd', 'e'>,
                         L_FUN, attr_list, R_FUN, L_BLK<Dimension::D2>,
                         obj_list<Dimension::D2>, R_BLK<Dimension::D2>> {};
                         L_FUN, attr_list, R_FUN,
                         opt<seq<L_BLK<Dimension::D2>, obj_list<Dimension::D2>,
                                 R_BLK<Dimension::D2>>>,
                         opt<S_CLN>> {};

struct hull
    : seq<string<'h', 'u', 'l', 'l'>, L_FUN, R_FUN, L_BLK<Dimension::D3>,
+35 −0
Original line number Diff line number Diff line
@@ -131,3 +131,38 @@ group();

  CHECK(un.objs.size() == 0);
}

TEST_CASE("empty boolean", "[csg]") {
  auto st = *csg::parse_csg(R"(
union();
sphere(r=0.1);
)");
  auto un = std::get<csg::Union3D>(st.top.objs.at(0));
  CHECK(un.objs.size() == 0);

  auto sph = std::get<csg::Sphere>(st.top.objs.at(1));
  CHECK(sph.radius == 0.1);
}

TEST_CASE("empty intersection", "[csg]") {
  auto st = *csg::parse_csg(R"(
intersection();
)");
  auto in = std::get<csg::Intersection3D>(st.top.objs.at(0));
  CHECK(in.objs.size() == 0);
}

TEST_CASE("empty difference", "[csg]") {
  auto st = *csg::parse_csg(R"(
cylinder(h = 200, r1 = 1.7, r2 = 2.7, center = false);
difference();
)");
  auto cone = std::get<csg::Cone>(st.top.objs.at(0));
  CHECK(cone.radius1 == 1.7);
  CHECK(cone.radius2 == 2.7);
  CHECK(cone.height == 200);

  auto diff = std::get<csg::Difference3D>(st.top.objs.at(1));
  CHECK(diff.first_obj == nullptr);
  CHECK(diff.next_objs.objs.size() == 0);
}
+34 −0
Original line number Diff line number Diff line
@@ -189,4 +189,38 @@ paths = [[0, 1, 2], [3, 4, 5]]);
  CHECK(inner.cgal_polygon().size() == 3);
}

TEST_CASE("empty linear_extrude", "[csg]") {
  auto st = *csg::parse_csg(R"(
linear_extrude(height = 10, center = true, scale = [10,1]);
)");
  auto lin_ext = std::get<csg::LinearExtrude>(st.top.objs.at(0));
  CHECK(lin_ext.height == 10);
  CHECK(lin_ext.center == true);

  auto [sx, sy] = lin_ext.scale;
  CHECK(sx == 10);
  CHECK(sy == 1);
}

TEST_CASE("empty intersection inside linear extrude", "[csg]") {
  auto st = *csg::parse_csg(R"(
linear_extrude(
height = 10,
center = true,
scale = [10,1]
) {
    circle(r=0.2);
    intersection();
}
)");
  auto lin_ext = std::get<csg::LinearExtrude>(st.top.objs.at(0));
  CHECK(lin_ext.group.objs.size() == 2);

  auto cir = std::get<csg::Circle>(lin_ext.group.objs.at(0));
  CHECK(cir.radius == 0.2);

  auto in = std::get<csg::Intersection2D>(lin_ext.group.objs.at(1));
  CHECK(in.objs.size() == 0);
}

} // namespace
+19 −0
Original line number Diff line number Diff line
@@ -151,3 +151,22 @@ multmatrix(
  CHECK(cone.radius1 == 1);
  CHECK(cone.radius2 == 2);
}

TEST_CASE("empty matmul", "[csg]") {
  auto st = *csg::parse_csg(R"(
multmatrix(
[
[1, 0, 0, 0.0020],
[0, 1, 0, 0.0005],
[0, 0, 1, 0.0005],
[0, 0, 0, 1]
]
);
)");
  auto mat = std::get<csg::Mulmatrix3D>(st.top.objs.back());
  CHECK(mat.group.objs.size() == 0);
  CHECK(mat.translation == std::array<double, 3>({0.0020, 0.0005, 0.0005}));
  CHECK(mat.rotation()[0] == std::array<double, 3>({1, 0, 0}));
  CHECK(mat.rotation()[1] == std::array<double, 3>({0, 1, 0}));
  CHECK(mat.rotation()[2] == std::array<double, 3>({0, 0, 1}));
}