1/** \brief This test shows how to fix polygons that have duplicated vertices
2 *
3 * We sometimes use padded vertices option, to reduce the number of data sequences
4 * We identify first the polygons that have padded vertices
5 * then we set the new ones with reduced number of vertices, but with the same global id tag
6 */78#include<iostream>9#include<cstdlib>10#include<cstdio>1112#include"moab/Core.hpp"13#include"moab/Interface.hpp"14#include"moab/Range.hpp"15#include"moab/ProgOptions.hpp"1617usingnamespace moab;
18usingnamespace std;
1920#ifdef MOAB_HAVE_HDF521 string test_file_name = string( MESH_DIR ) + string( "/64bricks_512hex_256part.h5m" );
22#endif23intmain( int argc, char** argv )
24 {
25 ProgOptions opts;
2627 string inputFile = test_file_name;
28 opts.addOpt< string >( "inFile,i", "Specify the input file name string ", &inputFile );
2930 string outFile = "out.h5m";
31 opts.addOpt< string >( "outFile,o", "Specify the output file name string ", &outFile );
3233 opts.parseCommandLine( argc, argv );
3435// Instantiate36 Core mb;
3738 ErrorCode rval = mb.load_file( inputFile.c_str() );MB_CHK_SET_ERR( rval, "Error loading file" );
3940 cout << " reading file " << inputFile << "\n";
4142// Get all 2d elements in the file set43 Range elems;
44 rval = mb.get_entities_by_dimension( 0, 2, elems );MB_CHK_SET_ERR( rval, "Error getting 2d elements" );
4546 cout << "number of cells: " << elems.size() << "\n";
4748 Tag gidTag = mb.globalId_tag();
49 Range OldCells;
50for( Range::iterator it = elems.begin(); it != elems.end(); ++it )
51 {
52 EntityHandle cell = *it;
53const EntityHandle* conn;
54int number_nodes;
55 rval = mb.get_connectivity( cell, conn, number_nodes );MB_CHK_SET_ERR( rval, "Error getting connectivity" );
56// now check if we have consecutive duplicated vertices, and if so, create a new cell57 std::vector< EntityHandle > new_verts;
58// push to it, if we do not have duplicates5960 EntityHandle current = conn[0];
61// new_verts.push_back(current);62for( int i = 1; i <= number_nodes; i++ )
63 {
64 EntityHandle nextV;
65if( i < number_nodes )
66 nextV = conn[i];
67else68 nextV = conn[0]; // first vertex69if( current != nextV )
70 {
71 new_verts.push_back( current );
72 current = nextV;
73 }
74 }
75if( number_nodes > (int)new_verts.size() )
76 {
77// create a new poly, and put this in a list to be removed78int gid;
79 rval = mb.tag_get_data( gidTag, &cell, 1, &gid );MB_CHK_SET_ERR( rval, "Error getting global id tag" );
80 EntityHandle newCell;
81 rval = mb.create_element( MBPOLYGON, &new_verts[0], (int)new_verts.size(), newCell );MB_CHK_SET_ERR( rval, "Error creating new polygon " );
82 rval = mb.tag_set_data( gidTag, &newCell, 1, &gid );MB_CHK_SET_ERR( rval, "Error setting global id tag" );
83 cout << "delete old cell " << cell << " with num_nodes vertices: " << number_nodes
84 << " and with global id: " << gid << "\n";
85for( int i = 0; i < number_nodes; i++ )
86 {
87 cout << " " << conn[i];
88 }
89 cout << "\n";
90 OldCells.insert( cell );
91 }
92 mb.delete_entities( OldCells );
93 }
94 rval = mb.write_file( outFile.c_str() );MB_CHK_SET_ERR( rval, "Error writing file" );
9596return0;
97 }