MOAB: Mesh Oriented datABase  (version 5.4.1)
mbex2.cpp
Go to the documentation of this file.
00001 /// \file mbex2.cpp
00002 ///
00003 /// \author Milad Fatenejad
00004 ///
00005 /// \brief beginner tutorial, example 2: Demonstrates loading a mesh
00006 ///        from a file, finding coordinate locations, connectivity
00007 ///        information...
00008 ///
00009 /// In this example, we read in the VTK file (mbex1.vtk) generated
00010 /// in example 1, pick one of the hexahedrons, find out the vertexes
00011 /// that define it (connectivity), and find the coordinates of those
00012 /// vertexes. Finally, we will rotate the mesh a little and write it
00013 /// out to a new file.
00014 
00015 // The moab/Core.hpp header file is needed for all MOAB work...
00016 #include "moab/Core.hpp"
00017 
00018 #include <iostream>
00019 #include <iomanip>
00020 #include <cmath>
00021 
00022 // ****************
00023 // *              *
00024 // *     main     *
00025 // *              *
00026 // ****************
00027 int main()
00028 {
00029     moab::ErrorCode rval;
00030     moab::Core mbcore;
00031     moab::Interface& mbint = mbcore;
00032 
00033     // First, lets load the file from the previous example. Note that
00034     // you must compile and run the previous example to get mbex1.vtk!
00035     rval = mbint.load_file( "mbex1.vtk" );MB_CHK_SET_ERR( rval, "load_file failed" );
00036 
00037     // *********************
00038     // *   Introspection   *
00039     // *********************
00040 
00041     // We can now access everything about the mesh through mbint. Lets
00042     // pick an element and find some information about it. In this case,
00043     // we know that all of the elements in the mesh are hexahedrons, so
00044     // we will ask MOAB for a range containing the handle for all hexes:
00045     moab::Range hex_range;
00046     rval = mbint.get_entities_by_type( 0, moab::MBHEX, hex_range );MB_CHK_SET_ERR( rval, "get_entities_by_type(HEX) failed" );
00047 
00048     std::cout << "Loaded a mesh containing: " << hex_range << std::endl;
00049 
00050     // Let's analyze one of the hexes (in this case, I picked hex three):
00051     moab::EntityHandle handle = hex_range[3];
00052 
00053     // Find out the eight vertexes that define this particular hex:
00054     moab::Range connectivity;
00055     rval = mbint.get_connectivity( &handle, 1, connectivity );MB_CHK_SET_ERR( rval, "get_connectivity failed" );
00056 
00057     // Connectivity should now contain the handles for the eight
00058     // vertexes of interest. Lets print the handle and coordinate of
00059     // each vertex. Note how we iterate through the range just like with
00060     // STL containers...
00061     double coord[3];
00062     moab::Range::iterator iter;
00063 
00064     std::cout << std::setw( 6 ) << "Handle" << std::setw( 10 ) << "X" << std::setw( 10 ) << "Y" << std::setw( 10 )
00065               << "Z" << std::endl;
00066 
00067     for( iter = connectivity.begin(); iter != connectivity.end(); ++iter )
00068     {
00069         rval = mbint.get_coords( &( *iter ), 1, coord );MB_CHK_SET_ERR( rval, "get_coords" );
00070 
00071         // Print the entity handle followed by the x, y, z coordinate of
00072         // the vertex:
00073         std::cout << std::setw( 6 ) << *iter << std::setw( 10 ) << coord[0] << std::setw( 10 ) << coord[1]
00074                   << std::setw( 10 ) << coord[2] << std::endl;
00075     }
00076 
00077     // ***********************
00078     // *   Rotate the Mesh   *
00079     // ***********************
00080 
00081     // Now, let's rotate the mesh about the z-axis by pi/4 radians. Do
00082     // to this, we will iterate through all of the vertexes
00083 
00084     // Start by getting a range containing all of the vertex handles:
00085     moab::Range vertex_range;
00086     rval = mbint.get_entities_by_type( 0, moab::MBVERTEX, vertex_range );MB_CHK_SET_ERR( rval, "get_entities_by_type(VERTEX) failed" );
00087 
00088     // Get the coordinates of all of the vertexes:
00089     std::vector< double > vertex_coords( 3 * vertex_range.size() );
00090     rval = mbint.get_coords( vertex_range, vertex_coords.data() );MB_CHK_SET_ERR( rval, "get_coords" );
00091 
00092     unsigned count     = 0;
00093     const double PI    = 3.14159265359;
00094     const double ANGLE = PI / 4;
00095     for( iter = vertex_range.begin(); iter != vertex_range.end(); ++iter )
00096     {
00097 
00098         // Save the old coordinates:
00099         double x = vertex_coords[count + 0];
00100         double y = vertex_coords[count + 1];
00101         // double z = vertex_coords[count+2];
00102 
00103         // Apply the rotation:
00104         vertex_coords[count + 0] = x * std::cos( ANGLE ) - y * std::sin( ANGLE );
00105         vertex_coords[count + 1] = x * std::sin( ANGLE ) + y * std::cos( ANGLE );
00106 
00107         count += 3;
00108     }
00109 
00110     // Now, the vertex_coords vector contains all of the updated
00111     // coordinates. Let's push these back into MOAB:
00112     mbint.set_coords( vertex_range, vertex_coords.data() );MB_CHK_SET_ERR( rval, "set_coords" );
00113 
00114     // **************************
00115     // *   Write Mesh to File   *
00116     // **************************
00117 
00118     rval = mbint.write_file( "mbex2.vtk" );MB_CHK_SET_ERR( rval, "write_file(mbex2.vtk) failed" );
00119 
00120     // Now you can open your favorite visualization tool (VisIt or ParaView)
00121     // and look at the original (mbex1.vtk) and rotated (mbex2.vtk) meshes.
00122 
00123     return 0;
00124 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines