Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' into BR_v14_rc
[modules/hydro.git] / src / HYDROData / HYDROData_BSplineOperation.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROData_BSplineOperation.h>
20
21 #include <TColgp_HArray1OfPnt.hxx>
22
23 #include <GeomAPI_Interpolate.hxx>
24 #include <GeomConvert_BSplineCurveToBezierCurve.hxx>
25 #include <Geom_BezierCurve.hxx>
26
27 #include <QPainterPath>
28
29 HYDROData_BSplineOperation::HYDROData_BSplineOperation(
30   const NCollection_Sequence<gp_XYZ>& thePoints,
31   const bool                          theIsClosed,
32   const double                        theTolerance )
33 {
34   // skip equal points
35   int aNbPoints = thePoints.Size();
36   NCollection_Sequence<gp_XYZ> aPoints;
37   if ( aNbPoints > 0 ) {
38     gp_XYZ aPrevPoint = thePoints.Value( 1 );
39     aPoints.Append( aPrevPoint );
40     for( int i = 2 ; i <= aNbPoints; ++i )
41     {
42       gp_XYZ aPoint( thePoints.Value( i ) );
43       if ( !aPoint.IsEqual( aPrevPoint, theTolerance ) )
44         aPoints.Append( aPoint );
45       aPrevPoint = aPoint;
46     }
47   }
48
49   // fill array for algorithm by the received coordinates
50   aNbPoints = aPoints.Size();
51   Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt( 1, aNbPoints );
52   for ( int i = 1; i <= aNbPoints; i++ )
53   {
54     gp_Pnt aPnt( aPoints.Value( i ) );
55     aHCurvePoints->SetValue( i, aPnt );
56   }
57
58   // compute BSpline
59   GeomAPI_Interpolate aGBC( aHCurvePoints, theIsClosed, gp::Resolution() );
60   aGBC.Perform();
61   if ( aGBC.IsDone() )
62     myCurve = aGBC.Curve();
63 }
64
65 void HYDROData_BSplineOperation::ComputePath( QPainterPath& thePath ) const
66 {
67   if ( myCurve.IsNull() ) // returns an empty Path if original curve is invalid
68     return;
69
70   GeomConvert_BSplineCurveToBezierCurve aConverter(myCurve);
71   int a, aNumArcs = aConverter.NbArcs();
72   for(a = 1; a <= aNumArcs; a++)
73   {
74     Handle(Geom_BezierCurve) anArc = aConverter.Arc(a);
75     if (a == 1) { // set a start point
76       gp_Pnt aStart = anArc->StartPoint();
77       thePath.moveTo(aStart.X(), aStart.Y());
78     }
79     gp_Pnt anEnd = anArc->EndPoint();
80     if (anArc->NbPoles() == 3) { // quadric segment in the path (pole 1 is start, pole 3 is end)
81       gp_Pnt aPole = anArc->Pole(2);
82       thePath.quadTo(aPole.X(), aPole.Y(), anEnd.X(), anEnd.Y());
83     } else if (anArc->NbPoles() == 4) { // cubic segment (usually this is used)
84       gp_Pnt aPole1 = anArc->Pole(2);
85       gp_Pnt aPole2 = anArc->Pole(3);
86       thePath.cubicTo(
87         aPole1.X(), aPole1.Y(), aPole2.X(), aPole2.Y(), anEnd.X(), anEnd.Y());
88     } else { // error, another number of poles is not supported
89       continue;
90     }
91   }
92 }