1 #include "moab/Range.hpp"
2 #include "moab/Core.hpp"
3 #include "moab/Skinner.hpp"
4 #include <iostream>
5 #include <stdlib.h>
6 
7 using namespace moab;
8 
9 enum {
10   NO_ERROR= 0,
11   SYNTAX_ERROR = 1,
12   FILE_IO_ERROR = 2,
13   INTERNAL_ERROR = 3
14 };
15 
16 const char* DEFAULT_TAG_NAME = "depth";
17 
usage(const char * argv0)18 static void usage( const char* argv0 )
19 {
20   std::cerr << "Usage: " << argv0 << "[-t <tag name] <input_file> <output_file>" << std::endl
21                          << argv0 << "-h" << std::endl;
22   exit(SYNTAX_ERROR);
23 }
24 
check(ErrorCode rval)25 static void check( ErrorCode rval )
26 {
27   if (MB_SUCCESS != rval) {
28     std::cerr << "Internal error.  Aborting." << std::endl;
29     exit(INTERNAL_ERROR);
30   }
31 }
32 
33 static void tag_depth( Interface& moab, Tag tag );
34 
main(int argc,char * argv[])35 int main( int argc, char* argv[] )
36 {
37   const char *input = 0, *output = 0, *tagname = DEFAULT_TAG_NAME;
38   bool expect_tag_name = false;
39   for (int i = 1; i < argc; ++i) {
40     if (expect_tag_name) {
41       tagname = argv[i];
42       expect_tag_name = false;
43     }
44     else if (!strcmp("-t", argv[i]))
45       expect_tag_name = true;
46     else if (input == 0)
47       input = argv[i];
48     else if (output == 0)
49       output = argv[i];
50     else {
51       std::cerr << "Unexpected argument: '" << argv[i] << "'" << std::endl;
52       usage(argv[0]);
53     }
54   }
55 
56   if (expect_tag_name) {
57     std::cerr << "Expected argument following '-t'" << std::endl;
58     usage(argv[0]);
59   }
60   if (!input) {
61     std::cerr << "No input file" << std::endl;
62     usage(argv[0]);
63   }
64   if (!output) {
65     std::cerr << "No output file" << std::endl;
66     usage(argv[0]);
67   }
68 
69   Core moab;
70   Interface& mb = moab;
71 
72   EntityHandle file;
73   ErrorCode rval;
74   rval = mb.create_meshset( MESHSET_SET, file ); check(rval);
75   rval = mb.load_file( input, &file );
76   if (MB_SUCCESS != rval) {
77     std::cerr << "Failed to load file: " << input << std::endl;
78     return FILE_IO_ERROR;
79   }
80 
81   int init_val = -1;
82   Tag tag;
83   bool created;
84   rval = mb.tag_get_handle( tagname, 1, MB_TYPE_INTEGER, tag, MB_TAG_DENSE|MB_TAG_CREAT, &init_val, &created );
85   if (!created) {
86     rval = mb.tag_delete( tag ); check(rval);
87     rval = mb.tag_get_handle( tagname, 1, MB_TYPE_INTEGER, tag, MB_TAG_DENSE|MB_TAG_CREAT, &init_val, &created );
88     check(rval);
89   }
90 
91   tag_depth( mb, tag );
92 
93   rval = mb.write_file( output, 0, 0, &file, 1 );
94   if (rval == MB_SUCCESS)
95     std::cout << "Wrote file: " << output << std::endl;
96   else {
97     std::cerr << "Failed to write file: " << output << std::endl;
98     return FILE_IO_ERROR;
99   }
100 
101   return NO_ERROR;
102 }
103 
get_adjacent_elems(Interface & mb,const Range & verts,Range & elems)104 static ErrorCode get_adjacent_elems( Interface& mb, const Range& verts, Range& elems )
105 {
106   elems.clear();
107   ErrorCode rval;
108   for (int dim = 3; dim > 0; --dim) {
109     rval = mb.get_adjacencies( verts, dim, false, elems, Interface::UNION );
110     if (MB_SUCCESS != rval)
111       break;
112   }
113   return rval;
114 }
115 
tag_depth(Interface & mb,Tag tag)116 void tag_depth( Interface& mb, Tag tag )
117 {
118   ErrorCode rval;
119   int dim;
120 
121   Skinner tool(&mb);
122   Range verts, elems;
123   dim = 3;
124   while (elems.empty()) {
125     rval = mb.get_entities_by_dimension( 0, dim, elems ); check(rval);
126     if (--dim == 0)
127       return; // no elements
128   }
129   rval = tool.find_skin( 0, elems, 0, verts ); check(rval);
130   rval = get_adjacent_elems( mb, verts, elems ); check(rval);
131 
132   std::vector<int> data;
133   int val, depth = 0;
134   while (!elems.empty()) {
135     data.clear();
136     data.resize( elems.size(), depth++ );
137     rval = mb.tag_set_data( tag, elems, &data[0] ); check(rval);
138 
139     verts.clear();
140     rval = mb.get_adjacencies( elems, 0, false, verts, Interface::UNION );
141     check(rval);
142 
143     Range tmp;
144     rval = get_adjacent_elems( mb, verts, tmp ); check(rval);
145     elems.clear();
146     for (Range::reverse_iterator i = tmp.rbegin(); i != tmp.rend(); ++i) {
147       rval = mb.tag_get_data( tag, &*i, 1, &val ); check(rval);
148       if (val == -1)
149         elems.insert( *i );
150     }
151   }
152 
153   std::cout << "Maximum depth: " << depth << std::endl;
154 }
155