1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
18 // Application de la methode du gradient corrige pour minimiser
19 // F = somme(||C(ui, Poles(ui)) - ptli||2.
20 // La methode de gradient conjugue est programmee dans la bibliotheque
21 // mathematique: math_BFGS.
22 // cet algorithme doit etre appele uniquement lorsque on a affaire a un set
23 // de points contraints (ailleurs qu aux extremites). En effet, l appel de la
24 // fonction F a minimiser implique un appel a ParLeastSquare et ResConstraint.
25 // Si ce n est pas le cas, l appel a ResConstraint est equivalent a une
26 // seconde resolution par les moindres carres donc beaucoup de temps perdu.
29 #define No_Standard_RangeError
30 #define No_Standard_OutOfRange
32 #include <AppParCurves_Constraint.hxx>
33 #include <math_BFGS.hxx>
34 #include <StdFail_NotDone.hxx>
35 #include <AppParCurves_MultiPoint.hxx>
37 #include <gp_Pnt2d.hxx>
39 #include <gp_Vec2d.hxx>
40 #include <TColgp_Array1OfPnt.hxx>
41 #include <TColgp_Array1OfPnt2d.hxx>
42 #include <TColgp_Array1OfVec.hxx>
43 #include <TColgp_Array1OfVec2d.hxx>
44 #include <BSplCLib.hxx>
47 // #define AppParCurves_Gradient_BFGS BFGS_/**/AppParCurves_Gradient
51 AppParCurves_Gradient::
52 AppParCurves_Gradient(const MultiLine& SSP,
53 const Standard_Integer FirstPoint,
54 const Standard_Integer LastPoint,
55 const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
56 math_Vector& Parameters,
57 const Standard_Integer Deg,
58 const Standard_Real Tol3d,
59 const Standard_Real Tol2d,
60 const Standard_Integer NbIterations):
61 ParError(FirstPoint, LastPoint,0.0) {
63 // Standard_Boolean grad = Standard_True;
64 Standard_Integer j, k, i2, l;
65 Standard_Real UF, DU, Fval = 0.0, FU, DFU;
66 Standard_Integer nbP3d = ToolLine::NbP3d(SSP);
67 Standard_Integer nbP2d = ToolLine::NbP2d(SSP);
68 Standard_Integer mynbP3d=nbP3d, mynbP2d=nbP2d;
69 Standard_Integer nbP = nbP3d + nbP2d;
72 // gp_Pnt2d Pt2d, P12d, P22d;
74 // gp_Vec V1, V2, MyV;
76 // gp_Vec2d V12d, V22d, MyV2d;
78 Done = Standard_False;
80 if (nbP3d == 0) mynbP3d = 1;
81 if (nbP2d == 0) mynbP2d = 1;
82 TColgp_Array1OfPnt TabP(1, mynbP3d);
83 TColgp_Array1OfPnt2d TabP2d(1, mynbP2d);
84 TColgp_Array1OfVec TabV(1, mynbP3d);
85 TColgp_Array1OfVec2d TabV2d(1, mynbP2d);
87 // Calcul de la fonction F= somme(||C(ui)-Ptli||2):
88 // Appel a une fonction heritant de MultipleVarFunctionWithGradient
89 // pour calculer F et grad_F.
90 // ================================================================
92 AppParCurves_ParFunction MyF(SSP, FirstPoint,LastPoint, TheConstraints, Parameters, Deg);
95 if (!MyF.Value(Parameters, Fval)) {
96 Done = Standard_False;
100 SCU = MyF.CurveValue();
101 Standard_Integer deg = SCU.NbPoles()-1;
102 TColgp_Array1OfPnt TabPole(1, deg+1), TabCoef(1, deg+1);
103 TColgp_Array1OfPnt2d TabPole2d(1, deg+1), TabCoef2d(1, deg+1);
104 TColgp_Array1OfPnt TheCoef(1, (deg+1)*mynbP3d);
105 TColgp_Array1OfPnt2d TheCoef2d(1, (deg+1)*mynbP2d);
108 // Stockage des Poles des courbes pour projeter:
109 // ============================================
111 for (k = 1; k <= nbP3d; k++) {
112 SCU.Curve(k, TabPole);
113 BSplCLib::PolesCoefficients(TabPole, PLib::NoWeights(),
114 TabCoef, PLib::NoWeights());
115 for (j=1; j<=deg+1; j++) TheCoef(j+i2) = TabCoef(j);
119 for (k = 1; k <= nbP2d; k++) {
120 SCU.Curve(nbP3d+k, TabPole2d);
121 BSplCLib::PolesCoefficients(TabPole2d, PLib::NoWeights(),
122 TabCoef2d, PLib::NoWeights());
123 for (j=1; j<=deg+1; j++) TheCoef2d(j+i2) = TabCoef2d(j);
127 // Une iteration rapide de projection est faite par la methode de
128 // Rogers & Fog 89, methode equivalente a Hoschek 88 qui ne necessite pas
132 // Iteration de Projection:
133 // =======================
134 for (j = FirstPoint+1; j <= LastPoint-1; j++) {
136 if (nbP != 0 && nbP2d != 0) ToolLine::Value(SSP, j, TabP, TabP2d);
137 else if (nbP2d != 0) ToolLine::Value(SSP, j, TabP2d);
138 else ToolLine::Value(SSP, j, TabP);
143 for (k = 1; k <= nbP3d; k++) {
144 for (l=1; l<=deg+1; l++) TabCoef(l) = TheCoef(l+i2);
146 BSplCLib::CoefsD1(UF, TabCoef, PLib::NoWeights(), Pt, V1);
147 MyV = gp_Vec(Pt, TabP(k));
149 DFU += V1.SquareMagnitude();
152 for (k = 1; k <= nbP2d; k++) {
153 for (l=1; l<=deg+1; l++) TabCoef2d(l) = TheCoef2d(l+i2);
155 BSplCLib::CoefsD1(UF, TabCoef2d, PLib::NoWeights(), Pt2d, V12d);
156 MyV2d = gp_Vec2d(Pt2d, TabP2d(k));
158 DFU += V12d.SquareMagnitude();
161 if (DFU >= RealEpsilon()) {
163 DU = Sign(Min(5.e-02, Abs(DU)), DU);
170 if (!MyF.Value(Parameters, Fval)) {
171 SCU = AppParCurves_MultiCurve();
172 Done = Standard_False;
175 MError3d = MyF.MaxError3d();
176 MError2d = MyF.MaxError2d();
178 if (MError3d<= Tol3d && MError2d <= Tol2d) {
179 Done = Standard_True;
180 SCU = MyF.CurveValue();
182 else if (NbIterations != 0) {
183 // NbIterations de gradient conjugue:
184 // =================================
185 Standard_Real Eps = 1.e-07;
186 AppParCurves_Gradient_BFGS FResol(MyF, Parameters, Tol3d, Tol2d, Eps, NbIterations);
187 Parameters = MyF.NewParameters();
188 SCU = MyF.CurveValue();
193 for (j = FirstPoint; j <= LastPoint; j++) {
194 // Recherche des erreurs maxi et moyenne a un index donne:
195 for (k = 1; k <= nbP; k++) {
196 ParError(j) = Max(ParError(j), MyF.Error(j, k));
198 AvError += ParError(j);
200 AvError = AvError/(LastPoint-FirstPoint+1);
203 MError3d = MyF.MaxError3d();
204 MError2d = MyF.MaxError2d();
205 if (MError3d <= Tol3d && MError2d <= Tol2d) {
206 Done = Standard_True;
213 AppParCurves_MultiCurve AppParCurves_Gradient::Value() const {
218 Standard_Boolean AppParCurves_Gradient::IsDone() const {
223 Standard_Real AppParCurves_Gradient::Error(const Standard_Integer Index) const {
224 return ParError(Index);
227 Standard_Real AppParCurves_Gradient::AverageError() const {
231 Standard_Real AppParCurves_Gradient::MaxError3d() const {
235 Standard_Real AppParCurves_Gradient::MaxError2d() const {