Salome HOME
Minor changes.
[modules/hydro.git] / src / HYDROData / HYDROData_BSplineOperation.cxx
1
2 #include <HYDROData_BSplineOperation.h>
3
4 #include <TColgp_HArray1OfPnt.hxx>
5
6 #include <GeomAPI_Interpolate.hxx>
7 #include <GeomConvert_BSplineCurveToBezierCurve.hxx>
8 #include <Geom_BezierCurve.hxx>
9
10 #include <QPainterPath>
11
12 HYDROData_BSplineOperation::HYDROData_BSplineOperation(
13   const NCollection_Sequence<gp_XYZ>& thePoints,
14   const bool                          theIsClosed )
15 {
16   // fill array for algorithm by the received coordinates
17   int aNbPoints = thePoints.Size();
18
19   Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt( 1, aNbPoints );
20   for ( int i = 1; i <= aNbPoints; i++ )
21   {
22     gp_Pnt aPnt( thePoints.Value( i ) );
23     aHCurvePoints->SetValue( i, aPnt );
24   }
25
26   // compute BSpline
27   GeomAPI_Interpolate aGBC( aHCurvePoints, theIsClosed, gp::Resolution() );
28   aGBC.Perform();
29   if ( aGBC.IsDone() )
30     myCurve = aGBC.Curve();
31 }
32
33 QPainterPath HYDROData_BSplineOperation::ComputePath() const
34 {
35   QPainterPath aResult;
36   if ( myCurve.IsNull() ) // returns an empty Path if original curve is invalid
37     return aResult;
38
39   GeomConvert_BSplineCurveToBezierCurve aConverter(myCurve);
40   int a, aNumArcs = aConverter.NbArcs();
41   for(a = 1; a <= aNumArcs; a++) {
42     Handle(Geom_BezierCurve) anArc = aConverter.Arc(a);
43     if (a == 1) { // set a start point
44       gp_Pnt aStart = anArc->StartPoint();
45       aResult.moveTo(aStart.X(), aStart.Y());
46     }
47     gp_Pnt anEnd = anArc->EndPoint();
48     if (anArc->NbPoles() == 3) { // quadric segment in the path (pole 1 is start, pole 3 is end)
49       gp_Pnt aPole = anArc->Pole(2);
50       aResult.quadTo(aPole.X(), aPole.Y(), anEnd.X(), anEnd.Y());
51     } else if (anArc->NbPoles() == 4) { // cubic segment (usually this is used)
52       gp_Pnt aPole1 = anArc->Pole(2);
53       gp_Pnt aPole2 = anArc->Pole(3);
54       aResult.cubicTo(
55         aPole1.X(), aPole1.Y(), aPole2.X(), aPole2.Y(), anEnd.X(), anEnd.Y());
56     } else { // error, another number of poles is not supported
57       return QPainterPath();
58     }
59   }
60   return aResult;
61 }