0023948: Wrong intersection between a surface of revolution and a plane.
[occt.git] / src / HLRBRep / HLRBRep_EdgeFaceTool.cxx
1 // Created on: 1993-10-18
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 <HLRBRep_EdgeFaceTool.ixx>
18 #include <HLRBRep_Curve.hxx>
19 #include <HLRBRep_Surface.hxx>
20 #include <BRepExtrema_ExtPF.hxx>
21 #include <BRepLib_MakeVertex.hxx>
22 #include <BRep_Tool.hxx>
23 #include <BRepAdaptor_Curve2d.hxx>
24 #include <TopoDS_Vertex.hxx>
25 #include <gp.hxx>
26
27 //=======================================================================
28 //function : CurvatureDirection
29 //purpose  : 
30 //=======================================================================
31
32 Standard_Real HLRBRep_EdgeFaceTool::CurvatureValue
33  (const Standard_Address F,
34   const Standard_Real U,
35   const Standard_Real V,
36   const gp_Dir& Tg)
37 {
38   gp_Pnt P;
39   gp_Vec D1U,D1V,D2U,D2V,D2UV;
40   ((HLRBRep_Surface*)F)->D2(U,V,P,D1U,D1V,D2U,D2V,D2UV);
41   Standard_Real d1ut   = D1U*Tg;
42   Standard_Real d1vt   = D1V*Tg;
43   Standard_Real d1ud1v = D1U*D1V;
44   Standard_Real nmu2   = D1U*D1U;
45   Standard_Real nmv2   = D1V*D1V;
46   Standard_Real det = nmu2 * nmv2 - d1ud1v * d1ud1v;
47   Standard_Real alfa = ( d1ut * nmv2 - d1vt * d1ud1v ) / det;
48   Standard_Real beta = ( d1vt * nmu2 - d1ut * d1ud1v ) / det;
49   gp_Vec Nm = D1U ^ D1V;
50   if (Nm.Magnitude() > gp::Resolution()) {
51     Nm.Normalize();
52     Standard_Real alfa2 = alfa*alfa;
53     Standard_Real beta2 = beta*beta;
54     Standard_Real alfabeta = alfa*beta;
55     Standard_Real N = (Nm*D2U)*alfa2  + 2*(Nm*D2UV)*alfabeta + (Nm*D2V)*beta2;
56     Standard_Real D = nmu2    *alfa2  + 2*d1ud1v   *alfabeta + nmv2    *beta2;
57     return N/D;
58   }
59   return 0.;
60 }
61
62 //=======================================================================
63 //function : UVPoint
64 //purpose  : 
65 //=======================================================================
66
67 Standard_Boolean HLRBRep_EdgeFaceTool::UVPoint(const Standard_Real Par,
68                                                const Standard_Address E,
69                                                const Standard_Address F,
70                                                Standard_Real& U,
71                                                Standard_Real& V)
72 {
73   Standard_Real pfbid,plbid;
74   if (BRep_Tool::CurveOnSurface
75       (((HLRBRep_Curve  *)E)->Curve().Edge(),
76        ((HLRBRep_Surface*)F)->Surface().Face(),pfbid,plbid).IsNull())
77   {
78     BRepExtrema_ExtPF proj
79       (BRepLib_MakeVertex(((HLRBRep_Curve*)E)->Value3D(Par)),
80        ((HLRBRep_Surface*)F)->Surface().Face());
81     Standard_Integer i, index = 0;
82     Standard_Real dist2 = RealLast();
83     const Standard_Integer n = proj.NbExt();
84     for (i = 1; i <= n; i++) {
85       const Standard_Real newdist2 = proj.SquareDistance(i);
86       if (newdist2 < dist2) {
87         dist2 = newdist2;
88         index = i;
89       }
90     }
91     if (index == 0)
92       return Standard_False;
93
94     proj.Parameter(index,U,V);
95   }
96   else {
97     BRepAdaptor_Curve2d PC
98       (((HLRBRep_Curve  *)E)->Curve().Edge(),
99        ((HLRBRep_Surface*)F)->Surface().Face());
100     gp_Pnt2d P2d;
101     PC.D0(Par,P2d);
102     U = P2d.X();
103     V = P2d.Y();
104   }
105   return Standard_True;
106 }