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