Salome HOME
Minor changes.
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineXY.cxx
index 6c17dd6dc14dc37024eeebd86601e657c545f64f..0d79dd238678eeb86abac756dc34ea566189eda2 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>
@@ -80,7 +85,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,6 +110,85 @@ 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() );
+
+      TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
+      aMakeWire.Add( anEdge );
+    }
+  }
+  else //if( theType == PolylineSection::SECTION_SPLINE )
+  {
+    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed );
+
+    TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
+    aMakeWire.Add( anEdge );
+  }
+
+  TopoDS_Wire aWire;
+  aMakeWire.Build();
+  if ( aMakeWire.IsDone() )
+    aWire = aMakeWire;
+
+  return aWire;
+}
+
+QPainterPath HYDROData_PolylineXY::BuildPainterPath( const SectionType&                  theType,
+                                                     const bool&                         theIsClosed,
+                                                     const NCollection_Sequence<gp_XYZ>& thePoints )
+{
+  QPainterPath aPath;
+  if ( thePoints.IsEmpty() )
+    return aPath;
+
+  if ( theType == SECTION_POLYLINE )
+  {
+    aPath.moveTo( thePoints.Value( 1 ).X(), thePoints.Value( 1 ).Y() );
+
+    for( int i = 2, n = thePoints.Size(); i <= n; ++i )
+    {
+      const gp_XYZ& aSectPoint = thePoints.Value( i );
+
+      aPath.lineTo( aSectPoint.X(), aSectPoint.Y() );
+    }
+
+    if( theIsClosed )
+      aPath.closeSubpath();
+  }
+  else
+  {
+    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed );
+    aPath = aBSpline.ComputePath();
+  }
+
+  return aPath;
+}
+
 void HYDROData_PolylineXY::Update()
 {
   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
@@ -125,50 +209,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 +269,70 @@ 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;
+    }
+  }
+  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 );
+
+    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;
@@ -636,34 +751,19 @@ QPainterPath HYDROData_PolylineXY::GetPainterPath() const
   SectionType aSectionType = aSectTypes.Value( 1 );
   bool anIsSectionClosed = aSectClosures.Value( 1 );
 
-  if ( aSectionType == SECTION_POLYLINE )
+  NCollection_Sequence<gp_XYZ> aPoints;
+  for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
   {
-    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 );
+    const Point& aSectPoint = aSectPointsList.Value( i );
 
-      aPath.lineTo( aSectPoint.X(), aSectPoint.Y() );
-    }
-
-    if( anIsSectionClosed )
-      aPath.closeSubpath();
+    gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
+    aPoints.Append( aPoint );
   }
-  else
-  {
-    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, anIsSectionClosed );
-    aPath = aBSpline.ComputePath();
-  }
+  aPath = BuildPainterPath( aSectionType, anIsSectionClosed, aPoints );
 
   return aPath;
+
 }