Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
GeomSetHierarchy.cpp
Go to the documentation of this file.
1 /** @example GeomSetHierarchy.cpp
2  * This example demonstrates geometric topology hierarchy traversal.
3  * It shows how to load a mesh file with geometric topology information,
4  * traverse the geometric hierarchy from regions down to vertices,
5  * access parent-child relationships between geometric entities,
6  * and determine geometric sense (forward/reverse) between entities.
7  * This tool is useful for understanding the geometric structure
8  * of CAD-based meshes and their hierarchical relationships.
9  */
10 
11 #include "moab/Core.hpp"
12 #include "moab/Range.hpp"
13 #include "MBCN.hpp"
14 #include "MBTagConventions.hpp"
15 #include "moab/GeomTopoTool.hpp"
16 #include <iostream>
17 #include <cassert>
18 
19 const char* ent_names[] = { "Vertex", "Edge", "Face", "Region" };
20 
21 int main( int argc, char** argv )
22 {
23  if( 1 == argc )
24  {
25  std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
26  return 0;
27  }
28 
29  // instantiate & load a file
30  moab::Interface* mb = new moab::Core();
31  MB_CHK_SET_ERR( mb->load_file( argv[1] ), "Failed to load file" );
32 
33  // get the geometric topology tag handle
34  moab::Tag geom_tag, gid_tag;
35  MB_CHK_SET_ERR( mb->tag_get_handle( GEOM_DIMENSION_TAG_NAME, 1, moab::MB_TYPE_INTEGER, geom_tag ),
36  "Failed to get geometric dimension tag" );
37  gid_tag = mb->globalId_tag();
38  assert( NULL != gid_tag );
39 
40  // traverse the model, from dimension 3 downward
41  moab::Range psets, chsets;
42  std::vector< moab::EntityHandle > sense_ents;
43  std::vector< int > senses;
44  int dim, pgid, chgid;
45  void* dim_ptr = &dim;
46  int sense;
47 
48  moab::GeomTopoTool gt( mb, true );
49 
50  for( dim = 3; dim >= 0; dim-- )
51  {
52  // get parents at this dimension
53  chsets.clear();
54  MB_CHK_SET_ERR( mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &geom_tag, &dim_ptr, 1, chsets, 1, false ),
55  "Failed to get entities by type and tag" );
56 
57  // for each child, get parents and do something with them
58  moab::Range::iterator ch_it, p_it;
59  for( ch_it = chsets.begin(); ch_it != chsets.end(); ++ch_it )
60  {
61  // get the children and put in child set list
62  psets.clear();
63  MB_CHK_SET_ERR( mb->get_parent_meshsets( *ch_it, psets ), "Failed to get parent meshsets" );
64 
65  MB_CHK_SET_ERR( mb->tag_get_data( gid_tag, &( *ch_it ), 1, &chgid ), "Failed to get tag data" );
66 
67  // print # parents
68  std::cout << ent_names[dim] << " " << chgid << " has " << psets.size() << " parents." << std::endl;
69 
70  if( 2 == dim )
71  {
72  for( p_it = psets.begin(); p_it != psets.end(); ++p_it )
73  {
74  MB_CHK_SET_ERR( mb->tag_get_data( gid_tag, &( *p_it ), 1, &pgid ), "Failed to get tag data" );
75  rval = gt.get_sense( *ch_it, *p_it, sense );
76  if( moab::MB_SUCCESS != rval ) continue;
77  std::cout << ent_names[dim + 1] << " " << pgid << ", " << ent_names[dim] << " " << chgid
78  << " sense is: ";
79  if( 1 == sense )
80  std::cout << "FORWARD" << std::endl;
81  else
82  std::cout << "REVERSE" << std::endl;
83  }
84  }
85  else if( 1 == dim )
86  {
87  sense_ents.clear();
88  senses.clear();
89  rval = gt.get_senses( *ch_it, sense_ents, senses );
90  if( moab::MB_SUCCESS != rval ) continue;
91  for( unsigned int i = 0; i < sense_ents.size(); i++ )
92  {
93  MB_CHK_SET_ERR( mb->tag_get_data( gid_tag, &sense_ents[i], 1, &pgid ), "Failed to get tag data" );
94  std::cout << ent_names[dim + 1] << " " << pgid << ", " << ent_names[dim] << " " << chgid
95  << " sense is: ";
96  if( -1 == senses[i] )
97  std::cout << "REVERSED" << std::endl;
98  else if( 0 == senses[i] )
99  std::cout << "BOTH" << std::endl;
100  else if( 1 == senses[i] )
101  std::cout << "FORWARD" << std::endl;
102  else
103  std::cout << "(invalid)" << std::endl;
104  }
105  }
106  }
107  }
108 
109  delete mb;
110 
111  return 0;
112 }