[MOAB-dev] r2510 - MOAB/trunk

kraftche at mcs.anl.gov kraftche at mcs.anl.gov
Tue Jan 6 15:43:42 CST 2009


Author: kraftche
Date: 2009-01-06 15:43:42 -0600 (Tue, 06 Jan 2009)
New Revision: 2510

Modified:
   MOAB/trunk/MBAdaptiveKDTree.cpp
   MOAB/trunk/MBAdaptiveKDTree.hpp
   MOAB/trunk/MBBSPTree.cpp
   MOAB/trunk/MBBSPTree.hpp
   MOAB/trunk/MBOrientedBoxTreeTool.cpp
   MOAB/trunk/MBOrientedBoxTreeTool.hpp
Log:
add option to delete created trees when tool is deleted

Modified: MOAB/trunk/MBAdaptiveKDTree.cpp
===================================================================
--- MOAB/trunk/MBAdaptiveKDTree.cpp	2009-01-06 21:19:07 UTC (rev 2509)
+++ MOAB/trunk/MBAdaptiveKDTree.cpp	2009-01-06 21:43:42 UTC (rev 2510)
@@ -116,8 +116,15 @@
   return rval;
 }
 
-MBAdaptiveKDTree::MBAdaptiveKDTree( MBInterface* mb, const char* tagname_in, unsigned set_flags )
-  : mbInstance(mb), meshSetFlags(set_flags)
+MBAdaptiveKDTree::MBAdaptiveKDTree( MBInterface* mb, const char* tagname, unsigned set_flags )
+  : mbInstance(mb), meshSetFlags(set_flags), cleanUpTrees(false)
+{ init(tagname); }
+
+MBAdaptiveKDTree::MBAdaptiveKDTree( MBInterface* mb, bool clean_up, const char* tagname, unsigned set_flags )
+  : mbInstance(mb), meshSetFlags(set_flags), cleanUpTrees(clean_up)
+{ init(tagname); }
+
+void MBAdaptiveKDTree::init( const char* tagname_in )
 {
   const char* tagname = tagname_in ? tagname_in : MB_AD_KD_TREE_DEFAULT_TAG_NAME;
   std::vector<MBTag> ctl;
@@ -176,6 +183,23 @@
 #endif
 }
 
+MBAdaptiveKDTree::~MBAdaptiveKDTree()
+{
+  if (!cleanUpTrees)
+    return;
+    
+  while (!createdTrees.empty()) {
+    MBEntityHandle tree = createdTrees.back();
+      // make sure this is a tree (rather than some other, stale handle)
+    const void* data_ptr = 0;
+    MBErrorCode rval = moab()->tag_get_data( rootTag, &tree, 1, &data_ptr );
+    if (MB_SUCCESS == rval)
+      rval = delete_tree( tree );
+    if (MB_SUCCESS != rval)
+      createdTrees.pop_back();
+  }
+}
+
 MBErrorCode MBAdaptiveKDTree::set_split_plane( MBEntityHandle entity, 
                                                const Plane& plane )
 {
@@ -229,6 +253,7 @@
     return rval;
   }
   
+  createdTrees.push_back( root_handle );
   return MB_SUCCESS;
 }
 
@@ -237,6 +262,10 @@
   MBErrorCode rval;
   
   std::vector<MBEntityHandle> children, dead_sets, current_sets;
+  createdTrees.erase( 
+        std::remove( createdTrees.begin(), createdTrees.end(), root_handle ),
+        createdTrees.end() );
+  
   current_sets.push_back( root_handle );
   while (!current_sets.empty()) {
     MBEntityHandle set = current_sets.back();

Modified: MOAB/trunk/MBAdaptiveKDTree.hpp
===================================================================
--- MOAB/trunk/MBAdaptiveKDTree.hpp	2009-01-06 21:19:07 UTC (rev 2509)
+++ MOAB/trunk/MBAdaptiveKDTree.hpp	2009-01-06 21:43:42 UTC (rev 2510)
@@ -38,6 +38,8 @@
   MBInterface* mbInstance;
   MBTag planeTag, axisTag, rootTag;
   unsigned meshSetFlags;
+  bool cleanUpTrees;
+  std::vector<MBEntityHandle> createdTrees;
   
 public:
 
@@ -45,6 +47,13 @@
                     const char* tagname = 0,
                     unsigned meshset_creation_flags = MESHSET_SET );
 
+  MBAdaptiveKDTree( MBInterface* iface, 
+                    bool destroy_created_trees,
+                    const char* tagname = 0,
+                    unsigned meshset_creation_flags = MESHSET_SET );
+
+  ~MBAdaptiveKDTree();
+
   //! Enumeriate split plane directions
   enum Axis { X = 0, Y = 1, Z = 2 };
   
