+ //================================================================================
+ /*!
+ * \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 id 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 ))
+ 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 Set to _hexLinks a next portion of splits located on one side of INTERNAL FACEs
+ */
+ bool Hexahedron::_SplitIterator::Next()
+ {
+ if ( _iterationNb > 0 )
+ // count used splits
+ for ( size_t i = 0; i < _splits.size(); ++i )
+ {
+ if ( _splits[i]._iCheckIteration == _iterationNb )
+ {
+ _splits[i]._isUsed = _splits[i]._checkedSplit->_faces[1];
+ _nbUsed += _splits[i]._isUsed;
+ }
+ if ( !More() )
+ return false;
+ }
+
+ ++_iterationNb;
+
+ bool toTestUsed = ( _nbChecked >= _splits.size() );
+ if ( toTestUsed )
+ {
+ // all splits are checked; find all not used splits
+ for ( size_t i = 0; i < _splits.size(); ++i )
+ if ( !_splits[i].IsCheckedOrUsed( toTestUsed ))
+ _splits[i]._iCheckIteration = _iterationNb;
+
+ _nbUsed = _splits.size(); // to stop iteration
+ }
+ else
+ {
+ // get any not used/checked split to start from
+ _freeNodes.clear();
+ for ( size_t i = 0; i < _splits.size(); ++i )
+ {
+ if ( !_splits[i].IsCheckedOrUsed( toTestUsed ))
+ {
+ _freeNodes.push_back( _splits[i]._nodes[0] );
+ _freeNodes.push_back( _splits[i]._nodes[1] );
+ _splits[i]._iCheckIteration = _iterationNb;
+ break;
+ }
+ }
+ // find splits connected to the start one via _freeNodes
+ for ( size_t iN = 0; iN < _freeNodes.size(); ++iN )
+ {
+ for ( size_t iS = 0; iS < _splits.size(); ++iS )
+ {
+ if ( _splits[iS].IsCheckedOrUsed( toTestUsed ))
+ continue;
+ int iN2 = -1;
+ if ( _freeNodes[iN] == _splits[iS]._nodes[0] )
+ iN2 = 1;
+ else if ( _freeNodes[iN] == _splits[iS]._nodes[1] )
+ iN2 = 0;
+ else
+ continue;
+ if ( _freeNodes[iN]->_isInternalFlags > 0 )
+ {
+ if ( _splits[iS]._nodes[ iN2 ]->_isInternalFlags == 0 )
+ continue;
+ if ( !_splits[iS]._nodes[ iN2 ]->IsLinked( _freeNodes[iN]->_intPoint ))
+ continue;
+ }
+ _splits[iS]._iCheckIteration = _iterationNb;
+ _freeNodes.push_back( _splits[iS]._nodes[ iN2 ]);
+ }
+ }
+ }
+ // set splits to hex links
+
+ for ( int iL = 0; iL < 12; ++iL )
+ _hexLinks[ iL ]._splits.clear();
+
+ _Link split;
+ for ( size_t i = 0; i < _splits.size(); ++i )
+ {
+ if ( _splits[i]._iCheckIteration == _iterationNb )
+ {
+ split._nodes[0] = _splits[i]._nodes[0];
+ split._nodes[1] = _splits[i]._nodes[1];
+ _Link & hexLink = _hexLinks[ _splits[i]._linkID ];
+ hexLink._splits.push_back( split );
+ _splits[i]._checkedSplit = & hexLink._splits.back();
+ ++_nbChecked;
+ }
+ }
+ return More();
+ }