1 // Created on: 1995-03-22
2 // Created by: Laurent BUCHARD
3 // Copyright (c) 1995-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_Curve.hxx>
20 #include <BRepAdaptor_Curve2d.hxx>
21 #include <BRepAdaptor_HSurface.hxx>
22 #include <BRepClass_FaceClassifier.hxx>
23 #include <BRepTools_WireExplorer.hxx>
24 #include <CSLib_Class2d.hxx>
26 #include <Geom2dInt_Geom2dCurveTool.hxx>
27 #include <GeomAbs_SurfaceType.hxx>
28 #include <GeomInt.hxx>
30 #include <gp_Pnt2d.hxx>
31 #include <IntTools_FClass2d.hxx>
32 #include <IntTools_Tools.hxx>
33 #include <Precision.hxx>
34 #include <TColgp_Array1OfPnt2d.hxx>
35 #include <TColgp_SequenceOfPnt2d.hxx>
36 #include <TColgp_SequenceOfVec2d.hxx>
37 #include <TColStd_DataMapOfIntegerInteger.hxx>
38 #include <TopAbs_Orientation.hxx>
40 #include <TopExp_Explorer.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Wire.hxx>
47 //=======================================================================
48 //function : IntTools_FClass2d:IntTools:_FClass2d
50 //=======================================================================
51 IntTools_FClass2d::IntTools_FClass2d()
54 //=======================================================================
55 //function : IntTools_FClass2d::IntTools_FClass2d
57 //=======================================================================
58 IntTools_FClass2d::IntTools_FClass2d(const TopoDS_Face& aFace,
59 const Standard_Real TolUV)
60 : Toluv(TolUV), Face(aFace)
64 //=======================================================================
67 //=======================================================================
68 Standard_Boolean IntTools_FClass2d::IsHole() const
72 //=======================================================================
75 //=======================================================================
76 void IntTools_FClass2d::Init(const TopoDS_Face& aFace,
77 const Standard_Real TolUV)
79 Standard_Boolean WireIsNotEmpty, Ancienpnt3dinitialise, degenerated;
80 Standard_Integer nbpnts, firstpoint, NbEdges;
81 Standard_Integer iX, aNbs1, nbs, Avant, BadWire;
82 Standard_Real u, du, Tole, Tol, pfbid, plbid;
83 Standard_Real FlecheU, FlecheV, TolVertex1, TolVertex;
84 Standard_Real uFirst, uLast;
85 Standard_Real aPrCf, aPrCf2;
89 TopAbs_Orientation Or;
90 BRepTools_WireExplorer aWExp;
91 TopExp_Explorer aExpF, aExp;
92 Handle(Geom2d_Curve) aC2D;
94 TColgp_SequenceOfPnt2d SeqPnt2d;
95 TColStd_DataMapOfIntegerInteger anIndexMap;
96 TColgp_SequenceOfVec2d aD1Prev;
97 TColgp_SequenceOfVec2d aD1Next;
99 aPrCf=Precision::Confusion();
101 myIsHole=Standard_True;
105 Face.Orientation(TopAbs_FORWARD);
106 Handle(BRepAdaptor_HSurface) surf = new BRepAdaptor_HSurface();
107 surf->ChangeSurface().Initialize(aFace, Standard_False);
111 Umin = Vmin = RealLast();
115 //if face has several wires and one of them is bad,
116 //it is necessary to process all of them for correct
117 //calculation of Umin, Umax, Vmin, Vmax - ifv, 23.08.06
119 aExpF.Init(Face,TopAbs_WIRE);
120 for(; aExpF.More(); aExpF.Next()) {
121 const TopoDS_Wire& aW=*((TopoDS_Wire*)&aExpF.Current());
129 WireIsNotEmpty = Standard_False;
130 Ancienpnt3dinitialise=Standard_False;
131 Ancienpnt3d.SetCoord(0.,0.,0.);
140 aExp.Init(aW, TopAbs_EDGE);
141 for(; aExp.More(); aExp.Next()) {
145 aWExp.Init(aW, Face);
146 for(;aWExp.More(); aWExp.Next()) {
148 edge = aWExp.Current();
149 Or = edge.Orientation();
150 if(!(Or==TopAbs_FORWARD || Or==TopAbs_REVERSED)) {
154 aC2D=BRep_Tool::CurveOnSurface(edge, Face, pfbid, plbid);
159 BRepAdaptor_Curve2d C(edge,Face);
160 BRepAdaptor_Curve C3d;
161 //------------------------------------------
162 degenerated=Standard_False;
163 if(BRep_Tool::Degenerated(edge) ||
164 BRep_Tool::IsClosed(edge, Face)) {
165 degenerated=Standard_True;
168 TopExp::Vertices(edge,Va,Vb);
173 degenerated=Standard_True;
176 TolVertex1=BRep_Tool::Tolerance(Va);
179 degenerated=Standard_True;
182 TolVertex=BRep_Tool::Tolerance(Vb);
185 if(TolVertex<TolVertex1) {
186 TolVertex=TolVertex1;
189 //-- Verification of cases when forgotten to code degenereted
191 // check that whole curve is located in vicinity of its middle point
192 // (within sphere of Precision::Confusion() diameter)
193 C3d.Initialize (edge, Face);
194 gp_Pnt P3da = C3d.Value (0.5 * (pfbid + plbid));
196 const int NBSTEPS = 10;
197 Standard_Real aPrec2 = 0.25 * Precision::Confusion() * Precision::Confusion();
198 degenerated = Standard_True;
199 for (Standard_Integer i=0; i <= NBSTEPS; i++)
201 Standard_Real u = pfbid + i * du / NBSTEPS;
202 gp_Pnt P3db = C3d.Value (u);
203 Standard_Real aR2 = P3da.SquareDistance (P3db);
205 degenerated = Standard_False;
210 //-- ----------------------------------------
211 Tole = BRep_Tool::Tolerance(edge);
217 nbs = Geom2dInt_Geom2dCurveTool::NbSamples(C);
221 du = (plbid-pfbid)/(Standard_Real)(nbs-1);
223 if(Or==TopAbs_FORWARD) {
237 TColStd_Array1OfReal aPrms(1, aNbs1);
240 Standard_Real aCoef=0.0025;
242 aPrms(2)=uFirst+aCoef*(uLast-uFirst);
248 for (iX=2; iX<aNbs1; ++iX) {
249 aPrms(iX)=u+(iX-1)*du;
254 //-- ------------------------------------------------------------
255 //-- Check distance uv between the start point of the edge
256 //-- and the last point saved in SeqPnt2d
257 //-- To to set the first point of the current
258 //-- afar from the last saved point
260 for(iX=firstpoint; iX<=aNbs1; iX++) {
261 Standard_Boolean IsRealCurve3d;
269 if(P2d.X()<Umin) Umin = P2d.X();
270 if(P2d.X()>Umax) Umax = P2d.X();
271 if(P2d.Y()<Vmin) Vmin = P2d.Y();
272 if(P2d.Y()>Vmax) Vmax = P2d.Y();
275 if(degenerated==Standard_False) {
278 if(Ancienpnt3dinitialise) {
279 aDstX=P3d.SquareDistance(Ancienpnt3d);
284 IsRealCurve3d = Standard_True;
285 if (aDstX < aPrCf2) {
287 Standard_Real aDstX1;
290 MidP3d = C3d.Value(0.5*(u+aPrms(iX-1)));
291 aDstX1=P3d.SquareDistance( MidP3d );
292 if (aDstX1 < aPrCf2){
293 IsRealCurve3d = Standard_False;
299 if(degenerated==Standard_False) {
301 Ancienpnt3dinitialise=Standard_True;
304 SeqPnt2d.Append(P2d);
309 Standard_Real ul, dU, dV;
312 gp_Lin2d Lin(SeqPnt2d(ii-2),gp_Dir2d(gp_Vec2d(SeqPnt2d(ii-2),SeqPnt2d(ii))));
313 ul = ElCLib::Parameter(Lin,SeqPnt2d(ii-1));
314 Pp = ElCLib::Value(ul,Lin);
315 dU = Abs(Pp.X()-SeqPnt2d(ii-1).X());
316 dV = Abs(Pp.Y()-SeqPnt2d(ii-1).Y());
324 }// for(iX=firstpoint; iX<=aNbs1; iX++) {
327 continue; //if face has several wires and one of them is bad,
328 //it is necessary to process all of them for correct
329 //calculation of Umin, Umax, Vmin, Vmax - ifv, 23.08.06
332 if(firstpoint==1) firstpoint=2;
333 WireIsNotEmpty = Standard_True;
334 // Append the derivative of the first parameter.
335 Standard_Real aU = aPrms(1);
341 if(Or == TopAbs_REVERSED)
346 // Append the derivative of the last parameter.
350 if(Or == TopAbs_REVERSED)
358 // Fill the map anIndexMap.
360 anIndexMap.Bind(Avant, aD1Next.Length());
362 anIndexMap.Bind(1, aD1Next.Length());
363 } //for(;aWExp.More(); aWExp.Next()) {
364 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
367 //-- count ++ with normal explorer and -- with Wire Explorer
368 TColgp_Array1OfPnt2d PClass(1,2);
369 gp_Pnt2d anInitPnt(0., 0.);
371 PClass.Init(anInitPnt);
372 TabClass.Append((void *)new CSLib_Class2d(PClass,
375 Umin,Vmin,Umax,Vmax));
380 else if(WireIsNotEmpty) {
381 TColgp_Array1OfPnt2d PClass(1,nbpnts);
382 gp_Pnt2d anInitPnt(0., 0.);
384 PClass.Init(anInitPnt);
386 Standard_Integer im2=nbpnts-2;
387 Standard_Integer im1=nbpnts-1;
388 Standard_Integer im0=1;
390 Standard_Real angle = 0.0;
391 Standard_Real aX0, aY0, aX1, aY1, aS;
395 Standard_Integer iFlag=1;
397 PClass(im2)=SeqPnt2d.Value(im2);
398 PClass(im1)=SeqPnt2d.Value(im1);
399 PClass(nbpnts)=SeqPnt2d.Value(nbpnts);
400 for(ii=1; ii<nbpnts; ii++,im0++,im1++,im2++) {
401 if(im2>=nbpnts) im2=1;
402 if(im1>=nbpnts) im1=1;
403 PClass(ii)=SeqPnt2d.Value(ii);
405 const gp_Pnt2d& aP2D1=PClass(im1);
406 const gp_Pnt2d& aP2D0=PClass(im0);
407 //aP2D0 is next to aP2D1
408 aP2D0.Coord(aX0, aY0);
409 aP2D1.Coord(aX1, aY1);
410 aS=aS+(aY0+aY1)*(aX1-aX0);
412 gp_Vec2d A(PClass(im2),PClass(im1));
413 gp_Vec2d B(PClass(im1),PClass(im0));
415 Standard_Real N = A.Magnitude() * B.Magnitude();
417 Standard_Real a=A.Angle(B);
419 if (anIndexMap.IsBound(im1)) {
420 Standard_Integer anInd = anIndexMap.Find(im1);
421 const gp_Vec2d &aVPrev = aD1Prev.Value(anInd);
422 const gp_Vec2d &aVNext = aD1Next.Value(anInd);
424 Standard_Real aN = aVPrev.Magnitude() * aVNext.Magnitude();
426 Standard_Real aDerivAngle, aAbsDA, aProduct, aPA;
428 aPA=Precision::Angular();
429 aDerivAngle = aVPrev.Angle(aVNext);
430 aAbsDA=Abs(aDerivAngle);
435 aProduct=aDerivAngle * a;
437 if(Abs(aAbsDA - M_PI) <= aPA) {
442 //ifv 23.08.06 : if edges continuity > G1, |aDerivAngle| ~0,
443 //but can has wrong sign and causes condition aDerivAngle * a < 0.
444 //that is wrong in such situation
445 if (iFlag && aProduct < 0.) {
454 }//for(ii=1; ii<nbpnts; ii++,im0++,im1++,im2++) {
459 myIsHole=Standard_False;
468 TabClass.Append((void *)new CSLib_Class2d(PClass,
471 Umin,Vmin,Umax,Vmax));
473 if((angle<2 && angle>-2)||(angle>10)||(angle<-10)) {
478 TabOrien.Append((angle>0.0)? 1 : 0);
484 TColgp_Array1OfPnt2d PPClass(1,2);
485 PPClass.Init(anInitPnt);
486 TabClass.Append((void *)new CSLib_Class2d(PPClass,
489 Umin,Vmin,Umax,Vmax));
491 }// else if(WireIsNotEmpty)
492 } // for(; aExpF.More(); aExpF.Next()) {
494 Standard_Integer nbtabclass = TabClass.Length();
497 //-- if an error on a wire was detected : all TabOrien set to -1
502 if( surf->GetType()==GeomAbs_Cone
503 || surf->GetType()==GeomAbs_Cylinder
504 || surf->GetType()==GeomAbs_Torus
505 || surf->GetType()==GeomAbs_Sphere
506 || surf->GetType()==GeomAbs_SurfaceOfRevolution) {
507 Standard_Real uuu=M_PI+M_PI-(Umax-Umin);
516 if(surf->GetType()==GeomAbs_Torus) {
517 Standard_Real uuu=M_PI+M_PI-(Vmax-Vmin);
528 //=======================================================================
529 //function : PerformInfinitePoint
531 //=======================================================================
532 TopAbs_State IntTools_FClass2d::PerformInfinitePoint() const
534 if(Umax==-RealLast() || Vmax==-RealLast() ||
535 Umin==RealLast() || Vmin==RealLast()) {
538 gp_Pnt2d P(Umin-(Umax-Umin),Vmin-(Vmax-Vmin));
539 return(Perform(P,Standard_False));
541 //=======================================================================
544 //=======================================================================
545 TopAbs_State IntTools_FClass2d::Perform
546 (const gp_Pnt2d& _Puv,
547 const Standard_Boolean RecadreOnPeriodic) const
549 Standard_Integer nbtabclass = TabClass.Length();
555 //-- U1 is the First Param and U2 is in this case U1+Period
556 Standard_Real u = _Puv.X();
557 Standard_Real v = _Puv.Y();
558 Standard_Real uu = u;
559 Standard_Real vv = v;
560 TopAbs_State Status = TopAbs_UNKNOWN;
562 Handle(BRepAdaptor_HSurface) surf = new BRepAdaptor_HSurface();
563 surf->ChangeSurface().Initialize( Face, Standard_False );
565 const Standard_Boolean IsUPer = surf->IsUPeriodic();
566 const Standard_Boolean IsVPer = surf->IsVPeriodic();
567 const Standard_Real uperiod = IsUPer ? surf->UPeriod() : 0.0;
568 const Standard_Real vperiod = IsVPer ? surf->VPeriod() : 0.0;
570 Standard_Boolean urecadre, vrecadre, bUseClassifier;
571 Standard_Integer dedans = 1;
573 urecadre = Standard_False;
574 vrecadre = Standard_False;
576 if (RecadreOnPeriodic) {
577 Standard_Real du, dv;
579 GeomInt::AdjustPeriodic(uu, Umin, Umax, uperiod, uu, du);
583 GeomInt::AdjustPeriodic(vv, Vmin, Vmax, vperiod, vv, dv);
590 bUseClassifier = (TabOrien(1) == -1);
591 if(!bUseClassifier) {
592 Standard_Integer n, cur, TabOrien_n ;
593 for(n=1; n<=nbtabclass; n++) {
594 cur = ((CSLib_Class2d *)TabClass(n))->SiDans(Puv);
595 TabOrien_n=TabOrien(n);
613 } // for(n=1; n<=nbtabclass; n++)
616 bUseClassifier = Standard_True;
619 Status = (dedans == 1) ? TopAbs_IN : TopAbs_OUT;
621 } // if(TabOrien(1)!=-1) {
622 //compute state of the point using face classifier
623 if (bUseClassifier) {
624 //compute tolerance to use in face classifier
625 Standard_Real aURes, aVRes, aFCTol;
626 Standard_Boolean bUIn, bVIn;
628 aURes = surf->UResolution(Toluv);
629 aVRes = surf->VResolution(Toluv);
631 bUIn = (u >= Umin) && (u <= Umax);
632 bVIn = (v >= Vmin) && (v <= Vmax);
635 aFCTol = Min(aURes, aVRes);
638 aFCTol = (!bUIn) ? aURes : aVRes;
641 BRepClass_FaceClassifier aClassifier;
642 aClassifier.Perform(Face,Puv,aFCTol);
643 Status = aClassifier.State();
646 if (!RecadreOnPeriodic || (!IsUPer && !IsVPer))
649 if (Status == TopAbs_IN || Status == TopAbs_ON)
654 urecadre = Standard_True;
662 if (u > Umax || !IsUPer) {
665 vrecadre = Standard_True;
675 if (v > Vmax || !IsVPer) {
682 //=======================================================================
683 //function : TestOnRestriction
685 //=======================================================================
686 TopAbs_State IntTools_FClass2d::TestOnRestriction
687 (const gp_Pnt2d& _Puv,
688 const Standard_Real Tol,
689 const Standard_Boolean RecadreOnPeriodic) const
691 Standard_Integer nbtabclass = TabClass.Length();
697 //-- U1 is the First Param and U2 in this case is U1+Period
698 Standard_Real u=_Puv.X();
699 Standard_Real v=_Puv.Y();
700 Standard_Real uu = u, vv = v;
702 Handle(BRepAdaptor_HSurface) surf = new BRepAdaptor_HSurface();
703 surf->ChangeSurface().Initialize( Face, Standard_False );
704 const Standard_Boolean IsUPer = surf->IsUPeriodic();
705 const Standard_Boolean IsVPer = surf->IsVPeriodic();
706 const Standard_Real uperiod = IsUPer ? surf->UPeriod() : 0.0;
707 const Standard_Real vperiod = IsVPer ? surf->VPeriod() : 0.0;
708 TopAbs_State Status = TopAbs_UNKNOWN;
709 Standard_Boolean urecadre = Standard_False, vrecadre = Standard_False;
710 Standard_Integer dedans = 1;
712 if (RecadreOnPeriodic) {
713 Standard_Real du, dv;
715 GeomInt::AdjustPeriodic(uu, Umin, Umax, uperiod, uu, du);
719 GeomInt::AdjustPeriodic(vv, Vmin, Vmax, vperiod, vv, dv);
727 if(TabOrien(1)!=-1) {
728 for(Standard_Integer n=1; n<=nbtabclass; n++) {
729 Standard_Integer cur = ((CSLib_Class2d *)TabClass(n))->SiDans_OnMode(Puv,Tol);
757 else { //-- TabOrien(1)=-1 Wrong Wire
758 BRepClass_FaceClassifier aClassifier;
759 aClassifier.Perform(Face,Puv,Tol);
760 Status = aClassifier.State();
763 if (!RecadreOnPeriodic || (!IsUPer && !IsVPer))
765 if (Status == TopAbs_IN || Status == TopAbs_ON)
771 urecadre = Standard_True;
776 if (u > Umax || !IsUPer)
781 vrecadre = Standard_True;
789 if (v > Vmax || !IsVPer)
794 //=======================================================================
797 //=======================================================================
798 void IntTools_FClass2d::Destroy()
800 Standard_Integer nbtabclass = TabClass.Length();
801 for(Standard_Integer d=1; d<=nbtabclass;d++) {
803 delete ((CSLib_Class2d *)TabClass(d));