Salome HOME
Bug #202 - Fatal error during polyline creation
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineXY.cxx
index 6c17dd6dc14dc37024eeebd86601e657c545f64f..18dc1e4c663e2bed89a8a823d1af7ee06fb35f1e 100755 (executable)
@@ -9,6 +9,11 @@
 #include <BRepBuilderAPI_MakeEdge.hxx>
 #include <BRepBuilderAPI_MakeWire.hxx>
 
+#include <GeomAPI_ProjectPointOnCurve.hxx>
+#include <GeomAdaptor_Curve.hxx>
+
+#include <GCPnts_AbscissaPoint.hxx>
+
 #include <ImageComposer_MetaTypes.h>
 
 #include <gp_Pnt.hxx>
@@ -35,6 +40,8 @@
 
 #define PYTHON_POLYLINEXY_ID "KIND_POLYLINEXY"
 
+const double LOCAL_SELECTION_TOLERANCE = 0.0001;
+
 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
 
@@ -80,7 +87,7 @@ QStringList HYDROData_PolylineXY::DumpToPython( MapOfTreatedObjects& theTreatedO
     HYDROData_PolylineXY::PointsList aSectPointsList = GetPoints( i );
     for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
     {
-      const HYDROData_PolylineXY::Point& aSectPoint = aSectPointsList.Value( k );
+      const Point& aSectPoint = aSectPointsList.Value( k );
 
       aResList << QString( "%1.AddPoint( %2, QPointF( %3, %4 ) );" ).arg( aPolylineName )
         .arg( i - 1 ).arg( aSectPoint.X() ).arg( aSectPoint.Y() );
@@ -105,8 +112,91 @@ TopoDS_Shape HYDROData_PolylineXY::GetShape()
   return getPolylineShape();
 }
 
+TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&                  theType,
+                                             const bool&                         theIsClosed,
+                                             const NCollection_Sequence<gp_XYZ>& thePoints )
+{
+  BRepBuilderAPI_MakeWire aMakeWire;
+  
+  if( theType == SECTION_POLYLINE )
+  {
+    for( int i = 1, n = thePoints.Size(); i <= n; ++i )
+    {
+      const gp_XYZ& aFirstPoint = thePoints.Value( i );
+
+      gp_XYZ aLastPoint;
+      if ( i == n )
+      {
+        if( theIsClosed )
+          aLastPoint = thePoints.Value( 1 );
+        else
+          break;
+      }
+      else
+      {
+        aLastPoint = thePoints.Value( i + 1 );
+      }
+
+      gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
+      gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), aLastPoint.Z() );
+
+      if ( aPnt1.IsEqual( aPnt2, LOCAL_SELECTION_TOLERANCE ) )
+        continue;
+
+      TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
+      aMakeWire.Add( anEdge );
+    }
+  }
+  else //if( theType == PolylineSection::SECTION_SPLINE )
+  {
+    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
+
+    TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
+    aMakeWire.Add( anEdge );
+  }
+
+  TopoDS_Wire aWire;
+  aMakeWire.Build();
+  if ( aMakeWire.IsDone() )
+    aWire = aMakeWire;
+
+  return aWire;
+}
+
+void HYDROData_PolylineXY::BuildPainterPath( QPainterPath&                       thePath,
+                                             const SectionType&                  theType,
+                                             const bool&                         theIsClosed,
+                                             const NCollection_Sequence<gp_XYZ>& thePoints )
+{
+  if ( thePoints.IsEmpty() )
+    return;
+
+  if ( theType == SECTION_POLYLINE )
+  {
+    const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
+    thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
+
+    for( int i = 2, n = thePoints.Size(); i <= n; ++i )
+    {
+      const gp_XYZ& aSectPoint = thePoints.Value( i );
+
+      thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
+    }
+
+    if( theIsClosed )
+      thePath.closeSubpath();
+  }
+  else
+  {
+    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
+    aBSpline.ComputePath( thePath );
+  }
+}
+
 void HYDROData_PolylineXY::Update()
 {
+  HYDROData_IPolyline::Update();
+
   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
   NCollection_Sequence<bool>                              aSectClosures;
@@ -125,50 +215,17 @@ void HYDROData_PolylineXY::Update()
     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
     if ( aSectPointsList.IsEmpty() )
       continue;
-
-    BRepBuilderAPI_MakeWire aMakeSectionWire;
-    if( aSectionType == SECTION_POLYLINE )
+    
+    NCollection_Sequence<gp_XYZ> aPoints;
+    for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
     {
-      for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
-      {
-        const Point& aFirstPoint = aSectPointsList.Value( i );
-
-        Point aLastPoint;
-        if ( i == n )
-        {
-          if( anIsSectionClosed )
-            aLastPoint = aSectPointsList.Value( 1 );
-          else
-            break;
-        }
-        else
-        {
-          aLastPoint = aSectPointsList.Value( i + 1 );
-        }
-
-        gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0.0 );
-        gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0.0 );
+      const Point& aSectPoint = aSectPointsList.Value( i );
 
-        TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
-        aMakeSectionWire.Add( anEdge );
-      }
+      gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
+      aPoints.Append( aPoint );
     }
