diff --git a/src/GeometryEvaluator.cc b/src/GeometryEvaluator.cc index 6b5a8c89..7f1a8cc9 100644 --- a/src/GeometryEvaluator.cc +++ b/src/GeometryEvaluator.cc @@ -342,6 +342,14 @@ Polygon2d *GeometryEvaluator::applyToChildren2D(const AbstractNode &node, OpenSC std::vector children = collectChildren2D(node); + if (children.empty()) { + return NULL; + } + + if (children.size() == 1) { + return new Polygon2d(*children[0]); // Copy + } + ClipperLib::ClipType clipType; switch (op) { case OPENSCAD_UNION: @@ -359,7 +367,26 @@ Polygon2d *GeometryEvaluator::applyToChildren2D(const AbstractNode &node, OpenSC break; } - return ClipperUtils::apply(children, clipType); + if (clipType == ClipperLib::ctIntersection && !children.empty()) { + // intersection operations must be split into a sequence of binary operations + + std::vector operands(2); + operands[0] = children[0]; + + for (int i = 1; i < children.size(); i++) { + operands[1] = children[i]; + Polygon2d * result = ClipperUtils::apply(operands, clipType); + if (operands[0] != children[0]) delete operands[0]; + operands[0] = result; + } + + assert(children.size() >= 2); + // We know that this is ok + return const_cast(operands[0]); + } + else { + return ClipperUtils::apply(children, clipType); + } } /*!