0030686: Visualization, SelectMgr_ViewerSelector - sorting issues of transformation...
[occt.git] / src / Approx / Approx_Curve2d.cxx
1 // Created on: 1997-10-28
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 <Approx_Curve2d.hxx>
22 #include <Geom2d_BSplineCurve.hxx>
23 #include <Precision.hxx>
24 #include <TColgp_Array1OfPnt2d.hxx>
25
26 //=======================================================================
27 //class : Approx_Curve2d_Eval
28 //purpose: evaluator class for approximation
29 //=======================================================================
30 class Approx_Curve2d_Eval : public AdvApprox_EvaluatorFunction
31 {
32  public:
33   Approx_Curve2d_Eval (const Handle(Adaptor2d_HCurve2d)& theFunc, 
34                        Standard_Real First, Standard_Real Last)
35     : fonct(theFunc) { StartEndSav[0] = First; StartEndSav[1] = Last; }
36   
37   virtual void Evaluate (Standard_Integer *Dimension,
38                          Standard_Real     StartEnd[2],
39                          Standard_Real    *Parameter,
40                          Standard_Integer *DerivativeRequest,
41                          Standard_Real    *Result, // [Dimension]
42                          Standard_Integer *ErrorCode);
43   
44  private:
45   Handle(Adaptor2d_HCurve2d) fonct;
46   Standard_Real StartEndSav[2];
47 };
48
49 void Approx_Curve2d_Eval::Evaluate (Standard_Integer *Dimension,
50                                     Standard_Real     StartEnd[2],
51                                     Standard_Real    *Param, // Parameter at which evaluation
52                                     Standard_Integer *Order, // Derivative Request
53                                     Standard_Real    *Result,// [Dimension]
54                                     Standard_Integer *ErrorCode)
55 {
56   *ErrorCode = 0;
57   Standard_Real par = *Param;
58
59 // Dimension is incorrect
60   if (*Dimension!=2) {
61     *ErrorCode = 1;
62   }
63 // Parameter is incorrect
64   if ( par < StartEnd[0] || par > StartEnd[1] ) {
65     *ErrorCode = 2;
66   }
67   if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1]) 
68     {
69       fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
70       StartEndSav[0]=StartEnd[0];
71       StartEndSav[1]=StartEnd[1];
72     }
73   gp_Pnt2d pnt;
74   gp_Vec2d v1, v2;
75
76   switch (*Order) {
77   case 0:
78     pnt = fonct->Value(par);
79     Result[0] = pnt.X();
80     Result[1] = pnt.Y();
81     break;
82   case 1:
83     fonct->D1(par, pnt, v1);
84     Result[0] = v1.X();
85     Result[1] = v1.Y();
86     break;
87   case 2:
88     fonct->D2(par, pnt, v1, v2);
89     Result[0] = v2.X();
90     Result[1] = v2.Y();
91     break;
92   default:
93     Result[0] = Result[1] = 0.;
94     *ErrorCode = 3;
95     break;
96   }
97 }
98
99  Approx_Curve2d::Approx_Curve2d(const Handle(Adaptor2d_HCurve2d)& C2D,const Standard_Real First,const Standard_Real Last,const Standard_Real TolU,const Standard_Real TolV,const GeomAbs_Shape Continuity,const Standard_Integer MaxDegree,const Standard_Integer MaxSegments)
100 {
101   C2D->Trim(First,Last,Precision::PConfusion());
102
103   Standard_Integer Num1DSS=2, Num2DSS=0, Num3DSS=0;
104   Handle(TColStd_HArray1OfReal) TwoDTolNul, ThreeDTolNul; 
105   Handle(TColStd_HArray1OfReal) OneDTol  = new TColStd_HArray1OfReal(1,Num1DSS);
106   OneDTol->ChangeValue(1) = TolU;
107   OneDTol->ChangeValue(2) = TolV;
108   
109   Standard_Integer NbInterv_C2 = C2D->NbIntervals(GeomAbs_C2);
110   TColStd_Array1OfReal CutPnts_C2(1, NbInterv_C2+1);
111   C2D->Intervals(CutPnts_C2, GeomAbs_C2);
112   Standard_Integer NbInterv_C3 = C2D->NbIntervals(GeomAbs_C3);
113   TColStd_Array1OfReal CutPnts_C3(1, NbInterv_C3+1);
114   C2D->Intervals(CutPnts_C3, GeomAbs_C3);
115
116   AdvApprox_PrefAndRec CutTool(CutPnts_C2,CutPnts_C3);
117
118   myMaxError2dU = 0;
119   myMaxError2dV = 0;
120
121   Approx_Curve2d_Eval ev (C2D, First, Last);
122   AdvApprox_ApproxAFunction aApprox (Num1DSS, Num2DSS, Num3DSS, 
123                                      OneDTol, TwoDTolNul, ThreeDTolNul,
124                                      First, Last, Continuity,
125                                      MaxDegree, MaxSegments,
126                                      ev, CutTool);
127
128   myIsDone = aApprox.IsDone();
129   myHasResult = aApprox.HasResult();
130   
131   if (myHasResult) {
132     TColgp_Array1OfPnt2d Poles2d(1,aApprox.NbPoles());
133     TColStd_Array1OfReal Poles1dU(1,aApprox.NbPoles());
134     aApprox.Poles1d(1, Poles1dU);
135     TColStd_Array1OfReal Poles1dV(1,aApprox.NbPoles());
136     aApprox.Poles1d(2, Poles1dV);
137     for(Standard_Integer i = 1; i <= aApprox.NbPoles(); i++)
138       Poles2d.SetValue(i, gp_Pnt2d(Poles1dU.Value(i), Poles1dV.Value(i)));
139
140     Handle(TColStd_HArray1OfReal)    Knots = aApprox.Knots();
141     Handle(TColStd_HArray1OfInteger) Mults = aApprox.Multiplicities();
142     Standard_Integer Degree = aApprox.Degree();
143     myCurve = new Geom2d_BSplineCurve(Poles2d, Knots->Array1(), Mults->Array1(), Degree);
144     myMaxError2dU = aApprox.MaxError(1, 1);
145     myMaxError2dV = aApprox.MaxError(1, 2);
146   } 
147 }
148
149  Standard_Boolean Approx_Curve2d::IsDone() const
150 {
151   return myIsDone;
152 }
153
154  Standard_Boolean Approx_Curve2d::HasResult() const
155 {
156   return myHasResult;
157 }
158
159  Handle(Geom2d_BSplineCurve) Approx_Curve2d::Curve() const
160 {
161   return myCurve;
162 }
163
164  Standard_Real Approx_Curve2d::MaxError2dU() const
165 {
166   return myMaxError2dU;
167 }
168
169  Standard_Real Approx_Curve2d::MaxError2dV() const
170 {
171   return myMaxError2dV;
172 }