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.
19 #define No_Standard_OutOfRange
22 #include <BRep_Tool.hxx>
23 #include <BRepAdaptor_Curve.hxx>
24 #include <BRepAdaptor_Curve2d.hxx>
25 #include <BRepAdaptor_HSurface.hxx>
26 #include <BRepClass_FaceClassifier.hxx>
27 #include <BRepTools_WireExplorer.hxx>
28 #include <BRepTopAdaptor_FClass2d.hxx>
29 #include <CSLib_Class2d.hxx>
31 #include <Geom2dInt_Geom2dCurveTool.hxx>
32 #include <GeomAbs_SurfaceType.hxx>
34 #include <gp_Pnt2d.hxx>
35 #include <Precision.hxx>
36 #include <TColgp_Array1OfPnt2d.hxx>
37 #include <TColgp_SequenceOfPnt2d.hxx>
38 #include <TopAbs_Orientation.hxx>
40 #include <TopExp_Explorer.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Face.hxx>
58 class StatistiquesFClass2d {
60 long unsigned NbConstrShape;
61 long unsigned NbPerformInfinitePoint;
62 long unsigned NbPerform;
63 long unsigned NbTestOnRestriction;
64 long unsigned NbDestroy;
66 StatistiquesFClass2d() {
67 NbConstrShape=NbPerform=NbPerformInfinitePoint=NbDestroy=0;
69 ~StatistiquesFClass2d() {
70 printf("\n--- Statistiques BRepTopAdaptor:\n");
71 printf("\nConstructeur(Shape) : %10lu",NbConstrShape);
72 printf("\nPerformInfinitePoint: %10lu",NbPerformInfinitePoint);
73 printf("\nTestOnRestriction : %10lu",NbTestOnRestriction);
74 printf("\nPerform(pnt2d) : %10lu",NbPerform);
75 printf("\nDestroy : %10lu",NbDestroy);
79 static StatistiquesFClass2d STAT;
85 BRepTopAdaptor_FClass2d::BRepTopAdaptor_FClass2d(const TopoDS_Face& aFace,const Standard_Real TolUV)
86 : Toluv(TolUV), Face(aFace) {
92 //-- dead end on surfaces defined on more than one period
94 Face.Orientation(TopAbs_FORWARD);
95 Handle(BRepAdaptor_HSurface) surf = new BRepAdaptor_HSurface();
96 surf->ChangeSurface().Initialize(aFace,Standard_False);
99 TopAbs_Orientation Or;
100 Standard_Real u,du,Tole = 0.0,Tol=0.0;
101 BRepTools_WireExplorer WireExplorer;
102 TopExp_Explorer FaceExplorer;
104 Umin = Vmin = 0.0; //RealLast();
107 Standard_Integer BadWire=0;
108 for( FaceExplorer.Init(Face,TopAbs_WIRE); (FaceExplorer.More() && BadWire==0); FaceExplorer.Next() )
110 Standard_Integer nbpnts = 0;
111 TColgp_SequenceOfPnt2d SeqPnt2d;
112 Standard_Integer firstpoint = 1;
113 Standard_Real FlecheU = 0.0;
114 Standard_Real FlecheV = 0.0;
115 Standard_Boolean WireIsNotEmpty = Standard_False;
116 Standard_Integer NbEdges = 0;
118 TopExp_Explorer Explorer;
119 for( Explorer.Init(FaceExplorer.Current(),TopAbs_EDGE); Explorer.More(); Explorer.Next() ) NbEdges++;
121 gp_Pnt Ancienpnt3d(0,0,0);
122 Standard_Boolean Ancienpnt3dinitialise = Standard_False;
124 for( WireExplorer.Init(TopoDS::Wire(FaceExplorer.Current()),Face); WireExplorer.More(); WireExplorer.Next() )
128 edge = WireExplorer.Current();
129 Or = edge.Orientation();
130 if(Or == TopAbs_FORWARD || Or == TopAbs_REVERSED)
132 Standard_Real pfbid,plbid;
133 if(BRep_Tool::CurveOnSurface(edge,Face,pfbid,plbid).IsNull()) return;
134 BRepAdaptor_Curve2d C(edge,Face);
136 //-- ----------------------------------------
137 Standard_Boolean degenerated=Standard_False;
138 if(BRep_Tool::Degenerated(edge)) degenerated=Standard_True;
139 if(BRep_Tool::IsClosed(edge,Face)) degenerated=Standard_True;
141 TopExp::Vertices(edge,Va,Vb);
142 Standard_Real TolVertex1=0.,TolVertex=0.;
143 if (Va.IsNull()) degenerated=Standard_True;
144 else TolVertex1=BRep_Tool::Tolerance(Va);
145 if (Vb.IsNull()) degenerated=Standard_True;
146 else TolVertex=BRep_Tool::Tolerance(Vb);
147 if(TolVertex<TolVertex1) TolVertex=TolVertex1;
148 BRepAdaptor_Curve C3d;
150 if(Abs(plbid-pfbid) < 1.e-9) continue;
152 //if(degenerated==Standard_False)
153 // C3d.Initialize(edge,Face);
155 //-- Check cases when it was forgotten to code degenerated : PRO17410 (janv 99)
156 if(degenerated == Standard_False)
158 C3d.Initialize(edge,Face);
159 du=(plbid-pfbid)*0.1;
161 gp_Pnt P3da=C3d.Value(u);
162 degenerated=Standard_True;
167 gp_Pnt P3db=C3d.Value(u);
168 // if(P3da.SquareDistance(P3db)) { degenerated=Standard_False; break; }
169 if(P3da.SquareDistance(P3db) > Precision::Confusion()) { degenerated=Standard_False; break; }
175 //-- ----------------------------------------
177 Tole = BRep_Tool::Tolerance(edge);
178 if(Tole>Tol) Tol=Tole;
180 //Standard_Integer nbs = 1 + Geom2dInt_Geom2dCurveTool::NbSamples(C);
181 Standard_Integer nbs = Geom2dInt_Geom2dCurveTool::NbSamples(C);
182 //-- Attention to rational bsplines of degree 3. (ends of circles among others)
184 du = (plbid-pfbid)/(Standard_Real)(nbs-1);
186 if(Or==TopAbs_FORWARD) u = pfbid;
187 else { u = plbid; du=-du; }
189 //-- ------------------------------------------------------------
190 //-- Check distance uv between the start point of the edge
191 //-- and the last point registered in SeqPnt2d
192 //-- Try to remote the first point of the current edge
193 //-- from the last saved point
195 gp_Pnt2d Pnt2dDebutEdgeCourant = C.Value(u); (void)Pnt2dDebutEdgeCourant;
198 //Standard_Real Baillement2dU=0;
199 //Standard_Real Baillement2dV=0;
201 if(nbpnts>1) printf("\nTolVertex %g ",TolVertex);
204 if(firstpoint==2) u+=du;
205 Standard_Integer Avant = nbpnts;
206 for(Standard_Integer e = firstpoint; e<=nbs; e++)
208 gp_Pnt2d P2d = C.Value(u);
209 if(P2d.X()<Umin) Umin = P2d.X();
210 if(P2d.X()>Umax) Umax = P2d.X();
211 if(P2d.Y()<Vmin) Vmin = P2d.Y();
212 if(P2d.Y()>Vmax) Vmax = P2d.Y();
214 Standard_Real dist3dptcourant_ancienpnt=1e+20;//RealLast();
216 if(degenerated==Standard_False)
219 if(nbpnts>1 && Ancienpnt3dinitialise) dist3dptcourant_ancienpnt = P3d.Distance(Ancienpnt3d);
221 Standard_Boolean IsRealCurve3d = Standard_True; //patch
222 if(dist3dptcourant_ancienpnt < Precision::Confusion())
224 gp_Pnt MidP3d = C3d.Value( u-du/2. );
225 if (P3d.Distance( MidP3d ) < Precision::Confusion()) IsRealCurve3d = Standard_False;
229 if(degenerated==Standard_False) { Ancienpnt3d=P3d; Ancienpnt3dinitialise=Standard_True; }
231 SeqPnt2d.Append(P2d);
234 else { static int mm=0; printf("\npoint p%d %g %g %g",++mm,P3d.X(),P3d.Y(),P3d.Z()); }
237 Standard_Integer ii = nbpnts;
238 //-- printf("\n nbpnts:%4d u=%7.5g FlecheU=%7.5g FlecheV=%7.5g ii=%3d Avant=%3d ",nbpnts,u,FlecheU,FlecheV,ii,Avant);
240 // Modified by Sergey KHROMOV - Fri Apr 19 09:46:12 2002 Begin
241 if(ii>(Avant+4) && SeqPnt2d(ii-2).SquareDistance(SeqPnt2d(ii)))
242 // Modified by Sergey KHROMOV - Fri Apr 19 09:46:13 2002 End
244 gp_Lin2d Lin(SeqPnt2d(ii-2),gp_Dir2d(gp_Vec2d(SeqPnt2d(ii-2),SeqPnt2d(ii))));
245 Standard_Real ul = ElCLib::Parameter(Lin,SeqPnt2d(ii-1));
246 gp_Pnt2d Pp = ElCLib::Value(ul,Lin);
247 Standard_Real dU = Abs(Pp.X()-SeqPnt2d(ii-1).X());
248 Standard_Real dV = Abs(Pp.Y()-SeqPnt2d(ii-1).Y());
249 //-- printf(" (du=%7.5g dv=%7.5g)",dU,dV);
250 if(dU>FlecheU) FlecheU = dU;
251 if(dV>FlecheV) FlecheV = dV;
254 if(firstpoint==1) firstpoint=2;
255 WireIsNotEmpty = Standard_True;
256 }//if(Or==FORWARD,REVERSED
257 } //-- Edges -> for(Ware.Explorer
260 { //-- on compte ++ with a normal explorer and with the Wire Explorer
265 cout << "*** BRepTopAdaptor_Fclass2d ** Wire Probablement FAUX **" << endl;
266 cout << "*** WireExplorer does not find all edges " << endl;
267 cout << "*** Connect old classifier" << endl;
270 TColgp_Array1OfPnt2d PClass(1,2);
271 //// modified by jgv, 28.04.2009 ////
272 PClass.Init(gp_Pnt2d(0.,0.));
273 /////////////////////////////////////
274 TabClass.Append((void *)new CSLib_Class2d(PClass,FlecheU,FlecheV,Umin,Vmin,Umax,Vmax));
278 else if(WireIsNotEmpty)
280 //Standard_Real anglep=0,anglem=0;
281 TColgp_Array1OfPnt2d PClass(1,nbpnts);
282 Standard_Real square = 0.0;
284 //-------------------------------------------------------------------
285 //-- ** The mode of calculation was somewhat changed
286 //-- Before Oct 31 97 , the total angle of
287 //-- rotation of the wire was evaluated on all angles except for the last
288 //-- ** Now, exactly the angle of rotation is evaluated
289 //-- If a value remote from 2PI or -2PI is found, it means that there is
290 //-- an uneven number of loops
294 // Standard_Integer im2=nbpnts-2;
295 Standard_Integer im1=nbpnts-1;
296 Standard_Integer im0=1;
297 // PClass(im2)=SeqPnt2d.Value(im2);
298 PClass(im1)=SeqPnt2d.Value(im1);
299 PClass(nbpnts)=SeqPnt2d.Value(nbpnts);
302 // for(Standard_Integer ii=1; ii<nbpnts; ii++,im0++,im1++,im2++)
303 for(Standard_Integer ii=1; ii<nbpnts; ii++,im0++,im1++)
305 // if(im2>=nbpnts) im2=1;
306 if(im1>=nbpnts) im1=1;
307 PClass(ii)=SeqPnt2d.Value(ii);
308 // gp_Vec2d A(PClass(im2),PClass(im1));
309 // gp_Vec2d B(PClass(im1),PClass(im0));
310 // Standard_Real N = A.Magnitude() * B.Magnitude();
312 square += (PClass(im0).X()-PClass(im1).X())*(PClass(im0).Y()+PClass(im1).Y())*.5;
314 // if(N>1e-16){ Standard_Real a=A.Angle(B); angle+=a; }
320 if(FlecheU<Toluv) FlecheU = Toluv;
321 if(FlecheV<Toluv) FlecheV = Toluv;
322 //-- cout<<" U:"<<FlecheU<<" V:"<<FlecheV<<endl;
323 TabClass.Append((void *)new CSLib_Class2d(PClass,FlecheU,FlecheV,Umin,Vmin,Umax,Vmax));
325 // if((angle<2 && angle>-2)||(angle>10)||(angle<-10))
328 // TabOrien.Append(-1);
331 // cout << "*** BRepTopAdaptor_Fclass2d ** Wire Probably FALSE **" << endl;
332 // cout << "*** Total rotation angle of the wire : " << angle << endl;
333 // cout << "*** Connect the old classifier" << endl;
336 // else TabOrien.Append(((angle>0.0)? 1 : 0));
337 TabOrien.Append(((square < 0.0)? 1 : 0));
343 cout << "*** BRepTopAdaptor_Fclass2d ** Wire Probably FALSE **" << endl;
344 cout << "*** The sample wire contains less than 3 points" << endl;
345 cout << "*** Connect the old classifier" << endl;
349 TColgp_Array1OfPnt2d xPClass(1,2);
350 xPClass(1) = SeqPnt2d(1);
351 xPClass(2) = SeqPnt2d(2);
352 TabClass.Append((void *)new CSLib_Class2d(xPClass,FlecheU,FlecheV,Umin,Vmin,Umax,Vmax));
354 }//else if(WareIsNotEmpty
357 Standard_Integer nbtabclass = TabClass.Length();
361 //-- If an error was detected on a wire: set all TabOrien to -1
362 if(BadWire) TabOrien(1)=-1;
364 if( surf->GetType()==GeomAbs_Cone
365 || surf->GetType()==GeomAbs_Cylinder
366 || surf->GetType()==GeomAbs_Torus
367 || surf->GetType()==GeomAbs_Sphere
368 || surf->GetType()==GeomAbs_SurfaceOfRevolution)
371 Standard_Real uuu=M_PI+M_PI-(Umax-Umin);
373 U1 = 0.0; // modified by NIZHNY-OFV Thu May 31 14:24:10 2001 ---> //Umin-uuu*0.5;
374 U2 = 2*M_PI; // modified by NIZHNY-OFV Thu May 31 14:24:35 2001 ---> //U1+M_PI+M_PI;
378 if(surf->GetType()==GeomAbs_Torus)
380 Standard_Real uuu=M_PI+M_PI-(Vmax-Vmin);
382 V1 = 0.0; // modified by NIZHNY-OFV Thu May 31 14:24:55 2001 ---> //Vmin-uuu*0.5;
383 V2 = 2*M_PI; // modified by NIZHNY-OFV Thu May 31 14:24:59 2001 ---> //V1+M_PI+M_PI;
389 TopAbs_State BRepTopAdaptor_FClass2d::PerformInfinitePoint() const {
391 STAT.NbPerformInfinitePoint++;
394 if(Umax==-RealLast() || Vmax==-RealLast() || Umin==RealLast() || Vmin==RealLast()) {
397 gp_Pnt2d P(Umin-(Umax-Umin),Vmin-(Vmax-Vmin));
398 return(Perform(P,Standard_False));
401 TopAbs_State BRepTopAdaptor_FClass2d::Perform(const gp_Pnt2d& _Puv,
402 const Standard_Boolean RecadreOnPeriodic) const
408 Standard_Integer dedans;
409 Standard_Integer nbtabclass = TabClass.Length();
415 //-- U1 is the First Param and U2 in this case is U1+Period
416 Standard_Real u=_Puv.X();
417 Standard_Real v=_Puv.Y();
418 Standard_Real uu = u, vv = v;
420 Handle(BRepAdaptor_HSurface) surf = new BRepAdaptor_HSurface();
421 surf->ChangeSurface().Initialize( Face, Standard_False );
422 const Standard_Boolean IsUPer = surf->IsUPeriodic();
423 const Standard_Boolean IsVPer = surf->IsVPeriodic();
424 const Standard_Real uperiod = IsUPer ? surf->UPeriod() : 0.0;
425 const Standard_Real vperiod = IsVPer ? surf->VPeriod() : 0.0;
426 TopAbs_State Status = TopAbs_UNKNOWN;
427 Standard_Boolean urecadre = Standard_False, vrecadre = Standard_False;
429 if (RecadreOnPeriodic)
462 if(TabOrien(1)!=-1) {
463 for(Standard_Integer n=1; n<=nbtabclass; n++) {
464 Standard_Integer cur = ((CSLib_Class2d *)TabClass(n))->SiDans(Puv);
483 BRepClass_FaceClassifier aClassifier;
484 Standard_Real m_Toluv = (Toluv > 4.0) ? 4.0 : Toluv;
485 //aClassifier.Perform(Face,Puv,Toluv);
486 aClassifier.Perform(Face,Puv,m_Toluv);
487 Status = aClassifier.State();
496 else { //-- TabOrien(1)=-1 False Wire
497 BRepClass_FaceClassifier aClassifier;
498 aClassifier.Perform(Face,Puv,Toluv);
499 Status = aClassifier.State();
502 if (!RecadreOnPeriodic || (!IsUPer && !IsVPer))
504 if (Status == TopAbs_IN || Status == TopAbs_ON)
510 urecadre = Standard_True;
515 if (u > Umax || !IsUPer)
520 vrecadre = Standard_True;
528 if (v > Vmax || !IsVPer)
534 TopAbs_State BRepTopAdaptor_FClass2d::TestOnRestriction(const gp_Pnt2d& _Puv,
535 const Standard_Real Tol,
536 const Standard_Boolean RecadreOnPeriodic) const
539 STAT.NbConstrShape++;
542 Standard_Integer dedans;
543 Standard_Integer nbtabclass = TabClass.Length();
549 //-- U1 is the First Param and U2 in this case is U1+Period
550 Standard_Real u=_Puv.X();
551 Standard_Real v=_Puv.Y();
552 Standard_Real uu = u, vv = v;
554 Handle(BRepAdaptor_HSurface) surf = new BRepAdaptor_HSurface();
555 surf->ChangeSurface().Initialize( Face, Standard_False );
556 const Standard_Boolean IsUPer = surf->IsUPeriodic();
557 const Standard_Boolean IsVPer = surf->IsVPeriodic();
558 const Standard_Real uperiod = IsUPer ? surf->UPeriod() : 0.0;
559 const Standard_Real vperiod = IsVPer ? surf->VPeriod() : 0.0;
560 TopAbs_State Status = TopAbs_UNKNOWN;
561 Standard_Boolean urecadre = Standard_False, vrecadre = Standard_False;
563 if (RecadreOnPeriodic)
596 if(TabOrien(1)!=-1) {
597 for(Standard_Integer n=1; n<=nbtabclass; n++) {
598 Standard_Integer cur = ((CSLib_Class2d *)TabClass(n))->SiDans_OnMode(Puv,Tol);
626 else { //-- TabOrien(1)=-1 False Wire
627 BRepClass_FaceClassifier aClassifier;
628 aClassifier.Perform(Face,Puv,Tol);
629 Status = aClassifier.State();
632 if (!RecadreOnPeriodic || (!IsUPer && !IsVPer))
634 if (Status == TopAbs_IN || Status == TopAbs_ON)
640 urecadre = Standard_True;
645 if (u > Umax || !IsUPer)
650 vrecadre = Standard_True;
658 if (v > Vmax || !IsVPer)
665 void BRepTopAdaptor_FClass2d::Destroy() {
670 Standard_Integer nbtabclass = TabClass.Length();
671 for(Standard_Integer d=1; d<=nbtabclass;d++) {
673 delete ((CSLib_Class2d *)TabClass(d));
681 #include <Standard_ConstructionError.hxx>
684 //const BRepTopAdaptor_FClass2d & BRepTopAdaptor_FClass2d::Copy(const BRepTopAdaptor_FClass2d& Other) const {
685 const BRepTopAdaptor_FClass2d & BRepTopAdaptor_FClass2d::Copy(const BRepTopAdaptor_FClass2d& ) const {
687 cerr<<"Copy not allowed in BRepTopAdaptor_FClass2d"<<endl;
689 Standard_ConstructionError::Raise();