0025492: the selected subshape does not have topological relationship with orginal...
[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
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
17#include <Geom2dConvert_ApproxCurve.ixx>
18#include <gp_Pnt2d.hxx>
19#include <gp_Vec2d.hxx>
20#include <Geom2dAdaptor_HCurve.hxx>
21#include <TColStd_HArray1OfReal.hxx>
22#include <AdvApprox_PrefAndRec.hxx>
23#include <AdvApprox_ApproxAFunction.hxx>
24#include <TColgp_Array1OfPnt2d.hxx>
25#include <Precision.hxx>
26
27//=======================================================================
28//class : Geom2dConvert_ApproxCurve_Eval
29//purpose: evaluator class for approximation
30//=======================================================================
31
32class Geom2dConvert_ApproxCurve_Eval : public AdvApprox_EvaluatorFunction
33{
34 public:
35 Geom2dConvert_ApproxCurve_Eval (const Handle(Adaptor2d_HCurve2d)& theFunc,
36 Standard_Real First, Standard_Real Last)
37 : fonct(theFunc) { StartEndSav[0] = First; StartEndSav[1] = Last; }
38
39 virtual void Evaluate (Standard_Integer *Dimension,
40 Standard_Real StartEnd[2],
41 Standard_Real *Parameter,
42 Standard_Integer *DerivativeRequest,
43 Standard_Real *Result, // [Dimension]
44 Standard_Integer *ErrorCode);
45
46 private:
47 Handle(Adaptor2d_HCurve2d) fonct;
48 Standard_Real StartEndSav[2];
49};
50
51void Geom2dConvert_ApproxCurve_Eval::Evaluate (Standard_Integer *Dimension,
52 Standard_Real StartEnd[2],
53 Standard_Real *Param, // Parameter at which evaluation
54 Standard_Integer *Order, // Derivative Request
55 Standard_Real *Result,// [Dimension]
56 Standard_Integer *ErrorCode)
57{
58 *ErrorCode = 0;
59 Standard_Real par = *Param;
60
61// Dimension is incorrect
62 if (*Dimension!=2) {
63 *ErrorCode = 1;
64 }
65// Parameter is incorrect
66 if ( par < StartEnd[0] || par > StartEnd[1] ) {
67 *ErrorCode = 2;
68 }
69 if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1])
70 {
71 fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
72 StartEndSav[0]=StartEnd[0];
73 StartEndSav[1]=StartEnd[1];
74 }
75
76 gp_Pnt2d pnt;
77 gp_Vec2d v1, v2;
78
79 switch (*Order) {
80 case 0:
81 pnt = fonct->Value(par);
82 Result[0] = pnt.X();
83 Result[1] = pnt.Y();
84 break;
85 case 1:
86 fonct->D1(par, pnt, v1);
87 Result[0] = v1.X();
88 Result[1] = v1.Y();
89 break;
90 case 2:
91 fonct->D2(par, pnt, v1, v2);
92 Result[0] = v2.X();
93 Result[1] = v2.Y();
94 break;
95 default:
96 Result[0] = Result[1] = 0.;
97 *ErrorCode = 3;
98 break;
99 }
100}
101
102Geom2dConvert_ApproxCurve::Geom2dConvert_ApproxCurve(const Handle(Geom2d_Curve)& Curve,const Standard_Real Tol2d,const GeomAbs_Shape Order,const Standard_Integer MaxSegments,const Standard_Integer MaxDegree)
103{
104 Handle(Geom2dAdaptor_HCurve) HCurve = new Geom2dAdaptor_HCurve (Curve);
105
106 // Initialisation of input parameters of AdvApprox
107
108 Standard_Integer Num1DSS=0, Num2DSS=1, Num3DSS=0;
109 Handle(TColStd_HArray1OfReal) OneDTolNul, ThreeDTolNul;
110 Handle(TColStd_HArray1OfReal) TwoDTol = new TColStd_HArray1OfReal(1,Num2DSS);
111 TwoDTol->Init(Tol2d);
112
113 Standard_Real First = Curve->FirstParameter();
114 Standard_Real Last = Curve->LastParameter();
115
116 Standard_Integer NbInterv_C2 = HCurve->NbIntervals(GeomAbs_C2);
117 TColStd_Array1OfReal CutPnts_C2(1, NbInterv_C2+1);
118 HCurve->Intervals(CutPnts_C2,GeomAbs_C2);
119 Standard_Integer NbInterv_C3 = HCurve->NbIntervals(GeomAbs_C3);
120 TColStd_Array1OfReal CutPnts_C3(1, NbInterv_C3+1);
121 HCurve->Intervals(CutPnts_C3,GeomAbs_C3);
122 AdvApprox_PrefAndRec CutTool(CutPnts_C2,CutPnts_C3);
123
124 myMaxError = 0;
125
126 Geom2dConvert_ApproxCurve_Eval ev (HCurve, First, Last);
127 AdvApprox_ApproxAFunction aApprox (Num1DSS, Num2DSS, Num3DSS,
128 OneDTolNul, TwoDTol, ThreeDTolNul,
129 First, Last, Order,
130 MaxDegree, MaxSegments,
131 ev, CutTool);
132
133 myIsDone = aApprox.IsDone();
134 myHasResult = aApprox.HasResult();
135
136 if (myHasResult) {
137 TColgp_Array1OfPnt2d Poles(1,aApprox.NbPoles());
138 aApprox.Poles2d(1,Poles);
139 Handle(TColStd_HArray1OfReal) Knots = aApprox.Knots();
140 Handle(TColStd_HArray1OfInteger) Mults = aApprox.Multiplicities();
141 Standard_Integer Degree = aApprox.Degree();
142 myBSplCurve = new Geom2d_BSplineCurve(Poles, Knots->Array1(), Mults->Array1(), Degree);
143 myMaxError = aApprox.MaxError(2, 1);
144 }
145}
146
147 Handle(Geom2d_BSplineCurve) Geom2dConvert_ApproxCurve::Curve() const
148{
149 return myBSplCurve;
150}
151
152 Standard_Boolean Geom2dConvert_ApproxCurve::IsDone() const
153{
154 return myIsDone;
155}
156
157 Standard_Boolean Geom2dConvert_ApproxCurve::HasResult() const
158{
159 return myHasResult;
160}
161
162 Standard_Real Geom2dConvert_ApproxCurve::MaxError() const
163{
164 return myMaxError;
165}
166
167 void Geom2dConvert_ApproxCurve::Dump(Standard_OStream& o) const
168{
169 o << "******* Dump of ApproxCurve *******" << endl;
170 o << "******* Error " << MaxError() << endl;
171}
172