0023043: Wrong results of BRepExtrema_DistShapeShape: non-null minimum distance betwe...
[occt.git] / src / Extrema / Extrema_FuncExtPS.cxx
1 // Created on: 1995-07-18
2 // Created by: Modelistation
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 //  Modified by skv - Thu Sep 30 15:21:07 2004 OCC593
23
24
25 #include <Extrema_FuncExtPS.ixx>
26 #include <gp_Vec.hxx>
27 #include <gp_Pnt.hxx>
28 #include <Standard_TypeMismatch.hxx>
29 #include <Precision.hxx>
30 #include <GeomAbs_IsoType.hxx>
31
32 Extrema_FuncExtPS::Extrema_FuncExtPS ()
33 {
34   myPinit = Standard_False;
35   mySinit = Standard_False;
36 }
37
38 //=============================================================================
39 Extrema_FuncExtPS::Extrema_FuncExtPS (const gp_Pnt& P,
40                                       const Adaptor3d_Surface& S)
41 {
42   myP = P;
43   myS = (Adaptor3d_SurfacePtr)&S;
44   GeomAbs_SurfaceType aSType = S.GetType();
45   myPinit = Standard_True;
46   mySinit = Standard_True;
47 }
48
49 //=============================================================================
50 void Extrema_FuncExtPS::Initialize(const Adaptor3d_Surface& S)
51 {
52   myS = (Adaptor3d_SurfacePtr)&S;
53   GeomAbs_SurfaceType aSType = S.GetType();
54   mySinit = Standard_True;
55   myPoint.Clear();
56   mySqDist.Clear();
57 }
58
59 //=============================================================================
60
61 void Extrema_FuncExtPS::SetPoint(const gp_Pnt& P)
62 {
63   myP = P;
64   myPinit = Standard_True;
65   myPoint.Clear();
66   mySqDist.Clear();
67 }
68
69 //=============================================================================
70
71 //=============================================================================
72
73 Standard_Integer Extrema_FuncExtPS::NbVariables () const { return 2;}
74 //=============================================================================
75
76 Standard_Integer Extrema_FuncExtPS::NbEquations () const { return 2;}
77 //=============================================================================
78
79 Standard_Boolean Extrema_FuncExtPS::Value (const math_Vector& UV, 
80                                            math_Vector& F)
81 {
82   if (!myPinit || !mySinit) Standard_TypeMismatch::Raise();
83   myU = UV(1);
84   myV = UV(2);
85   gp_Vec Dus, Dvs;
86   myS->D1(myU,myV,myPs,Dus,Dvs);
87
88   gp_Vec PPs (myP,myPs);
89
90   F(1) = PPs.Dot(Dus);
91   F(2) = PPs.Dot(Dvs);
92
93   return Standard_True;
94 }
95 //=============================================================================
96
97 Standard_Boolean Extrema_FuncExtPS::Derivatives (const math_Vector& UV, 
98                                                  math_Matrix& Df)
99 {
100   math_Vector F(1,2);
101   return Values(UV,F,Df);
102 }
103 //=============================================================================
104
105 Standard_Boolean Extrema_FuncExtPS::Values (const math_Vector& UV, 
106                                             math_Vector& F,
107                                             math_Matrix& Df)
108 {
109   if (!myPinit || !mySinit) Standard_TypeMismatch::Raise();
110   myU = UV(1);
111   myV = UV(2);
112   gp_Vec Dus, Dvs, Duus, Dvvs, Duvs;
113   myS->D2(myU,myV,myPs,Dus,Dvs,Duus,Dvvs,Duvs);
114
115   gp_Vec PPs (myP,myPs);
116
117   Df(1,1) = Dus.SquareMagnitude() + PPs.Dot(Duus);
118   Df(1,2) = Dvs.Dot(Dus)          + PPs.Dot(Duvs);
119   Df(2,1) = Df(1,2);
120   Df(2,2) = Dvs.SquareMagnitude() + PPs.Dot(Dvvs);
121
122   // 3. Value
123   F(1) = PPs.Dot(Dus);
124   F(2) = PPs.Dot(Dvs);
125
126   return Standard_True;
127 }
128 //=============================================================================
129
130 Standard_Integer Extrema_FuncExtPS::GetStateNumber ()
131 {
132   if (!myPinit || !mySinit) Standard_TypeMismatch::Raise();
133   mySqDist.Append(myPs.SquareDistance(myP));
134   myPoint.Append(Extrema_POnSurf(myU,myV,myPs));
135   return 0;
136 }
137 //=============================================================================
138
139 Standard_Integer Extrema_FuncExtPS::NbExt () const
140 {
141   return mySqDist.Length();
142 }
143 //=============================================================================
144
145 Standard_Real Extrema_FuncExtPS::SquareDistance (const Standard_Integer N) const
146 {
147   if (!myPinit || !mySinit) Standard_TypeMismatch::Raise();
148   return mySqDist.Value(N);
149 }
150 //=============================================================================
151
152 Extrema_POnSurf Extrema_FuncExtPS::Point (const Standard_Integer N) const
153 {
154   if (!myPinit || !mySinit) Standard_TypeMismatch::Raise();
155   return myPoint.Value(N);
156 }