diff --git a/src/csgtermnormalizer.cc b/src/csgtermnormalizer.cc index 461e965f..2f79841c 100644 --- a/src/csgtermnormalizer.cc +++ b/src/csgtermnormalizer.cc @@ -32,6 +32,22 @@ shared_ptr CSGTermNormalizer::normalize(const shared_ptr &root return temp; } +/*! + After aborting, a subtree might have become invalidated (NULL child term) + since terms can be instantiated multiple times. + This will search for NULL children an recursively repair the corresponding + subtree. + */ +shared_ptr CSGTermNormalizer::cleanup_term(shared_ptr &t) +{ + if (t->type != CSGTerm::TYPE_PRIMITIVE) { + if (t->left) t->left = cleanup_term(t->left); + if (t->right) t->right = cleanup_term(t->right); + return collapse_null_terms(t); + } + else return t; +} + shared_ptr CSGTermNormalizer::normalizePass(shared_ptr term) { // This function implements the CSG normalization @@ -62,7 +78,13 @@ shared_ptr CSGTermNormalizer::normalizePass(shared_ptr term) if (!this->aborted) term->right = normalizePass(term->right); // FIXME: Do we need to take into account any transformation of item here? - return collapse_null_terms(term); + shared_ptr t = collapse_null_terms(term); + + if (this->aborted) { + if (t) t = cleanup_term(t); + } + + return t; } shared_ptr CSGTermNormalizer::collapse_null_terms(const shared_ptr &term) diff --git a/src/csgtermnormalizer.h b/src/csgtermnormalizer.h index f7a444f6..bb55141e 100644 --- a/src/csgtermnormalizer.h +++ b/src/csgtermnormalizer.h @@ -15,6 +15,7 @@ private: shared_ptr normalizePass(shared_ptr term) ; bool match_and_replace(shared_ptr &term); shared_ptr collapse_null_terms(const shared_ptr &term); + shared_ptr cleanup_term(shared_ptr &t); unsigned int count(const shared_ptr &term) const; bool aborted;