Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
FixPolys.cpp
Go to the documentation of this file.
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  */
7 
8 #include <iostream>
9 #include <cstdlib>
10 #include <cstdio>
11 
12 #include "moab/Core.hpp"
13 #include "moab/Interface.hpp"
14 #include "moab/Range.hpp"
15 #include "moab/ProgOptions.hpp"
16 
17 using namespace moab;
18 using namespace std;
19 
20 #ifdef MOAB_HAVE_HDF5
21 string test_file_name = string( MESH_DIR ) + string( "/64bricks_512hex_256part.h5m" );
22 #endif
23 int main( int argc, char** argv )
24 {
25  ProgOptions opts;
26 
27  string inputFile = test_file_name;
28  opts.addOpt< string >( "inFile,i", "Specify the input file name string ", &inputFile );
29 
30  string outFile = "out.h5m";
31  opts.addOpt< string >( "outFile,o", "Specify the output file name string ", &outFile );
32 
33  opts.parseCommandLine( argc, argv );
34 
35  // Instantiate
36  Core mb;
37 
38  ErrorCode rval = mb.load_file( inputFile.c_str() );MB_CHK_SET_ERR( rval, "Error loading file" );
39 
40  cout << " reading file " << inputFile << "\n";
41 
42  // Get all 2d elements in the file set
43  Range elems;
44  rval = mb.get_entities_by_dimension( 0, 2, elems );MB_CHK_SET_ERR( rval, "Error getting 2d elements" );
45 
46  cout << "number of cells: " << elems.size() << "\n";
47 
48  Tag gidTag = mb.globalId_tag();
49  Range OldCells;
50  for( Range::iterator it = elems.begin(); it != elems.end(); ++it )
51  {
52  EntityHandle cell = *it;
53  const EntityHandle* conn;
54  int 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 cell
57  std::vector< EntityHandle > new_verts;
58  // push to it, if we do not have duplicates
59 
60  EntityHandle current = conn[0];
61  // new_verts.push_back(current);
62  for( int i = 1; i <= number_nodes; i++ )
63  {
64  EntityHandle nextV;
65  if( i < number_nodes )
66  nextV = conn[i];
67  else
68  nextV = conn[0]; // first vertex
69  if( current != nextV )
70  {
71  new_verts.push_back( current );
72  current = nextV;
73  }
74  }
75  if( number_nodes > (int)new_verts.size() )
76  {
77  // create a new poly, and put this in a list to be removed
78  int 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";
85  for( 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" );
95 
96  return 0;
97 }