diff --git a/context.cc b/context.cc index 60e19e64..1ae3c0dd 100644 --- a/context.cc +++ b/context.cc @@ -24,7 +24,7 @@ void Context::args(const QVector &argnames, const QVector const QVector &call_argnames, const QVector &call_argvalues) { for (int i=0; ievaluate(this->parent) : Value(); + variables[argnames[i]] = i < argexpr.size() && argexpr[i] ? argexpr[i]->evaluate(this->parent) : Value(); } int posarg = 0; diff --git a/difference.cc b/csg.cc similarity index 58% rename from difference.cc rename to csg.cc index a1f90252..9ce2bddb 100644 --- a/difference.cc +++ b/csg.cc @@ -22,52 +22,75 @@ #include "openscad.h" -class DifferenceModule : public AbstractModule +enum csg_type_e { + UNION, + DIFFERENCE, + INTERSECT +}; + +class CsgModule : public AbstractModule { public: + csg_type_e type; + CsgModule(csg_type_e type) : type(type) { } virtual AbstractNode *evaluate(const Context *ctx, const QVector &call_argnames, const QVector &call_argvalues, const QVector child_nodes) const; }; -class DifferenceNode : public AbstractNode +class CsgNode : public AbstractNode { public: + csg_type_e type; + CsgNode(csg_type_e type) : type(type) { } virtual CGAL_Nef_polyhedron render_cgal_nef_polyhedron() const; virtual QString dump(QString indent) const; }; -AbstractNode *DifferenceModule::evaluate(const Context*, const QVector&, const QVector&, const QVector child_nodes) const +AbstractNode *CsgModule::evaluate(const Context*, const QVector&, const QVector&, const QVector child_nodes) const { - DifferenceNode *node = new DifferenceNode(); + CsgNode *node = new CsgNode(type); foreach (AbstractNode *v, child_nodes) node->children.append(v); return node; } -CGAL_Nef_polyhedron DifferenceNode::render_cgal_nef_polyhedron() const +CGAL_Nef_polyhedron CsgNode::render_cgal_nef_polyhedron() const { - bool first = true; + bool first; CGAL_Nef_polyhedron N; foreach (AbstractNode *v, children) { - if (first) + if (first) { N = v->render_cgal_nef_polyhedron(); - else + first = false; + } else if (type == UNION) { + N += v->render_cgal_nef_polyhedron(); + } else if (type == DIFFERENCE) { N -= v->render_cgal_nef_polyhedron(); - first = false; + } else if (type == INTERSECT) { + N *= v->render_cgal_nef_polyhedron(); + } } progress_report(); return N; } -QString DifferenceNode::dump(QString indent) const +QString CsgNode::dump(QString indent) const { - QString text = indent + "difference() {\n"; + QString text = indent; + if (type == UNION) + text += "union() {\n"; + if (type == DIFFERENCE) + text += "difference() {\n"; + if (type == INTERSECT) + text += "intersect() {\n"; foreach (AbstractNode *v, children) text += v->dump(indent + QString("\t")); return text + indent + "}\n"; } -void register_builtin_difference() +void register_builtin_csg() { - builtin_modules["difference"] = new DifferenceModule(); + builtin_modules["union"] = new CsgModule(UNION); + builtin_modules["difference"] = new CsgModule(DIFFERENCE); + builtin_modules["intersect"] = new CsgModule(INTERSECT); } diff --git a/cube.cc b/cube.cc deleted file mode 100644 index c692a0de..00000000 --- a/cube.cc +++ /dev/null @@ -1,137 +0,0 @@ -/* - * OpenSCAD (www.openscad.at) - * Copyright (C) 2009 Clifford Wolf - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ - -#define INCLUDE_ABSTRACT_NODE_DETAILS - -#include "openscad.h" - -class CubeModule : public AbstractModule -{ -public: - virtual AbstractNode *evaluate(const Context *ctx, const QVector &call_argnames, const QVector &call_argvalues, const QVector child_nodes) const; -}; - -class CubeNode : public AbstractNode -{ -public: - double x, y, z; - virtual CGAL_Nef_polyhedron render_cgal_nef_polyhedron() const; - virtual QString dump(QString indent) const; -}; - -AbstractNode *CubeModule::evaluate(const Context*, const QVector&, const QVector &call_argvalues, const QVector child_nodes) const -{ - CubeNode *node = new CubeNode(); - if (call_argvalues.size() == 1 && call_argvalues[0].is_vector) { - node->x = call_argvalues[0].x; - node->y = call_argvalues[0].y; - node->z = call_argvalues[0].z; - } else if (call_argvalues.size() == 1 && !call_argvalues[0].is_nan) { - node->x = call_argvalues[0].x; - node->y = call_argvalues[0].x; - node->z = call_argvalues[0].x; - } - foreach (AbstractNode *v, child_nodes) - delete v; - return node; -} - -void register_builtin_cube() -{ - builtin_modules["cube"] = new CubeModule(); -} - -class CGAL_Build_cube : public CGAL::Modifier_base -{ -public: - const CubeNode *n; - CGAL_Build_cube(const CubeNode *n) : n(n) { } - void operator()(CGAL_HDS& hds) { - CGAL_Polybuilder B(hds, true); - B.begin_surface(8, 6, 24); - typedef CGAL_HDS::Vertex::Point Point; - B.add_vertex(Point(-n->x/2, -n->y/2, +n->z/2)); // 0 - B.add_vertex(Point(+n->x/2, -n->y/2, +n->z/2)); // 1 - B.add_vertex(Point(+n->x/2, +n->y/2, +n->z/2)); // 2 - B.add_vertex(Point(-n->x/2, +n->y/2, +n->z/2)); // 3 - B.add_vertex(Point(-n->x/2, -n->y/2, -n->z/2)); // 4 - B.add_vertex(Point(+n->x/2, -n->y/2, -n->z/2)); // 5 - B.add_vertex(Point(+n->x/2, +n->y/2, -n->z/2)); // 6 - B.add_vertex(Point(-n->x/2, +n->y/2, -n->z/2)); // 7 - B.begin_facet(); - B.add_vertex_to_facet(0); - B.add_vertex_to_facet(1); - B.add_vertex_to_facet(2); - B.add_vertex_to_facet(3); - B.end_facet(); - B.begin_facet(); - B.add_vertex_to_facet(7); - B.add_vertex_to_facet(6); - B.add_vertex_to_facet(5); - B.add_vertex_to_facet(4); - B.end_facet(); - B.begin_facet(); - B.add_vertex_to_facet(4); - B.add_vertex_to_facet(5); - B.add_vertex_to_facet(1); - B.add_vertex_to_facet(0); - B.end_facet(); - B.begin_facet(); - B.add_vertex_to_facet(5); - B.add_vertex_to_facet(6); - B.add_vertex_to_facet(2); - B.add_vertex_to_facet(1); - B.end_facet(); - B.begin_facet(); - B.add_vertex_to_facet(6); - B.add_vertex_to_facet(7); - B.add_vertex_to_facet(3); - B.add_vertex_to_facet(2); - B.end_facet(); - B.begin_facet(); - B.add_vertex_to_facet(7); - B.add_vertex_to_facet(4); - B.add_vertex_to_facet(0); - B.add_vertex_to_facet(3); - B.end_facet(); - B.end_surface(); - } -}; - -CGAL_Nef_polyhedron CubeNode::render_cgal_nef_polyhedron() const -{ - CGAL_Polyhedron P; - CGAL_Build_cube builder(this); - P.delegate(builder); - CGAL_Nef_polyhedron N(P); - progress_report(); - return N; -} - -QString CubeNode::dump(QString indent) const -{ - QString text; - if (x == y && y == z) - text.sprintf("cube(%f);\n", x); - else - text.sprintf("cube([%f %f %f]);\n", x, y, z); - return indent + text; -} - diff --git a/example.scad b/example.scad index 8a1fad49..a2b86c13 100644 --- a/example.scad +++ b/example.scad @@ -24,8 +24,8 @@ module test001() module test002() { difference() { - cube([2 2 0.5]); - cube([0.5 0.5 2]); + cube([2 2 0.5], 1); + cube([0.5 0.5 2], 1); } } diff --git a/intersect.cc b/intersect.cc deleted file mode 100644 index 29dd4e3a..00000000 --- a/intersect.cc +++ /dev/null @@ -1,73 +0,0 @@ -/* - * OpenSCAD (www.openscad.at) - * Copyright (C) 2009 Clifford Wolf - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ - -#define INCLUDE_ABSTRACT_NODE_DETAILS - -#include "openscad.h" - -class IntersectModule : public AbstractModule -{ -public: - virtual AbstractNode *evaluate(const Context *ctx, const QVector &call_argnames, const QVector &call_argvalues, const QVector child_nodes) const; -}; - -class IntersectNode : public AbstractNode -{ -public: - virtual CGAL_Nef_polyhedron render_cgal_nef_polyhedron() const; - virtual QString dump(QString indent) const; -}; - -AbstractNode *IntersectModule::evaluate(const Context*, const QVector&, const QVector&, const QVector child_nodes) const -{ - IntersectNode *node = new IntersectNode(); - foreach (AbstractNode *v, child_nodes) - node->children.append(v); - return node; -} - -CGAL_Nef_polyhedron IntersectNode::render_cgal_nef_polyhedron() const -{ - bool first = true; - CGAL_Nef_polyhedron N; - foreach (AbstractNode *v, children) { - if (first) - N = v->render_cgal_nef_polyhedron(); - else - N *= v->render_cgal_nef_polyhedron(); - first = false; - } - progress_report(); - return N; -} - -QString IntersectNode::dump(QString indent) const -{ - QString text = indent + "intersect() {\n"; - foreach (AbstractNode *v, children) - text += v->dump(indent + QString("\t")); - return text + indent + "}\n"; -} - -void register_builtin_intersect() -{ - builtin_modules["intersect"] = new IntersectModule(); -} - diff --git a/module.cc b/module.cc index e6f50c95..acc37d6b 100644 --- a/module.cc +++ b/module.cc @@ -164,13 +164,9 @@ void initialize_builtin_modules() { builtin_modules["group"] = new AbstractModule(); - register_builtin_union(); - register_builtin_difference(); - register_builtin_intersect(); - + register_builtin_csg(); register_builtin_trans(); - - register_builtin_cube(); + register_builtin_primitive(); } void destroy_builtin_modules() diff --git a/openscad.h b/openscad.h index 4b9abcab..3e656450 100644 --- a/openscad.h +++ b/openscad.h @@ -188,13 +188,9 @@ extern QHash builtin_modules; extern void initialize_builtin_modules(); extern void destroy_builtin_modules(); -extern void register_builtin_union(); -extern void register_builtin_difference(); -extern void register_builtin_intersect(); - +extern void register_builtin_csg(); extern void register_builtin_trans(); - -extern void register_builtin_cube(); +extern void register_builtin_primitive(); class Context { diff --git a/openscad.pro b/openscad.pro index bf335d95..f8002c81 100644 --- a/openscad.pro +++ b/openscad.pro @@ -9,7 +9,5 @@ YACCSOURCES += parser.y HEADERS += openscad.h SOURCES += openscad.cc value.cc expr.cc func.cc module.cc context.cc -SOURCES += union.cc difference.cc intersect.cc -SOURCES += trans.cc -SOURCES += cube.cc +SOURCES += csg.cc trans.cc primitive.cc diff --git a/primitive.cc b/primitive.cc new file mode 100644 index 00000000..9f5e7f30 --- /dev/null +++ b/primitive.cc @@ -0,0 +1,222 @@ +/* + * OpenSCAD (www.openscad.at) + * Copyright (C) 2009 Clifford Wolf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#define INCLUDE_ABSTRACT_NODE_DETAILS + +#include "openscad.h" + +enum primitive_type_e { + CUBE, + SPHERE, + CYLINDER +}; + +class PrimitiveModule : public AbstractModule +{ +public: + primitive_type_e type; + PrimitiveModule(primitive_type_e type) : type(type) { } + virtual AbstractNode *evaluate(const Context *ctx, const QVector &call_argnames, const QVector &call_argvalues, const QVector child_nodes) const; +}; + +class PrimitiveNode : public AbstractNode +{ +public: + bool center; + double x, y, z, h, r1, r2; + primitive_type_e type; + PrimitiveNode(primitive_type_e type) : type(type) { } + virtual CGAL_Nef_polyhedron render_cgal_nef_polyhedron() const; + virtual QString dump(QString indent) const; +}; + +AbstractNode *PrimitiveModule::evaluate(const Context *ctx, const QVector &call_argnames, const QVector &call_argvalues, const QVector child_nodes) const +{ + PrimitiveNode *node = new PrimitiveNode(type); + + node->center = false; + node->x = node->y = node->z = node->h = node->r1 = node->r2 = 1; + + QVector argnames; + QVector argexpr; + + if (type == CUBE) { + argnames = QVector() << "size" << "center"; + } + if (type == SPHERE) { + argnames = QVector() << "r"; + } + if (type == CYLINDER) { + argnames = QVector() << "h" << "r1" << "r2" << "center"; + } + + Context c(ctx); + c.args(argnames, argexpr, call_argnames, call_argvalues); + + if (type == CUBE) { + Value size = c.lookup_variable("size"); + Value center = c.lookup_variable("center"); + if (size.is_vector) { + node->x = size.x; + node->y = size.y; + node->z = size.z; + } else if (!size.is_nan) { + node->x = size.x; + node->y = size.x; + node->z = size.x; + } + if (!center.is_vector && !center.is_nan) { + node->center = center.x != 0; + } + } + + if (type == SPHERE) { + Value r = c.lookup_variable("r"); + if (!r.is_vector && !r.is_nan) { + node->r1 = r.x; + } + } + + if (type == CYLINDER) { + Value h = c.lookup_variable("h"); + Value r = c.lookup_variable("r"); + Value r1 = c.lookup_variable("r1"); + Value r2 = c.lookup_variable("r2"); + Value center = c.lookup_variable("center"); + if (!h.is_vector && !h.is_nan) { + node->h = h.x; + } + if (!r.is_vector && !r.is_nan) { + node->r1 = r.x; + node->r2 = r.x; + } + if (!r1.is_vector && !r1.is_nan) { + node->r1 = r1.x; + } + if (!r2.is_vector && !r2.is_nan) { + node->r2 = r2.x; + } + if (!center.is_vector && !center.is_nan) { + node->center = center.x != 0; + } + } + + foreach (AbstractNode *v, child_nodes) + delete v; + + return node; +} + +void register_builtin_primitive() +{ + builtin_modules["cube"] = new PrimitiveModule(CUBE); + builtin_modules["sphere"] = new PrimitiveModule(SPHERE); + builtin_modules["cylinder"] = new PrimitiveModule(CYLINDER); +} + +static int cube_facets[6][4] = { + { 0, 1, 2, 3 }, + { 7, 6, 5, 4 }, + { 4, 5, 1, 0 }, + { 5, 6, 2, 1 }, + { 6, 7, 3, 2 }, + { 7, 4, 0, 3 } +}; + +class CGAL_Build_cube : public CGAL::Modifier_base +{ +public: + typedef CGAL_HDS::Vertex::Point Point; + + const PrimitiveNode *n; + CGAL_Build_cube(const PrimitiveNode *n) : n(n) { } + + void operator()(CGAL_HDS& hds) + { + CGAL_Polybuilder B(hds, true); + + if (n->type == CUBE) { + B.begin_surface(8, 6, 24); + double x1, x2, y1, y2, z1, z2; + if (n->center) { + x1 = -n->x/2; + x2 = +n->x/2; + y1 = -n->y/2; + y2 = +n->y/2; + z1 = -n->z/2; + z2 = +n->z/2; + } else { + x1 = y1 = z1 = 0; + x2 = n->x; + y2 = n->y; + z2 = n->z; + } + B.add_vertex(Point(x1, y1, z2)); // 0 + B.add_vertex(Point(x2, y1, z2)); // 1 + B.add_vertex(Point(x2, y2, z2)); // 2 + B.add_vertex(Point(x1, y2, z2)); // 3 + B.add_vertex(Point(x1, y1, z1)); // 4 + B.add_vertex(Point(x2, y1, z1)); // 5 + B.add_vertex(Point(x2, y2, z1)); // 6 + B.add_vertex(Point(x1, y2, z1)); // 7 + for (int i = 0; i < 6; i++) { + B.begin_facet(); + for (int j = 0; j < 4; j++) + B.add_vertex_to_facet(cube_facets[i][j]); + B.end_facet(); + } + B.end_surface(); + } + + if (n->type == SPHERE) { + /* FIXME */ + } + + if (n->type == CYLINDER) { + int fragments = 10; + B.begin_surface(fragments*2, fragments+2); + /* FIXME */ + B.end_surface(); + } + } +}; + +CGAL_Nef_polyhedron PrimitiveNode::render_cgal_nef_polyhedron() const +{ + CGAL_Polyhedron P; + CGAL_Build_cube builder(this); + P.delegate(builder); + CGAL_Nef_polyhedron N(P); + progress_report(); + return N; +} + +QString PrimitiveNode::dump(QString indent) const +{ + QString text; + if (type == CUBE) + text.sprintf("cube(size = [%f %f %f], center = %d);\n", x, y, z, center ? 1 : 0); + if (type == SPHERE) + text.sprintf("sphere(r = %f);\n", r1); + if (type == CYLINDER) + text.sprintf("cylinder(h = %f, r1 = %f, r2 = %f, center = %d);\n", h, r1, r2, center ? 1 : 0); + return indent + text; +} + diff --git a/union.cc b/union.cc deleted file mode 100644 index a1315ce7..00000000 --- a/union.cc +++ /dev/null @@ -1,67 +0,0 @@ -/* - * OpenSCAD (www.openscad.at) - * Copyright (C) 2009 Clifford Wolf - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ - -#define INCLUDE_ABSTRACT_NODE_DETAILS - -#include "openscad.h" - -class UnionModule : public AbstractModule -{ -public: - virtual AbstractNode *evaluate(const Context *ctx, const QVector &call_argnames, const QVector &call_argvalues, const QVector child_nodes) const; -}; - -class UnionNode : public AbstractNode -{ -public: - virtual CGAL_Nef_polyhedron render_cgal_nef_polyhedron() const; - virtual QString dump(QString indent) const; -}; - -AbstractNode *UnionModule::evaluate(const Context*, const QVector&, const QVector&, const QVector child_nodes) const -{ - UnionNode *node = new UnionNode(); - foreach (AbstractNode *v, child_nodes) - node->children.append(v); - return node; -} - -CGAL_Nef_polyhedron UnionNode::render_cgal_nef_polyhedron() const -{ - CGAL_Nef_polyhedron N; - foreach (AbstractNode *v, children) - N += v->render_cgal_nef_polyhedron(); - progress_report(); - return N; -} - -QString UnionNode::dump(QString indent) const -{ - QString text = indent + "union() {\n"; - foreach (AbstractNode *v, children) - text += v->dump(indent + QString("\t")); - return text + indent + "}\n"; -} - -void register_builtin_union() -{ - builtin_modules["union"] = new UnionModule(); -} -