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