1 // Created by: Bruno DUMORTIER
2 // Copyright (c) 1995-1999 Matra Datavision
3 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <ProjLib_ComputeApproxOnPolarSurface.hxx>
17 #include <AppCont_Function2d.hxx>
20 #include <BSplCLib.hxx>
22 #include <Standard_NoSuchObject.hxx>
23 #include <Geom_UndefinedDerivative.hxx>
24 #include <gp_Trsf.hxx>
25 #include <Precision.hxx>
26 #include <Approx_FitAndDivide2d.hxx>
28 #include <AppParCurves_MultiCurve.hxx>
29 #include <Geom_Surface.hxx>
30 #include <Geom2d_BSplineCurve.hxx>
31 #include <Geom2d_BezierCurve.hxx>
32 #include <Geom2d_Line.hxx>
33 #include <Geom2d_Circle.hxx>
34 #include <Geom2d_Ellipse.hxx>
35 #include <Geom2d_Hyperbola.hxx>
36 #include <Geom2d_Parabola.hxx>
37 #include <Geom2d_TrimmedCurve.hxx>
38 #include <Geom_BSplineSurface.hxx>
39 #include <Geom_BezierSurface.hxx>
40 #include <Geom_BSplineCurve.hxx>
41 #include <Geom_BezierCurve.hxx>
42 #include <Geom_TrimmedCurve.hxx>
44 #include <TColgp_Array1OfPnt2d.hxx>
45 #include <TColgp_Array2OfPnt2d.hxx>
46 #include <TColgp_Array1OfPnt.hxx>
47 #include <TColgp_SequenceOfPnt2d.hxx>
48 #include <TColStd_Array1OfReal.hxx>
49 #include <TColStd_Array1OfInteger.hxx>
50 #include <TColStd_SequenceOfReal.hxx>
51 #include <TColStd_ListOfTransient.hxx>
53 #include <GeomAbs_SurfaceType.hxx>
54 #include <GeomAbs_CurveType.hxx>
55 #include <Adaptor3d_Surface.hxx>
56 #include <Adaptor3d_Curve.hxx>
57 #include <Adaptor3d_HSurface.hxx>
58 #include <Adaptor3d_HCurve.hxx>
59 #include <Adaptor2d_HCurve2d.hxx>
60 #include <Geom2dAdaptor_Curve.hxx>
61 #include <Geom2dAdaptor_HCurve.hxx>
62 #include <GeomAdaptor_HCurve.hxx>
63 #include <GeomAdaptor.hxx>
64 #include <GeomAdaptor_Surface.hxx>
65 #include <TColgp_SequenceOfPnt.hxx>
68 #include <gp_Pnt2d.hxx>
69 #include <gp_Vec2d.hxx>
70 #include <Extrema_GenLocateExtPS.hxx>
71 #include <Extrema_ExtPS.hxx>
72 #include <GCPnts_QuasiUniformAbscissa.hxx>
73 #include <Standard_DomainError.hxx>
74 //#include <GeomLib_IsIso.hxx>
75 //#include <GeomLib_CheckSameParameter.hxx>
79 #include <DrawTrSurf.hxx>
81 //static Standard_Integer compteur = 0;
84 //=======================================================================
86 //purpose : (OCC217 - apo)- Compute Point2d that project on polar surface(<Surf>) 3D<Curve>
87 // <InitCurve2d> use for calculate start 2D point.
88 //=======================================================================
90 static gp_Pnt2d Function_Value(const Standard_Real U,
91 const Handle(Adaptor3d_HSurface)& Surf,
92 const Handle(Adaptor3d_HCurve)& Curve,
93 const Handle(Adaptor2d_HCurve2d)& InitCurve2d,
95 const Standard_Real DistTol3d, const Standard_Real tolU, const Standard_Real tolV)
96 //const Standard_Real Tolerance)
99 //Standard_Real Tol3d = 100*Tolerance;
101 gp_Pnt2d p2d = InitCurve2d->Value(U) ;
102 gp_Pnt p = Curve->Value(U);
104 Standard_Real Uinf, Usup, Vinf, Vsup;
105 Uinf = Surf->Surface().FirstUParameter();
106 Usup = Surf->Surface().LastUParameter();
107 Vinf = Surf->Surface().FirstVParameter();
108 Vsup = Surf->Surface().LastVParameter();
109 Standard_Integer decalU = 0, decalV = 0;
110 Standard_Real U0 = p2d.X(), V0 = p2d.Y();
112 GeomAbs_SurfaceType Type = Surf->GetType();
113 if((Type != GeomAbs_BSplineSurface) &&
114 (Type != GeomAbs_BezierSurface) &&
115 (Type != GeomAbs_OffsetSurface) ) {
116 Standard_Real S = 0., T = 0.;
118 // case GeomAbs_Plane:
120 // gp_Pln Plane = Surf->Plane();
121 // ElSLib::Parameters( Plane, p, S, T);
124 case GeomAbs_Cylinder:
126 gp_Cylinder Cylinder = Surf->Cylinder();
127 ElSLib::Parameters( Cylinder, p, S, T);
128 if(U0 < Uinf) decalU = -int((Uinf - U0)/(2*M_PI))-1;
129 if(U0 > Usup) decalU = int((U0 - Usup)/(2*M_PI))+1;
135 gp_Cone Cone = Surf->Cone();
136 ElSLib::Parameters( Cone, p, S, T);
137 if(U0 < Uinf) decalU = -int((Uinf - U0)/(2*M_PI))-1;
138 if(U0 > Usup) decalU = int((U0 - Usup)/(2*M_PI))+1;
144 gp_Sphere Sphere = Surf->Sphere();
145 ElSLib::Parameters( Sphere, p, S, T);
146 if(U0 < Uinf) decalU = -int((Uinf - U0)/(2*M_PI))-1;
147 if(U0 > Usup) decalU = int((U0 - Usup)/(2*M_PI))+1;
149 if(V0 < Vinf) decalV = -int((Vinf - V0)/(2*M_PI))-1;
150 if(V0 > (Vsup+(Vsup-Vinf))) decalV = int((V0 - Vsup+(Vsup-Vinf))/(2*M_PI))+1;
152 if(0.4*M_PI < Abs(U0 - S) && Abs(U0 - S) < 1.6*M_PI) {
163 gp_Torus Torus = Surf->Torus();
164 ElSLib::Parameters( Torus, p, S, T);
165 if(U0 < Uinf) decalU = -int((Uinf - U0)/(2*M_PI))-1;
166 if(U0 > Usup) decalU = int((U0 - Usup)/(2*M_PI))+1;
167 if(V0 < Vinf) decalV = -int((Vinf - V0)/(2*M_PI))-1;
168 if(V0 > Vsup) decalV = int((V0 - Vsup)/(2*M_PI))+1;
169 S += decalU*2*M_PI; T += decalV*2*M_PI;
173 Standard_NoSuchObject::Raise("ProjLib_ComputeApproxOnPolarSurface::Value");
175 return gp_Pnt2d(S, T);
179 Standard_Real Dist2Min = RealLast();
181 //Standard_Real tolU,tolV ;
185 Standard_Real uperiod =0, vperiod = 0, u, v;
186 // U0 and V0 are the points within the initialized period
187 // (periode with u and v),
188 // U1 and V1 are the points for construction of tops
190 if(Surf->IsUPeriodic() || Surf->IsUClosed()) {
191 uperiod = Surf->LastUParameter() - Surf->FirstUParameter();
193 if(Surf->IsVPeriodic() || Surf->IsVClosed()) {
194 vperiod = Surf->LastVParameter() - Surf->FirstVParameter();
200 decalU = int((Uinf - U0)/uperiod)+1;
201 U0 += decalU*uperiod;
208 decalU = -(int((U0 - Usup)/uperiod)+1);
209 U0 += decalU*uperiod;
216 decalV = int((Vinf - V0)/vperiod)+1;
217 V0 += decalV*vperiod;
224 decalV = -int((V0 - Vsup)/vperiod)-1;
225 V0 += decalV*vperiod;
229 // The surface around U0 is reduced
230 Standard_Real uLittle = (Usup - Uinf)/10, vLittle = (Vsup - Vinf)/10;
231 Standard_Real uInfLi = 0, vInfLi = 0,uSupLi = 0, vSupLi = 0;
232 if((U0 - Uinf) > uLittle) uInfLi = U0 - uLittle; else uInfLi = Uinf;
233 if((V0 - Vinf) > vLittle) vInfLi = V0 - vLittle; else vInfLi = Vinf;
234 if((Usup - U0) > uLittle) uSupLi = U0 + uLittle; else uSupLi = Usup;
235 if((Vsup - V0) > vLittle) vSupLi = V0 + vLittle; else vSupLi = Vsup;
237 // const Adaptor3d_Surface GAS = Surf->Surface();
239 GeomAdaptor_Surface SurfLittle;
240 if (Type == GeomAbs_BSplineSurface) {
241 Handle(Geom_Surface) GBSS(Surf->Surface().BSpline());
242 SurfLittle.Load(GBSS, uInfLi, uSupLi, vInfLi, vSupLi);
244 else if (Type == GeomAbs_BezierSurface) {
245 Handle(Geom_Surface) GS(Surf->Surface().Bezier());
246 SurfLittle.Load(GS, uInfLi, uSupLi, vInfLi, vSupLi);
248 else if (Type == GeomAbs_OffsetSurface) {
249 Handle(Geom_Surface) GS = GeomAdaptor::MakeSurface(Surf->Surface());
250 SurfLittle.Load(GS, uInfLi, uSupLi, vInfLi, vSupLi);
253 Standard_NoSuchObject::Raise("");
256 Extrema_GenLocateExtPS locext(p, SurfLittle, U0, V0, tolU, tolV);
257 if (locext.IsDone()) {
258 Dist2Min = locext.SquareDistance();
259 if (Dist2Min < DistTol3d * DistTol3d) {
260 (locext.Point()).Parameter(u, v);
261 gp_Pnt2d pnt(u - decalU*uperiod,v - decalV*vperiod);
266 Extrema_ExtPS ext(p, SurfLittle, tolU, tolV) ;
267 if (ext.IsDone() && ext.NbExt()>=1 ) {
268 Dist2Min = ext.SquareDistance(1);
269 Standard_Integer GoodValue = 1;
270 for ( Standard_Integer i = 2 ; i <= ext.NbExt() ; i++ )
271 if( Dist2Min > ext.SquareDistance(i)) {
272 Dist2Min = ext.SquareDistance(i);
275 if (Dist2Min < DistTol3d * DistTol3d) {
276 (ext.Point(GoodValue)).Parameter(u,v);
277 gp_Pnt2d pnt(u - decalU*uperiod,v - decalV*vperiod);
286 //=======================================================================
287 //function : ProjLib_PolarFunction
288 //purpose : (OCC217 - apo)- This class produce interface to call "gp_Pnt2d Function_Value(...)"
289 //=======================================================================
291 class ProjLib_PolarFunction : public AppCont_Function2d
293 Handle(Adaptor3d_HCurve) myCurve;
294 Handle(Adaptor2d_HCurve2d) myInitialCurve2d ;
295 Handle(Adaptor3d_HSurface) mySurface ;
297 Standard_Real myTolU, myTolV;
298 Standard_Real myDistTol3d;
299 //Standard_Real myTolerance ;
303 ProjLib_PolarFunction(const Handle(Adaptor3d_HCurve) & C,
304 const Handle(Adaptor3d_HSurface)& Surf,
305 const Handle(Adaptor2d_HCurve2d)& InitialCurve2d,
307 const Standard_Real Tol3d) :
308 //const Standard_Real Tolerance) :
310 myInitialCurve2d(InitialCurve2d),
313 myTolU(Surf->UResolution(Tol3d)),
314 myTolV(Surf->VResolution(Tol3d)),
315 myDistTol3d(100.0*Tol3d){} ;
316 //myTolerance(Tolerance){} ;
318 ~ProjLib_PolarFunction() {}
320 Standard_Real FirstParameter() const
321 {return (myCurve->FirstParameter()/*+1.e-9*/);}
323 Standard_Real LastParameter() const
324 {return (myCurve->LastParameter()/*-1.e-9*/);}
326 gp_Pnt2d Value(const Standard_Real t) const {
327 return Function_Value
328 (t,mySurface,myCurve,myInitialCurve2d,myDistTol3d,myTolU,myTolV) ; //OCC217
329 //(t,mySurface,myCurve,myInitialCurve2d,myTolerance) ;
332 // Standard_Boolean D1(const Standard_Real t, gp_Pnt2d& P, gp_Vec2d& V) const
333 Standard_Boolean D1(const Standard_Real , gp_Pnt2d& , gp_Vec2d& ) const
334 {return Standard_False;}
337 //=======================================================================
338 //function : ProjLib_ComputeApproxOnPolarSurface
340 //=======================================================================
342 ProjLib_ComputeApproxOnPolarSurface::ProjLib_ComputeApproxOnPolarSurface(){}
345 //=======================================================================
346 //function : ProjLib_ComputeApproxOnPolarSurface
348 //=======================================================================
350 ProjLib_ComputeApproxOnPolarSurface::ProjLib_ComputeApproxOnPolarSurface
351 (const Handle(Adaptor2d_HCurve2d)& InitialCurve2d,
352 const Handle(Adaptor3d_HCurve)& Curve,
353 const Handle(Adaptor3d_HSurface)& S,
354 const Standard_Real tol3d):myProjIsDone(Standard_False) //OCC217
355 //const Standard_Real tolerance):myProjIsDone(Standard_False)
357 myTolerance = tol3d; //OCC217
358 //myTolerance = Max(Tolerance,Precision::PApproximation());
359 myBSpline = Perform(InitialCurve2d,Curve,S);
361 //=======================================================================
362 //function : ProjLib_ComputeApproxOnPolarSurface
363 //purpose : Process the case of sewing
364 //=======================================================================
366 ProjLib_ComputeApproxOnPolarSurface::ProjLib_ComputeApproxOnPolarSurface
367 (const Handle(Adaptor2d_HCurve2d)& InitialCurve2d,
368 const Handle(Adaptor2d_HCurve2d)& InitialCurve2dBis,
369 const Handle(Adaptor3d_HCurve)& Curve,
370 const Handle(Adaptor3d_HSurface)& S,
371 const Standard_Real tol3d):myProjIsDone(Standard_False) //OCC217
372 //const Standard_Real tolerance):myProjIsDone(Standard_False)
373 {// InitialCurve2d and InitialCurve2dBis are two pcurves of the sewing
374 myTolerance = tol3d; //OCC217
375 //myTolerance = Max(tolerance,Precision::PApproximation());
376 Handle(Geom2d_BSplineCurve) bsc = Perform(InitialCurve2d,Curve,S);
378 gp_Pnt2d P2dproj, P2d, P2dBis;
379 P2dproj = bsc->StartPoint();
380 P2d = InitialCurve2d->Value(InitialCurve2d->FirstParameter());
381 P2dBis = InitialCurve2dBis->Value(InitialCurve2dBis->FirstParameter());
383 Standard_Real Dist, DistBis;
384 Dist = P2dproj.Distance(P2d);
385 DistBis = P2dproj.Distance(P2dBis);
386 if( Dist < DistBis) {
387 // myBSpline2d is the pcurve that is found. It is translated to obtain myCurve2d
389 Handle(Geom2d_Geometry) GG = myBSpline->Translated(P2d, P2dBis);
390 my2ndCurve = Handle(Geom2d_Curve)::DownCast(GG);
394 Handle(Geom2d_Geometry) GG = my2ndCurve->Translated(P2dBis, P2d);
395 myBSpline = Handle(Geom2d_BSplineCurve)::DownCast(GG);
400 //=======================================================================
401 //function : ProjLib_ComputeApproxOnPolarSurface
402 //purpose : case without curve of initialization
403 //=======================================================================
405 ProjLib_ComputeApproxOnPolarSurface::ProjLib_ComputeApproxOnPolarSurface
406 (const Handle(Adaptor3d_HCurve)& Curve,
407 const Handle(Adaptor3d_HSurface)& S,
408 const Standard_Real tol3d):myProjIsDone(Standard_False) //OCC217
409 //const Standard_Real tolerance):myProjIsDone(Standard_False)
411 myTolerance = tol3d; //OCC217
412 //myTolerance = Max(tolerance,Precision::PApproximation());
413 const Handle(Adaptor2d_HCurve2d) InitCurve2d ;
414 myBSpline = Perform(InitCurve2d,Curve,S);
417 static Handle(Geom2d_BSplineCurve) Concat(Handle(Geom2d_BSplineCurve) C1,
418 Handle(Geom2d_BSplineCurve) C2)
420 Standard_Integer deg, deg1, deg2;
425 C1->IncreaseDegree(deg2);
428 else if ( deg2 < deg1) {
429 C2->IncreaseDegree(deg1);
434 Standard_Integer np1,np2,nk1,nk2,np,nk;
442 TColStd_Array1OfReal K1(1,nk1); C1->Knots(K1);
443 TColStd_Array1OfInteger M1(1,nk1); C1->Multiplicities(M1);
444 TColgp_Array1OfPnt2d P1(1,np1); C1->Poles(P1);
445 TColStd_Array1OfReal K2(1,nk2); C2->Knots(K2);
446 TColStd_Array1OfInteger M2(1,nk2); C2->Multiplicities(M2);
447 TColgp_Array1OfPnt2d P2(1,np2); C2->Poles(P2);
449 // Compute the new BSplineCurve
450 TColStd_Array1OfReal K(1,nk);
451 TColStd_Array1OfInteger M(1,nk);
452 TColgp_Array1OfPnt2d P(1,np);
454 Standard_Integer i, count = 0;
455 // Set Knots and Mults
456 for ( i = 1; i <= nk1; i++) {
462 for ( i = 2; i <= nk2; i++) {
469 for (i = 1; i <= np1; i++) {
473 for (i = 2; i <= np2; i++) {
478 Handle(Geom2d_BSplineCurve) BS =
479 new Geom2d_BSplineCurve(P,K,M,deg);
484 //=======================================================================
487 //=======================================================================
488 Handle(Geom2d_BSplineCurve) ProjLib_ComputeApproxOnPolarSurface::Perform
489 (const Handle(Adaptor2d_HCurve2d)& InitialCurve2d,
490 const Handle(Adaptor3d_HCurve)& Curve,
491 const Handle(Adaptor3d_HSurface)& S)
494 Standard_Real Tol3d = myTolerance;
495 Standard_Real ParamTol = Precision::PApproximation();
497 Handle(Adaptor2d_HCurve2d) AHC2d = InitialCurve2d;
498 Handle(Adaptor3d_HCurve) AHC = Curve;
500 // if the curve 3d is a BSpline with degree C0, it is cut into sections with degree C1
502 GeomAbs_CurveType typeCurve = Curve->GetType();
503 if(typeCurve == GeomAbs_BSplineCurve) {
504 TColStd_ListOfTransient LOfBSpline2d;
505 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
506 Standard_Integer nbInter = Curve->NbIntervals(GeomAbs_C1);
508 Standard_Integer i, j;
509 Handle(Geom_TrimmedCurve) GTC;
510 Handle(Geom2d_TrimmedCurve) G2dTC;
511 TColStd_Array1OfReal Inter(1,nbInter+1);
512 Curve->Intervals(Inter,GeomAbs_C1);
513 Standard_Real firstinter = Inter.Value(1), secondinter = Inter.Value(2);
515 GTC = new Geom_TrimmedCurve(BSC, firstinter, secondinter);
516 AHC = new GeomAdaptor_HCurve(GTC);
518 // if there is an initialization curve:
519 // - either this is a BSpline C0, with discontinuity at the same parameters of nodes
520 // and the sections C1 are taken
521 // - or this is a curve C1 and the sections of intrest are taken otherwise the curve is created.
524 Standard_Integer nbInter2d;
525 Standard_Boolean C2dIsToCompute;
526 C2dIsToCompute = InitialCurve2d.IsNull();
527 Handle(Geom2d_BSplineCurve) BSC2d;
528 Handle(Geom2d_Curve) G2dC;
530 if(!C2dIsToCompute) {
531 nbInter2d = InitialCurve2d->NbIntervals(GeomAbs_C1);
532 TColStd_Array1OfReal Inter2d(1,nbInter2d+1);
533 InitialCurve2d->Intervals(Inter2d,GeomAbs_C1);
535 for(i = 1,j = 1;i <= nbInter;i++)
536 if(Abs(Inter.Value(i) - Inter2d.Value(j)) < ParamTol) { //OCC217
537 //if(Abs(Inter.Value(i) - Inter2d.Value(j)) < myTolerance) {
538 if (j > nbInter2d) break;
541 if(j != (nbInter2d+1)) {
542 C2dIsToCompute = Standard_True;
547 AHC2d = BuildInitialCurve2d(AHC, S);
550 typeCurve = InitialCurve2d->GetType();
553 G2dC = new Geom2d_Line(InitialCurve2d->Line());
556 case GeomAbs_Circle: {
557 G2dC = new Geom2d_Circle(InitialCurve2d->Circle());
560 case GeomAbs_Ellipse: {
561 G2dC = new Geom2d_Ellipse(InitialCurve2d->Ellipse());
564 case GeomAbs_Hyperbola: {
565 G2dC = new Geom2d_Hyperbola(InitialCurve2d->Hyperbola());
568 case GeomAbs_Parabola: {
569 G2dC = new Geom2d_Parabola(InitialCurve2d->Parabola());
572 case GeomAbs_BezierCurve: {
573 G2dC = InitialCurve2d->Bezier();
576 case GeomAbs_BSplineCurve: {
577 G2dC = InitialCurve2d->BSpline();
580 case GeomAbs_OtherCurve:
584 gp_Pnt2d fp2d = G2dC->Value(firstinter), lp2d = G2dC->Value(secondinter);
585 gp_Pnt fps, lps, fpc, lpc;
586 S->D0(fp2d.X(), fp2d.Y(), fps);
587 S->D0(lp2d.X(), lp2d.Y(), lps);
588 Curve->D0(firstinter, fpc);
589 Curve->D0(secondinter, lpc);
591 if((fps.IsEqual(fpc, Tol3d)) &&
592 (lps.IsEqual(lpc, Tol3d))) {
593 //if((fps.IsEqual(fpc, myTolerance)) &&
594 // (lps.IsEqual(lpc, myTolerance))) {
595 G2dTC = new Geom2d_TrimmedCurve(G2dC, firstinter, secondinter);
596 Geom2dAdaptor_Curve G2dAC(G2dTC);
597 AHC2d = new Geom2dAdaptor_HCurve(G2dAC);
598 myProjIsDone = Standard_True;
601 AHC2d = BuildInitialCurve2d(AHC, S);
602 C2dIsToCompute = Standard_True;
607 BSC2d = ProjectUsingInitialCurve2d(AHC, S, AHC2d);
608 if(BSC2d.IsNull()) return Handle(Geom2d_BSplineCurve)(); //IFV
609 LOfBSpline2d.Append(BSC2d);
612 return Handle(Geom2d_BSplineCurve)();
617 Standard_Real iinter, ip1inter;
618 Standard_Integer nbK2d, deg;
619 nbK2d = BSC2d->NbKnots(); deg = BSC2d->Degree();
621 for(i = 2;i <= nbInter;i++) {
622 iinter = Inter.Value(i);
623 ip1inter = Inter.Value(i+1);
625 GTC->SetTrim(iinter, ip1inter);
626 AHC = new GeomAdaptor_HCurve(GTC);
630 AHC2d = BuildInitialCurve2d(AHC, S);
633 gp_Pnt2d fp2d = G2dC->Value(iinter), lp2d = G2dC->Value(ip1inter);
634 gp_Pnt fps, lps, fpc, lpc;
635 S->D0(fp2d.X(), fp2d.Y(), fps);
636 S->D0(lp2d.X(), lp2d.Y(), lps);
637 Curve->D0(iinter, fpc);
638 Curve->D0(ip1inter, lpc);
640 if((fps.IsEqual(fpc, Tol3d)) &&
641 (lps.IsEqual(lpc, Tol3d))) {
642 //if((fps.IsEqual(fpc, myTolerance)) &&
643 // (lps.IsEqual(lpc, myTolerance))) {
644 G2dTC->SetTrim(iinter, ip1inter);
645 Geom2dAdaptor_Curve G2dAC(G2dTC);
646 AHC2d = new Geom2dAdaptor_HCurve(G2dAC);
647 myProjIsDone = Standard_True;
650 AHC2d = BuildInitialCurve2d(AHC, S);
654 BSC2d = ProjectUsingInitialCurve2d(AHC, S, AHC2d);
656 return Handle(Geom2d_BSplineCurve)();
658 LOfBSpline2d.Append(BSC2d);
659 nbK2d += BSC2d->NbKnots() - 1;
660 deg = Max(deg, BSC2d->Degree());
663 return Handle(Geom2d_BSplineCurve)();
667 Standard_Integer NbC = LOfBSpline2d.Extent();
668 Handle(Geom2d_BSplineCurve) CurBS;
669 CurBS = Handle(Geom2d_BSplineCurve)::DownCast(LOfBSpline2d.First());
670 LOfBSpline2d.RemoveFirst();
671 for (Standard_Integer ii = 2; ii <= NbC; ii++) {
672 Handle(Geom2d_BSplineCurve) BS =
673 Handle(Geom2d_BSplineCurve)::DownCast(LOfBSpline2d.First());
674 CurBS = Concat(CurBS,BS);
675 LOfBSpline2d.RemoveFirst();
681 if(InitialCurve2d.IsNull()) {
682 AHC2d = BuildInitialCurve2d(Curve, S);
684 return Handle(Geom2d_BSplineCurve)();
686 return ProjectUsingInitialCurve2d(AHC, S, AHC2d);
689 //=======================================================================
690 //function : ProjLib_BuildInitialCurve2d
692 //=======================================================================
694 Handle(Adaptor2d_HCurve2d)
695 ProjLib_ComputeApproxOnPolarSurface::
696 BuildInitialCurve2d(const Handle(Adaptor3d_HCurve)& Curve,
697 const Handle(Adaptor3d_HSurface)& Surf)
699 // discretize the Curve with quasiuniform deflection
700 // density at least NbOfPnts points
701 myProjIsDone = Standard_False;
704 Standard_Real Tol3d = myTolerance;
705 Standard_Real TolU = Surf->UResolution(Tol3d), TolV = Surf->VResolution(Tol3d);
706 Standard_Real DistTol3d = 100.0*Tol3d;
708 Standard_Real uperiod = 0., vperiod = 0.;
709 if(Surf->IsUPeriodic() || Surf->IsUClosed())
710 uperiod = Surf->LastUParameter() - Surf->FirstUParameter();
712 if(Surf->IsVPeriodic() || Surf->IsVClosed())
713 vperiod = Surf->LastVParameter() - Surf->FirstVParameter();
716 // NO myTol is Tol2d !!!!
717 //Standard_Real TolU = myTolerance, TolV = myTolerance;
718 //Standard_Real Tol3d = 100*myTolerance; // At random Balthazar.
720 Standard_Integer NbOfPnts = 61;
721 GCPnts_QuasiUniformAbscissa QUA(Curve->GetCurve(),NbOfPnts);
722 TColgp_Array1OfPnt Pts(1,NbOfPnts);
723 TColStd_Array1OfReal Param(1,NbOfPnts);
724 Standard_Integer i, j;
725 for( i = 1; i <= NbOfPnts ; i++ ) {
726 Param(i) = QUA.Parameter(i);
727 Pts(i) = Curve->Value(Param(i));
730 TColgp_Array1OfPnt2d Pts2d(1,NbOfPnts);
731 TColStd_Array1OfInteger Mult(1,NbOfPnts);
733 Mult(1) = Mult(NbOfPnts) = 2;
735 Standard_Real Uinf, Usup, Vinf, Vsup;
736 Uinf = Surf->Surface().FirstUParameter();
737 Usup = Surf->Surface().LastUParameter();
738 Vinf = Surf->Surface().FirstVParameter();
739 Vsup = Surf->Surface().LastVParameter();
740 GeomAbs_SurfaceType Type = Surf->GetType();
741 if((Type != GeomAbs_BSplineSurface) && (Type != GeomAbs_BezierSurface) &&
742 (Type != GeomAbs_OffsetSurface)) {
744 // Standard_Integer usens = 0, vsens = 0;
745 // to know the position relatively to the period
747 // case GeomAbs_Plane:
749 // gp_Pln Plane = Surf->Plane();
750 // for ( i = 1 ; i <= NbOfPnts ; i++) {
751 // ElSLib::Parameters( Plane, Pts(i), S, T);
752 // Pts2d(i).SetCoord(S,T);
754 // myProjIsDone = Standard_True;
757 case GeomAbs_Cylinder:
759 // Standard_Real Sloc, Tloc;
761 Standard_Integer usens = 0;
762 gp_Cylinder Cylinder = Surf->Cylinder();
763 ElSLib::Parameters( Cylinder, Pts(1), S, T);
764 Pts2d(1).SetCoord(S,T);
765 for ( i = 2 ; i <= NbOfPnts ; i++) {
767 ElSLib::Parameters( Cylinder, Pts(i), S, T);
768 if(Abs(Sloc - S) > M_PI) {
774 Pts2d(i).SetCoord(S+usens*2*M_PI,T);
776 myProjIsDone = Standard_True;
781 // Standard_Real Sloc, Tloc;
783 Standard_Integer usens = 0;
784 gp_Cone Cone = Surf->Cone();
785 ElSLib::Parameters( Cone, Pts(1), S, T);
786 Pts2d(1).SetCoord(S,T);
787 for ( i = 2 ; i <= NbOfPnts ; i++) {
789 ElSLib::Parameters( Cone, Pts(i), S, T);
790 if(Abs(Sloc - S) > M_PI) {
796 Pts2d(i).SetCoord(S+usens*2*M_PI,T);
798 myProjIsDone = Standard_True;
803 Standard_Real Sloc, Tloc;
804 Standard_Integer usens = 0, vsens = 0; //usens steps by half-period
805 Standard_Boolean vparit = Standard_False;
806 gp_Sphere Sphere = Surf->Sphere();
807 ElSLib::Parameters( Sphere, Pts(1), S, T);
808 Pts2d(1).SetCoord(S,T);
809 for ( i = 2 ; i <= NbOfPnts ; i++) {
811 ElSLib::Parameters( Sphere, Pts(i), S, T);
812 if(1.6*M_PI < Abs(Sloc - S)) {
818 if(1.6*M_PI > Abs(Sloc - S) && Abs(Sloc - S) > 0.4*M_PI) {
824 if(Abs(Tloc - Vsup) < (Vsup - Vinf)/5)
830 Pts2d(i).SetCoord(S+usens*M_PI,(M_PI - T)*(vsens-1));
833 Pts2d(i).SetCoord(S+usens*M_PI,T+vsens*M_PI);
837 myProjIsDone = Standard_True;
842 Standard_Real Sloc, Tloc;
843 Standard_Integer usens = 0, vsens = 0;
844 gp_Torus Torus = Surf->Torus();
845 ElSLib::Parameters( Torus, Pts(1), S, T);
846 Pts2d(1).SetCoord(S,T);
847 for ( i = 2 ; i <= NbOfPnts ; i++) {
849 ElSLib::Parameters( Torus, Pts(i), S, T);
850 if(Abs(Sloc - S) > M_PI) {
856 if(Abs(Tloc - T) > M_PI) {
862 Pts2d(i).SetCoord(S+usens*2*M_PI,T+vsens*2*M_PI);
864 myProjIsDone = Standard_True;
868 Standard_NoSuchObject::Raise("ProjLib_ComputeApproxOnPolarSurface::BuildInitialCurve2d");
872 myProjIsDone = Standard_False;
873 Standard_Real Dist2Min = 1.e+200, u = 0., v = 0.;
876 TColgp_SequenceOfPnt2d Sols;
877 Standard_Boolean areManyZeros = Standard_False;
879 Curve->D0(Param.Value(1), pntproj) ;
880 Extrema_ExtPS aExtPS(pntproj, Surf->Surface(), TolU, TolV) ;
881 Standard_Real aMinSqDist = RealLast();
884 for (i = 1; i <= aExtPS.NbExt(); i++)
886 Standard_Real aSqDist = aExtPS.SquareDistance(i);
887 if (aSqDist < aMinSqDist)
888 aMinSqDist = aSqDist;
891 if (aMinSqDist > DistTol3d * DistTol3d) //try to project with less tolerance
893 TolU = Min(TolU, Precision::PConfusion());
894 TolV = Min(TolV, Precision::PConfusion());
895 aExtPS.Initialize(Surf->Surface(),
896 Surf->Surface().FirstUParameter(), Surf->Surface().LastUParameter(),
897 Surf->Surface().FirstVParameter(), Surf->Surface().LastVParameter(),
899 aExtPS.Perform(pntproj);
902 if( aExtPS.IsDone() && aExtPS.NbExt() >= 1 ) {
904 Standard_Integer GoodValue = 1;
906 for ( i = 1 ; i <= aExtPS.NbExt() ; i++ ) {
907 if( aExtPS.SquareDistance(i) < DistTol3d * DistTol3d ) {
908 if( aExtPS.SquareDistance(i) <= 1.e-18 ) {
909 aExtPS.Point(i).Parameter(u,v);
911 Standard_Boolean isSame = Standard_False;
912 for( j = 1; j <= Sols.Length(); j++ ) {
913 if( p2d.SquareDistance( Sols.Value(j) ) <= 1.e-18 ) {
914 isSame = Standard_True;
918 if( !isSame ) Sols.Append( p2d );
920 if( Dist2Min > aExtPS.SquareDistance(i) ) {
921 Dist2Min = aExtPS.SquareDistance(i);
927 if( Sols.Length() > 1 ) areManyZeros = Standard_True;
929 if( Dist2Min <= DistTol3d * DistTol3d) {
930 if( !areManyZeros ) {
931 aExtPS.Point(GoodValue).Parameter(u,v);
932 Pts2d(1).SetCoord(u,v);
933 myProjIsDone = Standard_True;
936 Standard_Integer nbSols = Sols.Length();
937 Standard_Real Dist2Max = -1.e+200;
938 for( i = 1; i <= nbSols; i++ ) {
939 const gp_Pnt2d& aP1 = Sols.Value(i);
940 for( j = i+1; j <= nbSols; j++ ) {
941 const gp_Pnt2d& aP2 = Sols.Value(j);
942 Standard_Real aDist2 = aP1.SquareDistance(aP2);
943 if( aDist2 > Dist2Max ) Dist2Max = aDist2;
946 Standard_Real aMaxT2 = Max(TolU,TolV);
948 if( Dist2Max > aMaxT2 ) {
949 Standard_Integer tPp = 0;
950 for( i = 1; i <= 5; i++ ) {
951 Standard_Integer nbExtOk = 0;
952 Standard_Integer indExt = 0;
953 Standard_Integer iT = 1 + (NbOfPnts - 1)/5*i;
954 Curve->D0( Param.Value(iT), pntproj );
955 Extrema_ExtPS aTPS( pntproj, Surf->Surface(), TolU, TolV );
957 if( aTPS.IsDone() && aTPS.NbExt() >= 1 ) {
958 for( j = 1 ; j <= aTPS.NbExt() ; j++ ) {
959 if( aTPS.SquareDistance(j) < DistTol3d * DistTol3d ) {
961 if( aTPS.SquareDistance(j) < Dist2Min ) {
962 Dist2Min = aTPS.SquareDistance(j);
970 aTPS.Point(indExt).Parameter(u,v);
976 gp_Pnt2d aPp = gp_Pnt2d(u,v);
979 Standard_Boolean isFound = Standard_False;
981 Curve->D0( Param.Value(tPp+j), pntproj );
982 Extrema_ExtPS aTPS( pntproj, Surf->Surface(), TolU, TolV );
984 Standard_Integer indExt = 0;
985 if( aTPS.IsDone() && aTPS.NbExt() >= 1 ) {
986 for( i = 1 ; i <= aTPS.NbExt() ; i++ ) {
987 if( aTPS.SquareDistance(i) < DistTol3d * DistTol3d && aTPS.SquareDistance(i) < Dist2Min ) {
988 Dist2Min = aTPS.SquareDistance(i);
990 isFound = Standard_True;
995 aTPS.Point(indExt).Parameter(u,v);
1000 if( (tPp+j) > NbOfPnts ) break;
1004 gp_Vec2d atV(aPp,aPn);
1005 Standard_Boolean isChosen = Standard_False;
1006 for( i = 1; i <= nbSols; i++ ) {
1007 const gp_Pnt2d& aP1 = Sols.Value(i);
1008 gp_Vec2d asV(aP1,aPp);
1009 if( asV.Dot(atV) > 0. ) {
1010 isChosen = Standard_True;
1011 Pts2d(1).SetCoord(aP1.X(),aP1.Y());
1012 myProjIsDone = Standard_True;
1017 aExtPS.Point(GoodValue).Parameter(u,v);
1018 Pts2d(1).SetCoord(u,v);
1019 myProjIsDone = Standard_True;
1023 aExtPS.Point(GoodValue).Parameter(u,v);
1024 Pts2d(1).SetCoord(u,v);
1025 myProjIsDone = Standard_True;
1029 aExtPS.Point(GoodValue).Parameter(u,v);
1030 Pts2d(1).SetCoord(u,v);
1031 myProjIsDone = Standard_True;
1035 aExtPS.Point(GoodValue).Parameter(u,v);
1036 Pts2d(1).SetCoord(u,v);
1037 myProjIsDone = Standard_True;
1042 // calculate the following points with GenLocate_ExtPS
1043 // (and store the result and each parameter in a sequence)
1044 Standard_Integer usens = 0, vsens = 0;
1045 // to know the position relatively to the period
1046 Standard_Real U0 = u, V0 = v, U1 = u, V1 = v;
1047 // U0 and V0 are the points in the initialized period
1048 // (period with u and v),
1049 // U1 and V1 are the points for construction of poles
1051 for ( i = 2 ; i <= NbOfPnts ; i++)
1053 myProjIsDone = Standard_False;
1054 Dist2Min = RealLast();
1055 Curve->D0(Param.Value(i), pntproj);
1056 Extrema_GenLocateExtPS aLocateExtPS
1057 (pntproj, Surf->Surface(), U0, V0, TolU, TolV) ;
1059 if (aLocateExtPS.IsDone())
1061 if (aLocateExtPS.SquareDistance() < DistTol3d * DistTol3d)
1063 //if (aLocateExtPS.SquareDistance() < Tol3d * Tol3d) {
1064 (aLocateExtPS.Point()).Parameter(U0,V0);
1065 U1 = U0 + usens*uperiod;
1066 V1 = V0 + vsens*vperiod;
1067 Pts2d(i).SetCoord(U1,V1);
1068 myProjIsDone = Standard_True;
1072 Extrema_ExtPS aGlobalExtr(pntproj, Surf->Surface(), TolU, TolV);
1073 if (aGlobalExtr.IsDone())
1075 Standard_Real LocalMinSqDist = RealLast();
1076 Standard_Integer imin = 0;
1077 for (Standard_Integer isol = 1; isol <= aGlobalExtr.NbExt(); isol++)
1079 Standard_Real aSqDist = aGlobalExtr.SquareDistance(isol);
1080 if (aSqDist < LocalMinSqDist)
1082 LocalMinSqDist = aSqDist;
1086 if (LocalMinSqDist < DistTol3d * DistTol3d)
1088 Standard_Real LocalU, LocalV;
1089 aGlobalExtr.Point(imin).Parameter(LocalU, LocalV);
1090 if (uperiod > 0. && Abs(U0 - LocalU) >= uperiod/2.)
1097 if (vperiod > 0. && Abs(V0 - LocalV) >= vperiod/2.)
1104 U0 = LocalU; V0 = LocalV;
1105 U1 = U0 + usens*uperiod;
1106 V1 = V0 + vsens*vperiod;
1107 Pts2d(i).SetCoord(U1,V1);
1108 myProjIsDone = Standard_True;
1110 if((i == 2) && (!IsEqual(uperiod, 0.0) || !IsEqual(vperiod, 0.0)))
1111 {//Make 1st point more precise for periodic surfaces
1112 const Standard_Integer aSize = 3;
1113 const gp_Pnt2d aP(Pts2d(2));
1114 Standard_Real aUpar[aSize], aVpar[aSize];
1115 Pts2d(1).Coord(aUpar[1], aVpar[1]);
1116 aUpar[0] = aUpar[1] - uperiod;
1117 aUpar[2] = aUpar[1] + uperiod;
1118 aVpar[0] = aVpar[1] - vperiod;
1119 aVpar[2] = aVpar[1] + vperiod;
1121 Standard_Real aSQdistMin = RealLast();
1122 Standard_Integer aBestUInd = 1, aBestVInd = 1;
1123 const Standard_Integer aSizeU = IsEqual(uperiod, 0.0) ? 1 : aSize,
1124 aSizeV = IsEqual(vperiod, 0.0) ? 1 : aSize;
1125 for(Standard_Integer uInd = 0; uInd < aSizeU; uInd++)
1127 for(Standard_Integer vInd = 0; vInd < aSizeV; vInd++)
1129 Standard_Real aSQdist = aP.SquareDistance(gp_Pnt2d(aUpar[uInd], aVpar[vInd]));
1130 if(aSQdist < aSQdistMin)
1132 aSQdistMin = aSQdist;
1139 Pts2d(1).SetCoord(aUpar[aBestUInd], aVpar[aBestVInd]);
1140 }//if(i == 2) condition
1145 if(!myProjIsDone && uperiod) {
1146 Standard_Real Uinf, Usup, Uaux;
1147 Uinf = Surf->Surface().FirstUParameter();
1148 Usup = Surf->Surface().LastUParameter();
1149 if((Usup - U0) > (U0 - Uinf))
1150 Uaux = 2*Uinf - U0 + uperiod;
1152 Uaux = 2*Usup - U0 - uperiod;
1153 Extrema_GenLocateExtPS locext(pntproj,
1155 Uaux, V0, TolU, TolV);
1156 if (locext.IsDone())
1157 if (locext.SquareDistance() < DistTol3d * DistTol3d) { //OCC217
1158 //if (locext.SquareDistance() < Tol3d * Tol3d) {
1159 (locext.Point()).Parameter(u,v);
1160 if((Usup - U0) > (U0 - Uinf))
1165 U1 = U0 + usens*uperiod;
1166 V1 = V0 + vsens*vperiod;
1167 Pts2d(i).SetCoord(U1,V1);
1168 myProjIsDone = Standard_True;
1171 if(!myProjIsDone && vperiod) {
1172 Standard_Real Vinf, Vsup, Vaux;
1173 Vinf = Surf->Surface().FirstVParameter();
1174 Vsup = Surf->Surface().LastVParameter();
1175 if((Vsup - V0) > (V0 - Vinf))
1176 Vaux = 2*Vinf - V0 + vperiod;
1178 Vaux = 2*Vsup - V0 - vperiod;
1179 Extrema_GenLocateExtPS locext(pntproj,
1181 U0, Vaux, TolU, TolV) ;
1182 if (locext.IsDone())
1183 if (locext.SquareDistance() < DistTol3d * DistTol3d) { //OCC217
1184 //if (locext.SquareDistance() < Tol3d * Tol3d) {
1185 (locext.Point()).Parameter(u,v);
1186 if((Vsup - V0) > (V0 - Vinf))
1191 U1 = U0 + usens*uperiod;
1192 V1 = V0 + vsens*vperiod;
1193 Pts2d(i).SetCoord(U1,V1);
1194 myProjIsDone = Standard_True;
1197 if(!myProjIsDone && uperiod && vperiod) {
1198 Standard_Real Uaux, Vaux;
1199 if((Usup - U0) > (U0 - Uinf))
1200 Uaux = 2*Uinf - U0 + uperiod;
1202 Uaux = 2*Usup - U0 - uperiod;
1203 if((Vsup - V0) > (V0 - Vinf))
1204 Vaux = 2*Vinf - V0 + vperiod;
1206 Vaux = 2*Vsup - V0 - vperiod;
1207 Extrema_GenLocateExtPS locext(pntproj,
1209 Uaux, Vaux, TolU, TolV);
1210 if (locext.IsDone())
1211 if (locext.SquareDistance() < DistTol3d * DistTol3d) {
1212 //if (locext.SquareDistance() < Tol3d * Tol3d) {
1213 (locext.Point()).Parameter(u,v);
1214 if((Usup - U0) > (U0 - Uinf))
1218 if((Vsup - V0) > (V0 - Vinf))
1223 U1 = U0 + usens*uperiod;
1224 V1 = V0 + vsens*vperiod;
1225 Pts2d(i).SetCoord(U1,V1);
1226 myProjIsDone = Standard_True;
1230 Extrema_ExtPS ext(pntproj, Surf->Surface(), TolU, TolV) ;
1232 Dist2Min = ext.SquareDistance(1);
1233 Standard_Integer GoodValue = 1;
1234 for ( j = 2 ; j <= ext.NbExt() ; j++ )
1235 if( Dist2Min > ext.SquareDistance(j)) {
1236 Dist2Min = ext.SquareDistance(j);
1239 if (Dist2Min < DistTol3d * DistTol3d) {
1240 //if (Dist2Min < Tol3d * Tol3d) {
1241 (ext.Point(GoodValue)).Parameter(u,v);
1243 if((U0 - u) > (2*uperiod/3)) {
1247 if((u - U0) > (2*uperiod/3)) {
1252 if((V0 - v) > (vperiod/2)) {
1256 if((v - V0) > (vperiod/2)) {
1261 U1 = U0 + usens*uperiod;
1262 V1 = V0 + vsens*vperiod;
1263 Pts2d(i).SetCoord(U1,V1);
1264 myProjIsDone = Standard_True;
1272 // -- Pnts2d is transformed into Geom2d_BSplineCurve, with the help of Param and Mult
1274 myBSpline = new Geom2d_BSplineCurve(Pts2d,Param,Mult,1);
1275 //jgv: put the curve into parametric range
1276 gp_Pnt2d MidPoint = myBSpline->Value(0.5*(myBSpline->FirstParameter() + myBSpline->LastParameter()));
1277 Standard_Real TestU = MidPoint.X(), TestV = MidPoint.Y();
1278 Standard_Real sense = 0.;
1281 if (TestU < Uinf - TolU)
1283 else if (TestU > Usup + TolU)
1285 while (TestU < Uinf - TolU || TestU > Usup + TolU)
1286 TestU += sense * uperiod;
1291 if (TestV < Vinf - TolV)
1293 else if (TestV > Vsup + TolV)
1295 while (TestV < Vinf - TolV || TestV > Vsup + TolV)
1296 TestV += sense * vperiod;
1298 gp_Vec2d Offset(TestU - MidPoint.X(), TestV - MidPoint.Y());
1299 if (Abs(Offset.X()) > gp::Resolution() ||
1300 Abs(Offset.Y()) > gp::Resolution())
1301 myBSpline->Translate(Offset);
1302 //////////////////////////////////////////
1303 Geom2dAdaptor_Curve GAC(myBSpline);
1304 Handle(Adaptor2d_HCurve2d) IC2d = new Geom2dAdaptor_HCurve(GAC);
1307 // sprintf(name,"%s_%d","build",compteur++);
1308 // DrawTrSurf::Set(name,myBSpline);
1313 // Modified by Sergey KHROMOV - Thu Apr 18 10:57:50 2002 Begin
1314 // Standard_NoSuchObject_Raise_if(1,"ProjLib_Compu: build echec");
1315 // Modified by Sergey KHROMOV - Thu Apr 18 10:57:51 2002 End
1316 return Handle(Adaptor2d_HCurve2d)();
1318 // myProjIsDone = Standard_False;
1319 // Modified by Sergey KHROMOV - Thu Apr 18 10:58:01 2002 Begin
1320 // Standard_NoSuchObject_Raise_if(1,"ProjLib_ComputeOnPS: build echec");
1321 // Modified by Sergey KHROMOV - Thu Apr 18 10:58:02 2002 End
1327 //=======================================================================
1328 //function : ProjLib_ProjectUsingInitialCurve2d
1330 //=======================================================================
1331 Handle(Geom2d_BSplineCurve)
1332 ProjLib_ComputeApproxOnPolarSurface::
1333 ProjectUsingInitialCurve2d(const Handle(Adaptor3d_HCurve)& Curve,
1334 const Handle(Adaptor3d_HSurface)& Surf,
1335 const Handle(Adaptor2d_HCurve2d)& InitCurve2d)
1338 Standard_Real Tol3d = myTolerance;
1339 Standard_Real DistTol3d = 1.0*Tol3d;
1340 Standard_Real TolU = Surf->UResolution(Tol3d), TolV = Surf->VResolution(Tol3d);
1341 Standard_Real Tol2d = Sqrt(TolU*TolU + TolV*TolV);
1344 GeomAbs_SurfaceType TheTypeS = Surf->GetType();
1345 GeomAbs_CurveType TheTypeC = Curve->GetType();
1346 // Handle(Standard_Type) TheTypeS = Surf->DynamicType();
1347 // Handle(Standard_Type) TheTypeC = Curve->DynamicType(); // si on a :
1348 // if(TheTypeS == STANDARD_TYPE(Geom_BSplineSurface)) {
1349 if(TheTypeS == GeomAbs_Plane) {
1351 gp_Pln Plane = Surf->Plane();
1352 if(TheTypeC == GeomAbs_BSplineCurve) {
1353 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
1354 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1355 for(i = 1;i <= Curve->NbPoles();i++) {
1356 ElSLib::Parameters( Plane, BSC->Pole(i), S, T);
1357 Poles2d(i).SetCoord(S,T);
1359 TColStd_Array1OfReal Knots(1, BSC->NbKnots());
1361 TColStd_Array1OfInteger Mults(1, BSC->NbKnots());
1362 BSC->Multiplicities(Mults);
1363 if(BSC->IsRational()) {
1364 TColStd_Array1OfReal Weights(1, BSC->NbPoles());
1365 BSC->Weights(Weights);
1366 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1367 BSC->Degree(), BSC->IsPeriodic()) ;
1369 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1370 BSC->Degree(), BSC->IsPeriodic()) ;
1373 if(TheTypeC == GeomAbs_BezierCurve) {
1374 Handle(Geom_BezierCurve) BC = Curve->Bezier();
1375 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1376 for(i = 1;i <= Curve->NbPoles();i++) {
1377 ElSLib::Parameters( Plane, BC->Pole(i), S, T);
1378 Poles2d(i).SetCoord(S,T);
1380 TColStd_Array1OfReal Knots(1, 2);
1381 Knots.SetValue(1,0.0);
1382 Knots.SetValue(2,1.0);
1383 TColStd_Array1OfInteger Mults(1, 2);
1384 Mults.Init(BC->NbPoles());
1385 if(BC->IsRational()) {
1386 TColStd_Array1OfReal Weights(1, BC->NbPoles());
1387 BC->Weights(Weights);
1388 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1389 BC->Degree(), BC->IsPeriodic()) ;
1391 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1392 BC->Degree(), BC->IsPeriodic()) ;
1395 if(TheTypeS == GeomAbs_BSplineSurface) {
1396 Handle(Geom_BSplineSurface) BSS = Surf->BSpline();
1397 if((BSS->MaxDegree() == 1) &&
1398 (BSS->NbUPoles() == 2) &&
1399 (BSS->NbVPoles() == 2)) {
1400 gp_Pnt p11 = BSS->Pole(1,1);
1401 gp_Pnt p12 = BSS->Pole(1,2);
1402 gp_Pnt p21 = BSS->Pole(2,1);
1403 gp_Pnt p22 = BSS->Pole(2,2);
1406 if(V1.IsEqual(V2,Tol3d,Tol3d/(p11.Distance(p12)*180/M_PI))){ //OCC217
1407 //if(V1.IsEqual(V2,myTolerance,myTolerance/(p11.Distance(p12)*180/M_PI))){
1408 // so the polar surface is plane
1409 // and if it is enough to projet the poles of Curve
1410 Standard_Integer Dist2Min = IntegerLast();
1413 //Standard_Real TolU = Surf->UResolution(myTolerance)
1414 // , TolV = Surf->VResolution(myTolerance);
1416 if(TheTypeC == GeomAbs_BSplineCurve) {
1417 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
1418 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1419 for(i = 1;i <= Curve->NbPoles();i++) {
1420 myProjIsDone = Standard_False;
1421 Dist2Min = IntegerLast();
1422 Extrema_GenLocateExtPS extrloc(BSC->Pole(i),Surf->Surface(),(p11.X()+p22.X())/2,
1423 (p11.Y()+p22.Y())/2,TolU,TolV) ;
1424 if (extrloc.IsDone()) {
1425 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1426 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1427 //if (Dist2Min < myTolerance * myTolerance) {
1428 (extrloc.Point()).Parameter(u,v);
1429 Poles2d(i).SetCoord(u,v);
1430 myProjIsDone = Standard_True;
1439 TColStd_Array1OfReal Knots(1, BSC->NbKnots());
1441 TColStd_Array1OfInteger Mults(1, BSC->NbKnots());
1442 BSC->Multiplicities(Mults);
1443 if(BSC->IsRational()) {
1444 TColStd_Array1OfReal Weights(1, BSC->NbPoles());
1445 BSC->Weights(Weights);
1446 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1447 BSC->Degree(), BSC->IsPeriodic()) ;
1449 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1450 BSC->Degree(), BSC->IsPeriodic()) ;
1455 if(TheTypeC == GeomAbs_BezierCurve) {
1456 Handle(Geom_BezierCurve) BC = Curve->Bezier();
1457 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1458 for(i = 1;i <= Curve->NbPoles();i++) {
1459 Dist2Min = IntegerLast();
1460 Extrema_GenLocateExtPS extrloc(BC->Pole(i),Surf->Surface(),0.5,
1462 if (extrloc.IsDone()) {
1463 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1464 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1465 //if (Dist2Min < myTolerance * myTolerance) {
1466 (extrloc.Point()).Parameter(u,v);
1467 Poles2d(i).SetCoord(u,v);
1468 myProjIsDone = Standard_True;
1474 myProjIsDone = Standard_False;
1478 TColStd_Array1OfReal Knots(1, 2);
1479 Knots.SetValue(1,0.0);
1480 Knots.SetValue(2,1.0);
1481 TColStd_Array1OfInteger Mults(1, 2);
1482 Mults.Init(BC->NbPoles());
1483 if(BC->IsRational()) {
1484 TColStd_Array1OfReal Weights(1, BC->NbPoles());
1485 BC->Weights(Weights);
1486 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1487 BC->Degree(), BC->IsPeriodic()) ;
1489 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1490 BC->Degree(), BC->IsPeriodic()) ;
1496 else if(TheTypeS == GeomAbs_BezierSurface) {
1497 Handle(Geom_BezierSurface) BS = Surf->Bezier();
1498 if((BS->MaxDegree() == 1) &&
1499 (BS->NbUPoles() == 2) &&
1500 (BS->NbVPoles() == 2)) {
1501 gp_Pnt p11 = BS->Pole(1,1);
1502 gp_Pnt p12 = BS->Pole(1,2);
1503 gp_Pnt p21 = BS->Pole(2,1);
1504 gp_Pnt p22 = BS->Pole(2,2);
1507 if(V1.IsEqual(V2,Tol3d,Tol3d/(p11.Distance(p12)*180/M_PI))){ //OCC217
1508 //if (V1.IsEqual(V2,myTolerance,myTolerance/(p11.Distance(p12)*180/M_PI))){
1509 // and if it is enough to project the poles of Curve
1510 Standard_Integer Dist2Min = IntegerLast();
1513 //Standard_Real TolU = Surf->UResolution(myTolerance)
1514 // , TolV = Surf->VResolution(myTolerance);
1517 if(TheTypeC == GeomAbs_BSplineCurve) {
1518 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
1519 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1520 for(i = 1;i <= Curve->NbPoles();i++) {
1521 myProjIsDone = Standard_False;
1522 Dist2Min = IntegerLast();
1523 Extrema_GenLocateExtPS extrloc(BSC->Pole(i),Surf->Surface(),(p11.X()+p22.X())/2,
1524 (p11.Y()+p22.Y())/2,TolU,TolV) ;
1525 if (extrloc.IsDone()) {
1526 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1527 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1528 //if (Dist2Min < myTolerance * myTolerance) {
1529 (extrloc.Point()).Parameter(u,v);
1530 Poles2d(i).SetCoord(u,v);
1531 myProjIsDone = Standard_True;
1540 TColStd_Array1OfReal Knots(1, BSC->NbKnots());
1542 TColStd_Array1OfInteger Mults(1, BSC->NbKnots());
1543 BSC->Multiplicities(Mults);
1544 if(BSC->IsRational()) {
1545 TColStd_Array1OfReal Weights(1, BSC->NbPoles());
1546 BSC->Weights(Weights);
1547 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1548 BSC->Degree(), BSC->IsPeriodic()) ;
1550 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1551 BSC->Degree(), BSC->IsPeriodic()) ;
1556 if(TheTypeC == GeomAbs_BezierCurve) {
1557 Handle(Geom_BezierCurve) BC = Curve->Bezier();
1558 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1559 for(i = 1;i <= Curve->NbPoles();i++) {
1560 Dist2Min = IntegerLast();
1561 Extrema_GenLocateExtPS extrloc(BC->Pole(i),Surf->Surface(),0.5,
1563 if (extrloc.IsDone()) {
1564 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1565 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1566 //if (Dist2Min < myTolerance * myTolerance) {
1567 (extrloc.Point()).Parameter(u,v);
1568 Poles2d(i).SetCoord(u,v);
1569 myProjIsDone = Standard_True;
1575 myProjIsDone = Standard_False;
1579 TColStd_Array1OfReal Knots(1, 2);
1580 Knots.SetValue(1,0.0);
1581 Knots.SetValue(2,1.0);
1582 TColStd_Array1OfInteger Mults(1, 2);
1583 Mults.Init(BC->NbPoles());
1584 if(BC->IsRational()) {
1585 TColStd_Array1OfReal Weights(1, BC->NbPoles());
1586 BC->Weights(Weights);
1587 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1588 BC->Degree(), BC->IsPeriodic()) ;
1590 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1591 BC->Degree(), BC->IsPeriodic()) ;
1598 ProjLib_PolarFunction F(Curve, Surf, InitCurve2d, Tol3d) ; //OCC217
1599 //ProjLib_PolarFunction F(Curve, Surf, InitCurve2d, myTolerance) ;
1602 Standard_Integer Nb = 50;
1604 Standard_Real U, U1, U2;
1605 U1 = F.FirstParameter();
1606 U2 = F.LastParameter();
1608 TColgp_Array1OfPnt2d DummyPoles(1,Nb+1);
1609 TColStd_Array1OfReal DummyKnots(1,Nb+1);
1610 TColStd_Array1OfInteger DummyMults(1,Nb+1);
1613 DummyMults(Nb+1) = 2;
1614 for (Standard_Integer ij = 0; ij <= Nb; ij++) {
1615 U = (Nb-ij)*U1 + ij*U2;
1617 DummyPoles(ij+1) = F.Value(U);
1618 DummyKnots(ij+1) = ij;
1620 Handle(Geom2d_BSplineCurve) DummyC2d =
1621 new Geom2d_BSplineCurve(DummyPoles, DummyKnots, DummyMults, 1);
1623 Standard_CString Temp = "bs2d";
1624 DrawTrSurf::Set(Temp,DummyC2d);
1626 // DrawTrSurf::Set((Standard_CString ) "bs2d",DummyC2d);
1627 Handle(Geom2dAdaptor_HCurve) DDD =
1628 Handle(Geom2dAdaptor_HCurve)::DownCast(InitCurve2d);
1632 DrawTrSurf::Set(Temp,DDD->ChangeCurve2d().Curve());
1634 // DrawTrSurf::Set((Standard_CString ) "initc2d",DDD->ChangeCurve2d().Curve());
1637 Standard_Integer Deg1,Deg2;
1643 Approx_FitAndDivide2d Fit(F,Deg1,Deg2,Tol3d,Tol2d, //OCC217
1644 //Approx_FitAndDivide2d Fit(F,Deg1,Deg2,myTolerance,myTolerance,
1647 if(Fit.IsAllApproximated()) {
1649 Standard_Integer NbCurves = Fit.NbMultiCurves();
1650 Standard_Integer MaxDeg = 0;
1651 // To transform the MultiCurve into BSpline, it is required that all
1652 // Bezier constituing it have the same degree -> Calculation of MaxDeg
1653 Standard_Integer NbPoles = 1;
1654 for (i = 1; i <= NbCurves; i++) {
1655 Standard_Integer Deg = Fit.Value(i).Degree();
1656 MaxDeg = Max ( MaxDeg, Deg);
1659 NbPoles = MaxDeg * NbCurves + 1; //Tops on the BSpline
1660 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
1662 TColgp_Array1OfPnt2d TempPoles( 1, MaxDeg + 1);//to augment the degree
1664 TColStd_Array1OfReal Knots( 1, NbCurves + 1); //Nodes of the BSpline
1666 Standard_Integer Compt = 1;
1667 for (i = 1; i <= NbCurves; i++) {
1668 Fit.Parameters(i, Knots(i), Knots(i+1));
1669 AppParCurves_MultiCurve MC = Fit.Value( i); //Load the Ith Curve
1670 TColgp_Array1OfPnt2d Poles2d( 1, MC.Degree() + 1);//Retrieve the tops
1671 MC.Curve(1, Poles2d);
1673 //Eventual augmentation of the degree
1674 Standard_Integer Inc = MaxDeg - MC.Degree();
1676 // BSplCLib::IncreaseDegree( Inc, Poles2d, PLib::NoWeights(),
1677 BSplCLib::IncreaseDegree( MaxDeg, Poles2d, PLib::NoWeights(),
1678 TempPoles, PLib::NoWeights());
1679 //update of tops of the PCurve
1680 for (Standard_Integer j = 1 ; j <= MaxDeg + 1; j++) {
1681 Poles.SetValue( Compt, TempPoles( j));
1686 //update of tops of the PCurve
1687 for (Standard_Integer j = 1 ; j <= MaxDeg + 1; j++) {
1688 Poles.SetValue( Compt, Poles2d( j));
1696 //update of fields of ProjLib_Approx
1697 Standard_Integer NbKnots = NbCurves + 1;
1699 // The start and end nodes are not correct : Cf: opening of the interval
1700 //Knots( 1) -= 1.e-9;
1701 //Knots(NbKnots) += 1.e-9;
1704 TColStd_Array1OfInteger Mults( 1, NbKnots);
1706 Mults.SetValue( 1, MaxDeg + 1);
1707 Mults.SetValue(NbKnots, MaxDeg + 1);
1708 myProjIsDone = Standard_True;
1709 Handle(Geom2d_BSplineCurve) Dummy =
1710 new Geom2d_BSplineCurve(Poles,Knots,Mults,MaxDeg);
1712 // try to smoother the Curve GeomAbs_C1.
1714 Standard_Boolean OK = Standard_True;
1716 for (Standard_Integer ij = 2; ij < NbKnots; ij++) {
1717 OK = OK && Dummy->RemoveKnot(ij,MaxDeg-1,Tol3d); //OCC217
1718 //OK = OK && Dummy->RemoveKnot(ij,MaxDeg-1,myTolerance);
1722 cout << "ProjLib_ComputeApproxOnPolarSurface : Smoothing echoue"<<endl;
1727 return Handle(Geom2d_BSplineCurve)();
1730 //=======================================================================
1731 //function : BSpline
1733 //=======================================================================
1735 Handle(Geom2d_BSplineCurve)
1736 ProjLib_ComputeApproxOnPolarSurface::BSpline() const
1739 // Modified by Sergey KHROMOV - Thu Apr 18 11:16:46 2002 End
1740 // Standard_NoSuchObject_Raise_if
1742 // "ProjLib_ComputeApproxOnPolarSurface:BSpline");
1743 // Modified by Sergey KHROMOV - Thu Apr 18 11:16:47 2002 End
1747 //=======================================================================
1748 //function : Curve2d
1750 //=======================================================================
1752 Handle(Geom2d_Curve)
1753 ProjLib_ComputeApproxOnPolarSurface::Curve2d() const
1756 Standard_NoSuchObject_Raise_if
1758 "ProjLib_ComputeApproxOnPolarSurface:2ndCurve2d");
1763 //=======================================================================
1766 //=======================================================================
1768 Standard_Boolean ProjLib_ComputeApproxOnPolarSurface::IsDone() const
1771 return myProjIsDone;