1 // Created on: 2014-01-20
2 // Created by: Alexaner Malyshev
3 // Copyright (c) 2014-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement
16 #include <Extrema_GlobOptFuncCC.hxx>
19 #include <gp_Pnt2d.hxx>
21 #include <gp_Vec2d.hxx>
22 #include <math_Vector.hxx>
23 #include <Standard_Integer.hxx>
24 #include <Standard_OutOfRange.hxx>
26 static Standard_Integer _NbVariables()
32 static Standard_Boolean _Value(const Adaptor3d_Curve& C1,
33 const Adaptor3d_Curve& C2,
37 Standard_Real u = X(1);
38 Standard_Real v = X(2);
40 if (u < C1.FirstParameter() ||
41 u > C1.LastParameter() ||
42 v < C2.FirstParameter() ||
43 v > C2.LastParameter())
45 return Standard_False;
48 F = C2.Value(v).Distance(C1.Value(u));
53 static Standard_Boolean _Value(const Adaptor2d_Curve2d& C1,
54 const Adaptor2d_Curve2d& C2,
58 Standard_Real u = X(1);
59 Standard_Real v = X(2);
61 if (u < C1.FirstParameter() ||
62 u > C1.LastParameter() ||
63 v < C2.FirstParameter() ||
64 v > C2.LastParameter())
66 return Standard_False;
69 F = C2.Value(v).Distance(C1.Value(u));
73 //! F = (x2(v) - x1(u))^2 + (y2(v) - y1(u))^2 + (z2(v) - z1(u))^2
76 static Standard_Boolean _Gradient(const Adaptor3d_Curve& C1,
77 const Adaptor3d_Curve& C2,
84 if(X(1) < C1.FirstParameter() ||
85 X(1) > C1.LastParameter() ||
86 X(2) < C2.FirstParameter() ||
87 X(2) > C2.LastParameter())
89 return Standard_False;
92 C1.D1(X(1), C1D0, C1D1);
93 C2.D1(X(2), C2D0, C2D1);
95 G(1) = - (C2D0.X() - C1D0.X()) * C1D1.X()
96 - (C2D0.Y() - C1D0.Y()) * C1D1.Y()
97 - (C2D0.Z() - C1D0.Z()) * C1D1.Z();
98 G(2) = (C2D0.X() - C1D0.X()) * C2D1.X()
99 + (C2D0.Y() - C1D0.Y()) * C2D1.Y()
100 + (C2D0.Z() - C1D0.Z()) * C2D1.Z();
101 return Standard_True;
105 static Standard_Boolean _Gradient(const Adaptor2d_Curve2d& C1,
106 const Adaptor2d_Curve2d& C2,
107 const math_Vector& X,
113 if(X(1) < C1.FirstParameter() ||
114 X(1) > C1.LastParameter() ||
115 X(2) < C2.FirstParameter() ||
116 X(2) > C2.LastParameter())
118 return Standard_False;
121 C1.D1(X(1), C1D0, C1D1);
122 C2.D1(X(2), C2D0, C2D1);
124 G(1) = - (C2D0.X() - C1D0.X()) * C1D1.X()
125 - (C2D0.Y() - C1D0.Y()) * C1D1.Y();
126 G(2) = (C2D0.X() - C1D0.X()) * C2D1.X()
127 + (C2D0.Y() - C1D0.Y()) * C2D1.Y();
128 return Standard_True;
132 static Standard_Boolean _Hessian (const Adaptor3d_Curve& C1,
133 const Adaptor3d_Curve& C2,
134 const math_Vector& X,
141 if(X(1) < C1.FirstParameter() ||
142 X(1) > C1.LastParameter() ||
143 X(2) < C2.FirstParameter() ||
144 X(2) > C2.LastParameter())
146 return Standard_False;
149 C1.D2(X(1), C1D0, C1D1, C1D2);
150 C2.D2(X(2), C2D0, C2D1, C2D2);
152 H(1, 1) = C1D1.X() * C1D1.X()
153 + C1D1.Y() * C1D1.Y()
154 + C1D1.Z() * C1D1.Z()
155 - (C2D0.X() - C1D0.X()) * C1D2.X()
156 - (C2D0.Y() - C1D0.Y()) * C1D2.Y()
157 - (C2D0.Z() - C1D0.Z()) * C1D2.Z();
159 H(1, 2) = - C2D1.X() * C1D1.X()
160 - C2D1.Y() * C1D1.Y()
161 - C2D1.Z() * C1D1.Z();
165 H(2,2) = C2D1.X() * C2D1.X()
166 + C2D1.Y() * C2D1.Y()
167 + C2D1.Z() * C2D1.Z()
168 + (C2D0.X() - C1D0.X()) * C2D2.X()
169 + (C2D0.Y() - C1D0.Y()) * C2D2.Y()
170 + (C2D0.Z() - C1D0.Z()) * C2D2.Z();
171 return Standard_True;
175 static Standard_Boolean _Hessian (const Adaptor2d_Curve2d& C1,
176 const Adaptor2d_Curve2d& C2,
177 const math_Vector& X,
184 if(X(1) < C1.FirstParameter() ||
185 X(1) > C1.LastParameter() ||
186 X(2) < C2.FirstParameter() ||
187 X(2) > C2.LastParameter())
189 return Standard_False;
192 C1.D2(X(1), C1D0, C1D1, C1D2);
193 C2.D2(X(2), C2D0, C2D1, C2D2);
195 H(1, 1) = C1D1.X() * C1D1.X()
196 + C1D1.Y() * C1D1.Y()
197 - (C2D0.X() - C1D0.X()) * C1D2.X()
198 - (C2D0.Y() - C1D0.Y()) * C1D2.Y();
200 H(1, 2) = - C2D1.X() * C1D1.X()
201 - C2D1.Y() * C1D1.Y();
205 H(2,2) = C2D1.X() * C2D1.X()
206 + C2D1.Y() * C2D1.Y()
207 + (C2D0.X() - C1D0.X()) * C2D2.X()
208 + (C2D0.Y() - C1D0.Y()) * C2D2.Y();
209 return Standard_True;
214 //=======================================================================
215 //function : Extrema_GlobOptFuncCCC0
216 //purpose : Constructor
217 //=======================================================================
218 Extrema_GlobOptFuncCCC0::Extrema_GlobOptFuncCCC0(const Adaptor3d_Curve& C1,
219 const Adaptor3d_Curve& C2)
226 //=======================================================================
227 //function : Extrema_GlobOptFuncCCC0
228 //purpose : Constructor
229 //=======================================================================
230 Extrema_GlobOptFuncCCC0::Extrema_GlobOptFuncCCC0(const Adaptor2d_Curve2d& C1,
231 const Adaptor2d_Curve2d& C2)
239 //=======================================================================
240 //function : NbVariables
242 //=======================================================================
243 Standard_Integer Extrema_GlobOptFuncCCC0::NbVariables() const
245 return _NbVariables();
248 //=======================================================================
251 //=======================================================================
252 Standard_Boolean Extrema_GlobOptFuncCCC0::Value(const math_Vector& X,Standard_Real& F)
255 return _Value(*myC1_3d, *myC2_3d, X, F);
257 return _Value(*myC1_2d, *myC2_2d, X, F);
262 //=======================================================================
263 //function : Extrema_GlobOptFuncCCC1
264 //purpose : Constructor
265 //=======================================================================
266 Extrema_GlobOptFuncCCC1::Extrema_GlobOptFuncCCC1(const Adaptor3d_Curve& C1,
267 const Adaptor3d_Curve& C2)
274 //=======================================================================
275 //function : Extrema_GlobOptFuncCCC1
276 //purpose : Constructor
277 //=======================================================================
278 Extrema_GlobOptFuncCCC1::Extrema_GlobOptFuncCCC1(const Adaptor2d_Curve2d& C1,
279 const Adaptor2d_Curve2d& C2)
286 //=======================================================================
287 //function : NbVariables
289 //=======================================================================
290 Standard_Integer Extrema_GlobOptFuncCCC1::NbVariables() const
292 return _NbVariables();
295 //=======================================================================
298 //=======================================================================
299 Standard_Boolean Extrema_GlobOptFuncCCC1::Value(const math_Vector& X,Standard_Real& F)
302 return _Value(*myC1_3d, *myC2_3d, X, F);
304 return _Value(*myC1_2d, *myC2_2d, X, F);
307 //=======================================================================
308 //function : Gradient
310 //=======================================================================
311 Standard_Boolean Extrema_GlobOptFuncCCC1::Gradient(const math_Vector& X,math_Vector& G)
314 return _Gradient(*myC1_3d, *myC2_3d, X, G);
316 return _Gradient(*myC1_2d, *myC2_2d, X, G);
319 //=======================================================================
322 //=======================================================================
323 Standard_Boolean Extrema_GlobOptFuncCCC1::Values(const math_Vector& X,Standard_Real& F,math_Vector& G)
325 return (Value(X, F) && Gradient(X, G));
330 //=======================================================================
331 //function : Extrema_GlobOptFuncCCC2
332 //purpose : Constructor
333 //=======================================================================
334 Extrema_GlobOptFuncCCC2::Extrema_GlobOptFuncCCC2(const Adaptor3d_Curve& C1,
335 const Adaptor3d_Curve& C2)
342 //=======================================================================
343 //function : Extrema_GlobOptFuncCCC2
344 //purpose : Constructor
345 //=======================================================================
346 Extrema_GlobOptFuncCCC2::Extrema_GlobOptFuncCCC2(const Adaptor2d_Curve2d& C1,
347 const Adaptor2d_Curve2d& C2)
354 //=======================================================================
355 //function : NbVariables
357 //=======================================================================
358 Standard_Integer Extrema_GlobOptFuncCCC2::NbVariables() const
360 return _NbVariables();
363 //=======================================================================
366 //=======================================================================
367 Standard_Boolean Extrema_GlobOptFuncCCC2::Value(const math_Vector& X,Standard_Real& F)
370 return _Value(*myC1_3d, *myC2_3d, X, F);
372 return _Value(*myC1_2d, *myC2_2d, X, F);
375 //=======================================================================
376 //function : Gradient
378 //=======================================================================
379 Standard_Boolean Extrema_GlobOptFuncCCC2::Gradient(const math_Vector& X,math_Vector& G)
382 return _Gradient(*myC1_3d, *myC2_3d, X, G);
384 return _Gradient(*myC1_2d, *myC2_2d, X, G);
387 //=======================================================================
390 //=======================================================================
391 Standard_Boolean Extrema_GlobOptFuncCCC2::Values(const math_Vector& X,Standard_Real& F,math_Vector& G)
393 return (Value(X, F) && Gradient(X, G));
396 //=======================================================================
399 //=======================================================================
400 Standard_Boolean Extrema_GlobOptFuncCCC2::Values(const math_Vector& X,Standard_Real& F,math_Vector& G,math_Matrix& H)
402 Standard_Boolean isHessianComputed = Standard_False;
404 isHessianComputed = _Hessian(*myC1_3d, *myC2_3d, X, H);
406 isHessianComputed = _Hessian(*myC1_2d, *myC2_2d, X, H);
409 return (Value(X, F) && Gradient(X, G) && isHessianComputed);