b311480e |
1 | // Created on: 1998-08-20 |
2 | // Created by: Philippe MANGIN |
3 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
5 | // |
973c2be1 |
6 | // This file is part of Open CASCADE Technology software library. |
b311480e |
7 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
13 | // |
973c2be1 |
14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. |
7fd59977 |
16 | |
42cf5bc1 |
17 | |
18 | #include <Adaptor3d_HCurve.hxx> |
19 | #include <AdvApprox_ApproxAFunction.hxx> |
20 | #include <AdvApprox_PrefAndRec.hxx> |
21 | #include <Approx_Curve3d.hxx> |
22 | #include <Geom_BSplineCurve.hxx> |
23 | #include <GeomAdaptor_HCurve.hxx> |
7fd59977 |
24 | #include <gp_Pnt.hxx> |
25 | #include <gp_Vec.hxx> |
7fd59977 |
26 | #include <Precision.hxx> |
42cf5bc1 |
27 | #include <Standard_ConstructionError.hxx> |
28 | #include <Standard_OutOfRange.hxx> |
29 | #include <TColgp_Array1OfPnt.hxx> |
30 | #include <TColStd_HArray1OfReal.hxx> |
7fd59977 |
31 | |
32 | //======================================================================= |
33 | //class : Approx_Curve3d_Eval |
34 | //purpose: evaluator class for approximation |
35 | //======================================================================= |
7fd59977 |
36 | class Approx_Curve3d_Eval : public AdvApprox_EvaluatorFunction |
37 | { |
38 | public: |
39 | Approx_Curve3d_Eval (const Handle(Adaptor3d_HCurve)& 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(Adaptor3d_HCurve) fonct; |
52 | Standard_Real StartEndSav[2]; |
53 | }; |
54 | |
55 | void Approx_Curve3d_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!=3) { |
67 | *ErrorCode = 1; |
68 | } |
69 | |
70 | if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1]) |
71 | { |
72 | fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion()); |
73 | StartEndSav[0]=StartEnd[0]; |
74 | StartEndSav[1]=StartEnd[1]; |
75 | } |
76 | |
77 | gp_Pnt pnt; |
78 | gp_Vec v1, v2; |
79 | |
80 | switch (*Order) { |
81 | case 0: |
82 | pnt = fonct->Value(par); |
83 | Result[0] = pnt.X(); |
84 | Result[1] = pnt.Y(); |
85 | Result[2] = pnt.Z(); |
86 | break; |
87 | case 1: |
88 | fonct->D1(par, pnt, v1); |
89 | Result[0] = v1.X(); |
90 | Result[1] = v1.Y(); |
91 | Result[2] = v1.Z(); |
92 | break; |
93 | case 2: |
94 | fonct->D2(par, pnt, v1, v2); |
95 | Result[0] = v2.X(); |
96 | Result[1] = v2.Y(); |
97 | Result[2] = v2.Z(); |
98 | break; |
99 | default: |
100 | Result[0] = Result[1] = Result[2] = 0.; |
101 | *ErrorCode = 3; |
102 | break; |
103 | } |
104 | } |
105 | |
106 | Approx_Curve3d::Approx_Curve3d(const Handle(Adaptor3d_HCurve)& Curve, |
107 | const Standard_Real Tol3d, |
108 | const GeomAbs_Shape Order, |
109 | const Standard_Integer MaxSegments, |
110 | const Standard_Integer MaxDegree) |
111 | { |
112 | // Initialisation of input parameters of AdvApprox |
113 | |
114 | Standard_Integer Num1DSS=0, Num2DSS=0, Num3DSS=1; |
115 | Handle(TColStd_HArray1OfReal) OneDTolNul, TwoDTolNul; |
116 | Handle(TColStd_HArray1OfReal) ThreeDTol = |
117 | new TColStd_HArray1OfReal(1,Num3DSS); |
118 | ThreeDTol->Init(Tol3d); |
119 | |
120 | Standard_Real First = Curve->FirstParameter(); |
121 | Standard_Real Last = Curve->LastParameter(); |
122 | |
123 | Standard_Integer NbInterv_C2 = Curve->NbIntervals(GeomAbs_C2); |
124 | TColStd_Array1OfReal CutPnts_C2(1, NbInterv_C2+1); |
125 | Curve->Intervals(CutPnts_C2,GeomAbs_C2); |
126 | Standard_Integer NbInterv_C3 = Curve->NbIntervals(GeomAbs_C3); |
127 | TColStd_Array1OfReal CutPnts_C3(1, NbInterv_C3+1); |
128 | Curve->Intervals(CutPnts_C3,GeomAbs_C3); |
129 | |
130 | AdvApprox_PrefAndRec CutTool(CutPnts_C2,CutPnts_C3); |
131 | |
132 | myMaxError = 0; |
133 | |
134 | Approx_Curve3d_Eval ev (Curve, First, Last); |
135 | AdvApprox_ApproxAFunction aApprox (Num1DSS, Num2DSS, Num3DSS, |
136 | OneDTolNul, TwoDTolNul, ThreeDTol, |
137 | First, Last, Order, |
138 | MaxDegree, MaxSegments, |
139 | ev, CutTool); |
140 | |
141 | myIsDone = aApprox.IsDone(); |
142 | myHasResult = aApprox.HasResult(); |
143 | |
144 | if (myHasResult) { |
145 | TColgp_Array1OfPnt Poles(1,aApprox.NbPoles()); |
146 | aApprox.Poles(1,Poles); |
147 | Handle(TColStd_HArray1OfReal) Knots = aApprox.Knots(); |
148 | Handle(TColStd_HArray1OfInteger) Mults = aApprox.Multiplicities(); |
149 | Standard_Integer Degree = aApprox.Degree(); |
150 | myBSplCurve = new Geom_BSplineCurve(Poles, Knots->Array1(), Mults->Array1(), Degree); |
151 | myMaxError = aApprox.MaxError(3, 1); |
152 | } |
153 | } |
154 | |
155 | Handle(Geom_BSplineCurve) Approx_Curve3d::Curve() const |
156 | { |
157 | return myBSplCurve; |
158 | } |
159 | |
160 | Standard_Boolean Approx_Curve3d::IsDone() const |
161 | { |
162 | return myIsDone; |
163 | } |
164 | |
165 | Standard_Boolean Approx_Curve3d::HasResult() const |
166 | { |
167 | return myHasResult; |
168 | } |
169 | |
170 | Standard_Real Approx_Curve3d::MaxError() const |
171 | { |
172 | return myMaxError; |
173 | } |
174 | |
175 | void Approx_Curve3d::Dump(Standard_OStream& o) const |
176 | { |
177 | o << "******* Dump of ApproxCurve *******" << endl; |
178 | o << "*******Degree " << Curve()->Degree() << endl; |
179 | o << "*******NbSegments " << Curve()->NbKnots() - 1 << endl; |
180 | o << "*******Error " << MaxError() << endl; |
181 | } |