+ * \brief Implements geom edges into the mesh
+ */
+ void Hexahedron::addEdges(SMESH_MesherHelper& helper,
+ vector< Hexahedron* >& hexes,
+ const map< TGeomID, vector< TGeomID > >& edge2faceIDsMap)
+ {
+ if ( edge2faceIDsMap.empty() ) return;
+
+ // Prepare planes for intersecting with EDGEs
+ GridPlanes pln[3];
+ {
+ for ( int iDirZ = 0; iDirZ < 3; ++iDirZ ) // iDirZ gives normal direction to planes
+ {
+ GridPlanes& planes = pln[ iDirZ ];
+ int iDirX = ( iDirZ + 1 ) % 3;
+ int iDirY = ( iDirZ + 2 ) % 3;
+ planes._zNorm = ( _grid->_axes[ iDirX ] ^ _grid->_axes[ iDirY ] ).Normalized();
+ planes._zProjs.resize ( _grid->_coords[ iDirZ ].size() );
+ planes._zProjs [0] = 0;
+ const double zFactor = _grid->_axes[ iDirZ ] * planes._zNorm;
+ const vector< double > & u = _grid->_coords[ iDirZ ];
+ for ( size_t i = 1; i < planes._zProjs.size(); ++i )
+ {
+ planes._zProjs [i] = zFactor * ( u[i] - u[0] );
+ }
+ }
+ }
+ const double deflection = _grid->_minCellSize / 20.;
+ const double tol = _grid->_tol;
+ E_IntersectPoint ip;
+
+ TColStd_MapOfInteger intEdgeIDs; // IDs of not shared INTERNAL EDGES
+
+ // Intersect EDGEs with the planes
+ map< TGeomID, vector< TGeomID > >::const_iterator e2fIt = edge2faceIDsMap.begin();
+ for ( ; e2fIt != edge2faceIDsMap.end(); ++e2fIt )
+ {
+ const TGeomID edgeID = e2fIt->first;
+ const TopoDS_Edge & E = TopoDS::Edge( _grid->Shape( edgeID ));
+ BRepAdaptor_Curve curve( E );
+ TopoDS_Vertex v1 = helper.IthVertex( 0, E, false );
+ TopoDS_Vertex v2 = helper.IthVertex( 1, E, false );
+
+ ip._faceIDs = e2fIt->second;
+ ip._shapeID = edgeID;
+
+ bool isInternal = ( ip._faceIDs.size() == 1 && _grid->IsInternal( edgeID ));
+ if ( isInternal )
+ {
+ intEdgeIDs.Add( edgeID );
+ intEdgeIDs.Add( _grid->ShapeID( v1 ));
+ intEdgeIDs.Add( _grid->ShapeID( v2 ));
+ }
+
+ // discretize the EDGE
+ GCPnts_UniformDeflection discret( curve, deflection, true );
+ if ( !discret.IsDone() || discret.NbPoints() < 2 )
+ continue;
+
+ // perform intersection
+ E_IntersectPoint* eip, *vip = 0;
+ for ( int iDirZ = 0; iDirZ < 3; ++iDirZ )
+ {
+ GridPlanes& planes = pln[ iDirZ ];
+ int iDirX = ( iDirZ + 1 ) % 3;
+ int iDirY = ( iDirZ + 2 ) % 3;
+ double xLen = _grid->_coords[ iDirX ].back() - _grid->_coords[ iDirX ][0];
+ double yLen = _grid->_coords[ iDirY ].back() - _grid->_coords[ iDirY ][0];
+ double zLen = _grid->_coords[ iDirZ ].back() - _grid->_coords[ iDirZ ][0];
+ int dIJK[3], d000[3] = { 0,0,0 };
+ double o[3] = { _grid->_coords[0][0],
+ _grid->_coords[1][0],
+ _grid->_coords[2][0] };
+
+ // locate the 1st point of a segment within the grid
+ gp_XYZ p1 = discret.Value( 1 ).XYZ();
+ double u1 = discret.Parameter( 1 );
+ double zProj1 = planes._zNorm * ( p1 - _grid->_origin );
+
+ _grid->ComputeUVW( p1, ip._uvw );
+ int iX1 = int(( ip._uvw[iDirX] - o[iDirX]) / xLen * (_grid->_coords[ iDirX ].size() - 1));
+ int iY1 = int(( ip._uvw[iDirY] - o[iDirY]) / yLen * (_grid->_coords[ iDirY ].size() - 1));
+ int iZ1 = int(( ip._uvw[iDirZ] - o[iDirZ]) / zLen * (_grid->_coords[ iDirZ ].size() - 1));
+ locateValue( iX1, ip._uvw[iDirX], _grid->_coords[ iDirX ], dIJK[ iDirX ], tol );
+ locateValue( iY1, ip._uvw[iDirY], _grid->_coords[ iDirY ], dIJK[ iDirY ], tol );
+ locateValue( iZ1, ip._uvw[iDirZ], _grid->_coords[ iDirZ ], dIJK[ iDirZ ], tol );
+
+ int ijk[3]; // grid index where a segment intersects a plane
+ ijk[ iDirX ] = iX1;
+ ijk[ iDirY ] = iY1;
+ ijk[ iDirZ ] = iZ1;
+
+ // add the 1st vertex point to a hexahedron
+ if ( iDirZ == 0 )
+ {
+ ip._point = p1;
+ ip._shapeID = _grid->ShapeID( v1 );
+ vip = _grid->Add( ip );
+ _grid->UpdateFacesOfVertex( *vip, v1 );
+ if ( isInternal )
+ vip->_faceIDs.push_back( _grid->PseudoIntExtFaceID() );
+ if ( !addIntersection( vip, hexes, ijk, d000 ))
+ _grid->Remove( vip );
+ ip._shapeID = edgeID;
+ }
+ for ( int iP = 2; iP <= discret.NbPoints(); ++iP )
+ {
+ // locate the 2nd point of a segment within the grid
+ gp_XYZ p2 = discret.Value( iP ).XYZ();
+ double u2 = discret.Parameter( iP );
+ double zProj2 = planes._zNorm * ( p2 - _grid->_origin );
+ int iZ2 = iZ1;
+ if ( Abs( zProj2 - zProj1 ) > std::numeric_limits<double>::min() )
+ {
+ locateValue( iZ2, zProj2, planes._zProjs, dIJK[ iDirZ ], tol );
+
+ // treat intersections with planes between 2 end points of a segment
+ int dZ = ( iZ1 <= iZ2 ) ? +1 : -1;
+ int iZ = iZ1 + ( iZ1 < iZ2 );
+ for ( int i = 0, nb = Abs( iZ1 - iZ2 ); i < nb; ++i, iZ += dZ )
+ {
+ ip._point = findIntPoint( u1, zProj1, u2, zProj2,
+ planes._zProjs[ iZ ],
+ curve, planes._zNorm, _grid->_origin );
+ _grid->ComputeUVW( ip._point.XYZ(), ip._uvw );
+ locateValue( ijk[iDirX], ip._uvw[iDirX], _grid->_coords[iDirX], dIJK[iDirX], tol );
+ locateValue( ijk[iDirY], ip._uvw[iDirY], _grid->_coords[iDirY], dIJK[iDirY], tol );
+ ijk[ iDirZ ] = iZ;
+
+ // add ip to hex "above" the plane
+ eip = _grid->Add( ip );
+ if ( isInternal )
+ eip->_faceIDs.push_back( _grid->PseudoIntExtFaceID() );
+ dIJK[ iDirZ ] = 0;
+ bool added = addIntersection( eip, hexes, ijk, dIJK);
+
+ // add ip to hex "below" the plane
+ ijk[ iDirZ ] = iZ-1;
+ if ( !addIntersection( eip, hexes, ijk, dIJK ) &&
+ !added )
+ _grid->Remove( eip );
+ }
+ }
+ iZ1 = iZ2;
+ p1 = p2;
+ u1 = u2;
+ zProj1 = zProj2;
+ }
+ // add the 2nd vertex point to a hexahedron
+ if ( iDirZ == 0 )
+ {
+ ip._point = p1;
+ ip._shapeID = _grid->ShapeID( v2 );
+ _grid->ComputeUVW( p1, ip._uvw );
+ locateValue( ijk[iDirX], ip._uvw[iDirX], _grid->_coords[iDirX], dIJK[iDirX], tol );
+ locateValue( ijk[iDirY], ip._uvw[iDirY], _grid->_coords[iDirY], dIJK[iDirY], tol );
+ ijk[ iDirZ ] = iZ1;
+ bool sameV = ( v1.IsSame( v2 ));
+ if ( !sameV )
+ {
+ vip = _grid->Add( ip );
+ _grid->UpdateFacesOfVertex( *vip, v2 );
+ if ( isInternal )
+ vip->_faceIDs.push_back( _grid->PseudoIntExtFaceID() );
+ }
+ if ( !addIntersection( vip, hexes, ijk, d000 ) && !sameV )
+ _grid->Remove( vip );
+ ip._shapeID = edgeID;
+ }
+ } // loop on 3 grid directions
+ } // loop on EDGEs
+
+
+ if ( intEdgeIDs.Size() > 0 )
+ cutByExtendedInternal( hexes, intEdgeIDs );
+
+ return;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Fully cut hexes that are partially cut by INTERNAL FACE.
+ * Cut them by extended INTERNAL FACE.
+ */
+ void Hexahedron::cutByExtendedInternal( std::vector< Hexahedron* >& hexes,
+ const TColStd_MapOfInteger& intEdgeIDs )
+ {
+ IntAna_IntConicQuad intersection;
+ SMESHDS_Mesh* meshDS = _grid->_helper->GetMeshDS();
+ const double tol2 = _grid->_tol * _grid->_tol;
+
+ for ( size_t iH = 0; iH < hexes.size(); ++iH )
+ {
+ Hexahedron* hex = hexes[ iH ];
+ if ( !hex || hex->_eIntPoints.size() < 2 )
+ continue;
+ if ( !intEdgeIDs.Contains( hex->_eIntPoints.back()->_shapeID ))
+ continue;
+
+ // get 3 points on INTERNAL FACE to construct a cutting plane
+ gp_Pnt p1 = hex->_eIntPoints[0]->_point;
+ gp_Pnt p2 = hex->_eIntPoints[1]->_point;
+ gp_Pnt p3 = hex->mostDistantInternalPnt( iH, p1, p2 );
+
+ gp_Vec norm = gp_Vec( p1, p2 ) ^ gp_Vec( p1, p3 );
+ gp_Pln pln;
+ try {
+ pln = gp_Pln( p1, norm );
+ }
+ catch(...)
+ {
+ continue;
+ }
+
+ TGeomID intFaceID = hex->_eIntPoints.back()->_faceIDs.front(); // FACE being "extended"
+ TGeomID solidID = _grid->GetSolid( intFaceID )->ID();
+
+ // cut links by the plane
+ //bool isCut = false;
+ for ( int iLink = 0; iLink < 12; ++iLink )
+ {
+ _Link& link = hex->_hexLinks[ iLink ];
+ if ( !link._fIntPoints.empty() )
+ {
+ // if ( link._fIntPoints[0]->_faceIDs.back() == _grid->PseudoIntExtFaceID() )
+ // isCut = true;
+ continue; // already cut link
+ }
+ if ( !link._nodes[0]->Node() ||
+ !link._nodes[1]->Node() )
+ continue; // outside link
+
+ if ( link._nodes[0]->IsOnFace( intFaceID ))
+ {
+ if ( link._nodes[0]->_intPoint->_faceIDs.back() != _grid->PseudoIntExtFaceID() )
+ if ( p1.SquareDistance( link._nodes[0]->Point() ) < tol2 ||
+ p2.SquareDistance( link._nodes[0]->Point() ) < tol2 )
+ link._nodes[0]->_intPoint->_faceIDs.push_back( _grid->PseudoIntExtFaceID() );
+ continue; // link is cut by FACE being "extended"
+ }
+ if ( link._nodes[1]->IsOnFace( intFaceID ))
+ {
+ if ( link._nodes[1]->_intPoint->_faceIDs.back() != _grid->PseudoIntExtFaceID() )
+ if ( p1.SquareDistance( link._nodes[1]->Point() ) < tol2 ||
+ p2.SquareDistance( link._nodes[1]->Point() ) < tol2 )
+ link._nodes[1]->_intPoint->_faceIDs.push_back( _grid->PseudoIntExtFaceID() );
+ continue; // link is cut by FACE being "extended"
+ }
+ gp_Pnt p4 = link._nodes[0]->Point();
+ gp_Pnt p5 = link._nodes[1]->Point();
+ gp_Lin line( p4, gp_Vec( p4, p5 ));
+
+ intersection.Perform( line, pln );
+ if ( !intersection.IsDone() ||
+ intersection.IsInQuadric() ||
+ intersection.IsParallel() ||
+ intersection.NbPoints() < 1 )
+ continue;
+
+ double u = intersection.ParamOnConic(1);
+ if ( u + _grid->_tol < 0 )
+ continue;
+ int iDir = iLink / 4;
+ int index = (&hex->_i)[iDir];
+ double linkLen = _grid->_coords[iDir][index+1] - _grid->_coords[iDir][index];
+ if ( u - _grid->_tol > linkLen )
+ continue;
+
+ if ( u < _grid->_tol ||
+ u > linkLen - _grid->_tol ) // intersection at grid node
+ {
+ int i = ! ( u < _grid->_tol ); // [0,1]
+ int iN = link._nodes[ i ] - hex->_hexNodes; // [0-7]
+
+ const F_IntersectPoint * & ip = _grid->_gridIntP[ hex->_origNodeInd +
+ _grid->_nodeShift[iN] ];
+ if ( !ip )
+ {
+ ip = _grid->_extIntPool.getNew();
+ ip->_faceIDs.push_back( _grid->PseudoIntExtFaceID() );
+ //ip->_transition = Trans_INTERNAL;
+ }
+ else if ( ip->_faceIDs.back() != _grid->PseudoIntExtFaceID() )
+ {
+ ip->_faceIDs.push_back( _grid->PseudoIntExtFaceID() );
+ }
+ hex->_nbFaceIntNodes++;
+ //isCut = true;
+ }
+ else
+ {
+ const gp_Pnt& p = intersection.Point( 1 );
+ F_IntersectPoint* ip = _grid->_extIntPool.getNew();
+ ip->_node = meshDS->AddNode( p.X(), p.Y(), p.Z() );
+ ip->_faceIDs.push_back( _grid->PseudoIntExtFaceID() );
+ ip->_transition = Trans_INTERNAL;
+ meshDS->SetNodeInVolume( ip->_node, solidID );
+
+ CellsAroundLink fourCells( _grid, iDir );
+ fourCells.Init( hex->_i, hex->_j, hex->_k, iLink );
+ int i,j,k, cellIndex;
+ for ( int iC = 0; iC < 4; ++iC ) // loop on 4 cells sharing the link
+ {
+ if ( !fourCells.GetCell( iC, i,j,k, cellIndex, iLink ))
+ continue;
+ Hexahedron * h = hexes[ cellIndex ];
+ if ( !h )
+ h = hexes[ cellIndex ] = new Hexahedron( *this, i, j, k, cellIndex );
+ h->_hexLinks[iLink]._fIntPoints.push_back( ip );
+ h->_nbFaceIntNodes++;
+ //isCut = true;
+ }
+ }
+ }
+
+ // if ( isCut )
+ // for ( size_t i = 0; i < hex->_eIntPoints.size(); ++i )
+ // {
+ // if ( _grid->IsInternal( hex->_eIntPoints[i]->_shapeID ) &&
+ // ! hex->_eIntPoints[i]->IsOnFace( _grid->PseudoIntExtFaceID() ))
+ // hex->_eIntPoints[i]->_faceIDs.push_back( _grid->PseudoIntExtFaceID() );
+ // }
+ continue;
+
+ } // loop on all hexes
+ return;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Return intersection point on INTERNAL FACE most distant from given ones
+ */
+ gp_Pnt Hexahedron::mostDistantInternalPnt( int hexIndex, const gp_Pnt& p1, const gp_Pnt& p2 )
+ {
+ gp_Pnt resultPnt = p1;
+
+ double maxDist2 = 0;
+ for ( int iLink = 0; iLink < 12; ++iLink ) // check links
+ {
+ _Link& link = _hexLinks[ iLink ];
+ for ( size_t i = 0; i < link._fIntPoints.size(); ++i )
+ if ( _grid->PseudoIntExtFaceID() != link._fIntPoints[i]->_faceIDs[0] &&
+ _grid->IsInternal( link._fIntPoints[i]->_faceIDs[0] ) &&
+ link._fIntPoints[i]->_node )
+ {
+ gp_Pnt p = SMESH_NodeXYZ( link._fIntPoints[i]->_node );
+ double d = p1.SquareDistance( p );
+ if ( d > maxDist2 )
+ {
+ resultPnt = p;
+ maxDist2 = d;
+ }
+ else
+ {
+ d = p2.SquareDistance( p );
+ if ( d > maxDist2 )
+ {
+ resultPnt = p;
+ maxDist2 = d;
+ }
+ }
+ }
+ }
+ setIJK( hexIndex );
+ _origNodeInd = _grid->NodeIndex( _i,_j,_k );
+
+ for ( size_t iN = 0; iN < 8; ++iN ) // check corners
+ {
+ _hexNodes[iN]._node = _grid->_nodes [ _origNodeInd + _grid->_nodeShift[iN] ];
+ _hexNodes[iN]._intPoint = _grid->_gridIntP[ _origNodeInd + _grid->_nodeShift[iN] ];
+ if ( _hexNodes[iN]._intPoint )
+ for ( size_t iF = 0; iF < _hexNodes[iN]._intPoint->_faceIDs.size(); ++iF )
+ {
+ if ( _grid->IsInternal( _hexNodes[iN]._intPoint->_faceIDs[iF]))
+ {
+ gp_Pnt p = SMESH_NodeXYZ( _hexNodes[iN]._node );
+ double d = p1.SquareDistance( p );
+ if ( d > maxDist2 )
+ {
+ resultPnt = p;
+ maxDist2 = d;
+ }
+ else
+ {
+ d = p2.SquareDistance( p );
+ if ( d > maxDist2 )
+ {
+ resultPnt = p;
+ maxDist2 = d;
+ }
+ }
+ }
+ }
+ }
+ if ( maxDist2 < _grid->_tol * _grid->_tol )
+ return p1;
+
+ return resultPnt;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Finds intersection of a curve with a plane
+ * \param [in] u1 - parameter of one curve point
+ * \param [in] proj1 - projection of the curve point to the plane normal
+ * \param [in] u2 - parameter of another curve point
+ * \param [in] proj2 - projection of the other curve point to the plane normal
+ * \param [in] proj - projection of a point where the curve intersects the plane
+ * \param [in] curve - the curve
+ * \param [in] axis - the plane normal
+ * \param [in] origin - the plane origin
+ * \return gp_Pnt - the found intersection point
+ */
+ gp_Pnt Hexahedron::findIntPoint( double u1, double proj1,
+ double u2, double proj2,
+ double proj,
+ BRepAdaptor_Curve& curve,
+ const gp_XYZ& axis,
+ const gp_XYZ& origin)
+ {
+ double r = (( proj - proj1 ) / ( proj2 - proj1 ));
+ double u = u1 * ( 1 - r ) + u2 * r;
+ gp_Pnt p = curve.Value( u );
+ double newProj = axis * ( p.XYZ() - origin );
+ if ( Abs( proj - newProj ) > _grid->_tol / 10. )
+ {
+ if ( r > 0.5 )
+ return findIntPoint( u2, proj2, u, newProj, proj, curve, axis, origin );
+ else
+ return findIntPoint( u1, proj2, u, newProj, proj, curve, axis, origin );
+ }
+ return p;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Returns indices of a hexahedron sub-entities holding a point
+ * \param [in] ip - intersection point
+ * \param [out] facets - 0-3 facets holding a point
+ * \param [out] sub - index of a vertex or an edge holding a point
+ * \return int - number of facets holding a point
+ */
+ int Hexahedron::getEntity( const E_IntersectPoint* ip, int* facets, int& sub )
+ {
+ enum { X = 1, Y = 2, Z = 4 }; // == 001, 010, 100
+ int nbFacets = 0;
+ int vertex = 0, edgeMask = 0;
+
+ if ( Abs( _grid->_coords[0][ _i ] - ip->_uvw[0] ) < _grid->_tol ) {
+ facets[ nbFacets++ ] = SMESH_Block::ID_F0yz;
+ edgeMask |= X;
+ }
+ else if ( Abs( _grid->_coords[0][ _i+1 ] - ip->_uvw[0] ) < _grid->_tol ) {
+ facets[ nbFacets++ ] = SMESH_Block::ID_F1yz;
+ vertex |= X;
+ edgeMask |= X;
+ }
+ if ( Abs( _grid->_coords[1][ _j ] - ip->_uvw[1] ) < _grid->_tol ) {
+ facets[ nbFacets++ ] = SMESH_Block::ID_Fx0z;
+ edgeMask |= Y;
+ }
+ else if ( Abs( _grid->_coords[1][ _j+1 ] - ip->_uvw[1] ) < _grid->_tol ) {
+ facets[ nbFacets++ ] = SMESH_Block::ID_Fx1z;
+ vertex |= Y;
+ edgeMask |= Y;
+ }
+ if ( Abs( _grid->_coords[2][ _k ] - ip->_uvw[2] ) < _grid->_tol ) {
+ facets[ nbFacets++ ] = SMESH_Block::ID_Fxy0;
+ edgeMask |= Z;
+ }
+ else if ( Abs( _grid->_coords[2][ _k+1 ] - ip->_uvw[2] ) < _grid->_tol ) {
+ facets[ nbFacets++ ] = SMESH_Block::ID_Fxy1;
+ vertex |= Z;
+ edgeMask |= Z;
+ }
+
+ switch ( nbFacets )
+ {
+ case 0: sub = 0; break;
+ case 1: sub = facets[0]; break;
+ case 2: {
+ const int edge [3][8] = {
+ { SMESH_Block::ID_E00z, SMESH_Block::ID_E10z,
+ SMESH_Block::ID_E01z, SMESH_Block::ID_E11z },
+ { SMESH_Block::ID_E0y0, SMESH_Block::ID_E1y0, 0, 0,
+ SMESH_Block::ID_E0y1, SMESH_Block::ID_E1y1 },
+ { SMESH_Block::ID_Ex00, 0, SMESH_Block::ID_Ex10, 0,
+ SMESH_Block::ID_Ex01, 0, SMESH_Block::ID_Ex11 }
+ };
+ switch ( edgeMask ) {
+ case X | Y: sub = edge[ 0 ][ vertex ]; break;
+ case X | Z: sub = edge[ 1 ][ vertex ]; break;
+ default: sub = edge[ 2 ][ vertex ];
+ }
+ break;
+ }
+ //case 3:
+ default:
+ sub = vertex + SMESH_Block::ID_FirstV;
+ }
+
+ return nbFacets;
+ }
+ //================================================================================
+ /*!
+ * \brief Adds intersection with an EDGE
+ */
+ bool Hexahedron::addIntersection( const E_IntersectPoint* ip,
+ vector< Hexahedron* >& hexes,
+ int ijk[], int dIJK[] )
+ {
+ bool added = false;
+
+ size_t hexIndex[4] = {
+ _grid->CellIndex( ijk[0], ijk[1], ijk[2] ),
+ dIJK[0] ? _grid->CellIndex( ijk[0]+dIJK[0], ijk[1], ijk[2] ) : -1,
+ dIJK[1] ? _grid->CellIndex( ijk[0], ijk[1]+dIJK[1], ijk[2] ) : -1,
+ dIJK[2] ? _grid->CellIndex( ijk[0], ijk[1], ijk[2]+dIJK[2] ) : -1
+ };
+ for ( int i = 0; i < 4; ++i )
+ {
+ if ( hexIndex[i] < hexes.size() && hexes[ hexIndex[i] ] )
+ {
+ Hexahedron* h = hexes[ hexIndex[i] ];
+ h->_eIntPoints.reserve(2);
+ h->_eIntPoints.push_back( ip );
+ added = true;
+#ifdef _DEBUG_
+ // check if ip is really inside the hex
+ if ( h->isOutParam( ip->_uvw ))
+ throw SALOME_Exception("ip outside a hex");
+#endif
+ }
+ }
+ return added;
+ }
+ //================================================================================
+ /*!
+ * \brief Check if a hexahedron facet lies on a FACE
+ * Also return true if the facet does not interfere with any FACE
+ */
+ bool Hexahedron::isQuadOnFace( const size_t iQuad )
+ {
+ _Face& quad = _hexQuads[ iQuad ] ;
+
+ int nbGridNodesInt = 0; // nb FACE intersections at grid nodes
+ int nbNoGeomNodes = 0;
+ for ( int iE = 0; iE < 4; ++iE )
+ {
+ nbNoGeomNodes = ( !quad._links[ iE ].FirstNode()->_intPoint &&
+ quad._links[ iE ].NbResultLinks() == 1 );
+ nbGridNodesInt +=
+ ( quad._links[ iE ].FirstNode()->_intPoint &&
+ quad._links[ iE ].NbResultLinks() == 1 &&
+ quad._links[ iE ].ResultLink( 0 ).FirstNode() == quad._links[ iE ].FirstNode() &&
+ quad._links[ iE ].ResultLink( 0 ).LastNode() == quad._links[ iE ].LastNode() );
+ }
+ if ( nbNoGeomNodes == 4 )
+ return true;
+
+ if ( nbGridNodesInt == 4 ) // all quad nodes are at FACE intersection
+ {
+ size_t iEmin = 0, minNbFaces = 1000;
+ for ( int iE = 0; iE < 4; ++iE ) // look for a node with min nb FACEs
+ {
+ size_t nbFaces = quad._links[ iE ].FirstNode()->faces().size();
+ if ( minNbFaces > nbFaces )
+ {
+ iEmin = iE;
+ minNbFaces = nbFaces;
+ }
+ }
+ // check if there is a FACE passing through all 4 nodes
+ for ( const TGeomID& faceID : quad._links[ iEmin ].FirstNode()->faces() )
+ {
+ bool allNodesAtFace = true;
+ for ( size_t iE = 0; iE < 4 && allNodesAtFace; ++iE )
+ allNodesAtFace = ( iE == iEmin ||
+ quad._links[ iE ].FirstNode()->IsOnFace( faceID ));
+ if ( allNodesAtFace ) // quad if on faceID
+ return true;
+ }
+ }
+ return false;
+ }
+ //================================================================================
+ /*!
+ * \brief Finds nodes at a path from one node to another via intersections with EDGEs
+ */
+ bool Hexahedron::findChain( _Node* n1,
+ _Node* n2,
+ _Face& quad,
+ vector<_Node*>& chn )
+ {
+ chn.clear();
+ chn.push_back( n1 );
+ for ( size_t iP = 0; iP < quad._eIntNodes.size(); ++iP )
+ if ( !quad._eIntNodes[ iP ]->IsUsedInFace( &quad ) &&
+ n1->IsLinked( quad._eIntNodes[ iP ]->_intPoint ) &&
+ n2->IsLinked( quad._eIntNodes[ iP ]->_intPoint ))
+ {
+ chn.push_back( quad._eIntNodes[ iP ]);
+ chn.push_back( n2 );
+ quad._eIntNodes[ iP ]->_usedInFace = &quad;
+ return true;
+ }
+ bool found;
+ do
+ {
+ found = false;
+ for ( size_t iP = 0; iP < quad._eIntNodes.size(); ++iP )
+ if ( !quad._eIntNodes[ iP ]->IsUsedInFace( &quad ) &&
+ chn.back()->IsLinked( quad._eIntNodes[ iP ]->_intPoint ))
+ {
+ chn.push_back( quad._eIntNodes[ iP ]);
+ found = ( quad._eIntNodes[ iP ]->_usedInFace = &quad );
+ break;
+ }
+ } while ( found && ! chn.back()->IsLinked( n2->_intPoint ) );
+
+ if ( chn.back() != n2 && chn.back()->IsLinked( n2->_intPoint ))
+ chn.push_back( n2 );
+
+ return chn.size() > 1;
+ }
+ //================================================================================
+ /*!
+ * \brief Try to heal a polygon whose ends are not connected
+ */
+ bool Hexahedron::closePolygon( _Face* polygon, vector<_Node*>& chainNodes ) const
+ {
+ int i = -1, nbLinks = polygon->_links.size();
+ if ( nbLinks < 3 )
+ return false;
+ vector< _OrientedLink > newLinks;
+ // find a node lying on the same FACE as the last one
+ _Node* node = polygon->_links.back().LastNode();
+ TGeomID avoidFace = node->IsLinked( polygon->_links.back().FirstNode()->_intPoint );
+ for ( i = nbLinks - 2; i >= 0; --i )
+ if ( node->IsLinked( polygon->_links[i].FirstNode()->_intPoint, avoidFace ))
+ break;
+ if ( i >= 0 )
+ {
+ for ( ; i < nbLinks; ++i )
+ newLinks.push_back( polygon->_links[i] );
+ }
+ else
+ {
+ // find a node lying on the same FACE as the first one
+ node = polygon->_links[0].FirstNode();
+ avoidFace = node->IsLinked( polygon->_links[0].LastNode()->_intPoint );
+ for ( i = 1; i < nbLinks; ++i )
+ if ( node->IsLinked( polygon->_links[i].LastNode()->_intPoint, avoidFace ))
+ break;
+ if ( i < nbLinks )
+ for ( nbLinks = i + 1, i = 0; i < nbLinks; ++i )
+ newLinks.push_back( polygon->_links[i] );
+ }
+ if ( newLinks.size() > 1 )
+ {
+ polygon->_links.swap( newLinks );
+ chainNodes.clear();
+ chainNodes.push_back( polygon->_links.back().LastNode() );
+ chainNodes.push_back( polygon->_links[0].FirstNode() );
+ return true;
+ }
+ return false;
+ }
+ //================================================================================
+ /*!
+ * \brief Finds nodes on the same EDGE as the first node of avoidSplit.
+ *
+ * This function is for
+ * 1) a case where an EDGE lies on a quad which lies on a FACE
+ * so that a part of quad in ON and another part is IN
+ * 2) INTERNAL FACE passes through the 1st node of avoidSplit
+ */
+ bool Hexahedron::findChainOnEdge( const vector< _OrientedLink >& splits,
+ const _OrientedLink& prevSplit,
+ const _OrientedLink& avoidSplit,
+ const std::set< TGeomID > & concaveFaces,
+ size_t & iS,
+ _Face& quad,
+ vector<_Node*>& chn )
+ {
+ _Node* pn1 = prevSplit.FirstNode();
+ _Node* pn2 = prevSplit.LastNode(); // pn2 is on EDGE, if not on INTERNAL FACE
+ _Node* an3 = avoidSplit.LastNode();
+ TGeomID avoidFace = pn1->IsLinked( pn2->_intPoint ); // FACE under the quad
+ if ( avoidFace < 1 && pn1->_intPoint )
+ return false;
+
+ chn.clear();
+
+ if ( !quad._eIntNodes.empty() ) // connect pn2 with EDGE intersections
+ {
+ chn.push_back( pn2 );
+ bool found;
+ do
+ {
+ found = false;
+ for ( size_t iP = 0; iP < quad._eIntNodes.size(); ++iP )
+ if (( !quad._eIntNodes[ iP ]->IsUsedInFace( &quad )) &&
+ ( chn.back()->IsLinked( quad._eIntNodes[ iP ]->_intPoint, avoidFace )) &&
+ ( !avoidFace || quad._eIntNodes[ iP ]->IsOnFace( avoidFace )))
+ {
+ chn.push_back( quad._eIntNodes[ iP ]);
+ found = ( quad._eIntNodes[ iP ]->_usedInFace = &quad );
+ break;
+ }
+ } while ( found );
+ pn2 = chn.back();
+ }
+
+ _Node* n = 0, *stopNode = avoidSplit.LastNode();
+
+ if ( pn2 == prevSplit.LastNode() && // pn2 is at avoidSplit.FirstNode()
+ !isCorner( stopNode )) // stopNode is in the middle of a _hexLinks
+ {
+ // move stopNode to a _hexNodes
+ for ( int iE = 0; iE < 4; ++iE ) // loop on 4 sides of a quadrangle
+ for ( size_t iL = 0; iL < quad._links[ iE ].NbResultLinks(); ++iL )
+ {
+ const _Link* sideSplit = & quad._links[ iE ]._link->_splits[ iL ];
+ if ( sideSplit == avoidSplit._link )
+ {
+ if ( quad._links[ iE ].LastNode()->Node() )
+ stopNode = quad._links[ iE ].LastNode();
+ iE = 4;
+ break;
+ }
+ }
+ }
+
+ // connect pn2 (probably new, at _eIntNodes) with a split
+
+ int i, iConn;
+ size_t nbCommon;
+ TGeomID commonFaces[20];
+ _Node* nPrev = nullptr;
+ for ( i = splits.size()-1; i >= 0; --i )
+ {
+ if ( !splits[i] )
+ continue;
+
+ bool stop = false;
+ for ( int is1st = 0; is1st < 2; ++is1st )
+ {
+ _Node* nConn = is1st ? splits[i].FirstNode() : splits[i].LastNode();
+ if ( nConn == nPrev )
+ {
+ if ( n == nConn )
+ iConn = i;
+ continue;
+ }
+ nPrev = nConn;
+ if (( stop = ( nConn == stopNode )))
+ break;
+ // find a FACE connecting nConn with pn2 but not with an3
+ if (( nConn != pn1 ) &&
+ ( nConn->_intPoint && !nConn->_intPoint->_faceIDs.empty() ) &&
+ ( nbCommon = nConn->GetCommonFaces( pn2->_intPoint, commonFaces )))
+ {
+ bool a3Coonect = true;
+ for ( size_t iF = 0; iF < nbCommon && a3Coonect; ++iF )
+ a3Coonect = an3->IsOnFace( commonFaces[ iF ]) || concaveFaces.count( commonFaces[ iF ]);
+ if ( a3Coonect )
+ continue;
+
+ if ( !n )
+ {
+ n = nConn;
+ iConn = i + !is1st;
+ }
+ if ( nbCommon > 1 ) // nConn is linked with pn2 by an EDGE
+ {
+ n = nConn;
+ iConn = i + !is1st;
+ stop = true;
+ break;
+ }
+ }
+ }
+ if ( stop )
+ {
+ i = iConn;
+ break;
+ }
+ }
+
+ if ( n && n != stopNode )
+ {
+ if ( chn.empty() )
+ chn.push_back( pn2 );
+ chn.push_back( n );
+ iS = i-1;
+ return true;
+ }
+ else if ( !chn.empty() && chn.back()->_isInternalFlags )
+ {
+ // INTERNAL FACE partially cuts the quad
+ for ( int ip = chn.size() - 2; ip >= 0; --ip )
+ chn.push_back( chn[ ip ]);
+ return true;
+ }
+ return false;
+ }
+ //================================================================================
+ /*!
+ * \brief Checks transition at the ginen intersection node of a link
+ */
+ bool Hexahedron::isOutPoint( _Link& link, int iP,
+ SMESH_MesherHelper& helper, const Solid* solid ) const
+ {
+ bool isOut = false;
+
+ if ( link._fIntNodes[iP]->faces().size() == 1 &&
+ _grid->IsInternal( link._fIntNodes[iP]->face(0) ))
+ return false;
+
+ const bool moreIntPoints = ( iP+1 < (int) link._fIntNodes.size() );
+
+ // get 2 _Node's
+ _Node* n1 = link._fIntNodes[ iP ];
+ if ( !n1->Node() )
+ n1 = link._nodes[0];
+ _Node* n2 = moreIntPoints ? link._fIntNodes[ iP+1 ] : 0;
+ if ( !n2 || !n2->Node() )
+ n2 = link._nodes[1];
+ if ( !n2->Node() )
+ return true;
+
+ // get all FACEs under n1 and n2
+ set< TGeomID > faceIDs;
+ if ( moreIntPoints ) faceIDs.insert( link._fIntNodes[iP+1]->faces().begin(),
+ link._fIntNodes[iP+1]->faces().end() );
+ if ( n2->_intPoint ) faceIDs.insert( n2->_intPoint->_faceIDs.begin(),
+ n2->_intPoint->_faceIDs.end() );
+ if ( faceIDs.empty() )
+ return false; // n2 is inside
+ if ( n1->_intPoint ) faceIDs.insert( n1->_intPoint->_faceIDs.begin(),
+ n1->_intPoint->_faceIDs.end() );
+ faceIDs.insert( link._fIntNodes[iP]->faces().begin(),
+ link._fIntNodes[iP]->faces().end() );
+
+ // get a point between 2 nodes
+ gp_Pnt p1 = n1->Point();
+ gp_Pnt p2 = n2->Point();
+ gp_Pnt pOnLink = 0.8 * p1.XYZ() + 0.2 * p2.XYZ();
+
+ TopLoc_Location loc;
+
+ set< TGeomID >::iterator faceID = faceIDs.begin();
+ for ( ; faceID != faceIDs.end(); ++faceID )
+ {
+ // project pOnLink on a FACE
+ if ( *faceID < 1 || !solid->Contains( *faceID )) continue;
+ const TopoDS_Face& face = TopoDS::Face( _grid->Shape( *faceID ));
+ GeomAPI_ProjectPointOnSurf& proj = helper.GetProjector( face, loc, 0.1*_grid->_tol );
+ gp_Pnt testPnt = pOnLink.Transformed( loc.Transformation().Inverted() );
+ proj.Perform( testPnt );
+ if ( proj.IsDone() && proj.NbPoints() > 0 )
+ {
+ Standard_Real u,v;
+ proj.LowerDistanceParameters( u,v );
+
+ if ( proj.LowerDistance() <= 0.1 * _grid->_tol )
+ {
+ isOut = false;
+ }
+ else
+ {
+ // find isOut by normals
+ gp_Dir normal;
+ if ( GeomLib::NormEstim( BRep_Tool::Surface( face, loc ),
+ gp_Pnt2d( u,v ),
+ 0.1*_grid->_tol,
+ normal ) < 3 )
+ {
+ if ( solid->Orientation( face ) == TopAbs_REVERSED )
+ normal.Reverse();
+ gp_Vec v( proj.NearestPoint(), testPnt );
+ isOut = ( v * normal > 0 );
+ }
+ }
+ if ( !isOut )
+ {
+ // classify a projection
+ if ( !n1->IsOnFace( *faceID ) || !n2->IsOnFace( *faceID ))
+ {
+ BRepTopAdaptor_FClass2d cls( face, Precision::Confusion() );
+ TopAbs_State state = cls.Perform( gp_Pnt2d( u,v ));
+ if ( state == TopAbs_OUT )
+ {
+ isOut = true;
+ continue;
+ }
+ }
+ return false;
+ }
+ }
+ }
+ return isOut;
+ }
+ //================================================================================
+ /*!
+ * \brief Sort nodes on a FACE
+ */
+ void Hexahedron::sortVertexNodes(vector<_Node*>& nodes, _Node* curNode, TGeomID faceID)
+ {
+ 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, /*v=*/nullptr,/*unset=*/true );
+ nodes[iN]->setIsMarked( true );
+ }
+ } // loop to get nodes
+
+ const SMDS_MeshElement* v = 0;
+ if ( !volDef->_quantities.empty() )
+ {
+ v = helper.AddPolyhedralVolume( nodes, volDef->_quantities );
+ volDef->_size = SMDS_VolumeTool( v ).GetSize();
+ if ( volDef->_size < 0 ) // invalid polyhedron
+ {
+ if ( ! SMESH_MeshEditor( helper.GetMesh() ).Reorient( v ) || // try to fix
+ SMDS_VolumeTool( v ).GetSize() < 0 )
+ {
+ helper.GetMeshDS()->RemoveFreeElement( v, /*sm=*/nullptr, /*fromGroups=*/false );
+ v = nullptr;
+ //_hasTooSmall = true;
+#ifdef _DEBUG_
+ std::cout << "Remove INVALID polyhedron, _cellID = " << _cellID
+ << " ijk = ( " << _i << " " << _j << " " << _k << " ) "
+ << " solid " << volDef->_solidID << std::endl;
+#endif
+ }
+ }
+ }
+ 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;
+ }
+ }
+ volDef->_volume = v;
+ nbAdded += bool( v );
+
+ } // loop on _volumeDefs chain
+
+ // avoid creating overlapping volumes (bos #24052)
+ if ( nbAdded > 1 )
+ {
+ double sumSize = 0, maxSize = 0;
+ _volumeDef* maxSizeDef = nullptr;
+ for ( _volumeDef* volDef = &_volumeDefs; volDef; volDef = volDef->_next )
+ {
+ if ( !volDef->_volume )
+ continue;
+ sumSize += volDef->_size;
+ if ( volDef->_size > maxSize )
+ {
+ maxSize = volDef->_size;
+ maxSizeDef = volDef;
+ }
+ }
+ if ( sumSize > _sideLength[0] * _sideLength[1] * _sideLength[2] * 1.05 )
+ {
+ for ( _volumeDef* volDef = &_volumeDefs; volDef; volDef = volDef->_next )
+ if ( volDef != maxSizeDef && volDef->_volume )
+ {
+ helper.GetMeshDS()->RemoveFreeElement( volDef->_volume, /*sm=*/nullptr,
+ /*fromGroups=*/false );
+ volDef->_volume = nullptr;
+ //volDef->_nodes.clear();
+ --nbAdded;
+ }
+ }
+ }
+
+ for ( _volumeDef* volDef = &_volumeDefs; volDef; volDef = volDef->_next )
+ {
+ if ( volDef->_volume )
+ {
+ helper.GetMeshDS()->SetMeshElementOnShape( volDef->_volume, volDef->_solidID );
+ }
+ }
+
+ 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, double & volume) const
+ {
+ volume = 0;
+
+ 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;
+ }
+ }
+ 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;
+
+ if ( this->hasStrangeEdge() && volume > 1e-13 )
+ return true;
+
+ 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 Return true if there are _eIntPoints at EDGEs forming a concave corner
+ */
+ bool Hexahedron::hasEdgesAround( const ConcaveFace* cf ) const
+ {
+ int nbEdges = 0;
+ ConcaveFace foundGeomHolder;
+ for ( const E_IntersectPoint* ip : _eIntPoints )
+ {
+ if ( cf->HasEdge( ip->_shapeID ))
+ {
+ if ( ++nbEdges == 2 )
+ return true;
+ foundGeomHolder.SetEdge( ip->_shapeID );
+ }
+ else if ( ip->_faceIDs.size() >= 3 )
+ {
+ const TGeomID & vID = ip->_shapeID;
+ if ( cf->HasVertex( vID ) && !foundGeomHolder.HasVertex( vID ))
+ {
+ if ( ++nbEdges == 2 )
+ return true;
+ foundGeomHolder.SetVertex( vID );
+ }
+ }
+ }
+
+ for ( const _Node& hexNode: _hexNodes )
+ {
+ if ( !hexNode._node || !hexNode._intPoint )
+ continue;
+ const B_IntersectPoint* ip = hexNode._intPoint;
+ if ( ip->_faceIDs.size() == 2 ) // EDGE
+ {
+ TGeomID edgeID = hexNode._node->GetShapeID();
+ if ( cf->HasEdge( edgeID ) && !foundGeomHolder.HasEdge( edgeID ))
+ {
+ foundGeomHolder.SetEdge( edgeID );
+ if ( ++nbEdges == 2 )
+ return true;
+ }
+ }
+ else if ( ip->_faceIDs.size() >= 3 ) // VERTEX
+ {
+ TGeomID vID = hexNode._node->GetShapeID();
+ if ( cf->HasVertex( vID ) && !foundGeomHolder.HasVertex( vID ))
+ {
+ if ( ++nbEdges == 2 )
+ return true;
+ foundGeomHolder.SetVertex( vID );
+ }
+ }
+ }
+
+ 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 Find existing triangulation of a polygon
+ */
+ int findExistingTriangulation( const SMDS_MeshElement* polygon,
+ //const SMDS_Mesh* mesh,
+ std::vector< const SMDS_MeshNode* >& nodes )
+ {
+ int nbSplits = 0;
+ nodes.clear();
+ std::vector<const SMDS_MeshNode *> twoNodes(2);
+ std::vector<const SMDS_MeshElement *> foundFaces; foundFaces.reserve(10);
+ std::set< const SMDS_MeshElement * > avoidFaces; avoidFaces.insert( polygon );
+
+ const int nbPolyNodes = polygon->NbCornerNodes();
+ twoNodes[1] = polygon->GetNode( nbPolyNodes - 1 );
+ for ( int iN = 0; iN < nbPolyNodes; ++iN ) // loop on border links of polygon
+ {
+ twoNodes[0] = polygon->GetNode( iN );
+
+ int nbFaces = SMDS_Mesh::GetElementsByNodes( twoNodes, foundFaces, SMDSAbs_Face );
+ int nbOkFaces = 0;
+ for ( int iF = 0; iF < nbFaces; ++iF ) // keep faces lying over polygon
+ {
+ if ( avoidFaces.count( foundFaces[ iF ]))
+ continue;
+ int i, nbFaceNodes = foundFaces[ iF ]->NbCornerNodes();
+ for ( i = 0; i < nbFaceNodes; ++i )
+ {
+ const SMDS_MeshNode* n = foundFaces[ iF ]->GetNode( i );
+ bool isCommonNode = ( n == twoNodes[0] ||
+ n == twoNodes[1] ||
+ polygon->GetNodeIndex( n ) >= 0 );
+ if ( !isCommonNode )
+ break;
+ }
+ if ( i == nbFaceNodes ) // all nodes of foundFaces[iF] are shared with polygon
+ if ( nbOkFaces++ != iF )
+ foundFaces[ nbOkFaces-1 ] = foundFaces[ iF ];
+ }
+ if ( nbOkFaces > 0 )
+ {
+ int iFaceSelected = 0;
+ if ( nbOkFaces > 1 ) // select a face with minimal distance from polygon
+ {
+ double minDist = Precision::Infinite();
+ for ( int iF = 0; iF < nbOkFaces; ++iF )
+ {
+ int i, nbFaceNodes = foundFaces[ iF ]->NbCornerNodes();
+ gp_XYZ gc = SMESH_NodeXYZ( foundFaces[ iF ]->GetNode( 0 ));
+ for ( i = 1; i < nbFaceNodes; ++i )
+ gc += SMESH_NodeXYZ( foundFaces[ iF ]->GetNode( i ));
+ gc /= nbFaceNodes;
+
+ double dist = SMESH_MeshAlgos::GetDistance( polygon, gc );
+ if ( dist < minDist )
+ {
+ minDist = dist;
+ iFaceSelected = iF;
+ }
+ }
+ }
+ if ( foundFaces[ iFaceSelected ]->NbCornerNodes() != 3 )
+ return 0;
+ nodes.insert( nodes.end(),
+ foundFaces[ iFaceSelected ]->begin_nodes(),
+ foundFaces[ iFaceSelected ]->end_nodes());
+ if ( !SMESH_MeshAlgos::IsRightOrder( foundFaces[ iFaceSelected ],
+ twoNodes[0], twoNodes[1] ))
+ {
+ // reverse just added nodes
+ std::reverse( nodes.end() - 3, nodes.end() );
+ }
+ avoidFaces.insert( foundFaces[ iFaceSelected ]);
+ nbSplits++;
+ }
+
+ twoNodes[1] = twoNodes[0];
+
+ } // loop on polygon nodes
+
+ return nbSplits;
+ }
+ //================================================================================
+ /*!
+ * \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);
+ bool triangulationExist = false;
+ int nbTrias = findExistingTriangulation( polygon, face.myNodes );
+ if ( nbTrias > 0 )
+ triangulationExist = true;
+ else
+ 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 );
+ if ( !triangulationExist )
+ {
+ 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 );
+ if ( !triangulationExist )
+ {
+ 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 Look for a FACE supporting all given nodes made on EDGEs and VERTEXes
+ */
+ TGeomID findCommonFace( const std::vector< const SMDS_MeshNode* > & nn,
+ const SMESH_Mesh* mesh )
+ {
+ TGeomID faceID = 0;
+ TGeomID shapeIDs[20];
+ for ( size_t iN = 0; iN < nn.size(); ++iN )
+ shapeIDs[ iN ] = nn[ iN ]->GetShapeID();
+
+ SMESH_subMesh* sm = mesh->GetSubMeshContaining( shapeIDs[ 0 ]);
+ for ( const SMESH_subMesh * smFace : sm->GetAncestors() )
+ {
+ if ( smFace->GetSubShape().ShapeType() != TopAbs_FACE )
+ continue;
+
+ faceID = smFace->GetId();
+
+ for ( size_t iN = 1; iN < nn.size() && faceID; ++iN )
+ {
+ if ( !smFace->DependsOn( shapeIDs[ iN ]))
+ faceID = 0;
+ }
+ if ( faceID > 0 )
+ break;
+ }
+ return faceID;
+ }
+ //================================================================================
+ /*!
+ * \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++ )
+ {
+ const SMDS_MeshElement* otherVol;
+ bool isBoundary = vTool.IsFreeFace( iF, &otherVol );
+ if ( isBoundary )
+ {
+ bndFacets.push_back( iF );
+ }
+ else if (( hasInternal ) ||
+ ( !_grid->IsSolid( otherVol->GetShapeID() )))
+ {
+ // 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();
+ }
+ if ( faceID == 0 )
+ faceID = findCommonFace( face.myNodes, helper.GetMesh() );
+
+ 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;
+ }
+ if ( !faceID && !isBoundary )
+ continue;
+ }
+
+ // orient a new face according to supporting FACE orientation in shape_to_mesh
+ if ( !isBoundary && !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;
+ }
+ }
+
+ if ( faceID && nbFaceNodes > 4 &&
+ !_grid->IsInternal( faceID ) &&
+ !_grid->IsShared( faceID ) &&
+ !_grid->IsBoundaryFace( faceID ))
+ {
+ // split a polygon that will be used by other 3D algorithm
+ if ( !existFace )
+ 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++ )
+ {
+ if ( mesh->FindEdge( nodes[i-1], nodes[i] ))
+ continue;
+ 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.