Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
mbex2.cpp
Go to the documentation of this file.
1 /** @example mbex2.cpp
2  * \brief beginner tutorial, example 2: Demonstrates loading a mesh
3  * from a file, finding coordinate locations, connectivity
4  * information...
5  *
6  * In this example, we read in the VTK file (mbex1.vtk) generated
7  * in example 1, pick one of the hexahedrons, find out the vertexes
8  * that define it (connectivity), and find the coordinates of those
9  * vertexes. Finally, we will rotate the mesh a little and write it
10  * out to a new file.
11  *
12  * \author Milad Fatenejad
13  */
14 
15 // The moab/Core.hpp header file is needed for all MOAB work...
16 #include "moab/Core.hpp"
17 
18 #include <iostream>
19 #include <iomanip>
20 #include <cmath>
21 
22 // ****************
23 // * *
24 // * main *
25 // * *
26 // ****************
27 int main()
28 {
29  moab::ErrorCode rval;
30  moab::Core mbcore;
31  moab::Interface& mbint = mbcore;
32 
33  // First, lets load the file from the previous example. Note that
34  // you must compile and run the previous example to get mbex1.vtk!
35  MB_CHK_SET_ERR( mbint.load_file( "mbex1.vtk" ), "load_file failed" );
36 
37  // *********************
38  // * Introspection *
39  // *********************
40 
41  // We can now access everything about the mesh through mbint. Lets
42  // pick an element and find some information about it. In this case,
43  // we know that all of the elements in the mesh are hexahedrons, so
44  // we will ask MOAB for a range containing the handle for all hexes:
45  moab::Range hex_range;
46  MB_CHK_SET_ERR( mbint.get_entities_by_type( 0, moab::MBHEX, hex_range ), "get_entities_by_type(HEX) failed" );
47  std::cout << "Loaded a mesh containing: " << hex_range << std::endl;
48 
49  // Let's analyze one of the hexes (in this case, I picked hex three):
50  moab::EntityHandle handle = hex_range[3];
51 
52  // Find out the eight vertexes that define this particular hex:
53  moab::Range connectivity;
54  MB_CHK_SET_ERR( mbint.get_connectivity( &handle, 1, connectivity ), "get_connectivity failed" );
55 
56  // Connectivity should now contain the handles for the eight
57  // vertexes of interest. Lets print the handle and coordinate of
58  // each vertex. Note how we iterate through the range just like with
59  // STL containers...
60  double coord[3];
62 
63  std::cout << std::setw( 6 ) << "Handle" << std::setw( 10 ) << "X" << std::setw( 10 ) << "Y" << std::setw( 10 )
64  << "Z" << std::endl;
65 
66  for( iter = connectivity.begin(); iter != connectivity.end(); ++iter )
67  {
68  MB_CHK_SET_ERR( mbint.get_coords( &( *iter ), 1, coord ), "get_coords" );
69 
70  // Print the entity handle followed by the x, y, z coordinate of
71  // the vertex:
72  std::cout << std::setw( 6 ) << *iter << std::setw( 10 ) << coord[0] << std::setw( 10 ) << coord[1]
73  << std::setw( 10 ) << coord[2] << std::endl;
74  }
75 
76  // ***********************
77  // * Rotate the Mesh *
78  // ***********************
79 
80  // Now, let's rotate the mesh about the z-axis by pi/4 radians. Do
81  // to this, we will iterate through all of the vertexes
82 
83  // Start by getting a range containing all of the vertex handles:
84  moab::Range vertex_range;
85  MB_CHK_SET_ERR( mbint.get_entities_by_type( 0, moab::MBVERTEX, vertex_range ),
86  "get_entities_by_type(VERTEX) failed" );
87 
88  // Get the coordinates of all of the vertexes:
89  std::vector< double > vertex_coords( 3 * vertex_range.size() );
90  MB_CHK_SET_ERR( mbint.get_coords( vertex_range, vertex_coords.data() ), "get_coords" );
91 
92  unsigned count = 0;
93  const double PI = 3.14159265359;
94  const double ANGLE = PI / 4;
95  for( iter = vertex_range.begin(); iter != vertex_range.end(); ++iter )
96  {
97  // Save the old coordinates:
98  double x = vertex_coords[count + 0];
99  double y = vertex_coords[count + 1];
100  // double z = vertex_coords[count+2];
101 
102  // Apply the rotation:
103  vertex_coords[count + 0] = x * std::cos( ANGLE ) - y * std::sin( ANGLE );
104  vertex_coords[count + 1] = x * std::sin( ANGLE ) + y * std::cos( ANGLE );
105 
106  count += 3;
107  }
108 
109  // Now, the vertex_coords vector contains all of the updated
110  // coordinates. Let's push these back into MOAB:
111  MB_CHK_SET_ERR( mbint.set_coords( vertex_range, vertex_coords.data() ), "set_coords" );
112 
113  // **************************
114  // * Write Mesh to File *
115  // **************************
116  MB_CHK_SET_ERR( mbint.write_file( "mbex2.vtk" ), "write_file(mbex2.vtk) failed" );
117 
118  // Now you can open your favorite visualization tool (VisIt or ParaView)
119  // and look at the original (mbex1.vtk) and rotated (mbex2.vtk) meshes.
120  return 0;
121 }