0031035: Coding - uninitialized class fields reported by Visual Studio Code Analysis
[occt.git] / src / Extrema / Extrema_GlobOptFuncCQuadric.cxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement
13
14 #include <Extrema_GlobOptFuncCQuadric.hxx>
15
16 #include <gp_Pnt.hxx>
17 #include <ElSLib.hxx>
18 #include <ElCLib.hxx>
19
20
21 //=======================================================================
22 //function : value
23 //purpose  : 
24 //=======================================================================
25 void Extrema_GlobOptFuncCQuadric::value(Standard_Real ct,
26                                         Standard_Real &F)
27 {
28   Standard_Real u, v;
29   //
30   gp_Pnt aCP = myC->Value(ct);
31   switch (mySType)
32   {
33   case GeomAbs_Plane:
34     ElSLib::Parameters(myPln, aCP, u, v);
35     break;
36   case GeomAbs_Cylinder:
37     ElSLib::Parameters(myCylinder, aCP, u, v);
38     break;
39   case GeomAbs_Cone:
40     ElSLib::Parameters(myCone, aCP, u, v);
41     break;
42   case GeomAbs_Sphere:
43     ElSLib::Parameters(mySphere, aCP, u, v);
44     break;
45   case GeomAbs_Torus:
46     ElSLib::Parameters(myTorus, aCP, u, v);
47     break;
48   default:
49     F = Precision::Infinite();
50     return;
51   }
52   //
53   if (mySType != GeomAbs_Plane)
54   {
55     if (myUl > 2. * M_PI + Precision::PConfusion())
56     {
57       u += 2. * M_PI;
58     }
59   }
60   if (mySType == GeomAbs_Torus)
61   {
62     if (myVl > 2. * M_PI + Precision::PConfusion())
63     {
64       v += 2. * M_PI;
65     }
66   }
67
68   F = RealLast();
69   if (u >= myUf && u <= myUl && v >= myVf && v <= myVl)
70   {
71     gp_Pnt aPS = myS->Value(u, v);
72     F = Min(F, aCP.SquareDistance(aPS));
73   }
74   Standard_Integer i;
75   for (i = 0; i < 4; ++i)
76   {
77     F = Min(F, aCP.SquareDistance(myPTrim[i]));
78   }
79 }
80
81
82 //=======================================================================
83 //function : checkInputData
84 //purpose  : 
85 //=======================================================================
86 Standard_Boolean Extrema_GlobOptFuncCQuadric::checkInputData(const math_Vector   &X,
87                                                              Standard_Real       &ct) 
88 {
89   ct = X(X.Lower());
90
91   if (ct < myTf || ct > myTl )
92   {
93     return Standard_False;
94   }
95   return Standard_True;
96 }
97
98 //=======================================================================
99 //function : Extrema_GlobOptFuncCQuadric
100 //purpose  : Constructor
101 //=======================================================================
102 Extrema_GlobOptFuncCQuadric::Extrema_GlobOptFuncCQuadric(const Adaptor3d_Curve   *C,
103                                                      const Adaptor3d_Surface *S)
104 : myC(C)
105 {
106   myTf = myC->FirstParameter();
107   myTl = myC->LastParameter();
108   Standard_Real anUf = S->FirstUParameter(), anUl = S->LastUParameter();
109   Standard_Real aVf = S->FirstVParameter(), aVl = S->LastVParameter();
110   LoadQuad(S, anUf, anUl, aVf, aVl);
111 }
112 //=======================================================================
113 //function : Extrema_GlobOptFuncCQuadric
114 //purpose  : Constructor
115 //=======================================================================
116 Extrema_GlobOptFuncCQuadric::Extrema_GlobOptFuncCQuadric(const Adaptor3d_Curve *C)
117   : myC(C)
118 {
119   myTf = myC->FirstParameter();
120   myTl = myC->LastParameter();
121 }
122 //=======================================================================
123 //function : Extrema_GlobOptFuncCQuadric
124 //purpose  : Constructor
125 //=======================================================================
126 Extrema_GlobOptFuncCQuadric::Extrema_GlobOptFuncCQuadric(const Adaptor3d_Curve *C,
127   const Standard_Real theTf, const Standard_Real theTl)
128   : myC(C), myTf(theTf), myTl(theTl)
129 {
130 }
131
132 //=======================================================================
133 //function : LoadQuad
134 //purpose  : 
135 //=======================================================================
136 void Extrema_GlobOptFuncCQuadric::LoadQuad( const Adaptor3d_Surface *S, 
137   const Standard_Real theUf, const Standard_Real theUl,
138   const Standard_Real theVf, const Standard_Real theVl)
139 {
140   myS = S;
141   myUf = theUf;
142   myUl = theUl;
143   myVf = theVf;
144   myVl = theVl;
145   //
146   if (myS->IsUPeriodic())
147   {
148     Standard_Real aTMax = 2. * M_PI + Precision::PConfusion();
149     if (myUf > aTMax || myUf < -Precision::PConfusion() ||
150       Abs(myUl - myUf) > aTMax)
151     {
152       ElCLib::AdjustPeriodic(0., 2. * M_PI,
153         Min(Abs(myUl - myUf) / 2, Precision::PConfusion()),
154         myUf, myUl);
155     }
156   }
157   if (myS->IsVPeriodic())
158   {
159     Standard_Real aTMax = 2. * M_PI + Precision::PConfusion();
160     if (myVf > aTMax || myVf < -Precision::PConfusion() ||
161       Abs(myVl - myVf) > aTMax)
162     {
163       ElCLib::AdjustPeriodic(0., 2. * M_PI,
164         Min(Abs(myVl - myVf) / 2, Precision::PConfusion()),
165         myVf, myVl);
166     }
167   }
168   myPTrim[0] = myS->Value(myUf, myVf);
169   myPTrim[1] = myS->Value(myUl, myVf);
170   myPTrim[2] = myS->Value(myUl, myVl);
171   myPTrim[3] = myS->Value(myUf, myVl);
172   mySType = S->GetType();
173   switch (mySType)
174   {
175   case GeomAbs_Plane:
176     myPln = myS->Plane();
177     break;
178   case GeomAbs_Cylinder:
179      myCylinder = myS->Cylinder();
180     break;
181   case GeomAbs_Cone:
182     myCone = myS->Cone();
183     break;
184   case GeomAbs_Sphere:
185     mySphere = myS->Sphere();
186     break;
187   case GeomAbs_Torus:
188     myTorus = myS->Torus();
189     break;
190   default:
191     break;
192   }
193
194 }
195
196 //=======================================================================
197 //function : NbVariables
198 //purpose  :
199 //=======================================================================
200 Standard_Integer Extrema_GlobOptFuncCQuadric::NbVariables() const
201 {
202   return 1;
203 }
204
205 //=======================================================================
206 //function : Value
207 //purpose  :
208 //=======================================================================
209 Standard_Boolean Extrema_GlobOptFuncCQuadric::Value(const math_Vector &X,
210                                                     Standard_Real     &F)
211 {
212   Standard_Real ct;
213   if (!checkInputData(X, ct))
214     return Standard_False;
215
216   value(ct, F);
217   if (Precision::IsInfinite(F))
218   {
219     return Standard_False;
220   }
221   return Standard_True;
222 }
223
224 //=======================================================================
225 //function : QuadricParameters
226 //purpose  :
227 //=======================================================================
228 void Extrema_GlobOptFuncCQuadric::QuadricParameters(const math_Vector& theCT,
229   math_Vector& theUV ) const
230 {
231   Standard_Real u, v;
232   //
233   //Arrays of extremity points parameters correspond to array of corner
234   //points  myPTrim[] 
235   Standard_Real uext[4] = { myUf, myUl, myUl, myUf };
236   Standard_Real vext[4] = { myVf, myVf, myVl, myVl };
237   gp_Pnt aCP = myC->Value(theCT(1));
238   switch (mySType)
239   {
240   case GeomAbs_Plane:
241     ElSLib::Parameters(myPln, aCP, u, v);
242     break;
243   case GeomAbs_Cylinder:
244     ElSLib::Parameters(myCylinder, aCP, u, v);
245     break;
246   case GeomAbs_Cone:
247     ElSLib::Parameters(myCone, aCP, u, v);
248     break;
249   case GeomAbs_Sphere:
250     ElSLib::Parameters(mySphere, aCP, u, v);
251     break;
252   case GeomAbs_Torus:
253     ElSLib::Parameters(myTorus, aCP, u, v);
254     break;
255   default:
256     theUV(1) = myUf;
257     theUV(2) = myUl;
258     return;
259   }
260   //
261   if (mySType != GeomAbs_Plane)
262   {
263     if (myUl > 2. * M_PI + Precision::PConfusion())
264     {
265       u += 2. * M_PI;
266     }
267   }
268   if (mySType == GeomAbs_Torus)
269   {
270     if (myVl > 2. * M_PI + Precision::PConfusion())
271     {
272       v += 2. * M_PI;
273     }
274   }
275
276   Standard_Real F = RealLast();
277   if (u >= myUf && u <= myUl && v >= myVf && v <= myVl)
278   {
279     gp_Pnt aPS = myS->Value(u, v);
280     F = aCP.SquareDistance(aPS);
281   }
282   Standard_Integer i;
283   for (i = 0; i < 4; ++i)
284   {
285     Standard_Real Fi = aCP.SquareDistance(myPTrim[i]);
286     if (Fi < F)
287     {
288       F = Fi;
289       u = uext[i];
290       v = vext[i];
291     }
292   }
293   theUV(1) = u;
294   theUV(2) = v;
295 }
296