@@ -227,6 +236,8 @@
 
 private:
   
+  void init( const char* tagname );
+  
   /**\brief find a triangle near the input point */
   MBErrorCode find_close_triangle( MBEntityHandle root,
                                    const double from_point[3],

Modified: MOAB/trunk/MBBSPTree.cpp
===================================================================
--- MOAB/trunk/MBBSPTree.cpp	2009-01-06 21:19:07 UTC (rev 2509)
+++ MOAB/trunk/MBBSPTree.cpp	2009-01-06 21:43:42 UTC (rev 2510)
@@ -85,29 +85,54 @@
   return true;
 }
 
-
-MBBSPTree::MBBSPTree( MBInterface* mb, 
-                      const char* tagname, 
-                      unsigned set_flags )
-  : mbInstance(mb), meshSetFlags(set_flags)
+MBErrorCode MBBSPTree::init_tags( const char* tagname )
 {
-  MBErrorCode rval;
-
   if (!tagname) 
     tagname = MB_BSP_TREE_DEFAULT_TAG_NAME;
   
   std::string rootname(tagname);
   rootname += "_box";
   
-  rval = mb->tag_create( tagname, 4*sizeof(double), MB_TAG_DENSE, MB_TYPE_DOUBLE, planeTag, 0, true );
+  MBErrorCode rval = moab()->tag_create( tagname, 4*sizeof(double), MB_TAG_DENSE, MB_TYPE_DOUBLE, planeTag, 0, true );
   if (MB_SUCCESS != rval)
     planeTag = 0;
-  
-  rval = mb->tag_create( rootname.c_str(), 24*sizeof(double), MB_TAG_SPARSE, MB_TYPE_DOUBLE, rootTag, 0, true );
+  else
+    rval = moab()->tag_create( rootname.c_str(), 24*sizeof(double), MB_TAG_SPARSE, MB_TYPE_DOUBLE, rootTag, 0, true );
   if (MB_SUCCESS != rval)
     rootTag = 0;
+  return rval;
 }
 
+MBBSPTree::MBBSPTree( MBInterface* mb, 
+                      const char* tagname, 
+                      unsigned set_flags )
+  : mbInstance(mb), meshSetFlags(set_flags), cleanUpTrees(false)
+{ init_tags( tagname ); }
+
+MBBSPTree::MBBSPTree( MBInterface* mb, 
+                      bool destroy_created_trees,
+                      const char* tagname, 
+                      unsigned set_flags )
+  : mbInstance(mb), meshSetFlags(set_flags), cleanUpTrees(destroy_created_trees)
+{ init_tags( tagname ); }
+
+MBBSPTree::~MBBSPTree()
+{
+  if (!cleanUpTrees)
+    return;
+    
+  while (!createdTrees.empty()) {
+    MBEntityHandle tree = createdTrees.back();
+      // make sure this is a tree (rather than some other, stale handle)
+    const void* data_ptr = 0;
+    MBErrorCode rval = moab()->tag_get_data( rootTag, &tree, 1, &data_ptr );
+    if (MB_SUCCESS == rval)
+      rval = delete_tree( tree );
+    if (MB_SUCCESS != rval)
+      createdTrees.pop_back();
+  }
+}
+
 MBErrorCode MBBSPTree::set_split_plane( MBEntityHandle node, const Plane& p )
 { 
     // check for unit-length normal
@@ -174,6 +199,7 @@
     return rval;
   }
   
+  createdTrees.push_back( root_handle );
   return MB_SUCCESS;
 }
                                     
@@ -208,6 +234,9 @@
   if (MB_SUCCESS != rval)
     return rval;
   
+  createdTrees.erase(
+    std::remove( createdTrees.begin(), createdTrees.end(), root_handle ),
+    createdTrees.end() );
   return moab()->delete_entities( &dead_sets[0], dead_sets.size() );
 }
 

Modified: MOAB/trunk/MBBSPTree.hpp
===================================================================
--- MOAB/trunk/MBBSPTree.hpp	2009-01-06 21:19:07 UTC (rev 2509)
+++ MOAB/trunk/MBBSPTree.hpp	2009-01-06 21:43:42 UTC (rev 2510)
@@ -38,7 +38,11 @@
   MBInterface* mbInstance;
   MBTag planeTag, rootTag;
   unsigned meshSetFlags;
+  bool cleanUpTrees;
+  std::vector<MBEntityHandle> createdTrees;
 
