1 // Created on: 1992-11-19
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <BRep_Tool.hxx>
19 #include <BRepAdaptor_Curve2d.hxx>
20 #include <BRepAdaptor_Surface.hxx>
21 #include <BRepClass_Edge.hxx>
22 #include <BRepClass_Intersector.hxx>
24 #include <Extrema_ExtPC2d.hxx>
25 #include <Geom2d_Curve.hxx>
26 #include <Geom2d_Line.hxx>
27 #include <Geom2dInt_GInter.hxx>
28 #include <Geom2dLProp_CLProps2d.hxx>
29 #include <gp_Dir2d.hxx>
30 #include <gp_Lin2d.hxx>
31 #include <IntRes2d_Domain.hxx>
32 #include <IntRes2d_IntersectionPoint.hxx>
33 #include <IntRes2d_Transition.hxx>
34 #include <Precision.hxx>
36 #include <TopoDS_Vertex.hxx>
39 void GetTangentAsChord(const Handle(Geom2d_Curve)& thePCurve,
41 const Standard_Real theParam,
42 const Standard_Real theFirst,
43 const Standard_Real theLast);
46 void RefineTolerance(const TopoDS_Face& aF,
47 const Geom2dAdaptor_Curve& aC,
48 const Standard_Real aT,
49 Standard_Real& aTolZ);
51 //=======================================================================
52 //function : BRepClass_Intersector
54 //=======================================================================
56 BRepClass_Intersector::BRepClass_Intersector()
60 //=======================================================================
63 //=======================================================================
64 void BRepClass_Intersector::Perform(const gp_Lin2d& L,
65 const Standard_Real P,
66 const Standard_Real Tol,
67 const BRepClass_Edge& E)
69 Standard_Real deb = 0.0, fin = 0.0, aTolZ = Tol;
70 Handle(Geom2d_Curve) aC2D;
72 const TopoDS_Edge& EE = E.Edge();
73 const TopoDS_Face& F = E.Face();
76 aC2D=BRep_Tool::CurveOnSurface(EE, F, deb, fin);
78 done = Standard_False; // !IsDone()
82 Geom2dAdaptor_Curve C(aC2D, deb, fin);
84 deb = C.FirstParameter();
85 fin = C.LastParameter();
87 // Case of "ON": direct check of belonging to edge
88 // taking into account the tolerance
89 Extrema_ExtPC2d anExtPC2d(L.Location(), C);
90 Standard_Real MinDist = RealLast(), aDist;
91 Standard_Integer MinInd = 0, i;
92 if (anExtPC2d.IsDone())
94 const Standard_Integer aNbPnts = anExtPC2d.NbExt();
95 for (i = 1; i <= aNbPnts; ++i)
97 aDist = anExtPC2d.SquareDistance(i);
108 MinDist = sqrt(MinDist);
110 if (MinDist <= aTolZ) {
111 gp_Pnt2d pnt_exact = (anExtPC2d.Point(MinInd)).Value();
112 Standard_Real par = (anExtPC2d.Point(MinInd)).Parameter();
114 RefineTolerance(F, C, par, aTolZ);
116 if (MinDist <= aTolZ) {
117 IntRes2d_Transition tr_on_lin(IntRes2d_Head);
118 IntRes2d_Position pos_on_curve = IntRes2d_Middle;
119 if (Abs(par - deb) <= Precision::Confusion()) {
120 pos_on_curve = IntRes2d_Head;
122 else if (Abs(par - fin) <= Precision::Confusion()) {
123 pos_on_curve = IntRes2d_End;
126 IntRes2d_Transition tr_on_curve(pos_on_curve);
127 IntRes2d_IntersectionPoint pnt_inter(pnt_exact, 0., par,
128 tr_on_lin, tr_on_curve,
132 done = Standard_True;
140 Standard_Real toldeb = 1.e-5, tolfin = 1.e-5;
145 DL.SetValues(L.Location(),0.,Precision::PConfusion(),ElCLib::Value(P,L),P,Precision::PConfusion());
148 DL.SetValues(L.Location(),0.,Precision::PConfusion(),Standard_True);
151 IntRes2d_Domain DE(pdeb,deb,toldeb,pfin,fin,tolfin);
152 // temporary periodic domain
153 if (C.Curve()->IsPeriodic()) {
154 DE.SetEquivalentParameters(C.FirstParameter(),
156 C.Curve()->LastParameter() -
157 C.Curve()->FirstParameter());
160 Handle(Geom2d_Line) GL= new Geom2d_Line(L);
161 Geom2dAdaptor_Curve CGA(GL);
162 Geom2dInt_GInter Inter(CGA,DL,C,DE,
163 Precision::PConfusion(),
164 Precision::PIntersection());
169 //=======================================================================
170 //function : LocalGeometry
172 //=======================================================================
173 void BRepClass_Intersector::LocalGeometry(const BRepClass_Edge& E,
174 const Standard_Real U,
177 Standard_Real& C) const
179 Standard_Real fpar, lpar;
180 Handle(Geom2d_Curve) aPCurve = BRep_Tool::CurveOnSurface(E.Edge(), E.Face(), fpar, lpar);
181 Geom2dLProp_CLProps2d Prop(aPCurve, U, 2, Precision::PConfusion());
184 if (Prop.IsTangentDefined())
187 C = Prop.Curvature();
190 GetTangentAsChord(aPCurve, Tang, U, fpar, lpar);
192 if (C > Precision::PConfusion() &&
193 !Precision::IsInfinite(C))
196 Norm.SetCoord(Tang.Y(),-Tang.X());
199 //=======================================================================
200 //function : RefineTolerance
202 //=======================================================================
203 void RefineTolerance(const TopoDS_Face& aF,
204 const Geom2dAdaptor_Curve& aC,
205 const Standard_Real aT,
206 Standard_Real& aTolZ)
208 GeomAbs_SurfaceType aTypeS;
210 BRepAdaptor_Surface aBAS(aF, Standard_False);
212 aTypeS=aBAS.GetType();
213 if (aTypeS==GeomAbs_Cylinder) {
214 Standard_Real aURes, aVRes, aTolX;
218 aURes=aBAS.UResolution(aTolZ);
219 aVRes=aBAS.VResolution(aTolZ);
221 aC.D1(aT, aP2D, aV2D);
224 aTolX=aURes*aD2D.Y()+aVRes*aD2D.X();
229 if (aTolX < Precision::Confusion()) {
230 aTolX = Precision::Confusion();
239 //=======================================================================
240 //function : GetTangentAsChord
242 //=======================================================================
243 void GetTangentAsChord(const Handle(Geom2d_Curve)& thePCurve,
244 gp_Dir2d& theTangent,
245 const Standard_Real theParam,
246 const Standard_Real theFirst,
247 const Standard_Real theLast)
249 Standard_Real Offset = 0.1*(theLast - theFirst);
251 if (theLast - theParam < Precision::PConfusion()) //theParam == theLast
253 else if (theParam + Offset > theLast) //<theParam> is close to <theLast>
254 Offset = 0.5*(theLast - theParam);
256 gp_Pnt2d aPnt2d = thePCurve->Value(theParam);
257 gp_Pnt2d OffsetPnt2d = thePCurve->Value(theParam + Offset);
259 gp_Vec2d aChord(aPnt2d, OffsetPnt2d);
263 Standard_Real SqLength = aChord.SquareMagnitude();
264 if (SqLength > Precision::SquarePConfusion())