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>
24 static Standard_Integer _NbVariables()
30 static Standard_Boolean _Value(const Adaptor3d_Curve& C1,
31 const Adaptor3d_Curve& C2,
35 Standard_Real u = X(1);
36 Standard_Real v = X(2);
38 if (u < C1.FirstParameter() ||
39 u > C1.LastParameter() ||
40 v < C2.FirstParameter() ||
41 v > C2.LastParameter())
43 return Standard_False;
46 F = C2.Value(v).Distance(C1.Value(u));
51 static Standard_Boolean _Value(const Adaptor2d_Curve2d& C1,
52 const Adaptor2d_Curve2d& C2,
56 Standard_Real u = X(1);
57 Standard_Real v = X(2);
59 if (u < C1.FirstParameter() ||
60 u > C1.LastParameter() ||
61 v < C2.FirstParameter() ||
62 v > C2.LastParameter())
64 return Standard_False;
67 F = C2.Value(v).Distance(C1.Value(u));
71 //! F = (x2(v) - x1(u))^2 + (y2(v) - y1(u))^2 + (z2(v) - z1(u))^2
74 static Standard_Boolean _Gradient(const Adaptor3d_Curve& C1,
75 const Adaptor3d_Curve& C2,
82 if(X(1) < C1.FirstParameter() ||
83 X(1) > C1.LastParameter() ||
84 X(2) < C2.FirstParameter() ||
85 X(2) > C2.LastParameter())
87 return Standard_False;
90 C1.D1(X(1), C1D0, C1D1);
91 C2.D1(X(2), C2D0, C2D1);
93 G(1) = - (C2D0.X() - C1D0.X()) * C1D1.X()
94 - (C2D0.Y() - C1D0.Y()) * C1D1.Y()
95 - (C2D0.Z() - C1D0.Z()) * C1D1.Z();
96 G(2) = (C2D0.X() - C1D0.X()) * C2D1.X()
97 + (C2D0.Y() - C1D0.Y()) * C2D1.Y()
98 + (C2D0.Z() - C1D0.Z()) * C2D1.Z();
100 return Standard_True;
104 static Standard_Boolean _Gradient(const Adaptor2d_Curve2d& C1,
105 const Adaptor2d_Curve2d& C2,
106 const math_Vector& X,
112 if(X(1) < C1.FirstParameter() ||
113 X(1) > C1.LastParameter() ||
114 X(2) < C2.FirstParameter() ||
115 X(2) > C2.LastParameter())
117 return Standard_False;
120 C1.D1(X(1), C1D0, C1D1);
121 C2.D1(X(2), C2D0, C2D1);
123 G(1) = - (C2D0.X() - C1D0.X()) * C1D1.X()
124 - (C2D0.Y() - C1D0.Y()) * C1D1.Y();
126 G(2) = (C2D0.X() - C1D0.X()) * C2D1.X()
127 + (C2D0.Y() - C1D0.Y()) * C2D1.Y();
130 return Standard_True;
134 static Standard_Boolean _Hessian (const Adaptor3d_Curve& C1,
135 const Adaptor3d_Curve& C2,
136 const math_Vector& X,
143 if(X(1) < C1.FirstParameter() ||
144 X(1) > C1.LastParameter() ||
145 X(2) < C2.FirstParameter() ||
146 X(2) > C2.LastParameter())
148 return Standard_False;
151 C1.D2(X(1), C1D0, C1D1, C1D2);
152 C2.D2(X(2), C2D0, C2D1, C2D2);
154 H(1, 1) = C1D1.X() * C1D1.X()
155 + C1D1.Y() * C1D1.Y()
156 + C1D1.Z() * C1D1.Z()
157 - (C2D0.X() - C1D0.X()) * C1D2.X()
158 - (C2D0.Y() - C1D0.Y()) * C1D2.Y()
159 - (C2D0.Z() - C1D0.Z()) * C1D2.Z();
161 H(1, 2) = - C2D1.X() * C1D1.X()
162 - C2D1.Y() * C1D1.Y()
163 - C2D1.Z() * C1D1.Z();
167 H(2,2) = C2D1.X() * C2D1.X()
168 + C2D1.Y() * C2D1.Y()
169 + C2D1.Z() * C2D1.Z()
170 + (C2D0.X() - C1D0.X()) * C2D2.X()
171 + (C2D0.Y() - C1D0.Y()) * C2D2.Y()
172 + (C2D0.Z() - C1D0.Z()) * C2D2.Z();
174 return Standard_True;
178 static Standard_Boolean _Hessian (const Adaptor2d_Curve2d& C1,
179 const Adaptor2d_Curve2d& C2,
180 const math_Vector& X,
187 if(X(1) < C1.FirstParameter() ||
188 X(1) > C1.LastParameter() ||
189 X(2) < C2.FirstParameter() ||
190 X(2) > C2.LastParameter())
192 return Standard_False;
195 C1.D2(X(1), C1D0, C1D1, C1D2);
196 C2.D2(X(2), C2D0, C2D1, C2D2);
198 H(1, 1) = C1D1.X() * C1D1.X()
199 + C1D1.Y() * C1D1.Y()
200 - (C2D0.X() - C1D0.X()) * C1D2.X()
201 - (C2D0.Y() - C1D0.Y()) * C1D2.Y();
203 H(1, 2) = - C2D1.X() * C1D1.X()
204 - C2D1.Y() * C1D1.Y();
208 H(2,2) = C2D1.X() * C2D1.X()
209 + C2D1.Y() * C2D1.Y()
210 + (C2D0.X() - C1D0.X()) * C2D2.X()
211 + (C2D0.Y() - C1D0.Y()) * C2D2.Y();
213 return Standard_True;
218 //=======================================================================
219 //function : Extrema_GlobOptFuncCCC0
220 //purpose : Constructor
221 //=======================================================================
222 Extrema_GlobOptFuncCCC0::Extrema_GlobOptFuncCCC0(const Adaptor3d_Curve& C1,
223 const Adaptor3d_Curve& C2)
232 //=======================================================================
233 //function : Extrema_GlobOptFuncCCC0
234 //purpose : Constructor
235 //=======================================================================
236 Extrema_GlobOptFuncCCC0::Extrema_GlobOptFuncCCC0(const Adaptor2d_Curve2d& C1,
237 const Adaptor2d_Curve2d& C2)
247 //=======================================================================
248 //function : NbVariables
250 //=======================================================================
251 Standard_Integer Extrema_GlobOptFuncCCC0::NbVariables() const
253 return _NbVariables();
256 //=======================================================================
259 //=======================================================================
260 Standard_Boolean Extrema_GlobOptFuncCCC0::Value(const math_Vector& X,Standard_Real& F)
263 return _Value(*myC1_3d, *myC2_3d, X, F);
265 return _Value(*myC1_2d, *myC2_2d, X, F);
270 //=======================================================================
271 //function : Extrema_GlobOptFuncCCC1
272 //purpose : Constructor
273 //=======================================================================
274 Extrema_GlobOptFuncCCC1::Extrema_GlobOptFuncCCC1(const Adaptor3d_Curve& C1,
275 const Adaptor3d_Curve& C2)
284 //=======================================================================
285 //function : Extrema_GlobOptFuncCCC1
286 //purpose : Constructor
287 //=======================================================================
288 Extrema_GlobOptFuncCCC1::Extrema_GlobOptFuncCCC1(const Adaptor2d_Curve2d& C1,
289 const Adaptor2d_Curve2d& C2)
298 //=======================================================================
299 //function : NbVariables
301 //=======================================================================
302 Standard_Integer Extrema_GlobOptFuncCCC1::NbVariables() const
304 return _NbVariables();
307 //=======================================================================
310 //=======================================================================
311 Standard_Boolean Extrema_GlobOptFuncCCC1::Value(const math_Vector& X,Standard_Real& F)
314 return _Value(*myC1_3d, *myC2_3d, X, F);
316 return _Value(*myC1_2d, *myC2_2d, X, F);
319 //=======================================================================
320 //function : Gradient
322 //=======================================================================
323 Standard_Boolean Extrema_GlobOptFuncCCC1::Gradient(const math_Vector& X,math_Vector& G)
326 return _Gradient(*myC1_3d, *myC2_3d, X, G);
328 return _Gradient(*myC1_2d, *myC2_2d, X, G);
331 //=======================================================================
334 //=======================================================================
335 Standard_Boolean Extrema_GlobOptFuncCCC1::Values(const math_Vector& X,Standard_Real& F,math_Vector& G)
337 return (Value(X, F) && Gradient(X, G));
342 //=======================================================================
343 //function : Extrema_GlobOptFuncCCC2
344 //purpose : Constructor
345 //=======================================================================
346 Extrema_GlobOptFuncCCC2::Extrema_GlobOptFuncCCC2(const Adaptor3d_Curve& C1,
347 const Adaptor3d_Curve& C2)
356 //=======================================================================
357 //function : Extrema_GlobOptFuncCCC2
358 //purpose : Constructor
359 //=======================================================================
360 Extrema_GlobOptFuncCCC2::Extrema_GlobOptFuncCCC2(const Adaptor2d_Curve2d& C1,
361 const Adaptor2d_Curve2d& C2)
370 //=======================================================================
371 //function : NbVariables
373 //=======================================================================
374 Standard_Integer Extrema_GlobOptFuncCCC2::NbVariables() const
376 return _NbVariables();
379 //=======================================================================
382 //=======================================================================
383 Standard_Boolean Extrema_GlobOptFuncCCC2::Value(const math_Vector& X,Standard_Real& F)
386 return _Value(*myC1_3d, *myC2_3d, X, F);
388 return _Value(*myC1_2d, *myC2_2d, X, F);
391 //=======================================================================
392 //function : Gradient
394 //=======================================================================
395 Standard_Boolean Extrema_GlobOptFuncCCC2::Gradient(const math_Vector& X,math_Vector& G)
398 return _Gradient(*myC1_3d, *myC2_3d, X, G);
400 return _Gradient(*myC1_2d, *myC2_2d, X, G);
403 //=======================================================================
406 //=======================================================================
407 Standard_Boolean Extrema_GlobOptFuncCCC2::Values(const math_Vector& X,Standard_Real& F,math_Vector& G)
409 return (Value(X, F) && Gradient(X, G));
412 //=======================================================================
415 //=======================================================================
416 Standard_Boolean Extrema_GlobOptFuncCCC2::Values(const math_Vector& X,Standard_Real& F,math_Vector& G,math_Matrix& H)
418 Standard_Boolean isHessianComputed = Standard_False;
420 isHessianComputed = _Hessian(*myC1_3d, *myC2_3d, X, H);
422 isHessianComputed = _Hessian(*myC1_2d, *myC2_2d, X, H);
424 return (Value(X, F) && Gradient(X, G) && isHessianComputed);