1 #include "moab/Tree.hpp"
2 #include "moab/Range.hpp"
3 #include "moab/Interface.hpp"
4 
5 #include <limits>
6 
7 namespace moab
8 {
parse_common_options(FileOptions & options)9     ErrorCode Tree::parse_common_options(FileOptions &options)
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) meshsetFlags = (unsigned) tmp_int;
30       else if (0 > tmp_int) return MB_FAILURE;
31 
32         // CLEAN_UP: if false, do not delete tree sets upon tree class destruction; default = true
33       bool tmp_bool;
34       rval = options.get_toggle_option("CLEAN_UP", true, tmp_bool);
35       if (MB_SUCCESS == rval && !tmp_bool) cleanUp = false;
36 
37         // TAG_NAME: tag name to store tree information on tree nodes; default = "AKDTree"
38       std::string tmp_str;
39       rval = options.get_str_option("TAG_NAME", tmp_str);
40       if (MB_SUCCESS == rval) boxTagName = tmp_str;
41 
42       return MB_SUCCESS;
43     }
44 
find_all_trees(Range & results)45     ErrorCode Tree::find_all_trees( Range& results )
46     {
47       Tag tag = get_box_tag();
48       ErrorCode rval = moab()->get_entities_by_type_and_tag( 0, MBENTITYSET, &tag, 0, 1, results );
49       if (MB_SUCCESS != rval || results.empty()) return rval;
50       std::vector<BoundBox> boxes(results.size());
51       rval = moab()->tag_get_data(tag, results, &boxes[0]);
52       if (MB_SUCCESS != rval) return rval;
53       for (std::vector<BoundBox>::iterator vit = boxes.begin(); vit != boxes.end(); ++vit)
54         boundBox.update(*vit);
55 
56       if (results.size() == 1) myRoot = *results.begin();
57 
58       return MB_SUCCESS;
59     }
60 
create_root(const double box_min[3],const double box_max[3],EntityHandle & root_handle)61     ErrorCode Tree::create_root( const double box_min[3],
62                                  const double box_max[3],
63                                  EntityHandle& root_handle )
64     {
65       ErrorCode rval = mbImpl->create_meshset( meshsetFlags, root_handle );
66       if (MB_SUCCESS != rval)
67         return rval;
68 
69       myRoot = root_handle;
70 
71       double box_tag[6];
72       for (int i = 0; i < 3; i++) {
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)
78         return rval;
79 
80       boundBox.bMin = box_min;
81       boundBox.bMax = box_max;
82 
83       return MB_SUCCESS;
84     }
85 
delete_tree_sets()86     ErrorCode Tree::delete_tree_sets()
87     {
88       if (!myRoot) return MB_SUCCESS;
89 
90       ErrorCode rval;
91       std::vector<EntityHandle> children, dead_sets, current_sets;
92       current_sets.push_back(myRoot);
93       while (!current_sets.empty()) {
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)
99           return rval;
100         std::copy( children.begin(), children.end(), std::back_inserter(current_sets) );
101         children.clear();
102       }
103 
104       rval = mbImpl->tag_delete_data( boxTag, &myRoot, 1 );
105       if (MB_SUCCESS != rval)
106         return rval;
107 
108       rval = mbImpl->delete_entities( &dead_sets[0], dead_sets.size() );
109       if (MB_SUCCESS != rval) return rval;
110 
111       myRoot = 0;
112 
113       return MB_SUCCESS;
114     }
115 
116 }
117