From a86c346e548052a21d8d1bb5f2034698d2ac2a0e Mon Sep 17 00:00:00 2001 From: Pierre Alliez Date: Tue, 28 Apr 2009 15:29:37 +0000 Subject: [PATCH] AABB: implemented two new functions suggested by Andreas: void clear(void) bool clear_and_insert(begin,beyond) --- AABB_tree/include/CGAL/AABB_tree.h | 52 +++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/AABB_tree/include/CGAL/AABB_tree.h b/AABB_tree/include/CGAL/AABB_tree.h index 0b647ac5ddb..95e9ab2c636 100644 --- a/AABB_tree/include/CGAL/AABB_tree.h +++ b/AABB_tree/include/CGAL/AABB_tree.h @@ -59,8 +59,25 @@ namespace CGAL { template AABB_tree(ConstPrimitiveIterator first, ConstPrimitiveIterator beyond); + /// Clears the current tree and rebuilds the datastructure. + /// Type ConstPrimitiveIterator can be any const iterator on + /// a container of Primitive::id_type such that Primitive has + /// a constructor taking a ConstPrimitiveIterator as argument. + /// Returns true if the memory allocation was successful. + template + bool clear_and_insert(ConstPrimitiveIterator first, ConstPrimitiveIterator beyond); + /// Non virtual destructor - ~AABB_tree() { delete[] m_p_root; } + ~AABB_tree() { clear(); } + + /// Clears the tree + void clear(void) + { + m_data.clear(); + delete[] m_p_root; + m_p_root = NULL; + m_search_tree_constructed = false; + } /// Construct internal search tree with a given point set template @@ -315,6 +332,39 @@ namespace CGAL { m_p_root->expand(m_data.begin(), m_data.end(), m_data.size()); } + // Clears tree and insert a set of primitives + // Returns true upon successful memory allocation + template + template + bool AABB_tree::clear_and_insert(ConstPrimitiveIterator first, + ConstPrimitiveIterator beyond) + { + clear(); + + // allocate memory + m_p_root = new Node[m_data.size()-1](); + if(m_p_root == NULL) + { + std::cerr << "Unable to allocate memory for AABB tree" << std::cerr; + return false; + } + + // inserts primitives + while(first != beyond) + { + m_data.push_back(Primitive(first)); + first++; + } + + // allocates tree nodes + m_p_root = new Node[m_data.size()-1](); + + // constructs the tree + m_p_root->expand(m_data.begin(), m_data.end(), m_data.size()); + + return true; + } + // constructs the search KD tree from given points template template