MOAB: Mesh Oriented datABase  (version 5.3.0)
read_cgm_group_test.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include "moab/Interface.hpp"
00003 #ifndef IS_BUILDING_MB
00004 #define IS_BUILDING_MB
00005 #endif
00006 #include "TestUtil.hpp"
00007 #include "Internals.hpp"
00008 #include "moab/Core.hpp"
00009 #include "MBTagConventions.hpp"
00010 #include "InitCGMA.hpp"
00011 #include "GeometryQueryTool.hpp"
00012 
00013 using namespace moab;
00014 
00015 #define CHKERR( A )                                                                                        \
00016     do                                                                                                     \
00017     {                                                                                                      \
00018         if( MB_SUCCESS != ( A ) )                                                                          \
00019         {                                                                                                  \
00020             std::cerr << "Failure (error code " << ( A ) << ") at " __FILE__ ":" << __LINE__ << std::endl; \
00021             return A;                                                                                      \
00022         }                                                                                                  \
00023     } while( false )
00024 
00025 #ifdef HAVE_OCC_STEP
00026 std::string input_cylcube = TestDir + "/io/cylcube.stp";
00027 #else
00028 std::string input_cylcube = TestDir + "/io/cylcube.sat";
00029 #endif
00030 
00031 // Function used to load the test file
00032 void read_file( Interface* moab, const char* input_file );
00033 
00034 // Function for getting entity ids
00035 int geom_id_by_handle( Interface* moab, const EntityHandle set );
00036 
00037 // Function for checking retrieved group data
00038 void check_group_data( std::vector< int >& group_ids, std::vector< std::string >& group_names,
00039 #ifdef HAVE_OCC_STEP
00040                        std::vector< int >& );
00041 #else
00042                        std::vector< int >& group_ent_ids );
00043 #endif
00044 
00045 // Function for loading all reference data
00046 void load_group_references( std::vector< int >& ids, std::vector< std::string >& names, std::vector< int >& ent_ids );
00047 
00048 // List of tests in this file
00049 void read_cylcube_groups_test();
00050 
00051 int main( int /* argc */, char** /* argv */ )
00052 {
00053     int result = 0;
00054 
00055     result += RUN_TEST( read_cylcube_groups_test );
00056 
00057     return result;
00058 }
00059 
00060 void read_file( Interface* moab, const char* input_file )
00061 {
00062     InitCGMA::initialize_cgma();
00063     GeometryQueryTool::instance()->delete_geometry();
00064 
00065     ErrorCode rval = moab->load_file( input_file );CHECK_ERR( rval );
00066 }
00067 
00068 // Checks that group information is being read correctly if MOAB is
00069 // being build with CGM. If MOAB is built with OCC, it makes sure
00070 // no erroneous group data is loaded as STEP files do not hold
00071 // information about groups.
00072 void read_cylcube_groups_test()
00073 {
00074 
00075     ErrorCode rval;
00076     // Open the test file
00077     Core moab;
00078     Interface* mb = &moab;
00079     read_file( mb, input_cylcube.c_str() );
00080 
00081     // Get (or create) the name and category tags
00082     Tag name_tag, category_tag;
00083 
00084     rval = mb->tag_get_handle( NAME_TAG_NAME, NAME_TAG_SIZE, MB_TYPE_OPAQUE, name_tag,
00085                                moab::MB_TAG_SPARSE | moab::MB_TAG_CREAT );CHECK_ERR( rval );
00086 
00087     rval = mb->tag_get_handle( CATEGORY_TAG_NAME, CATEGORY_TAG_SIZE, MB_TYPE_OPAQUE, category_tag,
00088                                moab::MB_TAG_SPARSE | moab::MB_TAG_CREAT );CHECK_ERR( rval );
00089 
00090     // Get the group entity handles
00091     Range group_sets;
00092     char query[CATEGORY_TAG_SIZE] = "Group\0";
00093     // Has to be this way because of the way tags are created
00094     void* val[] = { &query };
00095     rval        = mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &category_tag, val, 1, group_sets );CHECK_ERR( rval );
00096     // Get group names and IDs
00097     std::vector< int > g_ids;
00098     std::vector< std::string > g_names;
00099     std::vector< int > g_ent_ids;
00100 
00101     for( Range::iterator i = group_sets.begin(); i != group_sets.end(); ++i )
00102     {
00103         int group_id = geom_id_by_handle( mb, *i );
00104         g_ids.push_back( group_id );
00105         // Get the group name
00106         char group_name[NAME_TAG_SIZE + 1];
00107         rval = mb->tag_get_data( name_tag, &( *i ), 1, &group_name );CHECK_ERR( rval );
00108         // Store group name
00109         std::string temp( group_name );
00110         g_names.push_back( temp );
00111         // Get all entities in the group
00112         Range group_ents;
00113         rval = mb->get_entities_by_type( *i, MBENTITYSET, group_ents, false );CHECK_ERR( rval );
00114         if( group_ents.size() != 1 ) CHECK( false );
00115         int grp_ent_id = geom_id_by_handle( mb, group_ents[0] );
00116         g_ent_ids.push_back( grp_ent_id );
00117     }
00118     check_group_data( g_ids, g_names, g_ent_ids );
00119 }
00120 
00121 void check_group_data( std::vector< int >& group_ids, std::vector< std::string >& group_names,
00122 #ifdef HAVE_OCC_STEP
00123                        std::vector< int >& /*group_ent_ids*/ )
00124 #else
00125                        std::vector< int >& group_ent_ids )
00126 #endif
00127 {
00128     // Step files do not contain group data, MOAB shouldn't return errors when trying to access
00129     // this data but there shouldn't be any found.
00130 #ifdef HAVE_OCC_STEP
00131     int num_g_ids   = group_ids.size();
00132     int num_g_names = group_names.size();
00133 
00134     CHECK_EQUAL( 0, num_g_ids );
00135     CHECK_EQUAL( 0, num_g_names );
00136 #else
00137 
00138     // Initialize reference data
00139     std::vector< int > group_ref_ids;
00140     std::vector< std::string > group_ref_names;
00141     std::vector< int > group_ref_ent_ids;
00142     load_group_references( group_ref_ids, group_ref_names, group_ref_ent_ids );
00143 
00144     // check that the correct number of entities were found
00145     CHECK_EQUAL( group_ref_ids.size(), group_ids.size() );
00146     CHECK_EQUAL( group_ref_names.size(), group_names.size() );
00147     CHECK_EQUAL( group_ref_ent_ids.size(), group_ent_ids.size() );
00148 
00149     // now make sure that each group has a matching group
00150     for( unsigned int i = 0; i < group_ids.size(); i++ )
00151     {
00152         for( unsigned int j = 0; j < group_ref_ids.size(); j++ )
00153         {
00154             if( group_ids[i] == group_ref_ids[j] && group_names[i] == group_ref_names[j] &&
00155                 group_ent_ids[i] == group_ref_ent_ids[j] )
00156             {
00157                 group_ref_ids.erase( group_ref_ids.begin() + j );
00158                 group_ref_names.erase( group_ref_names.begin() + j );
00159                 group_ref_ent_ids.erase( group_ref_ent_ids.begin() + j );
00160                 continue;
00161             }
00162         }
00163     }
00164 
00165     // Check sizes of reference vectors after matching
00166     // (all should be zero)
00167     int leftovers = group_ref_ids.size();
00168     CHECK_EQUAL( 0, leftovers );
00169     leftovers = group_ref_names.size();
00170     CHECK_EQUAL( 0, leftovers );
00171     leftovers = group_ref_ent_ids.size();
00172     CHECK_EQUAL( 0, leftovers );
00173 #endif
00174 }
00175 
00176 void load_group_references( std::vector< int >& ids, std::vector< std::string >& names, std::vector< int >& ent_ids )
00177 {
00178     // First set of group info
00179     names.push_back( "Group 3" );
00180     ids.push_back( 3 );
00181     ent_ids.push_back( 2 );
00182 
00183     // Second set of group info
00184     names.push_back( "Group 2" );
00185     ids.push_back( 2 );
00186     ent_ids.push_back( 1 );
00187 }
00188 
00189 int geom_id_by_handle( Interface* moab, const EntityHandle set )
00190 {
00191     ErrorCode rval;
00192     // Get the id_tag handle
00193     Tag id_tag = moab->globalId_tag();
00194     // Load the ID for the EntHandle given to the function
00195     int id;
00196     rval = moab->tag_get_data( id_tag, &set, 1, &id );CHECK_ERR( rval );
00197     return id;
00198 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines