+//=======================================================================
+//function : IsDistorted2D
+//purpose : Return true if 2D mesh on FACE is ditorted
+//=======================================================================
+
+bool SMESH_MesherHelper::IsDistorted2D( SMESH_subMesh* faceSM,
+ bool checkUV)
+{
+ if ( !faceSM || faceSM->GetSubShape().ShapeType() != TopAbs_FACE )
+ return false;
+
+ bool haveBadFaces = false;
+
+ SMESH_MesherHelper helper( *faceSM->GetFather() );
+ helper.SetSubShape( faceSM->GetSubShape() );
+
+ const TopoDS_Face& F = TopoDS::Face( faceSM->GetSubShape() );
+ SMESHDS_SubMesh* smDS = helper.GetMeshDS()->MeshElements( F );
+ if ( !smDS || smDS->NbElements() == 0 ) return false;
+
+ SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
+ double prevArea = 0;
+ vector< const SMDS_MeshNode* > nodes;
+ vector< gp_XY > uv;
+ bool* toCheckUV = checkUV ? & checkUV : 0;
+ while ( faceIt->more() && !haveBadFaces )
+ {
+ const SMDS_MeshElement* face = faceIt->next();
+
+ // get nodes
+ nodes.resize( face->NbCornerNodes() );
+ SMDS_MeshElement::iterator n = face->begin_nodes();
+ for ( size_t i = 0; i < nodes.size(); ++n, ++i )
+ nodes[ i ] = *n;
+
+ // avoid elems on degenarate shapes as UV on them can be wrong
+ if ( helper.HasDegeneratedEdges() )
+ {
+ bool isOnDegen = false;
+ for ( size_t i = 0; ( i < nodes.size() && !isOnDegen ); ++i )
+ isOnDegen = helper.IsDegenShape( nodes[ i ]->getshapeId() );
+ if ( isOnDegen )
+ continue;
+ }
+ // prepare to getting UVs
+ const SMDS_MeshNode* inFaceNode = 0;
+ if ( helper.HasSeam() ) {
+ for ( size_t i = 0; ( i < nodes.size() && !inFaceNode ); ++i )
+ if ( !helper.IsSeamShape( nodes[ i ]->getshapeId() ))
+ inFaceNode = nodes[ i ];
+ if ( !inFaceNode )
+ continue;
+ }
+ // get UVs
+ uv.resize( nodes.size() );
+ for ( size_t i = 0; i < nodes.size(); ++i )
+ uv[ i ] = helper.GetNodeUV( F, nodes[ i ], inFaceNode, toCheckUV );
+
+ // compare orientation of triangles
+ double faceArea = 0;
+ for ( int iT = 0, nbT = nodes.size()-2; iT < nbT; ++iT )
+ {
+ gp_XY v1 = uv[ iT+1 ] - uv[ 0 ];
+ gp_XY v2 = uv[ iT+2 ] - uv[ 0 ];
+ faceArea += v2 ^ v1;
+ }
+ haveBadFaces = ( faceArea * prevArea < 0 );
+ prevArea = faceArea;
+ }
+
+ return haveBadFaces;
+}
+