Test for 0022778: Bug in BRepMesh
[occt.git] / src / GeomAPI / GeomAPI_ExtremaSurfaceSurface.cxx
1 // Created on: 1994-03-18
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1994-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 #include <GeomAPI_ExtremaSurfaceSurface.ixx>
23
24
25 #include <GeomAdaptor_Surface.hxx>
26 #include <Extrema_POnSurf.hxx>
27
28 #include <Precision.hxx>
29
30
31
32 //=======================================================================
33 //function : GeomAPI_ExtremaSurfaceSurface
34 //purpose  : 
35 //=======================================================================
36
37 GeomAPI_ExtremaSurfaceSurface::GeomAPI_ExtremaSurfaceSurface()
38 {
39   myIsDone = Standard_False;
40 }
41
42
43 //=======================================================================
44 //function : GeomAPI_ExtremaSurfaceSurface
45 //purpose  : 
46 //=======================================================================
47
48 GeomAPI_ExtremaSurfaceSurface::GeomAPI_ExtremaSurfaceSurface
49   (const Handle(Geom_Surface)& S1,
50    const Handle(Geom_Surface)& S2)
51 {
52   Init(S1,S2);
53 }
54
55
56 //=======================================================================
57 //function : GeomAPI_ExtremaSurfaceSurface
58 //purpose  : 
59 //=======================================================================
60
61 GeomAPI_ExtremaSurfaceSurface::GeomAPI_ExtremaSurfaceSurface
62   (const Handle(Geom_Surface)& S1,
63    const Handle(Geom_Surface)& S2,
64    const Standard_Real         U1min,
65    const Standard_Real         U1max,
66    const Standard_Real         V1min,
67    const Standard_Real         V1max,
68    const Standard_Real         U2min,
69    const Standard_Real         U2max,
70    const Standard_Real         V2min,
71    const Standard_Real         V2max)
72 {
73   Init(S1,S2,U1min,U1max,V1min,V1max,U2min,U2max,V2min,V2max);
74 }
75
76
77 //=======================================================================
78 //function : Init
79 //purpose  : 
80 //=======================================================================
81
82 void GeomAPI_ExtremaSurfaceSurface::Init
83   (const Handle(Geom_Surface)& S1,
84    const Handle(Geom_Surface)& S2)
85 {
86   GeomAdaptor_Surface TheSurface1 (S1);
87   GeomAdaptor_Surface TheSurface2 (S2);
88
89   Standard_Real Tol = Precision::PConfusion();
90   
91   Extrema_ExtSS theExtSS(TheSurface1, TheSurface2,Tol,Tol);
92   myExtSS = theExtSS;
93
94   myIsDone = myExtSS.IsDone() && ( myExtSS.NbExt() > 0);
95
96   if ( myIsDone) {
97
98     // evaluate the lower distance and its index;
99     
100     Standard_Real Dist2, Dist2Min = myExtSS.SquareDistance(1);
101     myIndex = 1;
102     
103     for ( Standard_Integer i = 2; i <= myExtSS.NbExt(); i++) {
104       Dist2 = myExtSS.SquareDistance(i);
105       if ( Dist2 < Dist2Min) {
106         Dist2Min = Dist2;
107         myIndex = i;
108       }
109     }
110   }
111 }
112
113
114 //=======================================================================
115 //function : Init
116 //purpose  : 
117 //=======================================================================
118
119 void GeomAPI_ExtremaSurfaceSurface::Init
120   (const Handle(Geom_Surface)& S1,
121    const Handle(Geom_Surface)& S2, 
122    const Standard_Real         U1min,
123    const Standard_Real         U1max,
124    const Standard_Real         V1min,
125    const Standard_Real         V1max,
126    const Standard_Real         U2min,
127    const Standard_Real         U2max,
128    const Standard_Real         V2min,
129    const Standard_Real         V2max)
130 {
131   GeomAdaptor_Surface TheSurface1 (S1,U1min,U1max,V1min,V1max);
132   GeomAdaptor_Surface TheSurface2 (S2,U2min,U2max,V2min,V2max);
133
134   Standard_Real Tol = Precision::PConfusion();
135   Extrema_ExtSS theExtSS(TheSurface1, TheSurface2,
136                           U1min,U1max,V1min,V1max,
137                           U2min,U2max,V2min,V2max,
138                           Tol,Tol);
139
140   myExtSS = theExtSS;
141   myIsDone = myExtSS.IsDone() && ( myExtSS.NbExt() > 0);
142
143   if ( myIsDone) {
144
145     // evaluate the lower distance and its index;
146     
147     Standard_Real Dist2, Dist2Min = myExtSS.SquareDistance(1);
148     myIndex = 1;
149     
150     for ( Standard_Integer i = 2; i <= myExtSS.NbExt(); i++) {
151       Dist2 = myExtSS.SquareDistance(i);
152       if ( Dist2 < Dist2Min) {
153         Dist2Min = Dist2;
154         myIndex = i;
155       }
156     }
157   }
158 }
159
160
161 //=======================================================================
162 //function : NbExtrema
163 //purpose  : 
164 //=======================================================================
165
166 Standard_Integer GeomAPI_ExtremaSurfaceSurface::NbExtrema() const 
167 {
168   if ( myIsDone) 
169     return myExtSS.NbExt();
170   else
171     return 0;
172 }
173
174
175 //=======================================================================
176 //function : Points
177 //purpose  : 
178 //=======================================================================
179
180 void GeomAPI_ExtremaSurfaceSurface::Points
181   (const Standard_Integer Index,
182          gp_Pnt&          P1,
183          gp_Pnt&          P2) const 
184 {
185   Standard_OutOfRange_Raise_if( Index < 1 || Index > NbExtrema(),
186                                "GeomAPI_ExtremaCurveCurve::Points");
187
188   Extrema_POnSurf PS1,PS2;
189   myExtSS.Points(Index,PS1,PS2);
190
191   P1 = PS1.Value();
192   P2 = PS2.Value();
193 }
194
195
196 //=======================================================================
197 //function : Parameters
198 //purpose  : 
199 //=======================================================================
200
201 void GeomAPI_ExtremaSurfaceSurface::Parameters
202   (const Standard_Integer Index,
203          Standard_Real&   U1,
204          Standard_Real&   V1,
205          Standard_Real&   U2,
206          Standard_Real&   V2) const 
207 {
208   Standard_OutOfRange_Raise_if( Index < 1 || Index > NbExtrema(),
209                                "GeomAPI_ExtremaCurveCurve::Parameters");
210
211   Extrema_POnSurf PS1,PS2;
212   myExtSS.Points(Index,PS1,PS2);
213
214   PS1.Parameter(U1,V1);
215   PS2.Parameter(U2,V2);
216 }
217
218
219 //=======================================================================
220 //function : Distance
221 //purpose  : 
222 //=======================================================================
223
224 Standard_Real GeomAPI_ExtremaSurfaceSurface::Distance
225   (const Standard_Integer Index) const 
226 {
227   Standard_OutOfRange_Raise_if( Index < 1 || Index > NbExtrema(),
228                                "GeomAPI_ExtremaCurveCurve::Distance");
229
230   return sqrt (myExtSS.SquareDistance(Index));
231 }
232
233
234 //=======================================================================
235 //function : NearestPoints
236 //purpose  : 
237 //=======================================================================
238
239 void GeomAPI_ExtremaSurfaceSurface::NearestPoints
240   (gp_Pnt& P1, 
241    gp_Pnt& P2) const 
242 {
243   StdFail_NotDone_Raise_if
244     ( !myIsDone, "GeomAPI_ExtremaSurfaceSurface::NearestPoints");
245
246   Points(myIndex,P1,P2);
247 }
248
249
250 //=======================================================================
251 //function : LowerDistanceParameters
252 //purpose  : 
253 //=======================================================================
254
255 void GeomAPI_ExtremaSurfaceSurface::LowerDistanceParameters
256   (Standard_Real& U1,
257    Standard_Real& V1,
258    Standard_Real& U2,
259    Standard_Real& V2) const 
260 {
261   StdFail_NotDone_Raise_if
262     ( !myIsDone, "GeomAPI_ExtremaSurfaceSurface::LowerDistanceParameters");
263
264   Parameters(myIndex,U1,V1,U2,V2);
265 }
266
267
268 //=======================================================================
269 //function : LowerDistance
270 //purpose  : 
271 //=======================================================================
272
273 Standard_Real GeomAPI_ExtremaSurfaceSurface::LowerDistance() const 
274 {
275   StdFail_NotDone_Raise_if
276     ( !myIsDone, "GeomAPI_ExtremaSurfaceSurface::LowerDistance");
277
278   return sqrt (myExtSS.SquareDistance(myIndex));
279 }
280
281
282 //=======================================================================
283 //function : Standard_Real
284 //purpose  : 
285 //=======================================================================
286
287 GeomAPI_ExtremaSurfaceSurface::operator Standard_Real() const
288 {
289   return LowerDistance();
290 }
291
292
293 //=======================================================================
294 //function : Standard_Integer
295 //purpose  : 
296 //=======================================================================
297
298 GeomAPI_ExtremaSurfaceSurface::operator Standard_Integer() const
299 {
300   return NbExtrema();
301 }
302
303
304