+
+ //================================================================================
+ /*!
+ * \brief Remove nodes splitting Cartesian cell edges in the case if a node
+ * is used in every cells only by two polygons sharing the edge
+ * Issue #19887.
+ */
+ //================================================================================
+
+ void Hexahedron::removeExcessNodes(vector< Hexahedron* >& allHexa)
+ {
+ if ( ! _volumeDefs.IsPolyhedron() )
+ return; // not a polyhedron
+
+ typedef vector< _volumeDef::_nodeDef >::iterator TNodeIt;
+ vector< int > nodesInPoly[ 4 ]; // node index in _volumeDefs._nodes
+ vector< int > volDefInd [ 4 ]; // index of a _volumeDefs
+ Hexahedron* hexa [ 4 ];
+ int i,j,k, cellIndex, iLink = 0, iCellLink;
+ for ( int iDir = 0; iDir < 3; ++iDir )
+ {
+ CellsAroundLink fourCells( _grid, iDir );
+ for ( int iL = 0; iL < 4; ++iL, ++iLink ) // 4 links in a direction
+ {
+ _Link& link = _hexLinks[ iLink ];
+ fourCells.Init( _i, _j, _k, iLink );
+
+ for ( size_t iP = 0; iP < link._fIntPoints.size(); ++iP ) // loop on nodes on the link
+ {
+ bool nodeRemoved = true;
+ _volumeDef::_nodeDef node; node._intPoint = link._fIntPoints[iP];
+
+ for ( size_t i = 0, nb = _volumeDefs.size(); i < nb && nodeRemoved; ++i )
+ if ( _volumeDef* vol = _volumeDefs.at( i ))
+ nodeRemoved =
+ ( std::find( vol->_nodes.begin(), vol->_nodes.end(), node ) == vol->_nodes.end() );
+ if ( nodeRemoved )
+ continue; // node already removed
+
+ // check if a node encounters zero or two times in 4 cells sharing iLink
+ // if so, the node can be removed from the cells
+ bool nodeIsOnEdge = true;
+ int nbPolyhedraWithNode = 0;
+ for ( int iC = 0; iC < 4; ++iC ) // loop on 4 cells sharing a link
+ {
+ nodesInPoly[ iC ].clear();
+ volDefInd [ iC ].clear();
+ hexa [ iC ] = 0;
+ if ( !fourCells.GetCell( iC, i,j,k, cellIndex, iCellLink ))
+ continue;
+ hexa[ iC ] = allHexa[ cellIndex ];
+ if ( !hexa[ iC ])
+ continue;
+ for ( size_t i = 0, nb = hexa[ iC ]->_volumeDefs.size(); i < nb; ++i )
+ if ( _volumeDef* vol = hexa[ iC ]->_volumeDefs.at( i ))
+ {
+ for ( TNodeIt nIt = vol->_nodes.begin(); nIt != vol->_nodes.end(); ++nIt )
+ {
+ nIt = std::find( nIt, vol->_nodes.end(), node );
+ if ( nIt != vol->_nodes.end() )
+ {
+ nodesInPoly[ iC ].push_back( std::distance( vol->_nodes.begin(), nIt ));
+ volDefInd [ iC ].push_back( i );
+ }
+ else
+ break;
+ }
+ nbPolyhedraWithNode += ( !nodesInPoly[ iC ].empty() );
+ }
+ if ( nodesInPoly[ iC ].size() != 0 &&
+ nodesInPoly[ iC ].size() != 2 )
+ {
+ nodeIsOnEdge = false;
+ break;
+ }
+ } // loop on 4 cells
+
+ // remove nodes from polyhedra
+ if ( nbPolyhedraWithNode > 0 && nodeIsOnEdge )
+ {
+ for ( int iC = 0; iC < 4; ++iC ) // loop on 4 cells sharing the link
+ {
+ if ( nodesInPoly[ iC ].empty() )
+ continue;
+ for ( int i = volDefInd[ iC ].size() - 1; i >= 0; --i )
+ {
+ _volumeDef* vol = hexa[ iC ]->_volumeDefs.at( volDefInd[ iC ][ i ]);
+ int nIndex = nodesInPoly[ iC ][ i ];
+ // decrement _quantities
+ for ( size_t iQ = 0; iQ < vol->_quantities.size(); ++iQ )
+ if ( nIndex < vol->_quantities[ iQ ])
+ {
+ vol->_quantities[ iQ ]--;
+ break;
+ }
+ else
+ {
+ nIndex -= vol->_quantities[ iQ ];
+ }
+ vol->_nodes.erase( vol->_nodes.begin() + nodesInPoly[ iC ][ i ]);
+
+ if ( i == 0 &&
+ vol->_nodes.size() == 6 * 4 &&
+ vol->_quantities.size() == 6 ) // polyhedron becomes hexahedron?
+ {
+ bool allQuads = true;
+ for ( size_t iQ = 0; iQ < vol->_quantities.size() && allQuads; ++iQ )
+ allQuads = ( vol->_quantities[ iQ ] == 4 );
+ if ( allQuads )
+ {
+ // set side nodes as this: bottom, top, top, ...
+ int iTop, iBot; // side indices
+ for ( int iS = 0; iS < 6; ++iS )
+ {
+ if ( vol->_names[ iS ] == SMESH_Block::ID_Fxy0 )
+ iBot = iS;
+ if ( vol->_names[ iS ] == SMESH_Block::ID_Fxy1 )
+ iTop = iS;
+ }
+ if ( iBot != 0 )
+ {
+ if ( iTop == 0 )
+ {
+ std::copy( vol->_nodes.begin(),
+ vol->_nodes.begin() + 4,
+ vol->_nodes.begin() + 4 );
+ iTop = 1;
+ }
+ std::copy( vol->_nodes.begin() + 4 * iBot,
+ vol->_nodes.begin() + 4 * ( iBot + 1),
+ vol->_nodes.begin() );
+ }
+ if ( iTop != 1 )
+ std::copy( vol->_nodes.begin() + 4 * iTop,
+ vol->_nodes.begin() + 4 * ( iTop + 1),
+ vol->_nodes.begin() + 4 );
+
+ std::copy( vol->_nodes.begin() + 4,
+ vol->_nodes.begin() + 8,
+ vol->_nodes.begin() + 8 );
+ // set up top facet nodes by comparing their uvw with bottom nodes
+ E_IntersectPoint ip[8];
+ for ( int iN = 0; iN < 8; ++iN )
+ {
+ SMESH_NodeXYZ p = vol->_nodes[ iN ].Node();
+ _grid->ComputeUVW( p, ip[ iN ]._uvw );
+ }
+ const double tol2 = _grid->_tol * _grid->_tol;
+ for ( int iN = 0; iN < 4; ++iN )
+ {
+ gp_Pnt2d pBot( ip[ iN ]._uvw[0], ip[ iN ]._uvw[1] );
+ for ( int iT = 4; iT < 8; ++iT )
+ {
+ gp_Pnt2d pTop( ip[ iT ]._uvw[0], ip[ iT ]._uvw[1] );
+ if ( pBot.SquareDistance( pTop ) < tol2 )
+ {
+ // vol->_nodes[ iN + 4 ]._node = ip[ iT ]._node;
+ // vol->_nodes[ iN + 4 ]._intPoint = 0;
+ vol->_nodes[ iN + 4 ] = vol->_nodes[ iT + 4 ];
+ break;
+ }
+ }
+ }
+ vol->_nodes.resize( 8 );
+ vol->_quantities.clear();
+ //vol->_names.clear();
+ }
+ }
+ } // loop on _volumeDefs
+ } // loop on 4 cell abound a link
+ } // if ( nodeIsOnEdge )
+ } // loop on intersection points of a link
+ } // loop on 4 links of a direction
+ } // loop on 3 directions
+
+ return;
+
+ } // removeExcessNodes()
+