summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarius Kintel <marius@kintel.net>2010-03-28 15:57:39 (GMT)
committerMarius Kintel <marius@kintel.net>2010-10-31 00:42:34 (GMT)
commit7468c940672b31e569cbb2310676fb1ac841e2d0 (patch)
tree1ddd374a0006e074695bd8638d1f5e59db6a3509
parent3396706e248d4baccf08707103deae378181b299 (diff)
Mostly completed test implementation of text-based CGALRenderer
-rw-r--r--openscad.pro2
-rw-r--r--src/CGALRenderer.cc150
-rw-r--r--src/CGALRenderer.h2
-rw-r--r--src/cgaladv.cc1
-rw-r--r--src/csgops.cc1
-rw-r--r--src/import.cc1
-rw-r--r--src/nodedumper.cc1
-rw-r--r--src/primitives.cc1
-rw-r--r--src/render.cc21
-rw-r--r--src/rendernode.h29
-rw-r--r--src/transform.cc21
-rw-r--r--src/transformnode.h29
-rw-r--r--test-code/cgaltest.pro2
13 files changed, 156 insertions, 105 deletions
diff --git a/openscad.pro b/openscad.pro
index 496250a..2a1bdf7 100644
--- a/openscad.pro
+++ b/openscad.pro
@@ -105,6 +105,8 @@ HEADERS += src/CGAL_renderer.h \
src/dxfrotextrudenode.h \
src/projectionnode.h \
src/importnode.h \
+ src/transformnode.h \
+ src/rendernode.h \
src/openscad.h \
src/polyset.h \
src/printutils.h \
diff --git a/src/CGALRenderer.cc b/src/CGALRenderer.cc
index 5e8accb..2926023 100644
--- a/src/CGALRenderer.cc
+++ b/src/CGALRenderer.cc
@@ -8,10 +8,11 @@
#include "module.h" // FIXME: Temporarily for ModuleInstantiation
#include "csgnode.h"
-
+#include "transformnode.h"
#include <sstream>
#include <iostream>
+#include <assert.h>
#include <QRegExp>
string CGALRenderer::getCGALMesh() const
@@ -105,83 +106,98 @@ CGALRenderer::process(string &target, const string &src, CGALRenderer::CsgOp op)
void CGALRenderer::applyToChildren(const AbstractNode &node, CGALRenderer::CsgOp op)
{
- // FIXME: assert that cache contains nodes in code below
- bool first = true;
+ std::stringstream stream;
+ stream << typeid(node).name();
+ stream << "<" << node.index() << ">";
+ string N = stream.str();
+ if (this->visitedchildren[node.index()].size() > 0) {
+ // FIXME: assert that cache contains nodes in code below
+ bool first = true;
// CGAL_Nef_polyhedron N;
- string N;
- for (ChildList::const_iterator iter = this->visitedchildren[node.index()].begin();
- iter != this->visitedchildren[node.index()].end();
- iter++) {
- const AbstractNode *chnode = iter->first;
- const QString &chcacheid = iter->second;
- // FIXME: Don't use deep access to modinst members
- if (chnode->modinst->tag_background) continue;
- if (first) {
- N = "(" + this->cache[chcacheid];
+ for (ChildList::const_iterator iter = this->visitedchildren[node.index()].begin();
+ iter != this->visitedchildren[node.index()].end();
+ iter++) {
+ const AbstractNode *chnode = iter->first;
+ const QString &chcacheid = iter->second;
+ // FIXME: Don't use deep access to modinst members
+ if (chnode->modinst->tag_background) continue;
+ if (first) {
+ N += "(" + this->cache[chcacheid];
// if (N.dim != 0) first = false; // FIXME: when can this happen?
- first = false;
- } else {
- process(N, this->cache[chcacheid], op);
+ first = false;
+ } else {
+ process(N, this->cache[chcacheid], op);
+ }
+ chnode->progress_report();
}
- chnode->progress_report();
+ N += ")";
}
- N += ")";
QString cacheid = mk_cache_id(node);
this->cache.insert(cacheid, N);
}
+/*
+ Typical visitor behavior:
+ o In prefix: Check if we're cached -> prune
+ o In postfix: Check if we're cached -> don't apply operator to children
+ o In postfix: addToParent()
+ */
+
Response CGALRenderer::visit(const State &state, const AbstractNode &node)
{
- if (isCached(node)) return PruneTraversal;
-
+ if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
- applyToChildren(node, UNION);
+ if (!isCached(node)) applyToChildren(node, UNION);
+ addToParent(state, node);
}
-
- handleVisitedChildren(state, node);
return ContinueTraversal;
}
Response CGALRenderer::visit(const State &state, const AbstractIntersectionNode &node)
{
- if (isCached(node)) return PruneTraversal;
-
+ if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
- applyToChildren(node, INTERSECTION);
+ if (!isCached(node)) applyToChildren(node, INTERSECTION);
+ addToParent(state, node);
}
-
- handleVisitedChildren(state, node);
return ContinueTraversal;
}
Response CGALRenderer::visit(const State &state, const CsgNode &node)
{
- if (isCached(node)) return PruneTraversal;
-
- CsgOp op;
- switch (node.type) {
- case CSG_TYPE_UNION:
- op = UNION;
- break;
- case CSG_TYPE_DIFFERENCE:
- op = DIFFERENCE;
- break;
- case CSG_TYPE_INTERSECTION:
- op = INTERSECTION;
- break;
- }
-
+ if (state.isPrefix() && isCached(node)) return PruneTraversal;
if (state.isPostfix()) {
- applyToChildren(node, op);
+ if (!isCached(node)) {
+ CsgOp op;
+ switch (node.type) {
+ case CSG_TYPE_UNION:
+ op = UNION;
+ break;
+ case CSG_TYPE_DIFFERENCE:
+ op = DIFFERENCE;
+ break;
+ case CSG_TYPE_INTERSECTION:
+ op = INTERSECTION;
+ break;
+ }
+ applyToChildren(node, op);
+ }
+ addToParent(state, node);
}
-
- handleVisitedChildren(state, node);
return ContinueTraversal;
}
Response CGALRenderer::visit(const State &state, const TransformNode &node)
{
- // FIXME: First union, then 2D/3D transform
+ if (state.isPrefix() && isCached(node)) return PruneTraversal;
+ if (state.isPostfix()) {
+ if (!isCached(node)) {
+ // First union all children
+ applyToChildren(node, UNION);
+ // FIXME: Then apply transform
+ }
+ addToParent(state, node);
+ }
return ContinueTraversal;
}
@@ -196,6 +212,10 @@ Response CGALRenderer::visit(const State &state, const TransformNode &node)
// (PrimitiveNode)
Response CGALRenderer::visit(const State &state, const AbstractPolyNode &node)
{
+ if (state.isPrefix() && isCached(node)) return PruneTraversal;
+ if (state.isPostfix()) {
+ if (!isCached(node)) {
+
// FIXME: Manage caching
// FIXME: Will generate one single Nef polyhedron (no csg ops necessary)
@@ -214,31 +234,33 @@ Response CGALRenderer::visit(const State &state, const AbstractPolyNode &node)
// throw;
// }
- if (state.isPostfix()) {
- string N = "X";
- QString cacheid = mk_cache_id(node);
- this->cache.insert(cacheid, N);
+ string N = typeid(node).name();
+ QString cacheid = mk_cache_id(node);
+ this->cache.insert(cacheid, N);
- std::cout << "Insert: " << N << "\n";
- std::cout << "Node: " << cacheid.toStdString() << "\n\n";
+// std::cout << "Insert: " << N << "\n";
+// std::cout << "Node: " << cacheid.toStdString() << "\n\n";
+ }
+ addToParent(state, node);
}
- handleVisitedChildren(state, node);
-
return ContinueTraversal;
}
-void CGALRenderer::handleVisitedChildren(const State &state, const AbstractNode &node)
+/*!
+ Adds ourself to out parent's list of traversed children.
+ Call this for _every_ node which affects output during the postfix traversal.
+*/
+void CGALRenderer::addToParent(const State &state, const AbstractNode &node)
{
+ assert(state.isPostfix());
QString cacheid = mk_cache_id(node);
- if (state.isPostfix()) {
- this->visitedchildren.erase(node.index());
- if (!state.parent()) {
- this->root = &node;
- }
- else {
- this->visitedchildren[state.parent()->index()].push_back(std::make_pair(&node, cacheid));
- }
+ this->visitedchildren.erase(node.index());
+ if (!state.parent()) {
+ this->root = &node;
+ }
+ else {
+ this->visitedchildren[state.parent()->index()].push_back(std::make_pair(&node, cacheid));
}
}
diff --git a/src/CGALRenderer.h b/src/CGALRenderer.h
index b30a24b..3933823 100644
--- a/src/CGALRenderer.h
+++ b/src/CGALRenderer.h
@@ -28,7 +28,7 @@ public:
string getCGALMesh() const;
// CGAL_Nef_polyhedron getCGALMesh() const;
private:
- void handleVisitedChildren(const State &state, const AbstractNode &node);
+ void addToParent(const State &state, const AbstractNode &node);
bool isCached(const AbstractNode &node);
QString mk_cache_id(const AbstractNode &node) const;
void process(string &target, const string &src, CGALRenderer::CsgOp op);
diff --git a/src/cgaladv.cc b/src/cgaladv.cc
index 4a7ba31..193bc27 100644
--- a/src/cgaladv.cc
+++ b/src/cgaladv.cc
@@ -31,6 +31,7 @@
#include "cgal.h"
#include "visitor.h"
#include <sstream>
+#include <assert.h>
#ifdef ENABLE_CGAL
extern CGAL_Nef_polyhedron3 minkowski3(CGAL_Nef_polyhedron3 a, CGAL_Nef_polyhedron3 b);
diff --git a/src/csgops.cc b/src/csgops.cc
index da2c2e5..62859a5 100644
--- a/src/csgops.cc
+++ b/src/csgops.cc
@@ -30,6 +30,7 @@
#include "builtin.h"
#include "printutils.h"
#include <sstream>
+#include <assert.h>
class CsgModule : public AbstractModule
{
diff --git a/src/import.cc b/src/import.cc
index c977b53..99b7eeb 100644
--- a/src/import.cc
+++ b/src/import.cc
@@ -38,6 +38,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sstream>
+#include <assert.h>
class ImportModule : public AbstractModule
{
diff --git a/src/nodedumper.cc b/src/nodedumper.cc
index 0e66e5b..a8de089 100644
--- a/src/nodedumper.cc
+++ b/src/nodedumper.cc
@@ -8,6 +8,7 @@
#include <sstream>
#include <iostream>
+#include <assert.h>
// For compatibility with old dump() output
#define NODEDUMPER_COMPAT_MODE
diff --git a/src/primitives.cc b/src/primitives.cc
index 84e6a6d..4cfb1ce 100644
--- a/src/primitives.cc
+++ b/src/primitives.cc
@@ -34,6 +34,7 @@
#include <assert.h>
#include "visitor.h"
#include <sstream>
+#include <assert.h>
enum primitive_type_e {
CUBE,
diff --git a/src/render.cc b/src/render.cc
index fb7dd59..4778083 100644
--- a/src/render.cc
+++ b/src/render.cc
@@ -23,8 +23,8 @@
*
*/
+#include "rendernode.h"
#include "module.h"
-#include "node.h"
#include "polyset.h"
#include "context.h"
#include "dxfdata.h"
@@ -49,25 +49,6 @@ public:
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
};
-class RenderNode : public AbstractNode
-{
-public:
- RenderNode(const ModuleInstantiation *mi) : AbstractNode(mi), convexity(1) { }
- virtual Response accept(const class State &state, Visitor &visitor) const {
- return visitor.visit(state, *this);
- }
- virtual std::string toString() const;
-
- int convexity;
-#ifdef ENABLE_CGAL
- virtual CGAL_Nef_polyhedron renderCSGMesh() const;
-#endif
- CSGTerm *render_csg_term(double m[20], QVector<CSGTerm*> *highlights, QVector<CSGTerm*> *background) const;
-#ifndef REMOVE_DUMP
- virtual QString dump(QString indent) const;
-#endif
-};
-
AbstractNode *RenderModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const
{
RenderNode *node = new RenderNode(inst);
diff --git a/src/rendernode.h b/src/rendernode.h
new file mode 100644
index 0000000..8bb2c9c
--- /dev/null
+++ b/src/rendernode.h
@@ -0,0 +1,29 @@
+#ifndef RENDERNODE_H_
+#define RENDERNODE_H_
+
+#include "node.h"
+#include "visitor.h"
+#ifdef ENABLE_CGAL
+# include "cgal.h"
+#endif
+
+class RenderNode : public AbstractNode
+{
+public:
+ RenderNode(const ModuleInstantiation *mi) : AbstractNode(mi), convexity(1) { }
+ virtual Response accept(const class State &state, Visitor &visitor) const {
+ return visitor.visit(state, *this);
+ }
+ virtual std::string toString() const;
+
+ int convexity;
+#ifdef ENABLE_CGAL
+ virtual CGAL_Nef_polyhedron renderCSGMesh() const;
+#endif
+ CSGTerm *render_csg_term(double m[20], QVector<CSGTerm*> *highlights, QVector<CSGTerm*> *background) const;
+#ifndef REMOVE_DUMP
+ virtual QString dump(QString indent) const;
+#endif
+};
+
+#endif
diff --git a/src/transform.cc b/src/transform.cc
index d49f3ff..f6e49c2 100644
--- a/src/transform.cc
+++ b/src/transform.cc
@@ -23,8 +23,8 @@
*
*/
+#include "transformnode.h"
#include "module.h"
-#include "node.h"
#include "context.h"
#include "dxfdata.h"
#include "csgterm.h"
@@ -52,25 +52,6 @@ public:
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
};
-class TransformNode : public AbstractNode
-{
-public:
- TransformNode(const ModuleInstantiation *mi) : AbstractNode(mi) { }
- virtual Response accept(const class State &state, Visitor &visitor) const {
- return visitor.visit(state, *this);
- }
- virtual std::string toString() const;
-
- double m[20];
-#ifdef ENABLE_CGAL
- virtual CGAL_Nef_polyhedron renderCSGMesh() const;
-#endif
- virtual CSGTerm *render_csg_term(double m[20], QVector<CSGTerm*> *highlights, QVector<CSGTerm*> *background) const;
-#ifndef REMOVE_DUMP
- virtual QString dump(QString indent) const;
-#endif
-};
-
AbstractNode *TransformModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const
{
TransformNode *node = new TransformNode(inst);
diff --git a/src/transformnode.h b/src/transformnode.h
new file mode 100644
index 0000000..c262afd
--- /dev/null
+++ b/src/transformnode.h
@@ -0,0 +1,29 @@
+#ifndef TRANSFORMNODE_H_
+#define TRANSFORMNODE_H_
+
+#include "node.h"
+#include "visitor.h"
+#ifdef ENABLE_CGAL
+# include "cgal.h"
+#endif
+
+class TransformNode : public AbstractNode
+{
+public:
+ TransformNode(const ModuleInstantiation *mi) : AbstractNode(mi) { }
+ virtual Response accept(const class State &state, Visitor &visitor) const {
+ return visitor.visit(state, *this);
+ }
+ virtual std::string toString() const;
+
+ double m[20];
+#ifdef ENABLE_CGAL
+ virtual CGAL_Nef_polyhedron renderCSGMesh() const;
+#endif
+ virtual CSGTerm *render_csg_term(double m[20], QVector<CSGTerm*> *highlights, QVector<CSGTerm*> *background) const;
+#ifndef REMOVE_DUMP
+ virtual QString dump(QString indent) const;
+#endif
+};
+
+#endif
diff --git a/test-code/cgaltest.pro b/test-code/cgaltest.pro
index 8b68937..94942da 100644
--- a/test-code/cgaltest.pro
+++ b/test-code/cgaltest.pro
@@ -52,6 +52,8 @@ HEADERS += ../src/builtin.h \
../src/projectionnode.h \
../src/importnode.h \
../src/csgnode.h \
+ ../src/transformnode.h \
+ ../src/rendernode.h \
../src/openscad.h \
../src/polyset.h \
../src/printutils.h \
contact: Jan Huwald // Impressum