+ for ( size_t iN = 3; iN < face.myNodes.size(); iN += 3 )
+ {
+ newVolumeDef.myPolyhedQuantities.push_back( 3 );
+ newVolumeDef.myNodes.insert( newVolumeDef.myNodes.end(),
+ face.myNodes.begin() + iN,
+ face.myNodes.begin() + iN + 3 );
+ newTriangle = meshDS->AddFace( face.myNodes[iN], face.myNodes[iN+1], face.myNodes[iN+2] );
+ meshDS->SetMeshElementOnShape( newTriangle, faceID );
+ }
+
+ meshDS->RemoveFreeElement( volume.Element(), 0, false );
+ SMDS_MeshElement* newVolume = editor.AddElement( newVolumeDef.myNodes, newVolumeDef );
+ meshDS->SetMeshElementOnShape( newVolume, solidID );
+
+ if ( reinitVolume )
+ {
+ volume.Set( 0 );
+ volume.Set( newVolume );
+ }
+ return;
+ }
+ //================================================================================
+ /*!
+ * \brief Create mesh faces at free facets
+ */
+ void Hexahedron::addFaces( SMESH_MesherHelper& helper,
+ const vector< const SMDS_MeshElement* > & boundaryVolumes )
+ {
+ if ( !_grid->_toCreateFaces )
+ return;
+
+ SMDS_VolumeTool vTool;
+ vector<int> bndFacets;
+ SMESH_MeshEditor editor( helper.GetMesh() );
+ SMESH_MeshEditor::ElemFeatures face( SMDSAbs_Face );
+ SMESHDS_Mesh* meshDS = helper.GetMeshDS();
+
+ // check if there are internal or shared FACEs
+ bool hasInternal = ( !_grid->_geometry.IsOneSolid() ||
+ _grid->_geometry._soleSolid.HasInternalFaces() );
+
+ for ( size_t iV = 0; iV < boundaryVolumes.size(); ++iV )
+ {
+ if ( !vTool.Set( boundaryVolumes[ iV ]))
+ continue;
+
+ TGeomID solidID = vTool.Element()->GetShapeID();
+ Solid * solid = _grid->GetOneOfSolids( solidID );
+
+ // find boundary facets
+
+ bndFacets.clear();
+ for ( int iF = 0, n = vTool.NbFaces(); iF < n; iF++ )
+ {
+ bool isBoundary = vTool.IsFreeFace( iF );
+ if ( isBoundary )
+ {
+ bndFacets.push_back( iF );
+ }
+ else if ( hasInternal )
+ {
+ // check if all nodes are on internal/shared FACEs
+ isBoundary = true;
+ const SMDS_MeshNode** nn = vTool.GetFaceNodes( iF );
+ const size_t nbFaceNodes = vTool.NbFaceNodes ( iF );
+ for ( size_t iN = 0; iN < nbFaceNodes && isBoundary; ++iN )
+ isBoundary = ( nn[ iN ]->GetShapeID() != solidID );
+ if ( isBoundary )
+ bndFacets.push_back( -( iF+1 )); // !!! minus ==> to check the FACE
+ }
+ }
+ if ( bndFacets.empty() )
+ continue;
+
+ // create faces
+
+ if ( !vTool.IsPoly() )
+ vTool.SetExternalNormal();
+ for ( size_t i = 0; i < bndFacets.size(); ++i ) // loop on boundary facets
+ {
+ const bool isBoundary = ( bndFacets[i] >= 0 );
+ const int iFacet = isBoundary ? bndFacets[i] : -bndFacets[i]-1;
+ const SMDS_MeshNode** nn = vTool.GetFaceNodes( iFacet );
+ const size_t nbFaceNodes = vTool.NbFaceNodes ( iFacet );
+ face.myNodes.assign( nn, nn + nbFaceNodes );
+
+ TGeomID faceID = 0;
+ const SMDS_MeshElement* existFace = 0, *newFace = 0;
+
+ if (( existFace = meshDS->FindElement( face.myNodes, SMDSAbs_Face )))
+ {
+ if ( existFace->isMarked() )
+ continue; // created by this method
+ faceID = existFace->GetShapeID();
+ }
+ else
+ {
+ // look for a supporting FACE
+ for ( size_t iN = 0; iN < nbFaceNodes && !faceID; ++iN ) // look for a node on FACE
+ {
+ if ( nn[ iN ]->GetPosition()->GetDim() == 2 )
+ faceID = nn[ iN ]->GetShapeID();
+ }
+ for ( size_t iN = 0; iN < nbFaceNodes && !faceID; ++iN )
+ {
+ // look for a father FACE of EDGEs and VERTEXes
+ const TopoDS_Shape& s1 = _grid->Shape( nn[ iN ]->GetShapeID() );
+ const TopoDS_Shape& s2 = _grid->Shape( nn[ iN+1 ]->GetShapeID() );
+ if ( s1 != s2 && s1.ShapeType() == TopAbs_EDGE && s2.ShapeType() == TopAbs_EDGE )
+ {
+ TopoDS_Shape f = helper.GetCommonAncestor( s1, s2, *helper.GetMesh(), TopAbs_FACE );
+ if ( !f.IsNull() )
+ faceID = _grid->ShapeID( f );
+ }
+ }
+
+ bool toCheckFace = faceID && (( !isBoundary ) ||
+ ( hasInternal && _grid->_toUseThresholdForInternalFaces ));
+ if ( toCheckFace ) // check if all nodes are on the found FACE
+ {
+ SMESH_subMesh* faceSM = helper.GetMesh()->GetSubMeshContaining( faceID );
+ for ( size_t iN = 0; iN < nbFaceNodes && faceID; ++iN )
+ {
+ TGeomID subID = nn[ iN ]->GetShapeID();
+ if ( subID != faceID && !faceSM->DependsOn( subID ))
+ faceID = 0;
+ }
+ if ( !faceID && !isBoundary )
+ continue;
+ }
+ }
+ // orient a new face according to supporting FACE orientation in shape_to_mesh
+ if ( !solid->IsOutsideOriented( faceID ))
+ {
+ if ( existFace )
+ editor.Reorient( existFace );
+ else
+ std::reverse( face.myNodes.begin(), face.myNodes.end() );
+ }
+
+ if ( ! ( newFace = existFace ))
+ {
+ face.SetPoly( nbFaceNodes > 4 );
+ newFace = editor.AddElement( face.myNodes, face );
+ if ( !newFace )
+ continue;
+ newFace->setIsMarked( true ); // to distinguish from face created in getBoundaryElems()
+ }
+
+ if ( faceID && _grid->IsBoundaryFace( faceID )) // face is not shared
+ {
+ // set newFace to the found FACE provided that it fully lies on the FACE
+ for ( size_t iN = 0; iN < nbFaceNodes && faceID; ++iN )
+ if ( nn[iN]->GetShapeID() == solidID )
+ {
+ if ( existFace )
+ meshDS->UnSetMeshElementOnShape( existFace, _grid->Shape( faceID ));
+ faceID = 0;
+ }
+ }
+
+ // split a polygon that will be used by other 3D algorithm
+ if ( faceID && nbFaceNodes > 4 &&
+ !_grid->IsInternal( faceID ) &&
+ !_grid->IsShared( faceID ) &&
+ !_grid->IsBoundaryFace( faceID ))
+ {
+ splitPolygon( newFace, vTool, iFacet, faceID, solidID,
+ face, editor, i+1 < bndFacets.size() );
+ }
+ else
+ {
+ if ( faceID )
+ meshDS->SetMeshElementOnShape( newFace, faceID );
+ else
+ meshDS->SetMeshElementOnShape( newFace, solidID );
+ }
+ } // loop on bndFacets
+ } // loop on boundaryVolumes
+
+
+ // Orient coherently mesh faces on INTERNAL FACEs
+
+ if ( hasInternal )
+ {
+ TopExp_Explorer faceExp( _grid->_geometry._mainShape, TopAbs_FACE );
+ for ( ; faceExp.More(); faceExp.Next() )
+ {
+ if ( faceExp.Current().Orientation() != TopAbs_INTERNAL )
+ continue;
+
+ SMESHDS_SubMesh* sm = meshDS->MeshElements( faceExp.Current() );
+ if ( !sm ) continue;
+
+ TIDSortedElemSet facesToOrient;
+ for ( SMDS_ElemIteratorPtr fIt = sm->GetElements(); fIt->more(); )
+ facesToOrient.insert( facesToOrient.end(), fIt->next() );
+ if ( facesToOrient.size() < 2 )
+ continue;
+
+ gp_Dir direction(1,0,0);
+ const SMDS_MeshElement* anyFace = *facesToOrient.begin();
+ editor.Reorient2D( facesToOrient, direction, anyFace );
+ }
+ }
+ return;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Create mesh segments.
+ */
+ void Hexahedron::addSegments( SMESH_MesherHelper& helper,
+ const map< TGeomID, vector< TGeomID > >& edge2faceIDsMap )
+ {
+ SMESHDS_Mesh* mesh = helper.GetMeshDS();
+
+ std::vector<const SMDS_MeshNode*> nodes;
+ std::vector<const SMDS_MeshElement *> elems;
+ map< TGeomID, vector< TGeomID > >::const_iterator e2ff = edge2faceIDsMap.begin();
+ for ( ; e2ff != edge2faceIDsMap.end(); ++e2ff )
+ {
+ const TopoDS_Edge& edge = TopoDS::Edge( _grid->Shape( e2ff->first ));
+ const TopoDS_Face& face = TopoDS::Face( _grid->Shape( e2ff->second[0] ));
+ StdMeshers_FaceSide side( face, edge, helper.GetMesh(), /*isFwd=*/true, /*skipMed=*/true );
+ nodes = side.GetOrderedNodes();
+
+ elems.clear();
+ if ( nodes.size() == 2 )
+ // check that there is an element connecting two nodes
+ if ( !mesh->GetElementsByNodes( nodes, elems ))
+ continue;
+
+ for ( size_t i = 1; i < nodes.size(); i++ )
+ {
+ SMDS_MeshElement* segment = mesh->AddEdge( nodes[i-1], nodes[i] );
+ mesh->SetMeshElementOnShape( segment, e2ff->first );
+ }
+ }
+ return;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Return created volumes and volumes that can have free facet because of
+ * skipped small volume. Also create mesh faces on free facets
+ * of adjacent not-cut volumes if the result volume is too small.
+ */
+ void Hexahedron::getBoundaryElems( vector< const SMDS_MeshElement* > & boundaryElems )
+ {
+ if ( _hasTooSmall /*|| _volumeDefs.IsEmpty()*/ )
+ {
+ // create faces around a missing small volume
+ TGeomID faceID = 0;
+ SMESH_MeshEditor editor( _grid->_helper->GetMesh() );
+ SMESH_MeshEditor::ElemFeatures polygon( SMDSAbs_Face );
+ SMESHDS_Mesh* meshDS = _grid->_helper->GetMeshDS();
+ std::vector<const SMDS_MeshElement *> adjVolumes(2);
+ for ( size_t iF = 0; iF < _polygons.size(); ++iF )
+ {
+ const size_t nbLinks = _polygons[ iF ]._links.size();
+ if ( nbLinks != 4 ) continue;
+ polygon.myNodes.resize( nbLinks );
+ polygon.myNodes.back() = 0;
+ for ( size_t iL = 0, iN = nbLinks - 1; iL < nbLinks; ++iL, --iN )
+ if ( ! ( polygon.myNodes[iN] = _polygons[ iF ]._links[ iL ].FirstNode()->Node() ))
+ break;
+ if ( !polygon.myNodes.back() )
+ continue;
+
+ meshDS->GetElementsByNodes( polygon.myNodes, adjVolumes, SMDSAbs_Volume );
+ if ( adjVolumes.size() != 1 )
+ continue;
+ if ( !adjVolumes[0]->isMarked() )
+ {
+ boundaryElems.push_back( adjVolumes[0] );
+ adjVolumes[0]->setIsMarked( true );
+ }
+
+ bool sameShape = true;
+ TGeomID shapeID = polygon.myNodes[0]->GetShapeID();
+ for ( size_t i = 1; i < polygon.myNodes.size() && sameShape; ++i )
+ sameShape = ( shapeID == polygon.myNodes[i]->GetShapeID() );
+
+ if ( !sameShape || !_grid->IsSolid( shapeID ))
+ continue; // some of shapes must be FACE
+
+ if ( !faceID )
+ {
+ faceID = getAnyFace();
+ if ( !faceID )
+ break;
+ if ( _grid->IsInternal( faceID ) ||
+ _grid->IsShared( faceID ) //||
+ //_grid->IsBoundaryFace( faceID ) -- commented for #19887
+ )
+ break; // create only if a new face will be used by other 3D algo
+ }
+
+ Solid * solid = _grid->GetOneOfSolids( adjVolumes[0]->GetShapeID() );
+ if ( !solid->IsOutsideOriented( faceID ))
+ std::reverse( polygon.myNodes.begin(), polygon.myNodes.end() );
+
+ //polygon.SetPoly( polygon.myNodes.size() > 4 );
+ const SMDS_MeshElement* newFace = editor.AddElement( polygon.myNodes, polygon );
+ meshDS->SetMeshElementOnShape( newFace, faceID );
+ }
+ }
+
+ // return created volumes
+ for ( _volumeDef* volDef = &_volumeDefs; volDef; volDef = volDef->_next )
+ {
+ if ( volDef->_volume && !volDef->_volume->isMarked() )
+ {
+ volDef->_volume->setIsMarked( true );
+ boundaryElems.push_back( volDef->_volume );
+
+ if ( _grid->IsToCheckNodePos() ) // un-mark nodes marked in addVolumes()
+ for ( size_t iN = 0; iN < volDef->_nodes.size(); ++iN )
+ volDef->_nodes[iN].Node()->setIsMarked( false );
+ }
+ }
+ }
+
+ //================================================================================
+ /*!
+ * \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 ( ! _volumeDefs.IsPolyhedron() )
+ 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 )
+ {
+ 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?
+
+ loopsJoined = true;
+
+ for ( size_t i = iLoop - 1; i < loops.size(); --i )
+ if ( loops[ i ] && loops[ i ]->_loopIndex == equal->_loopIndex )
+ loops[ i ] = 0;
+
+ // 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 );
+
+ 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 ];
+ }
+ }
+ // update volDef
+ if ( loopsJoined )
+ {
+ // set unchanged polygons
+ std::vector< int > newQuantities;
+ std::vector< _volumeDef::_nodeDef > newNodes;
+ vector< SMESH_Block::TShapeID > newNames;
+ newQuantities.reserve( volDef->_quantities.size() );
+ newNodes.reserve ( volDef->_nodes.size() );
+ 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()
+
+
+ //================================================================================
+ /*!
+ * \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 = 0, iBot = 0; // 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()
+
+ //================================================================================
+ /*!
+ * \brief [Issue #19913] Modify _hexLinks._splits to prevent creating overlapping volumes
+ */
+ //================================================================================
+
+ void Hexahedron::preventVolumesOverlapping()
+ {
+ // Cut off a quadrangle corner if two links sharing the corner
+ // are shared by same two solids, in this case each of solids gets
+ // a triangle for it-self.
+ std::vector< TGeomID > soIDs[4];
+ for ( int iF = 0; iF < 6; ++iF ) // loop on 6 sides of a hexahedron
+ {
+ _Face& quad = _hexQuads[ iF ] ;
+
+ int iFOpposite = iF + ( iF % 2 ? -1 : 1 );
+ _Face& quadOpp = _hexQuads[ iFOpposite ] ;
+
+ int nbSides = 0, nbSidesOpp = 0;
+ for ( int iE = 0; iE < 4; ++iE ) // loop on 4 sides of a quadrangle
+ {
+ nbSides += ( quad._links [ iE ].NbResultLinks() > 0 );
+ nbSidesOpp += ( quadOpp._links[ iE ].NbResultLinks() > 0 );
+ }
+ if ( nbSides < 4 || nbSidesOpp != 2 )
+ continue;
+
+ for ( int iE = 0; iE < 4; ++iE )
+ {
+ soIDs[ iE ].clear();
+ _Node* n = quad._links[ iE ].FirstNode();
+ if ( n->_intPoint && n->_intPoint->_faceIDs.size() )
+ soIDs[ iE ] = _grid->GetSolidIDs( n->_intPoint->_faceIDs[0] );
+ }
+ if ((( soIDs[0].size() >= 2 ) +
+ ( soIDs[1].size() >= 2 ) +
+ ( soIDs[2].size() >= 2 ) +
+ ( soIDs[3].size() >= 2 ) ) < 3 )
+ continue;
+
+ bool done = false;
+ for ( int i = 0; i < 4; ++i )
+ {
+ int i1 = _grid->_helper->WrapIndex( i + 1, 4 );
+ int i2 = _grid->_helper->WrapIndex( i + 2, 4 );
+ int i3 = _grid->_helper->WrapIndex( i + 3, 4 );
+ if ( soIDs[i1].size() == 2 && soIDs[i ] != soIDs[i1] &&
+ soIDs[i2].size() == 2 && soIDs[i1] == soIDs[i2] &&
+ soIDs[i3].size() == 2 && soIDs[i2] == soIDs[i3] )
+ {
+ quad._links[ i1 ]._link->_splits.clear();
+ quad._links[ i2 ]._link->_splits.clear();
+ done = true;
+ break;
+ }
+ }
+ if ( done )
+ break;
+ }
+ return;
+ } // preventVolumesOverlapping()
+
+ //================================================================================
+ /*!
+ * \brief Set to _hexLinks a next portion of splits located on one side of INTERNAL FACEs
+ */
+ bool Hexahedron::_SplitIterator::Next()
+ {
+ if ( _iterationNb > 0 )
+ // count used splits
+ for ( size_t i = 0; i < _splits.size(); ++i )
+ {
+ if ( _splits[i]._iCheckIteration == _iterationNb )
+ {
+ _splits[i]._isUsed = _splits[i]._checkedSplit->_faces[1];
+ _nbUsed += _splits[i]._isUsed;
+ }
+ if ( !More() )
+ return false;
+ }
+
+ ++_iterationNb;
+
+ bool toTestUsed = ( _nbChecked >= _splits.size() );
+ if ( toTestUsed )
+ {
+ // all splits are checked; find all not used splits
+ for ( size_t i = 0; i < _splits.size(); ++i )
+ if ( !_splits[i].IsCheckedOrUsed( toTestUsed ))
+ _splits[i]._iCheckIteration = _iterationNb;
+
+ _nbUsed = _splits.size(); // to stop iteration
+ }
+ else
+ {
+ // get any not used/checked split to start from
+ _freeNodes.clear();
+ for ( size_t i = 0; i < _splits.size(); ++i )
+ {
+ if ( !_splits[i].IsCheckedOrUsed( toTestUsed ))
+ {
+ _freeNodes.push_back( _splits[i]._nodes[0] );
+ _freeNodes.push_back( _splits[i]._nodes[1] );
+ _splits[i]._iCheckIteration = _iterationNb;
+ break;
+ }
+ }
+ // find splits connected to the start one via _freeNodes
+ for ( size_t iN = 0; iN < _freeNodes.size(); ++iN )
+ {
+ for ( size_t iS = 0; iS < _splits.size(); ++iS )
+ {
+ if ( _splits[iS].IsCheckedOrUsed( toTestUsed ))
+ continue;
+ int iN2 = -1;
+ if ( _freeNodes[iN] == _splits[iS]._nodes[0] )
+ iN2 = 1;
+ else if ( _freeNodes[iN] == _splits[iS]._nodes[1] )
+ iN2 = 0;
+ else
+ continue;
+ if ( _freeNodes[iN]->_isInternalFlags > 0 )
+ {
+ if ( _splits[iS]._nodes[ iN2 ]->_isInternalFlags == 0 )
+ continue;
+ if ( !_splits[iS]._nodes[ iN2 ]->IsLinked( _freeNodes[iN]->_intPoint ))
+ continue;
+ }
+ _splits[iS]._iCheckIteration = _iterationNb;
+ _freeNodes.push_back( _splits[iS]._nodes[ iN2 ]);
+ }
+ }
+ }
+ // set splits to hex links
+
+ for ( int iL = 0; iL < 12; ++iL )
+ _hexLinks[ iL ]._splits.clear();
+
+ _Link split;
+ for ( size_t i = 0; i < _splits.size(); ++i )
+ {
+ if ( _splits[i]._iCheckIteration == _iterationNb )
+ {
+ split._nodes[0] = _splits[i]._nodes[0];
+ split._nodes[1] = _splits[i]._nodes[1];
+ _Link & hexLink = _hexLinks[ _splits[i]._linkID ];
+ hexLink._splits.push_back( split );
+ _splits[i]._checkedSplit = & hexLink._splits.back();
+ ++_nbChecked;
+ }
+ }
+ return More();
+ }
+
+ //================================================================================
+ /*!
+ * \brief computes exact bounding box with axes parallel to given ones
+ */
+ //================================================================================
+
+ void getExactBndBox( const vector< TopoDS_Shape >& faceVec,
+ const double* axesDirs,
+ Bnd_Box& shapeBox )
+ {
+ BRep_Builder b;
+ TopoDS_Compound allFacesComp;
+ b.MakeCompound( allFacesComp );
+ for ( size_t iF = 0; iF < faceVec.size(); ++iF )
+ b.Add( allFacesComp, faceVec[ iF ] );
+
+ double sP[6]; // aXmin, aYmin, aZmin, aXmax, aYmax, aZmax
+ shapeBox.Get(sP[0],sP[1],sP[2],sP[3],sP[4],sP[5]);
+ double farDist = 0;
+ for ( int i = 0; i < 6; ++i )
+ farDist = Max( farDist, 10 * sP[i] );
+
+ gp_XYZ axis[3] = { gp_XYZ( axesDirs[0], axesDirs[1], axesDirs[2] ),
+ gp_XYZ( axesDirs[3], axesDirs[4], axesDirs[5] ),
+ gp_XYZ( axesDirs[6], axesDirs[7], axesDirs[8] ) };
+ axis[0].Normalize();
+ axis[1].Normalize();
+ axis[2].Normalize();
+
+ gp_Mat basis( axis[0], axis[1], axis[2] );
+ gp_Mat bi = basis.Inverted();
+
+ gp_Pnt pMin, pMax;
+ for ( int iDir = 0; iDir < 3; ++iDir )
+ {
+ gp_XYZ axis0 = axis[ iDir ];
+ gp_XYZ axis1 = axis[ ( iDir + 1 ) % 3 ];
+ gp_XYZ axis2 = axis[ ( iDir + 2 ) % 3 ];
+ for ( int isMax = 0; isMax < 2; ++isMax )
+ {
+ double shift = isMax ? farDist : -farDist;
+ gp_XYZ orig = shift * axis0;
+ gp_XYZ norm = axis1 ^ axis2;
+ gp_Pln pln( orig, norm );
+ norm = pln.Axis().Direction().XYZ();
+ BRepBuilderAPI_MakeFace plane( pln, -farDist, farDist, -farDist, farDist );
+
+ gp_Pnt& pAxis = isMax ? pMax : pMin;
+ gp_Pnt pPlane, pFaces;
+ double dist = GEOMUtils::GetMinDistance( plane, allFacesComp, pPlane, pFaces );
+ if ( dist < 0 )
+ {
+ Bnd_B3d bb;
+ gp_XYZ corner;
+ for ( int i = 0; i < 2; ++i ) {
+ corner.SetCoord( 1, sP[ i*3 ]);
+ for ( int j = 0; j < 2; ++j ) {
+ corner.SetCoord( 2, sP[ i*3 + 1 ]);
+ for ( int k = 0; k < 2; ++k )
+ {
+ corner.SetCoord( 3, sP[ i*3 + 2 ]);
+ corner *= bi;
+ bb.Add( corner );
+ }
+ }
+ }
+ corner = isMax ? bb.CornerMax() : bb.CornerMin();
+ pAxis.SetCoord( iDir+1, corner.Coord( iDir+1 ));
+ }
+ else
+ {
+ gp_XYZ pf = pFaces.XYZ() * bi;
+ pAxis.SetCoord( iDir+1, pf.Coord( iDir+1 ) );
+ }
+ }
+ } // loop on 3 axes
+
+ shapeBox.SetVoid();
+ shapeBox.Add( pMin );
+ shapeBox.Add( pMax );