b311480e |
1 | // Created on: 1998-12-08 |
2 | // Created by: Igor FEOKTISTOV |
3 | // Copyright (c) 1998-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 | void AppParCurves_Variational::Project(const Handle(FEmTool_Curve)& C, |
18 | const TColStd_Array1OfReal& Ti, |
19 | TColStd_Array1OfReal& ProjTi, |
20 | TColStd_Array1OfReal& Distance, |
21 | Standard_Integer& NumPoints, |
22 | Standard_Real& MaxErr, |
23 | Standard_Real& QuaErr, |
24 | Standard_Real& AveErr, |
25 | const Standard_Integer NbIterations) const |
26 | |
27 | { |
28 | // Initialisation |
29 | |
30 | const Standard_Real Seuil = 1.e-9, Eps = 1.e-12; |
31 | |
32 | MaxErr = QuaErr = AveErr = 0.; |
33 | |
34 | Standard_Integer Ipnt, NItCv, Iter, i, i0 = -myDimension, d0 = Distance.Lower() - 1; |
35 | |
36 | Standard_Real TNew, Dist, T0, Dist0, F1, F2, Aux, DF, Ecart; |
37 | |
38 | Standard_Boolean EnCour; |
39 | |
40 | TColStd_Array1OfReal ValOfC(1, myDimension), FirstDerOfC(1, myDimension), |
41 | SecndDerOfC(1, myDimension); |
42 | |
43 | for(Ipnt = 1; Ipnt <= ProjTi.Length(); Ipnt++) { |
44 | |
45 | i0 += myDimension; |
46 | |
47 | TNew = Ti(Ipnt); |
48 | |
49 | EnCour = Standard_True; |
50 | NItCv = 0; |
51 | Iter = 0; |
52 | C->D0(TNew, ValOfC); |
53 | |
54 | Dist = 0; |
55 | for(i = 1; i <= myDimension; i++) { |
56 | Aux = ValOfC(i) - myTabPoints->Value(i0 + i); |
57 | Dist += Aux * Aux; |
58 | } |
59 | Dist = Sqrt(Dist); |
60 | |
61 | // ------- Newton's method for solving (C'(t),C(t) - P) = 0 |
62 | |
63 | while( EnCour ) { |
64 | |
65 | Iter++; |
66 | T0 = TNew; |
67 | Dist0 = Dist; |
68 | |
69 | C->D2(TNew, SecndDerOfC); |
70 | C->D1(TNew, FirstDerOfC); |
71 | |
72 | F1 = F2 = 0.; |
73 | for(i = 1; i <= myDimension; i++) { |
74 | Aux = ValOfC(i) - myTabPoints->Value(i0 + i); |
75 | DF = FirstDerOfC(i); |
76 | F1 += Aux*DF; // (C'(t),C(t) - P) |
77 | F2 += DF*DF + Aux * SecndDerOfC(i); // ((C'(t),C(t) - P))' |
78 | } |
79 | |
80 | if(Abs(F2) < Eps) |
81 | EnCour = Standard_False; |
82 | else { |
83 | // Formula of Newton x(k+1) = x(k) - F(x(k))/F'(x(k)) |
84 | TNew -= F1 / F2; |
85 | if(TNew < 0.) TNew = 0.; |
86 | if(TNew > 1.) TNew = 1.; |
87 | |
88 | |
89 | // Analysis of result |
90 | |
91 | C->D0(TNew, ValOfC); |
92 | |
93 | Dist = 0; |
94 | for(i = 1; i <= myDimension; i++) { |
95 | Aux = ValOfC(i) - myTabPoints->Value(i0 + i); |
96 | Dist += Aux * Aux; |
97 | } |
98 | Dist = Sqrt(Dist); |
99 | |
100 | Ecart = Dist0 - Dist; |
101 | |
102 | if(Ecart <= -Seuil) { |
103 | // Pas d'amelioration on s'arrete |
104 | EnCour = Standard_False; |
105 | TNew = T0; |
106 | Dist = Dist0; |
107 | } |
108 | else if(Ecart <= Seuil) |
109 | // Convergence |
110 | NItCv++; |
111 | else |
112 | NItCv = 0; |
113 | |
114 | if((NItCv >= 2) || (Iter >= NbIterations)) EnCour = Standard_False; |
115 | |
116 | } |
117 | } |
118 | |
119 | |
120 | ProjTi(Ipnt) = TNew; |
121 | Distance(d0 + Ipnt) = Dist; |
122 | if(Dist > MaxErr) { |
123 | MaxErr = Dist; |
124 | NumPoints = Ipnt; |
125 | } |
126 | QuaErr += Dist * Dist; |
127 | AveErr += Dist; |
128 | } |
129 | |
130 | NumPoints = NumPoints + myFirstPoint - 1;// Setting NumPoints to interval [myFirstPoint, myLastPoint] |
131 | |
132 | } |