Salome HOME
debug salome tests
[modules/hydro.git] / src / HYDROData / HYDROData_TopoCurve.cxx
index 705b1b2bb521ec68a532fee813c5c2704043a50a..2323240709a5ca436f4d7c3a041ed3de93549f0c 100644 (file)
@@ -1,8 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
-//
-// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-//
+// Copyright (C) 2014-2015  EDF-R&D
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
 #include <TopTools_ListOfShape.hxx>
 
+//#define _DEVDEBUG_
+#include "HYDRO_trace.hxx"
+#include <BRepTools.hxx>
+
 //! The type is intended to traverse the container
 //! either from the begin to the end or vice versa.
 template<typename ContainerType, typename IteratorType>
@@ -134,7 +134,7 @@ static TopoDS_Edge ReplaceVertex(
 }
 
 // Projects the point to the curve.
-static double ProjectPointToCurve(
+double ProjectPointToCurve(
   const gp_XYZ& thePoint,
   const Adaptor3d_Curve& theCurve,
   double& theParameter)
@@ -283,12 +283,13 @@ static int IntersectCurve(
     if (ProjectPointToCurve(aEndPs[aPI], theCurve1, aParameter) <=
       Precision::SquareConfusion())
     {
+      DEBTRACE("aParameter " << aParameter);
       aIntCount += AddParameter(theCurve1, aParameter, theParameters);
     }
   }
 
   // Process the internal extremums.
-  Extrema_ExtCC aAlgo(theCurve1, theCurve2);
+  Extrema_ExtCC aAlgo(theCurve1, theCurve2); //, 1.e-6, 1.e-6);
   if (aAlgo.IsDone())
   {
     const int aECount = aAlgo.NbExt();
@@ -296,9 +297,11 @@ static int IntersectCurve(
     {
       Extrema_POnCurv aP1, aP2;
       aAlgo.Points(aEN, aP1, aP2);
+      DEBTRACE("SquareDistance " << aP1.Value().SquareDistance(aP2.Value()));
       if (aP1.Value().SquareDistance(aP2.Value()) <=
         Precision::SquareConfusion())
       {
+        DEBTRACE("aP1.Parameter() " << aP1.Parameter());
         aIntCount += AddParameter(theCurve1, aP1.Parameter(), theParameters);
       }
     }
@@ -367,12 +370,13 @@ static bool Interpolate(
 
 bool HYDROData_TopoCurve::Initialize(const TopoDS_Wire& theWire)
 {
-  // Check for nonemptiness.
+  // Check for non-emptiness.
   myEdges.clear();
   TopTools_IndexedDataMapOfShapeListOfShape aVertexToEdges;
   TopExp::MapShapesAndAncestors(theWire,
     TopAbs_VERTEX, TopAbs_EDGE, aVertexToEdges);
   const int aVCount = aVertexToEdges.Extent();
+  DEBTRACE("initialize VCount= "<< aVCount);
   if (aVCount == 0)
   {
     return false;
@@ -458,22 +462,24 @@ bool HYDROData_TopoCurve::Initialize(const TopoDS_Wire& theWire)
 TopoDS_Wire HYDROData_TopoCurve::Wire() const
 {
   TopoDS_Wire aWire;
-  BRep_Builder aBulder;
-  aBulder.MakeWire(aWire);
+  BRep_Builder aBuilder;
+  aBuilder.MakeWire(aWire);
   std::list<TopoDS_Edge>::const_iterator aEItLast = myEdges.end();
   std::list<TopoDS_Edge>::const_iterator aEIt = myEdges.begin();
   for (; aEIt != aEItLast; ++aEIt)
   {
-    aBulder.Add(aWire, *aEIt);
+    aBuilder.Add(aWire, *aEIt);
   }
   return aWire;
 }
 
-void HYDROData_TopoCurve::Cut(
+bool HYDROData_TopoCurve::Cut(
   const std::list<TopoDS_Edge>::iterator& theEdgePosition,
   const double theParameter,
   HYDROData_TopoCurve& theCurve)
 {
+  bool aResult = false;
+
   // Locate the edge.
   std::list<TopoDS_Edge>::iterator aFirstEIt = myEdges.begin();
   std::list<TopoDS_Edge>::iterator aEIt = aFirstEIt;
@@ -498,6 +504,8 @@ void HYDROData_TopoCurve::Cut(
   {
     aParamI ^= 1;
   }
+  const bool isClosed = IsClosed();
+  DEBTRACE("aParamI: " << aParamI << " isClosed: "<< isClosed);
   if (aParamI < 0)
   {
     aEdge.Orientation(TopAbs_FORWARD);
@@ -529,19 +537,41 @@ void HYDROData_TopoCurve::Cut(
     *aEIt = aEParts[aFirstPI];
     InsertAfter(aEIt, aEParts[1 - aFirstPI], myEdges);
     ++aEIt;
+
+    aResult = true;
   }
-  else if (aParamI > 0)
+  else
   {
-    ++aEIt;
+    TopoDS_Edge aNewEdge = ReplaceVertex(aEdge, (aParamI == 0) ? false : true);
+    *aEIt = aNewEdge;
+    if (aParamI > 0)
+    {
+      ++aEIt;
+
+      std::list<TopoDS_Edge>::iterator aEdgePosition = theEdgePosition;
+      if (isClosed || ++aEdgePosition != myEdges.end())
+      {
+        aResult = true;
+      }
+    }
+    else
+    {
+      if (isClosed || theEdgePosition != aFirstEIt)
+      {
+        aResult = true;
+      }
+    }
   }
 
   // Calculate the curve parts.
   std::list<TopoDS_Edge>::iterator aLastEIt = myEdges.end();
   if (aEIt != aFirstEIt && aEIt != aLastEIt)
   {
-    std::list<TopoDS_Edge>* aEdges = !IsClosed() ? &theCurve.myEdges : &myEdges;
+    std::list<TopoDS_Edge>* aEdges = !isClosed ? &theCurve.myEdges : &myEdges;
     aEdges->splice(aEdges->begin(), myEdges, aEIt, aLastEIt);
   }
+
+  return aResult;
 }
 
 void HYDROData_TopoCurve::Cut(
@@ -557,23 +587,34 @@ void HYDROData_TopoCurve::Cut(
   theCurve1.Cut(aEPos1, theParameter, theCurve2);
 }
 
-void HYDROData_TopoCurve::Cut(
+bool HYDROData_TopoCurve::Cut(
   const std::deque<std::list<double> >& theParameters,
   std::deque<HYDROData_TopoCurve>& theCurves) const
 {
+  bool aResult = false;
   HYDROData_TopoCurve aCurves[2];
   aCurves[0] = *this;
   int aCI = 0;
   std::list<TopoDS_Edge>::iterator aEIt = aCurves[0].myEdges.begin();
   std::deque<std::list<double> >::const_iterator aPLIt = theParameters.begin();
   for (std::deque<std::list<double> >::const_iterator aLastPLIt =
-    theParameters.end(); aPLIt != aLastPLIt; ++aEIt, ++aPLIt)
+    theParameters.end(); aPLIt != aLastPLIt; ++aPLIt)
   {
+    TopoDS_Edge aNextEdge;
+    {
+      std::list<TopoDS_Edge>::iterator aNextEIt = aEIt;
+      ++aNextEIt;
+      if (aNextEIt != aCurves[aCI].myEdges.end())
+      {
+        aNextEdge = *aNextEIt;
+      }
+    }
+
     for (Iterator<std::list<double>, std::list<double>::const_iterator> aPIt(
       *aPLIt, (aEIt->Orientation() != TopAbs_REVERSED)); aPIt.More(); ++aPIt)
     {
       const int aCI1 = 1 - aCI;
-      aCurves[aCI].Cut(aEIt, **aPIt, aCurves[aCI1]);
+      aResult |= aCurves[aCI].Cut(aEIt, **aPIt, aCurves[aCI1]);
       if (!aCurves[aCI1].IsEmpty())
       {
         theCurves.push_back(HYDROData_TopoCurve());
@@ -586,8 +627,14 @@ void HYDROData_TopoCurve::Cut(
         aEIt = aCurves[aCI].myEdges.begin();
       }
     }
+
+    if (!aNextEdge.IsNull() && !aEIt->IsEqual(aNextEdge))
+    {
+      ++aEIt;
+    }
   }
   theCurves.push_back(aCurves[aCI]);
+  return aResult;
 }
 
 double HYDROData_TopoCurve::Project(
@@ -616,21 +663,29 @@ int HYDROData_TopoCurve::Intersect(
   const TopoDS_Wire& theWire,
   std::deque<std::list<double> >& theParameters) const
 {
+  std::string brepName = "theWireToIntersect";
+  brepName += ".brep";
+  BRepTools::Write( theWire, brepName.c_str() );
+
   int aIntCount = 0;
   theParameters.resize(myEdges.size());
+  DEBTRACE("myEdges.size() " << myEdges.size());
   std::list<TopoDS_Edge>::const_iterator aEIt = myEdges.begin();
   std::list<TopoDS_Edge>::const_iterator aLastEIt = myEdges.end();
   std::deque<std::list<double> >::iterator aPIt = theParameters.begin();
   for (; aEIt != aLastEIt; ++aPIt, ++aEIt)
   {
+    DEBTRACE("---");
     const TopoDS_Edge& aEdge = *aEIt;
     std::list<double>& aParams = *aPIt;
     TopExp_Explorer aEIt2(theWire, TopAbs_EDGE);
     for (; aEIt2.More(); aEIt2.Next())
     {
       aIntCount += IntersectEdge(aEdge,TopoDS::Edge(aEIt2.Current()), aParams);
+      DEBTRACE("aParams.size() " << aParams.size());
     }
   }
+  DEBTRACE("aIntCount " << aIntCount);
   return aIntCount;
 }
 
@@ -808,9 +863,10 @@ bool HYDROData_TopoCurve::Connect(
   return true;
 }
 
-bool HYDROData_TopoCurve::BSplinePiecewiseCurve(
+int HYDROData_TopoCurve::BSplinePiecewiseCurve(
   const double theDeflection, HYDROData_TopoCurve& theCurve) const
 {
+  int aPieceCount = 0;
   std::list<TopoDS_Edge>::const_iterator aLastEIt = myEdges.end();
   std::list<TopoDS_Edge>::const_iterator aEIt = myEdges.begin();
   TopoDS_Vertex aEndVertex;
@@ -821,7 +877,7 @@ bool HYDROData_TopoCurve::BSplinePiecewiseCurve(
       ::BSpline(BRepAdaptor_Curve(*aEIt), theDeflection);
     if (aBSpline.IsNull())
     {
-      return false;
+      return 0;
     }
 
     if (aEIt->Orientation() == TopAbs_REVERSED)
@@ -844,6 +900,7 @@ bool HYDROData_TopoCurve::BSplinePiecewiseCurve(
     }
     BRep_Builder().Add(aEdge, aVertex);
     theCurve.myEdges.push_back(aEdge);
+    aPieceCount += aBSpline->NbKnots() - 1;
     aPrevEdge = aEdge;
   }
 
@@ -854,10 +911,10 @@ bool HYDROData_TopoCurve::BSplinePiecewiseCurve(
       aCurve.Value(aCurve.LastParameter()), Precision::Confusion());
   }
   BRep_Builder().Add(aPrevEdge, aEndVertex.Oriented(TopAbs_REVERSED));
-  return true;
+  return aPieceCount;
 }
 
-bool HYDROData_TopoCurve::ValuesInKnots(std::deque<gp_XYZ>& theValues) const
+bool HYDROData_TopoCurve::ValuesInKnots(std::list<gp_XYZ>& theValues) const
 {
   std::list<TopoDS_Edge>::const_iterator aLastEIt = myEdges.end();
   std::list<TopoDS_Edge>::const_iterator aEIt = myEdges.begin();