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