MOAB: Mesh Oriented datABase  (version 5.2.1)
Test_MBMeshSet.cpp
Go to the documentation of this file.
00001 #include "MeshSet.hpp"
00002 #include "AEntityFactory.hpp"
00003 #include "TestUtil.hpp"
00004 #include "moab/Core.hpp"
00005 #include "moab/SetIterator.hpp"
00006 
00007 using namespace moab;
00008 
00009 #include <iostream>
00010 
00011 //! test add_entities, get_entities, num_entities
00012 void test_add_entities( unsigned flags );
00013 //! test remove_entities, get_entities, num_entities
00014 void test_remove_entities( unsigned flags );
00015 //! test get_entities_by_type, num_entities_by_type
00016 void test_entities_by_type( unsigned flags );
00017 //! test get_entities_by_dimension, num_entities_by_dimension
00018 void test_entities_by_dimension( unsigned flags );
00019 //! test subtract_meshset
00020 void test_subtract( unsigned flags1, unsigned flags2 );
00021 //! test intersect_meshset
00022 void test_intersect( unsigned flags1, unsigned flags2 );
00023 //! test unite_meshset
00024 void test_unite( unsigned flags1, unsigned flags2 );
00025 //! test MeshSet::contains_entities
00026 void test_contains_entities( unsigned flags );
00027 //! test clear_meshset
00028 void test_clear( unsigned flags );
00029 
00030 //! Create 10x10x10 hex mesh
00031 void make_mesh( Interface& iface );
00032 //! Create 10x10x10 hex mesh, return ranges of handles
00033 void make_mesh( Interface& iface, EntityHandle& vstart, EntityHandle& vend, EntityHandle& hstart, EntityHandle& hend );
00034 //! Print std::vector<EntityHandle>
00035 void print_handle_vect( const char* prefix, const std::vector< EntityHandle >& vect );
00036 //! Print Range
00037 void print_mbrange( const char* prefix, const Range& range );
00038 //! Compare set contents against passed vector, and if MESHSET_TRACK_OWNER then check adjacencies
00039 bool check_set_contents( Core& mb, EntityHandle set, const std::vector< EntityHandle >& expected );
00040 //! Compare result of get_entities_by_type to result of get_entities_by_handle
00041 bool check_set_contents( Core& mb, EntityType type, EntityHandle set, unsigned flags );
00042 //! Compare result of get_entities_by_dimension to result of get_entities_by_handle
00043 bool check_set_contents( Core& mb, int dim, EntityHandle set, unsigned flags );
00044 //! For each entry in range, if one or more occurances in vect, remove last occurance from vect.
00045 void remove_from_back( std::vector< EntityHandle >& vect, const Range& range );
00046 enum BoolOp
00047 {
00048     UNITE,
00049     INTERSECT,
00050     SUBTRACT
00051 };
00052 //! Perform boolean op on two entity sets and verify result
00053 bool test_boolean( Core& mb, BoolOp op, unsigned flags1, const Range& set1_ents, unsigned flags2,
00054                    const Range& set2_ents );
00055 void test_iterator( Interface& moab, SetIterator* iter, EntityHandle set, EntityType etype, int dim );
00056 void test_iterators( unsigned int flags, bool type, bool dim );
00057 void test_all_iterators();
00058 
00059 void test_add_entities_ordered()
00060 {
00061     test_add_entities( MESHSET_ORDERED );
00062 }
00063 void test_add_entities_set()
00064 {
00065     test_add_entities( MESHSET_SET );
00066 }
00067 void test_add_entities_ordered_tracking()
00068 {
00069     test_add_entities( MESHSET_TRACK_OWNER | MESHSET_ORDERED );
00070 }
00071 void test_add_entities_set_tracking()
00072 {
00073     test_add_entities( MESHSET_TRACK_OWNER | MESHSET_SET );
00074 }
00075 
00076 void test_remove_entities_ordered()
00077 {
00078     test_remove_entities( MESHSET_ORDERED );
00079 }
00080 void test_remove_entities_set()
00081 {
00082     test_remove_entities( MESHSET_SET );
00083 }
00084 void test_remove_entities_ordered_tracking()
00085 {
00086     test_remove_entities( MESHSET_TRACK_OWNER | MESHSET_ORDERED );
00087 }
00088 void test_remove_entities_set_tracking()
00089 {
00090     test_remove_entities( MESHSET_TRACK_OWNER | MESHSET_SET );
00091 }
00092 
00093 void test_entities_by_type_ordered()
00094 {
00095     test_entities_by_type( MESHSET_ORDERED );
00096 }
00097 void test_entities_by_type_set()
00098 {
00099     test_entities_by_type( MESHSET_SET );
00100 }
00101 void test_entities_by_dim_ordered()
00102 {
00103     test_entities_by_dimension( MESHSET_ORDERED );
00104 }
00105 void test_entities_by_dim_set()
00106 {
00107     test_entities_by_dimension( MESHSET_SET );
00108 }
00109 
00110 void test_subtract_set_set()
00111 {
00112     test_subtract( MESHSET_SET, MESHSET_SET );
00113 }
00114 void test_subtract_set_ordered()
00115 {
00116     test_subtract( MESHSET_SET, MESHSET_ORDERED );
00117 }
00118 void test_subtract_ordered_set()
00119 {
00120     test_subtract( MESHSET_ORDERED, MESHSET_SET );
00121 }
00122 void test_subtract_ordered_ordered()
00123 {
00124     test_subtract( MESHSET_ORDERED, MESHSET_ORDERED );
00125 }
00126 void test_subtract_set_set_tracking()
00127 {
00128     test_subtract( MESHSET_TRACK_OWNER | MESHSET_SET, MESHSET_TRACK_OWNER | MESHSET_SET );
00129 }
00130 void test_subtract_set_ordered_tracking()
00131 {
00132     test_subtract( MESHSET_TRACK_OWNER | MESHSET_SET, MESHSET_TRACK_OWNER | MESHSET_ORDERED );
00133 }
00134 void test_subtract_ordered_set_tracking()
00135 {
00136     test_subtract( MESHSET_TRACK_OWNER | MESHSET_ORDERED, MESHSET_TRACK_OWNER | MESHSET_SET );
00137 }
00138 void test_subtract_ordered_ordered_tracking()
00139 {
00140     test_subtract( MESHSET_TRACK_OWNER | MESHSET_ORDERED, MESHSET_TRACK_OWNER | MESHSET_ORDERED );
00141 }
00142 
00143 void test_itersect_set_set()
00144 {
00145     test_intersect( MESHSET_SET, MESHSET_SET );
00146 }
00147 void test_itersect_set_ordered()
00148 {
00149     test_intersect( MESHSET_SET, MESHSET_ORDERED );
00150 }
00151 void test_itersect_ordered_set()
00152 {
00153     test_intersect( MESHSET_ORDERED, MESHSET_SET );
00154 }
00155 void test_itersect_ordered_ordered()
00156 {
00157     test_intersect( MESHSET_ORDERED, MESHSET_ORDERED );
00158 }
00159 void test_itersect_set_set_tracking()
00160 {
00161     test_intersect( MESHSET_TRACK_OWNER | MESHSET_SET, MESHSET_TRACK_OWNER | MESHSET_SET );
00162 }
00163 void test_itersect_set_ordered_tracking()
00164 {
00165     test_intersect( MESHSET_TRACK_OWNER | MESHSET_SET, MESHSET_TRACK_OWNER | MESHSET_ORDERED );
00166 }
00167 void test_itersect_ordered_set_tracking()
00168 {
00169     test_intersect( MESHSET_TRACK_OWNER | MESHSET_ORDERED, MESHSET_TRACK_OWNER | MESHSET_SET );
00170 }
00171 void test_itersect_ordered_ordered_tracking()
00172 {
00173     test_intersect( MESHSET_TRACK_OWNER | MESHSET_ORDERED, MESHSET_TRACK_OWNER | MESHSET_ORDERED );
00174 }
00175 
00176 void test_unite_set_set()
00177 {
00178     test_unite( MESHSET_SET, MESHSET_SET );
00179 }
00180 void test_unite_set_ordered()
00181 {
00182     test_unite( MESHSET_SET, MESHSET_ORDERED );
00183 }
00184 void test_unite_ordered_set()
00185 {
00186     test_unite( MESHSET_ORDERED, MESHSET_SET );
00187 }
00188 void test_unite_ordered_ordered()
00189 {
00190     test_unite( MESHSET_ORDERED, MESHSET_ORDERED );
00191 }
00192 void test_unite_set_set_tracking()
00193 {
00194     test_unite( MESHSET_TRACK_OWNER | MESHSET_SET, MESHSET_TRACK_OWNER | MESHSET_SET );
00195 }
00196 void test_unite_set_ordered_tracking()
00197 {
00198     test_unite( MESHSET_TRACK_OWNER | MESHSET_SET, MESHSET_TRACK_OWNER | MESHSET_ORDERED );
00199 }
00200 void test_unite_ordered_set_tracking()
00201 {
00202     test_unite( MESHSET_TRACK_OWNER | MESHSET_ORDERED, MESHSET_TRACK_OWNER | MESHSET_SET );
00203 }
00204 void test_unite_ordered_ordered_tracking()
00205 {
00206     test_unite( MESHSET_TRACK_OWNER | MESHSET_ORDERED, MESHSET_TRACK_OWNER | MESHSET_ORDERED );
00207 }
00208 
00209 void test_contains_entities_ordered()
00210 {
00211     test_contains_entities( MESHSET_ORDERED );
00212 }
00213 void test_contains_entities_set()
00214 {
00215     test_contains_entities( MESHSET_SET );
00216 }
00217 
00218 void test_clear_ordered()
00219 {
00220     test_clear( MESHSET_ORDERED );
00221 }
00222 void test_clear_set()
00223 {
00224     test_clear( MESHSET_SET );
00225 }
00226 void test_clear_ordered_tracking()
00227 {
00228     test_clear( MESHSET_TRACK_OWNER | MESHSET_ORDERED );
00229 }
00230 void test_clear_set_tracking()
00231 {
00232     test_clear( MESHSET_TRACK_OWNER | MESHSET_SET );
00233 }
00234 
00235 // Reproduce contitions that resulted in bug reported to
00236 // mailing list:
00237 // http://lists.mcs.anl.gov/pipermail/moab-dev/2010/002714.html
00238 void regression_insert_set_1();
00239 
00240 int main()
00241 {
00242     int err = 0;
00243 
00244     err += RUN_TEST( test_add_entities_ordered );
00245     err += RUN_TEST( test_add_entities_set );
00246     err += RUN_TEST( test_add_entities_ordered_tracking );
00247     err += RUN_TEST( test_add_entities_set_tracking );
00248 
00249     err += RUN_TEST( test_remove_entities_ordered );
00250     err += RUN_TEST( test_remove_entities_set );
00251     err += RUN_TEST( test_remove_entities_ordered_tracking );
00252     err += RUN_TEST( test_remove_entities_set_tracking );
00253 
00254     err += RUN_TEST( test_entities_by_type_ordered );
00255     err += RUN_TEST( test_entities_by_type_set );
00256     err += RUN_TEST( test_entities_by_dim_ordered );
00257     err += RUN_TEST( test_entities_by_dim_set );
00258 
00259     err += RUN_TEST( test_subtract_set_set );
00260     err += RUN_TEST( test_subtract_set_ordered );
00261     err += RUN_TEST( test_subtract_ordered_set );
00262     err += RUN_TEST( test_subtract_ordered_ordered );
00263     err += RUN_TEST( test_subtract_set_set_tracking );
00264     err += RUN_TEST( test_subtract_set_ordered_tracking );
00265     err += RUN_TEST( test_subtract_ordered_set_tracking );
00266     err += RUN_TEST( test_subtract_ordered_ordered_tracking );
00267 
00268     err += RUN_TEST( test_itersect_set_set );
00269     err += RUN_TEST( test_itersect_set_ordered );
00270     err += RUN_TEST( test_itersect_ordered_set );
00271     err += RUN_TEST( test_itersect_ordered_ordered );
00272     err += RUN_TEST( test_itersect_set_set_tracking );
00273     err += RUN_TEST( test_itersect_set_ordered_tracking );
00274     err += RUN_TEST( test_itersect_ordered_set_tracking );
00275     err += RUN_TEST( test_itersect_ordered_ordered_tracking );
00276 
00277     err += RUN_TEST( test_unite_set_set );
00278     err += RUN_TEST( test_unite_set_ordered );
00279     err += RUN_TEST( test_unite_ordered_set );
00280     err += RUN_TEST( test_unite_ordered_ordered );
00281     err += RUN_TEST( test_unite_set_set_tracking );
00282     err += RUN_TEST( test_unite_set_ordered_tracking );
00283     err += RUN_TEST( test_unite_ordered_set_tracking );
00284     err += RUN_TEST( test_unite_ordered_ordered_tracking );
00285 
00286     err += RUN_TEST( test_contains_entities_ordered );
00287     err += RUN_TEST( test_contains_entities_set );
00288 
00289     err += RUN_TEST( test_clear_ordered );
00290     err += RUN_TEST( test_clear_set );
00291     err += RUN_TEST( test_clear_ordered_tracking );
00292     err += RUN_TEST( test_clear_set_tracking );
00293 
00294     err += RUN_TEST( regression_insert_set_1 );
00295 
00296     err += RUN_TEST( test_all_iterators );
00297 
00298     if( !err )
00299         printf( "ALL TESTS PASSED\n" );
00300     else
00301         printf( "%d TESTS FAILED\n", err );
00302 
00303     return err;
00304 }
00305 
00306 // Create 100x100x100 hex mesh
00307 void make_mesh( Interface& iface )
00308 {
00309     const int dim = 10;
00310 
00311     // create vertices
00312     EntityHandle prev_handle = 0;
00313     for( int z = 0; z <= dim; ++z )
00314     {
00315         for( int y = 0; y <= dim; ++y )
00316         {
00317             for( int x = 0; x <= dim; ++x )
00318             {
00319                 const double coords[]   = { static_cast< double >( x ), static_cast< double >( y ),
00320                                           static_cast< double >( z ) };
00321                 EntityHandle new_handle = 0;
00322                 ErrorCode rval          = iface.create_vertex( coords, new_handle );CHECK_ERR( rval );
00323                 CHECK_EQUAL( ++prev_handle, new_handle );
00324             }
00325         }
00326     }
00327 
00328     // create hexes
00329     const int dim1 = dim + 1;
00330     const int dimq = dim1 * dim1;
00331     prev_handle    = FIRST_HANDLE( MBHEX );
00332     for( int z = 0; z < dim; ++z )
00333     {
00334         for( int y = 0; y < dim; ++y )
00335         {
00336             for( int x = 0; x < dim; ++x )
00337             {
00338                 const EntityHandle off    = 1 + x + dim1 * y + dimq * z;
00339                 const EntityHandle conn[] = { off,        off + 1,        off + 1 + dim1,        off + dim1,
00340                                               off + dimq, off + 1 + dimq, off + 1 + dim1 + dimq, off + dim1 + dimq };
00341                 EntityHandle new_handle   = 0;
00342                 ErrorCode rval            = iface.create_element( MBHEX, conn, 8, new_handle );CHECK_ERR( rval );
00343                 CHECK_EQUAL( prev_handle++, new_handle );
00344             }
00345         }
00346     }
00347 }
00348 
00349 void make_mesh( Interface& mb, EntityHandle& first_vert, EntityHandle& last_vert, EntityHandle& first_hex,
00350                 EntityHandle& last_hex )
00351 {
00352     make_mesh( mb );
00353 
00354     // Get handle ranges, and validate assumption that handles
00355     // are contiguous.
00356     Range range;
00357     ErrorCode rval = mb.get_entities_by_type( 0, MBVERTEX, range );CHECK_ERR( rval );
00358     first_vert = range.front();
00359     last_vert  = range.back();
00360     CHECK_EQUAL( (EntityHandle)1331, last_vert - first_vert + 1 );
00361     range.clear();
00362     rval = mb.get_entities_by_type( 0, MBHEX, range );CHECK_ERR( rval );
00363     first_hex = range.front();
00364     last_hex  = range.back();
00365     CHECK_EQUAL( (EntityHandle)1000, last_hex - first_hex + 1 );
00366 }
00367 
00368 template < typename iter_type >
00369 void print_handles( std::ostream& str, const char* prefix, iter_type begin, iter_type end )
00370 {
00371     if( prefix ) str << prefix << ':';
00372     if( begin == end )
00373     {
00374         str << " (empty)" << std::endl;
00375         return;
00376     }
00377 
00378     iter_type i           = begin;
00379     EntityType prev_type  = TYPE_FROM_HANDLE( *i );
00380     EntityHandle prev_ent = *i;
00381     str << ' ' << CN::EntityTypeName( prev_type ) << ' ' << ID_FROM_HANDLE( *i );
00382     for( ;; )
00383     {
00384         iter_type j = i;
00385         for( ++j, ++prev_ent; j != end && *j == prev_ent; ++j, ++prev_ent )
00386             ;
00387         --prev_ent;
00388         if( prev_ent - *i > 1 )
00389             str << "-" << ID_FROM_HANDLE( prev_ent );
00390         else if( prev_ent - *i == 1 )
00391             str << ", " << ID_FROM_HANDLE( prev_ent );
00392 
00393         i = j;
00394         if( i == end ) break;
00395 
00396         str << ',';
00397         if( TYPE_FROM_HANDLE( *i ) != prev_type )
00398             str << ' ' << CN::EntityTypeName( prev_type = TYPE_FROM_HANDLE( *i ) );
00399         str << ' ' << ID_FROM_HANDLE( *i );
00400         prev_ent = *i;
00401     }
00402     str << std::endl;
00403 }
00404 
00405 void print_handle_vect( const char* prefix, const std::vector< EntityHandle >& vect )
00406 {
00407     print_handles( std::cout, prefix, vect.begin(), vect.end() );
00408 }
00409 
00410 void print_mbrange( const char* prefix, const Range& range )
00411 {
00412     print_handles( std::cout, prefix, range.begin(), range.end() );
00413 }
00414 
00415 bool compare_set_contents( unsigned flags, const std::vector< EntityHandle >& expected, int set_count,
00416                            std::vector< EntityHandle >& vect, const Range& range )
00417 {
00418 
00419     std::vector< EntityHandle > sorted( expected );
00420     std::sort( sorted.begin(), sorted.end() );
00421     sorted.erase( std::unique( sorted.begin(), sorted.end() ), sorted.end() );
00422 
00423     int expected_size = 0;
00424     if( flags & MESHSET_ORDERED )
00425     {
00426         if( expected != vect )
00427         {
00428             std::cout << "Incorrect set contents from vector-based query" << std::endl;
00429             print_handle_vect( "Expected", expected );
00430             print_handle_vect( "Actual", vect );
00431             return false;
00432         }
00433         expected_size = expected.size();
00434     }
00435     else
00436     {
00437         if( sorted != vect )
00438         {
00439             std::cout << "Incorrect set contents from vector-based query" << std::endl;
00440             print_handle_vect( "Expected", sorted );
00441             print_handle_vect( "Actual", vect );
00442             return false;
00443         }
00444         expected_size = sorted.size();
00445     }
00446 
00447     if( expected_size != set_count )
00448     {
00449         std::cout << "Incorrect size for entity set" << std::endl;
00450         std::cout << "Expected: " << expected_size << std::endl;
00451         std::cout << "Actual:   " << set_count << std::endl;
00452         return false;
00453     }
00454 
00455     vect.clear();
00456     vect.resize( range.size() );
00457     std::copy( range.begin(), range.end(), vect.begin() );
00458     if( sorted != vect )
00459     {
00460         std::cout << "Incorrect set contents from mbrange-based query" << std::endl;
00461         print_handle_vect( "Expected", vect );
00462         print_mbrange( "Actual", range );
00463         return false;
00464     }
00465 
00466     return true;
00467 }
00468 
00469 bool check_set_contents( Core& mb, EntityHandle set, const std::vector< EntityHandle >& expected )
00470 {
00471     unsigned flags;
00472     ErrorCode rval = mb.get_meshset_options( set, flags );CHECK_ERR( rval );
00473 
00474     int count;
00475     std::vector< EntityHandle > vect;
00476     Range range;
00477     rval = mb.get_entities_by_handle( set, vect, false );CHECK_ERR( rval );
00478     rval = mb.get_entities_by_handle( set, range, false );CHECK_ERR( rval );
00479     rval = mb.get_number_entities_by_handle( set, count, false );CHECK_ERR( rval );
00480 
00481     if( !compare_set_contents( flags, expected, count, vect, range ) ) return false;
00482 
00483     if( !( flags & MESHSET_TRACK_OWNER ) ) return true;
00484 
00485     // get all entitites with an adjacency to the set
00486     std::vector< EntityHandle > adj;
00487     Range all, adjacent;
00488     Range::iterator in = adjacent.begin();
00489     mb.get_entities_by_handle( 0, all );
00490     for( Range::iterator i = all.begin(); i != all.end(); ++i )
00491     {
00492         adj.clear();
00493         rval = mb.a_entity_factory()->get_adjacencies( *i, adj );CHECK_ERR( rval );
00494         std::vector< EntityHandle >::iterator j = std::lower_bound( adj.begin(), adj.end(), set );
00495         if( j != adj.end() && *j == set ) in = adjacent.insert( in, *i, *i );
00496     }
00497 
00498     if( range != adjacent )
00499     {
00500         std::cout << "Incorrect adjacent entities for tracking set" << std::endl;
00501         print_mbrange( "Expected", range );
00502         print_mbrange( "Actual", adjacent );
00503         return false;
00504     }
00505 
00506     return true;
00507 }
00508 
00509 bool check_set_contents( Core& mb, EntityType type, EntityHandle set, unsigned flags )
00510 {
00511     ErrorCode rval;
00512     int count;
00513     std::vector< EntityHandle > vect, expected;
00514     Range range;
00515 
00516     rval = mb.get_entities_by_handle( set, expected, false );CHECK_ERR( rval );
00517     std::vector< EntityHandle >::iterator i = expected.begin();
00518     while( i != expected.end() )
00519     {
00520         if( TYPE_FROM_HANDLE( *i ) != type )
00521             i = expected.erase( i );
00522         else
00523             ++i;
00524     }
00525 
00526     rval = mb.get_entities_by_type( set, type, range, false );CHECK_ERR( rval );
00527     rval = mb.get_number_entities_by_type( set, type, count, false );CHECK_ERR( rval );
00528 
00529     std::copy( range.begin(), range.end(), std::back_inserter( vect ) );
00530     return compare_set_contents( flags, expected, count, vect, range );
00531 }
00532 
00533 bool check_set_contents( Core& mb, int dim, EntityHandle set, unsigned flags )
00534 {
00535     ErrorCode rval;
00536     int count;
00537     std::vector< EntityHandle > vect, expected;
00538     Range range;
00539 
00540     rval = mb.get_entities_by_handle( set, expected, false );CHECK_ERR( rval );
00541     std::vector< EntityHandle >::iterator i = expected.begin();
00542     while( i != expected.end() )
00543     {
00544         if( CN::Dimension( TYPE_FROM_HANDLE( *i ) ) != dim )
00545             i = expected.erase( i );
00546         else
00547             ++i;
00548     }
00549 
00550     rval = mb.get_entities_by_dimension( set, dim, range, false );CHECK_ERR( rval );
00551     rval = mb.get_number_entities_by_dimension( set, dim, count, false );CHECK_ERR( rval );
00552 
00553     std::copy( range.begin(), range.end(), std::back_inserter( vect ) );
00554     return compare_set_contents( flags, expected, count, vect, range );
00555 }
00556 
00557 void test_add_entities( unsigned flags )
00558 {
00559     Core mb;
00560     make_mesh( mb );
00561 
00562     EntityHandle set;
00563     ErrorCode rval = mb.create_meshset( flags, set );CHECK_ERR( rval );
00564 
00565     std::vector< EntityHandle > contents, vect;
00566     Range range;
00567 
00568     range.clear();
00569     range.insert( 11, 20 );
00570     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00571     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00572     // [11,20]
00573     CHECK( check_set_contents( mb, set, contents ) );
00574 
00575     range.clear();
00576     range.insert( 31, 40 );
00577     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00578     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00579     // [11,20],[31,40]
00580     CHECK( check_set_contents( mb, set, contents ) );
00581 
00582     range.clear();
00583     range.insert( 51, 60 );
00584     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00585     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00586     // [11,20],[31,40],[51,60]
00587     CHECK( check_set_contents( mb, set, contents ) );
00588 
00589     range.clear();
00590     range.insert( 71, 80 );
00591     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00592     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00593     // [11,20],[31,40],[51,60],[71,80]
00594     CHECK( check_set_contents( mb, set, contents ) );
00595 
00596     range.clear();
00597     range.insert( 91, 100 );
00598     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00599     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00600     // [11,20],[31,40],[51,60],[71,80],[91,100]
00601     CHECK( check_set_contents( mb, set, contents ) );
00602 
00603     range.clear();
00604     range.insert( 111, 120 );
00605     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00606     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00607     // [11,20],[31,40],[51,60],[71,80],[91,100],[111,120]
00608     CHECK( check_set_contents( mb, set, contents ) );
00609 
00610     range.clear();
00611     range.insert( 6, 12 );
00612     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00613     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00614     // [6,20],[31,40],[51,60],[71,80],[91,100],[111,120]
00615     CHECK( check_set_contents( mb, set, contents ) );
00616 
00617     range.clear();
00618     range.insert( 1, 3 );
00619     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00620     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00621     // [1,3],[6,20],[31,40],[51,60],[71,80],[91,100],[111,120]
00622     CHECK( check_set_contents( mb, set, contents ) );
00623 
00624     range.clear();
00625     range.insert( 25, 25 );
00626     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00627     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00628     // [1,3],[6,20],[25,25],[31,40],[51,60],[71,80],[91,100],[111,120]
00629     CHECK( check_set_contents( mb, set, contents ) );
00630 
00631     range.clear();
00632     range.insert( 30, 30 );
00633     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00634     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00635     // [1,3],[6,20],[25,25],[30,40],[51,60],[71,80],[91,100],[111,120]
00636     CHECK( check_set_contents( mb, set, contents ) );
00637 
00638     range.clear();
00639     range.insert( 29, 31 );
00640     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00641     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00642     // [1,3],[6,20],[25,25],[29,40],[51,60],[71,80],[91,100],[111,120]
00643     CHECK( check_set_contents( mb, set, contents ) );
00644 
00645     range.clear();
00646     range.insert( 41, 41 );
00647     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00648     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00649     // [1,3],[6,20],[25,25],[29,41],[51,60],[71,80],[91,100],[111,120]
00650     CHECK( check_set_contents( mb, set, contents ) );
00651 
00652     range.clear();
00653     range.insert( 41, 45 );
00654     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00655     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00656     // [1,3],[6,20],[25,25],[29,45],[51,60],[71,80],[91,100],[111,120]
00657     CHECK( check_set_contents( mb, set, contents ) );
00658 
00659     range.clear();
00660     range.insert( 47, 47 );
00661     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00662     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00663     // [1,3],[6,20],[25,25],[29,45],[47,47],[51,60],[71,80],[91,100],[111,120]
00664     CHECK( check_set_contents( mb, set, contents ) );
00665 
00666     range.clear();
00667     range.insert( 51, 80 );
00668     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00669     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00670     // [1,3],[6,20],[25,25],[29,45],[47,47],[51,80],[91,100],[111,120]
00671     CHECK( check_set_contents( mb, set, contents ) );
00672 
00673     range.clear();
00674     range.insert( 49, 105 );
00675     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00676     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00677     // [1,3],[6,20],[25,25],[29,45],[47,47],[49,105],[111,120]
00678     CHECK( check_set_contents( mb, set, contents ) );
00679 
00680     vect.clear();
00681     for( EntityHandle h = 1; h < 100; ++h )
00682     {
00683         vect.push_back( h );
00684         contents.push_back( h );
00685     }
00686     rval = mb.add_entities( set, &vect[0], vect.size() );CHECK_ERR( rval );
00687     // [1,105],[111,120]
00688     CHECK( check_set_contents( mb, set, contents ) );
00689 
00690     vect.clear();
00691     vect.push_back( 106 );
00692     vect.push_back( 108 );
00693     vect.push_back( 110 );
00694     std::copy( vect.begin(), vect.end(), std::back_inserter( contents ) );
00695     rval = mb.add_entities( set, &vect[0], vect.size() );CHECK_ERR( rval );
00696     // [1,106],[108,108],[110,120]
00697     CHECK( check_set_contents( mb, set, contents ) );
00698 
00699     range.clear();
00700     range.insert( 107, 200 );
00701     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00702     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00703     // [1,200]
00704     CHECK( check_set_contents( mb, set, contents ) );
00705 
00706     range.clear();
00707     range.insert( 1, 1 );
00708     range.insert( 5, 6 );
00709     range.insert( 199, 200 );
00710     range.insert( 201, 202 );
00711     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00712     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00713     // [1,202]
00714     CHECK( check_set_contents( mb, set, contents ) );
00715 
00716     range.clear();
00717     range.insert( 300, 301 );
00718     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00719     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00720     // [1,202],[300,301]
00721     CHECK( check_set_contents( mb, set, contents ) );
00722 
00723     range.clear();
00724     range.insert( 203, 203 );
00725     range.insert( 205, 205 );
00726     range.insert( 207, 207 );
00727     range.insert( 299, 299 );
00728     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00729     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00730     // [1,203],[205,205],[207,207],[299,301]
00731     CHECK( check_set_contents( mb, set, contents ) );
00732 }
00733 
00734 void remove_from_back( std::vector< EntityHandle >& vect, const Range& range )
00735 {
00736     for( Range::const_iterator r = range.begin(); r != range.end(); ++r )
00737     {
00738         std::vector< EntityHandle >::reverse_iterator i = find( vect.rbegin(), vect.rend(), *r );
00739         if( i != vect.rend() ) *i = 0;
00740     }
00741     std::vector< EntityHandle >::iterator j = vect.begin();
00742     while( j != vect.end() )
00743     {
00744         if( *j == 0 )
00745             j = vect.erase( j );
00746         else
00747             ++j;
00748     }
00749 }
00750 
00751 void test_remove_entities( unsigned flags )
00752 {
00753     Core mb;
00754     make_mesh( mb );
00755 
00756     EntityHandle set;
00757     ErrorCode rval = mb.create_meshset( flags, set );CHECK_ERR( rval );
00758 
00759     std::vector< EntityHandle > contents;
00760     Range range;
00761 
00762     range.clear();
00763     range.insert( 1, 1000 );
00764     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00765     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00766     // [1,1000]
00767     CHECK( check_set_contents( mb, set, contents ) );
00768 
00769     range.clear();
00770     range.insert( 1, 5 );
00771     remove_from_back( contents, range );
00772     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00773     // [6,1000]
00774     CHECK( check_set_contents( mb, set, contents ) );
00775 
00776     range.clear();
00777     range.insert( 6, 6 );
00778     remove_from_back( contents, range );
00779     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00780     // [7,1000]
00781     CHECK( check_set_contents( mb, set, contents ) );
00782 
00783     range.clear();
00784     range.insert( 7, 10 );
00785     remove_from_back( contents, range );
00786     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00787     // [11,1000]
00788     CHECK( check_set_contents( mb, set, contents ) );
00789 
00790     range.clear();
00791     range.insert( 1, 20 );
00792     remove_from_back( contents, range );
00793     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00794     // [21,1000]
00795     CHECK( check_set_contents( mb, set, contents ) );
00796 
00797     range.clear();
00798     range.insert( 1, 5 );
00799     remove_from_back( contents, range );
00800     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00801     // [21,1000]
00802     CHECK( check_set_contents( mb, set, contents ) );
00803 
00804     range.clear();
00805     range.insert( 22, 30 );
00806     remove_from_back( contents, range );
00807     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00808     // [21,21],[31,1000]
00809     CHECK( check_set_contents( mb, set, contents ) );
00810 
00811     range.clear();
00812     range.insert( 1000, 1000 );
00813     remove_from_back( contents, range );
00814     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00815     // [21,21],[31,999]
00816     CHECK( check_set_contents( mb, set, contents ) );
00817 
00818     range.clear();
00819     range.insert( 901, 999 );
00820     remove_from_back( contents, range );
00821     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00822     // [21,21],[31,900]
00823     CHECK( check_set_contents( mb, set, contents ) );
00824 
00825     range.clear();
00826     range.insert( 900, 999 );
00827     remove_from_back( contents, range );
00828     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00829     // [21,21],[31,899]
00830     CHECK( check_set_contents( mb, set, contents ) );
00831 
00832     range.clear();
00833     range.insert( 1000, 1001 );
00834     remove_from_back( contents, range );
00835     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00836     // [21,21],[31,899]
00837     CHECK( check_set_contents( mb, set, contents ) );
00838 
00839     range.clear();
00840     range.insert( 890, 898 );
00841     remove_from_back( contents, range );
00842     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00843     // [21,21],[31,889],[899,899]
00844     CHECK( check_set_contents( mb, set, contents ) );
00845 
00846     range.clear();
00847     range.insert( 100, 149 );
00848     remove_from_back( contents, range );
00849     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00850     // [21,21],[31,99],[150,889],[899,899]
00851     CHECK( check_set_contents( mb, set, contents ) );
00852 
00853     range.clear();
00854     range.insert( 31, 99 );
00855     remove_from_back( contents, range );
00856     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00857     // [21,21],[150,889],[899,899]
00858     CHECK( check_set_contents( mb, set, contents ) );
00859 
00860     range.clear();
00861     range.insert( 200, 249 );
00862     remove_from_back( contents, range );
00863     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00864     // [21,21],[150,199],[250,889],[899,899]
00865     CHECK( check_set_contents( mb, set, contents ) );
00866 
00867     range.clear();
00868     range.insert( 300, 349 );
00869     remove_from_back( contents, range );
00870     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00871     // [21,21],[150,199],[250,299],[350,889],[899,899]
00872     CHECK( check_set_contents( mb, set, contents ) );
00873 
00874     range.clear();
00875     range.insert( 159, 399 );
00876     remove_from_back( contents, range );
00877     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00878     // [21,21],[150,158],[400,889],[899,899]
00879     CHECK( check_set_contents( mb, set, contents ) );
00880 
00881     range.clear();
00882     range.insert( 450, 499 );
00883     remove_from_back( contents, range );
00884     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00885     // [21,21],[150,158],[400,449],[500,889],[899,899]
00886     CHECK( check_set_contents( mb, set, contents ) );
00887 
00888     range.clear();
00889     range.insert( 550, 599 );
00890     remove_from_back( contents, range );
00891     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00892     // [21,21],[150,158],[400,449],[500,549],[600,889],[899,899]
00893     CHECK( check_set_contents( mb, set, contents ) );
00894 
00895     range.clear();
00896     range.insert( 150, 549 );
00897     remove_from_back( contents, range );
00898     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00899     // [21,21],[600,889],[899,899]
00900     CHECK( check_set_contents( mb, set, contents ) );
00901 
00902     range.clear();
00903     range.insert( 650, 699 );
00904     remove_from_back( contents, range );
00905     rval = mb.remove_entities( set, range );CHECK_ERR( rval );
00906     // [21,21],[600,649],[700,889],[899,899]
00907     CHECK( check_set_contents( mb, set, contents ) );
00908 
00909     // test vector-based remove
00910     assert( contents.size() == 242 );
00911     std::vector< EntityHandle > remlist( 5 );
00912     remlist[0] = contents[240];
00913     contents.erase( contents.begin() + 240 );
00914     remlist[1] = contents[200];
00915     contents.erase( contents.begin() + 200 );
00916     remlist[2] = contents[100];
00917     contents.erase( contents.begin() + 100 );
00918     remlist[3] = contents[25];
00919     contents.erase( contents.begin() + 25 );
00920     remlist[4] = contents[0];
00921     contents.erase( contents.begin() + 0 );
00922     rval = mb.remove_entities( set, &remlist[0], remlist.size() );CHECK_ERR( rval );
00923     // [600,623],[625,649],[700,748],[750,848],[850,888],[899,899]
00924     CHECK( check_set_contents( mb, set, contents ) );
00925 
00926     // remove everything
00927     std::reverse( contents.begin(), contents.begin() + contents.size() / 2 );  // mix up a bit
00928     rval = mb.remove_entities( set, &contents[0], contents.size() );CHECK_ERR( rval );
00929     contents.clear();
00930     CHECK( check_set_contents( mb, set, contents ) );
00931 
00932     // try complicated range-based remove
00933     range.clear();
00934     contents.clear();
00935     range.insert( 100, 200 );
00936     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00937     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00938     // [1000,2000]
00939     CHECK( check_set_contents( mb, set, contents ) );
00940 
00941     Range remove;
00942     remove.insert( 1, 3 );
00943     remove.insert( 10, 100 );
00944     remove.insert( 110, 120 );
00945     remove.insert( 130, 140 );
00946     remove.insert( 150, 160 );
00947     remove.insert( 190, 200 );
00948     remove.insert( 210, 220 );
00949     remove.insert( 230, 240 );
00950     range = subtract( range, remove );
00951 
00952     contents.clear();
00953     std::copy( range.begin(), range.end(), std::back_inserter( contents ) );
00954     rval = mb.remove_entities( set, remove );CHECK_ERR( rval );
00955     CHECK( check_set_contents( mb, set, contents ) );
00956 }
00957 
00958 void test_entities_by_type( unsigned flags )
00959 {
00960     EntityHandle first_vert, last_vert, first_hex, last_hex;
00961     Core mb;
00962     make_mesh( mb, first_vert, last_vert, first_hex, last_hex );
00963 
00964     // Create an entity set
00965     EntityHandle set;
00966     ErrorCode rval = mb.create_meshset( flags, set );CHECK_ERR( rval );
00967 
00968     // Test empty set
00969     CHECK( check_set_contents( mb, MBVERTEX, set, flags ) );
00970     CHECK( check_set_contents( mb, MBEDGE, set, flags ) );
00971     CHECK( check_set_contents( mb, MBHEX, set, flags ) );
00972 
00973     // Add stuff to set
00974     Range range;
00975     range.insert( first_vert, first_vert + 10 );
00976     range.insert( first_vert + 100, first_vert + 110 );
00977     range.insert( first_hex + 200, first_hex + 299 );
00978     range.insert( last_hex, last_hex - 99 );
00979     rval = mb.add_entities( set, range );CHECK_ERR( rval );
00980 
00981     // Test
00982     CHECK( check_set_contents( mb, MBVERTEX, set, flags ) );
00983     CHECK( check_set_contents( mb, MBEDGE, set, flags ) );
00984     CHECK( check_set_contents( mb, MBHEX, set, flags ) );
00985 }
00986 
00987 void test_entities_by_dimension( unsigned flags )
00988 {
00989     EntityHandle first_vert, last_vert, first_hex, last_hex;
00990     Core mb;
00991     make_mesh( mb, first_vert, last_vert, first_hex, last_hex );
00992 
00993     // Create an entity set
00994     EntityHandle set;
00995     ErrorCode rval = mb.create_meshset( flags, set );CHECK_ERR( rval );
00996 
00997     // Test empty set
00998     CHECK( check_set_contents( mb, 0, set, flags ) );
00999     CHECK( check_set_contents( mb, 1, set, flags ) );
01000     CHECK( check_set_contents( mb, 2, set, flags ) );
01001     CHECK( check_set_contents( mb, 3, set, flags ) );
01002 
01003     // Add stuff to set
01004     Range range;
01005     range.insert( first_vert, first_vert + 10 );
01006     range.insert( first_vert + 100, first_vert + 110 );
01007     range.insert( first_hex + 200, first_hex + 299 );
01008     range.insert( last_hex, last_hex - 99 );
01009     rval = mb.add_entities( set, range );CHECK_ERR( rval );
01010 
01011     // Test
01012     CHECK( check_set_contents( mb, 0, set, flags ) );
01013     CHECK( check_set_contents( mb, 1, set, flags ) );
01014     CHECK( check_set_contents( mb, 2, set, flags ) );
01015     CHECK( check_set_contents( mb, 3, set, flags ) );
01016 }
01017 
01018 bool test_boolean( Core& mb, BoolOp op, unsigned flags1, const Range& set1_ents, unsigned flags2,
01019                    const Range& set2_ents )
01020 {
01021     ErrorCode rval;
01022 
01023     // make sets
01024     EntityHandle set1, set2;
01025     rval = mb.create_meshset( flags1, set1 );CHECK_ERR( rval );
01026     rval = mb.create_meshset( flags2, set2 );CHECK_ERR( rval );
01027     rval = mb.add_entities( set1, set1_ents );CHECK_ERR( rval );
01028     rval = mb.add_entities( set2, set2_ents );CHECK_ERR( rval );
01029 
01030     Range tmp_range;
01031     std::vector< EntityHandle > expected;
01032     rval = MB_INDEX_OUT_OF_RANGE;
01033     switch( op )
01034     {
01035         case UNITE:
01036             if( flags1 & MESHSET_SET )
01037             {
01038                 tmp_range = set1_ents;
01039                 tmp_range.merge( set2_ents );
01040                 expected.resize( tmp_range.size() );
01041                 std::copy( tmp_range.begin(), tmp_range.end(), expected.begin() );
01042             }
01043             else
01044             {
01045                 expected.clear();
01046                 std::copy( set1_ents.begin(), set1_ents.end(), std::back_inserter( expected ) );
01047                 std::copy( set2_ents.begin(), set2_ents.end(), std::back_inserter( expected ) );
01048             }
01049             rval = mb.unite_meshset( set1, set2 );
01050             break;
01051         case INTERSECT:
01052             tmp_range = intersect( set1_ents, set2_ents );
01053             expected.resize( tmp_range.size() );
01054             std::copy( tmp_range.begin(), tmp_range.end(), expected.begin() );
01055             rval = mb.intersect_meshset( set1, set2 );
01056             break;
01057         case SUBTRACT:
01058             tmp_range = subtract( set1_ents, set2_ents );
01059             expected.resize( tmp_range.size() );
01060             std::copy( tmp_range.begin(), tmp_range.end(), expected.begin() );
01061             rval = mb.subtract_meshset( set1, set2 );
01062             break;
01063     }
01064     CHECK_ERR( rval );
01065 
01066     return check_set_contents( mb, set1, expected );
01067 }
01068 
01069 void test_intersect( unsigned flags1, unsigned flags2 )
01070 {
01071     Range empty, set1_ents, set2_ents;
01072     EntityHandle first_vert, last_vert, first_hex, last_hex;
01073     Core mb;
01074     make_mesh( mb, first_vert, last_vert, first_hex, last_hex );
01075 
01076     // define contents of first set
01077     set1_ents.insert( first_vert, first_vert + 10 );
01078     set1_ents.insert( first_vert + 100, first_vert + 199 );
01079     set1_ents.insert( first_hex + 100, first_hex + 200 );
01080 
01081     // define contents of second set
01082     set2_ents.insert( first_vert, first_vert );
01083     set2_ents.insert( first_vert + 10, first_vert + 10 );
01084     set2_ents.insert( first_vert + 90, first_vert + 209 );
01085     set2_ents.insert( first_hex + 50, first_hex + 300 );
01086 
01087     // check empty sets
01088     CHECK( test_boolean( mb, INTERSECT, flags1, empty, flags2, empty ) );
01089     // check intersection with 1 empty
01090     CHECK( test_boolean( mb, INTERSECT, flags1, empty, flags2, set2_ents ) );
01091     CHECK( test_boolean( mb, INTERSECT, flags1, set1_ents, flags2, empty ) );
01092     // check intersection of non-empty sets
01093     CHECK( test_boolean( mb, INTERSECT, flags1, set1_ents, flags2, set2_ents ) );
01094 }
01095 
01096 void test_unite( unsigned flags1, unsigned flags2 )
01097 {
01098     Range empty, set1_ents, set2_ents;
01099     EntityHandle first_vert, last_vert, first_hex, last_hex;
01100     Core mb;
01101     make_mesh( mb, first_vert, last_vert, first_hex, last_hex );
01102 
01103     // define contents of first set
01104     set1_ents.insert( first_vert, first_vert + 10 );
01105     set1_ents.insert( first_vert + 100, first_vert + 199 );
01106     set1_ents.insert( first_hex + 100, first_hex + 200 );
01107 
01108     // define contents of second set
01109     set2_ents.insert( first_vert, first_vert );
01110     set2_ents.insert( first_vert + 11, first_vert + 99 );
01111     set2_ents.insert( first_vert + 150, first_vert + 209 );
01112     set2_ents.insert( first_vert + 211, first_vert + 211 );
01113     set2_ents.insert( first_hex + 50, first_hex + 99 );
01114 
01115     // check empty sets
01116     CHECK( test_boolean( mb, UNITE, flags1, empty, flags2, empty ) );
01117     // check union with 1 empty
01118     CHECK( test_boolean( mb, UNITE, flags1, empty, flags2, set2_ents ) );
01119     CHECK( test_boolean( mb, UNITE, flags1, set1_ents, flags2, empty ) );
01120     // check union of non-empty sets
01121     CHECK( test_boolean( mb, UNITE, flags1, set1_ents, flags2, set2_ents ) );
01122 }
01123 
01124 void test_subtract( unsigned flags1, unsigned flags2 )
01125 {
01126     Range empty, set1_ents, set2_ents;
01127     EntityHandle first_vert, last_vert, first_hex, last_hex;
01128     Core mb;
01129     make_mesh( mb, first_vert, last_vert, first_hex, last_hex );
01130 
01131     // define contents of first set
01132     set1_ents.insert( first_vert, first_vert + 10 );
01133     set1_ents.insert( first_vert + 100, first_vert + 199 );
01134     set1_ents.insert( first_hex + 100, first_hex + 200 );
01135 
01136     // define contents of second set
01137     set2_ents.insert( first_vert, first_vert );
01138     set2_ents.insert( first_vert + 9, first_vert + 9 );
01139     set2_ents.insert( first_vert + 11, first_vert + 99 );
01140     set2_ents.insert( first_vert + 150, first_vert + 199 );
01141     set2_ents.insert( first_hex + 50, first_hex + 60 );
01142     set2_ents.insert( first_hex + 90, first_hex + 220 );
01143 
01144     // check empty sets
01145     CHECK( test_boolean( mb, SUBTRACT, flags1, empty, flags2, empty ) );
01146     // check union with 1 empty
01147     CHECK( test_boolean( mb, SUBTRACT, flags1, empty, flags2, set2_ents ) );
01148     CHECK( test_boolean( mb, SUBTRACT, flags1, set1_ents, flags2, empty ) );
01149     // check union of non-empty sets
01150     CHECK( test_boolean( mb, SUBTRACT, flags1, set1_ents, flags2, set2_ents ) );
01151 }
01152 
01153 void test_contains_entities( unsigned flags )
01154 {
01155     CHECK( !( flags & MESHSET_TRACK_OWNER ) );
01156     MeshSet set( flags );
01157     bool result;
01158 
01159     const EntityHandle entities[] = { 1, 2, 3, 6, 10, 11, 25, 100 };
01160     const int num_ents            = sizeof( entities ) / sizeof( EntityHandle );
01161     ErrorCode rval                = set.add_entities( entities, num_ents, 0, 0 );CHECK_ERR( rval );
01162 
01163     result = set.contains_entities( entities, num_ents, Interface::UNION );
01164     CHECK( result );
01165     result = set.contains_entities( entities, num_ents, Interface::INTERSECT );
01166     CHECK( result );
01167 
01168     result = set.contains_entities( entities, 1, Interface::UNION );
01169     CHECK( result );
01170     result = set.contains_entities( entities, 1, Interface::INTERSECT );
01171     CHECK( result );
01172 
01173     const EntityHandle entities2[] = { 3, 4, 5 };
01174     const int num_ents2            = sizeof( entities2 ) / sizeof( EntityHandle );
01175     result                         = set.contains_entities( entities2, num_ents2, Interface::UNION );
01176     CHECK( result );
01177     result = set.contains_entities( entities2, num_ents2, Interface::INTERSECT );
01178     CHECK( !result );
01179 }
01180 
01181 void test_clear( unsigned flags )
01182 {
01183     std::vector< EntityHandle > contents( 4 );
01184     Core mb;
01185     make_mesh( mb, contents[0], contents[1], contents[2], contents[3] );
01186     EntityHandle set;
01187     ErrorCode rval = mb.create_meshset( flags, set );CHECK_ERR( rval );
01188     rval = mb.add_entities( set, &contents[0], contents.size() );CHECK_ERR( rval );
01189     CHECK( check_set_contents( mb, set, contents ) );
01190     rval = mb.clear_meshset( &set, 1 );CHECK_ERR( rval );
01191     contents.clear();
01192     CHECK( check_set_contents( mb, set, contents ) );
01193 }
01194 
01195 void regression_insert_set_1()
01196 {
01197     EntityHandle e                      = CREATE_HANDLE( MBEDGE, 0 );
01198     EntityHandle q                      = CREATE_HANDLE( MBQUAD, 0 );
01199     const EntityHandle initial_ranges[] = {
01200         0x7fe,      0x7fe,      0x802,      0x80b,      0xb3a,      0xb3c,      0xb6b,      0xb6b,      0xbed,
01201         0xbee,      0x19ff,     0x19ff,     0x1a0b,     0x1a0b,     0x1a16,     0x1a17,     0x1a56,     0x1a57,
01202         0x2554,     0x255c,     e + 0x0099, e + 0x009b, e + 0x00c0, e + 0x00c2, e + 0x0729, e + 0x0732, e + 0x0a3b,
01203         e + 0x0a3d, e + 0x0ba9, e + 0x0bab, e + 0x2322, e + 0x232b, q + 0x00c,  q + 0x017,  q + 0x0e9,  q + 0x112,
01204         q + 0x2f2,  q + 0x303,  q + 0x67e,  q + 0x6a5,  q + 0x866,  q + 0x871,  q + 0x8f5,  q + 0x900,  q + 0xc06,
01205         q + 0xc17,  q + 0xc7e,  q + 0xc9b,  q + 0xce0,  q + 0xd07
01206     };
01207 
01208     const EntityHandle new_ranges[] = { 0x7e1,  0x829,  0xb37,  0xb63,  0xb6b,  0xb6b,  0xb73,  0xb75,  0xbed,
01209                                         0xbee,  0xc0b,  0xc10,  0x19fd, 0x19fd, 0x19ff, 0x19ff, 0x1a02, 0x1a04,
01210                                         0x1a0b, 0x1a0b, 0x1a11, 0x1a17, 0x1a1b, 0x1a23, 0x1a56, 0x1a57, 0x1a7c,
01211                                         0x1a96, 0x1bb5, 0x1bba, 0x254b, 0x2565, 0x25a5, 0x25bf };
01212 
01213     Core moab;
01214     Interface& mb = moab;
01215     ErrorCode rval;
01216 
01217     EntityHandle set;
01218     rval = mb.create_meshset( MESHSET_SET, set );CHECK_ERR( rval );
01219 
01220     Range init, add;
01221     for( size_t i = 0; i < sizeof( initial_ranges ) / sizeof( initial_ranges[0] ); i += 2 )
01222         init.insert( initial_ranges[i], initial_ranges[i + 1] );
01223     for( size_t i = 0; i < sizeof( new_ranges ) / sizeof( new_ranges[0] ); i += 2 )
01224         add.insert( new_ranges[i], new_ranges[i + 1] );
01225 
01226     rval = mb.add_entities( set, init );CHECK_ERR( rval );
01227     rval = mb.add_entities( set, add );CHECK_ERR( rval );
01228 
01229     std::vector< EntityHandle > contents;
01230     rval = mb.get_entities_by_handle( set, contents );CHECK_ERR( rval );
01231 
01232     init.merge( add );
01233     Range::iterator r                       = init.begin();
01234     std::vector< EntityHandle >::iterator v = contents.begin();
01235     for( ;; )
01236     {
01237         if( r == init.end() || v == contents.end() )
01238         {
01239             CHECK( r == init.end() );
01240             CHECK( v == contents.end() );
01241             break;
01242         }
01243         CHECK_EQUAL( *r, *v );
01244         ++r;
01245         ++v;
01246     }
01247 }
01248 
01249 void test_all_iterators()
01250 {
01251     test_iterators( MESHSET_SET, true, false );
01252     test_iterators( MESHSET_SET, false, true );
01253 
01254     test_iterators( MESHSET_ORDERED, true, false );
01255     test_iterators( MESHSET_ORDERED, false, true );
01256 }
01257 
01258 void test_iterators( unsigned int flags, bool test_type, bool test_dim )
01259 {
01260     if( test_type && test_dim ) CHECK_ERR( MB_FAILURE );
01261 
01262     EntityHandle first_vert, last_vert, first_hex, last_hex;
01263     Core mb;
01264     make_mesh( mb, first_vert, last_vert, first_hex, last_hex );
01265 
01266     // Create an entity set
01267     EntityHandle set;
01268     ErrorCode rval = mb.create_meshset( flags, set );CHECK_ERR( rval );
01269 
01270     // Test iteration over empty set
01271     SetIterator* iter = NULL;
01272     rval              = mb.create_set_iterator( set, MBMAXTYPE, -1, 1, false, iter );CHECK_ERR( rval );
01273     if( NULL != iter )
01274     {
01275         test_iterator( mb, iter, set, MBMAXTYPE, -1 );
01276         delete iter;
01277         iter = NULL;
01278     }
01279 
01280     rval = mb.create_set_iterator( set, MBMAXTYPE, -1, 100, false, iter );CHECK_ERR( rval );
01281     if( NULL != iter )
01282     {
01283         test_iterator( mb, iter, set, MBMAXTYPE, -1 );
01284         delete iter;
01285         iter = NULL;
01286     }
01287 
01288     rval = mb.create_set_iterator( set, MBMAXTYPE, 0, 1, false, iter );CHECK_ERR( rval );
01289     if( NULL != iter )
01290     {
01291         test_iterator( mb, iter, set, MBMAXTYPE, 0 );
01292         delete iter;
01293         iter = NULL;
01294     }
01295 
01296     rval = mb.create_set_iterator( set, MBMAXTYPE, 3, 1, false, iter );CHECK_ERR( rval );
01297     if( NULL != iter )
01298     {
01299         test_iterator( mb, iter, set, MBMAXTYPE, 3 );
01300         delete iter;
01301         iter = NULL;
01302     }
01303 
01304     // Add stuff to set
01305     Range range;
01306     range.insert( first_vert, first_vert + 10 );
01307     range.insert( first_vert + 100, first_vert + 110 );
01308     range.insert( first_hex + 200, first_hex + 299 );
01309     range.insert( last_hex, last_hex - 99 );
01310     rval = mb.add_entities( set, range );CHECK_ERR( rval );
01311 
01312     EntityType etype = MBVERTEX;
01313     int edim         = -1;
01314     if( test_dim && !test_type )
01315     {
01316         edim  = 0;
01317         etype = MBMAXTYPE;
01318     }
01319 
01320     // Test
01321     // chunk size 1
01322     rval = mb.create_set_iterator( set, etype, edim, 1, false, iter );CHECK_ERR( rval );
01323     if( NULL != iter )
01324     {
01325         test_iterator( mb, iter, set, etype, edim );
01326         delete iter;
01327         iter = NULL;
01328     }
01329 
01330     // chunk size 3
01331     rval = mb.create_set_iterator( set, etype, edim, 3, false, iter );CHECK_ERR( rval );
01332     if( NULL != iter )
01333     {
01334         test_iterator( mb, iter, set, etype, edim );
01335         delete iter;
01336         iter = NULL;
01337     }
01338 
01339     // chunk size 11
01340     rval = mb.create_set_iterator( set, etype, edim, 11, false, iter );CHECK_ERR( rval );
01341     if( NULL != iter )
01342     {
01343         test_iterator( mb, iter, set, etype, edim );
01344         delete iter;
01345         iter = NULL;
01346     }
01347 
01348     // chunk size 100
01349     rval = mb.create_set_iterator( set, etype, edim, 100, false, iter );CHECK_ERR( rval );
01350     if( NULL != iter )
01351     {
01352         test_iterator( mb, iter, set, etype, edim );
01353         delete iter;
01354         iter = NULL;
01355     }
01356 
01357     if( test_type )
01358     {
01359         // chunk size 1, all ents
01360         rval = mb.create_set_iterator( set, MBMAXTYPE, -1, 1, false, iter );CHECK_ERR( rval );
01361         if( NULL != iter )
01362         {
01363             test_iterator( mb, iter, set, MBMAXTYPE, -1 );
01364             delete iter;
01365             iter = NULL;
01366         }
01367 
01368         // chunk size 3, all ents
01369         rval = mb.create_set_iterator( set, MBMAXTYPE, -1, 3, false, iter );CHECK_ERR( rval );
01370         if( NULL != iter )
01371         {
01372             test_iterator( mb, iter, set, MBMAXTYPE, -1 );
01373             delete iter;
01374             iter = NULL;
01375         }
01376 
01377         // chunk size large, all ents
01378         rval = mb.create_set_iterator( set, MBMAXTYPE, -1, 100000, false, iter );CHECK_ERR( rval );
01379         if( NULL != iter )
01380         {
01381             test_iterator( mb, iter, set, MBMAXTYPE, -1 );
01382             delete iter;
01383             iter = NULL;
01384         }
01385 
01386         etype = MBEDGE;
01387         // edges, chunk size 1
01388         rval = mb.create_set_iterator( set, etype, edim, 1, false, iter );CHECK_ERR( rval );
01389         if( NULL != iter )
01390         {
01391             test_iterator( mb, iter, set, etype, edim );
01392             delete iter;
01393             iter = NULL;
01394         }
01395 
01396         // edges, chunk size 100
01397         rval = mb.create_set_iterator( set, etype, edim, 100, false, iter );CHECK_ERR( rval );
01398         if( NULL != iter )
01399         {
01400             test_iterator( mb, iter, set, etype, edim );
01401             delete iter;
01402             iter = NULL;
01403         }
01404 
01405         etype = MBHEX;
01406 
01407         // hexes, chunk size 1
01408         rval = mb.create_set_iterator( set, etype, edim, 1, false, iter );CHECK_ERR( rval );
01409         if( NULL != iter )
01410         {
01411             test_iterator( mb, iter, set, etype, edim );
01412             delete iter;
01413             iter = NULL;
01414         }
01415 
01416         // hexes, chunk size 3
01417         rval = mb.create_set_iterator( set, etype, edim, 3, false, iter );CHECK_ERR( rval );
01418         if( NULL != iter )
01419         {
01420             test_iterator( mb, iter, set, etype, edim );
01421             delete iter;
01422             iter = NULL;
01423         }
01424 
01425         // hexes, chunk size 1000
01426         rval = mb.create_set_iterator( set, etype, edim, 1000, false, iter );CHECK_ERR( rval );
01427         if( NULL != iter )
01428         {
01429             test_iterator( mb, iter, set, etype, edim );
01430             delete iter;
01431             iter = NULL;
01432         }
01433     }
01434 }
01435 
01436 void test_iterator( Interface& moab, SetIterator* iter, EntityHandle set, EntityType etype, int dim )
01437 {
01438     // iterate over the set, adding to contents
01439     std::vector< EntityHandle > entities, entities2;
01440     bool atend = false;
01441     ErrorCode rval;
01442 
01443     while( !atend )
01444     {
01445         rval = iter->get_next_arr( entities, atend );CHECK_ERR( rval );
01446     }
01447 
01448     // check contents against what's in the set
01449     if( MBMAXTYPE != etype )
01450     {
01451         rval = moab.get_entities_by_type( set, etype, entities2 );CHECK_ERR( rval );
01452     }
01453     else if( MBMAXTYPE == etype && -1 == dim )
01454     {
01455         rval = moab.get_entities_by_handle( set, entities2 );CHECK_ERR( rval );
01456     }
01457     else if( -1 != dim )
01458     {
01459         rval = moab.get_entities_by_dimension( set, dim, entities2 );CHECK_ERR( rval );
01460     }
01461     else
01462     {
01463         // error, one of those needs to be true
01464         CHECK_ERR( MB_FAILURE );
01465     }
01466 
01467     CHECK_EQUAL( entities.size(), entities2.size() );
01468 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines