+ //================================================================================
+ /*!
+ * \brief Remove edges and nodes dividing a hexa side in the case if an adjacent
+ * volume also sharing the dividing edge is missing due to its small side.
+ * Issue #19887.
+ */
+ //================================================================================
+
+ void Hexahedron::removeExcessSideDivision(const vector< Hexahedron* >& allHexa)
+ {
+ if ( !_grid->IsToRemoveExcessEntities() || _volumeDefs.IsEmpty() )
+ return;
+ if (( _volumeDefs._quantities.empty() ) &&
+ ( !_volumeDefs._next || _volumeDefs._next->_quantities.empty() ))
+ return; // not a polyhedron
+
+ // look for a divided side adjacent to a small hexahedron
+
+ int di[6] = { 0, 0, 0, 0,-1, 1 };
+ int dj[6] = { 0, 0,-1, 1, 0, 0 };
+ int dk[6] = {-1, 1, 0, 0, 0, 0 };
+
+ for ( int iF = 0; iF < 6; ++iF ) // loop on 6 sides of a hexahedron
+ {
+ size_t neighborIndex = _grid->CellIndex( _i + di[iF],
+ _j + dj[iF],
+ _k + dk[iF] );
+ if ( neighborIndex >= allHexa.size() ||
+ !allHexa[ neighborIndex ] ||
+ !allHexa[ neighborIndex ]->_hasTooSmall )
+ continue;
+
+ // check if a side is divided into several polygons
+ for ( _volumeDef* volDef = &_volumeDefs; volDef; volDef = volDef->_next )
+ {
+ int nbPolygons = 0, nbNodes = 0;
+ for ( size_t i = 0; i < volDef->_names.size(); ++i )
+ if ( volDef->_names[ i ] == _hexQuads[ iF ]._name )
+ {
+ ++nbPolygons;
+ nbNodes += volDef->_quantities[ i ];
+ }
+ if ( nbPolygons < 2 )
+ continue;
+
+ // construct loops from polygons
+ typedef _volumeDef::_linkDef TLinkDef;
+ std::vector< TLinkDef* > loops;
+ std::vector< TLinkDef > links( nbNodes );
+ for ( size_t i = 0, iN = 0, iLoop = 0; iLoop < volDef->_quantities.size(); ++iLoop )
+ {
+ size_t nbLinks = volDef->_quantities[ iLoop ];
+ if ( volDef->_names[ iLoop ] != _hexQuads[ iF ]._name )
+ {
+ iN += nbLinks;
+ continue;
+ }
+ loops.push_back( & links[i] );
+ for ( size_t n = 0; n < nbLinks-1; ++n, ++i, ++iN )
+ {
+ links[i].init( volDef->_nodes[iN], volDef->_nodes[iN+1], iLoop );
+ links[i].setNext( &links[i+1] );
+ }
+ links[i].init( volDef->_nodes[iN], volDef->_nodes[iN-nbLinks+1], iLoop );
+ links[i].setNext( &links[i-nbLinks+1] );
+ ++i; ++iN;
+ }
+
+ // look for equal links in different loops and join such loops
+ bool loopsJoined = false;
+ std::set< TLinkDef > linkSet;
+ for ( size_t iLoop = 0; iLoop < loops.size(); ++iLoop )
+ {
+ bool joined = false;
+ TLinkDef* beg = 0;
+ for ( TLinkDef* l = loops[ iLoop ]; l != beg; l = l->_next ) // walk around the iLoop
+ {
+ std::pair< std::set< TLinkDef >::iterator, bool > it2new = linkSet.insert( *l );
+ if ( !it2new.second ) // equal found, join loops
+ {
+ const TLinkDef* equal = &(*it2new.first);
+ if ( equal->_loopIndex == l->_loopIndex )
+ continue; // error?
+
+ // exclude l and equal and join two loops
+ if ( l->_prev != equal )
+ l->_prev->setNext( equal->_next );
+ if ( equal->_prev != l )
+ equal->_prev->setNext( l->_next );
+
+ joined = true;
+ if ( volDef->_quantities[ l->_loopIndex ] > 0 )
+ volDef->_quantities[ l->_loopIndex ] *= -1;
+ if ( volDef->_quantities[ equal->_loopIndex ] > 0 )
+ volDef->_quantities[ equal->_loopIndex ] *= -1;
+
+ if ( loops[ iLoop ] == l )
+ loops[ iLoop ] = l->_prev->_next;
+ }
+ beg = loops[ iLoop ];
+ }
+ if ( joined )
+ {
+ loops[ iLoop ] = 0;
+ loopsJoined = true;
+ }
+ }
+ // update volDef
+ if ( loopsJoined )
+ {
+ // set unchanged polygons
+ std::vector< int > newQuantities; newQuantities.reserve( volDef->_quantities.size() );
+ std::vector< _volumeDef::_nodeDef > newNodes; newNodes.reserve( volDef->_nodes.size() );
+ vector< SMESH_Block::TShapeID > newNames; newNames.reserve( volDef->_names.size() );
+ for ( size_t i = 0, iLoop = 0; iLoop < volDef->_quantities.size(); ++iLoop )
+ {
+ if ( volDef->_quantities[ iLoop ] < 0 )
+ {
+ i -= volDef->_quantities[ iLoop ];
+ continue;
+ }
+ newQuantities.push_back( volDef->_quantities[ iLoop ]);
+ newNodes.insert( newNodes.end(),
+ volDef->_nodes.begin() + i,
+ volDef->_nodes.begin() + i + newQuantities.back() );
+ newNames.push_back( volDef->_names[ iLoop ]);
+ i += volDef->_quantities[ iLoop ];
+ }
+
+ // set joined loops
+ for ( size_t iLoop = 0; iLoop < loops.size(); ++iLoop )
+ {
+ if ( !loops[ iLoop ] )
+ continue;
+ newQuantities.push_back( 0 );
+ TLinkDef* beg = 0;
+ for ( TLinkDef* l = loops[ iLoop ]; l != beg; l = l->_next, ++newQuantities.back() )
+ {
+ newNodes.push_back( l->_node1 );
+ beg = loops[ iLoop ];
+ }
+ newNames.push_back( _hexQuads[ iF ]._name );
+ }
+ volDef->_quantities.swap( newQuantities );
+ volDef->_nodes.swap( newNodes );
+ volDef->_names.swap( newNames );
+ }
+ } // loop on volDef's
+ } // loop on hex sides
+
+ return;
+ } // removeExcessSideDivision()
+