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
9 // under the terms of the GNU Lesser General Public 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.
17 #include <IntTools_FClass2d.ixx>
21 #include <Precision.hxx>
25 #include <Geom2dInt_Geom2dCurveTool.hxx>
26 #include <GeomAbs_SurfaceType.hxx>
28 #include <BRep_Tool.hxx>
29 #include <BRepTools_WireExplorer.hxx>
30 #include <BRepClass_FaceClassifier.hxx>
31 #include <CSLib_Class2d.hxx>
32 #include <BRepAdaptor_HSurface.hxx>
33 #include <BRepAdaptor_Curve.hxx>
34 #include <BRepAdaptor_Curve2d.hxx>
37 #include <TopoDS_Edge.hxx>
39 #include <TopAbs_Orientation.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TColgp_SequenceOfPnt2d.hxx>
43 #include <TColgp_Array1OfPnt2d.hxx>
44 #include <TopoDS_Wire.hxx>
45 #include <TColStd_DataMapOfIntegerInteger.hxx>
46 #include <TColgp_SequenceOfVec2d.hxx>
48 //=======================================================================
49 //function : IntTools_FClass2d:IntTools:_FClass2d
51 //=======================================================================
52 IntTools_FClass2d::IntTools_FClass2d()
55 //=======================================================================
56 //function : IntTools_FClass2d::IntTools_FClass2d
58 //=======================================================================
59 IntTools_FClass2d::IntTools_FClass2d(const TopoDS_Face& aFace,
60 const Standard_Real TolUV)
61 : Toluv(TolUV), Face(aFace)
65 //=======================================================================
68 //=======================================================================
69 Standard_Boolean IntTools_FClass2d::IsHole() const
73 //=======================================================================
76 //=======================================================================
77 void IntTools_FClass2d::Init(const TopoDS_Face& aFace,
78 const Standard_Real TolUV)
80 Standard_Boolean WireIsNotEmpty, Ancienpnt3dinitialise, degenerated;
81 Standard_Integer nbpnts, firstpoint, NbEdges;
82 Standard_Integer iX, aNbs1, nbs, Avant, BadWire;
83 Standard_Real u, du, Tole, Tol, pfbid, plbid;
84 Standard_Real FlecheU, FlecheV, TolVertex1, TolVertex;
85 Standard_Real uFirst, uLast;
86 Standard_Real aPrCf, aPrCf2;
90 TopAbs_Orientation Or;
91 BRepTools_WireExplorer aWExp;
92 TopExp_Explorer aExpF, aExp;
93 Handle(Geom2d_Curve) aC2D;
95 TColgp_SequenceOfPnt2d SeqPnt2d;
96 TColStd_DataMapOfIntegerInteger anIndexMap;
97 TColgp_SequenceOfVec2d aD1Prev;
98 TColgp_SequenceOfVec2d aD1Next;
100 aPrCf=Precision::Confusion();
102 myIsHole=Standard_True;
106 Face.Orientation(TopAbs_FORWARD);
107 Handle(BRepAdaptor_HSurface) surf = new BRepAdaptor_HSurface();
108 surf->ChangeSurface().Initialize(aFace, Standard_False);
112 Umin = Vmin = RealLast();
116 //if face has several wires and one of them is bad,
117 //it is necessary to process all of them for correct
118 //calculation of Umin, Umax, Vmin, Vmax - ifv, 23.08.06
120 aExpF.Init(Face,TopAbs_WIRE);
121 for(; aExpF.More(); aExpF.Next()) {
122 const TopoDS_Wire& aW=*((TopoDS_Wire*)&aExpF.Current());
130 WireIsNotEmpty = Standard_False;
131 Ancienpnt3dinitialise=Standard_False;
132 Ancienpnt3d.SetCoord(0.,0.,0.);
141 aExp.Init(aW, TopAbs_EDGE);
142 for(; aExp.More(); aExp.Next()) {
146 aWExp.Init(aW, Face);
147 for(;aWExp.More(); aWExp.Next()) {
149 edge = aWExp.Current();
150 Or = edge.Orientation();
151 if(!(Or==TopAbs_FORWARD || Or==TopAbs_REVERSED)) {
155 aC2D=BRep_Tool::CurveOnSurface(edge, Face, pfbid, plbid);
160 BRepAdaptor_Curve2d C(edge,Face);
161 BRepAdaptor_Curve C3d;
162 //------------------------------------------
163 degenerated=Standard_False;
164 if(BRep_Tool::Degenerated(edge) ||
165 BRep_Tool::IsClosed(edge, Face)) {
166 degenerated=Standard_True;
169 TopExp::Vertices(edge,Va,Vb);
174 degenerated=Standard_True;
177 TolVertex1=BRep_Tool::Tolerance(Va);
180 degenerated=Standard_True;
183 TolVertex=BRep_Tool::Tolerance(Vb);
186 if(TolVertex<TolVertex1) {
187 TolVertex=TolVertex1;
190 //-- Verification of cases when forgotten to code degenereted
192 // check that whole curve is located in vicinity of its middle point
193 // (within sphere of Precision::Confusion() diameter)
194 C3d.Initialize (edge, Face);
195 gp_Pnt P3da = C3d.Value (0.5 * (pfbid + plbid));
197 const int NBSTEPS = 10;
198 Standard_Real aPrec2 = 0.25 * Precision::Confusion() * Precision::Confusion();
199 degenerated = Standard_True;
200 for (Standard_Integer i=0; i <= NBSTEPS; i++)
202 Standard_Real u = pfbid + i * du / NBSTEPS;
203 gp_Pnt P3db = C3d.Value (u);
204 Standard_Real aR2 = P3da.SquareDistance (P3db);
206 degenerated = Standard_False;
211 //-- ----------------------------------------
212 Tole = BRep_Tool::Tolerance(edge);
218 nbs = Geom2dInt_Geom2dCurveTool::NbSamples(C);
222 du = (plbid-pfbid)/(Standard_Real)(nbs-1);
224 if(Or==TopAbs_FORWARD) {
238 TColStd_Array1OfReal aPrms(1, aNbs1);
241 Standard_Real aCoef=0.0025;
243 aPrms(2)=uFirst+aCoef*(uLast-uFirst);
249 for (iX=2; iX<aNbs1; ++iX) {
250 aPrms(iX)=u+(iX-1)*du;
255 //-- ------------------------------------------------------------
256 //-- Check distance uv between the start point of the edge
257 //-- and the last point saved in SeqPnt2d
258 //-- To to set the first point of the current
259 //-- afar from the last saved point
261 for(iX=firstpoint; iX<=aNbs1; iX++) {
262 Standard_Boolean IsRealCurve3d;
270 if(P2d.X()<Umin) Umin = P2d.X();
271 if(P2d.X()>Umax) Umax = P2d.X();
272 if(P2d.Y()<Vmin) Vmin = P2d.Y();
273 if(P2d.Y()>Vmax) Vmax = P2d.Y();
276 if(degenerated==Standard_False) {
279 if(Ancienpnt3dinitialise) {
280 aDstX=P3d.SquareDistance(Ancienpnt3d);
285 IsRealCurve3d = Standard_True;
286 if (aDstX < aPrCf2) {
288 Standard_Real aDstX1;
291 MidP3d = C3d.Value(0.5*(u+aPrms(iX-1)));
292 aDstX1=P3d.SquareDistance( MidP3d );
293 if (aDstX1 < aPrCf2){
294 IsRealCurve3d = Standard_False;
300 if(degenerated==Standard_False) {
302 Ancienpnt3dinitialise=Standard_True;
305 SeqPnt2d.Append(P2d);
310 Standard_Real ul, dU, dV;
313 gp_Lin2d Lin(SeqPnt2d(ii-2),gp_Dir2d(gp_Vec2d(SeqPnt2d(ii-2),SeqPnt2d(ii))));
314 ul = ElCLib::Parameter(Lin,SeqPnt2d(ii-1));
315 Pp = ElCLib::Value(ul,Lin);
316 dU = Abs(Pp.X()-SeqPnt2d(ii-1).X());
317 dV = Abs(Pp.Y()-SeqPnt2d(ii-1).Y());
325 }// for(iX=firstpoint; iX<=aNbs1; iX++) {
328 continue; //if face has several wires and one of them is bad,
329 //it is necessary to process all of them for correct
330 //calculation of Umin, Umax, Vmin, Vmax - ifv, 23.08.06
333 if(firstpoint==1) firstpoint=2;
334 WireIsNotEmpty = Standard_True;
335 // Append the derivative of the first parameter.
336 Standard_Real aU = aPrms(1);
342 if(Or == TopAbs_REVERSED)
347 // Append the derivative of the last parameter.
351 if(Or == TopAbs_REVERSED)
359 // Fill the map anIndexMap.
361 anIndexMap.Bind(Avant, aD1Next.Length());
363 anIndexMap.Bind(1, aD1Next.Length());
364 } //for(;aWExp.More(); aWExp.Next()) {
365 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
368 //-- count ++ with normal explorer and -- with Wire Explorer
369 TColgp_Array1OfPnt2d PClass(1,2);
370 gp_Pnt2d anInitPnt(0., 0.);
372 PClass.Init(anInitPnt);
373 TabClass.Append((void *)new CSLib_Class2d(PClass,FlecheU,FlecheV,Umin,Vmin,Umax,Vmax));
378 else if(WireIsNotEmpty) {
379 TColgp_Array1OfPnt2d PClass(1,nbpnts);
380 gp_Pnt2d anInitPnt(0., 0.);
382 PClass.Init(anInitPnt);
384 Standard_Integer im2=nbpnts-2;
385 Standard_Integer im1=nbpnts-1;
386 Standard_Integer im0=1;
388 Standard_Real angle = 0.0;
389 Standard_Real aX0, aY0, aX1, aY1, aS;
394 Standard_Integer iFlag=1;
395 PClass(im2)=SeqPnt2d.Value(im2);
396 PClass(im1)=SeqPnt2d.Value(im1);
397 PClass(nbpnts)=SeqPnt2d.Value(nbpnts);
398 for(ii=1; ii<nbpnts; ii++,im0++,im1++,im2++) {
399 if(im2>=nbpnts) im2=1;
400 if(im1>=nbpnts) im1=1;
401 PClass(ii)=SeqPnt2d.Value(ii);
403 const gp_Pnt2d& aP2D1=PClass(im1);
404 const gp_Pnt2d& aP2D0=PClass(im0);
405 //aP2D0 is next to aP2D1
406 aP2D0.Coord(aX0, aY0);
407 aP2D1.Coord(aX1, aY1);
408 aS=aS+(aY0+aY1)*(aX1-aX0);
410 gp_Vec2d A(PClass(im2),PClass(im1));
411 gp_Vec2d B(PClass(im1),PClass(im0));
413 Standard_Real N = A.Magnitude() * B.Magnitude();
415 Standard_Real a=A.Angle(B);
417 if (anIndexMap.IsBound(im1)) {
418 Standard_Integer anInd = anIndexMap.Find(im1);
419 const gp_Vec2d &aVPrev = aD1Prev.Value(anInd);
420 const gp_Vec2d &aVNext = aD1Next.Value(anInd);
422 Standard_Real aN = aVPrev.Magnitude() * aVNext.Magnitude();
424 Standard_Real aDerivAngle = aVPrev.Angle(aVNext);
426 if(Abs(aDerivAngle) <= Precision::Angular()) aDerivAngle = 0.;
427 //ifv 23.08.06 : if edges continuity > G1, |aDerivAngle| ~0,
428 //but can has wrong sign and causes condition aDerivAngle * a < 0.
429 //that is wrong in such situation
430 if (iFlag && aDerivAngle * a < 0.) {
439 }//for(ii=1; ii<nbpnts; ii++,im0++,im1++,im2++) {
444 myIsHole=Standard_False;
453 TabClass.Append((void *)new CSLib_Class2d(PClass,FlecheU,FlecheV,Umin,Vmin,Umax,Vmax));
455 if((angle<2 && angle>-2)||(angle>10)||(angle<-10)) {
460 TabOrien.Append((angle>0.0)? 1 : 0);
466 TColgp_Array1OfPnt2d PPClass(1,2);
467 PPClass.Init(anInitPnt);
468 TabClass.Append((void *)new CSLib_Class2d(PPClass,FlecheU,FlecheV,Umin,Vmin,Umax,Vmax));
470 }// else if(WireIsNotEmpty)
471 } // for(; aExpF.More(); aExpF.Next()) {
473 Standard_Integer nbtabclass = TabClass.Length();
476 //-- if an error on a wire was detected : all TabOrien set to -1
481 if( surf->GetType()==GeomAbs_Cone
482 || surf->GetType()==GeomAbs_Cylinder
483 || surf->GetType()==GeomAbs_Torus
484 || surf->GetType()==GeomAbs_Sphere
485 || surf->GetType()==GeomAbs_SurfaceOfRevolution) {
486 Standard_Real uuu=M_PI+M_PI-(Umax-Umin);
495 if(surf->GetType()==GeomAbs_Torus) {
496 Standard_Real uuu=M_PI+M_PI-(Vmax-Vmin);
507 //=======================================================================
508 //function : PerformInfinitePoint
510 //=======================================================================
511 TopAbs_State IntTools_FClass2d::PerformInfinitePoint() const
513 if(Umax==-RealLast() || Vmax==-RealLast() || Umin==RealLast() || Vmin==RealLast()) {
516 gp_Pnt2d P(Umin-(Umax-Umin),Vmin-(Vmax-Vmin));
517 return(Perform(P,Standard_False));
519 //=======================================================================
522 //=======================================================================
523 TopAbs_State IntTools_FClass2d::Perform(const gp_Pnt2d& _Puv,
524 const Standard_Boolean RecadreOnPeriodic) const
526 Standard_Integer nbtabclass = TabClass.Length();
532 //-- U1 is the First Param and U2 is in this case U1+Period
533 Standard_Real u = _Puv.X();
534 Standard_Real v = _Puv.Y();
535 Standard_Real uu = u;
536 Standard_Real vv = v;
537 TopAbs_State Status = TopAbs_UNKNOWN;
539 Handle(BRepAdaptor_HSurface) surf = new BRepAdaptor_HSurface();
540 surf->ChangeSurface().Initialize( Face, Standard_False );
542 const Standard_Boolean IsUPer = surf->IsUPeriodic();
543 const Standard_Boolean IsVPer = surf->IsVPeriodic();
544 const Standard_Real uperiod = IsUPer ? surf->UPeriod() : 0.0;
545 const Standard_Real vperiod = IsVPer ? surf->VPeriod() : 0.0;
547 Standard_Boolean urecadre, vrecadre, bUseClassifier;
548 Standard_Integer dedans = 1;
550 urecadre = Standard_False;
551 vrecadre = Standard_False;
553 if (RecadreOnPeriodic) {
585 bUseClassifier = (TabOrien(1) == -1);
586 if(!bUseClassifier) {
587 Standard_Integer n, cur, TabOrien_n ;
588 for(n=1; n<=nbtabclass; n++) {
589 cur = ((CSLib_Class2d *)TabClass(n))->SiDans(Puv);
590 TabOrien_n=TabOrien(n);
608 } // for(n=1; n<=nbtabclass; n++)
611 bUseClassifier = Standard_True;
614 Status = (dedans == 1) ? TopAbs_IN : TopAbs_OUT;
616 } // if(TabOrien(1)!=-1) {
617 //compute state of the point using face classifier
618 if (bUseClassifier) {
619 //compute tolerance to use in face classifier
620 Standard_Real aURes, aVRes, aFCTol;
621 Standard_Boolean bUIn, bVIn;
623 aURes = surf->UResolution(Toluv);
624 aVRes = surf->VResolution(Toluv);
626 bUIn = (u >= Umin) && (u <= Umax);
627 bVIn = (v >= Vmin) && (v <= Vmax);
629 aFCTol = (bUIn==bVIn) ? Max(aURes, aVRes) :
630 (!bUIn ? aURes : aVRes);
632 BRepClass_FaceClassifier aClassifier;
633 aClassifier.Perform(Face,Puv,aFCTol);
634 Status = aClassifier.State();
637 if (!RecadreOnPeriodic || (!IsUPer && !IsVPer))
640 if (Status == TopAbs_IN || Status == TopAbs_ON)
645 urecadre = Standard_True;
653 if (u > Umax || !IsUPer) {
656 vrecadre = Standard_True;
666 if (v > Vmax || !IsVPer) {
673 //=======================================================================
674 //function : TestOnRestriction
676 //=======================================================================
677 TopAbs_State IntTools_FClass2d::TestOnRestriction(const gp_Pnt2d& _Puv,
678 const Standard_Real Tol,
679 const Standard_Boolean RecadreOnPeriodic) const
681 Standard_Integer nbtabclass = TabClass.Length();
687 //-- U1 is the First Param and U2 in this case is U1+Period
688 Standard_Real u=_Puv.X();
689 Standard_Real v=_Puv.Y();
690 Standard_Real uu = u, vv = v;
692 Handle(BRepAdaptor_HSurface) surf = new BRepAdaptor_HSurface();
693 surf->ChangeSurface().Initialize( Face, Standard_False );
694 const Standard_Boolean IsUPer = surf->IsUPeriodic();
695 const Standard_Boolean IsVPer = surf->IsVPeriodic();
696 const Standard_Real uperiod = IsUPer ? surf->UPeriod() : 0.0;
697 const Standard_Real vperiod = IsVPer ? surf->VPeriod() : 0.0;
698 TopAbs_State Status = TopAbs_UNKNOWN;
699 Standard_Boolean urecadre = Standard_False, vrecadre = Standard_False;
700 Standard_Integer dedans = 1;
702 if (RecadreOnPeriodic)
734 if(TabOrien(1)!=-1) {
735 for(Standard_Integer n=1; n<=nbtabclass; n++) {
736 Standard_Integer cur = ((CSLib_Class2d *)TabClass(n))->SiDans_OnMode(Puv,Tol);
764 else { //-- TabOrien(1)=-1 Wrong Wire
765 BRepClass_FaceClassifier aClassifier;
766 aClassifier.Perform(Face,Puv,Tol);
767 Status = aClassifier.State();
770 if (!RecadreOnPeriodic || (!IsUPer && !IsVPer))
772 if (Status == TopAbs_IN || Status == TopAbs_ON)
778 urecadre = Standard_True;
783 if (u > Umax || !IsUPer)
788 vrecadre = Standard_True;
796 if (v > Vmax || !IsVPer)
802 //=======================================================================
805 //=======================================================================
806 void IntTools_FClass2d::Destroy()
808 Standard_Integer nbtabclass = TabClass.Length();
809 for(Standard_Integer d=1; d<=nbtabclass;d++) {
811 delete ((CSLib_Class2d *)TabClass(d));