MOAB: Mesh Oriented datABase  (version 5.2.1)
MeshTopoUtil.cpp
Go to the documentation of this file.
00001 /**
00002  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
00003  * storing and accessing finite element mesh data.
00004  *
00005  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
00006  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00007  * retains certain rights in this software.
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  */
00015 
00016 #ifdef WIN32
00017 #pragma warning( disable : 4786 )
00018 #endif
00019 
00020 #include "moab/MeshTopoUtil.hpp"
00021 #include "moab/Range.hpp"
00022 #include "Internals.hpp"
00023 #include "moab/Interface.hpp"
00024 #include "moab/CN.hpp"
00025 
00026 #include <assert.h>
00027 
00028 #define RR                                        \
00029     {                                             \
00030         if( MB_SUCCESS != result ) return result; \
00031     }
00032 
00033 namespace moab
00034 {
00035 
00036 //! generate all the AEntities bounding the vertices
00037 ErrorCode MeshTopoUtil::construct_aentities( const Range& vertices )
00038 {
00039     Range out_range;
00040     ErrorCode result;
00041     result = mbImpl->get_adjacencies( vertices, 1, true, out_range, Interface::UNION );
00042     if( MB_SUCCESS != result ) return result;
00043     out_range.clear();
00044     result = mbImpl->get_adjacencies( vertices, 2, true, out_range, Interface::UNION );
00045     if( MB_SUCCESS != result ) return result;
00046     out_range.clear();
00047     result = mbImpl->get_adjacencies( vertices, 3, true, out_range, Interface::UNION );
00048 
00049     return result;
00050 }
00051 
00052 //! given an entity, get its average position (avg vertex locations)
00053 ErrorCode MeshTopoUtil::get_average_position( Range& entities, double* avg_position )
00054 {
00055     std::vector< EntityHandle > ent_vec;
00056     std::copy( entities.begin(), entities.end(), std::back_inserter( ent_vec ) );
00057     return get_average_position( &ent_vec[0], ent_vec.size(), avg_position );
00058 }
00059 
00060 //! given an entity, get its average position (avg vertex locations)
00061 ErrorCode MeshTopoUtil::get_average_position( const EntityHandle* entities, const int num_entities,
00062                                               double* avg_position )
00063 {
00064     double dum_pos[3];
00065     avg_position[0] = avg_position[1] = avg_position[2] = 0.0;
00066 
00067     Range connect;
00068     ErrorCode result = mbImpl->get_adjacencies( entities, num_entities, 0, false, connect, Interface::UNION );
00069     if( MB_SUCCESS != result ) return result;
00070 
00071     if( connect.empty() ) return MB_FAILURE;
00072 
00073     for( Range::iterator rit = connect.begin(); rit != connect.end(); ++rit )
00074     {
00075         result = mbImpl->get_coords( &( *rit ), 1, dum_pos );
00076         if( MB_SUCCESS != result ) return result;
00077         avg_position[0] += dum_pos[0];
00078         avg_position[1] += dum_pos[1];
00079         avg_position[2] += dum_pos[2];
00080     }
00081     avg_position[0] /= (double)connect.size();
00082     avg_position[1] /= (double)connect.size();
00083     avg_position[2] /= (double)connect.size();
00084 
00085     return MB_SUCCESS;
00086 }
00087 
00088 //! given an entity, get its average position (avg vertex locations)
00089 ErrorCode MeshTopoUtil::get_average_position( const EntityHandle entity, double* avg_position )
00090 {
00091     const EntityHandle* connect = NULL;
00092     int num_connect             = 0;
00093     if( MBVERTEX == mbImpl->type_from_handle( entity ) ) return mbImpl->get_coords( &entity, 1, avg_position );
00094 
00095     ErrorCode result = mbImpl->get_connectivity( entity, connect, num_connect );
00096     if( MB_SUCCESS != result ) return result;
00097 
00098     return get_average_position( connect, num_connect, avg_position );
00099 }
00100 
00101 // given an entity, find the entities of next higher dimension around
00102 // that entity, ordered by connection through next higher dimension entities;
00103 // if any of the star entities is in only one entity of next higher dimension,
00104 // on_boundary is returned true
00105 ErrorCode MeshTopoUtil::star_entities( const EntityHandle star_center, std::vector< EntityHandle >& star_ents,
00106                                        bool& bdy_entity, const EntityHandle starting_star_entity,
00107                                        std::vector< EntityHandle >* star_entities_dp2, Range* star_candidates_dp2 )
00108 {
00109     // now start the traversal
00110     bdy_entity               = false;
00111     EntityHandle last_entity = starting_star_entity, last_dp2 = 0, next_entity, next_dp2;
00112     std::vector< EntityHandle > star_dp2;
00113     ErrorCode result;
00114     int center_dim = mbImpl->dimension_from_handle( star_center );
00115 
00116     Range tmp_candidates_dp2;
00117     if( NULL != star_candidates_dp2 )
00118         tmp_candidates_dp2 = *star_candidates_dp2;
00119     else
00120     {
00121         result = mbImpl->get_adjacencies( &star_center, 1, center_dim + 2, false, tmp_candidates_dp2 );
00122         if( MB_SUCCESS != result ) return result;
00123     }
00124 
00125     do
00126     {
00127         // get the next star entity
00128         result = star_next_entity( star_center, last_entity, last_dp2, &tmp_candidates_dp2, next_entity, next_dp2 );
00129         if( MB_SUCCESS != result ) return result;
00130 
00131         // special case: if starting_star_entity isn't connected to any entities of next
00132         // higher dimension, it's the only entity in the star; put it on the list and return
00133         if( star_ents.empty() && next_entity == 0 && next_dp2 == 0 )
00134         {
00135             star_ents.push_back( last_entity );
00136             bdy_entity = true;
00137             return MB_SUCCESS;
00138         }
00139 
00140         // if we're at a bdy and bdy_entity hasn't been set yet, we're at the
00141         // first bdy; reverse the lists and start traversing in the other direction; but,
00142         // pop the last star entity off the list and find it again, so that we properly
00143         // check for next_dp2
00144         if( 0 == next_dp2 && !bdy_entity )
00145         {
00146             star_ents.push_back( next_entity );
00147             bdy_entity = true;
00148             std::reverse( star_ents.begin(), star_ents.end() );
00149             star_ents.pop_back();
00150             last_entity = star_ents.back();
00151             if( !star_dp2.empty() )
00152             {
00153                 std::reverse( star_dp2.begin(), star_dp2.end() );
00154                 last_dp2 = star_dp2.back();
00155             }
00156         }
00157         // else if we're not on the bdy and next_entity is already in star, that means
00158         // we've come all the way around; don't put next_entity on list again, and
00159         // zero out last_dp2 to terminate while loop
00160         else if( !bdy_entity && std::find( star_ents.begin(), star_ents.end(), next_entity ) != star_ents.end() &&
00161                  ( std::find( star_dp2.begin(), star_dp2.end(), next_dp2 ) != star_dp2.end() || !next_dp2 ) )
00162         {
00163             last_dp2 = 0;
00164         }
00165 
00166         // else, just assign last entities seen and go on to next iteration
00167         else
00168         {
00169             if( std::find( star_ents.begin(), star_ents.end(), next_entity ) == star_ents.end() )
00170                 star_ents.push_back( next_entity );
00171             if( 0 != next_dp2 )
00172             {
00173                 star_dp2.push_back( next_dp2 );
00174                 tmp_candidates_dp2.erase( next_dp2 );
00175             }
00176             last_entity = next_entity;
00177             last_dp2    = next_dp2;
00178         }
00179     } while( 0 != last_dp2 );
00180 
00181     // copy over the star_dp2 list, if requested
00182     if( NULL != star_entities_dp2 ) ( *star_entities_dp2 ).swap( star_dp2 );
00183 
00184     return MB_SUCCESS;
00185 }
00186 
00187 ErrorCode MeshTopoUtil::star_next_entity( const EntityHandle star_center, const EntityHandle last_entity,
00188                                           const EntityHandle last_dp1, Range* star_candidates_dp1,
00189                                           EntityHandle& next_entity, EntityHandle& next_dp1 )
00190 {
00191     // given a star_center, a last_entity (whose dimension should be 1 greater than center)
00192     // and last_dp1 (dimension 2 higher than center), returns the next star entity across
00193     // last_dp1, and the next dp1 entity sharing next_entity; if star_candidates is non-empty,
00194     // star must come from those
00195     Range from_ents, to_ents;
00196     from_ents.insert( star_center );
00197     if( 0 != last_dp1 ) from_ents.insert( last_dp1 );
00198 
00199     int dim = mbImpl->dimension_from_handle( star_center );
00200 
00201     ErrorCode result = mbImpl->get_adjacencies( from_ents, dim + 1, true, to_ents );
00202     if( MB_SUCCESS != result ) return result;
00203 
00204     // remove last_entity from result, and should only have 1 left, if any
00205     if( 0 != last_entity ) to_ents.erase( last_entity );
00206 
00207     // if no last_dp1, contents of to_ents should share dp1-dimensional entity with last_entity
00208     if( 0 != last_entity && 0 == last_dp1 )
00209     {
00210         Range tmp_to_ents;
00211         for( Range::iterator rit = to_ents.begin(); rit != to_ents.end(); ++rit )
00212         {
00213             if( 0 != common_entity( last_entity, *rit, dim + 2 ) ) tmp_to_ents.insert( *rit );
00214         }
00215         to_ents = tmp_to_ents;
00216     }
00217 
00218     if( 0 == last_dp1 && to_ents.size() > 1 && NULL != star_candidates_dp1 && !star_candidates_dp1->empty() )
00219     {
00220         // if we have a choice of to_ents and no previous dp1 and there are dp1 candidates,
00221         // the one we choose needs to be adjacent to one of the candidates
00222         result = mbImpl->get_adjacencies( *star_candidates_dp1, dim + 1, true, from_ents, Interface::UNION );
00223         if( MB_SUCCESS != result ) return result;
00224         to_ents = intersect( to_ents, from_ents );
00225     }
00226 
00227     if( !to_ents.empty() )
00228         next_entity = *to_ents.begin();
00229     else
00230     {
00231         next_entity = 0;
00232         next_dp1    = 0;
00233         return MB_SUCCESS;
00234     }
00235 
00236     // get next_dp1
00237     if( 0 != star_candidates_dp1 )
00238         to_ents = *star_candidates_dp1;
00239     else
00240         to_ents.clear();
00241 
00242     result = mbImpl->get_adjacencies( &next_entity, 1, dim + 2, true, to_ents );
00243     if( MB_SUCCESS != result ) return result;
00244 
00245     // can't be last one
00246     if( 0 != last_dp1 ) to_ents.erase( last_dp1 );
00247 
00248     if( !to_ents.empty() ) next_dp1 = *to_ents.begin();
00249 
00250     // could be zero, means we're at bdy
00251     else
00252         next_dp1 = 0;
00253 
00254     return MB_SUCCESS;
00255 }
00256 
00257 ErrorCode MeshTopoUtil::star_entities_nonmanifold( const EntityHandle star_entity,
00258                                                    std::vector< std::vector< EntityHandle > >& stars,
00259                                                    std::vector< bool >* bdy_flags,
00260                                                    std::vector< std::vector< EntityHandle > >* dp2_stars )
00261 {
00262     // Get a series of (d+1)-dimensional stars around a d-dimensional entity, such that
00263     // each star is on a (d+2)-manifold containing the d-dimensional entity; each star
00264     // is either open or closed, and also defines a (d+2)-star whose entities are bounded by
00265     // (d+1)-entities on the star and on the (d+2)-manifold
00266     //
00267     // Algorithm:
00268     // get the (d+2)-manifold entities; for d=1 / d+2=3, just assume all connected elements, since
00269     //   we don't do 4d yet
00270     // get intersection of (d+1)-entities adjacent to star entity and union of (d+1)-entities
00271     //   adjacent to (d+2)-manifold entities; these will be the entities in the star
00272     // while (d+1)-entities
00273     //   remove (d+1)-entity from (d+1)-entities
00274     //   get the (d+1)-star and (d+2)-star around that (d+1)-entity (using star_entities)
00275     //   save that star to the star list, and the bdy flag and (d+2)-star if requested
00276     //   remove (d+2)-entities from the (d+2)-manifold entities
00277     //   remove (d+1)-entities from the (d+1)-entities
00278     // (end while)
00279 
00280     int this_dim = mbImpl->dimension_from_handle( star_entity );
00281     if( 3 <= this_dim || 0 > this_dim ) return MB_FAILURE;
00282 
00283     // get the (d+2)-manifold entities; for d=1 / d+2=3, just assume all connected elements, since
00284     //   we don't do 4d yet
00285     Range dp2_manifold;
00286     ErrorCode result = get_manifold( star_entity, this_dim + 2, dp2_manifold );
00287     if( MB_SUCCESS != result ) return result;
00288 
00289     // get intersection of (d+1)-entities adjacent to star and union of (d+1)-entities
00290     //   adjacent to (d+2)-manifold entities; also add manifold (d+1)-entities, to catch
00291     //   any not connected to (d+2)-entities
00292     Range dp1_manifold;
00293     result = mbImpl->get_adjacencies( dp2_manifold, this_dim + 1, false, dp1_manifold, Interface::UNION );
00294     if( MB_SUCCESS != result ) return result;
00295 
00296     result = mbImpl->get_adjacencies( &star_entity, 1, this_dim + 1, false, dp1_manifold );
00297     if( MB_SUCCESS != result ) return result;
00298 
00299     result = get_manifold( star_entity, this_dim + 1, dp1_manifold );
00300     if( MB_SUCCESS != result ) return result;
00301 
00302     // while (d+1)-entities
00303     while( !dp1_manifold.empty() )
00304     {
00305 
00306         //   get (d+1)-entity from (d+1)-entities (don't remove it until after star,
00307         //     since the star entities must come from dp1_manifold)
00308         EntityHandle this_ent = *dp1_manifold.begin();
00309 
00310         //   get the (d+1)-star and (d+2)-star around that (d+1)-entity (using star_entities)
00311         std::vector< EntityHandle > this_star_dp1, this_star_dp2;
00312         bool on_bdy;
00313         result = star_entities( star_entity, this_star_dp1, on_bdy, this_ent, &this_star_dp2, &dp2_manifold );
00314         if( MB_SUCCESS != result ) return result;
00315 
00316         // if there's no star entities, it must mean this_ent isn't bounded by any dp2
00317         // entities (wasn't put into star in star_entities 'cuz we're passing in non-null
00318         // dp2_manifold above); put it in
00319         if( this_star_dp1.empty() )
00320         {
00321             Range dum_range;
00322             result = mbImpl->get_adjacencies( &this_ent, 1, this_dim + 2, false, dum_range );
00323             if( MB_SUCCESS != result ) return result;
00324             if( dum_range.empty() ) this_star_dp1.push_back( this_ent );
00325         }
00326 
00327         // now we can remove it
00328         dp1_manifold.erase( dp1_manifold.begin() );
00329 
00330         //   save that star to the star list, and the bdy flag and (d+2)-star if requested
00331         if( !this_star_dp1.empty() )
00332         {
00333             stars.push_back( this_star_dp1 );
00334             if( NULL != bdy_flags ) bdy_flags->push_back( on_bdy );
00335             if( NULL != dp2_stars ) dp2_stars->push_back( this_star_dp2 );
00336         }
00337 
00338         //   remove (d+2)-entities from the (d+2)-manifold entities
00339         for( std::vector< EntityHandle >::iterator vit = this_star_dp2.begin(); vit != this_star_dp2.end(); ++vit )
00340             dp2_manifold.erase( *vit );
00341 
00342         //   remove (d+1)-entities from the (d+1)-entities
00343         for( std::vector< EntityHandle >::iterator vit = this_star_dp1.begin(); vit != this_star_dp1.end(); ++vit )
00344             dp1_manifold.erase( *vit );
00345 
00346         // (end while)
00347     }
00348 
00349     // check for leftover dp2 manifold entities, these should be in one of the
00350     // stars
00351     if( !dp2_manifold.empty() )
00352     {
00353         for( Range::iterator rit = dp2_manifold.begin(); rit != dp2_manifold.end(); ++rit ) {}
00354     }
00355 
00356     return MB_SUCCESS;
00357 }
00358 
00359 //! get (target_dim)-dimensional manifold entities connected to star_entity; that is,
00360 //! the entities with <= 1 connected (target_dim+2)-dimensional adjacent entities;
00361 //! for target_dim=3, just return all of them
00362 //! just insert into the list, w/o clearing manifold list first
00363 ErrorCode MeshTopoUtil::get_manifold( const EntityHandle star_entity, const int target_dim, Range& manifold )
00364 {
00365     // get all the entities of target dimension connected to star
00366     Range tmp_range;
00367     ErrorCode result = mbImpl->get_adjacencies( &star_entity, 1, target_dim, false, tmp_range );
00368     if( MB_SUCCESS != result ) return result;
00369 
00370     // now save the ones which are (target_dim+1)-dimensional manifold;
00371     // for target_dim=3, just return whole range, since we don't do 4d
00372     if( target_dim == 3 )
00373     {
00374         manifold.merge( tmp_range );
00375         return MB_SUCCESS;
00376     }
00377 
00378     for( Range::iterator rit = tmp_range.begin(); rit != tmp_range.end(); ++rit )
00379     {
00380         Range dum_range;
00381         // get (target_dim+1)-dimensional entities
00382         result = mbImpl->get_adjacencies( &( *rit ), 1, target_dim + 1, false, dum_range );
00383         if( MB_SUCCESS != result ) return result;
00384 
00385         // if there are only 1 or zero, add to manifold list
00386         if( 1 >= dum_range.size() ) manifold.insert( *rit );
00387     }
00388 
00389     return MB_SUCCESS;
00390 }
00391 
00392 //! get "bridge" or "2nd order" adjacencies, going through dimension bridge_dim
00393 ErrorCode MeshTopoUtil::get_bridge_adjacencies( Range& from_entities, int bridge_dim, int to_dim, Range& to_ents,
00394                                                 int num_layers )
00395 {
00396     Range bridge_ents, accum_layers, new_toents( from_entities );
00397     ErrorCode result;
00398     if( 0 == num_layers || from_entities.empty() ) return MB_FAILURE;
00399 
00400     // for each layer, get bridge-adj entities and accumulate
00401     for( int nl = 0; nl < num_layers; nl++ )
00402     {
00403         Range new_bridges;
00404         // get bridge ents
00405         result = mbImpl->get_adjacencies( new_toents, bridge_dim, true, new_bridges, Interface::UNION );
00406         if( MB_SUCCESS != result ) return result;
00407 
00408         // get to_dim adjacencies, merge into to_ents
00409         Range new_layer;
00410         if( -1 == to_dim )
00411         {
00412             result = mbImpl->get_adjacencies( new_bridges, 3, false, new_layer, Interface::UNION );
00413             if( MB_SUCCESS != result ) return result;
00414             for( int d = 2; d >= 1; d-- )
00415             {
00416                 result = mbImpl->get_adjacencies( to_ents, d, true, new_layer, Interface::UNION );
00417                 if( MB_SUCCESS != result ) return result;
00418             }
00419         }
00420         else
00421         {
00422             result = mbImpl->get_adjacencies( new_bridges, to_dim, false, new_layer, Interface::UNION );
00423             if( MB_SUCCESS != result ) return result;
00424         }
00425 
00426         // subtract last_toents to get new_toents
00427         accum_layers.merge( new_layer );
00428         if( nl < num_layers - 1 ) new_toents = subtract( new_layer, new_toents );
00429     }
00430     to_ents.merge( accum_layers );
00431 
00432     return MB_SUCCESS;
00433 }
00434 
00435 //! get "bridge" or "2nd order" adjacencies, going through dimension bridge_dim
00436 ErrorCode MeshTopoUtil::get_bridge_adjacencies( const EntityHandle from_entity, const int bridge_dim, const int to_dim,
00437                                                 Range& to_adjs )
00438 {
00439     // get pointer to connectivity for this entity
00440     const EntityHandle* connect;
00441     int num_connect;
00442     ErrorCode result     = MB_SUCCESS;
00443     EntityType from_type = TYPE_FROM_HANDLE( from_entity );
00444     if( from_type == MBVERTEX )
00445     {
00446         connect     = &from_entity;
00447         num_connect = 1;
00448     }
00449     else
00450     {
00451         result = mbImpl->get_connectivity( from_entity, connect, num_connect );
00452         if( MB_SUCCESS != result ) return result;
00453     }
00454 
00455     if( from_type >= MBENTITYSET ) return MB_FAILURE;
00456 
00457     int from_dim = CN::Dimension( from_type );
00458 
00459     Range to_ents;
00460 
00461     if( bridge_dim < from_dim )
00462     {
00463         // looping over each sub-entity of dimension bridge_dim...
00464         if( MBPOLYGON == from_type )
00465         {
00466             for( int i = 0; i < num_connect; i++ )
00467             {
00468                 // loop over edges, and get the vertices
00469                 EntityHandle verts_on_edge[2] = { connect[i], connect[( i + 1 ) % num_connect] };
00470                 to_ents.clear();
00471                 ErrorCode tmp_result =
00472                     mbImpl->get_adjacencies( verts_on_edge, 2, to_dim, false, to_ents, Interface::INTERSECT );
00473                 if( MB_SUCCESS != tmp_result ) result = tmp_result;
00474                 to_adjs.merge( to_ents );
00475             }
00476         }
00477         else
00478         {
00479             EntityHandle bridge_verts[MAX_SUB_ENTITIES];
00480             int bridge_indices[MAX_SUB_ENTITIES];
00481             for( int i = 0; i < CN::NumSubEntities( from_type, bridge_dim ); i++ )
00482             {
00483 
00484                 // get the vertices making up this sub-entity
00485                 int num_bridge_verts = CN::VerticesPerEntity( CN::SubEntityType( from_type, bridge_dim, i ) );
00486                 assert( num_bridge_verts >= 0 && num_bridge_verts <= MAX_SUB_ENTITIES );
00487                 CN::SubEntityVertexIndices( from_type, bridge_dim, i, bridge_indices );
00488                 for( int j = 0; j < num_bridge_verts; ++j )
00489                 {
00490                     if( bridge_indices[j] >= 0 && bridge_indices[j] < num_connect )
00491                         bridge_verts[j] = connect[bridge_indices[j]];
00492                     else
00493                         bridge_verts[j] = 0;
00494                 }
00495                 // CN::SubEntityConn(connect, from_type, bridge_dim, i, &bridge_verts[0],
00496                 // num_bridge_verts);
00497 
00498                 // get the to_dim entities adjacent
00499                 to_ents.clear();
00500                 ErrorCode tmp_result = mbImpl->get_adjacencies( bridge_verts, num_bridge_verts, to_dim, false, to_ents,
00501                                                                 Interface::INTERSECT );
00502                 if( MB_SUCCESS != tmp_result ) result = tmp_result;
00503 
00504                 to_adjs.merge( to_ents );
00505             }
00506         }
00507     }
00508 
00509     // now get the direct ones too, or only in the case where we're
00510     // going to higher dimension for bridge
00511     Range bridge_ents, tmp_ents;
00512     tmp_ents.insert( from_entity );
00513     ErrorCode tmp_result = mbImpl->get_adjacencies( tmp_ents, bridge_dim, false, bridge_ents, Interface::UNION );
00514     if( MB_SUCCESS != tmp_result ) return tmp_result;
00515 
00516     tmp_result = mbImpl->get_adjacencies( bridge_ents, to_dim, false, to_adjs, Interface::UNION );
00517     if( MB_SUCCESS != tmp_result ) return tmp_result;
00518 
00519     // if to_dimension is same as that of from_entity, make sure from_entity isn't
00520     // in list
00521     if( to_dim == from_dim ) to_adjs.erase( from_entity );
00522 
00523     return result;
00524 }
00525 
00526 //! return a common entity of the specified dimension, or 0 if there isn't one
00527 EntityHandle MeshTopoUtil::common_entity( const EntityHandle ent1, const EntityHandle ent2, const int dim )
00528 {
00529     Range tmp_range, tmp_range2;
00530     tmp_range.insert( ent1 );
00531     tmp_range.insert( ent2 );
00532     ErrorCode result = mbImpl->get_adjacencies( tmp_range, dim, false, tmp_range2 );
00533     if( MB_SUCCESS != result || tmp_range2.empty() )
00534         return 0;
00535     else
00536         return *tmp_range2.begin();
00537 }
00538 
00539 //! return the opposite side entity given a parent and bounding entity.
00540 //! This function is only defined for certain types of parent/child types;
00541 //! See CN.hpp::OppositeSide for details.
00542 //!
00543 //! \param parent The parent element
00544 //! \param child The child element
00545 //! \param opposite_element The index of the opposite element
00546 ErrorCode MeshTopoUtil::opposite_entity( const EntityHandle parent, const EntityHandle child,
00547                                          EntityHandle& opposite_element )
00548 {
00549     // get the side no.
00550     int side_no, offset, sense;
00551     ErrorCode result = mbImpl->side_number( parent, child, side_no, offset, sense );
00552     if( MB_SUCCESS != result ) return result;
00553 
00554     // get the child index from CN
00555     int opposite_index, opposite_dim;
00556     int status = CN::OppositeSide( mbImpl->type_from_handle( parent ), side_no, mbImpl->dimension_from_handle( child ),
00557                                    opposite_index, opposite_dim );
00558     if( 0 != status ) return MB_FAILURE;
00559 
00560     // now get the side element from MOAB
00561     result = mbImpl->side_element( parent, opposite_dim, opposite_index, opposite_element );
00562     if( MB_SUCCESS != result ) return result;
00563 
00564     return MB_SUCCESS;
00565 }
00566 
00567 ErrorCode MeshTopoUtil::split_entities_manifold( Range& entities, Range& new_entities, Range* fill_entities )
00568 {
00569     Range tmp_range, *tmp_ptr_fill_entity;
00570     if( NULL != fill_entities )
00571         tmp_ptr_fill_entity = &tmp_range;
00572     else
00573         tmp_ptr_fill_entity = NULL;
00574 
00575     for( Range::iterator rit = entities.begin(); rit != entities.end(); ++rit )
00576     {
00577         EntityHandle new_entity;
00578         if( NULL != tmp_ptr_fill_entity ) tmp_ptr_fill_entity->clear();
00579 
00580         EntityHandle this_ent = *rit;
00581         ErrorCode result      = split_entities_manifold( &this_ent, 1, &new_entity, tmp_ptr_fill_entity );
00582         if( MB_SUCCESS != result ) return result;
00583 
00584         new_entities.insert( new_entity );
00585         if( NULL != fill_entities ) fill_entities->merge( *tmp_ptr_fill_entity );
00586     }
00587 
00588     return MB_SUCCESS;
00589 }
00590 
00591 ErrorCode MeshTopoUtil::split_entities_manifold( EntityHandle* entities, const int num_entities,
00592                                                  EntityHandle* new_entities, Range* fill_entities,
00593                                                  EntityHandle* gowith_ents )
00594 {
00595     // split entities by duplicating them; splitting manifold means that there is at
00596     // most two higher-dimension entities bounded by a given entity; after split, the
00597     // new entity bounds one and the original entity bounds the other
00598 
00599 #define ITERATE_RANGE( range, it ) for( Range::iterator it = range.begin(); it != range.end(); ++it )
00600 #define GET_CONNECT_DECL( ent, connect, num_connect )                                     \
00601     const EntityHandle* connect = NULL;                                                   \
00602     int num_connect             = 0;                                                      \
00603     {                                                                                     \
00604         ErrorCode connect_result = mbImpl->get_connectivity( ent, connect, num_connect ); \
00605         if( MB_SUCCESS != connect_result ) return connect_result;                         \
00606     }
00607 #define GET_CONNECT( ent, connect, num_connect )                                          \
00608     {                                                                                     \
00609         ErrorCode connect_result = mbImpl->get_connectivity( ent, connect, num_connect ); \
00610         if( MB_SUCCESS != connect_result ) return connect_result;                         \
00611     }
00612 #define TC                         \
00613     if( MB_SUCCESS != tmp_result ) \
00614     {                              \
00615         result = tmp_result;       \
00616         continue;                  \
00617     }
00618 
00619     ErrorCode result = MB_SUCCESS;
00620     for( int i = 0; i < num_entities; i++ )
00621     {
00622         ErrorCode tmp_result;
00623 
00624         // get original higher-dimensional bounding entities
00625         Range up_adjs[4];
00626         // can only do a split_manifold if there are at most 2 entities of each
00627         // higher dimension; otherwise it's a split non-manifold
00628         bool valid_up_adjs = true;
00629         for( int dim = 1; dim <= 3; dim++ )
00630         {
00631             tmp_result = mbImpl->get_adjacencies( entities + i, 1, dim, false, up_adjs[dim] );
00632             TC;
00633             if( dim > CN::Dimension( TYPE_FROM_HANDLE( entities[i] ) ) && up_adjs[dim].size() > 2 )
00634             {
00635                 valid_up_adjs = false;
00636                 break;
00637             }
00638         }
00639         if( !valid_up_adjs ) return MB_FAILURE;
00640 
00641         // ok to split; create the new entity, with connectivity of the original
00642         GET_CONNECT_DECL( entities[i], connect, num_connect );
00643         EntityHandle new_entity;
00644         result = mbImpl->create_element( mbImpl->type_from_handle( entities[i] ), connect, num_connect, new_entity );
00645         TC;
00646 
00647         // by definition, new entity and original will be equivalent; need to add explicit
00648         // adjs to distinguish them; don't need to check if there's already one there,
00649         // 'cuz add_adjacency does that for us
00650         for( int dim = 1; dim <= 3; dim++ )
00651         {
00652             if( up_adjs[dim].empty() || dim == CN::Dimension( TYPE_FROM_HANDLE( entities[i] ) ) ) continue;
00653 
00654             if( dim < CN::Dimension( TYPE_FROM_HANDLE( entities[i] ) ) )
00655             {
00656                 // adjacencies from other entities to this one; if any of those are equivalent
00657                 // entities, need to make explicit adjacency to new entity too
00658                 for( Range::iterator rit = up_adjs[dim].begin(); rit != up_adjs[dim].end(); ++rit )
00659                 {
00660                     if( equivalent_entities( *rit ) ) result = mbImpl->add_adjacencies( *rit, &new_entity, 1, false );
00661                 }
00662             }
00663             else
00664             {
00665 
00666                 // get the two up-elements
00667                 EntityHandle up_elem1 = *( up_adjs[dim].begin() ),
00668                              up_elem2 = ( up_adjs[dim].size() > 1 ? *( up_adjs[dim].rbegin() ) : 0 );
00669 
00670                 // if two, and a gowith entity was input, make sure the new entity goes with
00671                 // that one
00672                 if( gowith_ents && up_elem2 && gowith_ents[i] != up_elem1 && gowith_ents[i] == up_elem2 )
00673                 {
00674                     EntityHandle tmp_elem = up_elem1;
00675                     up_elem1              = up_elem2;
00676                     up_elem2              = tmp_elem;
00677                 }
00678 
00679                 mbImpl->remove_adjacencies( entities[i], &up_elem1, 1 );
00680                 // (ok if there's an error, that just means there wasn't an explicit adj)
00681 
00682                 tmp_result = mbImpl->add_adjacencies( new_entity, &up_elem1, 1, false );
00683                 TC;
00684                 if( !up_elem2 ) continue;
00685 
00686                 // add adj to other up_adj
00687                 tmp_result = mbImpl->add_adjacencies( entities[i], &up_elem2, 1, false );
00688                 TC;
00689             }
00690         }
00691 
00692         // if we're asked to build a next-higher-dimension object, do so
00693         EntityHandle fill_entity = 0;
00694         EntityHandle tmp_ents[2];
00695         if( NULL != fill_entities )
00696         {
00697             // how to do this depends on dimension
00698             switch( CN::Dimension( TYPE_FROM_HANDLE( entities[i] ) ) )
00699             {
00700                 case 0:
00701                     tmp_ents[0] = entities[i];
00702                     tmp_ents[1] = new_entity;
00703                     tmp_result  = mbImpl->create_element( MBEDGE, tmp_ents, 2, fill_entity );
00704                     TC;
00705                     break;
00706                 case 1:
00707                     tmp_result = mbImpl->create_element( MBPOLYGON, connect, 2, fill_entity );
00708                     TC;
00709                     // need to create explicit adj in this case
00710                     tmp_result = mbImpl->add_adjacencies( entities[i], &fill_entity, 1, false );
00711                     TC;
00712                     tmp_result = mbImpl->add_adjacencies( new_entity, &fill_entity, 1, false );
00713                     TC;
00714                     break;
00715                 case 2:
00716                     tmp_ents[0] = entities[i];
00717                     tmp_ents[1] = new_entity;
00718                     tmp_result  = mbImpl->create_element( MBPOLYHEDRON, tmp_ents, 2, fill_entity );
00719                     TC;
00720                     break;
00721             }
00722             if( 0 == fill_entity )
00723             {
00724                 result = MB_FAILURE;
00725                 continue;
00726             }
00727             fill_entities->insert( fill_entity );
00728         }
00729 
00730         new_entities[i] = new_entity;
00731 
00732     }  // end for over input entities
00733 
00734     return result;
00735 }
00736 
00737 ErrorCode MeshTopoUtil::split_entity_nonmanifold( EntityHandle split_ent, Range& old_adjs, Range& new_adjs,
00738                                                   EntityHandle& new_entity )
00739 {
00740     // split an entity into two entities; new entity gets explicit adj to new_adjs,
00741     // old to old_adjs
00742 
00743     // make new entities and add adjacencies
00744     // create the new entity
00745     EntityType split_type = mbImpl->type_from_handle( split_ent );
00746 
00747     ErrorCode result;
00748     if( MBVERTEX == split_type )
00749     {
00750         double coords[3];
00751         result = mbImpl->get_coords( &split_ent, 1, coords );RR;
00752         result = mbImpl->create_vertex( coords, new_entity );RR;
00753     }
00754     else
00755     {
00756         const EntityHandle* connect;
00757         int num_connect;
00758         result = mbImpl->get_connectivity( split_ent, connect, num_connect );RR;
00759         result = mbImpl->create_element( split_type, connect, num_connect, new_entity );RR;
00760 
00761         // remove any explicit adjacencies between new_adjs and split entity
00762         for( Range::iterator rit = new_adjs.begin(); rit != new_adjs.end(); ++rit )
00763             mbImpl->remove_adjacencies( split_ent, &( *rit ), 1 );
00764     }
00765 
00766     if( MBVERTEX != split_type )
00767     {
00768         //  add adj's between new_adjs & new entity, old_adjs & split_entity
00769         for( Range::iterator rit = new_adjs.begin(); rit != new_adjs.end(); ++rit )
00770             mbImpl->add_adjacencies( new_entity, &( *rit ), 1, true );
00771         for( Range::iterator rit = old_adjs.begin(); rit != old_adjs.end(); ++rit )
00772             mbImpl->add_adjacencies( split_ent, &( *rit ), 1, true );
00773     }
00774     else if( split_ent != new_entity )
00775     {
00776         // in addition to explicit adjs, need to check if vertex is part of any
00777         // other entities, and check those entities against ents in old and new adjs
00778         Range other_adjs;
00779         for( int i = 1; i < 4; i++ )
00780         {
00781             result = mbImpl->get_adjacencies( &split_ent, 1, i, false, other_adjs, Interface::UNION );RR;
00782         }
00783         other_adjs = subtract( other_adjs, old_adjs );
00784         other_adjs = subtract( other_adjs, new_adjs );
00785         for( Range::iterator rit1 = other_adjs.begin(); rit1 != other_adjs.end(); ++rit1 )
00786         {
00787             // find an adjacent lower-dimensional entity in old_ or new_ adjs
00788             bool found = false;
00789             for( Range::iterator rit2 = old_adjs.begin(); rit2 != old_adjs.end(); ++rit2 )
00790             {
00791                 if( mbImpl->dimension_from_handle( *rit1 ) != mbImpl->dimension_from_handle( *rit2 ) &&
00792                     common_entity( *rit1, *rit2, mbImpl->dimension_from_handle( *rit1 ) ) )
00793                 {
00794                     found = true;
00795                     old_adjs.insert( *rit1 );
00796                     break;
00797                 }
00798             }
00799             if( found ) continue;
00800             for( Range::iterator rit2 = new_adjs.begin(); rit2 != new_adjs.end(); ++rit2 )
00801             {
00802                 if( mbImpl->dimension_from_handle( *rit1 ) != mbImpl->dimension_from_handle( *rit2 ) &&
00803                     common_entity( *rit1, *rit2, mbImpl->dimension_from_handle( *rit1 ) ) )
00804                 {
00805                     found = true;
00806                     new_adjs.insert( *rit1 );
00807                     break;
00808                 }
00809             }
00810             if( !found ) return MB_FAILURE;
00811         }
00812 
00813         // instead of adjs replace in connectivity
00814         std::vector< EntityHandle > connect;
00815         for( Range::iterator rit = new_adjs.begin(); rit != new_adjs.end(); ++rit )
00816         {
00817             connect.clear();
00818             result = mbImpl->get_connectivity( &( *rit ), 1, connect );RR;
00819             std::replace( connect.begin(), connect.end(), split_ent, new_entity );
00820             result = mbImpl->set_connectivity( *rit, &connect[0], connect.size() );RR;
00821         }
00822     }
00823 
00824     return result;
00825 
00826     /*
00827 
00828     Commented out for now, because I decided to do a different implementation
00829     for the sake of brevity.  However, I still think this function is the right
00830     way to do it, if I ever get the time.  Sigh.
00831 
00832         // split entity d, producing entity nd; generates various new entities,
00833         // see algorithm description in notes from 2/25/05
00834       const EntityHandle split_types = {MBEDGE, MBPOLYGON, MBPOLYHEDRON};
00835       ErrorCode result = MB_SUCCESS;
00836       const int dim = CN::Dimension(TYPE_FROM_HANDLE(d));
00837       MeshTopoUtil mtu(this);
00838 
00839         // get all (d+2)-, (d+1)-cells connected to d
00840       Range dp2s, dp1s, dp1s_manif, dp2s_manif;
00841       result = get_adjacencies(&d, 1, dim+2, false, dp2s); RR;
00842       result = get_adjacencies(&d, 1, dim+1, false, dp1s); RR;
00843 
00844         // also get (d+1)-cells connected to d which are manifold
00845       get_manifold_dp1s(d, dp1s_manif);
00846       get_manifold_dp2s(d, dp2s_manif);
00847 
00848         // make new cell nd, then ndp1
00849       result = copy_entity(d, nd); RR;
00850       EntityHandle tmp_connect[] = {d, nd};
00851       EntityHandle ndp1;
00852       result = create_element(split_types[dim],
00853                               tmp_connect, 2, ndp1); RR;
00854 
00855         // modify (d+2)-cells, depending on what type they are
00856       ITERATE_RANGE(dp2s, dp2) {
00857           // first, get number of connected manifold (d+1)-entities
00858         Range tmp_range, tmp_range2(dp1s_manif);
00859         tmp_range.insert(*dp2);
00860         tmp_range.insert(d);
00861         tmp_result = get_adjacencies(tmp_range, 1, false, tmp_range2); TC;
00862         EntityHandle ndp2;
00863 
00864           // a. manif (d+1)-cells is zero
00865         if (tmp_range2.empty()) {
00866             // construct new (d+1)-cell
00867           EntityHandle ndp1a;
00868           EntityHandle tmp_result = create_element(split_types[dim],
00869                                                      tmp_connect, 2, ndp1a); TC;
00870             // now make new (d+2)-cell
00871           EntityHandle tmp_connect2[] = {ndp1, ndp1a};
00872           tmp_result = create_element(split_types[dim+1],
00873                                       tmp_connect2, 2, ndp2); TC;
00874             // need to add explicit adjacencies, since by definition ndp1, ndp1a will be equivalent
00875           tmp_result = add_adjacencies(ndp1a, &dp2, 1, false); TC;
00876           tmp_result = add_adjacencies(ndp1a, &ndp2, 1, false); TC;
00877           tmp_result = add_adjacencies(ndp1, &ndp2, 1, false); TC;
00878 
00879             // now insert nd into connectivity of dp2, right after d if dim < 1
00880           std::vector<EntityHandle> connect;
00881           tmp_result = get_connectivity(&dp2, 1, connect); TC;
00882           if (dim < 1) {
00883             std::vector<EntityHandle>::iterator vit = std::find(connect.begin(), connect.end(), d);
00884             if (vit == connect.end()) {
00885               result = MB_FAILURE;
00886               continue;
00887             }
00888             connect.insert(vit, nd);
00889           }
00890           else
00891             connect.push_back(nd);
00892           tmp_result = set_connectivity(dp2, connect); TC;
00893 
00894             // if dim < 1, need to add explicit adj from ndp2 to higher-dim ents, since it'll
00895             // be equiv to other dp2 entities
00896           if (dim < 1) {
00897             Range tmp_dp3s;
00898             tmp_result = get_adjacencies(&dp2, 1, dim+3, false, tmp_dp3s); TC;
00899             tmp_result = add_adjacencies(ndp2, tmp_dp3s, false); TC;
00900           }
00901         } // end if (tmp_range2.empty())
00902 
00903           // b. single manifold (d+1)-cell, which isn't adjacent to manifold (d+2)-cell
00904         else if (tmp_range2.size() == 1) {
00905             // b1. check validity, and skip if not valid
00906 
00907             // only change if not dp1-adjacent to manifold dp2cell; check that...
00908           Range tmp_adjs(dp2s_manif);
00909           tmp_result = get_adjacencies(&(*tmp_range2.begin()), 1, dim+2, false, tmp_adjs); TC;
00910           if (!tmp_adjs.empty()) continue;
00911 
00912           EntityHandle dp1 = *tmp_range2.begin();
00913 
00914             // b2. make new (d+1)- and (d+2)-cell next to dp2
00915 
00916             // get the (d+2)-cell on the other side of dp1
00917           tmp_result = get_adjacencies(&dp1, 1, dim+2, false, tmp_adjs); TC;
00918           EntityHandle odp2 = *tmp_adjs.begin();
00919           if (odp2 == dp2) odp2 = *tmp_adjs.rbegin();
00920 
00921             // get od, the d-cell on dp1_manif which isn't d
00922           tmp_result = get_adjacencies(&dp1_manif, 1, dim, false, tmp_adjs); TC;
00923           tmp_adjs.erase(d);
00924           if (tmp_adjs.size() != 1) {
00925             result = MB_FAILURE;
00926             continue;
00927           }
00928           EntityHandle od = *tmp_adjs.begin();
00929 
00930             // make a new (d+1)-cell from od and nd
00931           tmp_adjs.insert(nd);
00932           tmp_result = create_element(split_types[1], tmp_adjs, ndp1a); TC;
00933 
00934             // construct new (d+2)-cell from dp1, ndp1, ndp1a
00935           tmp_adjs.clear();
00936           tmp_adjs.insert(dp1); tmp_adjs.insert(ndp1); tmp_adjs.insert(ndp1a);
00937           tmp_result = create_element(split_types[2], tmp_adjs, ndp2); TC;
00938 
00939             // b3. replace d, dp1 in connect/adjs of odp2
00940           std::vector<EntityHandle> connect;
00941           tmp_result = get_connectivity(&odp2, 1, connect); TC;
00942           if (dim == 0) {
00943             *(std::find(connect.begin(), connect.end(), d)) = nd;
00944             remove_adjacency(dp1, odp2);
00945 
00946 
00947 
00948             // if dp1 was explicitly adj to odp2, remove it
00949           remove_adjacency
00950 
00951     ...
00952 
00953     */
00954 }
00955 
00956 //! return whether entity is equivalent to any other of same type and same vertices;
00957 //! if equivalent entity is found, it's returned in equiv_ents and return value is true,
00958 //! false otherwise.
00959 bool MeshTopoUtil::equivalent_entities( const EntityHandle entity, Range* equiv_ents )
00960 {
00961     const EntityHandle* connect = NULL;
00962     int num_connect             = 0;
00963     ErrorCode result            = mbImpl->get_connectivity( entity, connect, num_connect );
00964     if( MB_SUCCESS != result ) return false;
00965 
00966     Range dum;
00967     result = mbImpl->get_adjacencies( connect, num_connect, mbImpl->dimension_from_handle( entity ), false, dum );
00968     dum.erase( entity );
00969 
00970     if( NULL != equiv_ents ) { equiv_ents->swap( dum ); }
00971 
00972     if( !dum.empty() )
00973         return true;
00974     else
00975         return false;
00976 }
00977 
00978 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines