moab
|
00001 #include "moab/Range.hpp" 00002 #include "moab/Core.hpp" 00003 #include "moab/Skinner.hpp" 00004 #include <iostream> 00005 #include <stdlib.h> 00006 00007 using namespace moab; 00008 00009 enum { 00010 NO_ERROR= 0, 00011 SYNTAX_ERROR = 1, 00012 FILE_IO_ERROR = 2, 00013 INTERNAL_ERROR = 3 00014 }; 00015 00016 const char* DEFAULT_TAG_NAME = "depth"; 00017 00018 static void usage( const char* argv0 ) 00019 { 00020 std::cerr << "Usage: " << argv0 << "[-t <tag name] <input_file> <output_file>" << std::endl 00021 << argv0 << "-h" << std::endl; 00022 exit(SYNTAX_ERROR); 00023 } 00024 00025 static void check( ErrorCode rval ) 00026 { 00027 if (MB_SUCCESS != rval) { 00028 std::cerr << "Internal error. Aborting." << std::endl; 00029 exit(INTERNAL_ERROR); 00030 } 00031 } 00032 00033 static void tag_depth( Interface& moab, Tag tag ); 00034 00035 int main( int argc, char* argv[] ) 00036 { 00037 const char *input = 0, *output = 0, *tagname = DEFAULT_TAG_NAME; 00038 bool expect_tag_name = false; 00039 for (int i = 1; i < argc; ++i) { 00040 if (expect_tag_name) { 00041 tagname = argv[i]; 00042 expect_tag_name = false; 00043 } 00044 else if (!strcmp("-t", argv[i])) 00045 expect_tag_name = true; 00046 else if (input == 0) 00047 input = argv[i]; 00048 else if (output == 0) 00049 output = argv[i]; 00050 else { 00051 std::cerr << "Unexpected argument: '" << argv[i] << "'" << std::endl; 00052 usage(argv[0]); 00053 } 00054 } 00055 00056 if (expect_tag_name) { 00057 std::cerr << "Expected argument following '-t'" << std::endl; 00058 usage(argv[0]); 00059 } 00060 if (!input) { 00061 std::cerr << "No input file" << std::endl; 00062 usage(argv[0]); 00063 } 00064 if (!output) { 00065 std::cerr << "No output file" << std::endl; 00066 usage(argv[0]); 00067 } 00068 00069 Core moab; 00070 Interface& mb = moab; 00071 00072 EntityHandle file; 00073 ErrorCode rval; 00074 rval = mb.create_meshset( MESHSET_SET, file ); check(rval); 00075 rval = mb.load_file( input, &file ); 00076 if (MB_SUCCESS != rval) { 00077 std::cerr << "Failed to load file: " << input << std::endl; 00078 return FILE_IO_ERROR; 00079 } 00080 00081 int init_val = -1; 00082 Tag tag; 00083 bool created; 00084 rval = mb.tag_get_handle( tagname, 1, MB_TYPE_INTEGER, tag, MB_TAG_DENSE|MB_TAG_CREAT, &init_val, &created ); 00085 if (!created) { 00086 rval = mb.tag_delete( tag ); check(rval); 00087 rval = mb.tag_get_handle( tagname, 1, MB_TYPE_INTEGER, tag, MB_TAG_DENSE|MB_TAG_CREAT, &init_val, &created ); 00088 check(rval); 00089 } 00090 00091 tag_depth( mb, tag ); 00092 00093 rval = mb.write_file( output, 0, 0, &file, 1 ); 00094 if (rval == MB_SUCCESS) 00095 std::cout << "Wrote file: " << output << std::endl; 00096 else { 00097 std::cerr << "Failed to write file: " << output << std::endl; 00098 return FILE_IO_ERROR; 00099 } 00100 00101 return NO_ERROR; 00102 } 00103 00104 static ErrorCode get_adjacent_elems( Interface& mb, const Range& verts, Range& elems ) 00105 { 00106 elems.clear(); 00107 ErrorCode rval; 00108 for (int dim = 3; dim > 0; --dim) { 00109 rval = mb.get_adjacencies( verts, dim, false, elems, Interface::UNION ); 00110 if (MB_SUCCESS != rval) 00111 break; 00112 } 00113 return rval; 00114 } 00115 00116 void tag_depth( Interface& mb, Tag tag ) 00117 { 00118 ErrorCode rval; 00119 int dim; 00120 00121 Skinner tool(&mb); 00122 Range verts, elems; 00123 dim = 3; 00124 while (elems.empty()) { 00125 rval = mb.get_entities_by_dimension( 0, dim, elems ); check(rval); 00126 if (--dim == 0) 00127 return; // no elements 00128 } 00129 rval = tool.find_skin( 0, elems, 0, verts ); check(rval); 00130 rval = get_adjacent_elems( mb, verts, elems ); check(rval); 00131 00132 std::vector<int> data; 00133 int val, depth = 0; 00134 while (!elems.empty()) { 00135 data.clear(); 00136 data.resize( elems.size(), depth++ ); 00137 rval = mb.tag_set_data( tag, elems, &data[0] ); check(rval); 00138 00139 verts.clear(); 00140 rval = mb.get_adjacencies( elems, 0, false, verts, Interface::UNION ); 00141 check(rval); 00142 00143 Range tmp; 00144 rval = get_adjacent_elems( mb, verts, tmp ); check(rval); 00145 elems.clear(); 00146 for (Range::reverse_iterator i = tmp.rbegin(); i != tmp.rend(); ++i) { 00147 rval = mb.tag_get_data( tag, &*i, 1, &val ); check(rval); 00148 if (val == -1) 00149 elems.insert( *i ); 00150 } 00151 } 00152 00153 std::cout << "Maximum depth: " << depth << std::endl; 00154 }