Salome HOME
Dump to Python script correction.
[modules/hydro.git] / src / HYDROData / HYDROData_BSplineOperation.cxx
1 #include<HYDROData_BSplineOperation.h>
2
3 #include<TColgp_HArray1OfPnt.hxx>
4 #include<GeomAPI_Interpolate.hxx>
5 #include<GeomConvert_BSplineCurveToBezierCurve.hxx>
6 #include<Geom_BezierCurve.hxx>
7
8 HYDROData_BSplineOperation::HYDROData_BSplineOperation(
9   const QList<double>& thePoints,
10   const double theZValue,
11   const bool theIsClosed)
12 {
13   // fill array for algorithm by the received coordinates
14   int aLen = thePoints.size() / 2;
15   Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
16   QList<double>::const_iterator aListIter = thePoints.begin();
17   for (int ind = 1; ind <= aLen; ind++) {
18     gp_Pnt aPnt(gp::Origin());
19     aPnt.SetX(*aListIter);
20     aListIter++;
21     aPnt.SetY(*aListIter);
22     aListIter++;
23     aPnt.SetZ(theZValue);
24     aHCurvePoints->SetValue(ind, aPnt);
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
34 QPainterPath HYDROData_BSplineOperation::ComputePath() const
35 {
36   QPainterPath aResult;
37   if (myCurve.IsNull()) // returns an empty Path if original curve is invalid
38     return aResult;
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 }