Test for 0022778: Bug in BRepMesh
[occt.git] / src / Geom2dConvert / Geom2dConvert_ApproxCurve.cxx
CommitLineData
b311480e 1// Created on: 1997-09-11
2// Created by: Roman BORISOV
3// Copyright (c) 1997-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
5//
6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
7fd59977 21
22
23#include <Geom2dConvert_ApproxCurve.ixx>
24#include <gp_Pnt2d.hxx>
25#include <gp_Vec2d.hxx>
26#include <Geom2dAdaptor_HCurve.hxx>
27#include <TColStd_HArray1OfReal.hxx>
28#include <AdvApprox_PrefAndRec.hxx>
29#include <AdvApprox_ApproxAFunction.hxx>
30#include <TColgp_Array1OfPnt2d.hxx>
31#include <Precision.hxx>
32
33//=======================================================================
34//class : Geom2dConvert_ApproxCurve_Eval
35//purpose: evaluator class for approximation
36//=======================================================================
37
38class Geom2dConvert_ApproxCurve_Eval : public AdvApprox_EvaluatorFunction
39{
40 public:
41 Geom2dConvert_ApproxCurve_Eval (const Handle(Adaptor2d_HCurve2d)& theFunc,
42 Standard_Real First, Standard_Real Last)
43 : fonct(theFunc) { StartEndSav[0] = First; StartEndSav[1] = Last; }
44
45 virtual void Evaluate (Standard_Integer *Dimension,
46 Standard_Real StartEnd[2],
47 Standard_Real *Parameter,
48 Standard_Integer *DerivativeRequest,
49 Standard_Real *Result, // [Dimension]
50 Standard_Integer *ErrorCode);
51
52 private:
53 Handle(Adaptor2d_HCurve2d) fonct;
54 Standard_Real StartEndSav[2];
55};
56
57void Geom2dConvert_ApproxCurve_Eval::Evaluate (Standard_Integer *Dimension,
58 Standard_Real StartEnd[2],
59 Standard_Real *Param, // Parameter at which evaluation
60 Standard_Integer *Order, // Derivative Request
61 Standard_Real *Result,// [Dimension]
62 Standard_Integer *ErrorCode)
63{
64 *ErrorCode = 0;
65 Standard_Real par = *Param;
66
67// Dimension is incorrect
68 if (*Dimension!=2) {
69 *ErrorCode = 1;
70 }
71// Parameter is incorrect
72 if ( par < StartEnd[0] || par > StartEnd[1] ) {
73 *ErrorCode = 2;
74 }
75 if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1])
76 {
77 fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
78 StartEndSav[0]=StartEnd[0];
79 StartEndSav[1]=StartEnd[1];
80 }
81
82 gp_Pnt2d pnt;
83 gp_Vec2d v1, v2;
84
85 switch (*Order) {
86 case 0:
87 pnt = fonct->Value(par);
88 Result[0] = pnt.X();
89 Result[1] = pnt.Y();
90 break;
91 case 1:
92 fonct->D1(par, pnt, v1);
93 Result[0] = v1.X();
94 Result[1] = v1.Y();
95 break;
96 case 2:
97 fonct->D2(par, pnt, v1, v2);
98 Result[0] = v2.X();
99 Result[1] = v2.Y();
100 break;
101 default:
102 Result[0] = Result[1] = 0.;
103 *ErrorCode = 3;
104 break;
105 }
106}
107
108Geom2dConvert_ApproxCurve::Geom2dConvert_ApproxCurve(const Handle(Geom2d_Curve)& Curve,const Standard_Real Tol2d,const GeomAbs_Shape Order,const Standard_Integer MaxSegments,const Standard_Integer MaxDegree)
109{
110 Handle(Geom2dAdaptor_HCurve) HCurve = new Geom2dAdaptor_HCurve (Curve);
111
112 // Initialisation of input parameters of AdvApprox
113
114 Standard_Integer Num1DSS=0, Num2DSS=1, Num3DSS=0;
115 Handle(TColStd_HArray1OfReal) OneDTolNul, ThreeDTolNul;
116 Handle(TColStd_HArray1OfReal) TwoDTol = new TColStd_HArray1OfReal(1,Num2DSS);
117 TwoDTol->Init(Tol2d);
118
119 Standard_Real First = Curve->FirstParameter();
120 Standard_Real Last = Curve->LastParameter();
121
122 Standard_Integer NbInterv_C2 = HCurve->NbIntervals(GeomAbs_C2);
123 TColStd_Array1OfReal CutPnts_C2(1, NbInterv_C2+1);
124 HCurve->Intervals(CutPnts_C2,GeomAbs_C2);
125 Standard_Integer NbInterv_C3 = HCurve->NbIntervals(GeomAbs_C3);
126 TColStd_Array1OfReal CutPnts_C3(1, NbInterv_C3+1);
127 HCurve->Intervals(CutPnts_C3,GeomAbs_C3);
128 AdvApprox_PrefAndRec CutTool(CutPnts_C2,CutPnts_C3);
129
130 myMaxError = 0;
131
132 Geom2dConvert_ApproxCurve_Eval ev (HCurve, First, Last);
133 AdvApprox_ApproxAFunction aApprox (Num1DSS, Num2DSS, Num3DSS,
134 OneDTolNul, TwoDTol, ThreeDTolNul,
135 First, Last, Order,
136 MaxDegree, MaxSegments,
137 ev, CutTool);
138
139 myIsDone = aApprox.IsDone();
140 myHasResult = aApprox.HasResult();
141
142 if (myHasResult) {
143 TColgp_Array1OfPnt2d Poles(1,aApprox.NbPoles());
144 aApprox.Poles2d(1,Poles);
145 Handle(TColStd_HArray1OfReal) Knots = aApprox.Knots();
146 Handle(TColStd_HArray1OfInteger) Mults = aApprox.Multiplicities();
147 Standard_Integer Degree = aApprox.Degree();
148 myBSplCurve = new Geom2d_BSplineCurve(Poles, Knots->Array1(), Mults->Array1(), Degree);
149 myMaxError = aApprox.MaxError(2, 1);
150 }
151}
152
153 Handle(Geom2d_BSplineCurve) Geom2dConvert_ApproxCurve::Curve() const
154{
155 return myBSplCurve;
156}
157
158 Standard_Boolean Geom2dConvert_ApproxCurve::IsDone() const
159{
160 return myIsDone;
161}
162
163 Standard_Boolean Geom2dConvert_ApproxCurve::HasResult() const
164{
165 return myHasResult;
166}
167
168 Standard_Real Geom2dConvert_ApproxCurve::MaxError() const
169{
170 return myMaxError;
171}
172
173 void Geom2dConvert_ApproxCurve::Dump(Standard_OStream& o) const
174{
175 o << "******* Dump of ApproxCurve *******" << endl;
176 o << "******* Error " << MaxError() << endl;
177}
178