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