+namespace
+{
+
+ //================================================================================
+ /*!
+ * \brief Checks structure of a quadrangular mesh at the common VERTEX of two EDGEs.
+ * Returns true if there are two quadrangles near the VERTEX.
+ */
+ //================================================================================
+
+ bool isContinuousMesh(TopoDS_Edge E1,
+ TopoDS_Edge E2,
+ const TopoDS_Face& F,
+ const SMESH_ProxyMesh& mesh)
+ {
+ if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
+ E1.Orientation( TopAbs_FORWARD );
+ if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
+ E2.Orientation( TopAbs_FORWARD );
+
+ TopoDS_Vertex V;
+ if ( !TopExp::CommonVertex( E1, E2, V )) return false;
+
+ const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
+ if ( !n ) return SMESH_Algo::IsContinuous( E1, E2 ); // meshed by "composite segment"
+
+ n = mesh.GetProxyNode( n );
+
+ const SMESHDS_SubMesh* sm = mesh.GetSubMesh( F );
+ if ( !sm ) return false;
+
+ int nbQuads = 0;
+ SMDS_ElemIteratorPtr fIt = mesh.GetInverseElementIterator( n, SMDSAbs_Face );
+ if ( !fIt->more() )
+ return SMESH_Algo::IsContinuous( E1, E2 ); // meshed by "composite segment"
+ while ( fIt->more() )
+ {
+ const SMDS_MeshElement* f = fIt->next();
+ if ( !sm->Contains( f )) continue;
+
+ if ( f->NbCornerNodes() == 4 )
+ ++nbQuads;
+ else
+ return false;
+ }
+ return nbQuads == 2;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Return true if a vertex holds a node and this node is used by some quadrangle
+ */
+ //================================================================================
+
+ // bool isMeshedVertex( TopoDS_Vertex& V,
+ // const SMESH_Mesh& mesh )
+ // {
+ // const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
+ // if ( !n ) return false;
+
+ // SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
+ // while ( fIt->more() )
+ // {
+ // const SMDS_MeshElement* f = fIt->next();
+ // if ( f->NbCornerNodes() == 4 )
+ // return true;
+ // }
+ // return false;
+ // }
+
+ //================================================================================
+ /*!
+ * \brief Finds VERTEXes located at block corners
+ */
+ //================================================================================
+
+ void getBlockCorners( SMESH_ProxyMesh& mesh,
+ const TopoDS_Shape& shape,
+ TopTools_MapOfShape& cornerVV)
+ {
+ std::set<int> faceIDs; // ids of FACEs in the shape
+ TopExp_Explorer exp;
+ for ( exp.Init( shape, TopAbs_FACE ); exp.More(); exp.Next() )
+ faceIDs.insert( mesh.GetMeshDS()->ShapeToIndex( exp.Current() ));
+
+ TopTools_MapOfShape checkedVV;
+ for ( exp.Init( shape, TopAbs_VERTEX ); exp.More(); exp.Next() )
+ {
+ TopoDS_Vertex V = TopoDS::Vertex( exp.Current() );
+ if ( !checkedVV.Add( V )) continue;
+
+ const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
+ if ( !n ) continue;
+
+ const SMDS_MeshNode* nProxy = mesh.GetProxyNode( n );
+ bool isProxy = ( nProxy != n );
+ n = nProxy;
+
+ int nbQuads = 0;
+ SMDS_ElemIteratorPtr fIt = mesh.GetInverseElementIterator( n, SMDSAbs_Face );
+ while ( fIt->more() )
+ {
+ const SMDS_MeshElement* f = fIt->next();
+ if ( !faceIDs.count( f->getshapeId() )) continue;
+
+ if ( isProxy && !mesh.GetSubMesh( f->getshapeId() )->Contains( f ))
+ continue;
+
+ if ( f->NbCornerNodes() == 4 )
+ ++nbQuads;
+ else
+ nbQuads = 100;
+ }
+ if ( nbQuads == 3 )
+ cornerVV.Add( V );
+ }
+ }
+
+ //================================================================================
+ /*!
+ * \brief Return EDGEs dividing one box side
+ */
+ //================================================================================
+
+ bool getInternalEdges( SMESH_Mesh& mesh,
+ const TopoDS_Shape& shape,
+ const TopTools_MapOfShape& cornerVV,
+ TopTools_MapOfShape& internEE)
+ {
+ TopTools_IndexedMapOfShape subEE, subFF;
+ TopExp::MapShapes( shape, TopAbs_EDGE, subEE );
+ TopExp::MapShapes( shape, TopAbs_FACE, subFF );
+
+ TopoDS_Vertex VV[2];
+ TopTools_MapOfShape subChecked, ridgeEE;
+ TopTools_MapIteratorOfMapOfShape vIt( cornerVV );
+ for ( ; vIt.More(); vIt.Next() )
+ {
+ TopoDS_Shape V0 = vIt.Key();
+ // walk from one corner VERTEX to another along ridge EDGEs
+ PShapeIteratorPtr riIt = SMESH_MesherHelper::GetAncestors( V0, mesh, TopAbs_EDGE );
+ while ( const TopoDS_Shape* riE = riIt->next() )
+ {
+ if ( !subEE.Contains( *riE ) || !subChecked.Add( *riE ))
+ continue;
+ TopoDS_Edge ridgeE = TopoDS::Edge( *riE );
+ while ( !ridgeE.IsNull() )
+ {
+ if ( !ridgeEE.Add( ridgeE ))
+ break;
+ TopExp::Vertices( ridgeE, VV[0], VV[1] );
+ TopoDS_Shape V1 = VV[ V0.IsSame( VV[0] )];
+ if ( cornerVV.Contains( V1 ) )
+ break; // ridgeE reached a corner VERTEX
+
+ // detect internal EDGEs among those sharing V1. There can be 2, 3 or 4 EDGEs and
+ // number of internal EDGEs is N-2
+ TopoDS_Shape nextRidgeE;
+ PShapeIteratorPtr eIt = SMESH_MesherHelper::GetAncestors( V1, mesh, TopAbs_EDGE );
+ while ( const TopoDS_Shape* E = eIt->next() )
+ {
+ if ( E->IsSame( ridgeE ) || !subEE.Contains( *E ) || !subChecked.Add( *E ))
+ continue;
+ // look for FACEs sharing both E and ridgeE
+ PShapeIteratorPtr fIt = SMESH_MesherHelper::GetAncestors( *E, mesh, TopAbs_FACE );
+ while ( const TopoDS_Shape* F = fIt->next() )
+ {
+ if ( !SMESH_MesherHelper::IsSubShape( ridgeE, *F ))
+ continue;
+ if ( !subFF.Contains( *F ))
+ continue;
+ if ( isContinuousMesh( ridgeE, TopoDS::Edge( *E ), TopoDS::Face( *F ), mesh ))
+ {
+ nextRidgeE = *E;
+ }
+ else
+ {
+ internEE.Add( *E );
+ }
+ break;
+ }
+ }
+ // look for the next ridge EDGE ending at V1
+ if ( nextRidgeE.IsNull() )
+ {
+ eIt = SMESH_MesherHelper::GetAncestors( V1, mesh, TopAbs_EDGE );
+ while ( const TopoDS_Shape* E = eIt->next() )
+ if ( !ridgeE.IsSame( *E ) && !internEE.Contains( *E ) && subEE.Contains( *E ))
+ {
+ nextRidgeE = *E;
+ break;
+ }
+ }
+ ridgeE = TopoDS::Edge( nextRidgeE );
+ V0 = V1;
+
+ if ( ridgeE.IsNull() )
+ return false;
+ } // check EDGEs around the last VERTEX of ridgeE
+ } // loop on ridge EDGEs around a corner VERTEX
+ } // loop on on corner VERTEXes
+
+ if ( subEE.Extent() > ridgeEE.Extent() + internEE.Extent() ) // PAL23269
+ for ( int i = 1; i < subEE.Extent(); ++i )
+ if ( !ridgeEE.Contains( subEE(i) ))
+ internEE.Add( subEE(i) );
+
+ return true;
+ } // getInternalEdges()
+
+ //================================================================================
+ /*!
+ * \brief Find a face including two given nodes
+ */
+ //================================================================================
+
+ const SMDS_MeshElement* FindFaceByNodes( const SMDS_MeshNode* n1,
+ const SMDS_MeshNode* n2,
+ TIDSortedElemSet avoidSet,
+ SMESH_ProxyMesh& mesh)
+ {
+ SMDS_ElemIteratorPtr faceIt = mesh.GetInverseElementIterator( n1, SMDSAbs_Face );
+ while ( faceIt->more() )
+ {
+ const SMDS_MeshElement* f = faceIt->next();
+ if ( !avoidSet.count( f ) && f->GetNodeIndex( n2 ) >= 0 )
+ return f;
+ }
+ return 0;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Check that a segment bounds a face belonging to smOfFaces
+ */
+ //================================================================================
+
+ bool IsSegmentOnSubMeshBoundary( const SMDS_MeshNode* n1,
+ const SMDS_MeshNode* n2,
+ const SMESHDS_SubMesh* smOfFaces,
+ SMESH_ProxyMesh& mesh)
+ {
+ TIDSortedElemSet avoidSet;
+ bool faceFound = false;
+
+ while ( const SMDS_MeshElement* f = FindFaceByNodes( n1, n2, avoidSet, mesh ))
+ {
+ if (( faceFound = smOfFaces->Contains( f )))
+ break;
+ avoidSet.insert( f );
+ }
+ return faceFound;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Rearrange block sides according to StdMeshers_BlockRenumber hypothesis
+ */
+ //================================================================================
+
+ bool arrangeForRenumber( list< _QuadFaceGrid >& blockSides,
+ const TopTools_MapOfShape& cornerVertices,
+ SMESH_Mesh* mesh,
+ TopoDS_Vertex& v000,
+ TopoDS_Vertex& v001 )
+ {
+ if ( v000.IsNull() )
+ {
+ // block CS is not defined;
+ // renumber only if the block has an edge parallel to an axis of global CS
+
+ v000 = StdMeshers_RenumberHelper::GetVertex000( cornerVertices );
+ }
+
+ Bnd_B3d bbox;
+ for ( auto it = cornerVertices.cbegin(); it != cornerVertices.cend(); ++it )
+ bbox.Add( BRep_Tool::Pnt( TopoDS::Vertex( *it )));
+ double tol = 1e-5 * Sqrt( bbox.SquareExtent() );
+
+ // get block edges starting at v000
+
+ std::vector< const _FaceSide* > edgesAtV000;
+ std::vector< gp_Vec > edgeDir;
+ std::vector< int > iParallel; // 0 - none, 1 - X, 2 - Y, 3 - Z
+ TopTools_MapOfShape lastVertices;
+ for ( _QuadFaceGrid & quad: blockSides )
+ {
+ for ( int iS = 0; iS < 4 && edgesAtV000.size() < 3; ++iS )
+ {
+ const _FaceSide* side = & quad.GetSide( iS );
+ TopoDS_Vertex v1 = side->FirstVertex(), v2 = side->LastVertex();
+ if (( v1.IsSame( v000 ) && !lastVertices.Contains( v2 )) ||
+ ( v2.IsSame( v000 ) && !lastVertices.Contains( v1 )))
+ {
+ bool reverse = v2.IsSame( v000 );
+ if ( reverse )
+ std::swap( v1, v2 );
+ lastVertices.Add( v2 );
+
+ edgesAtV000.push_back( side );
+
+ gp_Pnt pf = BRep_Tool::Pnt( v1 );
+ gp_Pnt pl = BRep_Tool::Pnt( v2 );
+ gp_Vec vec( pf, pl );
+ edgeDir.push_back( vec );
+
+ iParallel.push_back( 0 );
+ if ( !v001.IsNull() )
+ {
+ if ( quad.IsComplex() )
+ for ( _QuadFaceGrid::TChildIterator chIt = quad.GetChildren(); chIt.more(); )
+ {
+ const _QuadFaceGrid& child = chIt.next();
+ if ( child.GetSide( iS ).Contain( v001 ))
+ {
+ iParallel.back() = 3;
+ break;
+ }
+ }
+ else if ( side->Contain( v001 ))
+ iParallel.back() = 3;
+ }
+ else
+ {
+ bool isStraight = true;
+ std::list< TopoDS_Edge > edges;
+ for ( int iE = 0; true; ++iE )
+ {
+ TopoDS_Edge edge = side->Edge( iE );
+ if ( edge.IsNull() )
+ break;
+ edges.push_back( edge );
+ if ( isStraight )
+ isStraight = SMESH_Algo::IsStraight( edge );
+ }
+ // is parallel to a GCS axis?
+ if ( isStraight )
+ {
+ int nbDiff = (( Abs( vec.X() ) > tol ) +
+ ( Abs( vec.Y() ) > tol ) +
+ ( Abs( vec.Z() ) > tol ) );
+ if ( nbDiff == 1 )
+ iParallel.back() = ( Abs( vec.X() ) > tol ) ? 1 : ( Abs( vec.Y() ) > tol ) ? 2 : 3;
+ }
+ else
+ {
+ TopoDS_Face nullFace;
+ StdMeshers_FaceSide fSide( nullFace, edges, mesh, true, true );
+ edgeDir.back() = gp_Vec( pf, fSide.Value3d( reverse ? 0.99 : 0.01 ));
+ }
+ }
+ }
+ }
+ }
+ if ( std::accumulate( iParallel.begin(), iParallel.end(), 0 ) == 0 )
+ return false;
+
+ // find edge OZ and edge OX
+ const _FaceSide* edgeOZ = nullptr, *edgeOY = nullptr, *edgeOX = nullptr;
+ auto iZIt = std::find( iParallel.begin(), iParallel.end(), 3 );
+ if ( iZIt != iParallel.end() )
+ {
+ int i = std::distance( iParallel.begin(), iZIt );
+ edgeOZ = edgesAtV000[ i ];
+ int iE1 = SMESH_MesherHelper::WrapIndex( i + 1, edgesAtV000.size() );
+ int iE2 = SMESH_MesherHelper::WrapIndex( i + 2, edgesAtV000.size() );
+ if (( edgeDir[ iE1 ] ^ edgeDir[ iE2 ] ) * edgeDir[ i ] < 0 )
+ std::swap( iE1, iE2 );
+ edgeOX = edgesAtV000[ iE1 ];
+ edgeOY = edgesAtV000[ iE2 ];
+ }
+ else
+ {
+ for ( size_t i = 0; i < edgesAtV000.size(); ++i )
+ {
+ if ( !iParallel[ i ] )
+ continue;
+ int iE1 = SMESH_MesherHelper::WrapIndex( i + 1, edgesAtV000.size() );
+ int iE2 = SMESH_MesherHelper::WrapIndex( i + 2, edgesAtV000.size() );
+ if (( edgeDir[ iE1 ] ^ edgeDir[ iE2 ] ) * edgeDir[ i ] < 0 )
+ std::swap( iE1, iE2 );
+ edgeOZ = edgesAtV000[ iParallel[i] == 1 ? iE2 : iE1 ];
+ edgeOX = edgesAtV000[ iParallel[i] == 1 ? i : iE1 ];
+ edgeOY = edgesAtV000[ iParallel[i] == 1 ? iE1 : i ];
+ break;
+ }
+ }
+
+ if ( !edgeOZ || !edgeOX || !edgeOY )
+ return false;
+
+ TopoDS_Vertex v100 = edgeOX->LastVertex();
+ if ( v100.IsSame( v000 ))
+ v100 = edgeOX->FirstVertex();
+
+ // Find the left quad, one including v000 but not v100
+
+ for ( auto quad = blockSides.begin(); quad != blockSides.end(); ++quad )
+ {
+ if ( quad->Contain( v000 ) && !quad->Contain( v100 )) // it's a left quad
+ {
+ if ( quad != blockSides.begin() )
+ blockSides.splice( blockSides.begin(), blockSides, quad );
+ blockSides.front().SetBottomSide( *edgeOZ ); // edgeOY
+
+ return true;
+ }
+ }
+ return false;
+ }
+
+} // namespace
+