Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
FixPolys.cpp
Go to the documentation of this file.
1 /**
2  * @file FixPolys.cpp
3  * @brief Example demonstrating how to fix polygons with duplicated vertices
4  *
5  * This example shows how to:
6  * - Load a mesh file containing polygons
7  * - Identify polygons with duplicated vertices
8  * - Remove consecutive duplicate vertices from polygon connectivity
9  * - Create new polygons with cleaned connectivity
10  * - Preserve global ID tags during polygon reconstruction
11  * - Write the corrected mesh to a new file
12  *
13  * The program is useful for cleaning up meshes that have been created
14  * with padded vertices to reduce data sequences, but now need to be
15  * optimized by removing redundant vertex references.
16  *
17  * @author MOAB Development Team
18  * @date 2024
19  *
20 
21  * \brief This test shows how to fix polygons that have duplicated vertices
22  *
23  * We sometimes use padded vertices option, to reduce the number of data sequences
24  * We identify first the polygons that have padded vertices
25  * then we set the new ones with reduced number of vertices, but with the same global id tag
26  *
27  * @param argc Number of command line arguments
28  * @param argv Command line arguments array
29  * @return 0 on success, 1 on failure
30  */
31 
32 #include <iostream>
33 #include <cstdlib>
34 #include <cstdio>
35 
36 #include "moab/Core.hpp"
37 #include "moab/Interface.hpp"
38 #include "moab/Range.hpp"
39 #include "moab/ProgOptions.hpp"
40 
41 using namespace moab;
42 using namespace std;
43 
44 #ifdef MOAB_HAVE_HDF5
45 string test_file_name = string( MESH_DIR ) + string( "/64bricks_512hex_256part.h5m" );
46 #endif
47 int main( int argc, char** argv )
48 {
49  ProgOptions opts;
50 
51  string inputFile = test_file_name;
52  opts.addOpt< string >( "inFile,i", "Specify the input file name string ", &inputFile );
53 
54  string outFile = "out.h5m";
55  opts.addOpt< string >( "outFile,o", "Specify the output file name string ", &outFile );
56 
57  opts.parseCommandLine( argc, argv );
58 
59  // Instantiate
60  Core mb;
61 
62  MB_CHK_SET_ERR( mb.load_file( inputFile.c_str() ), "Error loading file" );
63 
64  cout << " reading file " << inputFile << "\n";
65 
66  // Get all 2d elements in the file set
67  Range elems;
68  MB_CHK_SET_ERR( mb.get_entities_by_dimension( 0, 2, elems ), "Error getting 2d elements" );
69 
70  cout << "number of cells: " << elems.size() << "\n";
71 
72  Tag gidTag = mb.globalId_tag();
73  Range OldCells;
74  for( Range::iterator it = elems.begin(); it != elems.end(); ++it )
75  {
76  EntityHandle cell = *it;
77  const EntityHandle* conn;
78  int number_nodes;
79  MB_CHK_SET_ERR( mb.get_connectivity( cell, conn, number_nodes ), "Error getting connectivity" );
80  // now check if we have consecutive duplicated vertices, and if so, create a new cell
81  std::vector< EntityHandle > new_verts;
82  // push to it, if we do not have duplicates
83 
84  EntityHandle current = conn[0];
85  // new_verts.push_back(current);
86  for( int i = 1; i <= number_nodes; i++ )
87  {
88  EntityHandle nextV;
89  if( i < number_nodes )
90  nextV = conn[i];
91  else
92  nextV = conn[0]; // first vertex
93  if( current != nextV )
94  {
95  new_verts.push_back( current );
96  current = nextV;
97  }
98  }
99  if( number_nodes > (int)new_verts.size() )
100  {
101  // create a new poly, and put this in a list to be removed
102  int gid;
103  MB_CHK_SET_ERR( mb.tag_get_data( gidTag, &cell, 1, &gid ), "Error getting global id tag" );
104  EntityHandle newCell;
105  MB_CHK_SET_ERR( mb.create_element( MBPOLYGON, &new_verts[0], (int)new_verts.size(), newCell ),
106  "Error creating new polygon " );
107  MB_CHK_SET_ERR( mb.tag_set_data( gidTag, &newCell, 1, &gid ), "Error setting global id tag" );
108  cout << "delete old cell " << cell << " with num_nodes vertices: " << number_nodes
109  << " and with global id: " << gid << "\n";
110  for( int i = 0; i < number_nodes; i++ )
111  {
112  cout << " " << conn[i];
113  }
114  cout << "\n";
115  OldCells.insert( cell );
116  }
117  mb.delete_entities( OldCells );
118  }
119  MB_CHK_SET_ERR( mb.write_file( outFile.c_str() ), "Error writing file" );
120 
121  return 0;
122 }