diff options
author | Marius Kintel <marius@kintel.net> | 2011-09-09 03:53:05 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2011-09-09 03:53:05 (GMT) |
commit | cbba974d3ac1edeb716a1384a2262ed5447fa9e6 (patch) | |
tree | 30708ef085204435d644c4ab685b87556628d22a | |
parent | 328897c1f28e0d438aa678891f8d5a45b114f267 (diff) |
Initial attempt of cleaning up polyset handling. PolySet no longer keeps a refcount, basic cache mechanism is in place, instantiating polysets are controlled through PolySetEvaluator
36 files changed, 159 insertions, 168 deletions
diff --git a/src/CGALEvaluator.cc b/src/CGALEvaluator.cc index 34911b1..6bd7092 100644 --- a/src/CGALEvaluator.cc +++ b/src/CGALEvaluator.cc @@ -216,7 +216,6 @@ Response CGALEvaluator::visit(State &state, const TransformNode &node) dxf_tesselate(&ps, *dd, 0, true, false, 0); N = evaluateCGALMesh(ps); - ps.refcount = 0; delete dd; } else if (N.dim == 3) { @@ -240,18 +239,15 @@ Response CGALEvaluator::visit(State &state, const AbstractPolyNode &node) if (state.isPostfix()) { if (!isCached(node)) { // Apply polyset operation - PolySet *ps = node.evaluate_polyset(AbstractPolyNode::RENDER_CGAL, &this->psevaluator); + PolySet *ps = this->psevaluator.getPolySet(node); CGAL_Nef_polyhedron N; if (ps) { try { N = evaluateCGALMesh(*ps); // print_messages_pop(); node.progress_report(); - - ps->unlink(); } - catch (...) { // Don't leak the PolySet on ProgressCancelException - ps->unlink(); + catch (...) { throw; } } @@ -316,7 +312,7 @@ CGAL_Nef_polyhedron CGALEvaluator::evaluateCGALMesh(const AbstractPolyNode &node // print_messages_push(); - PolySet *ps = node.evaluate_polyset(AbstractPolyNode::RENDER_CGAL); + PolySet *ps = this->psevaluator->getPolySet(node); if (ps) { try { CGAL_Nef_polyhedron N = ps->evaluateCSGMesh(); diff --git a/src/CGALEvaluator.h b/src/CGALEvaluator.h index 2453c25..a8e9844 100644 --- a/src/CGALEvaluator.h +++ b/src/CGALEvaluator.h @@ -49,6 +49,9 @@ private: QHash<string, CGAL_Nef_polyhedron> &cache; const Tree &tree; +public: + // FIXME: Do we need to make this visible? Used for cache management + // Note: psevaluator constructor needs this->tree to be initialized first PolySetCGALEvaluator psevaluator; }; diff --git a/src/CGALRenderer.cc b/src/CGALRenderer.cc index f8e914b..4d165ce 100644 --- a/src/CGALRenderer.cc +++ b/src/CGALRenderer.cc @@ -68,7 +68,7 @@ CGALRenderer::CGALRenderer(const CGAL_Nef_polyhedron &root) : root(root) CGALRenderer::~CGALRenderer() { - if (this->polyset) this->polyset->unlink(); + delete this->polyset; delete this->polyhedron; } diff --git a/src/CSGTermEvaluator.cc b/src/CSGTermEvaluator.cc index dafa6a6..40e46b9 100644 --- a/src/CSGTermEvaluator.cc +++ b/src/CSGTermEvaluator.cc @@ -108,10 +108,12 @@ Response CSGTermEvaluator::visit(State &state, const AbstractPolyNode &node) { if (state.isPostfix()) { CSGTerm *t1 = NULL; - PolySet *ps = node.evaluate_polyset(AbstractPolyNode::RENDER_OPENCSG, this->psevaluator); - if (ps) { - t1 = evaluate_csg_term_from_ps(state, this->highlights, this->background, - ps, node.modinst, node); + if (this->psevaluator) { + PolySet *ps = this->psevaluator->getPolySet(node); + if (ps) { + t1 = evaluate_csg_term_from_ps(state, this->highlights, this->background, + ps, node.modinst, node); + } } this->stored_term[node.index()] = t1; addToParent(state, node); @@ -182,10 +184,9 @@ Response CSGTermEvaluator::visit(State &state, const RenderNode &node) { if (state.isPostfix()) { CSGTerm *t1 = NULL; - // FIXME: Calling evaluator directly since we're not a PolyNode. Generalize this. PolySet *ps = NULL; if (this->psevaluator) { - ps = this->psevaluator->evaluatePolySet(node, AbstractPolyNode::RENDER_OPENCSG); + ps = this->psevaluator->getPolySet(node); } if (ps) { t1 = evaluate_csg_term_from_ps(state, this->highlights, this->background, @@ -204,7 +205,7 @@ Response CSGTermEvaluator::visit(State &state, const CgaladvNode &node) // FIXME: Calling evaluator directly since we're not a PolyNode. Generalize this. PolySet *ps = NULL; if (this->psevaluator) { - ps = this->psevaluator->evaluatePolySet(node, AbstractPolyNode::RENDER_OPENCSG); + ps = this->psevaluator->getPolySet(node); } if (ps) { t1 = evaluate_csg_term_from_ps(state, this->highlights, this->background, diff --git a/src/PolySetCGALEvaluator.cc b/src/PolySetCGALEvaluator.cc index a648587..9f9ab6e 100644 --- a/src/PolySetCGALEvaluator.cc +++ b/src/PolySetCGALEvaluator.cc @@ -1,5 +1,6 @@ #include "PolySetCGALEvaluator.h" #include "cgal.h" +#include "cgalutils.h" #include "polyset.h" #include "CGALEvaluator.h" #include "projectionnode.h" @@ -15,11 +16,13 @@ #include "openscad.h" // get_fragments_from_r() #include <boost/foreach.hpp> -PolySet *PolySetCGALEvaluator::evaluatePolySet(const ProjectionNode &node, AbstractPolyNode::render_mode_e) +PolySetCGALEvaluator::PolySetCGALEvaluator(CGALEvaluator &cgalevaluator) + : PolySetEvaluator(cgalevaluator.getTree()), cgalevaluator(cgalevaluator) { - const string &cacheid = this->cgalevaluator.getTree().getString(node); - if (this->cache.contains(cacheid)) return this->cache[cacheid]->ps->link(); +} +PolySet *PolySetCGALEvaluator::evaluatePolySet(const ProjectionNode &node) +{ // Before projecting, union all children CGAL_Nef_polyhedron sum; BOOST_FOREACH (AbstractNode * v, node.getChildren()) { @@ -38,47 +41,46 @@ PolySet *PolySetCGALEvaluator::evaluatePolySet(const ProjectionNode &node, Abstr if (node.cut_mode) { - PolySet *cube = new PolySet(); + PolySet cube; double infval = 1e8, eps = 0.1; double x1 = -infval, x2 = +infval, y1 = -infval, y2 = +infval, z1 = 0, z2 = eps; - cube->append_poly(); // top - cube->append_vertex(x1, y1, z2); - cube->append_vertex(x2, y1, z2); - cube->append_vertex(x2, y2, z2); - cube->append_vertex(x1, y2, z2); - - cube->append_poly(); // bottom - cube->append_vertex(x1, y2, z1); - cube->append_vertex(x2, y2, z1); - cube->append_vertex(x2, y1, z1); - cube->append_vertex(x1, y1, z1); - - cube->append_poly(); // side1 - cube->append_vertex(x1, y1, z1); - cube->append_vertex(x2, y1, z1); - cube->append_vertex(x2, y1, z2); - cube->append_vertex(x1, y1, z2); - - cube->append_poly(); // side2 - cube->append_vertex(x2, y1, z1); - cube->append_vertex(x2, y2, z1); - cube->append_vertex(x2, y2, z2); - cube->append_vertex(x2, y1, z2); - - cube->append_poly(); // side3 - cube->append_vertex(x2, y2, z1); - cube->append_vertex(x1, y2, z1); - cube->append_vertex(x1, y2, z2); - cube->append_vertex(x2, y2, z2); - - cube->append_poly(); // side4 - cube->append_vertex(x1, y2, z1); - cube->append_vertex(x1, y1, z1); - cube->append_vertex(x1, y1, z2); - cube->append_vertex(x1, y2, z2); - CGAL_Nef_polyhedron Ncube = this->cgalevaluator.evaluateCGALMesh(*cube); - cube->unlink(); + cube.append_poly(); // top + cube.append_vertex(x1, y1, z2); + cube.append_vertex(x2, y1, z2); + cube.append_vertex(x2, y2, z2); + cube.append_vertex(x1, y2, z2); + + cube.append_poly(); // bottom + cube.append_vertex(x1, y2, z1); + cube.append_vertex(x2, y2, z1); + cube.append_vertex(x2, y1, z1); + cube.append_vertex(x1, y1, z1); + + cube.append_poly(); // side1 + cube.append_vertex(x1, y1, z1); + cube.append_vertex(x2, y1, z1); + cube.append_vertex(x2, y1, z2); + cube.append_vertex(x1, y1, z2); + + cube.append_poly(); // side2 + cube.append_vertex(x2, y1, z1); + cube.append_vertex(x2, y2, z1); + cube.append_vertex(x2, y2, z2); + cube.append_vertex(x2, y1, z2); + + cube.append_poly(); // side3 + cube.append_vertex(x2, y2, z1); + cube.append_vertex(x1, y2, z1); + cube.append_vertex(x1, y2, z2); + cube.append_vertex(x2, y2, z2); + + cube.append_poly(); // side4 + cube.append_vertex(x1, y2, z1); + cube.append_vertex(x1, y1, z1); + cube.append_vertex(x1, y1, z2); + cube.append_vertex(x1, y2, z2); + CGAL_Nef_polyhedron Ncube = this->cgalevaluator.evaluateCGALMesh(cube); // N.p3 *= CGAL_Nef_polyhedron3(CGAL_Plane(0, 0, 1, 0), CGAL_Nef_polyhedron3::INCLUDED); sum *= Ncube; @@ -109,7 +111,7 @@ PolySet *PolySetCGALEvaluator::evaluatePolySet(const ProjectionNode &node, Abstr } next_ps3_polygon_cut_mode:; } - ps3->unlink(); + delete ps3; } else { @@ -166,16 +168,14 @@ PolySet *PolySetCGALEvaluator::evaluatePolySet(const ProjectionNode &node, Abstr (*np.p2) += CGAL_Nef_polyhedron2(plist.begin(), plist.end(), CGAL_Nef_polyhedron2::INCLUDED); } } + delete ps3; DxfData *dxf = np.convertToDxfData(); dxf_tesselate(ps, *dxf, 0, true, false, 0); dxf_border_to_ps(ps, *dxf); - ps3->unlink(); delete dxf; } cant_project_non_simple_polyhedron: - - this->cache.insert(cacheid, new cache_entry(ps->link())); return ps; } @@ -251,12 +251,8 @@ static void add_slice(PolySet *ps, const DxfData &dxf, DxfData::Path &path, doub } } -PolySet *PolySetCGALEvaluator::evaluatePolySet(const DxfLinearExtrudeNode &node, - AbstractPolyNode::render_mode_e) +PolySet *PolySetCGALEvaluator::evaluatePolySet(const DxfLinearExtrudeNode &node) { - const string &cacheid = this->cgalevaluator.getTree().getString(node); - if (this->cache.contains(cacheid)) return this->cache[cacheid]->ps->link(); - DxfData *dxf; if (node.filename.empty()) @@ -283,7 +279,6 @@ PolySet *PolySetCGALEvaluator::evaluatePolySet(const DxfLinearExtrudeNode &node, } PolySet *ps = extrudeDxfData(node, *dxf); - this->cache.insert(cacheid, new cache_entry(ps->link())); delete dxf; return ps; } @@ -354,12 +349,8 @@ PolySet *PolySetCGALEvaluator::extrudeDxfData(const DxfLinearExtrudeNode &node, return ps; } -PolySet *PolySetCGALEvaluator::evaluatePolySet(const DxfRotateExtrudeNode &node, - AbstractPolyNode::render_mode_e) +PolySet *PolySetCGALEvaluator::evaluatePolySet(const DxfRotateExtrudeNode &node) { - const string &cacheid = this->cgalevaluator.getTree().getString(node); - if (this->cache.contains(cacheid)) return this->cache[cacheid]->ps->link(); - DxfData *dxf; if (node.filename.empty()) @@ -386,20 +377,20 @@ PolySet *PolySetCGALEvaluator::evaluatePolySet(const DxfRotateExtrudeNode &node, } PolySet *ps = rotateDxfData(node, *dxf); - this->cache.insert(cacheid, new cache_entry(ps->link())); delete dxf; return ps; } -PolySet *PolySetCGALEvaluator::evaluatePolySet(const CgaladvNode &node, AbstractPolyNode::render_mode_e) +PolySet *PolySetCGALEvaluator::evaluatePolySet(const CgaladvNode &node) { CGAL_Nef_polyhedron N = this->cgalevaluator.evaluateCGALMesh(node); PolySet *ps = NULL; if (!N.empty()) ps = N.convertToPolyset(); + return ps; } -PolySet *PolySetCGALEvaluator::evaluatePolySet(const RenderNode &node, AbstractPolyNode::render_mode_e) +PolySet *PolySetCGALEvaluator::evaluatePolySet(const RenderNode &node) { CGAL_Nef_polyhedron N = this->cgalevaluator.evaluateCGALMesh(node); PolySet *ps = NULL; @@ -479,6 +470,6 @@ PolySet *PolySetCGALEvaluator::rotateDxfData(const DxfRotateExtrudeNode &node, D } delete[] points; } - + return ps; } diff --git a/src/PolySetCGALEvaluator.h b/src/PolySetCGALEvaluator.h index f5b8665..2aa5b13 100644 --- a/src/PolySetCGALEvaluator.h +++ b/src/PolySetCGALEvaluator.h @@ -10,14 +10,13 @@ class PolySetCGALEvaluator : public PolySetEvaluator { public: - PolySetCGALEvaluator(class CGALEvaluator &CGALEvaluator) : - PolySetEvaluator(), cgalevaluator(CGALEvaluator) { } + PolySetCGALEvaluator(class CGALEvaluator &cgalevaluator); virtual ~PolySetCGALEvaluator() { } - virtual PolySet *evaluatePolySet(const ProjectionNode &node, AbstractPolyNode::render_mode_e); - virtual PolySet *evaluatePolySet(const DxfLinearExtrudeNode &node, AbstractPolyNode::render_mode_e); - virtual PolySet *evaluatePolySet(const DxfRotateExtrudeNode &node, AbstractPolyNode::render_mode_e); - virtual PolySet *evaluatePolySet(const CgaladvNode &node, AbstractPolyNode::render_mode_e); - virtual PolySet *evaluatePolySet(const RenderNode &node, AbstractPolyNode::render_mode_e); + virtual PolySet *evaluatePolySet(const ProjectionNode &node); + virtual PolySet *evaluatePolySet(const DxfLinearExtrudeNode &node); + virtual PolySet *evaluatePolySet(const DxfRotateExtrudeNode &node); + virtual PolySet *evaluatePolySet(const CgaladvNode &node); + virtual PolySet *evaluatePolySet(const RenderNode &node); protected: PolySet *extrudeDxfData(const DxfLinearExtrudeNode &node, class DxfData &dxf); diff --git a/src/PolySetEvaluator.cc b/src/PolySetEvaluator.cc index 56acb1d..2808686 100644 --- a/src/PolySetEvaluator.cc +++ b/src/PolySetEvaluator.cc @@ -2,14 +2,31 @@ #include "printutils.h" #include "polyset.h" -PolySetEvaluator *PolySetEvaluator::global_evaluator = NULL; +/*! + The task of PolySetEvaluator is to create, keep track of and cache PolySet instances. -PolySetEvaluator::cache_entry::cache_entry(PolySet *ps) : - ps(ps), msg(print_messages_stack.last()) + All instances of PolySet which are not strictly temporary should be requested through this + class. +*/ + +PolySet *PolySetEvaluator::getPolySet(const AbstractNode &node) +{ + const string &cacheid = this->tree.getString(node); + if (this->cache.contains(cacheid)) return this->cache[cacheid]->ps; + + PolySet *ps = node.evaluate_polyset(this); + this->cache.insert(cacheid, new cache_entry(ps), ps?ps->polygons.size():0); + return ps; +} + +PolySetEvaluator::cache_entry::cache_entry(PolySet *ps) + : ps(ps) { + if (print_messages_stack.size() > 0) this->msg = print_messages_stack.last(); } -PolySetEvaluator::cache_entry::~cache_entry() +void PolySetEvaluator::printCache() { - ps->unlink(); + PRINTF("PolySets in cache: %d", this->cache.size()); + PRINTF("Polygons in cache: %d", this->cache.totalCost()); } diff --git a/src/PolySetEvaluator.h b/src/PolySetEvaluator.h index 70ec7ed..1a97cb5 100644 --- a/src/PolySetEvaluator.h +++ b/src/PolySetEvaluator.h @@ -3,39 +3,42 @@ #include "myqhash.h" #include "node.h" +#include "Tree.h" #include <QCache> class PolySetEvaluator { public: - enum EvaluateMode { EVALUATE_CGAL, EVALUATE_OPENCSG }; - PolySetEvaluator() : cache(100) {} - + PolySetEvaluator(const Tree &tree) : cache(100000), tree(tree) {} virtual ~PolySetEvaluator() {} - virtual PolySet *evaluatePolySet(const class ProjectionNode &, AbstractPolyNode::render_mode_e) = 0; - virtual PolySet *evaluatePolySet(const class DxfLinearExtrudeNode &, AbstractPolyNode::render_mode_e) = 0; - virtual PolySet *evaluatePolySet(const class DxfRotateExtrudeNode &, AbstractPolyNode::render_mode_e) = 0; - virtual PolySet *evaluatePolySet(const class CgaladvNode &, AbstractPolyNode::render_mode_e) = 0; - virtual PolySet *evaluatePolySet(const class RenderNode &, AbstractPolyNode::render_mode_e) = 0; + const Tree &getTree() const { return this->tree; } + + virtual PolySet *getPolySet(const class AbstractNode &); + + virtual PolySet *evaluatePolySet(const class ProjectionNode &) { return NULL; } + virtual PolySet *evaluatePolySet(const class DxfLinearExtrudeNode &) { return NULL; } + virtual PolySet *evaluatePolySet(const class DxfRotateExtrudeNode &) { return NULL; } + virtual PolySet *evaluatePolySet(const class CgaladvNode &) { return NULL; } + virtual PolySet *evaluatePolySet(const class RenderNode &) { return NULL; } void clearCache() { this->cache.clear(); } - + void printCache(); protected: struct cache_entry { class PolySet *ps; QString msg; cache_entry(PolySet *ps); - ~cache_entry(); + ~cache_entry() { } }; QCache<std::string, cache_entry> cache; private: - static PolySetEvaluator *global_evaluator; + const Tree &tree; }; #endif diff --git a/src/cgaladv.cc b/src/cgaladv.cc index 8ffd626..5014133 100644 --- a/src/cgaladv.cc +++ b/src/cgaladv.cc @@ -29,6 +29,7 @@ #include "context.h" #include "builtin.h" #include "printutils.h" +#include "PolySetEvaluator.h" #include <sstream> #include <assert.h> #include <boost/assign/std/vector.hpp> @@ -92,6 +93,11 @@ AbstractNode *CgaladvModule::evaluate(const Context *ctx, const ModuleInstantiat return node; } +PolySet *CgaladvNode::evaluate_polyset(PolySetEvaluator *ps) const +{ + return ps->evaluatePolySet(*this); +} + void register_builtin_cgaladv() { builtin_modules["minkowski"] = new CgaladvModule(MINKOWSKI); diff --git a/src/cgaladvnode.h b/src/cgaladvnode.h index a3f8bea..8e769bf 100644 --- a/src/cgaladvnode.h +++ b/src/cgaladvnode.h @@ -24,6 +24,7 @@ public: } virtual std::string toString() const; virtual std::string name() const; + PolySet *evaluate_polyset(class PolySetEvaluator *ps) const; Value path; std::string subdiv_type; diff --git a/src/csgterm.cc b/src/csgterm.cc index f24a41a..91497b4 100644 --- a/src/csgterm.cc +++ b/src/csgterm.cc @@ -167,8 +167,6 @@ CSGTerm *CSGTerm::link() void CSGTerm::unlink() { if (--refcounter <= 0) { - if (polyset) - polyset->unlink(); if (left) left->unlink(); if (right) diff --git a/src/dxflinextrude.cc b/src/dxflinextrude.cc index fd37ffa..ca98b66 100644 --- a/src/dxflinextrude.cc +++ b/src/dxflinextrude.cc @@ -124,19 +124,16 @@ void register_builtin_dxf_linear_extrude() builtin_modules["linear_extrude"] = new DxfLinearExtrudeModule(); } -PolySet *DxfLinearExtrudeNode::evaluate_polyset(render_mode_e mode, - PolySetEvaluator *evaluator) const +PolySet *DxfLinearExtrudeNode::evaluate_polyset(PolySetEvaluator *evaluator) const { if (!evaluator) { PRINTF("WARNING: No suitable PolySetEvaluator found for %s module!", this->name().c_str()); - PolySet *ps = new PolySet(); - ps->is2d = true; - return ps; + return NULL; } print_messages_push(); - PolySet *ps = evaluator->evaluatePolySet(*this, mode); + PolySet *ps = evaluator->evaluatePolySet(*this); print_messages_pop(); diff --git a/src/dxflinextrudenode.h b/src/dxflinextrudenode.h index 3c3beca..360fd28 100644 --- a/src/dxflinextrudenode.h +++ b/src/dxflinextrudenode.h @@ -24,7 +24,7 @@ public: double origin_x, origin_y, scale; bool center, has_twist; std::string filename, layername; - virtual PolySet *evaluate_polyset(render_mode_e mode, class PolySetEvaluator *) const; + virtual PolySet *evaluate_polyset(class PolySetEvaluator *) const; }; #endif diff --git a/src/dxfrotextrude.cc b/src/dxfrotextrude.cc index 5889cee..cd585aa 100644 --- a/src/dxfrotextrude.cc +++ b/src/dxfrotextrude.cc @@ -100,19 +100,16 @@ void register_builtin_dxf_rotate_extrude() builtin_modules["rotate_extrude"] = new DxfRotateExtrudeModule(); } -PolySet *DxfRotateExtrudeNode::evaluate_polyset(render_mode_e mode, - PolySetEvaluator *evaluator) const +PolySet *DxfRotateExtrudeNode::evaluate_polyset(PolySetEvaluator *evaluator) const { if (!evaluator) { PRINTF("WARNING: No suitable PolySetEvaluator found for %s module!", this->name().c_str()); - PolySet *ps = new PolySet(); - ps->is2d = true; - return ps; + return NULL; } print_messages_push(); - PolySet *ps = evaluator->evaluatePolySet(*this, mode); + PolySet *ps = evaluator->evaluatePolySet(*this); print_messages_pop(); diff --git a/src/dxfrotextrudenode.h b/src/dxfrotextrudenode.h index 797b53a..e0ade82 100644 --- a/src/dxfrotextrudenode.h +++ b/src/dxfrotextrudenode.h @@ -22,7 +22,7 @@ public: double fn, fs, fa; double origin_x, origin_y, scale; std::string filename, layername; - virtual PolySet *evaluate_polyset(render_mode_e mode, class PolySetEvaluator *) const; + virtual PolySet *evaluate_polyset(class PolySetEvaluator *) const; }; #endif diff --git a/src/import.cc b/src/import.cc index 0fdc156..f64a8f6 100644 --- a/src/import.cc +++ b/src/import.cc @@ -111,7 +111,7 @@ AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiati return node; } -PolySet *ImportNode::evaluate_polyset(render_mode_e, class PolySetEvaluator *) const +PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *evaluator) const { PolySet *p = NULL; diff --git a/src/importnode.h b/src/importnode.h index 49e9f16..c08ecc9 100644 --- a/src/importnode.h +++ b/src/importnode.h @@ -27,7 +27,7 @@ public: int convexity; double fn, fs, fa; double origin_x, origin_y, scale; - virtual PolySet *evaluate_polyset(render_mode_e mode, class PolySetEvaluator *) const; + virtual PolySet *evaluate_polyset(class PolySetEvaluator *) const; }; #endif diff --git a/src/mainwin.cc b/src/mainwin.cc index 8dc1fea..2fd876c 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -790,6 +790,7 @@ void MainWindow::compileCSG(bool procevents) if (procevents) QApplication::processEvents(); } + psevaluator.printCache(); } catch (ProgressCancelException e) { PRINT("CSG generation cancelled."); @@ -1225,6 +1226,7 @@ void MainWindow::actionRenderCGAL() QHash<std::string, CGAL_Nef_polyhedron> cache; CGALEvaluator evaluator(cache, this->tree); this->root_N = new CGAL_Nef_polyhedron(evaluator.evaluateCGALMesh(*this->root_node)); + evaluator.psevaluator.printCache(); } catch (ProgressCancelException e) { PRINT("Rendering cancelled."); @@ -36,6 +36,8 @@ public: overloaded to provide specialization for e.g. CSG nodes, primitive nodes etc. Used for human-readable output. */ virtual std::string name() const; + /*! Should return a PolySet of the given geometry. Returns NULL if smth. goes wrong */ + virtual class PolySet *evaluate_polyset(class PolySetEvaluator *evaluator) const { return NULL; } // FIXME: Make return value a reference const std::vector<AbstractNode*> &getChildren() const { @@ -79,10 +81,6 @@ public: RENDER_CGAL, RENDER_OPENCSG }; - /*! Should return a PolySet of the given geometry. It's normal to return an - empty PolySet if smth. is wrong, but don't return NULL unless we change the calling - strategy for this method. */ - virtual class PolySet *evaluate_polyset(render_mode_e mode, class PolySetEvaluator *evaluator) const = 0; }; std::ostream &operator<<(std::ostream &stream, const AbstractNode &node); diff --git a/src/polyset.cc b/src/polyset.cc index db37b49..23b9876 100644 --- a/src/polyset.cc +++ b/src/polyset.cc @@ -36,28 +36,12 @@ #include <Eigen/LU> #include <QColor> -PolySet::PolySet() : grid(GRID_FINE) +PolySet::PolySet() : grid(GRID_FINE), is2d(false), convexity(1) { - is2d = false; - convexity = 1; - refcount = 1; } PolySet::~PolySet() { - assert(refcount == 0); -} - -PolySet* PolySet::link() -{ - refcount++; - return this; -} - -void PolySet::unlink() -{ - if (--refcount == 0) - delete this; } void PolySet::append_poly() diff --git a/src/polyset.h b/src/polyset.h index e80d182..c59d86a 100644 --- a/src/polyset.h +++ b/src/polyset.h @@ -51,10 +51,6 @@ public: void render_surface(colormode_e colormode, csgmode_e csgmode, double *m, GLint *shaderinfo = NULL) const; void render_edges(colormode_e colormode, csgmode_e csgmode) const; - - int refcount; - PolySet *link(); - void unlink(); }; #endif diff --git a/src/primitives.cc b/src/primitives.cc index 50a197d..08b9c62 100644 --- a/src/primitives.cc +++ b/src/primitives.cc @@ -102,7 +102,7 @@ public: primitive_type_e type; int convexity; Value points, paths, triangles; - virtual PolySet *evaluate_polyset(render_mode_e mode, class PolySetEvaluator *) const; + virtual PolySet *evaluate_polyset(class PolySetEvaluator *) const; }; AbstractNode *PrimitiveModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const @@ -273,7 +273,7 @@ static void generate_circle(point2d *circle, double r, int fragments) } } -PolySet *PrimitiveNode::evaluate_polyset(render_mode_e, class PolySetEvaluator *) const +PolySet *PrimitiveNode::evaluate_polyset(class PolySetEvaluator *) const { PolySet *p = new PolySet(); @@ -518,7 +518,7 @@ sphere_next_r2: double x,y; if (!this->points.vec[i]->getv2(x, y)) { PRINTF("ERROR: Unable to convert point at index %d to a vec2 of numbers", i); - p->unlink(); + delete p; return NULL; } dd.points.push_back(Vector2d(x, y)); diff --git a/src/projection.cc b/src/projection.cc index 5a7ea6e..2c9d821 100644 --- a/src/projection.cc +++ b/src/projection.cc @@ -79,18 +79,16 @@ AbstractNode *ProjectionModule::evaluate(const Context *ctx, const ModuleInstant return node; } -PolySet *ProjectionNode::evaluate_polyset(render_mode_e mode, PolySetEvaluator *evaluator) const +PolySet *ProjectionNode::evaluate_polyset(PolySetEvaluator *evaluator) const { if (!evaluator) { PRINTF("WARNING: No suitable PolySetEvaluator found for %s module!", this->name().c_str()); - PolySet *ps = new PolySet(); - ps->is2d = true; - return ps; + return NULL; } print_messages_push(); - PolySet *ps = evaluator->evaluatePolySet(*this, mode); + PolySet *ps = evaluator->evaluatePolySet(*this); print_messages_pop(); diff --git a/src/projectionnode.h b/src/projectionnode.h index 4c29b7b..41cca7c 100644 --- a/src/projectionnode.h +++ b/src/projectionnode.h @@ -19,7 +19,7 @@ public: int convexity; bool cut_mode; - virtual PolySet *evaluate_polyset(render_mode_e mode, class PolySetEvaluator *evaluator) const; + virtual PolySet *evaluate_polyset(class PolySetEvaluator *evaluator) const; }; #endif diff --git a/src/render.cc b/src/render.cc index 48a8535..f08423a 100644 --- a/src/render.cc +++ b/src/render.cc @@ -26,15 +26,12 @@ #include "rendernode.h" #include "module.h" -#include "polyset.h" #include "context.h" -#include "dxfdata.h" -#include "dxftess.h" -#include "csgterm.h" #include "builtin.h" #include "printutils.h" #include "progress.h" #include "visitor.h" +#include "PolySetEvaluator.h" #include <sstream> #include <boost/assign/std/vector.hpp> @@ -68,6 +65,11 @@ AbstractNode *RenderModule::evaluate(const Context *ctx, const ModuleInstantiati return node; } +class PolySet *RenderNode::evaluate_polyset(PolySetEvaluator *ps) const +{ + return ps->evaluatePolySet(*this); +} + void register_builtin_render() { builtin_modules["render"] = new RenderModule(); diff --git a/src/rendernode.h b/src/rendernode.h index 9138c29..9e49baf 100644 --- a/src/rendernode.h +++ b/src/rendernode.h @@ -14,6 +14,7 @@ public: } virtual std::string toString() const; virtual std::string name() const { return "render"; } + PolySet *evaluate_polyset(class PolySetEvaluator *ps) const; int convexity; }; diff --git a/src/surface.cc b/src/surface.cc index 22598bf..35449ed 100644 --- a/src/surface.cc +++ b/src/surface.cc @@ -62,7 +62,7 @@ public: std::string filename; bool center; int convexity; - virtual PolySet *evaluate_polyset(render_mode_e mode, class PolySetEvaluator *) const; + virtual PolySet *evaluate_polyset(class PolySetEvaluator *) const; }; AbstractNode *SurfaceModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const @@ -98,17 +98,17 @@ void register_builtin_surface() builtin_modules["surface"] = new SurfaceModule(); } -PolySet *SurfaceNode::evaluate_polyset(render_mode_e, class PolySetEvaluator *) const +PolySet *SurfaceNode::evaluate_polyset(class PolySetEvaluator *) const { - PolySet *p = new PolySet(); handle_dep(filename); QFile f(QString::fromStdString(filename)); if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) { PRINTF("WARNING: Can't open DAT file `%s'.", filename.c_str()); - return p; + return NULL; } + PolySet *p = new PolySet(); int lines = 0, columns = 0; boost::unordered_map<std::pair<int,int>,double> data; double min_val = 0; diff --git a/tests/csgtermtest.cc b/tests/csgtermtest.cc index 26b809a..7c37b0a 100644 --- a/tests/csgtermtest.cc +++ b/tests/csgtermtest.cc @@ -24,6 +24,7 @@ */ #include "myqhash.h" +#include "PolySetEvaluator.h" #include "CSGTermEvaluator.h" #include "CSGTextCache.h" #include "openscad.h" @@ -145,7 +146,8 @@ int main(int argc, char **argv) vector<CSGTerm*> highlights; vector<CSGTerm*> background; - CSGTermEvaluator evaluator(tree); + PolySetEvaluator psevaluator(tree); + CSGTermEvaluator evaluator(tree, &psevaluator); CSGTerm *root_term = evaluator.evaluateCSGTerm(*root_node, highlights, background); // cout << "Stored terms: " << evaluator.stored_term.size() << "\n"; diff --git a/tests/opencsgtest.cc b/tests/opencsgtest.cc index 72f7488..28c0daa 100644 --- a/tests/opencsgtest.cc +++ b/tests/opencsgtest.cc @@ -144,8 +144,7 @@ int main(int argc, char *argv[]) CsgInfo csgInfo; QHash<std::string, CGAL_Nef_polyhedron> cache; CGALEvaluator cgalevaluator(cache, tree); - PolySetCGALEvaluator psevaluator(cgalevaluator); - CSGTermEvaluator evaluator(tree, &psevaluator); + CSGTermEvaluator evaluator(tree, &cgalevaluator.psevaluator); CSGTerm *root_raw_term = evaluator.evaluateCSGTerm(*root_node, csgInfo.highlight_terms, csgInfo.background_terms); diff --git a/tests/regression/csgtermtest/allmodules-expected.txt b/tests/regression/csgtermtest/allmodules-expected.txt index d7a04fc..f544c01 100644 --- a/tests/regression/csgtermtest/allmodules-expected.txt +++ b/tests/regression/csgtermtest/allmodules-expected.txt @@ -1 +1 @@ -(((((((((((((linear_extrude14 + linear_extrude15) + rotate_extrude16) + rotate_extrude17) + import21) + cube23) + sphere24) + cylinder25) + polyhedron26) + square27) + circle28) + polygon29) + projection30) + surface32) +((((((cube23 + sphere24) + cylinder25) + polyhedron26) + square27) + circle28) + polygon29) diff --git a/tests/regression/csgtermtest/dxf_linear_extrude-expected.txt b/tests/regression/csgtermtest/dxf_linear_extrude-expected.txt index bebcc7a..a40cf60 100644 --- a/tests/regression/csgtermtest/dxf_linear_extrude-expected.txt +++ b/tests/regression/csgtermtest/dxf_linear_extrude-expected.txt @@ -1 +1 @@ -linear_extrude2 +No top-level CSG object diff --git a/tests/regression/csgtermtest/dxf_rotate_extrude-expected.txt b/tests/regression/csgtermtest/dxf_rotate_extrude-expected.txt index 318aa70..a40cf60 100644 --- a/tests/regression/csgtermtest/dxf_rotate_extrude-expected.txt +++ b/tests/regression/csgtermtest/dxf_rotate_extrude-expected.txt @@ -1 +1 @@ -rotate_extrude2 +No top-level CSG object diff --git a/tests/regression/csgtermtest/linear_extrude-expected.txt b/tests/regression/csgtermtest/linear_extrude-expected.txt index bebcc7a..a40cf60 100644 --- a/tests/regression/csgtermtest/linear_extrude-expected.txt +++ b/tests/regression/csgtermtest/linear_extrude-expected.txt @@ -1 +1 @@ -linear_extrude2 +No top-level CSG object diff --git a/tests/regression/csgtermtest/projection-expected.txt b/tests/regression/csgtermtest/projection-expected.txt index 2a4c47e..a40cf60 100644 --- a/tests/regression/csgtermtest/projection-expected.txt +++ b/tests/regression/csgtermtest/projection-expected.txt @@ -1 +1 @@ -projection2 +No top-level CSG object diff --git a/tests/regression/csgtermtest/rotate_extrude-expected.txt b/tests/regression/csgtermtest/rotate_extrude-expected.txt index 318aa70..a40cf60 100644 --- a/tests/regression/csgtermtest/rotate_extrude-expected.txt +++ b/tests/regression/csgtermtest/rotate_extrude-expected.txt @@ -1 +1 @@ -rotate_extrude2 +No top-level CSG object diff --git a/tests/regression/csgtermtest/surface-expected.txt b/tests/regression/csgtermtest/surface-expected.txt index d0e3459..a40cf60 100644 --- a/tests/regression/csgtermtest/surface-expected.txt +++ b/tests/regression/csgtermtest/surface-expected.txt @@ -1 +1 @@ -surface2 +No top-level CSG object |