+  MBErrorCode init_tags( const char* tagname = 0 );
+
 public:
   
   static double epsilon() { return 1e-6; }
@@ -46,7 +50,14 @@
   MBBSPTree( MBInterface* iface,
              const char* tagname = 0,
              unsigned meshset_creation_flags = MESHSET_SET );
+
+  MBBSPTree( MBInterface* iface,
+             bool destroy_created_trees,
+             const char* tagname = 0,
+             unsigned meshset_creation_flags = MESHSET_SET );
   
+  ~MBBSPTree();
+  
   /**\brief struct to store a plane 
    *
    * If plane is defined as Ax+By+Cz+D=0, then

Modified: MOAB/trunk/MBOrientedBoxTreeTool.cpp
===================================================================
--- MOAB/trunk/MBOrientedBoxTreeTool.cpp	2009-01-06 21:19:07 UTC (rev 2509)
+++ MOAB/trunk/MBOrientedBoxTreeTool.cpp	2009-01-06 21:43:42 UTC (rev 2510)
@@ -45,8 +45,9 @@
 MBOrientedBoxTreeTool::Op::~Op() {}
 
 MBOrientedBoxTreeTool::MBOrientedBoxTreeTool( MBInterface* i,
-                                              const char* tag_name )
-  : instance( i )
+                                              const char* tag_name,
+                                              bool destroy_created_trees )
+  : instance( i ), cleanUpTrees(destroy_created_trees)
 {
   if (!tag_name)
     tag_name = DEFAULT_TAG_NAME;
@@ -55,6 +56,23 @@
     tagHandle = 0;
 }
 
+MBOrientedBoxTreeTool::~MBOrientedBoxTreeTool()
+{
+  if (!cleanUpTrees)
+    return;
+    
+  while (!createdTrees.empty()) {
+    MBEntityHandle tree = createdTrees.back();
+      // make sure this is a tree (rather than some other, stale handle)
+    const void* data_ptr = 0;
+    MBErrorCode rval = instance->tag_get_data( tagHandle, &tree, 1, &data_ptr );
+    if (MB_SUCCESS == rval)
+      rval = delete_tree( tree );
+    if (MB_SUCCESS != rval)
+      createdTrees.pop_back();
+  }
+}
+
 MBOrientedBoxTreeTool::Settings::Settings() 
   : max_leaf_entities( 8 ),
     max_depth( 0 ),
@@ -148,8 +166,18 @@
       return rval;
   }
 
-  return build_sets( data, set_handle_out, 0, 
-                     settings ? *settings : Settings() );
+  MBErrorCode result = build_sets( data, set_handle_out, 0, 
+                          settings ? *settings : Settings() );
+  if (MB_SUCCESS != result)
+    return result;
+  
+  for (MBRange::reverse_iterator i = sets.rbegin(); i != sets.rend(); ++i) {
+    createdTrees.erase(
+      std::remove( createdTrees.begin(), createdTrees.end(), *i ), 
+      createdTrees.end() );
+  }
+  createdTrees.push_back( set_handle_out );
+  return MB_SUCCESS;
 }
   
 
@@ -287,6 +315,7 @@
       { delete_tree( set ); return rval; }
   }
   
+  createdTrees.push_back( set );
   return MB_SUCCESS;
 }
 
@@ -428,6 +457,9 @@
   if (MB_SUCCESS != rval)
     return rval;
   
+  createdTrees.erase( 
+    std::remove( createdTrees.begin(), createdTrees.end(), set ),
+    createdTrees.end() );
   children.insert( children.begin(), set );
   return instance->delete_entities( &children[0], children.size() );
 }

Modified: MOAB/trunk/MBOrientedBoxTreeTool.hpp
===================================================================
--- MOAB/trunk/MBOrientedBoxTreeTool.hpp	2009-01-06 21:19:07 UTC (rev 2509)
+++ MOAB/trunk/MBOrientedBoxTreeTool.hpp	2009-01-06 21:43:42 UTC (rev 2510)
@@ -85,8 +85,11 @@
     };
   
     MBOrientedBoxTreeTool( MBInterface* i, 
-                           const char* tag_name = 0 ) ;
+                           const char* tag_name = 0,
+                           bool destroy_created_trees = false ) ;
   
+    ~MBOrientedBoxTreeTool();
+  
     /**\brief Build oriented bounding box tree
      *
      * Build an oriented bounding box tree.  
@@ -336,6 +339,9 @@
   
     MBInterface* instance;
     MBTag tagHandle;
+ 
+    bool cleanUpTrees;
+    std::vector<MBEntityHandle> createdTrees;
 };
 
 #endif




More information about the moab-dev mailing list