+ if ( nodes.size() > 20 ) return;
+
+ // get shapes under nodes
+ TGeomID nShapeIds[20], *nShapeIdsEnd = &nShapeIds[0] + nodes.size();
+ for ( size_t i = 0; i < nodes.size(); ++i )
+ if ( !( nShapeIds[i] = nodes[i]->ShapeID() ))
+ return;
+
+ // get shapes of the FACE
+ const TopoDS_Face& face = TopoDS::Face( _grid->Shape( faceID ));
+ list< TopoDS_Edge > edges;
+ list< int > nbEdges;
+ int nbW = SMESH_Block::GetOrderedEdges (face, edges, nbEdges);
+ if ( nbW > 1 ) {
+ // select a WIRE - remove EDGEs of irrelevant WIREs from edges
+ list< TopoDS_Edge >::iterator e = edges.begin(), eEnd = e;
+ list< int >::iterator nE = nbEdges.begin();
+ for ( ; nbW > 0; ++nE, --nbW )
+ {
+ std::advance( eEnd, *nE );
+ for ( ; e != eEnd; ++e )
+ for ( int i = 0; i < 2; ++i )
+ {
+ TGeomID id = i==0 ?
+ _grid->ShapeID( *e ) :
+ _grid->ShapeID( SMESH_MesherHelper::IthVertex( 0, *e ));
+ if (( id > 0 ) &&
+ ( std::find( &nShapeIds[0], nShapeIdsEnd, id ) != nShapeIdsEnd ))
+ {
+ edges.erase( eEnd, edges.end() ); // remove rest wires
+ e = eEnd = edges.end();
+ --e;
+ nbW = 0;
+ break;
+ }
+ }
+ if ( nbW > 0 )
+ edges.erase( edges.begin(), eEnd ); // remove a current irrelevant wire
+ }
+ }
+ // rotate edges to have the first one at least partially out of the hexa
+ list< TopoDS_Edge >::iterator e = edges.begin(), eMidOut = edges.end();
+ for ( ; e != edges.end(); ++e )
+ {
+ if ( !_grid->ShapeID( *e ))
+ continue;
+ bool isOut = false;
+ gp_Pnt p;
+ double uvw[3], f,l;
+ for ( int i = 0; i < 2 && !isOut; ++i )
+ {
+ if ( i == 0 )
+ {
+ TopoDS_Vertex v = SMESH_MesherHelper::IthVertex( 0, *e );
+ p = BRep_Tool::Pnt( v );
+ }
+ else if ( eMidOut == edges.end() )
+ {
+ TopLoc_Location loc;
+ Handle(Geom_Curve) c = BRep_Tool::Curve( *e, loc, f, l);
+ if ( c.IsNull() ) break;
+ p = c->Value( 0.5 * ( f + l )).Transformed( loc );
+ }
+ else
+ {
+ continue;
+ }
+
+ _grid->ComputeUVW( p.XYZ(), uvw );
+ if ( isOutParam( uvw ))
+ {
+ if ( i == 0 )
+ isOut = true;
+ else
+ eMidOut = e;
+ }
+ }
+ if ( isOut )
+ break;
+ }
+ if ( e != edges.end() )
+ edges.splice( edges.end(), edges, edges.begin(), e );
+ else if ( eMidOut != edges.end() )
+ edges.splice( edges.end(), edges, edges.begin(), eMidOut );
+
+ // sort nodes according to the order of edges
+ _Node* orderNodes [20];
+ //TGeomID orderShapeIDs[20];
+ size_t nbN = 0;
+ TGeomID id, *pID = 0;
+ for ( e = edges.begin(); e != edges.end(); ++e )
+ {
+ if (( id = _grid->ShapeID( SMESH_MesherHelper::IthVertex( 0, *e ))) &&
+ (( pID = std::find( &nShapeIds[0], nShapeIdsEnd, id )) != nShapeIdsEnd ))
+ {
+ //orderShapeIDs[ nbN ] = id;
+ orderNodes [ nbN++ ] = nodes[ pID - &nShapeIds[0] ];
+ *pID = -1;
+ }
+ if (( id = _grid->ShapeID( *e )) &&
+ (( pID = std::find( &nShapeIds[0], nShapeIdsEnd, id )) != nShapeIdsEnd ))
+ {
+ //orderShapeIDs[ nbN ] = id;
+ orderNodes [ nbN++ ] = nodes[ pID - &nShapeIds[0] ];
+ *pID = -1;
+ }
+ }
+ if ( nbN != nodes.size() )
+ return;
+
+ bool reverse = ( orderNodes[0 ]->Point().SquareDistance( curNode->Point() ) >
+ orderNodes[nbN-1]->Point().SquareDistance( curNode->Point() ));
+
+ for ( size_t i = 0; i < nodes.size(); ++i )
+ nodes[ i ] = orderNodes[ reverse ? nbN-1-i : i ];
+ }
+
+ //================================================================================
+ /*!
+ * \brief Adds computed elements to the mesh
+ */
+ int Hexahedron::addVolumes( SMESH_MesherHelper& helper )
+ {
+ F_IntersectPoint noIntPnt;
+ const bool toCheckNodePos = _grid->IsToCheckNodePos();
+
+ int nbAdded = 0;
+ // add elements resulted from hexahedron intersection
+ for ( _volumeDef* volDef = &_volumeDefs; volDef; volDef = volDef->_next )
+ {
+ vector< const SMDS_MeshNode* > nodes( volDef->_nodes.size() );
+ for ( size_t iN = 0; iN < nodes.size(); ++iN )
+ {
+ if ( !( nodes[iN] = volDef->_nodes[iN].Node() ))
+ {
+ if ( const E_IntersectPoint* eip = volDef->_nodes[iN].EdgeIntPnt() )
+ {
+ nodes[iN] = volDef->_nodes[iN]._intPoint->_node =
+ helper.AddNode( eip->_point.X(),
+ eip->_point.Y(),
+ eip->_point.Z() );
+ if ( _grid->ShapeType( eip->_shapeID ) == TopAbs_VERTEX )
+ helper.GetMeshDS()->SetNodeOnVertex( nodes[iN], eip->_shapeID );
+ else
+ helper.GetMeshDS()->SetNodeOnEdge( nodes[iN], eip->_shapeID );
+ }
+ else
+ throw SALOME_Exception("Bug: no node at intersection point");
+ }
+ else if ( volDef->_nodes[iN]._intPoint &&
+ volDef->_nodes[iN]._intPoint->_node == volDef->_nodes[iN]._node )
+ {
+ // Update position of node at EDGE intersection;
+ // see comment to _Node::Add( E_IntersectPoint )
+ SMESHDS_Mesh* mesh = helper.GetMeshDS();
+ TGeomID shapeID = volDef->_nodes[iN].EdgeIntPnt()->_shapeID;
+ mesh->UnSetNodeOnShape( nodes[iN] );
+ if ( _grid->ShapeType( shapeID ) == TopAbs_VERTEX )
+ mesh->SetNodeOnVertex( nodes[iN], shapeID );
+ else
+ mesh->SetNodeOnEdge( nodes[iN], shapeID );
+ }
+ else if ( toCheckNodePos &&
+ !nodes[iN]->isMarked() &&
+ _grid->ShapeType( nodes[iN]->GetShapeID() ) == TopAbs_FACE )
+ {
+ _grid->SetOnShape( nodes[iN], noIntPnt, /*unset=*/true );
+ nodes[iN]->setIsMarked( true );
+ }
+ }
+
+ const SMDS_MeshElement* v = 0;
+ if ( !volDef->_quantities.empty() )
+ {
+ v = helper.AddPolyhedralVolume( nodes, volDef->_quantities );
+ }
+ else
+ {
+ switch ( nodes.size() )
+ {
+ case 8: v = helper.AddVolume( nodes[0],nodes[1],nodes[2],nodes[3],
+ nodes[4],nodes[5],nodes[6],nodes[7] );
+ break;
+ case 4: v = helper.AddVolume( nodes[0],nodes[1],nodes[2],nodes[3] );
+ break;
+ case 6: v = helper.AddVolume( nodes[0],nodes[1],nodes[2],nodes[3],nodes[4],nodes[5] );
+ break;
+ case 5: v = helper.AddVolume( nodes[0],nodes[1],nodes[2],nodes[3],nodes[4] );
+ break;
+ }
+ }
+ if (( volDef->_volume = v ))
+ {
+ helper.GetMeshDS()->SetMeshElementOnShape( v, volDef->_solidID );
+ ++nbAdded;
+ }
+ }
+
+ return nbAdded;
+ }
+ //================================================================================
+ /*!
+ * \brief Return true if the element is in a hole
+ */
+ bool Hexahedron::isInHole() const
+ {
+ if ( !_vIntNodes.empty() )
+ return false;
+
+ const size_t ijk[3] = { _i, _j, _k };
+ F_IntersectPoint curIntPnt;
+
+ // consider a cell to be in a hole if all links in any direction
+ // comes OUT of geometry
+ for ( int iDir = 0; iDir < 3; ++iDir )
+ {
+ const vector<double>& coords = _grid->_coords[ iDir ];
+ LineIndexer li = _grid->GetLineIndexer( iDir );
+ li.SetIJK( _i,_j,_k );
+ size_t lineIndex[4] = { li.LineIndex (),
+ li.LineIndex10(),
+ li.LineIndex01(),
+ li.LineIndex11() };
+ bool allLinksOut = true, hasLinks = false;
+ for ( int iL = 0; iL < 4 && allLinksOut; ++iL ) // loop on 4 links parallel to iDir
+ {
+ const _Link& link = _hexLinks[ iL + 4*iDir ];
+ // check transition of the first node of a link
+ const F_IntersectPoint* firstIntPnt = 0;
+ if ( link._nodes[0]->Node() ) // 1st node is a hexa corner
+ {
+ curIntPnt._paramOnLine = coords[ ijk[ iDir ]] - coords[0] + _grid->_tol;
+ const GridLine& line = _grid->_lines[ iDir ][ lineIndex[ iL ]];
+ if ( !line._intPoints.empty() )
+ {
+ multiset< F_IntersectPoint >::const_iterator ip =
+ line._intPoints.upper_bound( curIntPnt );
+ --ip;
+ firstIntPnt = &(*ip);
+ }
+ }
+ else if ( !link._fIntPoints.empty() )
+ {
+ firstIntPnt = link._fIntPoints[0];
+ }
+
+ if ( firstIntPnt )
+ {
+ hasLinks = true;
+ allLinksOut = ( firstIntPnt->_transition == Trans_OUT &&
+ !_grid->IsShared( firstIntPnt->_faceIDs[0] ));
+ }
+ }
+ if ( hasLinks && allLinksOut )
+ return true;
+ }
+ return false;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Check if a polyherdon has an edge lying on EDGE shared by strange FACE
+ * that will be meshed by other algo
+ */
+ bool Hexahedron::hasStrangeEdge() const
+ {
+ if ( _eIntPoints.size() < 2 )
+ return false;
+
+ TopTools_MapOfShape edges;
+ for ( size_t i = 0; i < _eIntPoints.size(); ++i )
+ {
+ if ( !_grid->IsStrangeEdge( _eIntPoints[i]->_shapeID ))
+ continue;
+ const TopoDS_Shape& s = _grid->Shape( _eIntPoints[i]->_shapeID );
+ if ( s.ShapeType() == TopAbs_EDGE )
+ {
+ if ( ! edges.Add( s ))
+ return true; // an EDGE encounters twice
+ }
+ else
+ {
+ PShapeIteratorPtr edgeIt = _grid->_helper->GetAncestors( s,
+ *_grid->_helper->GetMesh(),
+ TopAbs_EDGE );
+ while ( const TopoDS_Shape* edge = edgeIt->next() )
+ if ( ! edges.Add( *edge ))
+ return true; // an EDGE encounters twice
+ }
+ }
+ return false;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Return true if a polyhedron passes _sizeThreshold criterion
+ */
+ bool Hexahedron::checkPolyhedronSize( bool cutByInternalFace ) const
+ {
+ if ( cutByInternalFace && !_grid->_toUseThresholdForInternalFaces )
+ {
+ // check if any polygon fully lies on shared/internal FACEs
+ for ( size_t iP = 0; iP < _polygons.size(); ++iP )
+ {
+ const _Face& polygon = _polygons[iP];
+ if ( polygon._links.empty() )
+ continue;
+ bool allNodesInternal = true;
+ for ( size_t iL = 0; iL < polygon._links.size() && allNodesInternal; ++iL )
+ {
+ _Node* n = polygon._links[ iL ].FirstNode();
+ allNodesInternal = (( n->IsCutByInternal() ) ||
+ ( n->_intPoint && _grid->IsAnyShared( n->_intPoint->_faceIDs )));
+ }
+ if ( allNodesInternal )
+ return true;
+ }
+ }
+ if ( this->hasStrangeEdge() )
+ return true;
+
+ double volume = 0;
+ for ( size_t iP = 0; iP < _polygons.size(); ++iP )
+ {
+ const _Face& polygon = _polygons[iP];
+ if ( polygon._links.empty() )
+ continue;
+ gp_XYZ area (0,0,0);
+ gp_XYZ p1 = polygon._links[ 0 ].FirstNode()->Point().XYZ();
+ for ( size_t iL = 0; iL < polygon._links.size(); ++iL )
+ {
+ gp_XYZ p2 = polygon._links[ iL ].LastNode()->Point().XYZ();
+ area += p1 ^ p2;
+ p1 = p2;
+ }
+ volume += p1 * area;
+ }
+ volume /= 6;
+
+ double initVolume = _sideLength[0] * _sideLength[1] * _sideLength[2];
+
+ return volume > initVolume / _grid->_sizeThreshold;
+ }
+ //================================================================================
+ /*!
+ * \brief Tries to create a hexahedron
+ */
+ bool Hexahedron::addHexa()
+ {
+ int nbQuad = 0, iQuad = -1;
+ for ( size_t i = 0; i < _polygons.size(); ++i )
+ {
+ if ( _polygons[i]._links.empty() )
+ continue;
+ if ( _polygons[i]._links.size() != 4 )
+ return false;
+ ++nbQuad;
+ if ( iQuad < 0 )
+ iQuad = i;
+ }
+ if ( nbQuad != 6 )
+ return false;
+
+ _Node* nodes[8];
+ int nbN = 0;
+ for ( int iL = 0; iL < 4; ++iL )
+ {
+ // a base node
+ nodes[iL] = _polygons[iQuad]._links[iL].FirstNode();
+ ++nbN;
+
+ // find a top node above the base node
+ _Link* link = _polygons[iQuad]._links[iL]._link;
+ if ( !link->_faces[0] || !link->_faces[1] )
+ return debugDumpLink( link );
+ // a quadrangle sharing <link> with _polygons[iQuad]
+ _Face* quad = link->_faces[ bool( link->_faces[0] == & _polygons[iQuad] )];
+ for ( int i = 0; i < 4; ++i )
+ if ( quad->_links[i]._link == link )
+ {
+ // 1st node of a link opposite to <link> in <quad>
+ nodes[iL+4] = quad->_links[(i+2)%4].FirstNode();
+ ++nbN;
+ break;
+ }
+ }
+ if ( nbN == 8 )
+ _volumeDefs.Set( &nodes[0], 8 );
+
+ return nbN == 8;
+ }
+ //================================================================================
+ /*!
+ * \brief Tries to create a tetrahedron
+ */
+ bool Hexahedron::addTetra()
+ {
+ int iTria = -1;
+ for ( size_t i = 0; i < _polygons.size() && iTria < 0; ++i )
+ if ( _polygons[i]._links.size() == 3 )
+ iTria = i;
+ if ( iTria < 0 )
+ return false;
+
+ _Node* nodes[4];
+ nodes[0] = _polygons[iTria]._links[0].FirstNode();
+ nodes[1] = _polygons[iTria]._links[1].FirstNode();
+ nodes[2] = _polygons[iTria]._links[2].FirstNode();
+
+ _Link* link = _polygons[iTria]._links[0]._link;
+ if ( !link->_faces[0] || !link->_faces[1] )
+ return debugDumpLink( link );
+
+ // a triangle sharing <link> with _polygons[0]
+ _Face* tria = link->_faces[ bool( link->_faces[0] == & _polygons[iTria] )];
+ for ( int i = 0; i < 3; ++i )
+ if ( tria->_links[i]._link == link )
+ {
+ nodes[3] = tria->_links[(i+1)%3].LastNode();
+ _volumeDefs.Set( &nodes[0], 4 );
+ return true;
+ }
+
+ return false;
+ }
+ //================================================================================
+ /*!
+ * \brief Tries to create a pentahedron
+ */
+ bool Hexahedron::addPenta()
+ {
+ // find a base triangular face
+ int iTri = -1;
+ for ( int iF = 0; iF < 5 && iTri < 0; ++iF )
+ if ( _polygons[ iF ]._links.size() == 3 )
+ iTri = iF;
+ if ( iTri < 0 ) return false;
+
+ // find nodes
+ _Node* nodes[6];
+ int nbN = 0;
+ for ( int iL = 0; iL < 3; ++iL )
+ {
+ // a base node
+ nodes[iL] = _polygons[ iTri ]._links[iL].FirstNode();
+ ++nbN;
+
+ // find a top node above the base node
+ _Link* link = _polygons[ iTri ]._links[iL]._link;
+ if ( !link->_faces[0] || !link->_faces[1] )
+ return debugDumpLink( link );
+ // a quadrangle sharing <link> with a base triangle
+ _Face* quad = link->_faces[ bool( link->_faces[0] == & _polygons[ iTri ] )];
+ if ( quad->_links.size() != 4 ) return false;
+ for ( int i = 0; i < 4; ++i )
+ if ( quad->_links[i]._link == link )
+ {
+ // 1st node of a link opposite to <link> in <quad>
+ nodes[iL+3] = quad->_links[(i+2)%4].FirstNode();
+ ++nbN;
+ break;
+ }
+ }
+ if ( nbN == 6 )
+ _volumeDefs.Set( &nodes[0], 6 );
+
+ return ( nbN == 6 );
+ }
+ //================================================================================
+ /*!
+ * \brief Tries to create a pyramid
+ */
+ bool Hexahedron::addPyra()
+ {
+ // find a base quadrangle
+ int iQuad = -1;
+ for ( int iF = 0; iF < 5 && iQuad < 0; ++iF )
+ if ( _polygons[ iF ]._links.size() == 4 )
+ iQuad = iF;
+ if ( iQuad < 0 ) return false;
+
+ // find nodes
+ _Node* nodes[5];
+ nodes[0] = _polygons[iQuad]._links[0].FirstNode();
+ nodes[1] = _polygons[iQuad]._links[1].FirstNode();
+ nodes[2] = _polygons[iQuad]._links[2].FirstNode();
+ nodes[3] = _polygons[iQuad]._links[3].FirstNode();
+
+ _Link* link = _polygons[iQuad]._links[0]._link;
+ if ( !link->_faces[0] || !link->_faces[1] )
+ return debugDumpLink( link );
+
+ // a triangle sharing <link> with a base quadrangle
+ _Face* tria = link->_faces[ bool( link->_faces[0] == & _polygons[ iQuad ] )];
+ if ( tria->_links.size() != 3 ) return false;
+ for ( int i = 0; i < 3; ++i )
+ if ( tria->_links[i]._link == link )
+ {
+ nodes[4] = tria->_links[(i+1)%3].LastNode();
+ _volumeDefs.Set( &nodes[0], 5 );
+ return true;
+ }
+
+ return false;
+ }
+ //================================================================================
+ /*!
+ * \brief Dump a link and return \c false
+ */
+ bool Hexahedron::debugDumpLink( Hexahedron::_Link* link )
+ {
+#ifdef _DEBUG_
+ gp_Pnt p1 = link->_nodes[0]->Point(), p2 = link->_nodes[1]->Point();
+ cout << "BUG: not shared link. IKJ = ( "<< _i << " " << _j << " " << _k << " )" << endl
+ << "n1 (" << p1.X() << ", "<< p1.Y() << ", "<< p1.Z() << " )" << endl
+ << "n2 (" << p2.X() << ", "<< p2.Y() << ", "<< p2.Z() << " )" << endl;
+#else
+ (void)link; // unused in release mode
+#endif
+ return false;
+ }
+ //================================================================================
+ /*!
+ * \brief Classify a point by grid parameters
+ */
+ bool Hexahedron::isOutParam(const double uvw[3]) const
+ {
+ return (( _grid->_coords[0][ _i ] - _grid->_tol > uvw[0] ) ||
+ ( _grid->_coords[0][ _i+1 ] + _grid->_tol < uvw[0] ) ||
+ ( _grid->_coords[1][ _j ] - _grid->_tol > uvw[1] ) ||
+ ( _grid->_coords[1][ _j+1 ] + _grid->_tol < uvw[1] ) ||
+ ( _grid->_coords[2][ _k ] - _grid->_tol > uvw[2] ) ||
+ ( _grid->_coords[2][ _k+1 ] + _grid->_tol < uvw[2] ));
+ }
+ //================================================================================
+ /*!
+ * \brief Divide a polygon into triangles and modify accordingly an adjacent polyhedron
+ */
+ void splitPolygon( const SMDS_MeshElement* polygon,
+ SMDS_VolumeTool & volume,
+ const int facetIndex,
+ const TGeomID faceID,
+ const TGeomID solidID,
+ SMESH_MeshEditor::ElemFeatures& face,
+ SMESH_MeshEditor& editor,
+ const bool reinitVolume)
+ {
+ SMESH_MeshAlgos::Triangulate divider(/*optimize=*/false);
+ int nbTrias = divider.GetTriangles( polygon, face.myNodes );
+ face.myNodes.resize( nbTrias * 3 );
+
+ SMESH_MeshEditor::ElemFeatures newVolumeDef;
+ newVolumeDef.Init( volume.Element() );
+ newVolumeDef.SetID( volume.Element()->GetID() );
+
+ newVolumeDef.myPolyhedQuantities.reserve( volume.NbFaces() + nbTrias );
+ newVolumeDef.myNodes.reserve( volume.NbNodes() + nbTrias * 3 );
+
+ SMESHDS_Mesh* meshDS = editor.GetMeshDS();
+ SMDS_MeshElement* newTriangle;
+ for ( int iF = 0, nF = volume.NbFaces(); iF < nF; iF++ )
+ {
+ if ( iF == facetIndex )
+ {
+ newVolumeDef.myPolyhedQuantities.push_back( 3 );
+ newVolumeDef.myNodes.insert( newVolumeDef.myNodes.end(),
+ face.myNodes.begin(),
+ face.myNodes.begin() + 3 );
+ meshDS->RemoveFreeElement( polygon, 0, false );
+ newTriangle = meshDS->AddFace( face.myNodes[0], face.myNodes[1], face.myNodes[2] );
+ meshDS->SetMeshElementOnShape( newTriangle, faceID );
+ }
+ else
+ {
+ const SMDS_MeshNode** nn = volume.GetFaceNodes( iF );
+ const size_t nbFaceNodes = volume.NbFaceNodes ( iF );
+ newVolumeDef.myPolyhedQuantities.push_back( nbFaceNodes );
+ newVolumeDef.myNodes.insert( newVolumeDef.myNodes.end(), nn, nn + nbFaceNodes );
+ }
+ }
+
+ 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 };