1/*
2 This example takes a .cub mesh file as input and prints out the total area of
3 meshes in each surface of the model. It works for tri and quad elements.
4 It makes use of CUBIT's reserved tag - GEOM_DIMENSION and GLOBAL_ID tag.
5 Both GLOBAL_ID & GEOM_DIMENSION tag are associated with all the geometric
6 entities in a .cub file. Note: The program would give incorrect result for a
7 non-convex element, since it breaks polygons into triangles for computing the area
8 */910#include"moab/Core.hpp"11#include"moab/Range.hpp"12#include"moab/CartVect.hpp"13#include<iostream>1415usingnamespace moab;
1617doublecompute_area( std::vector< EntityHandle >& );
1819// instantiate20 Interface* mb;
2122intmain( int argc, char** argv )
23 {
24if( 1 == argc )
25 {
26 std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
27return0;
28 }
2930// declare variables31 Tag gtag, idtag;
32 ErrorCode rval;
33constchar* tag_geom = "GEOM_DIMENSION";
34constchar* tag_gid = "GLOBAL_ID";
35 Range sets;
36 std::vector< EntityHandle > ents;
3738// load a file39 mb = newCore();
40 rval = mb->load_file( argv[1] );
41if( MB_SUCCESS != rval ) return1;
4243// get the tag handle for the tags44 rval = mb->tag_get_handle( tag_geom, 1, MB_TYPE_INTEGER, gtag );
45if( MB_SUCCESS != rval ) return1;
46 rval = mb->tag_get_handle( tag_gid, 1, MB_TYPE_INTEGER, idtag );
47if( MB_SUCCESS != rval ) return1;
4849// get all the sets with GEOM_DIMESION tag50 rval = mb->get_entities_by_type_and_tag( 0, MBENTITYSET, >ag, NULL, 1, sets );
51if( MB_SUCCESS != rval ) return1;
5253// iterate over each set, getting entities54 Range::iterator set_it;
5556// loop thru all the geometric entity sets57for( set_it = sets.begin(); set_it != sets.end(); ++set_it )
58 {
5960 EntityHandle this_set = *set_it;
6162// get the id for this set63int set_id;
64 rval = mb->tag_get_data( gtag, &this_set, 1, &set_id );
65if( MB_SUCCESS != rval ) return1;
6667// check if it is a surface entities (GEOM_DIMENSION 2) then compute area68if( set_id == 2 )
69 {
7071// area of a surface72double total_area = 0.0;
7374// get the global id of this surface75int gid = 0;
76 rval = mb->tag_get_data( idtag, &this_set, 1, &gid );
77if( MB_SUCCESS != rval ) return1;
7879// get all entities with dimension 2 in ents80 rval = mb->get_entities_by_dimension( this_set, 2, ents );
81if( MB_SUCCESS != rval ) return1;
8283// compute the area84 total_area = compute_area( ents );
8586 ents.clear();
8788 std::cout << "Total area of meshes in surface " << gid << " = " << total_area << std::endl;
89 }
90 }
91 }
9293// This routine takes all the element entities in a face as input and computes the surface area94// iterating over each element95doublecompute_area( std::vector< EntityHandle >& entities )
96 {
9798 ErrorCode rval = MB_SUCCESS;
99double area = 0.0;
100101// loop thro' all the elements102for( int i = 0; i < int( entities.size() ); i++ )
103 {
104 std::vector< EntityHandle > conn;
105 EntityHandle handle = entities[i];
106107// get the connectivity of this element108 rval = mb->get_connectivity( &handle, 1, conn );
109if( MB_SUCCESS != rval ) return-1.0;
110111// break polygon into triangles and sum the area - Limitation: Convex polygon112for( int j = 2; j <= int( conn.size() ); ++j )
113 {
114115 EntityHandle vertices[3] = { conn[0], conn[j - 1], conn[j - 2] };
116 CartVect coords[3];
117118// get 3 coordinates forming the triangle119 rval = mb->get_coords( vertices, 3, coords[0].array() );
120if( MB_SUCCESS != rval ) return-1.0;
121122 CartVect edge0 = coords[1] - coords[0];
123 CartVect edge1 = coords[2] - coords[0];
124125// using MBCarVect overloaded operators and computing triangle area126 area += ( edge0 * edge1 ).length() / 2.0;
127 }
128 }
129// clear the entities, else old entities remain130 entities.clear();
131return area;
132 }