1 // Created on: 1994-10-07
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1994-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 <BRepIntCurveSurface_Inter.hxx>
20 #include <Geom_Curve.hxx>
22 #include <gp_Pnt2d.hxx>
23 #include <IntCurveSurface_IntersectionPoint.hxx>
24 #include <IntCurveSurface_TransitionOnCurve.hxx>
25 #include <Precision.hxx>
26 #include <Standard_ProgramError.hxx>
27 #include <TopExp_Explorer.hxx>
28 #include <TopLoc_Location.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Vertex.hxx>
32 #include <TopOpeBRep_FaceEdgeIntersector.hxx>
33 #include <TopOpeBRepDS_Transition.hxx>
34 #include <TopOpeBRepTool_ShapeTool.hxx>
38 extern Standard_Boolean TopOpeBRep_GettraceFITOL();
39 extern Standard_Boolean TopOpeBRep_GettraceSAVFF();
40 #include <TCollection_AsciiString.hxx>
41 #include <Standard_CString.hxx>
42 #include <BRepTools.hxx>
43 static void SAVFE(const TopoDS_Face& F1,const TopoDS_Edge& E)
45 TCollection_AsciiString aname_1("FE_face"), aname_2("FE_edge");
46 Standard_CString name_1 = aname_1.ToCString(), name_2 = aname_2.ToCString();
47 std::cout<<"FaceEdgeIntersector : "<<name_1<<","<<name_2<<std::endl;
48 BRepTools::Write(F1,name_1); BRepTools::Write(E,name_2);
50 extern Standard_Boolean TopOpeBRepTool_GettraceKRO();
51 #include <TopOpeBRepTool_KRO.hxx>
52 Standard_EXPORT TOPKRO KRO_DSFILLER_INTFE("intersection face/edge");
56 //=======================================================================
57 //function : TopOpeBRep_FaceEdgeIntersector
59 //=======================================================================
61 TopOpeBRep_FaceEdgeIntersector::TopOpeBRep_FaceEdgeIntersector()
66 //=======================================================================
67 //function : ResetIntersection
69 //=======================================================================
71 void TopOpeBRep_FaceEdgeIntersector::ResetIntersection()
73 mySequenceOfPnt.Clear();
74 mySequenceOfState.Clear();
76 myIntersectionDone = Standard_False;
80 //=======================================================================
83 //=======================================================================
85 void TopOpeBRep_FaceEdgeIntersector::Perform(const TopoDS_Shape& SF,
86 const TopoDS_Shape& SE)
89 if (!myForceTolerance) ShapeTolerances(SF,SE);
90 myTol = BRep_Tool::Tolerance(TopoDS::Edge(SE));
92 if (TopOpeBRep_GettraceFITOL()) std::cout<<"Perform : myTol = "<<myTol<<std::endl;
95 myFace = TopoDS::Face(SF); myFace.Orientation(TopAbs_FORWARD);
96 myEdge = TopoDS::Edge(SE); myEdge.Orientation(TopAbs_FORWARD);
99 if (TopOpeBRep_GettraceSAVFF()) SAVFE(myFace,myEdge);
104 const Handle(Geom_Curve) C = BRep_Tool::Curve(myEdge,loc,f,l);
106 Handle(Geom_Geometry) GGao1 = C->Transformed(loc.Transformation());
107 Handle(Geom_Curve)* PGCao1 = (Handle(Geom_Curve)*)&GGao1;
108 myCurve.Load(*PGCao1,f,l);
112 if (TopOpeBRepTool_GettraceKRO()) KRO_DSFILLER_INTFE.Start();
115 BRepIntCurveSurface_Inter FEINT;
116 FEINT.Init(myFace,myCurve,myTol);
119 if (TopOpeBRepTool_GettraceKRO()) KRO_DSFILLER_INTFE.Stop();
122 for (FEINT.Init(myFace,myCurve,myTol); FEINT.More(); FEINT.Next()) {
123 mySequenceOfPnt.Append(FEINT.Point());
124 Standard_Integer i = (FEINT.State() == TopAbs_IN) ? 0 : 1;
125 mySequenceOfState.Append(i);
128 myNbPoints = mySequenceOfPnt.Length();
129 myIntersectionDone = Standard_True;
134 //=======================================================================
137 //=======================================================================
139 Standard_Boolean TopOpeBRep_FaceEdgeIntersector::IsEmpty ()
141 Standard_Boolean b = myNbPoints == 0;
146 //=======================================================================
149 //=======================================================================
151 const TopoDS_Shape& TopOpeBRep_FaceEdgeIntersector::Shape
152 (const Standard_Integer Index) const
154 if ( Index == 1 ) return myFace;
155 else if ( Index == 2 ) return myEdge;
156 else throw Standard_ProgramError("TopOpeBRep_FaceEdgeIntersector::Shape");
159 //=======================================================================
160 //function : ForceTolerance
162 //=======================================================================
164 void TopOpeBRep_FaceEdgeIntersector::ForceTolerance(const Standard_Real Tol)
167 myForceTolerance = Standard_True;
170 if (TopOpeBRep_GettraceFITOL())
171 std::cout<<"ForceTolerance : myTol = "<<myTol<<std::endl;
175 //=======================================================================
176 //function : Tolerance
178 //=======================================================================
180 Standard_Real TopOpeBRep_FaceEdgeIntersector::Tolerance() const
185 //=======================================================================
186 //function : NbPoints
188 //=======================================================================
190 Standard_Integer TopOpeBRep_FaceEdgeIntersector::NbPoints() const
192 Standard_Integer n = myNbPoints;
197 //=======================================================================
198 //function : InitPoint
200 //=======================================================================
202 void TopOpeBRep_FaceEdgeIntersector::InitPoint()
207 //=======================================================================
208 //function : MorePoint
210 //=======================================================================
212 Standard_Boolean TopOpeBRep_FaceEdgeIntersector::MorePoint() const
214 Standard_Boolean b = myPointIndex <= myNbPoints;
218 //=======================================================================
219 //function : NextPoint
221 //=======================================================================
223 void TopOpeBRep_FaceEdgeIntersector::NextPoint()
228 //=======================================================================
231 //=======================================================================
233 gp_Pnt TopOpeBRep_FaceEdgeIntersector::Value() const
235 const IntCurveSurface_IntersectionPoint& IP = mySequenceOfPnt(myPointIndex);
236 const gp_Pnt& P = IP.Pnt();
241 //=======================================================================
242 //function : Parameter
244 //=======================================================================
246 Standard_Real TopOpeBRep_FaceEdgeIntersector::Parameter() const
248 const IntCurveSurface_IntersectionPoint& IP = mySequenceOfPnt(myPointIndex);
249 Standard_Real p = IP.W();
253 //=======================================================================
256 //=======================================================================
258 void TopOpeBRep_FaceEdgeIntersector::UVPoint(gp_Pnt2d& P2d) const
260 const IntCurveSurface_IntersectionPoint& IP = mySequenceOfPnt(myPointIndex);
261 Standard_Real u = IP.U();
262 Standard_Real v = IP.V();
266 //=======================================================================
269 //=======================================================================
271 TopAbs_State TopOpeBRep_FaceEdgeIntersector::State() const
273 Standard_Integer i = mySequenceOfState(myPointIndex);
274 TopAbs_State s = (i == 0 ) ? TopAbs_IN : TopAbs_ON;
278 //=======================================================================
279 //function : Transition
281 //=======================================================================
283 TopOpeBRepDS_Transition TopOpeBRep_FaceEdgeIntersector::Transition
284 (const Standard_Integer Index,
285 const TopAbs_Orientation FaceOrientation) const
287 // TopAbs_ShapeEnum onB = TopAbs_FACE, onA = TopAbs_FACE; // bidon
288 // if ((FaceOrientation == TopAbs_INTERNAL) ||
289 // (FaceOrientation == TopAbs_EXTERNAL)) {
290 // TopOpeBRepDS_Transition TR(TopAbs_IN,TopAbs_IN,onB,onA); // IN bidon
291 // TR.Set(FaceOrientation);
295 TopAbs_State stB, stA;
297 const IntCurveSurface_IntersectionPoint& IP = mySequenceOfPnt(myPointIndex);
299 if ( Index == 2 ) { //-- Edge In <=> Rentre ds la matiere face
300 switch (IP.Transition()) {
301 case IntCurveSurface_In : stB = TopAbs_OUT; stA = TopAbs_IN; break;
302 case IntCurveSurface_Out : stB = TopAbs_IN; stA = TopAbs_OUT; break;
303 default : stB = TopAbs_IN; stA = TopAbs_IN; break;
306 TopOpeBRepDS_Transition TR;
307 TopAbs_ShapeEnum onB = TopAbs_FACE, onA = TopAbs_FACE;
308 if (FaceOrientation == TopAbs_FORWARD)
309 TR.Set(stB,stA,onB,onA);
310 else if (FaceOrientation == TopAbs_REVERSED)
311 TR.Set(stA,stB,onA,onB);
312 else if (FaceOrientation == TopAbs_EXTERNAL)
313 TR.Set(TopAbs_OUT,TopAbs_OUT,onA,onB);
314 else if (FaceOrientation == TopAbs_INTERNAL)
315 TR.Set(TopAbs_IN,TopAbs_IN,onA,onB);
319 else if ( Index == 1 ) { //-- Face On est toujours ds la face .
320 switch (IP.Transition()) {
321 case IntCurveSurface_In : stB = stA = TopAbs_IN; break;
322 case IntCurveSurface_Out : stB = stA = TopAbs_IN; break;
323 default : stB = stA = TopAbs_IN; break;
325 TopAbs_ShapeEnum onB = TopAbs_FACE, onA = TopAbs_FACE;
326 TopOpeBRepDS_Transition TR;
327 TR.Set(stB,stA,onB,onA);
331 else throw Standard_ProgramError("FEINT Transition Index");
334 //=======================================================================
335 //function : IsVertex
337 //=======================================================================
339 Standard_Boolean TopOpeBRep_FaceEdgeIntersector::IsVertex
340 (const TopoDS_Shape& S, const gp_Pnt& P,
341 const Standard_Real Tol, TopoDS_Vertex& VR)
343 Standard_Boolean isv = Standard_False;
346 Standard_Real Tol2=Tol*Tol;
347 for (myVertexExplorer.Init(S,TopAbs_VERTEX);
348 myVertexExplorer.More();
349 myVertexExplorer.Next()) {
350 const TopoDS_Shape& SS = myVertexExplorer.Current();
351 const TopoDS_Vertex& VV = TopoDS::Vertex(SS);
352 gp_Pnt PV = BRep_Tool::Pnt(VV);
353 isv = P.SquareDistance(PV) < Tol2;
362 //=======================================================================
363 //function : IsVertex
365 //=======================================================================
367 Standard_Boolean TopOpeBRep_FaceEdgeIntersector::IsVertex
368 (const Standard_Integer I, TopoDS_Vertex& VR)
370 Standard_Boolean isv = Standard_False;
372 if (I == 1) isv = IsVertex(myFace,P,myTol,VR);
373 else if (I == 2) isv = IsVertex(myEdge,P,myTol,VR);
377 //=======================================================================
380 //=======================================================================
382 Standard_Integer TopOpeBRep_FaceEdgeIntersector::Index() const
392 //=======================================================================
393 //function : ShapeTolerances
394 //purpose : (private)
395 //=======================================================================
397 void TopOpeBRep_FaceEdgeIntersector::ShapeTolerances(const TopoDS_Shape& S1,
398 const TopoDS_Shape& S2)
400 myTol = Max(ToleranceMax(S1,TopAbs_EDGE),ToleranceMax(S2,TopAbs_EDGE));
401 myForceTolerance = Standard_False;
404 if (TopOpeBRep_GettraceFITOL()) {
405 std::cout<<"ShapeTolerances on S1 = ";TopAbs::Print(S1.ShapeType(),std::cout);
406 std::cout<<" S2 = ";TopAbs::Print(S2.ShapeType(),std::cout);
407 std::cout<<" : myTol = "<<myTol<<std::endl;
412 //=======================================================================
413 //function : ToleranceMax
414 //purpose : (private)
415 //=======================================================================
417 Standard_Real TopOpeBRep_FaceEdgeIntersector::ToleranceMax
418 (const TopoDS_Shape& S,
419 const TopAbs_ShapeEnum T)const
421 TopExp_Explorer e(S,T);
422 if ( ! e.More() ) return Precision::Intersection();
424 Standard_Real tol = RealFirst();
425 for (; e.More(); e.Next())
426 tol = Max(tol,TopOpeBRepTool_ShapeTool::Tolerance(e.Current()));