Clifford Wolf:

Various cleanups



git-svn-id: http://svn.clifford.at/openscad/trunk@10 b57f626f-c46c-0410-a088-ec61d464b74c
stl_dim
clifford 2009-06-22 22:36:36 +00:00
parent a19c8ed475
commit 4480b67f96
10 changed files with 266 additions and 308 deletions

View File

@ -24,7 +24,7 @@ void Context::args(const QVector<QString> &argnames, const QVector<Expression*>
const QVector<QString> &call_argnames, const QVector<Value> &call_argvalues)
{
for (int i=0; i<argnames.size(); i++) {
variables[argnames[i]] = argexpr[i] ? argexpr[i]->evaluate(this->parent) : Value();
variables[argnames[i]] = i < argexpr.size() && argexpr[i] ? argexpr[i]->evaluate(this->parent) : Value();
}
int posarg = 0;

View File

@ -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<QString> &call_argnames, const QVector<Value> &call_argvalues, const QVector<AbstractNode*> 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<QString>&, const QVector<Value>&, const QVector<AbstractNode*> child_nodes) const
AbstractNode *CsgModule::evaluate(const Context*, const QVector<QString>&, const QVector<Value>&, const QVector<AbstractNode*> 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);
}

137
cube.cc
View File

@ -1,137 +0,0 @@
/*
* OpenSCAD (www.openscad.at)
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
*
* 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<QString> &call_argnames, const QVector<Value> &call_argvalues, const QVector<AbstractNode*> 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<QString>&, const QVector<Value> &call_argvalues, const QVector<AbstractNode*> 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<CGAL_HDS>
{
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;
}

View File

@ -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);
}
}

View File

@ -1,73 +0,0 @@
/*
* OpenSCAD (www.openscad.at)
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
*
* 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<QString> &call_argnames, const QVector<Value> &call_argvalues, const QVector<AbstractNode*> 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<QString>&, const QVector<Value>&, const QVector<AbstractNode*> 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();
}

View File

@ -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()

View File

@ -188,13 +188,9 @@ extern QHash<QString, AbstractModule*> 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
{

View File

@ -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

222
primitive.cc Normal file
View File

@ -0,0 +1,222 @@
/*
* OpenSCAD (www.openscad.at)
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
*
* 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<QString> &call_argnames, const QVector<Value> &call_argvalues, const QVector<AbstractNode*> 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<QString> &call_argnames, const QVector<Value> &call_argvalues, const QVector<AbstractNode*> 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<QString> argnames;
QVector<Expression*> argexpr;
if (type == CUBE) {
argnames = QVector<QString>() << "size" << "center";
}
if (type == SPHERE) {
argnames = QVector<QString>() << "r";
}
if (type == CYLINDER) {
argnames = QVector<QString>() << "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<CGAL_HDS>
{
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;
}

View File

@ -1,67 +0,0 @@
/*
* OpenSCAD (www.openscad.at)
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
*
* 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<QString> &call_argnames, const QVector<Value> &call_argvalues, const QVector<AbstractNode*> 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<QString>&, const QVector<Value>&, const QVector<AbstractNode*> 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();
}