0024766: Wrong result for distmini between a TopoDS_Shell and a TopoDS_Edge on versio...
[occt.git] / src / BRepExtrema / BRepExtrema_ExtCF.cxx
1 // Created on: 1993-12-15
2 // Created by: Christophe MARION
3 // Copyright (c) 1993-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 <BRepExtrema_ExtCF.hxx>
18
19 #include <BRep_Tool.hxx>
20 #include <BRepTools.hxx>
21 #include <Geom_Curve.hxx>
22 #include <BRepClass_FaceClassifier.hxx>
23 #include <gp_Pnt2d.hxx>
24 #include <BRepAdaptor_Surface.hxx>
25 #include <BRepAdaptor_HSurface.hxx>
26 #include <BRepAdaptor_Curve.hxx>
27 #include <BRepAdaptor_HCurve.hxx>
28
29 //=======================================================================
30 //function : BRepExtrema_ExtCF
31 //purpose  : 
32 //=======================================================================
33
34 BRepExtrema_ExtCF::BRepExtrema_ExtCF(const TopoDS_Edge& E, const TopoDS_Face& F)
35 {
36   Initialize(E, F);
37   Perform(E, F);
38 }
39
40 //=======================================================================
41 //function : Initialize
42 //purpose  : 
43 //=======================================================================
44
45 void BRepExtrema_ExtCF::Initialize(const TopoDS_Edge& E, const TopoDS_Face& F)
46 {
47   BRepAdaptor_Surface Surf(F);
48   BRepAdaptor_Curve aC(E);
49   myHS = new BRepAdaptor_HSurface(Surf);
50   Standard_Real aTolC, aTolS;
51   //
52   aTolS = Min(BRep_Tool::Tolerance(F), Precision::Confusion());
53   aTolS = Min(Surf.UResolution(aTolS), Surf.VResolution(aTolS));
54   aTolS = Max(aTolS, Precision::PConfusion());
55   //
56   aTolC = Min(BRep_Tool::Tolerance(E), Precision::Confusion());
57   aTolC = aC.Resolution(aTolC);
58   aTolC = Max(aTolC, Precision::PConfusion());
59   //
60   Standard_Real U1, U2, V1, V2;
61   BRepTools::UVBounds(F, U1, U2, V1, V2);
62   myExtCS.Initialize(myHS->Surface(), U1, U2, V1, V2, aTolC, aTolS);
63 }
64
65 //=======================================================================
66 //function : Perform
67 //purpose  : 
68 //=======================================================================
69
70 void BRepExtrema_ExtCF::Perform(const TopoDS_Edge& E, const TopoDS_Face& F2)
71 {
72   mySqDist.Clear();
73   myPointsOnS.Clear();
74   myPointsOnC.Clear();
75
76   Standard_Real U1, U2;
77   BRep_Tool::Range(E, U1, U2);
78
79   BRepAdaptor_Curve Curv(E);
80   Handle(BRepAdaptor_HCurve) HC = new BRepAdaptor_HCurve(Curv);
81   myExtCS.Perform(HC->Curve(), U1, U2);
82
83   if(!myExtCS.IsDone())
84     return;
85
86   if (myExtCS.IsParallel())
87     mySqDist.Append(myExtCS.SquareDistance(1));
88   else
89   {
90     // Exploration of points and classification
91     BRepClass_FaceClassifier classifier;
92     const Standard_Real Tol = BRep_Tool::Tolerance(F2);
93     Extrema_POnCurv P1;
94     Extrema_POnSurf P2;
95
96     for (Standard_Integer i = 1; i <= myExtCS.NbExt(); i++)
97     {
98       myExtCS.Points(i, P1, P2);
99       P2.Parameter(U1, U2);
100       const gp_Pnt2d Puv(U1, U2);
101       classifier.Perform(F2, Puv, Tol);
102       const TopAbs_State state = classifier.State();
103       if (state == TopAbs_ON || state == TopAbs_IN)
104       {
105         mySqDist.Append(myExtCS.SquareDistance(i));
106         myPointsOnC.Append(P1);
107         myPointsOnS.Append(P2);
108       }
109     }
110   }
111 }