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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROData_BSplineOperation.h>
20 #include <GeomConvert_BSplineCurveToBezierCurve.hxx>
21 #include <Geom_BezierCurve.hxx>
23 #include <TColgp_HArray1OfPnt.hxx>
24 #include <QPainterPath>
26 #include <CurveCreator_Utils.hxx>
28 Handle(Geom_BSplineCurve) HYDROData_BSplineOperation::ComputeCurve(
29 const NCollection_Sequence<gp_XYZ>& thePoints,
30 const bool theIsClosed,
31 const double theTolerance )
34 int aNbPoints = thePoints.Size();
35 NCollection_Sequence<gp_XYZ> aPoints;
36 if ( aNbPoints > 0 ) {
37 gp_XYZ aPrevPoint = thePoints.Value( 1 );
38 aPoints.Append( aPrevPoint );
39 for( int i = 2 ; i <= aNbPoints; ++i )
41 gp_XYZ aPoint( thePoints.Value( i ) );
42 if ( !aPoint.IsEqual( aPrevPoint, theTolerance ) )
43 aPoints.Append( aPoint );
48 // fill array for algorithm by the received coordinates
49 aNbPoints = aPoints.Size();
50 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt( 1, aNbPoints );
51 for ( int i = 1; i <= aNbPoints; i++ )
53 gp_Pnt aPnt( aPoints.Value( i ) );
54 aHCurvePoints->SetValue( i, aPnt );
58 Handle(Geom_BSplineCurve) aBSpline;
59 if( CurveCreator_Utils::constructBSpline( aHCurvePoints, theIsClosed, aBSpline ) )
62 return Handle(Geom_BSplineCurve)();
65 void HYDROData_BSplineOperation::ComputePath( const Handle(Geom_BSplineCurve)& theCurve,
66 QPainterPath& thePath )
68 if ( theCurve.IsNull() ) // returns an empty Path if original curve is invalid
71 GeomConvert_BSplineCurveToBezierCurve aConverter(theCurve);
72 int a, aNumArcs = aConverter.NbArcs();
73 for(a = 1; a <= aNumArcs; a++)
75 Handle(Geom_BezierCurve) anArc = aConverter.Arc(a);
76 if (a == 1) { // set a start point
77 gp_Pnt aStart = anArc->StartPoint();
78 thePath.moveTo(aStart.X(), aStart.Y());
80 gp_Pnt anEnd = anArc->EndPoint();
81 if (anArc->NbPoles() == 3) { // quadric segment in the path (pole 1 is start, pole 3 is end)
82 gp_Pnt aPole = anArc->Pole(2);
83 thePath.quadTo(aPole.X(), aPole.Y(), anEnd.X(), anEnd.Y());
84 } else if (anArc->NbPoles() == 4) { // cubic segment (usually this is used)
85 gp_Pnt aPole1 = anArc->Pole(2);
86 gp_Pnt aPole2 = anArc->Pole(3);
88 aPole1.X(), aPole1.Y(), aPole2.X(), aPole2.Y(), anEnd.X(), anEnd.Y());
89 } else { // error, another number of poles is not supported