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
7 // under the terms of the GNU Lesser General Public 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.
19 #define No_Standard_RangeError
20 #define No_Standard_OutOfRange
22 #include <AppParCurves_Constraint.hxx>
23 #include <StdFail_NotDone.hxx>
24 #include <AppParCurves_MultiPoint.hxx>
26 #include <gp_Pnt2d.hxx>
28 #include <gp_Vec2d.hxx>
29 #include <TColgp_Array1OfPnt.hxx>
30 #include <TColgp_Array1OfPnt2d.hxx>
31 #include <TColgp_Array1OfVec.hxx>
32 #include <TColgp_Array1OfVec2d.hxx>
34 #include <BSplCLib.hxx>
40 AppParCurves_Projection::
41 AppParCurves_Projection(const MultiLine& SSP,
42 const Standard_Integer FirstPoint,
43 const Standard_Integer LastPoint,
44 const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
45 math_Vector& Parameters,
46 const Standard_Integer Deg,
47 const Standard_Real Tol3d,
48 const Standard_Real Tol2d,
49 const Standard_Integer NbIterations):
50 ParError(FirstPoint, LastPoint,0.0) {
52 Standard_Boolean grad = Standard_True;
53 Standard_Integer i, j, k, i2, l;
54 Standard_Real UF, DU, Fval = 0.0, FU, DFU;
55 Standard_Real MErr3d=0.0, MErr2d=0.0,
56 Gain3d = Tol3d, Gain2d=Tol2d;
57 Standard_Real EC, ECmax = 1.e10, Errsov3d, Errsov2d;
58 Standard_Integer nbP3d = ToolLine::NbP3d(SSP);
59 Standard_Integer nbP2d = ToolLine::NbP2d(SSP);
60 Standard_Integer mynbP3d=nbP3d, mynbP2d=nbP2d;
61 Standard_Integer nbP = nbP3d + nbP2d;
63 gp_Pnt2d Pt2d, P12d, P22d;
65 gp_Vec2d V12d, V22d, MyV2d;
67 if (nbP3d == 0) mynbP3d = 1;
68 if (nbP2d == 0) mynbP2d = 1;
69 TColgp_Array1OfPnt TabP(1, mynbP3d);
70 TColgp_Array1OfPnt2d TabP2d(1, mynbP2d);
71 TColgp_Array1OfVec TabV(1, mynbP3d);
72 TColgp_Array1OfVec2d TabV2d(1, mynbP2d);
74 // Calcul de la fonction F= somme(||C(ui)-Ptli||2):
75 // Appel a une fonction heritant de MultipleVarFunctionWithGradient
76 // pour calculer F et grad_F.
77 // ================================================================
79 AppParCurves_ProFunction MyF(SSP, FirstPoint,LastPoint, TheConstraints, Parameters, Deg);
85 MyF.Value(Parameters, Fval);
86 SCU = MyF.CurveValue();
87 Standard_Integer deg = SCU.Degree();
88 TColgp_Array1OfPnt TabPole(1, deg+1), TabCoef(1, deg+1);
89 TColgp_Array1OfPnt2d TabPole2d(1, deg+1), TabCoef2d(1, deg+1);
90 TColgp_Array1OfPnt TheCoef(1, (deg+1)*mynbP3d);
91 TColgp_Array1OfPnt2d TheCoef2d(1, (deg+1)*mynbP2d);
94 for (i = 1; i <= NbIterations; i++) {
96 // Stockage des Poles des courbes:
97 // ===============================
99 for (k = 1; k <= nbP3d; k++) {
100 SCU.Curve(k, TabPole);
101 BSplCLib::PolesCoefficients(TabPole, PLib::NoWeights(),
102 TabCoef, PLib::NoWeights());
103 for (j=1; j<=deg+1; j++) TheCoef(j+i2) = TabCoef(j);
107 for (k = 1; k <= nbP2d; k++) {
108 SCU.Curve(nbP3d+k, TabPole2d);
109 BSplCLib::PolesCoefficients(TabPole2d, PLib::NoWeights(),
110 TabCoef2d, PLib::NoWeights());
111 for (j=1; j<=deg+1; j++) TheCoef2d(j+i2) = TabCoef2d(j);
114 for (j = FirstPoint+1; j <= LastPoint-1; j++) {
116 if (nbP != 0 && nbP2d != 0) ToolLine::Value(SSP, j, TabP, TabP2d);
117 else if (nbP2d != 0) ToolLine::Value(SSP, j, TabP2d);
118 else ToolLine::Value(SSP, j, TabP);
123 for (k = 1; k <= nbP3d; k++) {
124 for (l=1; l<=deg+1; l++) TabCoef(l) = TheCoef(l+i2);
126 BSplCLib::CoefsD2(UF, TabCoef, PLib::NoWeights(), Pt, V1, V2);
127 MyV = gp_Vec(Pt, TabP(k));
129 DFU += V1.SquareMagnitude() + MyV*V2;
132 for (k = 1; k <= nbP2d; k++) {
133 for (l=1; l<=deg+1; l++) TabCoef2d(l) = TheCoef2d(l+i2);
135 BSplCLib::CoefsD2(UF, TabCoef2d, PLib::NoWeights(), Pt2d, V12d, V22d);
136 MyV2d = gp_Vec2d(Pt2d, TabP2d(k));
138 DFU += V12d.SquareMagnitude() + MyV2d*V22d;
141 if (DFU <= RealEpsilon()) {
142 // Verification du parametrage:
143 EC = Abs(Parameters(j) - UF);
144 if (EC > ECmax) ECmax = EC;
149 DU = Sign(Min(5.e-02, Abs(DU)), DU);
154 // Test de progression:
155 // ====================
159 MyF.Value(Parameters, Fval);
160 SCU = MyF.CurveValue();
161 MErr3d = MyF.MaxError3d();
162 MErr2d = MyF.MaxError2d();
164 if (MErr3d< Tol3d && MErr2d < Tol2d) {
165 Done = Standard_True;
168 if (MErr3d > 100.0*Tol3d || MErr2d > 100.0*Tol2d) {
169 Done = Standard_False;
170 grad = Standard_False;
174 Gain3d = Abs(Errsov3d-MErr3d);
175 Gain2d = Abs(Errsov2d-MErr2d);
176 if ((MErr3d-Gain3d*(NbIterations-i)*0.5) > Tol3d ||
177 (MErr2d-Gain2d*(NbIterations-i)*0.5) > Tol2d) {
178 if (Gain3d < Tol3d/(2*NbIterations) &&
179 Gain2d < Tol2d/(2*NbIterations)) {
190 for (j = FirstPoint; j <= LastPoint; j++) {
191 // Recherche des erreurs maxi et moyenne a un index donne:
192 for (k = 1; k <= nbP; k++) {
193 ParError(j) = Max(ParError(j), MyF.Error(j, k));
195 AvError += ParError(j);
197 AvError = AvError/(LastPoint-FirstPoint+1);
200 MError3d = MyF.MaxError3d();
201 MError2d = MyF.MaxError2d();
202 if (MError3d < Tol3d && MError2d < Tol2d) {
203 Done = Standard_True;
210 AppParCurves_MultiCurve AppParCurves_Projection::Value() const {
215 Standard_Boolean AppParCurves_Projection::IsDone() const {
220 Standard_Real AppParCurves_Projection::Error(const Standard_Integer Index) const {
221 return ParError(Index);
224 Standard_Real AppParCurves_Projection::AverageError() const {
228 Standard_Real AppParCurves_Projection::MaxError3d() const {
232 Standard_Real AppParCurves_Projection::MaxError2d() const {