+ //================================================================================
+ /*!
+ * \brief Fix bad faces by smoothing
+ */
+ //================================================================================
+
+ bool fixDistortedFaces( SMESH_MesherHelper& helper,
+ TSideVector& tgtWires )
+ {
+ SMESH_subMesh* faceSM = helper.GetMesh()->GetSubMesh( helper.GetSubShape() );
+
+ if ( helper.IsDistorted2D( faceSM, /*checkUV=*/false ))
+ {
+ SMESH_MeshEditor editor( helper.GetMesh() );
+ SMESHDS_SubMesh* smDS = faceSM->GetSubMeshDS();
+ const TopoDS_Face& F = TopoDS::Face( faceSM->GetSubShape() );
+
+ TIDSortedElemSet faces;
+ SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
+ for ( faceIt = smDS->GetElements(); faceIt->more(); )
+ faces.insert( faces.end(), faceIt->next() );
+
+ // choose smoothing algo
+ //SMESH_MeshEditor:: SmoothMethod algo = SMESH_MeshEditor::CENTROIDAL;
+ bool isConcaveBoundary = false;
+ for ( size_t iW = 0; iW < tgtWires.size() && !isConcaveBoundary; ++iW )
+ {
+ TopoDS_Edge prevEdge = tgtWires[iW]->Edge( tgtWires[iW]->NbEdges() - 1 );
+ for ( int iE = 0; iE < tgtWires[iW]->NbEdges() && !isConcaveBoundary; ++iE )
+ {
+ double angle = helper.GetAngle( prevEdge, tgtWires[iW]->Edge( iE ),
+ F, tgtWires[iW]->FirstVertex( iE ));
+ isConcaveBoundary = ( angle < -5. * M_PI / 180. );
+
+ prevEdge = tgtWires[iW]->Edge( iE );
+ }
+ }
+ SMESH_MeshEditor:: SmoothMethod algo =
+ isConcaveBoundary ? SMESH_MeshEditor::CENTROIDAL : SMESH_MeshEditor::LAPLACIAN;
+
+ // smooth in 2D or 3D?
+ TopLoc_Location loc;
+ Handle(Geom_Surface) surface = BRep_Tool::Surface( F, loc );
+ bool isPlanar = GeomLib_IsPlanarSurface( surface ).IsPlanar();
+
+ // smoothing
+ set<const SMDS_MeshNode*> fixedNodes;
+ editor.Smooth( faces, fixedNodes, algo, /*nbIterations=*/ 10,
+ /*theTgtAspectRatio=*/1.0, /*the2D=*/!isPlanar);
+
+ helper.ToFixNodeParameters( true );
+
+ return !helper.IsDistorted2D( faceSM, /*checkUV=*/true );
+ }
+ return true;
+ }
+