Salome HOME
058005d9fdc62d228ad29fc929ecba8f07ac42c7
[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 #include <GeomConvert_BSplineCurveToBezierCurve.hxx>
21 #include <Geom_BezierCurve.hxx>
22 #include <gp_Pnt.hxx>
23 #include <TColgp_HArray1OfPnt.hxx>
24 #include <QPainterPath>
25
26 #ifndef LIGHT_MODE
27 #include <CurveCreator_Utils.hxx>
28 #endif
29
30 Handle(Geom_BSplineCurve) HYDROData_BSplineOperation::ComputeCurve(
31   const NCollection_Sequence<gp_XYZ>& thePoints,
32   const bool                          theIsClosed,
33   const double                        theTolerance )
34 {
35   // skip equal points
36   int aNbPoints = thePoints.Size();
37   NCollection_Sequence<gp_XYZ> aPoints;
38   if ( aNbPoints > 0 ) {
39     gp_XYZ aPrevPoint = thePoints.Value( 1 );
40     aPoints.Append( aPrevPoint );
41     for( int i = 2 ; i <= aNbPoints; ++i )
42     {
43       gp_XYZ aPoint( thePoints.Value( i ) );
44       if ( !aPoint.IsEqual( aPrevPoint, theTolerance ) )
45         aPoints.Append( aPoint );
46       aPrevPoint = aPoint;
47     }
48   }
49
50   // fill array for algorithm by the received coordinates
51   aNbPoints = aPoints.Size();
52   Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt( 1, aNbPoints );
53   for ( int i = 1; i <= aNbPoints; i++ )
54   {
55     gp_Pnt aPnt( aPoints.Value( i ) );
56     aHCurvePoints->SetValue( i, aPnt );
57   }
58
59   // compute BSpline
60   Handle(Geom_BSplineCurve) aBSpline;
61 #ifndef LIGHT_MODE
62   if( CurveCreator_Utils::constructBSpline( aHCurvePoints, theIsClosed, aBSpline ) )
63     return aBSpline;
64   else
65 #endif
66     return Handle(Geom_BSplineCurve)();
67 }
68
69 void HYDROData_BSplineOperation::ComputePath( const Handle(Geom_BSplineCurve)& theCurve,
70                                               QPainterPath& thePath )
71 {
72   if ( theCurve.IsNull() ) // returns an empty Path if original curve is invalid
73     return;
74
75   GeomConvert_BSplineCurveToBezierCurve aConverter(theCurve);
76   int a, aNumArcs = aConverter.NbArcs();
77   for(a = 1; a <= aNumArcs; a++)
78   {
79     Handle(Geom_BezierCurve) anArc = aConverter.Arc(a);
80     if (a == 1) { // set a start point
81       gp_Pnt aStart = anArc->StartPoint();
82       thePath.moveTo(aStart.X(), aStart.Y());
83     }
84     gp_Pnt anEnd = anArc->EndPoint();
85     if (anArc->NbPoles() == 3) { // quadric segment in the path (pole 1 is start, pole 3 is end)
86       gp_Pnt aPole = anArc->Pole(2);
87       thePath.quadTo(aPole.X(), aPole.Y(), anEnd.X(), anEnd.Y());
88     } else if (anArc->NbPoles() == 4) { // cubic segment (usually this is used)
89       gp_Pnt aPole1 = anArc->Pole(2);
90       gp_Pnt aPole2 = anArc->Pole(3);
91       thePath.cubicTo(
92         aPole1.X(), aPole1.Y(), aPole2.X(), aPole2.Y(), anEnd.X(), anEnd.Y());
93     } else { // error, another number of poles is not supported
94       continue;
95     }
96   }
97 }