0030686: Visualization, SelectMgr_ViewerSelector - sorting issues of transformation...
[occt.git] / src / GeomConvert / GeomConvert_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 <Adaptor3d_HCurve.hxx>
19 #include <AdvApprox_ApproxAFunction.hxx>
20 #include <AdvApprox_PrefAndRec.hxx>
21 #include <Geom_BSplineCurve.hxx>
22 #include <Geom_Curve.hxx>
23 #include <GeomAdaptor_HCurve.hxx>
24 #include <GeomConvert_ApproxCurve.hxx>
25 #include <gp_Pnt.hxx>
26 #include <gp_Vec.hxx>
27 #include <Precision.hxx>
28 #include <Standard_ConstructionError.hxx>
29 #include <Standard_OutOfRange.hxx>
30 #include <TColgp_Array1OfPnt.hxx>
31 #include <TColStd_HArray1OfReal.hxx>
32
33 //=======================================================================
34 //class : GeomConvert_ApproxCurve_Eval
35 //purpose: evaluator class for approximation
36 //=======================================================================
37 class GeomConvert_ApproxCurve_Eval : public AdvApprox_EvaluatorFunction
38 {
39  public:
40   GeomConvert_ApproxCurve_Eval (const Handle(Adaptor3d_HCurve)& theFunc, 
41                                 Standard_Real First, Standard_Real Last)
42     : fonct(theFunc) { StartEndSav[0] = First; StartEndSav[1] = Last; }
43   
44   virtual void Evaluate (Standard_Integer *Dimension,
45                          Standard_Real     StartEnd[2],
46                          Standard_Real    *Parameter,
47                          Standard_Integer *DerivativeRequest,
48                          Standard_Real    *Result, // [Dimension]
49                          Standard_Integer *ErrorCode);
50   
51  private:
52   Handle(Adaptor3d_HCurve) fonct;
53   Standard_Real StartEndSav[2];
54 };
55
56 void GeomConvert_ApproxCurve_Eval::Evaluate (Standard_Integer *Dimension,
57                                              Standard_Real     StartEnd[2],
58                                              Standard_Real    *Param, // Parameter at which evaluation
59                                              Standard_Integer *Order, // Derivative Request
60                                              Standard_Real    *Result,// [Dimension]
61                                              Standard_Integer *ErrorCode)
62 {
63   *ErrorCode = 0;
64   Standard_Real par = *Param;
65
66 // Dimension is incorrect
67   if (*Dimension!=3) {
68     *ErrorCode = 1;
69   }
70
71   if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1]) 
72     {
73       fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
74       StartEndSav[0]=StartEnd[0];
75       StartEndSav[1]=StartEnd[1];
76     }
77
78   gp_Pnt pnt;
79   gp_Vec v1, v2;
80
81   switch (*Order) {
82   case 0:
83     pnt = fonct->Value(par);
84     Result[0] = pnt.X();
85     Result[1] = pnt.Y();
86     Result[2] = pnt.Z();
87     break;
88   case 1:
89     fonct->D1(par, pnt, v1);
90     Result[0] = v1.X();
91     Result[1] = v1.Y();
92     Result[2] = v1.Z();
93     break;
94   case 2:
95     fonct->D2(par, pnt, v1, v2);
96     Result[0] = v2.X();
97     Result[1] = v2.Y();
98     Result[2] = v2.Z();
99     break;
100   default:
101     Result[0] = Result[1] = Result[2] = 0.;
102     *ErrorCode = 3;
103     break;
104   }
105 }
106
107 GeomConvert_ApproxCurve::GeomConvert_ApproxCurve(const Handle(Geom_Curve)& Curve,const Standard_Real Tol3d,const GeomAbs_Shape Order,const Standard_Integer MaxSegments,const Standard_Integer MaxDegree)
108 {
109   Handle(GeomAdaptor_HCurve) HCurve = new GeomAdaptor_HCurve (Curve);
110   Approximate(HCurve, Tol3d, Order, MaxSegments, MaxDegree);
111 }
112
113 GeomConvert_ApproxCurve::GeomConvert_ApproxCurve(const Handle(Adaptor3d_HCurve)& Curve,const Standard_Real Tol3d,const GeomAbs_Shape Order,const Standard_Integer MaxSegments,const Standard_Integer MaxDegree)
114 {
115   Approximate(Curve, Tol3d, Order, MaxSegments, MaxDegree);
116 }
117
118 void GeomConvert_ApproxCurve::Approximate(
119     const Handle(Adaptor3d_HCurve)& theCurve,
120     const Standard_Real             theTol3d,
121     const GeomAbs_Shape             theOrder,
122     const Standard_Integer          theMaxSegments,
123     const Standard_Integer          theMaxDegree)
124 {
125   // Initialisation of input parameters of AdvApprox
126
127   Standard_Integer Num1DSS=0, Num2DSS=0, Num3DSS=1;
128   Handle(TColStd_HArray1OfReal) OneDTolNul, TwoDTolNul;
129   Handle(TColStd_HArray1OfReal) ThreeDTol = new TColStd_HArray1OfReal(1,Num3DSS);
130   ThreeDTol->Init(theTol3d);
131
132   Standard_Real First = theCurve->FirstParameter();
133   Standard_Real Last  = theCurve->LastParameter();
134
135   Standard_Integer NbInterv_C2 = theCurve->NbIntervals(GeomAbs_C2);
136   TColStd_Array1OfReal CutPnts_C2(1, NbInterv_C2+1);
137   theCurve->Intervals(CutPnts_C2,GeomAbs_C2);
138   Standard_Integer NbInterv_C3 = theCurve->NbIntervals(GeomAbs_C3);
139   TColStd_Array1OfReal CutPnts_C3(1, NbInterv_C3+1);
140   theCurve->Intervals(CutPnts_C3,GeomAbs_C3);
141
142   AdvApprox_PrefAndRec CutTool(CutPnts_C2,CutPnts_C3);
143
144   myMaxError = 0;
145
146   GeomConvert_ApproxCurve_Eval ev (theCurve, First, Last);
147   AdvApprox_ApproxAFunction aApprox (Num1DSS, Num2DSS, Num3DSS, 
148                                      OneDTolNul, TwoDTolNul, ThreeDTol,
149                                      First, Last, theOrder,
150                                      theMaxDegree, theMaxSegments,
151                                      ev, CutTool);
152
153   myIsDone = aApprox.IsDone();
154   myHasResult = aApprox.HasResult();
155
156   if (myHasResult) {
157     TColgp_Array1OfPnt Poles(1,aApprox.NbPoles());
158     aApprox.Poles(1,Poles);
159     Handle(TColStd_HArray1OfReal)    Knots = aApprox.Knots();
160     Handle(TColStd_HArray1OfInteger) Mults = aApprox.Multiplicities();
161     Standard_Integer Degree = aApprox.Degree();
162     myBSplCurve = new Geom_BSplineCurve(Poles, Knots->Array1(), Mults->Array1(), Degree);
163     myMaxError = aApprox.MaxError(3, 1);
164   } 
165 }
166
167  Handle(Geom_BSplineCurve) GeomConvert_ApproxCurve::Curve() const
168 {
169   return myBSplCurve;
170 }
171
172  Standard_Boolean GeomConvert_ApproxCurve::IsDone() const
173 {
174   return myIsDone; 
175 }
176
177  Standard_Boolean GeomConvert_ApproxCurve::HasResult() const
178 {
179   return myHasResult; 
180 }
181
182  Standard_Real GeomConvert_ApproxCurve::MaxError() const
183 {
184   return myMaxError;
185 }
186
187  void GeomConvert_ApproxCurve::Dump(Standard_OStream& o) const
188 {
189   o << "******* Dump of ApproxCurve *******" << endl;
190   o << "*******Error   " << MaxError() << endl;
191 }
192