]> SALOME platform Git repositories - modules/smesh.git/blobdiff - src/StdMeshers/StdMeshers_ProjectionUtils.cxx
Salome HOME
Merge from V6_6_BR (V6_6_0rc2) 11/12/2012
[modules/smesh.git] / src / StdMeshers / StdMeshers_ProjectionUtils.cxx
index 8d198fe6955d095eab59464a1c4a7757004a94f6..76785fad0e64139375d4bbb51da17ac0f2eeb8a4 100644 (file)
@@ -66,6 +66,7 @@
 #include <gp_Vec.hxx>
 
 #include <numeric>
+#include <limits>
 
 using namespace std;
 
@@ -307,7 +308,7 @@ namespace {
     if ( gr1It.Value().ShapeType() == TopAbs_FACE )
     {
       // find a boundary edge of group1 to start from
-      TopoDS_Shape bndEdge = StdMeshers_ProjectionUtils::GetBoundaryEdge( theGroup1, theMesh );
+      TopoDS_Shape bndEdge = HERE::GetBoundaryEdge( theGroup1, theMesh );
       if ( bndEdge.IsNull() )
         return false;
 
@@ -377,40 +378,41 @@ namespace {
    */
   //================================================================================
 
-  TopoDS_Shape getOuterEdge( const TopoDS_Shape theShape1, SMESH_Mesh& mesh )
+  bool getOuterEdges( const TopoDS_Shape        shape,
+                      SMESH_Mesh&               mesh,
+                      std::list< TopoDS_Edge >& allBndEdges )
   {
-    TopoDS_Shape edge;
-    if ( theShape1.ShapeType() == TopAbs_COMPOUND )
+    if ( shape.ShapeType() == TopAbs_COMPOUND )
     {
-      TopoDS_Iterator it( theShape1 );
-      if ( it.Value().ShapeType() == TopAbs_FACE ) // group of FACEs
+      TopoDS_Iterator it( shape );
+      if ( it.More() && it.Value().ShapeType() == TopAbs_FACE ) // group of FACEs
       {
         // look for a boundary EDGE of a group
-        edge = StdMeshers_ProjectionUtils::GetBoundaryEdge( theShape1, mesh );
-        if ( !edge.IsNull() )
-          return edge;
+        StdMeshers_ProjectionUtils::GetBoundaryEdge( shape, mesh, &allBndEdges );
+        if ( !allBndEdges.empty() )
+          return true;
       }
     }
-    edge = theShape1;
-    TopExp_Explorer expF( theShape1, TopAbs_FACE ), expE;
+    TopExp_Explorer expF( shape, TopAbs_FACE ), expE;
     if ( expF.More() ) {
       for ( ; expF.More(); expF.Next() ) {
-        edge.Nullify();
         TopoDS_Shape wire =
           StdMeshers_ProjectionUtils::OuterShape( TopoDS::Face( expF.Current() ), TopAbs_WIRE );
-        for ( expE.Init( wire, TopAbs_EDGE ); edge.IsNull() && expE.More(); expE.Next() )
+        for ( expE.Init( wire, TopAbs_EDGE ); expE.More(); expE.Next() )
           if ( !SMESH_MesherHelper::IsClosedEdge( TopoDS::Edge( expE.Current() )))
-            edge = expE.Current();
-        if ( !edge.IsNull() )
-          break;
+            allBndEdges.push_back( TopoDS::Edge( expE.Current() ));
       }
-    } else if (edge.ShapeType() != TopAbs_EDGE) { // no faces
-      edge.Nullify();
-      for ( expE.Init( theShape1, TopAbs_EDGE ); edge.IsNull() && expE.More(); expE.Next() )
+    }
+    else if ( shape.ShapeType() != TopAbs_EDGE) { // no faces
+      for ( expE.Init( shape, TopAbs_EDGE ); expE.More(); expE.Next() )
         if ( !SMESH_MesherHelper::IsClosedEdge( TopoDS::Edge( expE.Current() )))
-          edge = expE.Current();
+          allBndEdges.push_back( TopoDS::Edge( expE.Current() ));
     }
-    return edge;
+    else if ( shape.ShapeType() == TopAbs_EDGE ) {
+      if ( !SMESH_MesherHelper::IsClosedEdge( TopoDS::Edge( shape )))
+        allBndEdges.push_back( TopoDS::Edge( shape ));
+    }
+    return !allBndEdges.empty();
   }
 
 } // namespace
@@ -1175,29 +1177,64 @@ bool StdMeshers_ProjectionUtils::FindSubShapeAssociation(const TopoDS_Shape& the
   // Find 2 closest vertices
 
   // get 2 linked vertices of shape 1 not belonging to an inner wire of a face
-  TopoDS_Shape edge = getOuterEdge( theShape1, *theMesh1 );
-  if ( edge.IsNull() || edge.ShapeType() != TopAbs_EDGE )
+  std::list< TopoDS_Edge > allBndEdges1;
+  if ( !getOuterEdges( theShape1, *theMesh1, allBndEdges1 ))
     RETURN_BAD_RESULT("Edge not found");
 
-  TopExp::Vertices( TopoDS::Edge( edge.Oriented(TopAbs_FORWARD)), VV1[0], VV1[1]);
-  if ( VV1[0].IsSame( VV1[1] ))
-    RETURN_BAD_RESULT("Only closed edges");
-
-  // find vertices closest to 2 linked vertices of shape 1
-  for ( int i1 = 0; i1 < 2; ++i1 )
+  std::list< TopoDS_Edge >::iterator edge1 = allBndEdges1.begin();
+  double minDist = std::numeric_limits<double>::max();
+  for ( int nbChecked=0; edge1 != allBndEdges1.end() && nbChecked++ < 10; ++edge1 )
   {
-    double dist2 = DBL_MAX;
-    gp_Pnt p1 = BRep_Tool::Pnt( VV1[ i1 ]);
-    p1.Translate( vec01 );
-    p1.Scale( gc[1], scale );
-    for ( int i2 = 1; i2 <= vMap2.Extent(); ++i2 )
+    TopExp::Vertices( TopoDS::Edge( edge1->Oriented(TopAbs_FORWARD)), VV1[0], VV1[1]);
+    if ( VV1[0].IsSame( VV1[1] ))
+      continue;//RETURN_BAD_RESULT("Only closed edges");
+
+    // find vertices closest to 2 linked vertices of shape 1
+    double dist2[2] = { 1e+100, 1e+100 };
+    TopoDS_Vertex edge2VV[2];
+    for ( int i1 = 0; i1 < 2; ++i1 )
     {
-      TopoDS_Vertex V2 = TopoDS::Vertex( vMap2( i2 ));
-      gp_Pnt p2 = BRep_Tool::Pnt ( V2 );
-      double d2 = p1.SquareDistance( p2 );
-      if ( d2 < dist2 && !V2.IsSame( VV2[ 0 ])) {
-        VV2[ i1 ] = V2; dist2 = d2;
+      gp_Pnt p1 = BRep_Tool::Pnt( VV1[ i1 ]);
+      p1.Scale( gc[0], scale );
+      p1.Translate( vec01 );
+      if ( !i1 ) {
+        // select a closest vertex among all ones in vMap2
+        for ( int i2 = 1; i2 <= vMap2.Extent(); ++i2 )
+        {
+          TopoDS_Vertex V2 = TopoDS::Vertex( vMap2( i2 ));
+          gp_Pnt        p2 = BRep_Tool::Pnt ( V2 );
+          double        d2 = p1.SquareDistance( p2 );
+          if ( d2 < dist2[ 0 ] && d2 < minDist ) {
+            edge2VV[ 0 ] = V2;
+            dist2  [ 0 ] = d2;
+          }
+        }
       }
+      else if ( !edge2VV[0].IsNull() ) {
+        // select a closest vertex among ends of edges meeting at edge2VV[0]
+        PShapeIteratorPtr edgeIt = SMESH_MesherHelper::GetAncestors( edge2VV[0],
+                                                                     *theMesh2, TopAbs_EDGE);
+        while ( const TopoDS_Shape* edge2 = edgeIt->next() )
+          for ( TopoDS_Iterator itV2( *edge2 ); itV2.More(); itV2.Next() )
+          {
+            if ( itV2.Value().IsSame( edge2VV[ 0 ])) continue;
+            if ( !vMap2.Contains( itV2.Value()    )) continue;
+            TopoDS_Vertex V2 = TopoDS::Vertex( itV2.Value() );
+            gp_Pnt        p2 = BRep_Tool::Pnt ( V2 );
+            double        d2 = p1.SquareDistance( p2 );
+            if ( d2 < dist2[1] && d2 < minDist ) {
+              edge2VV[ 1 ] = V2;
+              dist2  [ 1 ] = d2;
+            }
+          }
+      }
+    }
+    if ( dist2[0] + dist2[1] < minDist ) {
+      VV2[0] = edge2VV[0];
+      VV2[1] = edge2VV[1];
+      minDist = dist2[0] + dist2[1];
+      if ( minDist < 1e-10 )
+        break;
     }
   }
 
@@ -1248,8 +1285,13 @@ int StdMeshers_ProjectionUtils::FindFaceAssociation(const TopoDS_Face&    face1,
          SMESH_Block::GetOrderedEdges( face2, VV2[0], edges2, nbEInW2, outer_wire_algo) )
       CONT_BAD_RESULT("Different number of wires in faces ");
 
-    if ( nbEInW1 != nbEInW2 )
-      CONT_BAD_RESULT("Different number of edges in faces: " <<
+    if ( nbEInW1 != nbEInW2 && outer_wire_algo == 0 &&
+         ( std::accumulate( nbEInW1.begin(), nbEInW1.end(), 0) !=
+           std::accumulate( nbEInW2.begin(), nbEInW2.end(), 0)))
+      RETURN_BAD_RESULT("Different number of edges in faces");
+
+    if ( nbEInW1.front() != nbEInW2.front() )
+      CONT_BAD_RESULT("Different number of edges in the outer wire: " <<
                       nbEInW1.front() << " != " << nbEInW2.front());
 
     i_ok_wire_algo = outer_wire_algo;
@@ -1300,6 +1342,7 @@ int StdMeshers_ProjectionUtils::FindFaceAssociation(const TopoDS_Face&    face1,
   } // loop algos getting an outer wire
   
   // Try to orient all (if !OK) or only internal wires (issue 0020996) by UV similarity
+
   if (( !OK || nbEInW1.size() > 1 ) && i_ok_wire_algo > -1 )
   {
     // Check that Vec(VV1[0],VV1[1]) in 2D on face1 is the same
@@ -1325,44 +1368,66 @@ int StdMeshers_ProjectionUtils::FindFaceAssociation(const TopoDS_Face&    face1,
         SMESH_Block::GetOrderedEdges( face2, VV2[0], edges2, nbEInW2, i_ok_wire_algo);
       }
       gp_XY dUV = v0f2UV.XY() - v0f1UV.XY(); // UV shift between 2 faces
+      //
       // skip edges of the outer wire (if the outer wire is OK)
-      list< int >::iterator nbEInW = nbEInW1.begin();
-      list< TopoDS_Edge >::iterator edge1Beg = edges1.begin(), edge2Beg = edges2.begin();
-      if ( OK )
-      {
-        for ( int i = 0; i < *nbEInW; ++i )
-          ++edge1Beg, ++edge2Beg;
-        ++nbEInW;
-      }
-      for ( ; nbEInW != nbEInW1.end(); ++nbEInW ) // loop on wires
+      list< int >::iterator nbE2, nbE1 = nbEInW1.begin();
+      list< TopoDS_Edge >::iterator edge2Beg, edge1Beg = edges1.begin();
+      if ( OK ) std::advance( edge1Beg, *nbE1++ );
+      // reach an end of edges of a current wire1
+      list< TopoDS_Edge >::iterator edge2End, edge1End;
+      //
+      // find corresponding wires of face2
+      for ( int iW1 = OK; nbE1 != nbEInW1.end(); ++nbE1, ++iW1 ) // loop on wires of face1
       {
-        // reach an end of edges of a current wire
-        list< TopoDS_Edge >::iterator edge1End = edge1Beg, edge2End = edge2Beg;
-        for ( int i = 0; i < *nbEInW; ++i )
-          ++edge1End, ++edge2End;
-        // rotate edges2 untill coincident with edges1 in 2D
+        // reach an end of edges of a current wire1
+        edge1End = edge1Beg;
+        std::advance( edge1End, *nbE1 );
+        // UV on face1 to find on face2
         v0f1UV = BRep_Tool::Parameters( TopExp::FirstVertex(*edge1Beg,true), face1 );
         v1f1UV = BRep_Tool::Parameters( TopExp::LastVertex (*edge1Beg,true), face1 );
         v0f1UV.ChangeCoord() += dUV;
         v1f1UV.ChangeCoord() += dUV;
-        int i = *nbEInW;
-        while ( --i > 0 && !sameVertexUV( *edge2Beg, face2, 0, v0f1UV, vTolUV ))
-          edges2.splice( edge2End, edges2, edge2Beg++ ); // move edge2Beg to place before edge2End
-        if ( sameVertexUV( *edge2Beg, face2, 0, v0f1UV, vTolUV ))
+        //
+        // look through wires of face2
+        edge2Beg = edges2.begin();
+        nbE2     = nbEInW2.begin();
+        if ( OK ) std::advance( edge2Beg, *nbE2++ );
+        for ( int iW2 = OK; nbE2 != nbEInW2.end(); ++nbE2, ++iW2 ) // loop on wires of face2
         {
-          if ( nbEInW == nbEInW1.begin() )
-            OK = true; // OK is for the first wire
-          // reverse edges2 if needed
-          if ( !sameVertexUV( *edge2Beg, face2, 1, v1f1UV, vTolUV ))
+          // reach an end of edges of a current wire2
+          edge2End = edge2Beg;
+          std::advance( edge2End, *nbE2 );
+          if ( *nbE1 == *nbE2 && iW2 >= iW1 )
           {
-            Reverse( edges2 , *nbEInW, distance( edges2.begin(),edge2Beg ));
-            // set correct edge2End
-            edge2End = edges2.begin();
-            std::advance( edge2End, std::accumulate( nbEInW1.begin(), nbEInW, *nbEInW));
+            // rotate edge2 untill coincidence with edge1 in 2D
+            int i = *nbE2;
+            while ( i-- > 0 && !sameVertexUV( *edge2Beg, face2, 0, v0f1UV, vTolUV ))
+              // move edge2Beg to place before edge2End
+              edges2.splice( edge2End, edges2, edge2Beg++ );
+
+            if ( sameVertexUV( *edge2Beg, face2, 0, v0f1UV, vTolUV ))
+            {
+              if ( iW1 == 0 ) OK = true; // OK is for the first wire
+              // reverse edges2 if needed
+              if ( !sameVertexUV( *edge2Beg, face2, 1, v1f1UV, vTolUV ))
+                Reverse( edges2 , *nbE2, std::distance( edges2.begin(),edge2Beg ));
+              // put wire2 at a right place within edges2
+              if ( iW1 != iW2 ) {
+                list< TopoDS_Edge >::iterator place2 = edges2.begin();
+                std::advance( place2, std::distance( edges1.begin(), edge1Beg ));
+                edges2.splice( place2, edges2, edge2Beg, edge2End );
+                // move nbE2 as well
+                list< int >::iterator placeNbE2 = nbEInW2.begin();
+                std::advance( placeNbE2, iW1 );
+                nbEInW2.splice( placeNbE2, nbEInW2, nbE2 );
+              }
+              break;
+            }
           }
+          // prepare to the next wire loop
+          edge2Beg = edge2End;
         }
-        // prepare to the next wire loop
-        edge1Beg = edge1End, edge2Beg = edge2End;
+        edge1Beg = edge1End;
       }
     }
   }
@@ -1370,7 +1435,7 @@ int StdMeshers_ProjectionUtils::FindFaceAssociation(const TopoDS_Face&    face1,
   const int nbEdges = nbEInW1.front();
   if ( OK && nbEdges == 2 )
   {
-    // if a wire includes 2 edges, it's impossible to associate them using
+    // if wires include 2 edges, it's impossible to associate them using
     // topological information only. Try to use length of edges for association.
     double l1[2], l2[2];
     edgeIt = edges1.begin();
@@ -2064,8 +2129,9 @@ int StdMeshers_ProjectionUtils::Count(const TopoDS_Shape&    shape,
  */
 //================================================================================
 
-TopoDS_Edge StdMeshers_ProjectionUtils::GetBoundaryEdge(const TopoDS_Shape& edgeContainer,
-                                                        const SMESH_Mesh&   mesh)
+TopoDS_Edge StdMeshers_ProjectionUtils::GetBoundaryEdge(const TopoDS_Shape&       edgeContainer,
+                                                        const SMESH_Mesh&         mesh,
+                                                        std::list< TopoDS_Edge >* allBndEdges)
 {
   TopTools_IndexedMapOfShape facesOfEdgeContainer, facesNearEdge;
   TopExp::MapShapes( edgeContainer, TopAbs_FACE, facesOfEdgeContainer );
@@ -2080,8 +2146,12 @@ TopoDS_Edge StdMeshers_ProjectionUtils::GetBoundaryEdge(const TopoDS_Shape& edge
         if ( facesOfEdgeContainer.Contains( *face ))
           if ( facesNearEdge.Add( *face ) && facesNearEdge.Extent() > 1 )
             break;
-      if ( facesNearEdge.Extent() == 1 )
-        return edge;
+      if ( facesNearEdge.Extent() == 1 ) {
+        if ( allBndEdges )
+          allBndEdges->push_back( edge );
+        else
+          return edge;
+      }
     }
 
   return TopoDS_Edge();