2009-06-20 23:00:19 +04:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
#define INCLUDE_ABSTRACT_NODE_DETAILS
|
|
|
|
|
2009-06-20 23:00:19 +04:00
|
|
|
#include "openscad.h"
|
|
|
|
|
|
|
|
AbstractModule::~AbstractModule()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *AbstractModule::evaluate(const Context*, const ModuleInstanciation *inst) const
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *node = new AbstractNode(inst);
|
2009-06-30 00:30:43 +04:00
|
|
|
|
2009-07-01 13:48:25 +04:00
|
|
|
foreach (ModuleInstanciation *v, inst->children) {
|
|
|
|
AbstractNode *n = v->evaluate(inst->ctx);
|
2009-06-30 00:30:43 +04:00
|
|
|
if (n)
|
|
|
|
node->children.append(n);
|
|
|
|
}
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
return node;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
QString AbstractModule::dump(QString indent, QString name) const
|
|
|
|
{
|
|
|
|
return QString("%1abstract module %2();\n").arg(indent, name);
|
|
|
|
}
|
|
|
|
|
2009-06-20 23:00:19 +04:00
|
|
|
ModuleInstanciation::~ModuleInstanciation()
|
|
|
|
{
|
|
|
|
foreach (Expression *v, argexpr)
|
|
|
|
delete v;
|
2009-06-21 12:59:53 +04:00
|
|
|
foreach (ModuleInstanciation *v, children)
|
|
|
|
delete v;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
QString ModuleInstanciation::dump(QString indent) const
|
|
|
|
{
|
|
|
|
QString text = indent;
|
|
|
|
if (!label.isEmpty())
|
|
|
|
text += label + QString(": ");
|
|
|
|
text += modname + QString("(");
|
|
|
|
for (int i=0; i < argnames.size(); i++) {
|
|
|
|
if (i > 0)
|
|
|
|
text += QString(", ");
|
|
|
|
if (!argnames[i].isEmpty())
|
|
|
|
text += argnames[i] + QString(" = ");
|
|
|
|
text += argexpr[i]->dump();
|
|
|
|
}
|
2009-06-21 12:59:53 +04:00
|
|
|
if (children.size() == 0) {
|
|
|
|
text += QString(");\n");
|
|
|
|
} else if (children.size() == 1) {
|
|
|
|
text += QString(")\n");
|
|
|
|
text += children[0]->dump(indent + QString("\t"));
|
|
|
|
} else {
|
|
|
|
text += QString(") {\n");
|
|
|
|
for (int i = 0; i < children.size(); i++) {
|
|
|
|
text += children[i]->dump(indent + QString("\t"));
|
|
|
|
}
|
|
|
|
text += QString("%1}\n").arg(indent);
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
AbstractNode *ModuleInstanciation::evaluate(const Context *ctx) const
|
2009-06-21 12:59:53 +04:00
|
|
|
{
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *node = NULL;
|
|
|
|
if (this->ctx) {
|
|
|
|
PRINTA("WARNING: Ignoring recursive module instanciation of '%1'.", modname);
|
|
|
|
} else {
|
|
|
|
ModuleInstanciation *that = (ModuleInstanciation*)this;
|
|
|
|
that->argvalues.clear();
|
|
|
|
foreach (Expression *v, that->argexpr) {
|
|
|
|
that->argvalues.append(v->evaluate(ctx));
|
|
|
|
}
|
|
|
|
that->ctx = ctx;
|
|
|
|
node = ctx->evaluate_module(this);
|
|
|
|
that->ctx = NULL;
|
|
|
|
that->argvalues.clear();
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
2009-07-01 13:48:25 +04:00
|
|
|
return node;
|
2009-06-21 12:59:53 +04:00
|
|
|
}
|
|
|
|
|
2009-06-20 23:00:19 +04:00
|
|
|
Module::~Module()
|
|
|
|
{
|
2009-06-21 15:41:11 +04:00
|
|
|
foreach (Expression *v, assignments_expr)
|
2009-06-20 23:00:19 +04:00
|
|
|
delete v;
|
|
|
|
foreach (AbstractFunction *v, functions)
|
|
|
|
delete v;
|
|
|
|
foreach (AbstractModule *v, modules)
|
|
|
|
delete v;
|
2009-06-21 12:59:53 +04:00
|
|
|
foreach (ModuleInstanciation *v, children)
|
|
|
|
delete v;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *Module::evaluate(const Context *ctx, const ModuleInstanciation *inst) const
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
|
|
|
Context c(ctx);
|
2009-07-01 13:48:25 +04:00
|
|
|
c.args(argnames, argexpr, inst->argnames, inst->argvalues);
|
2009-06-20 23:00:19 +04:00
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
c.functions_p = &functions;
|
|
|
|
c.modules_p = &modules;
|
|
|
|
|
|
|
|
for (int i = 0; i < assignments_var.size(); i++) {
|
2009-06-26 08:32:52 +04:00
|
|
|
c.set_variable(assignments_var[i], assignments_expr[i]->evaluate(&c));
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
|
|
|
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *node = new AbstractNode(inst);
|
2009-06-21 15:41:11 +04:00
|
|
|
for (int i = 0; i < children.size(); i++) {
|
2009-06-27 11:31:40 +04:00
|
|
|
AbstractNode *n = children[i]->evaluate(&c);
|
|
|
|
if (n != NULL)
|
|
|
|
node->children.append(n);
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
|
|
|
|
2009-07-01 13:48:25 +04:00
|
|
|
foreach(ModuleInstanciation *v, inst->children) {
|
|
|
|
AbstractNode *n = v->evaluate(inst->ctx);
|
2009-06-30 00:30:43 +04:00
|
|
|
if (n != NULL)
|
|
|
|
node->children.append(n);
|
2009-06-23 14:31:25 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
return node;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
QString Module::dump(QString indent, QString name) const
|
|
|
|
{
|
2009-06-23 23:56:46 +04:00
|
|
|
QString text, tab;
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
text = QString("%1module %2(").arg(indent, name);
|
|
|
|
for (int i=0; i < argnames.size(); i++) {
|
|
|
|
if (i > 0)
|
|
|
|
text += QString(", ");
|
|
|
|
text += argnames[i];
|
|
|
|
if (argexpr[i])
|
|
|
|
text += QString(" = ") + argexpr[i]->dump();
|
|
|
|
}
|
|
|
|
text += QString(") {\n");
|
|
|
|
tab = "\t";
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
QHashIterator<QString, AbstractFunction*> i(functions);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
2009-06-23 23:56:46 +04:00
|
|
|
text += i.value()->dump(indent + tab, i.key());
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QHashIterator<QString, AbstractModule*> i(modules);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
2009-06-23 23:56:46 +04:00
|
|
|
text += i.value()->dump(indent + tab, i.key());
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
}
|
2009-06-21 15:41:11 +04:00
|
|
|
for (int i = 0; i < assignments_var.size(); i++) {
|
2009-06-23 23:56:46 +04:00
|
|
|
text += QString("%1%2 = %3;\n").arg(indent + tab, assignments_var[i], assignments_expr[i]->dump());
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
2009-06-21 10:53:46 +04:00
|
|
|
for (int i = 0; i < children.size(); i++) {
|
2009-06-23 23:56:46 +04:00
|
|
|
text += children[i]->dump(indent + tab);
|
|
|
|
}
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
text += QString("%1}\n").arg(indent);
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2009-06-20 23:00:19 +04:00
|
|
|
QHash<QString, AbstractModule*> builtin_modules;
|
|
|
|
|
|
|
|
void initialize_builtin_modules()
|
|
|
|
{
|
2009-06-21 15:41:11 +04:00
|
|
|
builtin_modules["group"] = new AbstractModule();
|
|
|
|
|
2009-06-30 00:30:43 +04:00
|
|
|
register_builtin_csgops();
|
|
|
|
register_builtin_transform();
|
|
|
|
register_builtin_primitives();
|
2009-07-25 06:37:21 +04:00
|
|
|
register_builtin_surface();
|
2009-06-30 00:30:43 +04:00
|
|
|
register_builtin_control();
|
2009-07-14 09:25:55 +04:00
|
|
|
register_builtin_render();
|
2009-10-14 16:34:50 +04:00
|
|
|
register_builtin_import();
|
2009-07-17 03:11:08 +04:00
|
|
|
register_builtin_dxf_linear_extrude();
|
2009-07-17 04:34:15 +04:00
|
|
|
register_builtin_dxf_rotate_extrude();
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroy_builtin_modules()
|
|
|
|
{
|
|
|
|
foreach (AbstractModule *v, builtin_modules)
|
|
|
|
delete v;
|
|
|
|
builtin_modules.clear();
|
|
|
|
}
|
|
|
|
|
2009-06-24 14:59:15 +04:00
|
|
|
int AbstractNode::idx_counter;
|
|
|
|
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode::AbstractNode(const ModuleInstanciation *mi)
|
2009-06-24 14:59:15 +04:00
|
|
|
{
|
2009-07-01 13:48:25 +04:00
|
|
|
modinst = mi;
|
2009-06-24 14:59:15 +04:00
|
|
|
idx = idx_counter++;
|
|
|
|
}
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
AbstractNode::~AbstractNode()
|
|
|
|
{
|
|
|
|
foreach (AbstractNode *v, children)
|
|
|
|
delete v;
|
|
|
|
}
|
|
|
|
|
2009-07-28 16:53:52 +04:00
|
|
|
QString AbstractNode::mk_cache_id() const
|
2009-07-03 11:51:22 +04:00
|
|
|
{
|
2009-07-14 09:25:55 +04:00
|
|
|
QString cache_id = dump("");
|
|
|
|
cache_id.remove(QRegExp("[a-zA-Z_][a-zA-Z_0-9]*:"));
|
|
|
|
cache_id.remove(' ');
|
|
|
|
cache_id.remove('\t');
|
|
|
|
cache_id.remove('\n');
|
2009-07-03 11:51:22 +04:00
|
|
|
return cache_id;
|
|
|
|
}
|
|
|
|
|
2009-07-28 16:53:52 +04:00
|
|
|
#ifdef ENABLE_CGAL
|
|
|
|
|
|
|
|
QCache<QString, CGAL_Nef_polyhedron> AbstractNode::cgal_nef_cache(100000);
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
CGAL_Nef_polyhedron AbstractNode::render_cgal_nef_polyhedron() const
|
|
|
|
{
|
2009-07-28 16:53:52 +04:00
|
|
|
QString cache_id = mk_cache_id();
|
2009-07-03 11:51:22 +04:00
|
|
|
if (cgal_nef_cache.contains(cache_id)) {
|
|
|
|
progress_report();
|
|
|
|
return *cgal_nef_cache[cache_id];
|
|
|
|
}
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
CGAL_Nef_polyhedron N;
|
2009-07-24 12:22:05 +04:00
|
|
|
foreach (AbstractNode *v, children) {
|
|
|
|
if (v->modinst->tag_background)
|
|
|
|
continue;
|
2009-06-21 15:41:11 +04:00
|
|
|
N += v->render_cgal_nef_polyhedron();
|
2009-07-24 12:22:05 +04:00
|
|
|
}
|
2009-07-03 11:51:22 +04:00
|
|
|
|
|
|
|
cgal_nef_cache.insert(cache_id, new CGAL_Nef_polyhedron(N), N.number_of_vertices());
|
2009-06-21 16:43:52 +04:00
|
|
|
progress_report();
|
2009-06-21 15:41:11 +04:00
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2009-06-23 23:56:46 +04:00
|
|
|
#endif /* ENABLE_CGAL */
|
|
|
|
|
2009-07-23 16:05:05 +04:00
|
|
|
CSGTerm *AbstractNode::render_csg_term(double m[16], QVector<CSGTerm*> *highlights, QVector<CSGTerm*> *background) const
|
2009-06-24 14:59:15 +04:00
|
|
|
{
|
|
|
|
CSGTerm *t1 = NULL;
|
2009-07-01 15:09:19 +04:00
|
|
|
foreach(AbstractNode *v, children) {
|
2009-07-23 16:05:05 +04:00
|
|
|
CSGTerm *t2 = v->render_csg_term(m, highlights, background);
|
2009-06-24 14:59:15 +04:00
|
|
|
if (t2 && !t1) {
|
|
|
|
t1 = t2;
|
|
|
|
} else if (t2 && t1) {
|
2009-08-04 22:55:25 +04:00
|
|
|
t1 = new CSGTerm(CSGTerm::TYPE_UNION, t1, t2);
|
2009-06-24 14:59:15 +04:00
|
|
|
}
|
|
|
|
}
|
2009-07-23 16:05:05 +04:00
|
|
|
if (t1 && modinst->tag_highlight && highlights)
|
2009-07-01 15:09:19 +04:00
|
|
|
highlights->append(t1->link());
|
2009-07-23 16:05:05 +04:00
|
|
|
if (t1 && modinst->tag_background && background) {
|
|
|
|
background->append(t1);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-06-24 14:59:15 +04:00
|
|
|
return t1;
|
|
|
|
}
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
QString AbstractNode::dump(QString indent) const
|
|
|
|
{
|
2009-07-03 11:51:22 +04:00
|
|
|
if (dump_cache.isEmpty()) {
|
|
|
|
QString text = indent + QString("n%1: group() {\n").arg(idx);
|
|
|
|
foreach (AbstractNode *v, children)
|
|
|
|
text += v->dump(indent + QString("\t"));
|
|
|
|
((AbstractNode*)this)->dump_cache = text + indent + "}\n";
|
|
|
|
}
|
|
|
|
return dump_cache;
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 16:43:52 +04:00
|
|
|
int progress_report_count;
|
|
|
|
void (*progress_report_f)(const class AbstractNode*, void*, int);
|
|
|
|
void *progress_report_vp;
|
|
|
|
|
|
|
|
void AbstractNode::progress_prepare()
|
|
|
|
{
|
|
|
|
foreach (AbstractNode *v, children)
|
|
|
|
v->progress_prepare();
|
|
|
|
progress_mark = ++progress_report_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractNode::progress_report() const
|
|
|
|
{
|
|
|
|
if (progress_report_f)
|
|
|
|
progress_report_f(this, progress_report_vp, progress_mark);
|
|
|
|
}
|
|
|
|
|
|
|
|
void progress_report_prep(AbstractNode *root, void (*f)(const class AbstractNode *node, void *vp, int mark), void *vp)
|
|
|
|
{
|
|
|
|
progress_report_count = 0;
|
|
|
|
progress_report_f = f;
|
|
|
|
progress_report_vp = vp;
|
|
|
|
root->progress_prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
void progress_report_fin()
|
|
|
|
{
|
|
|
|
progress_report_count = 0;
|
|
|
|
progress_report_f = NULL;
|
|
|
|
progress_report_vp = NULL;
|
|
|
|
}
|
|
|
|
|