diff options
author | Marius Kintel <marius@kintel.net> | 2011-12-06 00:16:54 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2011-12-06 00:17:03 (GMT) |
commit | 65a5aa23bfd78718972082f82eb7366d01a968a6 (patch) | |
tree | 756394b55f73d9927fa61e8438d9ddf70d6d3dda | |
parent | d392ee33f6eb57d2d08a7a7703fb9118aa82698c (diff) |
Refactoring: Use shared_ptr instead of our own shared pointer implementation for CSGTerm
-rw-r--r-- | src/CSGTermEvaluator.cc | 47 | ||||
-rw-r--r-- | src/CSGTermEvaluator.h | 13 | ||||
-rw-r--r-- | src/MainWindow.h | 9 | ||||
-rw-r--r-- | src/Tree.cc | 6 | ||||
-rw-r--r-- | src/Tree.h | 2 | ||||
-rw-r--r-- | src/csgterm.cc | 153 | ||||
-rw-r--r-- | src/csgterm.h | 15 | ||||
-rw-r--r-- | src/mainwin.cc | 46 | ||||
-rw-r--r-- | tests/csgtermtest.cc | 7 | ||||
-rw-r--r-- | tests/csgtestcore.cc | 38 |
10 files changed, 160 insertions, 176 deletions
diff --git a/src/CSGTermEvaluator.cc b/src/CSGTermEvaluator.cc index 0c7bca7..1aedfec 100644 --- a/src/CSGTermEvaluator.cc +++ b/src/CSGTermEvaluator.cc @@ -18,6 +18,7 @@ #include <iostream> #include <assert.h> #include <cstddef> +#include <boost/foreach.hpp> /*! \class CSGTermEvaluator @@ -26,9 +27,9 @@ with OpenCSG. */ -CSGTerm *CSGTermEvaluator::evaluateCSGTerm(const AbstractNode &node, - std::vector<CSGTerm*> &highlights, - std::vector<CSGTerm*> &background) +shared_ptr<CSGTerm> CSGTermEvaluator::evaluateCSGTerm(const AbstractNode &node, + std::vector<shared_ptr<CSGTerm> > &highlights, + std::vector<shared_ptr<CSGTerm> > &background) { Traverser evaluate(*this, node, Traverser::PRE_AND_POSTFIX); evaluate.execute(); @@ -39,31 +40,28 @@ CSGTerm *CSGTermEvaluator::evaluateCSGTerm(const AbstractNode &node, void CSGTermEvaluator::applyToChildren(const AbstractNode &node, CSGTermEvaluator::CsgOp op) { - CSGTerm *t1 = NULL; - for (ChildList::const_iterator iter = this->visitedchildren[node.index()].begin(); - iter != this->visitedchildren[node.index()].end(); - iter++) { - const AbstractNode *chnode = *iter; - CSGTerm *t2 = this->stored_term[chnode->index()]; + shared_ptr<CSGTerm> t1; + BOOST_FOREACH(const AbstractNode *chnode, this->visitedchildren[node.index()]) { + shared_ptr<CSGTerm> t2(this->stored_term[chnode->index()]); this->stored_term.erase(chnode->index()); if (t2 && !t1) { t1 = t2; } else if (t2 && t1) { if (op == CSGT_UNION) { - t1 = new CSGTerm(CSGTerm::TYPE_UNION, t1, t2); + t1.reset(new CSGTerm(CSGTerm::TYPE_UNION, t1, t2)); } else if (op == CSGT_DIFFERENCE) { - t1 = new CSGTerm(CSGTerm::TYPE_DIFFERENCE, t1, t2); + t1.reset(new CSGTerm(CSGTerm::TYPE_DIFFERENCE, t1, t2)); } else if (op == CSGT_INTERSECTION) { - t1 = new CSGTerm(CSGTerm::TYPE_INTERSECTION, t1, t2); + t1.reset(new CSGTerm(CSGTerm::TYPE_INTERSECTION, t1, t2)); } } } if (t1 && node.modinst->tag_highlight) { - this->highlights.push_back(t1->link()); + this->highlights.push_back(t1); } if (t1 && node.modinst->tag_background) { this->background.push_back(t1); - t1 = NULL; // don't propagate background tagged nodes + t1.reset(); // don't propagate background tagged nodes } this->stored_term[node.index()] = t1; } @@ -86,21 +84,22 @@ Response CSGTermEvaluator::visit(State &state, const AbstractIntersectionNode &n return ContinueTraversal; } -static CSGTerm *evaluate_csg_term_from_ps(const State &state, - std::vector<CSGTerm*> &highlights, - std::vector<CSGTerm*> &background, +static shared_ptr<CSGTerm> evaluate_csg_term_from_ps(const State &state, + std::vector<shared_ptr<CSGTerm> > &highlights, + std::vector<shared_ptr<CSGTerm> > &background, const shared_ptr<PolySet> &ps, const ModuleInstantiation *modinst, const AbstractNode &node) { std::stringstream stream; stream << node.name() << node.index(); - CSGTerm *t = new CSGTerm(ps, state.matrix(), state.color(), stream.str()); - if (modinst->tag_highlight) - highlights.push_back(t->link()); + shared_ptr<CSGTerm> t(new CSGTerm(ps, state.matrix(), state.color(), stream.str())); + if (modinst->tag_highlight) { + highlights.push_back(t); + } if (modinst->tag_background) { background.push_back(t); - return NULL; + t.reset(); } return t; } @@ -108,7 +107,7 @@ static CSGTerm *evaluate_csg_term_from_ps(const State &state, Response CSGTermEvaluator::visit(State &state, const AbstractPolyNode &node) { if (state.isPostfix()) { - CSGTerm *t1 = NULL; + shared_ptr<CSGTerm> t1; if (this->psevaluator) { shared_ptr<PolySet> ps = this->psevaluator->getPolySet(node, true); if (ps) { @@ -173,7 +172,7 @@ Response CSGTermEvaluator::visit(State &state, const ColorNode &node) Response CSGTermEvaluator::visit(State &state, const RenderNode &node) { if (state.isPostfix()) { - CSGTerm *t1 = NULL; + shared_ptr<CSGTerm> t1; shared_ptr<PolySet> ps; if (this->psevaluator) { ps = this->psevaluator->getPolySet(node, true); @@ -191,7 +190,7 @@ Response CSGTermEvaluator::visit(State &state, const RenderNode &node) Response CSGTermEvaluator::visit(State &state, const CgaladvNode &node) { if (state.isPostfix()) { - CSGTerm *t1 = NULL; + shared_ptr<CSGTerm> t1; // FIXME: Calling evaluator directly since we're not a PolyNode. Generalize this. shared_ptr<PolySet> ps; if (this->psevaluator) { diff --git a/src/CSGTermEvaluator.h b/src/CSGTermEvaluator.h index 3a8122b..49bac17 100644 --- a/src/CSGTermEvaluator.h +++ b/src/CSGTermEvaluator.h @@ -6,6 +6,7 @@ #include <vector> #include <cstddef> #include "visitor.h" +#include "memory.h" class CSGTermEvaluator : public Visitor { @@ -24,9 +25,9 @@ public: virtual Response visit(State &state, const class RenderNode &node); virtual Response visit(State &state, const class CgaladvNode &node); - class CSGTerm *evaluateCSGTerm(const AbstractNode &node, - std::vector<CSGTerm*> &highlights, - std::vector<CSGTerm*> &background); + shared_ptr<class CSGTerm> evaluateCSGTerm(const AbstractNode &node, + std::vector<shared_ptr<CSGTerm> > &highlights, + std::vector<shared_ptr<CSGTerm> > &background); private: enum CsgOp {CSGT_UNION, CSGT_INTERSECTION, CSGT_DIFFERENCE, CSGT_MINKOWSKI}; @@ -38,10 +39,10 @@ private: std::map<int, ChildList> visitedchildren; public: - std::map<int, class CSGTerm*> stored_term; // The term evaluated from each node index + std::map<int, shared_ptr<CSGTerm> > stored_term; // The term evaluated from each node index - std::vector<CSGTerm*> highlights; - std::vector<CSGTerm*> background; + std::vector<shared_ptr<CSGTerm> > highlights; + std::vector<shared_ptr<CSGTerm> > background; const Tree &tree; class PolySetEvaluator *psevaluator; }; diff --git a/src/MainWindow.h b/src/MainWindow.h index 06332b0..b2d0f60 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -7,6 +7,7 @@ #include "context.h" #include "module.h" #include "Tree.h" +#include "memory.h" #include <vector> class MainWindow : public QMainWindow, public Ui::MainWindow @@ -34,8 +35,8 @@ public: AbstractNode *root_node; // Root if the root modifier (!) is used Tree tree; - class CSGTerm *root_raw_term; // Result of CSG term rendering - CSGTerm *root_norm_term; // Normalized CSG products + shared_ptr<class CSGTerm> root_raw_term; // Result of CSG term rendering + shared_ptr<CSGTerm> root_norm_term; // Normalized CSG products class CSGChain *root_chain; #ifdef ENABLE_CGAL class CGAL_Nef_polyhedron *root_N; @@ -46,9 +47,9 @@ public: #endif class ThrownTogetherRenderer *thrownTogetherRenderer; - std::vector<CSGTerm*> highlight_terms; + std::vector<shared_ptr<CSGTerm> > highlight_terms; CSGChain *highlights_chain; - std::vector<CSGTerm*> background_terms; + std::vector<shared_ptr<CSGTerm> > background_terms; CSGChain *background_chain; QString last_compiled_doc; diff --git a/src/Tree.cc b/src/Tree.cc index 7c4866b..d27e198 100644 --- a/src/Tree.cc +++ b/src/Tree.cc @@ -4,6 +4,12 @@ #include <assert.h> #include <algorithm> +Tree::~Tree() +{ + this->nodecache.clear(); + this->nodeidcache.clear(); +} + /*! Returns the cached string representation of the subtree rooted by \a node. If node is not cached, the cache will be rebuilt. @@ -13,7 +13,7 @@ class Tree { public: Tree(const AbstractNode *root = NULL) : root_node(root) {} - ~Tree() {} + ~Tree(); void setRoot(const AbstractNode *root); const AbstractNode *root() const { return this->root_node; } diff --git a/src/csgterm.cc b/src/csgterm.cc index 16ef75f..b21a20c 100644 --- a/src/csgterm.cc +++ b/src/csgterm.cc @@ -48,20 +48,28 @@ CSGTerm::CSGTerm(const shared_ptr<PolySet> &polyset, const Transform3d &matrix, const double color[4], const std::string &label) - : type(TYPE_PRIMITIVE), polyset(polyset), label(label), left(NULL), right(NULL) + : type(TYPE_PRIMITIVE), polyset(polyset), label(label) { this->m = matrix; for (int i = 0; i < 4; i++) this->color[i] = color[i]; - refcounter = 1; +} + +CSGTerm::CSGTerm(type_e type, shared_ptr<CSGTerm> left, shared_ptr<CSGTerm> right) + : type(type), left(left), right(right) +{ } CSGTerm::CSGTerm(type_e type, CSGTerm *left, CSGTerm *right) : type(type), left(left), right(right) { - refcounter = 1; } -CSGTerm *CSGTerm::normalize() +CSGTerm::~CSGTerm() +{ +} + + +shared_ptr<CSGTerm> CSGTerm::normalize(shared_ptr<CSGTerm> &term) { // This function implements the CSG normalization // Reference: Florian Kirsch, Juergen Doeller, @@ -69,103 +77,100 @@ CSGTerm *CSGTerm::normalize() // University of Potsdam, Hasso-Plattner-Institute, Germany // http://www.opencsg.org/data/csg_freenix2005_paper.pdf - if (type == TYPE_PRIMITIVE) - return link(); - - CSGTerm *t1, *t2, *x, *y; + if (term->type == TYPE_PRIMITIVE) return term; - x = left->normalize(); - y = right->normalize(); + shared_ptr<CSGTerm> x = normalize(term->left); + shared_ptr<CSGTerm> y = normalize(term->right); - if (x != left || y != right) { - t1 = new CSGTerm(type, x, y); - } else { - t1 = link(); - x->unlink(); - y->unlink(); - } + shared_ptr<CSGTerm> t1(term); + if (x != term->left || y != term->right) t1.reset(new CSGTerm(term->type, x, y)); + shared_ptr<CSGTerm> t2; while (1) { - t2 = t1->normalize_tail(); - t1->unlink(); - if (t1 == t2) - break; + t2 = normalize_tail(t1); + if (t1 == t2) break; t1 = t2; } return t1; } -CSGTerm *CSGTerm::normalize_tail() +shared_ptr<CSGTerm> CSGTerm::normalize_tail(shared_ptr<CSGTerm> &term) { - CSGTerm *x, *y, *z; - // Part A: The 'x . (y . z)' expressions - x = left; - y = right->left; - z = right->right; + shared_ptr<CSGTerm> x = term->left; + shared_ptr<CSGTerm> y = term->right->left; + shared_ptr<CSGTerm> z = term->right->right; - // 1. x - (y + z) -> (x - y) - z - if (type == TYPE_DIFFERENCE && right->type == TYPE_UNION) - return new CSGTerm(TYPE_DIFFERENCE, new CSGTerm(TYPE_DIFFERENCE, x->link(), y->link()), z->link()); + CSGTerm *result = NULL; + // 1. x - (y + z) -> (x - y) - z + if (term->type == TYPE_DIFFERENCE && term->right->type == TYPE_UNION) { + result = new CSGTerm(TYPE_DIFFERENCE, + shared_ptr<CSGTerm>(new CSGTerm(TYPE_DIFFERENCE, x, y)), + z); + } // 2. x * (y + z) -> (x * y) + (x * z) - if (type == TYPE_INTERSECTION && right->type == TYPE_UNION) - return new CSGTerm(TYPE_UNION, new CSGTerm(TYPE_INTERSECTION, x->link(), y->link()), new CSGTerm(TYPE_INTERSECTION, x->link(), z->link())); - + else if (term->type == TYPE_INTERSECTION && term->right->type == TYPE_UNION) { + result = new CSGTerm(TYPE_UNION, + new CSGTerm(TYPE_INTERSECTION, x, y), + new CSGTerm(TYPE_INTERSECTION, x, z)); + } // 3. x - (y * z) -> (x - y) + (x - z) - if (type == TYPE_DIFFERENCE && right->type == TYPE_INTERSECTION) - return new CSGTerm(TYPE_UNION, new CSGTerm(TYPE_DIFFERENCE, x->link(), y->link()), new CSGTerm(TYPE_DIFFERENCE, x->link(), z->link())); - + else if (term->type == TYPE_DIFFERENCE && term->right->type == TYPE_INTERSECTION) { + result = new CSGTerm(TYPE_UNION, + new CSGTerm(TYPE_DIFFERENCE, x, y), + new CSGTerm(TYPE_DIFFERENCE, x, z)); + } // 4. x * (y * z) -> (x * y) * z - if (type == TYPE_INTERSECTION && right->type == TYPE_INTERSECTION) - return new CSGTerm(TYPE_INTERSECTION, new CSGTerm(TYPE_INTERSECTION, x->link(), y->link()), z->link()); - + else if (term->type == TYPE_INTERSECTION && term->right->type == TYPE_INTERSECTION) { + result = new CSGTerm(TYPE_INTERSECTION, + shared_ptr<CSGTerm>(new CSGTerm(TYPE_INTERSECTION, x, y)), + z); + } // 5. x - (y - z) -> (x - y) + (x * z) - if (type == TYPE_DIFFERENCE && right->type == TYPE_DIFFERENCE) - return new CSGTerm(TYPE_UNION, new CSGTerm(TYPE_DIFFERENCE, x->link(), y->link()), new CSGTerm(TYPE_INTERSECTION, x->link(), z->link())); - + else if (term->type == TYPE_DIFFERENCE && term->right->type == TYPE_DIFFERENCE) { + result = new CSGTerm(TYPE_UNION, + new CSGTerm(TYPE_DIFFERENCE, x, y), + new CSGTerm(TYPE_INTERSECTION, x, z)); + } // 6. x * (y - z) -> (x * y) - z - if (type == TYPE_INTERSECTION && right->type == TYPE_DIFFERENCE) - return new CSGTerm(TYPE_DIFFERENCE, new CSGTerm(TYPE_INTERSECTION, x->link(), y->link()), z->link()); + else if (term->type == TYPE_INTERSECTION && term->right->type == TYPE_DIFFERENCE) { + result = new CSGTerm(TYPE_DIFFERENCE, + shared_ptr<CSGTerm>(new CSGTerm(TYPE_INTERSECTION, x, y)), + z); + } + if (result) return shared_ptr<CSGTerm>(result); // Part B: The '(x . y) . z' expressions - x = left->left; - y = left->right; - z = right; + x = term->left->left; + y = term->left->right; + z = term->right; // 7. (x - y) * z -> (x * z) - y - if (left->type == TYPE_DIFFERENCE && type == TYPE_INTERSECTION) - return new CSGTerm(TYPE_DIFFERENCE, new CSGTerm(TYPE_INTERSECTION, x->link(), z->link()), y->link()); - + if (term->left->type == TYPE_DIFFERENCE && term->type == TYPE_INTERSECTION) { + result = new CSGTerm(TYPE_DIFFERENCE, + shared_ptr<CSGTerm>(new CSGTerm(TYPE_INTERSECTION, x, z)), + y); + } // 8. (x + y) - z -> (x - z) + (y - z) - if (left->type == TYPE_UNION && type == TYPE_DIFFERENCE) - return new CSGTerm(TYPE_UNION, new CSGTerm(TYPE_DIFFERENCE, x->link(), z->link()), new CSGTerm(TYPE_DIFFERENCE, y->link(), z->link())); - + else if (term->left->type == TYPE_UNION && term->type == TYPE_DIFFERENCE) { + result = new CSGTerm(TYPE_UNION, + new CSGTerm(TYPE_DIFFERENCE, x, z), + new CSGTerm(TYPE_DIFFERENCE, y, z)); + } // 9. (x + y) * z -> (x * z) + (y * z) - if (left->type == TYPE_UNION && type == TYPE_INTERSECTION) - return new CSGTerm(TYPE_UNION, new CSGTerm(TYPE_INTERSECTION, x->link(), z->link()), new CSGTerm(TYPE_INTERSECTION, y->link(), z->link())); - - return link(); -} - -CSGTerm *CSGTerm::link() -{ - refcounter++; - return this; -} - -void CSGTerm::unlink() -{ - if (--refcounter <= 0) { - if (left) - left->unlink(); - if (right) - right->unlink(); - delete this; + else if (term->left->type == TYPE_UNION && term->type == TYPE_INTERSECTION) { + result = new CSGTerm(TYPE_UNION, + new CSGTerm(TYPE_INTERSECTION, x, z), + new CSGTerm(TYPE_INTERSECTION, y, z)); } + + if (result) return shared_ptr<CSGTerm>(result); + + return term; } std::string CSGTerm::dump() @@ -197,7 +202,7 @@ void CSGChain::add(const shared_ptr<PolySet> &polyset, const Transform3d &m, dou labels.push_back(label); } -void CSGChain::import(CSGTerm *term, CSGTerm::type_e type) +void CSGChain::import(shared_ptr<CSGTerm> term, CSGTerm::type_e type) { if (term->type == CSGTerm::TYPE_PRIMITIVE) { add(term->polyset, term->m, term->color, type, term->label); diff --git a/src/csgterm.h b/src/csgterm.h index 1d9d9fd..1895839 100644 --- a/src/csgterm.h +++ b/src/csgterm.h @@ -21,20 +21,19 @@ public: type_e type; shared_ptr<PolySet> polyset; std::string label; - CSGTerm *left; - CSGTerm *right; + shared_ptr<CSGTerm> left; + shared_ptr<CSGTerm> right; Transform3d m; double color[4]; - int refcounter; CSGTerm(const shared_ptr<PolySet> &polyset, const Transform3d &matrix, const double color[4], const std::string &label); + CSGTerm(type_e type, shared_ptr<CSGTerm> left, shared_ptr<CSGTerm> right); CSGTerm(type_e type, CSGTerm *left, CSGTerm *right); + ~CSGTerm(); - CSGTerm *normalize(); - CSGTerm *normalize_tail(); + static shared_ptr<CSGTerm> normalize(shared_ptr<CSGTerm> &term); + static shared_ptr<CSGTerm> normalize_tail(shared_ptr<CSGTerm> &term); - CSGTerm *link(); - void unlink(); std::string dump(); }; @@ -50,7 +49,7 @@ public: CSGChain(); void add(const shared_ptr<PolySet> &polyset, const Transform3d &m, double *color, CSGTerm::type_e type, std::string label); - void import(CSGTerm *term, CSGTerm::type_e type = CSGTerm::TYPE_UNION); + void import(shared_ptr<CSGTerm> term, CSGTerm::type_e type = CSGTerm::TYPE_UNION); std::string dump(); BoundingBox getBoundingBox() const; diff --git a/src/mainwin.cc b/src/mainwin.cc index 29c84b0..c81f2f2 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -147,8 +147,6 @@ MainWindow::MainWindow(const QString &filename) root_module = NULL; absolute_root_node = NULL; - root_raw_term = NULL; - root_norm_term = NULL; root_chain = NULL; #ifdef ENABLE_CGAL this->root_N = NULL; @@ -619,30 +617,18 @@ void MainWindow::compile(bool procevents) this->absolute_root_node = NULL; } - if (this->root_raw_term) { - this->root_raw_term->unlink(); - this->root_raw_term = NULL; - } - - if (this->root_norm_term) { - this->root_norm_term->unlink(); - this->root_norm_term = NULL; - } + this->root_raw_term.reset(); + this->root_norm_term.reset(); if (this->root_chain) { delete this->root_chain; this->root_chain = NULL; } - std::for_each(this->highlight_terms.begin(), this->highlight_terms.end(), - bind(&CSGTerm::unlink, _1)); - this->highlight_terms.clear(); delete this->highlights_chain; this->highlights_chain = NULL; - std::for_each(this->background_terms.begin(), this->background_terms.end(), - bind(&CSGTerm::unlink, _1)); this->background_terms.clear(); delete this->background_chain; this->background_chain = NULL; @@ -771,7 +757,7 @@ void MainWindow::compileCSG(bool procevents) CGALEvaluator cgalevaluator(this->tree); PolySetCGALEvaluator psevaluator(cgalevaluator); CSGTermEvaluator csgrenderer(this->tree, &psevaluator); - root_raw_term = csgrenderer.evaluateCSGTerm(*root_node, highlight_terms, background_terms); + this->root_raw_term = csgrenderer.evaluateCSGTerm(*root_node, highlight_terms, background_terms); if (!root_raw_term) { PRINT("ERROR: CSG generation failed! (no top level object found)"); if (procevents) @@ -794,21 +780,19 @@ void MainWindow::compileCSG(bool procevents) if (procevents) QApplication::processEvents(); - root_norm_term = root_raw_term->link(); + this->root_norm_term = this->root_raw_term; // CSG normalization while (1) { - CSGTerm *n = root_norm_term->normalize(); - root_norm_term->unlink(); - if (root_norm_term == n) - break; - root_norm_term = n; + shared_ptr<CSGTerm> n = CSGTerm::normalize(this->root_norm_term); + if (this->root_norm_term == n) break; + this->root_norm_term = n; } - assert(root_norm_term); + assert(this->root_norm_term); root_chain = new CSGChain(); - root_chain->import(root_norm_term); + root_chain->import(this->root_norm_term); if (highlight_terms.size() > 0) { @@ -819,10 +803,8 @@ void MainWindow::compileCSG(bool procevents) highlights_chain = new CSGChain(); for (unsigned int i = 0; i < highlight_terms.size(); i++) { while (1) { - CSGTerm *n = highlight_terms[i]->normalize(); - highlight_terms[i]->unlink(); - if (highlight_terms[i] == n) - break; + shared_ptr<CSGTerm> n = CSGTerm::normalize(highlight_terms[i]); + if (highlight_terms[i] == n) break; highlight_terms[i] = n; } highlights_chain->import(highlight_terms[i]); @@ -838,10 +820,8 @@ void MainWindow::compileCSG(bool procevents) background_chain = new CSGChain(); for (unsigned int i = 0; i < background_terms.size(); i++) { while (1) { - CSGTerm *n = background_terms[i]->normalize(); - background_terms[i]->unlink(); - if (background_terms[i] == n) - break; + shared_ptr<CSGTerm> n = CSGTerm::normalize(background_terms[i]); + if (background_terms[i] == n) break; background_terms[i] = n; } background_chain->import(background_terms[i]); diff --git a/tests/csgtermtest.cc b/tests/csgtermtest.cc index 2383126..aabbc05 100644 --- a/tests/csgtermtest.cc +++ b/tests/csgtermtest.cc @@ -116,11 +116,11 @@ int main(int argc, char **argv) // cout << tree.getString(*root_node) << "\n"; - std::vector<CSGTerm*> highlights; - std::vector<CSGTerm*> background; + std::vector<shared_ptr<CSGTerm> > highlights; + std::vector<shared_ptr<CSGTerm> > background; PolySetEvaluator psevaluator(tree); CSGTermEvaluator evaluator(tree, &psevaluator); - CSGTerm *root_term = evaluator.evaluateCSGTerm(*root_node, highlights, background); + shared_ptr<CSGTerm> root_term = evaluator.evaluateCSGTerm(*root_node, highlights, background); // cout << "Stored terms: " << evaluator.stored_term.size() << "\n"; // for (map<int, class CSGTerm*>::iterator iter = evaluator.stored_term.begin(); @@ -143,7 +143,6 @@ int main(int argc, char **argv) } outfile.close(); - if (root_term) root_term->unlink(); delete root_node; delete root_module; diff --git a/tests/csgtestcore.cc b/tests/csgtestcore.cc index 49bd473..864d40e 100644 --- a/tests/csgtestcore.cc +++ b/tests/csgtestcore.cc @@ -39,21 +39,18 @@ class CsgInfo { public: CsgInfo(); - CSGTerm *root_norm_term; // Normalized CSG products + shared_ptr<CSGTerm> root_norm_term; // Normalized CSG products class CSGChain *root_chain; - std::vector<CSGTerm*> highlight_terms; + std::vector<shared_ptr<CSGTerm> > highlight_terms; CSGChain *highlights_chain; - std::vector<CSGTerm*> background_terms; + std::vector<shared_ptr<CSGTerm> > background_terms; CSGChain *background_chain; OffscreenView *glview; }; CsgInfo::CsgInfo() { - root_norm_term = NULL; root_chain = NULL; - highlight_terms = std::vector<CSGTerm*>(); highlights_chain = NULL; - background_terms = std::vector<CSGTerm*>(); background_chain = NULL; glview = NULL; } @@ -129,9 +126,9 @@ int csgtestcore(int argc, char *argv[], test_type_e test_type) CsgInfo csgInfo = CsgInfo(); CGALEvaluator cgalevaluator(tree); CSGTermEvaluator evaluator(tree, &cgalevaluator.psevaluator); - CSGTerm *root_raw_term = evaluator.evaluateCSGTerm(*root_node, - csgInfo.highlight_terms, - csgInfo.background_terms); + shared_ptr<CSGTerm> root_raw_term = evaluator.evaluateCSGTerm(*root_node, + csgInfo.highlight_terms, + csgInfo.background_terms); if (!root_raw_term) { cerr << "Error: CSG generation failed! (no top level object found)\n"; @@ -139,16 +136,17 @@ int csgtestcore(int argc, char *argv[], test_type_e test_type) } // CSG normalization - csgInfo.root_norm_term = root_raw_term->link(); + csgInfo.root_norm_term = root_raw_term; while (1) { - CSGTerm *n = csgInfo.root_norm_term->normalize(); - csgInfo.root_norm_term->unlink(); - if (csgInfo.root_norm_term == n) - break; + shared_ptr<CSGTerm> n = CSGTerm::normalize(csgInfo.root_norm_term); + if (csgInfo.root_norm_term == n) break; csgInfo.root_norm_term = n; } assert(csgInfo.root_norm_term); + if (csgInfo.root_norm_term.use_count() <= 1) { + fprintf(stderr, "XXX\n"); + } csgInfo.root_chain = new CSGChain(); csgInfo.root_chain->import(csgInfo.root_norm_term); @@ -160,10 +158,8 @@ int csgtestcore(int argc, char *argv[], test_type_e test_type) csgInfo.highlights_chain = new CSGChain(); for (unsigned int i = 0; i < csgInfo.highlight_terms.size(); i++) { while (1) { - CSGTerm *n = csgInfo.highlight_terms[i]->normalize(); - csgInfo.highlight_terms[i]->unlink(); - if (csgInfo.highlight_terms[i] == n) - break; + shared_ptr<CSGTerm> n = CSGTerm::normalize(csgInfo.highlight_terms[i]); + if (csgInfo.highlight_terms[i] == n) break; csgInfo.highlight_terms[i] = n; } csgInfo.highlights_chain->import(csgInfo.highlight_terms[i]); @@ -176,10 +172,8 @@ int csgtestcore(int argc, char *argv[], test_type_e test_type) csgInfo.background_chain = new CSGChain(); for (unsigned int i = 0; i < csgInfo.background_terms.size(); i++) { while (1) { - CSGTerm *n = csgInfo.background_terms[i]->normalize(); - csgInfo.background_terms[i]->unlink(); - if (csgInfo.background_terms[i] == n) - break; + shared_ptr<CSGTerm> n = CSGTerm::normalize(csgInfo.background_terms[i]); + if (csgInfo.background_terms[i] == n) break; csgInfo.background_terms[i] = n; } csgInfo.background_chain->import(csgInfo.background_terms[i]); |