Salome HOME
0020452: EDF 1056 SMESH : 2D Projection Issue
[modules/smesh.git] / src / StdMeshers / StdMeshers_ProjectionUtils.cxx
index 1fa7c45249d03f142d0be502c17a07df444ecba8..50365eb7f79a19d885ae32995a24641061ec7286 100644 (file)
@@ -36,7 +36,7 @@
 #include "SMESH_Hypothesis.hxx"
 #include "SMESH_IndexedDataMapOfShapeIndexedMapOfShape.hxx"
 #include "SMESH_Mesh.hxx"
-#include "SMESH_MeshEditor.hxx"
+#include "SMESH_MesherHelper.hxx"
 #include "SMESH_subMesh.hxx"
 #include "SMESH_subMeshEventListener.hxx"
 #include "SMDS_EdgePosition.hxx"
@@ -67,13 +67,15 @@ using namespace std;
 
 
 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
-#define SHOW_VERTEX(v,msg) // { \
-//  if ( v.IsNull() ) cout << msg << " NULL SHAPE" << endl; \
-// else if (v.ShapeType() == TopAbs_VERTEX) {\
-//   gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( v ));\
-//   cout << msg << (v).TShape().operator->()<<" ( " <<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;}\
+#define CONT_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); continue; }
+#define SHOW_VERTEX(v,msg) \
+// { \
+//  if ( (v).IsNull() ) cout << msg << " NULL SHAPE" << endl; \
+// else if ((v).ShapeType() == TopAbs_VERTEX) {\
+//   gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( (v) ));\
+//   cout<<msg<<" "<<(v).TShape().operator->()<<" ( "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;}\
 // else {\
-// cout << msg << " "; TopAbs::Print(v.ShapeType(),cout) <<" "<<(v).TShape().operator->()<<endl;}\
+// cout << msg << " "; TopAbs::Print((v).ShapeType(),cout) <<" "<<(v).TShape().operator->()<<endl;}\
 // }
 #define SHOW_LIST(msg,l) \
 // { \
@@ -369,8 +371,28 @@ bool StdMeshers_ProjectionUtils::FindSubShapeAssociation(const TopoDS_Shape& the
                                                          SMESH_Mesh*         theMesh2,
                                                          TShapeShapeMap &    theMap)
 {
+  // Structure of this long function is following
+  // 1) Group->group projection: theShape1 is a group member,
+  //    theShape2 is a group. We find a group theShape1 is in and recall self.
+  // 2) Accosiate same shapes with different location (partners).
+  // 3) If vertex association is given, perform accosiation according to shape type:
+  //       switch ( ShapeType ) {
+  //         case TopAbs_EDGE:
+  //         case ...:
+  //       }
+  //    else try to accosiate in different ways:
+  //       a) accosiate shapes by propagation and other simple cases
+  //            switch ( ShapeType ) {
+  //            case TopAbs_EDGE:
+  //            case ...:
+  //            }
+  //       b) find association of a couple of vertices and recall self.
+  //
+
+  // =================================================================================
+  // Is it the case of associating a group member -> another group? (PAL16202, 16203)
+  // =================================================================================
   if ( theShape1.ShapeType() != theShape2.ShapeType() ) {
-    // is it the case of a group member -> another group? (PAL16202, 16203)
     TopoDS_Shape group1, group2;
     if ( theShape1.ShapeType() == TopAbs_COMPOUND ) {
       group1 = theShape1;
@@ -387,6 +409,31 @@ bool StdMeshers_ProjectionUtils::FindSubShapeAssociation(const TopoDS_Shape& the
   }
 
   bool bidirect = ( !theShape1.IsSame( theShape2 ));
+
+  // ============
+  // Is partner?
+  // ============
+  bool partner = theShape1.IsPartner( theShape2 );
+  TopTools_DataMapIteratorOfDataMapOfShapeShape vvIt( theMap );
+  for ( ; partner && vvIt.More(); vvIt.Next() )
+    partner = vvIt.Key().IsPartner( vvIt.Value() );
+
+  if ( partner ) // Same shape with different location
+  {
+    // recursively associate all subshapes of theShape1 and theShape2
+    typedef list< pair< TopoDS_Shape, TopoDS_Shape > > TShapePairsList;
+    TShapePairsList shapesQueue( 1, make_pair( theShape1, theShape2 ));
+    TShapePairsList::iterator s1_s2 = shapesQueue.begin();
+    for ( ; s1_s2 != shapesQueue.end(); ++s1_s2 )
+    {
+      InsertAssociation( s1_s2->first, s1_s2->second, theMap, bidirect);
+      TopoDS_Iterator s1It( s1_s2->first), s2It( s1_s2->second );
+      for ( ; s1It.More(); s1It.Next(), s2It.Next() )
+        shapesQueue.push_back( make_pair( s1It.Value(), s2It.Value() ));
+    }
+    return true;
+  }
+
   if ( !theMap.IsEmpty() )
   {
     //======================================================================
@@ -884,6 +931,7 @@ bool StdMeshers_ProjectionUtils::FindSubShapeAssociation(const TopoDS_Shape& the
         pair<int,TopoDS_Edge> step_edge = GetPropagationEdge( theMesh1, edge2, edge1 );
         if ( !step_edge.second.IsNull() ) { // propagation found
           propag_edges.insert( step_edge );
+          if ( step_edge.first == 1 ) break; // most close found
         }
       }
       if ( !propag_edges.empty() ) // propagation found
@@ -1105,66 +1153,69 @@ bool StdMeshers_ProjectionUtils::FindSubShapeAssociation(const TopoDS_Shape& the
  */
 //================================================================================
 
-int StdMeshers_ProjectionUtils::FindFaceAssociation(const TopoDS_Face& face1,
-                                                    TopoDS_Vertex      VV1[2],
-                                                    const TopoDS_Face& face2,
-                                                    TopoDS_Vertex      VV2[2],
+int StdMeshers_ProjectionUtils::FindFaceAssociation(const TopoDS_Face&    face1,
+                                                    TopoDS_Vertex         VV1[2],
+                                                    const TopoDS_Face&    face2,
+                                                    TopoDS_Vertex         VV2[2],
                                                     list< TopoDS_Edge > & edges1,
                                                     list< TopoDS_Edge > & edges2)
 {
-  edges1.clear();
-  edges2.clear();
-
   list< int > nbVInW1, nbVInW2;
-  if ( SMESH_Block::GetOrderedEdges( face1, VV1[0], edges1, nbVInW1) !=
-       SMESH_Block::GetOrderedEdges( face2, VV2[0], edges2, nbVInW2) )
-    RETURN_BAD_RESULT("Different number of wires in faces ");
+  for ( int outer_wire_algo = 0; outer_wire_algo < 2; ++outer_wire_algo )
+  {
+    edges1.clear();
+    edges2.clear();
 
-  if ( nbVInW1.front() != nbVInW2.front() )
-    RETURN_BAD_RESULT("Different number of edges in faces: " <<
+    if ( SMESH_Block::GetOrderedEdges( face1, VV1[0], edges1, nbVInW1, outer_wire_algo) !=
+         SMESH_Block::GetOrderedEdges( face2, VV2[0], edges2, nbVInW2, outer_wire_algo) )
+      CONT_BAD_RESULT("Different number of wires in faces ");
+
+    if ( nbVInW1.front() != nbVInW2.front() )
+      CONT_BAD_RESULT("Different number of edges in faces: " <<
                       nbVInW1.front() << " != " << nbVInW2.front());
 
-  // Define if we need to reverse one of wires to make edges in lists match each other
-
-  bool reverse = false;
-
-  list< TopoDS_Edge >::iterator eBackIt;
-  if ( !VV1[1].IsSame( TopExp::LastVertex( edges1.front(), true ))) {
-    reverse = true;
-    eBackIt = --edges1.end();
-    // check if the second vertex belongs to the first or last edge in the wire
-    if ( !VV1[1].IsSame( TopExp::FirstVertex( *eBackIt, true ))) {
-      bool KO = true; // belongs to none
-      if ( nbVInW1.size() > 1 ) { // several wires
-        eBackIt = edges1.begin();
-        for ( int i = 1; i < nbVInW1.front(); ++i ) ++eBackIt;
-        KO = !VV1[1].IsSame( TopExp::FirstVertex( *eBackIt, true ));
+    // Define if we need to reverse one of wires to make edges in lists match each other
+
+    bool reverse = false;
+
+    list< TopoDS_Edge >::iterator edgeIt;
+    if ( !VV1[1].IsSame( TopExp::LastVertex( edges1.front(), true ))) {
+      reverse = true;
+      edgeIt = --edges1.end();
+      // check if the second vertex belongs to the first or last edge in the wire
+      if ( !VV1[1].IsSame( TopExp::FirstVertex( *edgeIt, true ))) {
+        bool KO = true; // belongs to none
+        if ( nbVInW1.size() > 1 ) { // several wires
+          edgeIt = edges1.begin();
+          for ( int i = 1; i < nbVInW1.front(); ++i ) ++edgeIt;
+          KO = !VV1[1].IsSame( TopExp::FirstVertex( *edgeIt, true ));
+        }
+        if ( KO )
+          CONT_BAD_RESULT("GetOrderedEdges() failed");
       }
-      if ( KO )
-        RETURN_BAD_RESULT("GetOrderedEdges() failed");
     }
-  }
-  eBackIt = --edges2.end();
-  if ( !VV2[1].IsSame( TopExp::LastVertex( edges2.front(), true ))) {
-    reverse = !reverse;
-    // check if the second vertex belongs to the first or last edge in the wire
-    if ( !VV2[1].IsSame( TopExp::FirstVertex( *eBackIt, true ))) {
-      bool KO = true; // belongs to none
-      if ( nbVInW2.size() > 1 ) { // several wires
-        eBackIt = edges2.begin();
-        for ( int i = 1; i < nbVInW2.front(); ++i ) ++eBackIt;
-        KO = !VV2[1].IsSame( TopExp::FirstVertex( *eBackIt, true ));
+    edgeIt = --edges2.end();
+    if ( !VV2[1].IsSame( TopExp::LastVertex( edges2.front(), true ))) {
+      reverse = !reverse;
+      // check if the second vertex belongs to the first or last edge in the wire
+      if ( !VV2[1].IsSame( TopExp::FirstVertex( *edgeIt, true ))) {
+        bool KO = true; // belongs to none
+        if ( nbVInW2.size() > 1 ) { // several wires
+          edgeIt = edges2.begin();
+          for ( int i = 1; i < nbVInW2.front(); ++i ) ++edgeIt;
+          KO = !VV2[1].IsSame( TopExp::FirstVertex( *edgeIt, true ));
+        }
+        if ( KO )
+          CONT_BAD_RESULT("GetOrderedEdges() failed");
       }
-      if ( KO )
-        RETURN_BAD_RESULT("GetOrderedEdges() failed");
     }
-  }
-  if ( reverse )
-  {
-    Reverse( edges2 , nbVInW2.front());
-    if (( VV1[1].IsSame( TopExp::LastVertex( edges1.front(), true ))) !=
-        ( VV2[1].IsSame( TopExp::LastVertex( edges2.front(), true ))))
-      RETURN_BAD_RESULT("GetOrderedEdges() failed");
+    if ( reverse )
+    {
+      Reverse( edges2 , nbVInW2.front());
+      if (( VV1[1].IsSame( TopExp::LastVertex( edges1.front(), true ))) !=
+          ( VV2[1].IsSame( TopExp::LastVertex( edges2.front(), true ))))
+        CONT_BAD_RESULT("GetOrderedEdges() failed");
+    }
   }
   return nbVInW2.front();
 }
@@ -1475,7 +1526,7 @@ FindMatchingNodesOnFaces( const TopoDS_Face&     face1,
   // 1. Nodes of corresponding links:
 
   // get 2 matching edges, try to find not seam ones
-  TopoDS_Edge edge1, edge2, seam1, seam2;
+  TopoDS_Edge edge1, edge2, seam1, seam2, anyEdge1, anyEdge2;
   TopExp_Explorer eE( OuterShape( face2, TopAbs_WIRE ), TopAbs_EDGE );
   do {
     // edge 2
@@ -1493,6 +1544,7 @@ FindMatchingNodesOnFaces( const TopoDS_Face&     face1,
     SMESHDS_SubMesh * eSM2 = meshDS2->MeshElements( e2 );
     bool nodesOnEdges = ( eSM1 && eSM2 && eSM1->NbNodes() && eSM2->NbNodes() );
     // check that the nodes on edges belong to faces
+    // (as NETGEN ignores nodes on the degenerated geom edge)
     bool nodesOfFaces = false;
     if ( nodesOnEdges ) {
       const SMDS_MeshNode* n1 = eSM1->GetNodes()->next();
@@ -1509,12 +1561,19 @@ FindMatchingNodesOnFaces( const TopoDS_Face&     face1,
         edge1 = e1; edge2 = e2;
       }
     }
+    else {
+      anyEdge1 = e1; anyEdge2 = e2;
+    }
   } while ( edge2.IsNull() && eE.More() );
   //
   if ( edge2.IsNull() ) {
     edge1 = seam1; edge2 = seam2;
   }
-  if ( edge2.IsNull() ) RETURN_BAD_RESULT("No matching edges with nodes found");
+  bool hasNodesOnEdge = (! edge2.IsNull() );
+  if ( !hasNodesOnEdge ) {
+    // 0020338 - nb segments == 1
+    edge1 = anyEdge1; edge2 = anyEdge2;
+  }
 
   // get 2 matching vertices
   TopoDS_Vertex V2 = TopExp::FirstVertex( TopoDS::Edge( edge2 ));
@@ -1532,35 +1591,52 @@ FindMatchingNodesOnFaces( const TopoDS_Face&     face1,
   const SMDS_MeshNode* nullNode = 0;
   vector< const SMDS_MeshNode*> eNode1( 2, nullNode );
   vector< const SMDS_MeshNode*> eNode2( 2, nullNode );
-  int nbNodeToGet = 1;
-  if ( IsClosedEdge( edge1 ) || IsClosedEdge( edge2 ) )
-    nbNodeToGet = 2;
-  for ( int is2 = 0; is2 < 2; ++is2 )
+  if ( hasNodesOnEdge )
   {
-    TopoDS_Edge &     edge  = is2 ? edge2 : edge1;
-    SMESHDS_Mesh *    smDS  = is2 ? meshDS2 : meshDS1;
-    SMESHDS_SubMesh* edgeSM = smDS->MeshElements( edge );
-    // nodes linked with ones on vertices
-    const SMDS_MeshNode*           vNode = is2 ? vNode2 : vNode1;
-    vector< const SMDS_MeshNode*>& eNode = is2 ? eNode2 : eNode1;
-    int nbGotNode = 0;
-    SMDS_ElemIteratorPtr vElem = vNode->GetInverseElementIterator();
-    while ( vElem->more() && nbGotNode != nbNodeToGet ) {
-      const SMDS_MeshElement* elem = vElem->next();
-      if ( elem->GetType() == SMDSAbs_Edge && edgeSM->Contains( elem ))
-        eNode[ nbGotNode++ ] = 
-          ( elem->GetNode(0) == vNode ) ? elem->GetNode(1) : elem->GetNode(0);
-    }
-    if ( nbGotNode > 1 ) // sort found nodes by param on edge
+    int nbNodeToGet = 1;
+    if ( IsClosedEdge( edge1 ) || IsClosedEdge( edge2 ) )
+      nbNodeToGet = 2;
+    for ( int is2 = 0; is2 < 2; ++is2 )
     {
-      SMESH_MesherHelper* helper = is2 ? &helper2 : &helper1;
-      double u0 = helper->GetNodeU( edge, eNode[ 0 ]);
-      double u1 = helper->GetNodeU( edge, eNode[ 1 ]);
-      if ( u0 > u1 ) std::swap( eNode[ 0 ], eNode[ 1 ]);
+      TopoDS_Edge &     edge  = is2 ? edge2 : edge1;
+      SMESHDS_Mesh *    smDS  = is2 ? meshDS2 : meshDS1;
+      SMESHDS_SubMesh* edgeSM = smDS->MeshElements( edge );
+      // nodes linked with ones on vertices
+      const SMDS_MeshNode*           vNode = is2 ? vNode2 : vNode1;
+      vector< const SMDS_MeshNode*>& eNode = is2 ? eNode2 : eNode1;
+      int nbGotNode = 0;
+      SMDS_ElemIteratorPtr vElem = vNode->GetInverseElementIterator(SMDSAbs_Edge);
+      while ( vElem->more() && nbGotNode != nbNodeToGet ) {
+        const SMDS_MeshElement* elem = vElem->next();
+        if ( edgeSM->Contains( elem ))
+          eNode[ nbGotNode++ ] = 
+            ( elem->GetNode(0) == vNode ) ? elem->GetNode(1) : elem->GetNode(0);
+      }
+      if ( nbGotNode > 1 ) // sort found nodes by param on edge
+      {
+        SMESH_MesherHelper* helper = is2 ? &helper2 : &helper1;
+        double u0 = helper->GetNodeU( edge, eNode[ 0 ]);
+        double u1 = helper->GetNodeU( edge, eNode[ 1 ]);
+        if ( u0 > u1 ) std::swap( eNode[ 0 ], eNode[ 1 ]);
+      }
+      if ( nbGotNode == 0 )
+        RETURN_BAD_RESULT("Found no nodes on edge " << smDS->ShapeToIndex( edge ) <<
+                          " linked to " << vNode );
     }
-    if ( nbGotNode == 0 )
-      RETURN_BAD_RESULT("Found no nodes on edge " << smDS->ShapeToIndex( edge ) <<
-                        " linked to " << vNode );
+  }
+  else // 0020338 - nb segments == 1
+  {
+    // get 2 other matching vertices
+    V2 = TopExp::LastVertex( TopoDS::Edge( edge2 ));
+    if ( !assocMap.IsBound( V2 ))
+      RETURN_BAD_RESULT("Association not found for vertex " << meshDS2->ShapeToIndex( V2 ));
+    V1 = TopoDS::Vertex( assocMap( V2 ));
+
+    // nodes on vertices
+    eNode1[0] = SMESH_Algo::VertexNode( V1, meshDS1 );
+    eNode2[0] = SMESH_Algo::VertexNode( V2, meshDS2 );
+    if ( !eNode1[0] ) RETURN_BAD_RESULT("No node on vertex #" << meshDS1->ShapeToIndex( V1 ));
+    if ( !eNode2[0] ) RETURN_BAD_RESULT("No node on vertex #" << meshDS2->ShapeToIndex( V2 ));
   }
 
   // 2. face sets
@@ -1639,7 +1715,7 @@ FindMatchingNodesOnFaces( const TopoDS_Face&     face1,
       notInSet.insert( f2 );
       for ( int i = 0; i < nbNodes; ++i ) {
         const SMDS_MeshNode* n1 = faceToKeep->GetNode( i );
-        const SMDS_MeshNode* n2 = faceToKeep->GetNode( i+1 );
+        const SMDS_MeshNode* n2 = faceToKeep->GetNode( i+1 % nbNodes );
         f1 = SMESH_MeshEditor::FindFaceInSet( n1, n2, inSet, notInSet );
         if ( f1 )
           elems.insert( f1 );