0025720: Incorrect code of math classes can lead to unpredicted behavior of algorithms
[occt.git] / src / Extrema / Extrema_GenLocateExtPS.cxx
1 // Created on: 1995-07-18
2 // Created by: Modelistation
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Extrema_GenLocateExtPS.ixx>
18 #include <Extrema_FuncExtPS.hxx>
19 #include <StdFail_NotDone.hxx>
20 #include <gp.hxx>
21 #include <math_FunctionSetRoot.hxx>
22 #include <math_NewtonFunctionSetRoot.hxx>
23 #include <math_Vector.hxx>
24
25 //=============================================================================
26
27 Extrema_GenLocateExtPS::Extrema_GenLocateExtPS () { myDone = Standard_False; }
28 //=============================================================================
29
30 Extrema_GenLocateExtPS::Extrema_GenLocateExtPS (const gp_Pnt&          P,
31                                                 const Adaptor3d_Surface& S, 
32                                                 const Standard_Real    U0, 
33                                                 const Standard_Real    V0,
34                                                 const Standard_Real    TolU, 
35                                                 const Standard_Real    TolV)
36 /*-----------------------------------------------------------------------------
37 Function:
38   Find (U,V) close to (U0,V0) so that dist(S(U,V),P) was extreme.
39
40 Method:
41   If (u,v) is a solution, it is possible to write:
42    { F1(u,v) = (S(u,v)-P).dS/du(u,v) = 0.
43    { F2(u,v) = (S(u,v)-P).dS/dv(u,v) = 0.
44   The problem consists in finding, in the interval of surface definition,
45   the root of the system closest to (U0,V0).
46   Use class math_FunctionSetRoot with the following construction arguments:
47   - F: Extrema_FuncExtPS created from P and S,
48   - U0V0: math_Vector (U0,V0),
49   - Tol: Min(TolU,TolV),            
50                                     
51   - math_Vector (Uinf,Usup),
52   - math_Vector (Vinf,Vsup),
53   - 100. .
54 ---------------------------------------------------------------------------*/  
55 {
56   myDone = Standard_False;
57
58   Standard_Real Uinf, Usup, Vinf, Vsup;
59   Uinf = S.FirstUParameter();
60   Usup = S.LastUParameter();
61   Vinf = S.FirstVParameter();
62   Vsup = S.LastVParameter();
63
64   Extrema_FuncExtPS F (P,S);
65   math_Vector Tol(1, 2), Start(1, 2), BInf(1, 2), BSup(1, 2);
66
67   Tol(1) = TolU;
68   Tol(2) = TolV;
69
70   Start(1) = U0;
71   Start(2) = V0;
72
73   BInf(1) = Uinf;
74   BInf(2) = Vinf;
75   BSup(1) = Usup;
76   BSup(2) = Vsup;
77
78   math_FunctionSetRoot SR (F, Tol);
79   SR.Perform(F, Start, BInf, BSup);
80   if (!SR.IsDone()) 
81     return;
82
83   mySqDist = F.SquareDistance(1);
84   myPoint = F.Point(1);
85   myDone = Standard_True;
86 }
87 //=============================================================================
88
89 Standard_Boolean Extrema_GenLocateExtPS::IsDone () const { return myDone; }
90 //=============================================================================
91
92 Standard_Real Extrema_GenLocateExtPS::SquareDistance () const
93 {
94   if (!IsDone()) { StdFail_NotDone::Raise(); }
95   return mySqDist;
96 }
97 //=============================================================================
98
99 const Extrema_POnSurf& Extrema_GenLocateExtPS::Point () const
100 {
101   if (!IsDone()) { StdFail_NotDone::Raise(); }
102   return myPoint;
103 }
104 //=============================================================================