-    else //if( aSectionType == PolylineSection::SECTION_SPLINE )
-    {
-      QList<double> aPoints;
-      for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
-      {
-        const Point& aSectPoint = aSectPointsList.Value( i );
-        aPoints << aSectPoint.X() << aSectPoint.Y();
-      }
 
-      HYDROData_BSplineOperation aBSpline( aPoints, 0.0, anIsSectionClosed );
-
-      TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
-      aMakeSectionWire.Add( anEdge );
-    }
-   
-    TopoDS_Wire aSectionWire = aMakeSectionWire.Wire();
+    TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
     aSectionWiresList.Append( aSectionWire );
     aMakeWire.Add( aSectionWire );
   }
@@ -218,6 +275,73 @@ bool HYDROData_PolylineXY::IsClosed() const
   return anIsClosed;
 }
 
+double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
+                                          const int thePointIndex ) const
+{
+  double aResDistance = -1;
+  if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
+    return aResDistance;
+
+  if ( thePointIndex == 0 )
+    return 0.0;
+
+  SectionType aSectionType = GetSectionType( theSectionIndex );
+  bool anIsSectionClosed = IsClosedSection( theSectionIndex );
+  PointsList aSectPointsList = GetPoints( theSectionIndex );
+  if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size()  )
+    return aResDistance;
+
+  if ( aSectionType == SECTION_POLYLINE )
+  {
+    aResDistance = 0.0;
+  
+    Point aPrevPoint = aSectPointsList.Value( 1 );
+    for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
+    {
+      const Point& aSectPoint = aSectPointsList.Value( i );
+      aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
+      aPrevPoint = aSectPoint;
+
+      if ( thePointIndex == i - 1 )
+        break;
+    }
+  }
+  else
+  {
+    gp_XYZ aPointToTest;
+
+    int aSectNbPoints = aSectPointsList.Size();
+    NCollection_Sequence<gp_XYZ> aPoints;
+    for( int i = 1 ; i <= aSectNbPoints; ++i )
+    {
+      const Point& aSectPoint = aSectPointsList.Value( i );
+
+      gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
+      aPoints.Append( aPoint );
+
+      if ( thePointIndex == i - 1 )
+        aPointToTest = aPoint;
+    }
+
+    HYDROData_BSplineOperation aBSpline( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
+
+    Quantity_Parameter aFirstParam = aBSpline.Curve()->FirstParameter();
+    Quantity_Parameter aSecondParam = aBSpline.Curve()->LastParameter();
+
+    if ( thePointIndex != aSectNbPoints - 1 )
+    {
+      GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aBSpline.Curve() );
+      aSecondParam = aProject.LowerDistanceParameter();
+    }
+
+    GeomAdaptor_Curve anAdap( aBSpline.Curve() );
+    
+    aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
+  }
+
+  return aResDistance;
+}
+
 int HYDROData_PolylineXY::NbSections() const
 {
   Handle(TDataStd_ExtStringList) aNamesList;
@@ -265,6 +389,8 @@ void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectNam
   aNamesList->Append( aSectName );
   aTypesList->Append( theSectionType );
   aClosuresList->Append( theIsClosed );
+
+  SetToUpdate( true );
 }
 
 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
