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