MOAB: Mesh Oriented datABase  (version 5.5.0)
lloyd_smoother_test.cpp
Go to the documentation of this file.
1 #include "moab/Core.hpp"
2 #include "moab/LloydSmoother.hpp"
3 #include "moab/CartVect.hpp"
4 #include "TestUtil.hpp"
5 
6 std::string filename = TestDir + "unittest/surfrandomtris-4part.h5m";
7 
8 using namespace moab;
9 
10 int main( int argc, char** argv )
11 {
12  if( argc > 1 ) filename = std::string( argv[1] );
13  Core mb;
14  ErrorCode rval = mb.load_file( filename.c_str() );CHECK_ERR( rval );
15 
16  Range elems;
17  rval = mb.get_entities_by_dimension( 0, 3, elems );CHECK_ERR( rval );
18  if( elems.empty() )
19  {
20  rval = mb.get_entities_by_dimension( 0, 2, elems );CHECK_ERR( rval );
21  }
22  if( elems.empty() )
23  {
24  std::cout << "Mesh must have faces or regions for this test." << std::endl;CHECK_ERR( MB_FAILURE );
25  }
26 
27  // get the vertex positions and set on an intermediate tag, to test smoothing for
28  // a tag instead of for coords
29  Tag ctag;
30  rval = mb.tag_get_handle( "vcentroid", 3, MB_TYPE_DOUBLE, ctag, MB_TAG_CREAT | MB_TAG_DENSE );CHECK_ERR( rval );
31  Range verts;
32  rval = mb.get_entities_by_dimension( 0, 0, verts );CHECK_ERR( rval );
33  std::vector< double > coords( 3 * verts.size() );
34  rval = mb.get_coords( verts, &coords[0] );CHECK_ERR( rval );
35  rval = mb.tag_set_data( ctag, verts, &coords[0] );CHECK_ERR( rval );
36 
37  LloydSmoother ll( &mb, NULL, elems, ctag );
38  ll.report_its( 10 );
39  rval = ll.perform_smooth();CHECK_ERR( rval );
40  std::cout << "Mesh smoothed in " << ll.num_its() << " iterations." << std::endl;
41 
42  // now, set vertex coords to almost their converged positions, then re-smooth; should take fewer
43  // iterations
44  std::vector< double > new_coords( 3 * verts.size() );
45  rval = mb.tag_get_data( ctag, verts, &new_coords[0] );CHECK_ERR( rval );
46  unsigned int i;
47  Range::iterator vit;
48  for( vit = verts.begin(), i = 0; vit != verts.end(); ++vit, i += 3 )
49  {
50  CartVect old_pos( &coords[i] ), new_pos( &new_coords[i] );
51  CartVect almost_pos = old_pos + .99 * ( new_pos - old_pos );
52  almost_pos.get( &new_coords[i] );
53  }
54  rval = mb.set_coords( verts, &new_coords[0] );CHECK_ERR( rval );
55 
56  LloydSmoother ll2( &mb, NULL, elems );
57  ll2.report_its( 10 );
58  rval = ll2.perform_smooth();CHECK_ERR( rval );
59  std::cout << "Mesh smoothed in " << ll2.num_its() << " iterations." << std::endl;
60 }