0030675: Visualization - remove redundant proxy classes in hierarchy of PrsMgr_Presen...
[occt.git] / src / Geom2dConvert / Geom2dConvert_ApproxCurve.cxx
1 // Created on: 1997-09-11
2 // Created by: Roman BORISOV
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Adaptor2d_HCurve2d.hxx>
19 #include <AdvApprox_ApproxAFunction.hxx>
20 #include <AdvApprox_PrefAndRec.hxx>
21 #include <Geom2d_BSplineCurve.hxx>
22 #include <Geom2d_Curve.hxx>
23 #include <Geom2dAdaptor_HCurve.hxx>
24 #include <Geom2dConvert_ApproxCurve.hxx>
25 #include <gp_Pnt2d.hxx>
26 #include <gp_Vec2d.hxx>
27 #include <Precision.hxx>
28 #include <Standard_OutOfRange.hxx>
29 #include <TColgp_Array1OfPnt2d.hxx>
30 #include <TColStd_HArray1OfReal.hxx>
31
32 //=======================================================================
33 //class : Geom2dConvert_ApproxCurve_Eval
34 //purpose: evaluator class for approximation
35 //=======================================================================
36 class Geom2dConvert_ApproxCurve_Eval : public AdvApprox_EvaluatorFunction
37 {
38  public:
39   Geom2dConvert_ApproxCurve_Eval (const Handle(Adaptor2d_HCurve2d)& theFunc, 
40                                   Standard_Real First, Standard_Real Last)
41     : fonct(theFunc) { StartEndSav[0] = First; StartEndSav[1] = Last; }
42   
43   virtual void Evaluate (Standard_Integer *Dimension,
44                          Standard_Real     StartEnd[2],
45                          Standard_Real    *Parameter,
46                          Standard_Integer *DerivativeRequest,
47                          Standard_Real    *Result, // [Dimension]
48                          Standard_Integer *ErrorCode);
49   
50  private:
51   Handle(Adaptor2d_HCurve2d) fonct;
52   Standard_Real StartEndSav[2];
53 };
54
55 void Geom2dConvert_ApproxCurve_Eval::Evaluate (Standard_Integer *Dimension,
56                                                Standard_Real     StartEnd[2],
57                                                Standard_Real    *Param, // Parameter at which evaluation
58                                                Standard_Integer *Order, // Derivative Request
59                                                Standard_Real    *Result,// [Dimension]
60                                                Standard_Integer *ErrorCode)
61 {
62   *ErrorCode = 0;
63   Standard_Real par = *Param;
64
65 // Dimension is incorrect
66   if (*Dimension!=2) {
67     *ErrorCode = 1;
68   }
69 // Parameter is incorrect
70   if ( par < StartEnd[0] || par > StartEnd[1] ) {
71     *ErrorCode = 2;
72   }
73   if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1]) 
74     {
75       fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
76       StartEndSav[0]=StartEnd[0];
77       StartEndSav[1]=StartEnd[1];
78     }
79
80   gp_Pnt2d pnt;
81   gp_Vec2d v1, v2;
82
83   switch (*Order) {
84   case 0:
85     pnt = fonct->Value(par);
86     Result[0] = pnt.X();
87     Result[1] = pnt.Y();
88     break;
89   case 1:
90     fonct->D1(par, pnt, v1);
91     Result[0] = v1.X();
92     Result[1] = v1.Y();
93     break;
94   case 2:
95     fonct->D2(par, pnt, v1, v2);
96     Result[0] = v2.X();
97     Result[1] = v2.Y();
98     break;
99   default:
100     Result[0] = Result[1] = 0.;
101     *ErrorCode = 3;
102     break;
103   }
104 }
105
106 Geom2dConvert_ApproxCurve::Geom2dConvert_ApproxCurve(const Handle(Geom2d_Curve)& Curve,const Standard_Real Tol2d,const GeomAbs_Shape Order,const Standard_Integer MaxSegments,const Standard_Integer MaxDegree)
107 {
108   Handle(Geom2dAdaptor_HCurve) HCurve = new Geom2dAdaptor_HCurve (Curve);
109   Approximate(HCurve, Tol2d, Order, MaxSegments, MaxDegree);
110 }
111
112 Geom2dConvert_ApproxCurve::Geom2dConvert_ApproxCurve(const Handle(Adaptor2d_HCurve2d)& Curve,
113                                                      const Standard_Real               Tol2d,
114                                                      const GeomAbs_Shape               Order,
115                                                      const Standard_Integer            MaxSegments,
116                                                      const Standard_Integer            MaxDegree)
117 {
118   Approximate(Curve, Tol2d, Order, MaxSegments, MaxDegree);
119 }
120
121 void Geom2dConvert_ApproxCurve::Approximate(const Handle(Adaptor2d_HCurve2d)& theCurve,
122                                             const Standard_Real               theTol2d,
123                                             const GeomAbs_Shape               theOrder,
124                                             const Standard_Integer            theMaxSegments,
125                                             const Standard_Integer            theMaxDegree)
126 {
127   // Initialisation of input parameters of AdvApprox
128
129   Standard_Integer Num1DSS=0, Num2DSS=1, Num3DSS=0;
130   Handle(TColStd_HArray1OfReal) OneDTolNul, ThreeDTolNul;
131   Handle(TColStd_HArray1OfReal) TwoDTol  = new TColStd_HArray1OfReal(1,Num2DSS);
132   TwoDTol->Init(theTol2d);
133
134   Standard_Real First = theCurve->FirstParameter();
135   Standard_Real Last  = theCurve->LastParameter();
136
137   Standard_Integer NbInterv_C2 = theCurve->NbIntervals(GeomAbs_C2);
138   TColStd_Array1OfReal CutPnts_C2(1, NbInterv_C2+1);
139   theCurve->Intervals(CutPnts_C2,GeomAbs_C2);
140   Standard_Integer NbInterv_C3 = theCurve->NbIntervals(GeomAbs_C3);
141   TColStd_Array1OfReal CutPnts_C3(1, NbInterv_C3+1);
142   theCurve->Intervals(CutPnts_C3,GeomAbs_C3);
143   AdvApprox_PrefAndRec CutTool(CutPnts_C2,CutPnts_C3);
144
145   myMaxError = 0;
146
147   Geom2dConvert_ApproxCurve_Eval ev (theCurve, First, Last);
148   AdvApprox_ApproxAFunction aApprox (Num1DSS, Num2DSS, Num3DSS, 
149                                      OneDTolNul, TwoDTol, ThreeDTolNul,
150                                      First, Last, theOrder,
151                                      theMaxDegree, theMaxSegments,
152                                      ev, CutTool);
153
154   myIsDone = aApprox.IsDone();
155   myHasResult = aApprox.HasResult();
156
157   if (myHasResult) {
158     TColgp_Array1OfPnt2d Poles(1,aApprox.NbPoles());
159     aApprox.Poles2d(1,Poles);
160     Handle(TColStd_HArray1OfReal)    Knots = aApprox.Knots();
161     Handle(TColStd_HArray1OfInteger) Mults = aApprox.Multiplicities();
162     Standard_Integer Degree = aApprox.Degree();
163     myBSplCurve = new Geom2d_BSplineCurve(Poles, Knots->Array1(), Mults->Array1(), Degree);
164     myMaxError = aApprox.MaxError(2, 1);
165   } 
166 }
167
168  Handle(Geom2d_BSplineCurve) Geom2dConvert_ApproxCurve::Curve() const
169 {
170   return myBSplCurve;
171 }
172
173  Standard_Boolean Geom2dConvert_ApproxCurve::IsDone() const
174 {
175   return myIsDone; 
176 }
177
178  Standard_Boolean Geom2dConvert_ApproxCurve::HasResult() const
179 {
180   return myHasResult; 
181 }
182
183  Standard_Real Geom2dConvert_ApproxCurve::MaxError() const
184 {
185   return myMaxError;
186 }
187
188  void Geom2dConvert_ApproxCurve::Dump(Standard_OStream& o) const
189 {
190   o << "******* Dump of ApproxCurve *******" << endl;
191   o << "******* Error   " << MaxError() << endl;
192 }
193