Mesh Oriented datABase  (version 5.5.0)
An array-based unstructured mesh library
Tree.cpp
Go to the documentation of this file.
1 #include "moab/Tree.hpp"
2 #include "moab/Range.hpp"
3 #include "moab/Interface.hpp"
4 
5 #include <limits>
6 
7 namespace moab
8 {
10 {
11  double tmp_dbl;
12  int tmp_int;
13  // MAX_PER_LEAF: max entities per leaf; default = 6
14  ErrorCode rval = options.get_int_option( "MAX_PER_LEAF", tmp_int );
15  if( MB_SUCCESS == rval ) maxPerLeaf = std::max( tmp_int, 1 );
16 
17  // MAX_DEPTH: max depth of the tree; default = 30
18  rval = options.get_int_option( "MAX_DEPTH", tmp_int );
19  if( MB_SUCCESS == rval ) maxDepth = tmp_int;
20  if( maxDepth < 1 ) maxDepth = std::numeric_limits< unsigned >::max();
21 
22  // MIN_WIDTH: minimum width of box, used like a tolerance; default = 1.0e-10
23  rval = options.get_real_option( "MIN_WIDTH", tmp_dbl );
24  if( MB_SUCCESS == rval ) minWidth = tmp_dbl;
25 
26  // MESHSET_FLAGS: flags passed into meshset creation for tree nodes; should be a value from
27  // ENTITY_SET_PROPERTY (see Types.hpp); default = MESHSET_SET
28  rval = options.get_int_option( "MESHSET_FLAGS", tmp_int );
29  if( MB_SUCCESS == rval && 0 <= tmp_int )
30  meshsetFlags = (unsigned)tmp_int;
31  else if( 0 > tmp_int )
32  return MB_FAILURE;
33 
34  // CLEAN_UP: if false, do not delete tree sets upon tree class destruction; default = true
35  bool tmp_bool;
36  rval = options.get_toggle_option( "CLEAN_UP", true, tmp_bool );
37  if( MB_SUCCESS == rval && !tmp_bool ) cleanUp = false;
38 
39  // TAG_NAME: tag name to store tree information on tree nodes; default = "AKDTree"
40  std::string tmp_str;
41  rval = options.get_str_option( "TAG_NAME", tmp_str );
42  if( MB_SUCCESS == rval ) boxTagName = tmp_str;
43 
44  return MB_SUCCESS;
45 }
46 
48 {
49  Tag tag = get_box_tag();
50  ErrorCode rval = moab()->get_entities_by_type_and_tag( 0, MBENTITYSET, &tag, 0, 1, results );
51  if( MB_SUCCESS != rval || results.empty() ) return rval;
52  std::vector< BoundBox > boxes( results.size() );
53  rval = moab()->tag_get_data( tag, results, &boxes[0] );
54  if( MB_SUCCESS != rval ) return rval;
55  for( std::vector< BoundBox >::iterator vit = boxes.begin(); vit != boxes.end(); ++vit )
56  boundBox.update( *vit );
57 
58  if( results.size() == 1 ) myRoot = *results.begin();
59 
60  return MB_SUCCESS;
61 }
62 
63 ErrorCode Tree::create_root( const double box_min[3], const double box_max[3], EntityHandle& root_handle )
64 {
65  ErrorCode rval = mbImpl->create_meshset( meshsetFlags, root_handle );
66  if( MB_SUCCESS != rval ) return rval;
67 
68  myRoot = root_handle;
69 
70  double box_tag[6];
71  for( int i = 0; i < 3; i++ )
72  {
73  box_tag[i] = box_min[i];
74  box_tag[3 + i] = box_max[i];
75  }
76  rval = mbImpl->tag_set_data( get_box_tag(), &root_handle, 1, box_tag );
77  if( MB_SUCCESS != rval ) return rval;
78 
81 
82  return MB_SUCCESS;
83 }
84 
86 {
87  if( !myRoot ) return MB_SUCCESS;
88 
89  ErrorCode rval;
90  std::vector< EntityHandle > children, dead_sets, current_sets;
91  current_sets.push_back( myRoot );
92  while( !current_sets.empty() )
93  {
94  EntityHandle set = current_sets.back();
95  current_sets.pop_back();
96  dead_sets.push_back( set );
97  rval = mbImpl->get_child_meshsets( set, children );
98  if( MB_SUCCESS != rval ) return rval;
99  std::copy( children.begin(), children.end(), std::back_inserter( current_sets ) );
100  children.clear();
101  }
102 
103  rval = mbImpl->tag_delete_data( boxTag, &myRoot, 1 );
104  if( MB_SUCCESS != rval ) return rval;
105 
106  rval = mbImpl->delete_entities( &dead_sets[0], dead_sets.size() );
107  if( MB_SUCCESS != rval ) return rval;
108 
109  myRoot = 0;
110 
111  return MB_SUCCESS;
112 }
113 
114 } // namespace moab