Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
FixPolys.cpp File Reference

Example demonstrating how to fix polygons with duplicated vertices. More...

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "moab/Core.hpp"
#include "moab/Interface.hpp"
#include "moab/Range.hpp"
#include "moab/ProgOptions.hpp"
+ Include dependency graph for FixPolys.cpp:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Example demonstrating how to fix polygons with duplicated vertices.

This example shows how to:

  • Load a mesh file containing polygons
  • Identify polygons with duplicated vertices
  • Remove consecutive duplicate vertices from polygon connectivity
  • Create new polygons with cleaned connectivity
  • Preserve global ID tags during polygon reconstruction
  • Write the corrected mesh to a new file

The program is useful for cleaning up meshes that have been created with padded vertices to reduce data sequences, but now need to be optimized by removing redundant vertex references.

Author
MOAB Development Team
Date
2024

This test shows how to fix polygons that have duplicated vertices

We sometimes use padded vertices option, to reduce the number of data sequences We identify first the polygons that have padded vertices then we set the new ones with reduced number of vertices, but with the same global id tag

Parameters
argcNumber of command line arguments
argvCommand line arguments array
Returns
0 on success, 1 on failure

Definition in file FixPolys.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 47 of file FixPolys.cpp.

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 }

References ProgOptions::addOpt(), moab::Range::begin(), moab::Core::create_element(), moab::Core::delete_entities(), moab::Range::end(), moab::Core::get_connectivity(), moab::Core::get_entities_by_dimension(), moab::Core::globalId_tag(), moab::Range::insert(), moab::Core::load_file(), mb, MB_CHK_SET_ERR, MBPOLYGON, ProgOptions::parseCommandLine(), moab::Range::size(), moab::Core::tag_get_data(), moab::Core::tag_set_data(), test_file_name, and moab::Core::write_file().