Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
FixPolys.cpp
Go to the documentation of this file.
00001 /** \brief This test shows how to fix polygons that have duplicated vertices
00002  *
00003  * We sometimes use padded vertices option, to reduce the number of data sequences
00004  * We identify first the polygons that have padded vertices
00005  * then we set the new ones with reduced number of vertices, but with the same global id tag
00006  */
00007 
00008 #include <iostream>
00009 #include <cstdlib>
00010 #include <cstdio>
00011 
00012 #include "moab/Core.hpp"
00013 #include "moab/Interface.hpp"
00014 #include "moab/Range.hpp"
00015 #include "moab/ProgOptions.hpp"
00016 
00017 using namespace moab;
00018 using namespace std;
00019 
00020 #ifdef MOAB_HAVE_HDF5
00021 string test_file_name = string( MESH_DIR ) + string( "/64bricks_512hex_256part.h5m" );
00022 #endif
00023 int main( int argc, char** argv )
00024 {
00025     ProgOptions opts;
00026 
00027     string inputFile = test_file_name;
00028     opts.addOpt< string >( "inFile,i", "Specify the input file name string ", &inputFile );
00029 
00030     string outFile = "out.h5m";
00031     opts.addOpt< string >( "outFile,o", "Specify the output file name string ", &outFile );
00032 
00033     opts.parseCommandLine( argc, argv );
00034 
00035     // Instantiate
00036     Core mb;
00037 
00038     ErrorCode rval = mb.load_file( inputFile.c_str() );MB_CHK_SET_ERR( rval, "Error loading file" );
00039 
00040     cout << " reading file " << inputFile << "\n";
00041 
00042     // Get all 2d elements in the file set
00043     Range elems;
00044     rval = mb.get_entities_by_dimension( 0, 2, elems );MB_CHK_SET_ERR( rval, "Error getting 2d elements" );
00045 
00046     cout << "number of cells: " << elems.size() << "\n";
00047 
00048     Tag gidTag = mb.globalId_tag();
00049     Range OldCells;
00050     for( Range::iterator it = elems.begin(); it != elems.end(); ++it )
00051     {
00052         EntityHandle cell = *it;
00053         const EntityHandle* conn;
00054         int number_nodes;
00055         rval = mb.get_connectivity( cell, conn, number_nodes );MB_CHK_SET_ERR( rval, "Error getting connectivity" );
00056         // now check if we have consecutive duplicated vertices, and if so, create a new cell
00057         std::vector< EntityHandle > new_verts;
00058         // push to it, if we do not have duplicates
00059 
00060         EntityHandle current = conn[0];
00061         // new_verts.push_back(current);
00062         for( int i = 1; i <= number_nodes; i++ )
00063         {
00064             EntityHandle nextV;
00065             if( i < number_nodes )
00066                 nextV = conn[i];
00067             else
00068                 nextV = conn[0];  // first vertex
00069             if( current != nextV )
00070             {
00071                 new_verts.push_back( current );
00072                 current = nextV;
00073             }
00074         }
00075         if( number_nodes > (int)new_verts.size() )
00076         {
00077             // create a new poly, and put this in a list to be removed
00078             int gid;
00079             rval = mb.tag_get_data( gidTag, &cell, 1, &gid );MB_CHK_SET_ERR( rval, "Error getting global id tag" );
00080             EntityHandle newCell;
00081             rval = mb.create_element( MBPOLYGON, &new_verts[0], (int)new_verts.size(), newCell );MB_CHK_SET_ERR( rval, "Error creating new polygon " );
00082             rval = mb.tag_set_data( gidTag, &newCell, 1, &gid );MB_CHK_SET_ERR( rval, "Error setting global id tag" );
00083             cout << "delete old cell " << cell << " with num_nodes vertices: " << number_nodes
00084                  << " and with global id: " << gid << "\n";
00085             for( int i = 0; i < number_nodes; i++ )
00086             {
00087                 cout << " " << conn[i];
00088             }
00089             cout << "\n";
00090             OldCells.insert( cell );
00091         }
00092         mb.delete_entities( OldCells );
00093     }
00094     rval = mb.write_file( outFile.c_str() );MB_CHK_SET_ERR( rval, "Error writing file" );
00095 
00096     return 0;
00097 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines