0029544: Regression vs 7.2.0: ShapeUpgrade_UnifySameDomain fails to merge linear...
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_UnifySameDomain.cxx
index 5c2960f..57d9f03 100644 (file)
@@ -802,7 +802,9 @@ static Standard_Boolean MergeSubSeq(const TopTools_SequenceOfShape& aChain,
 //=======================================================================
 
 static Standard_Boolean IsMergingPossible(const TopoDS_Edge& edge1, const TopoDS_Edge& edge2, 
-                                          double theAngTol, const TopTools_MapOfShape& AvoidEdgeVrt)
+                                          double theAngTol, double theLinTol, 
+                                          const TopTools_MapOfShape& AvoidEdgeVrt, const bool theLineDirectionOk,
+                                          const gp_Pnt& theFirstPoint, const gp_Vec& theDirectionVec)
 {
   TopoDS_Vertex CV = TopExp::LastVertex(edge1, Standard_True);
   if (CV.IsNull() || AvoidEdgeVrt.Contains(CV))
@@ -845,6 +847,46 @@ static Standard_Boolean IsMergingPossible(const TopoDS_Edge& edge1, const TopoDS
   if (Diff1.Angle(Diff2) > theAngTol)
     return Standard_False;
 
+  if (theLineDirectionOk && t2 == GeomAbs_Line)
+  {
+    Standard_Real aLast = (edge2.Orientation() == TopAbs_FORWARD) ?
+      ade2.LastParameter() : ade2.FirstParameter();
+    gp_Vec aCurV(theFirstPoint, ade2.Value(aLast));
+    Standard_Real aDD = theDirectionVec.CrossSquareMagnitude(aCurV);
+    if (aDD > theLinTol*theLinTol)
+      return Standard_False;
+  }
+
+  return Standard_True;
+}
+
+//=======================================================================
+//function : GetLineEdgePoints
+//purpose  : 
+//=======================================================================
+static Standard_Boolean GetLineEdgePoints(const TopoDS_Edge& theInpEdge, gp_Pnt& theFirstPoint, gp_Vec& theDirectionVec)
+{
+  double f, l;
+  Handle(Geom_Curve) aCur = BRep_Tool::Curve(theInpEdge, f, l);
+  if(aCur.IsNull()) 
+    return Standard_False;
+
+  Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
+  if (!aTC.IsNull())
+    aCur = aTC->BasisCurve();
+
+  if (aCur->DynamicType() != STANDARD_TYPE(Geom_Line))
+    return Standard_False;
+
+  if (theInpEdge.Orientation() == TopAbs_REVERSED) {
+    Standard_Real tmp = f;
+    f = l;
+    l = tmp;
+  }
+  theFirstPoint = aCur->Value(f);
+  gp_Pnt aLP = aCur->Value(l);
+  theDirectionVec = aLP.XYZ().Subtracted(theFirstPoint.XYZ());
+  theDirectionVec.Normalize();
   return Standard_True;
 }
 
@@ -856,25 +898,32 @@ static Standard_Boolean IsMergingPossible(const TopoDS_Edge& edge1, const TopoDS
 
 static void GenerateSubSeq (const TopTools_SequenceOfShape& anInpEdgeSeq,
                             NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
-                            Standard_Boolean IsClosed, double theAngTol, const TopTools_MapOfShape& AvoidEdgeVrt)
+                            Standard_Boolean IsClosed, double theAngTol, double theLinTol, 
+                            const TopTools_MapOfShape& AvoidEdgeVrt)
 {
   Standard_Boolean isOk = Standard_False;
   TopoDS_Edge edge1, edge2;
 
   SubSequenceOfEdges SubSeq;
-  SubSeq.SeqsEdges.Append(TopoDS::Edge(anInpEdgeSeq(1)));
+  TopoDS_Edge RefEdge = TopoDS::Edge(anInpEdgeSeq(1));
+  SubSeq.SeqsEdges.Append(RefEdge);
   SeqOfSubSeqOfEdges.Append(SubSeq);
 
+  gp_Pnt aFirstPoint;
+  gp_Vec aDirectionVec;
+  Standard_Boolean isLineDirectionOk = GetLineEdgePoints(RefEdge, aFirstPoint, aDirectionVec);  
+  
   for (int i = 1; i < anInpEdgeSeq.Length(); i++)
   {
     edge1 = TopoDS::Edge(anInpEdgeSeq(i));
     edge2 = TopoDS::Edge(anInpEdgeSeq(i+1));
-    isOk = IsMergingPossible(edge1, edge2, theAngTol, AvoidEdgeVrt);
+    isOk = IsMergingPossible(edge1, edge2, theAngTol, theLinTol, AvoidEdgeVrt, isLineDirectionOk, aFirstPoint, aDirectionVec);
     if (!isOk)
     {
       SubSequenceOfEdges aSubSeq;
       aSubSeq.SeqsEdges.Append(edge2);
       SeqOfSubSeqOfEdges.Append(aSubSeq);
+      isLineDirectionOk = GetLineEdgePoints(edge2, aFirstPoint, aDirectionVec);
     }
     else
       SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(edge2);
@@ -884,7 +933,7 @@ static void GenerateSubSeq (const TopTools_SequenceOfShape& anInpEdgeSeq,
   {
     edge1 = TopoDS::Edge(anInpEdgeSeq.Last());
     edge2 = TopoDS::Edge(anInpEdgeSeq.First());
-    if (IsMergingPossible(edge1, edge2, theAngTol, AvoidEdgeVrt))
+    if (IsMergingPossible(edge1, edge2, theAngTol, theLinTol, AvoidEdgeVrt, Standard_False, aFirstPoint, aDirectionVec))
     {
       SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(SeqOfSubSeqOfEdges.ChangeFirst().SeqsEdges);
       SeqOfSubSeqOfEdges.Remove(1);
@@ -898,6 +947,7 @@ static void GenerateSubSeq (const TopTools_SequenceOfShape& anInpEdgeSeq,
 //=======================================================================
 static Standard_Boolean MergeEdges(TopTools_SequenceOfShape& SeqEdges,
                                    const Standard_Real theAngTol,
+                                   const Standard_Real theLinTol,
                                    const Standard_Boolean ConcatBSplines,
                                    const Standard_Boolean isSafeInputMode,
                                    Handle(ShapeBuild_ReShape)& theContext,
@@ -1000,7 +1050,7 @@ static Standard_Boolean MergeEdges(TopTools_SequenceOfShape& SeqEdges,
 
     // split chain by vertices at which merging is not possible
     NCollection_Sequence<SubSequenceOfEdges> aOneSeq;
-    GenerateSubSeq(aChain, aOneSeq, IsClosed, theAngTol, VerticesToAvoid);
+    GenerateSubSeq(aChain, aOneSeq, IsClosed, theAngTol, theLinTol, VerticesToAvoid);
 
     // put sub-chains in the result
     SeqOfSubSeqOfEdges.Append(aOneSeq);
@@ -1025,13 +1075,14 @@ static Standard_Boolean MergeEdges(TopTools_SequenceOfShape& SeqEdges,
 //=======================================================================
 static Standard_Boolean MergeSeq (TopTools_SequenceOfShape& SeqEdges,
                                   const Standard_Real theAngTol,
+                                  const Standard_Real theLinTol,
                                   const Standard_Boolean ConcatBSplines,
                                   const Standard_Boolean isSafeInputMode,
                                   Handle(ShapeBuild_ReShape)& theContext,
                                   const TopTools_MapOfShape& nonMergVert)
 {
   NCollection_Sequence<SubSequenceOfEdges> SeqOfSubsSeqOfEdges;
-  if (MergeEdges(SeqEdges, theAngTol, ConcatBSplines, isSafeInputMode,
+  if (MergeEdges(SeqEdges, theAngTol, theLinTol, ConcatBSplines, isSafeInputMode,
                  theContext, SeqOfSubsSeqOfEdges, nonMergVert))
   {
     for (Standard_Integer i = 1; i <= SeqOfSubsSeqOfEdges.Length(); i++ )
@@ -1601,122 +1652,48 @@ void ShapeUpgrade_UnifySameDomain::IntUnifyFaces(const TopoDS_Shape& theInpShape
 void ShapeUpgrade_UnifySameDomain::UnifyEdges()
 {
   TopoDS_Shape aRes = myContext->Apply(myShape);
-
-  TopTools_IndexedMapOfShape ChangedFaces;
-
   // creating map of edge faces
   TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
   TopExp::MapShapesAndAncestors(aRes, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
-  
   // creating map of vertex edges
   TopTools_IndexedDataMapOfShapeListOfShape aMapEdgesVertex;
   TopExp::MapShapesAndUniqueAncestors(aRes, TopAbs_VERTEX, TopAbs_EDGE, aMapEdgesVertex);
-  
-  TopTools_MapOfShape SharedVert;
-
-  TopTools_IndexedMapOfShape anOldEdges;
-  TopExp::MapShapes(myInitShape, TopAbs_EDGE, anOldEdges);
-
-  TopTools_DataMapOfShapeShape NewEdges2OldEdges;
-  for (int i = 1; i <= anOldEdges.Extent(); i++)
-  {
-    const TopoDS_Shape& anOldEdge = anOldEdges(i);
-    TopoDS_Shape aNewEdge = myContext->Apply(anOldEdge);
-    if (!aNewEdge.IsNull() && !aNewEdge.IsSame(anOldEdge))
-      NewEdges2OldEdges.Bind(aNewEdge, anOldEdge);
-  }
 
   if (mySafeInputMode)
     UpdateMapOfShapes(myKeepShapes, myContext);
 
-  TopExp_Explorer exp;
-  // processing separate wires
-  for (exp.Init(aRes, TopAbs_WIRE, TopAbs_FACE); exp.More(); exp.Next()) 
+  // Sequence of the edges of the shape
+  TopTools_SequenceOfShape aSeqEdges;
+  const Standard_Integer aNbE = aMapEdgeFaces.Extent();
+  for (Standard_Integer i = 1; i <= aNbE; ++i)
+    aSeqEdges.Append(aMapEdgeFaces.FindKey(i));
+
+  // Prepare map of shared vertices (with the number of connected edges greater then 2)
+  TopTools_MapOfShape aSharedVert;
+  CheckSharedVertices(aSeqEdges, aMapEdgesVertex, myKeepShapes, aSharedVert);
+  // Merge the edges avoiding removal of the shared vertices
+  Standard_Boolean isMerged = MergeSeq(aSeqEdges, myAngTol, myLinTol, myConcatBSplines,
+                                       mySafeInputMode, myContext, aSharedVert);
+  // Collect faces to rebuild
+  TopTools_IndexedMapOfShape aChangedFaces;
+  if (isMerged)
   {
-    TopTools_SequenceOfShape SeqEdges;
-    TopExp_Explorer expE(exp.Current(), TopAbs_EDGE);
-    for (; expE.More(); expE.Next())
-      SeqEdges.Append(expE.Current());
-    SharedVert.Clear();
-    CheckSharedVertices(SeqEdges, aMapEdgesVertex, myKeepShapes, SharedVert); 
-    MergeSeq(SeqEdges, myAngTol, myConcatBSplines, mySafeInputMode, myContext,
-      SharedVert);
-  }
-
-  // processing each face
-  for (exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
-    TopoDS_Shape aFace = exp.Current().Oriented(TopAbs_FORWARD);
-    TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
-    TopTools_SequenceOfShape aNonSharedEdges;
-    for (TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
-      TopoDS_Edge edge = TopoDS::Edge(expe.Current());
-      const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
-      TopTools_ListIteratorOfListOfShape anIter(aList);
-      Standard_Integer NbFacesPerEdge = aList.Extent();
-      for ( ; anIter.More(); anIter.Next()) {
-        const TopoDS_Shape& aFace1 = anIter.Value();
-        if (aFace1.IsSame(aFace) && NbFacesPerEdge != 1)
-          continue;
-        if (NbFacesPerEdge == 1)
-          //store non-shared edges separately 
-          aNonSharedEdges.Append(edge);
-        else 
-        {
-          if (aMapFacesEdges.Contains(aFace1))
-            aMapFacesEdges.ChangeFromKey(aFace1).Append(edge);
-          else 
-          {
-            TopTools_ListOfShape ListEdges;
-            ListEdges.Append(edge);
-            aMapFacesEdges.Add(aFace1, ListEdges);
-          }
-        }
-      }
-    }
-      
-    for (Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++)
-    {
-      const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
-      TopTools_SequenceOfShape SeqEdges;
-      TopTools_ListIteratorOfListOfShape anIter(ListEdges);
-      for ( ; anIter.More(); anIter.Next())
-        SeqEdges.Append(anIter.Value());
-      if (SeqEdges.Length()==1) 
-        continue;  
-
-      SharedVert.Clear();
-      CheckSharedVertices(SeqEdges, aMapEdgesVertex, myKeepShapes, SharedVert);
-      if (MergeSeq(SeqEdges, myAngTol, myConcatBSplines, mySafeInputMode,
-        myContext, SharedVert))
-      {
-        TopoDS_Face tmpF = TopoDS::Face(exp.Current());
-        if ( !ChangedFaces.Contains(tmpF) )
-          ChangedFaces.Add(tmpF);
-        tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
-        if ( !ChangedFaces.Contains(tmpF) )
-          ChangedFaces.Add(tmpF);
-      }
-    }
-    
-    if ( aNonSharedEdges.Length() > 1 )
+    for (Standard_Integer i = 1; i <= aNbE; ++i)
     {
-      SharedVert.Clear();
-      CheckSharedVertices(aNonSharedEdges, aMapEdgesVertex, myKeepShapes, SharedVert);
-      if (MergeSeq(aNonSharedEdges, myAngTol, myConcatBSplines, mySafeInputMode,
-        myContext, SharedVert))
+      const TopoDS_Shape& aE = aMapEdgeFaces.FindKey(i);
+      if (myContext->IsRecorded(aE))
       {
-        TopoDS_Face tmpF = TopoDS::Face(exp.Current());
-        if ( !ChangedFaces.Contains(tmpF) )
-          ChangedFaces.Add(tmpF);
+        TopTools_ListIteratorOfListOfShape it(aMapEdgeFaces(i));
+        for (; it.More(); it.Next())
+          aChangedFaces.Add(it.Value());
       }
     }
-
-  } // end processing each face
+  }
 
   // fix changed faces and replace them in the local context
   Standard_Real aPrec = Precision::Confusion();
-  for (Standard_Integer i = 1; i <= ChangedFaces.Extent(); i++) {
-    TopoDS_Face aFace = TopoDS::Face(myContext->Apply(ChangedFaces.FindKey(i)));
+  for (Standard_Integer i = 1; i <= aChangedFaces.Extent(); i++) {
+    TopoDS_Face aFace = TopoDS::Face(myContext->Apply(aChangedFaces.FindKey(i)));
     if (aFace.IsNull())
       continue;
 
@@ -1754,7 +1731,7 @@ void ShapeUpgrade_UnifySameDomain::UnifyEdges()
     myContext->Replace(aFace,aNewFace);
   }
 
-  if (ChangedFaces.Extent() > 0) {
+  if (aChangedFaces.Extent() > 0) {
     // fix changed shell and replace it in the local context
     TopoDS_Shape aRes1 = myContext->Apply(aRes);
     Standard_Boolean isChanged = Standard_False;