0024428: Implementation of LGPL license
[occt.git] / src / Approx / Approx_Curve3d.cxx
... / ...
CommitLineData
1// Created on: 1998-08-20
2// Created by: Philippe MANGIN
3// Copyright (c) 1998-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
9// under the terms of the GNU Lesser General Public 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#include <Approx_Curve3d.ixx>
18#include <gp_Pnt.hxx>
19#include <gp_Vec.hxx>
20#include <GeomAdaptor_HCurve.hxx>
21#include <TColStd_HArray1OfReal.hxx>
22#include <AdvApprox_PrefAndRec.hxx>
23#include <AdvApprox_ApproxAFunction.hxx>
24#include <TColgp_Array1OfPnt.hxx>
25#include <Precision.hxx>
26
27//=======================================================================
28//class : Approx_Curve3d_Eval
29//purpose: evaluator class for approximation
30//=======================================================================
31
32class Approx_Curve3d_Eval : public AdvApprox_EvaluatorFunction
33{
34 public:
35 Approx_Curve3d_Eval (const Handle(Adaptor3d_HCurve)& 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(Adaptor3d_HCurve) fonct;
48 Standard_Real StartEndSav[2];
49};
50
51void Approx_Curve3d_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!=3) {
63 *ErrorCode = 1;
64 }
65
66 if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1])
67 {
68 fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
69 StartEndSav[0]=StartEnd[0];
70 StartEndSav[1]=StartEnd[1];
71 }
72
73 gp_Pnt pnt;
74 gp_Vec 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 Result[2] = pnt.Z();
82 break;
83 case 1:
84 fonct->D1(par, pnt, v1);
85 Result[0] = v1.X();
86 Result[1] = v1.Y();
87 Result[2] = v1.Z();
88 break;
89 case 2:
90 fonct->D2(par, pnt, v1, v2);
91 Result[0] = v2.X();
92 Result[1] = v2.Y();
93 Result[2] = v2.Z();
94 break;
95 default:
96 Result[0] = Result[1] = Result[2] = 0.;
97 *ErrorCode = 3;
98 break;
99 }
100}
101
102Approx_Curve3d::Approx_Curve3d(const Handle(Adaptor3d_HCurve)& Curve,
103 const Standard_Real Tol3d,
104 const GeomAbs_Shape Order,
105 const Standard_Integer MaxSegments,
106 const Standard_Integer MaxDegree)
107{
108 // Initialisation of input parameters of AdvApprox
109
110 Standard_Integer Num1DSS=0, Num2DSS=0, Num3DSS=1;
111 Handle(TColStd_HArray1OfReal) OneDTolNul, TwoDTolNul;
112 Handle(TColStd_HArray1OfReal) ThreeDTol =
113 new TColStd_HArray1OfReal(1,Num3DSS);
114 ThreeDTol->Init(Tol3d);
115
116 Standard_Real First = Curve->FirstParameter();
117 Standard_Real Last = Curve->LastParameter();
118
119 Standard_Integer NbInterv_C2 = Curve->NbIntervals(GeomAbs_C2);
120 TColStd_Array1OfReal CutPnts_C2(1, NbInterv_C2+1);
121 Curve->Intervals(CutPnts_C2,GeomAbs_C2);
122 Standard_Integer NbInterv_C3 = Curve->NbIntervals(GeomAbs_C3);
123 TColStd_Array1OfReal CutPnts_C3(1, NbInterv_C3+1);
124 Curve->Intervals(CutPnts_C3,GeomAbs_C3);
125
126 AdvApprox_PrefAndRec CutTool(CutPnts_C2,CutPnts_C3);
127
128 myMaxError = 0;
129
130 Approx_Curve3d_Eval ev (Curve, First, Last);
131 AdvApprox_ApproxAFunction aApprox (Num1DSS, Num2DSS, Num3DSS,
132 OneDTolNul, TwoDTolNul, ThreeDTol,
133 First, Last, Order,
134 MaxDegree, MaxSegments,
135 ev, CutTool);
136
137 myIsDone = aApprox.IsDone();
138 myHasResult = aApprox.HasResult();
139
140 if (myHasResult) {
141 TColgp_Array1OfPnt Poles(1,aApprox.NbPoles());
142 aApprox.Poles(1,Poles);
143 Handle(TColStd_HArray1OfReal) Knots = aApprox.Knots();
144 Handle(TColStd_HArray1OfInteger) Mults = aApprox.Multiplicities();
145 Standard_Integer Degree = aApprox.Degree();
146 myBSplCurve = new Geom_BSplineCurve(Poles, Knots->Array1(), Mults->Array1(), Degree);
147 myMaxError = aApprox.MaxError(3, 1);
148 }
149}
150
151 Handle(Geom_BSplineCurve) Approx_Curve3d::Curve() const
152{
153 return myBSplCurve;
154}
155
156 Standard_Boolean Approx_Curve3d::IsDone() const
157{
158 return myIsDone;
159}
160
161 Standard_Boolean Approx_Curve3d::HasResult() const
162{
163 return myHasResult;
164}
165
166 Standard_Real Approx_Curve3d::MaxError() const
167{
168 return myMaxError;
169}
170
171 void Approx_Curve3d::Dump(Standard_OStream& o) const
172{
173 o << "******* Dump of ApproxCurve *******" << endl;
174 o << "*******Degree " << Curve()->Degree() << endl;
175 o << "*******NbSegments " << Curve()->NbKnots() - 1 << endl;
176 o << "*******Error " << MaxError() << endl;
177}