@@ -308,6 +434,8 @@ void HYDROData_PolylineXY::SetSectionName( const int                      theSec
   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
   for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
     aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
+
+  SetToUpdate( true );
 }
 
 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
@@ -344,6 +472,8 @@ void HYDROData_PolylineXY::SetSectionType( const int         theSectionIndex,
   TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
   for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
     aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
+
+  SetToUpdate( true );
 }
 
 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
@@ -380,6 +510,8 @@ void HYDROData_PolylineXY::SetSectionClosed( const int  theSectionIndex,
   TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
   for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
     aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
+
+  SetToUpdate( true );
 }
 
 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
@@ -460,12 +592,15 @@ void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
     // Remove points that belongs to removed section
     removePointsLists( theSectionIndex );
   }
+
+  SetToUpdate( true );
 }
 
 void HYDROData_PolylineXY::RemoveSections()
 {
   removeSectionsLists();
   removePointsLists();
+  SetToUpdate( true );
 }
 
 void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
@@ -510,6 +645,8 @@ void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
       aListY->Append( aCoordY );
     }
   }
+
+  SetToUpdate( true );
 }
 
 void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
@@ -559,6 +696,8 @@ void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
       aListY->Append( aCoordY );
     }
   }
+
+  SetToUpdate( true );
 }
 
 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
@@ -596,6 +735,8 @@ void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
       aListY->Append( anIterY.Value() );
     }
   }
+
+  SetToUpdate( true );
 }
 
 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex ) const
@@ -626,41 +767,27 @@ QPainterPath HYDROData_PolylineXY::GetPainterPath() const
   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
   NCollection_Sequence<bool>                              aSectClosures;
   GetSections( aSectNames, aSectTypes, aSectClosures );
-  if( aSectNames.IsEmpty() )
-    return aPath;
-
-  PointsList aSectPointsList = GetPoints( 0 );
-  if( aSectPointsList.IsEmpty() )
-    return aPath;
 
-  SectionType aSectionType = aSectTypes.Value( 1 );
-  bool anIsSectionClosed = aSectClosures.Value( 1 );
-
-  if ( aSectionType == SECTION_POLYLINE )
+  for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
   {
-    aPath.moveTo( aSectPointsList.Value( 1 ).X(), aSectPointsList.Value( 1 ).Y() );
-
-    for( int i = 2, n = aSectPointsList.Size(); i <= n; ++i )
-    {
-      const Point& aSectPoint = aSectPointsList.Value( i );
+    TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
+    SectionType aSectionType = aSectTypes.Value( aSectionId );
+    bool anIsSectionClosed = aSectClosures.Value( aSectionId );
 
-      aPath.lineTo( aSectPoint.X(), aSectPoint.Y() );
-    }
+    PointsList aSectPointsList = GetPoints( aSectionId - 1 );
+    if ( aSectPointsList.IsEmpty() )
+      continue;
 
-    if( anIsSectionClosed )
-      aPath.closeSubpath();
-  }
-  else
-  {
-    QList<double> aPoints;
+    NCollection_Sequence<gp_XYZ> aPoints;
     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
     {
       const Point& aSectPoint = aSectPointsList.Value( i );
-      aPoints << aSectPoint.X() << aSectPoint.Y();
+
+      gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
+      aPoints.Append( aPoint );
     }
 
-    HYDROData_BSplineOperation aBSpline( aPoints, 0, anIsSectionClosed );
-    aPath = aBSpline.ComputePath();
+    BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
   }
 
   return aPath;