1 // Created by: Bruno DUMORTIER
2 // Copyright (c) 1995-1999 Matra Datavision
3 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 #include <ProjLib_ComputeApproxOnPolarSurface.hxx>
21 #include <AppCont_Function2d.hxx>
24 #include <BSplCLib.hxx>
26 #include <Standard_NoSuchObject.hxx>
27 #include <Geom_UndefinedDerivative.hxx>
28 #include <gp_Trsf.hxx>
29 #include <Precision.hxx>
30 #include <Approx_FitAndDivide2d.hxx>
32 #include <AppParCurves_MultiCurve.hxx>
33 #include <Geom_Surface.hxx>
34 #include <Geom2d_BSplineCurve.hxx>
35 #include <Geom2d_BezierCurve.hxx>
36 #include <Geom2d_Line.hxx>
37 #include <Geom2d_Circle.hxx>
38 #include <Geom2d_Ellipse.hxx>
39 #include <Geom2d_Hyperbola.hxx>
40 #include <Geom2d_Parabola.hxx>
41 #include <Geom2d_TrimmedCurve.hxx>
42 #include <Geom_BSplineSurface.hxx>
43 #include <Geom_BezierSurface.hxx>
44 #include <Geom_BSplineCurve.hxx>
45 #include <Geom_BezierCurve.hxx>
46 #include <Geom_TrimmedCurve.hxx>
48 #include <TColgp_Array1OfPnt2d.hxx>
49 #include <TColgp_Array2OfPnt2d.hxx>
50 #include <TColgp_Array1OfPnt.hxx>
51 #include <TColgp_SequenceOfPnt2d.hxx>
52 #include <TColStd_Array1OfReal.hxx>
53 #include <TColStd_Array1OfInteger.hxx>
54 #include <TColStd_SequenceOfReal.hxx>
55 #include <TColStd_ListOfTransient.hxx>
57 #include <GeomAbs_SurfaceType.hxx>
58 #include <GeomAbs_CurveType.hxx>
59 #include <Handle_Adaptor3d_HCurve.hxx>
60 #include <Handle_Adaptor3d_HSurface.hxx>
61 #include <Adaptor3d_Surface.hxx>
62 #include <Adaptor3d_Curve.hxx>
63 #include <Adaptor3d_HSurface.hxx>
64 #include <Adaptor3d_HCurve.hxx>
65 #include <Adaptor2d_HCurve2d.hxx>
66 #include <Geom2dAdaptor_Curve.hxx>
67 #include <Geom2dAdaptor_HCurve.hxx>
68 #include <GeomAdaptor_HCurve.hxx>
69 #include <GeomAdaptor.hxx>
70 #include <GeomAdaptor_Surface.hxx>
71 #include <TColgp_SequenceOfPnt.hxx>
74 #include <gp_Pnt2d.hxx>
75 #include <gp_Vec2d.hxx>
76 #include <Extrema_GenLocateExtPS.hxx>
77 #include <Extrema_ExtPS.hxx>
78 #include <GCPnts_QuasiUniformAbscissa.hxx>
79 #include <Standard_DomainError.hxx>
80 //#include <GeomLib_IsIso.hxx>
81 //#include <GeomLib_CheckSameParameter.hxx>
85 #include <DrawTrSurf.hxx>
87 //static Standard_Integer compteur = 0;
90 //=======================================================================
92 //purpose : (OCC217 - apo)- Compute Point2d that project on polar surface(<Surf>) 3D<Curve>
93 // <InitCurve2d> use for calculate start 2D point.
94 //=======================================================================
96 static gp_Pnt2d Function_Value(const Standard_Real U,
97 const Handle(Adaptor3d_HSurface)& Surf,
98 const Handle(Adaptor3d_HCurve)& Curve,
99 const Handle(Adaptor2d_HCurve2d)& InitCurve2d,
101 const Standard_Real DistTol3d, const Standard_Real tolU, const Standard_Real tolV)
102 //const Standard_Real Tolerance)
105 //Standard_Real Tol3d = 100*Tolerance;
107 gp_Pnt2d p2d = InitCurve2d->Value(U) ;
108 gp_Pnt p = Curve->Value(U);
110 Standard_Real Uinf, Usup, Vinf, Vsup;
111 Uinf = Surf->Surface().FirstUParameter();
112 Usup = Surf->Surface().LastUParameter();
113 Vinf = Surf->Surface().FirstVParameter();
114 Vsup = Surf->Surface().LastVParameter();
115 Standard_Integer decalU = 0, decalV = 0;
116 Standard_Real U0 = p2d.X(), V0 = p2d.Y();
118 GeomAbs_SurfaceType Type = Surf->GetType();
119 if((Type != GeomAbs_BSplineSurface) &&
120 (Type != GeomAbs_BezierSurface) &&
121 (Type != GeomAbs_OffsetSurface) ) {
124 // case GeomAbs_Plane:
126 // gp_Pln Plane = Surf->Plane();
127 // ElSLib::Parameters( Plane, p, S, T);
130 case GeomAbs_Cylinder:
132 gp_Cylinder Cylinder = Surf->Cylinder();
133 ElSLib::Parameters( Cylinder, p, S, T);
134 if(U0 < Uinf) decalU = -int((Uinf - U0)/(2*M_PI))-1;
135 if(U0 > Usup) decalU = int((U0 - Usup)/(2*M_PI))+1;
141 gp_Cone Cone = Surf->Cone();
142 ElSLib::Parameters( Cone, p, S, T);
143 if(U0 < Uinf) decalU = -int((Uinf - U0)/(2*M_PI))-1;
144 if(U0 > Usup) decalU = int((U0 - Usup)/(2*M_PI))+1;
150 gp_Sphere Sphere = Surf->Sphere();
151 ElSLib::Parameters( Sphere, p, S, T);
152 if(U0 < Uinf) decalU = -int((Uinf - U0)/(2*M_PI))-1;
153 if(U0 > Usup) decalU = int((U0 - Usup)/(2*M_PI))+1;
155 if(V0 < Vinf) decalV = -int((Vinf - V0)/(2*M_PI))-1;
156 if(V0 > (Vsup+(Vsup-Vinf))) decalV = int((V0 - Vsup+(Vsup-Vinf))/(2*M_PI))+1;
158 if(0.4*M_PI < Abs(U0 - S) && Abs(U0 - S) < 1.6*M_PI) {
169 gp_Torus Torus = Surf->Torus();
170 ElSLib::Parameters( Torus, p, S, T);
171 if(U0 < Uinf) decalU = -int((Uinf - U0)/(2*M_PI))-1;
172 if(U0 > Usup) decalU = int((U0 - Usup)/(2*M_PI))+1;
173 if(V0 < Vinf) decalV = -int((Vinf - V0)/(2*M_PI))-1;
174 if(V0 > Vsup) decalV = int((V0 - Vsup)/(2*M_PI))+1;
175 S += decalU*2*M_PI; T += decalV*2*M_PI;
179 Standard_NoSuchObject::Raise("ProjLib_ComputeApproxOnPolarSurface::Value");
181 return gp_Pnt2d(S, T);
185 Standard_Real Dist2Min = RealLast();
187 //Standard_Real tolU,tolV ;
191 Standard_Real uperiod =0, vperiod = 0, u, v;
192 // U0 and V0 are the points within the initialized period
193 // (periode with u and v),
194 // U1 and V1 are the points for construction of tops
196 if(Surf->IsUPeriodic() || Surf->IsUClosed()) {
197 uperiod = Surf->LastUParameter() - Surf->FirstUParameter();
199 if(Surf->IsVPeriodic() || Surf->IsVClosed()) {
200 vperiod = Surf->LastVParameter() - Surf->FirstVParameter();
206 decalU = int((Uinf - U0)/uperiod)+1;
207 U0 += decalU*uperiod;
213 decalU = -(int((U0 - Usup)/uperiod)+1);
214 U0 += decalU*uperiod;
220 decalV = int((Vinf - V0)/vperiod)+1;
221 V0 += decalV*vperiod;
227 decalV = -int((V0 - Vsup)/vperiod)-1;
228 V0 += decalV*vperiod;
231 // The surface around U0 is reduced
232 Standard_Real uLittle = (Usup - Uinf)/10, vLittle = (Vsup - Vinf)/10;
233 Standard_Real uInfLi = 0, vInfLi = 0,uSupLi = 0, vSupLi = 0;
234 if((U0 - Uinf) > uLittle) uInfLi = U0 - uLittle; else uInfLi = Uinf;
235 if((V0 - Vinf) > vLittle) vInfLi = V0 - vLittle; else vInfLi = Vinf;
236 if((Usup - U0) > uLittle) uSupLi = U0 + uLittle; else uSupLi = Usup;
237 if((Vsup - V0) > vLittle) vSupLi = V0 + vLittle; else vSupLi = Vsup;
239 // const Adaptor3d_Surface GAS = Surf->Surface();
241 GeomAdaptor_Surface SurfLittle;
242 if (Type == GeomAbs_BSplineSurface) {
243 Handle(Geom_Surface) GBSS(Surf->Surface().BSpline());
244 SurfLittle.Load(GBSS, uInfLi, uSupLi, vInfLi, vSupLi);
246 else if (Type == GeomAbs_BezierSurface) {
247 Handle(Geom_Surface) GS(Surf->Surface().Bezier());
248 SurfLittle.Load(GS, uInfLi, uSupLi, vInfLi, vSupLi);
250 else if (Type == GeomAbs_OffsetSurface) {
251 Handle(Geom_Surface) GS = GeomAdaptor::MakeSurface(Surf->Surface());
252 SurfLittle.Load(GS, uInfLi, uSupLi, vInfLi, vSupLi);
255 Standard_NoSuchObject::Raise("");
258 Extrema_GenLocateExtPS locext(p, SurfLittle, U0, V0, tolU, tolV);
259 if (locext.IsDone()) {
260 Dist2Min = locext.SquareDistance();
261 if (Dist2Min < DistTol3d * DistTol3d) {
262 (locext.Point()).Parameter(u, v);
263 gp_Pnt2d pnt(u - decalU*uperiod,v - decalV*vperiod);
268 Extrema_ExtPS ext(p, SurfLittle, tolU, tolV) ;
269 if (ext.IsDone() && ext.NbExt()>=1 ) {
270 Dist2Min = ext.SquareDistance(1);
271 Standard_Integer GoodValue = 1;
272 for ( Standard_Integer i = 2 ; i <= ext.NbExt() ; i++ )
273 if( Dist2Min > ext.SquareDistance(i)) {
274 Dist2Min = ext.SquareDistance(i);
277 if (Dist2Min < DistTol3d * DistTol3d) {
278 (ext.Point(GoodValue)).Parameter(u,v);
279 gp_Pnt2d pnt(u - decalU*uperiod,v - decalV*vperiod);
288 //=======================================================================
289 //function : ProjLib_PolarFunction
290 //purpose : (OCC217 - apo)- This class produce interface to call "gp_Pnt2d Function_Value(...)"
291 //=======================================================================
293 class ProjLib_PolarFunction : public AppCont_Function2d
295 Handle(Adaptor3d_HCurve) myCurve;
296 Handle(Adaptor2d_HCurve2d) myInitialCurve2d ;
297 Handle(Adaptor3d_HSurface) mySurface ;
299 Standard_Real myTolU, myTolV;
300 Standard_Real myDistTol3d;
301 //Standard_Real myTolerance ;
305 ProjLib_PolarFunction(const Handle(Adaptor3d_HCurve) & C,
306 const Handle(Adaptor3d_HSurface)& Surf,
307 const Handle(Adaptor2d_HCurve2d)& InitialCurve2d,
309 const Standard_Real Tol3d) :
310 //const Standard_Real Tolerance) :
312 myInitialCurve2d(InitialCurve2d),
315 myTolU(Surf->UResolution(Tol3d)),
316 myTolV(Surf->VResolution(Tol3d)),
317 myDistTol3d(100.0*Tol3d){} ;
318 //myTolerance(Tolerance){} ;
320 ~ProjLib_PolarFunction() {}
322 Standard_Real FirstParameter() const
323 {return (myCurve->FirstParameter()/*+1.e-9*/);}
325 Standard_Real LastParameter() const
326 {return (myCurve->LastParameter()/*-1.e-9*/);}
328 gp_Pnt2d Value(const Standard_Real t) const {
329 return Function_Value
330 (t,mySurface,myCurve,myInitialCurve2d,myDistTol3d,myTolU,myTolV) ; //OCC217
331 //(t,mySurface,myCurve,myInitialCurve2d,myTolerance) ;
334 // Standard_Boolean D1(const Standard_Real t, gp_Pnt2d& P, gp_Vec2d& V) const
335 Standard_Boolean D1(const Standard_Real , gp_Pnt2d& , gp_Vec2d& ) const
336 {return Standard_False;}
339 //=======================================================================
340 //function : ProjLib_ComputeApproxOnPolarSurface
342 //=======================================================================
344 ProjLib_ComputeApproxOnPolarSurface::ProjLib_ComputeApproxOnPolarSurface(){}
347 //=======================================================================
348 //function : ProjLib_ComputeApproxOnPolarSurface
350 //=======================================================================
352 ProjLib_ComputeApproxOnPolarSurface::ProjLib_ComputeApproxOnPolarSurface
353 (const Handle(Adaptor2d_HCurve2d)& InitialCurve2d,
354 const Handle(Adaptor3d_HCurve)& Curve,
355 const Handle(Adaptor3d_HSurface)& S,
356 const Standard_Real tol3d):myProjIsDone(Standard_False) //OCC217
357 //const Standard_Real tolerance):myProjIsDone(Standard_False)
359 myTolerance = tol3d; //OCC217
360 //myTolerance = Max(Tolerance,Precision::PApproximation());
361 myBSpline = Perform(InitialCurve2d,Curve,S);
363 //=======================================================================
364 //function : ProjLib_ComputeApproxOnPolarSurface
365 //purpose : Process the case of sewing
366 //=======================================================================
368 ProjLib_ComputeApproxOnPolarSurface::ProjLib_ComputeApproxOnPolarSurface
369 (const Handle(Adaptor2d_HCurve2d)& InitialCurve2d,
370 const Handle(Adaptor2d_HCurve2d)& InitialCurve2dBis,
371 const Handle(Adaptor3d_HCurve)& Curve,
372 const Handle(Adaptor3d_HSurface)& S,
373 const Standard_Real tol3d):myProjIsDone(Standard_False) //OCC217
374 //const Standard_Real tolerance):myProjIsDone(Standard_False)
375 {// InitialCurve2d and InitialCurve2dBis are two pcurves of the sewing
376 myTolerance = tol3d; //OCC217
377 //myTolerance = Max(tolerance,Precision::PApproximation());
378 Handle(Geom2d_BSplineCurve) bsc = Perform(InitialCurve2d,Curve,S);
380 gp_Pnt2d P2dproj, P2d, P2dBis;
381 P2dproj = bsc->StartPoint();
382 P2d = InitialCurve2d->Value(InitialCurve2d->FirstParameter());
383 P2dBis = InitialCurve2dBis->Value(InitialCurve2dBis->FirstParameter());
385 Standard_Real Dist, DistBis;
386 Dist = P2dproj.Distance(P2d);
387 DistBis = P2dproj.Distance(P2dBis);
388 if( Dist < DistBis) {
389 // myBSpline2d is the pcurve that is found. It is translated to obtain myCurve2d
391 Handle(Geom2d_Geometry) GG = myBSpline->Translated(P2d, P2dBis);
392 my2ndCurve = Handle(Geom2d_Curve)::DownCast(GG);
396 Handle(Geom2d_Geometry) GG = my2ndCurve->Translated(P2dBis, P2d);
397 myBSpline = Handle(Geom2d_BSplineCurve)::DownCast(GG);
402 //=======================================================================
403 //function : ProjLib_ComputeApproxOnPolarSurface
404 //purpose : case without curve of initialization
405 //=======================================================================
407 ProjLib_ComputeApproxOnPolarSurface::ProjLib_ComputeApproxOnPolarSurface
408 (const Handle(Adaptor3d_HCurve)& Curve,
409 const Handle(Adaptor3d_HSurface)& S,
410 const Standard_Real tol3d):myProjIsDone(Standard_False) //OCC217
411 //const Standard_Real tolerance):myProjIsDone(Standard_False)
413 myTolerance = tol3d; //OCC217
414 //myTolerance = Max(tolerance,Precision::PApproximation());
415 const Handle_Adaptor2d_HCurve2d InitCurve2d ;
416 myBSpline = Perform(InitCurve2d,Curve,S);
419 static Handle(Geom2d_BSplineCurve) Concat(Handle(Geom2d_BSplineCurve) C1,
420 Handle(Geom2d_BSplineCurve) C2)
422 Standard_Integer deg, deg1, deg2;
427 C1->IncreaseDegree(deg2);
430 else if ( deg2 < deg1) {
431 C2->IncreaseDegree(deg1);
436 Standard_Integer np1,np2,nk1,nk2,np,nk;
444 TColStd_Array1OfReal K1(1,nk1); C1->Knots(K1);
445 TColStd_Array1OfInteger M1(1,nk1); C1->Multiplicities(M1);
446 TColgp_Array1OfPnt2d P1(1,np1); C1->Poles(P1);
447 TColStd_Array1OfReal K2(1,nk2); C2->Knots(K2);
448 TColStd_Array1OfInteger M2(1,nk2); C2->Multiplicities(M2);
449 TColgp_Array1OfPnt2d P2(1,np2); C2->Poles(P2);
451 // Compute the new BSplineCurve
452 TColStd_Array1OfReal K(1,nk);
453 TColStd_Array1OfInteger M(1,nk);
454 TColgp_Array1OfPnt2d P(1,np);
456 Standard_Integer i, count = 0;
457 // Set Knots and Mults
458 for ( i = 1; i <= nk1; i++) {
464 for ( i = 2; i <= nk2; i++) {
471 for (i = 1; i <= np1; i++) {
475 for (i = 2; i <= np2; i++) {
480 Handle(Geom2d_BSplineCurve) BS =
481 new Geom2d_BSplineCurve(P,K,M,deg);
486 //=======================================================================
489 //=======================================================================
490 Handle(Geom2d_BSplineCurve) ProjLib_ComputeApproxOnPolarSurface::Perform
491 (const Handle(Adaptor2d_HCurve2d)& InitialCurve2d,
492 const Handle(Adaptor3d_HCurve)& Curve,
493 const Handle(Adaptor3d_HSurface)& S)
496 Standard_Real Tol3d = myTolerance;
497 Standard_Real ParamTol = Precision::PApproximation();
499 Handle(Adaptor2d_HCurve2d) AHC2d = InitialCurve2d;
500 Handle(Adaptor3d_HCurve) AHC = Curve;
502 // if the curve 3d is a BSpline with degree C0, it is cut into sections with degree C1
504 GeomAbs_CurveType typeCurve = Curve->GetType();
505 if(typeCurve == GeomAbs_BSplineCurve) {
506 TColStd_ListOfTransient LOfBSpline2d;
507 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
508 Standard_Integer nbInter = Curve->NbIntervals(GeomAbs_C1);
510 Standard_Integer i, j;
511 Handle(Geom_TrimmedCurve) GTC;
512 Handle(Geom2d_TrimmedCurve) G2dTC;
513 TColStd_Array1OfReal Inter(1,nbInter+1);
514 Curve->Intervals(Inter,GeomAbs_C1);
515 Standard_Real firstinter = Inter.Value(1), secondinter = Inter.Value(2);
517 GTC = new Geom_TrimmedCurve(BSC, firstinter, secondinter);
518 AHC = new GeomAdaptor_HCurve(GTC);
520 // if there is an initialization curve:
521 // - either this is a BSpline C0, with discontinuity at the same parameters of nodes
522 // and the sections C1 are taken
523 // - or this is a curve C1 and the sections of intrest are taken otherwise the curve is created.
526 Standard_Integer nbInter2d;
527 Standard_Boolean C2dIsToCompute;
528 C2dIsToCompute = InitialCurve2d.IsNull();
529 Handle(Geom2d_BSplineCurve) BSC2d;
530 Handle(Geom2d_Curve) G2dC;
532 if(!C2dIsToCompute) {
533 nbInter2d = InitialCurve2d->NbIntervals(GeomAbs_C1);
534 TColStd_Array1OfReal Inter2d(1,nbInter2d+1);
535 InitialCurve2d->Intervals(Inter2d,GeomAbs_C1);
537 for(i = 1,j = 1;i <= nbInter;i++)
538 if(Abs(Inter.Value(i) - Inter2d.Value(j)) < ParamTol) { //OCC217
539 //if(Abs(Inter.Value(i) - Inter2d.Value(j)) < myTolerance) {
540 if (j > nbInter2d) break;
543 if(j != (nbInter2d+1)) {
544 C2dIsToCompute = Standard_True;
549 AHC2d = BuildInitialCurve2d(AHC, S);
552 typeCurve = InitialCurve2d->GetType();
555 G2dC = new Geom2d_Line(InitialCurve2d->Line());
558 case GeomAbs_Circle: {
559 G2dC = new Geom2d_Circle(InitialCurve2d->Circle());
562 case GeomAbs_Ellipse: {
563 G2dC = new Geom2d_Ellipse(InitialCurve2d->Ellipse());
566 case GeomAbs_Hyperbola: {
567 G2dC = new Geom2d_Hyperbola(InitialCurve2d->Hyperbola());
570 case GeomAbs_Parabola: {
571 G2dC = new Geom2d_Parabola(InitialCurve2d->Parabola());
574 case GeomAbs_BezierCurve: {
575 G2dC = InitialCurve2d->Bezier();
578 case GeomAbs_BSplineCurve: {
579 G2dC = InitialCurve2d->BSpline();
582 case GeomAbs_OtherCurve:
586 gp_Pnt2d fp2d = G2dC->Value(firstinter), lp2d = G2dC->Value(secondinter);
587 gp_Pnt fps, lps, fpc, lpc;
588 S->D0(fp2d.X(), fp2d.Y(), fps);
589 S->D0(lp2d.X(), lp2d.Y(), lps);
590 Curve->D0(firstinter, fpc);
591 Curve->D0(secondinter, lpc);
593 if((fps.IsEqual(fpc, Tol3d)) &&
594 (lps.IsEqual(lpc, Tol3d))) {
595 //if((fps.IsEqual(fpc, myTolerance)) &&
596 // (lps.IsEqual(lpc, myTolerance))) {
597 G2dTC = new Geom2d_TrimmedCurve(G2dC, firstinter, secondinter);
598 Geom2dAdaptor_Curve G2dAC(G2dTC);
599 AHC2d = new Geom2dAdaptor_HCurve(G2dAC);
600 myProjIsDone = Standard_True;
603 AHC2d = BuildInitialCurve2d(AHC, S);
604 C2dIsToCompute = Standard_True;
609 BSC2d = ProjectUsingInitialCurve2d(AHC, S, AHC2d);
610 if(BSC2d.IsNull()) return Handle(Geom2d_BSplineCurve)(); //IFV
611 LOfBSpline2d.Append(BSC2d);
614 return Handle(Geom2d_BSplineCurve)();
619 Standard_Real iinter, ip1inter;
620 Standard_Integer nbK2d, deg;
621 nbK2d = BSC2d->NbKnots(); deg = BSC2d->Degree();
623 for(i = 2;i <= nbInter;i++) {
624 iinter = Inter.Value(i);
625 ip1inter = Inter.Value(i+1);
627 GTC->SetTrim(iinter, ip1inter);
628 AHC = new GeomAdaptor_HCurve(GTC);
632 AHC2d = BuildInitialCurve2d(AHC, S);
635 gp_Pnt2d fp2d = G2dC->Value(iinter), lp2d = G2dC->Value(ip1inter);
636 gp_Pnt fps, lps, fpc, lpc;
637 S->D0(fp2d.X(), fp2d.Y(), fps);
638 S->D0(lp2d.X(), lp2d.Y(), lps);
639 Curve->D0(iinter, fpc);
640 Curve->D0(ip1inter, lpc);
642 if((fps.IsEqual(fpc, Tol3d)) &&
643 (lps.IsEqual(lpc, Tol3d))) {
644 //if((fps.IsEqual(fpc, myTolerance)) &&
645 // (lps.IsEqual(lpc, myTolerance))) {
646 G2dTC->SetTrim(iinter, ip1inter);
647 Geom2dAdaptor_Curve G2dAC(G2dTC);
648 AHC2d = new Geom2dAdaptor_HCurve(G2dAC);
649 myProjIsDone = Standard_True;
652 AHC2d = BuildInitialCurve2d(AHC, S);
656 BSC2d = ProjectUsingInitialCurve2d(AHC, S, AHC2d);
658 return Handle(Geom2d_BSplineCurve)();
660 LOfBSpline2d.Append(BSC2d);
661 nbK2d += BSC2d->NbKnots() - 1;
662 deg = Max(deg, BSC2d->Degree());
665 return Handle(Geom2d_BSplineCurve)();
669 Standard_Integer NbC = LOfBSpline2d.Extent();
670 Handle(Geom2d_BSplineCurve) CurBS;
671 CurBS = Handle(Geom2d_BSplineCurve)::DownCast(LOfBSpline2d.First());
672 LOfBSpline2d.RemoveFirst();
673 for (Standard_Integer ii = 2; ii <= NbC; ii++) {
674 Handle(Geom2d_BSplineCurve) BS =
675 Handle(Geom2d_BSplineCurve)::DownCast(LOfBSpline2d.First());
676 CurBS = Concat(CurBS,BS);
677 LOfBSpline2d.RemoveFirst();
683 if(InitialCurve2d.IsNull()) {
684 AHC2d = BuildInitialCurve2d(Curve, S);
686 return Handle(Geom2d_BSplineCurve)();
688 return ProjectUsingInitialCurve2d(AHC, S, AHC2d);
691 //=======================================================================
692 //function : ProjLib_BuildInitialCurve2d
694 //=======================================================================
696 Handle(Adaptor2d_HCurve2d)
697 ProjLib_ComputeApproxOnPolarSurface::
698 BuildInitialCurve2d(const Handle(Adaptor3d_HCurve)& Curve,
699 const Handle(Adaptor3d_HSurface)& Surf)
701 // discretize the Curve with quasiuniform deflection
702 // density at least NbOfPnts points
703 myProjIsDone = Standard_False;
706 Standard_Real Tol3d = myTolerance;
707 Standard_Real TolU = Surf->UResolution(Tol3d), TolV = Surf->VResolution(Tol3d);
708 Standard_Real DistTol3d = 100.0*Tol3d;
710 // NO myTol is Tol2d !!!!
711 //Standard_Real TolU = myTolerance, TolV = myTolerance;
712 //Standard_Real Tol3d = 100*myTolerance; // At random Balthazar.
714 Standard_Integer NbOfPnts = 61;
715 GCPnts_QuasiUniformAbscissa QUA(Curve->GetCurve(),NbOfPnts);
716 TColgp_Array1OfPnt Pts(1,NbOfPnts);
717 TColStd_Array1OfReal Param(1,NbOfPnts);
718 Standard_Integer i, j;
719 for( i = 1; i <= NbOfPnts ; i++ ) {
720 Param(i) = QUA.Parameter(i);
721 Pts(i) = Curve->Value(Param(i));
724 TColgp_Array1OfPnt2d Pts2d(1,NbOfPnts);
725 TColStd_Array1OfInteger Mult(1,NbOfPnts);
727 Mult(1) = Mult(NbOfPnts) = 2;
729 Standard_Real Vinf, Vsup;
730 Vinf = Surf->Surface().FirstVParameter();
731 Vsup = Surf->Surface().LastVParameter();
732 GeomAbs_SurfaceType Type = Surf->GetType();
733 if((Type != GeomAbs_BSplineSurface) && (Type != GeomAbs_BezierSurface) &&
734 (Type != GeomAbs_OffsetSurface)) {
736 // Standard_Integer usens = 0, vsens = 0;
737 // to know the position relatively to the period
739 // case GeomAbs_Plane:
741 // gp_Pln Plane = Surf->Plane();
742 // for ( i = 1 ; i <= NbOfPnts ; i++) {
743 // ElSLib::Parameters( Plane, Pts(i), S, T);
744 // Pts2d(i).SetCoord(S,T);
746 // myProjIsDone = Standard_True;
749 case GeomAbs_Cylinder:
751 // Standard_Real Sloc, Tloc;
753 Standard_Integer usens = 0;
754 gp_Cylinder Cylinder = Surf->Cylinder();
755 ElSLib::Parameters( Cylinder, Pts(1), S, T);
756 Pts2d(1).SetCoord(S,T);
757 for ( i = 2 ; i <= NbOfPnts ; i++) {
759 ElSLib::Parameters( Cylinder, Pts(i), S, T);
760 if(Abs(Sloc - S) > M_PI)
765 Pts2d(i).SetCoord(S+usens*2*M_PI,T);
767 myProjIsDone = Standard_True;
772 // Standard_Real Sloc, Tloc;
774 Standard_Integer usens = 0;
775 gp_Cone Cone = Surf->Cone();
776 ElSLib::Parameters( Cone, Pts(1), S, T);
777 Pts2d(1).SetCoord(S,T);
778 for ( i = 2 ; i <= NbOfPnts ; i++) {
780 ElSLib::Parameters( Cone, Pts(i), S, T);
781 if(Abs(Sloc - S) > M_PI)
786 Pts2d(i).SetCoord(S+usens*2*M_PI,T);
788 myProjIsDone = Standard_True;
793 Standard_Real Sloc, Tloc;
794 Standard_Integer usens = 0, vsens = 0; //usens steps by half-period
795 Standard_Boolean vparit = Standard_False;
796 gp_Sphere Sphere = Surf->Sphere();
797 ElSLib::Parameters( Sphere, Pts(1), S, T);
798 Pts2d(1).SetCoord(S,T);
799 for ( i = 2 ; i <= NbOfPnts ; i++) {
801 ElSLib::Parameters( Sphere, Pts(i), S, T);
802 if(1.6*M_PI < Abs(Sloc - S))
807 if(1.6*M_PI > Abs(Sloc - S) && Abs(Sloc - S) > 0.4*M_PI) {
813 if(Abs(Tloc - Vsup) < (Vsup - Vinf)/5)
819 Pts2d(i).SetCoord(S+usens*M_PI,(M_PI - T)*(vsens-1));
822 Pts2d(i).SetCoord(S+usens*M_PI,T+vsens*M_PI);
826 myProjIsDone = Standard_True;
831 Standard_Real Sloc, Tloc;
832 Standard_Integer usens = 0, vsens = 0;
833 gp_Torus Torus = Surf->Torus();
834 ElSLib::Parameters( Torus, Pts(1), S, T);
835 Pts2d(1).SetCoord(S,T);
836 for ( i = 2 ; i <= NbOfPnts ; i++) {
838 ElSLib::Parameters( Torus, Pts(i), S, T);
839 if(Abs(Sloc - S) > M_PI)
844 if(Abs(Tloc - T) > M_PI)
849 Pts2d(i).SetCoord(S+usens*2*M_PI,T+vsens*2*M_PI);
851 myProjIsDone = Standard_True;
855 Standard_NoSuchObject::Raise("ProjLib_ComputeApproxOnPolarSurface::BuildInitialCurve2d");
859 Standard_Real Uinf = Surf->Surface().FirstUParameter();
860 Standard_Real Usup = Surf->Surface().LastUParameter();
862 myProjIsDone = Standard_False;
863 Standard_Real Dist2Min = 1.e+200, u = 0., v = 0.;
866 TColgp_SequenceOfPnt2d Sols;
867 Standard_Boolean areManyZeros = Standard_False;
869 Curve->D0(Param.Value(1), pntproj) ;
870 Extrema_ExtPS aExtPS(pntproj, Surf->Surface(), TolU, TolV) ;
872 if( aExtPS.IsDone() && aExtPS.NbExt() >= 1 ) {
874 Standard_Integer GoodValue = 1;
876 for ( i = 1 ; i <= aExtPS.NbExt() ; i++ ) {
877 if( aExtPS.SquareDistance(i) < DistTol3d * DistTol3d ) {
878 if( aExtPS.SquareDistance(i) <= 1.e-18 ) {
879 aExtPS.Point(i).Parameter(u,v);
881 Standard_Boolean isSame = Standard_False;
882 for( j = 1; j <= Sols.Length(); j++ ) {
883 if( p2d.SquareDistance( Sols.Value(j) ) <= 1.e-18 ) {
884 isSame = Standard_True;
888 if( !isSame ) Sols.Append( p2d );
890 if( Dist2Min > aExtPS.SquareDistance(i) ) {
891 Dist2Min = aExtPS.SquareDistance(i);
897 if( Sols.Length() > 1 ) areManyZeros = Standard_True;
899 if( Dist2Min <= DistTol3d * DistTol3d) {
900 if( !areManyZeros ) {
901 aExtPS.Point(GoodValue).Parameter(u,v);
902 Pts2d(1).SetCoord(u,v);
903 myProjIsDone = Standard_True;
906 Standard_Integer nbSols = Sols.Length();
907 Standard_Real Dist2Max = -1.e+200;
908 for( i = 1; i <= nbSols; i++ ) {
909 const gp_Pnt2d& aP1 = Sols.Value(i);
910 for( j = i+1; j <= nbSols; j++ ) {
911 const gp_Pnt2d& aP2 = Sols.Value(j);
912 Standard_Real aDist2 = aP1.SquareDistance(aP2);
913 if( aDist2 > Dist2Max ) Dist2Max = aDist2;
916 Standard_Real aMaxT2 = Max(TolU,TolV);
918 if( Dist2Max > aMaxT2 ) {
919 Standard_Integer tPp = 0;
920 for( i = 1; i <= 5; i++ ) {
921 Standard_Integer nbExtOk = 0;
922 Standard_Integer indExt = 0;
923 Standard_Integer iT = 1 + (NbOfPnts - 1)/5*i;
924 Curve->D0( Param.Value(iT), pntproj );
925 Extrema_ExtPS aTPS( pntproj, Surf->Surface(), TolU, TolV );
927 if( aTPS.IsDone() && aTPS.NbExt() >= 1 ) {
928 for( j = 1 ; j <= aTPS.NbExt() ; j++ ) {
929 if( aTPS.SquareDistance(j) < DistTol3d * DistTol3d ) {
931 if( aTPS.SquareDistance(j) < Dist2Min ) {
932 Dist2Min = aTPS.SquareDistance(j);
940 aTPS.Point(indExt).Parameter(u,v);
946 gp_Pnt2d aPp = gp_Pnt2d(u,v);
949 Standard_Boolean isFound = Standard_False;
951 Curve->D0( Param.Value(tPp+j), pntproj );
952 Extrema_ExtPS aTPS( pntproj, Surf->Surface(), TolU, TolV );
954 Standard_Integer indExt = 0;
955 if( aTPS.IsDone() && aTPS.NbExt() >= 1 ) {
956 for( i = 1 ; i <= aTPS.NbExt() ; i++ ) {
957 if( aTPS.SquareDistance(i) < DistTol3d * DistTol3d && aTPS.SquareDistance(i) < Dist2Min ) {
958 Dist2Min = aTPS.SquareDistance(i);
960 isFound = Standard_True;
965 aTPS.Point(indExt).Parameter(u,v);
970 if( (tPp+j) > NbOfPnts ) break;
974 gp_Vec2d atV(aPp,aPn);
975 Standard_Boolean isChosen = Standard_False;
976 for( i = 1; i <= nbSols; i++ ) {
977 const gp_Pnt2d& aP1 = Sols.Value(i);
978 gp_Vec2d asV(aP1,aPp);
979 if( asV.Dot(atV) > 0. ) {
980 isChosen = Standard_True;
981 Pts2d(1).SetCoord(aP1.X(),aP1.Y());
982 myProjIsDone = Standard_True;
987 aExtPS.Point(GoodValue).Parameter(u,v);
988 Pts2d(1).SetCoord(u,v);
989 myProjIsDone = Standard_True;
993 aExtPS.Point(GoodValue).Parameter(u,v);
994 Pts2d(1).SetCoord(u,v);
995 myProjIsDone = Standard_True;
999 aExtPS.Point(GoodValue).Parameter(u,v);
1000 Pts2d(1).SetCoord(u,v);
1001 myProjIsDone = Standard_True;
1005 aExtPS.Point(GoodValue).Parameter(u,v);
1006 Pts2d(1).SetCoord(u,v);
1007 myProjIsDone = Standard_True;
1012 // calculate the following points with GenLocate_ExtPS
1013 // (and store the result and each parameter in a sequence)
1014 Standard_Integer usens = 0, vsens = 0;
1015 // to know the position relatively to the period
1016 Standard_Real U0 = u, V0 = v, U1 = u, V1 = v, uperiod =0, vperiod = 0;
1017 // U0 and V0 are the points in the initialized period
1018 // (period with u and v),
1019 // U1 and V1 are the points for construction of poles
1022 if(Surf->IsUPeriodic() || Surf->IsUClosed()) {
1023 uperiod = Surf->LastUParameter() - Surf->FirstUParameter();
1026 if(Surf->IsVPeriodic() || Surf->IsVClosed()) {
1027 vperiod = Surf->LastVParameter() - Surf->FirstVParameter();
1030 for ( i = 2 ; i <= NbOfPnts ; i++)
1032 myProjIsDone = Standard_False;
1033 Dist2Min = RealLast();
1034 Curve->D0(Param.Value(i), pntproj);
1035 Extrema_GenLocateExtPS aLocateExtPS
1036 (pntproj, Surf->Surface(), U0, V0, TolU, TolV) ;
1038 if (aLocateExtPS.IsDone())
1039 if (aLocateExtPS.SquareDistance() < DistTol3d * DistTol3d) { //OCC217
1040 //if (aLocateExtPS.SquareDistance() < Tol3d * Tol3d) {
1041 (aLocateExtPS.Point()).Parameter(U0,V0);
1042 U1 = U0 + usens*uperiod;
1043 V1 = V0 + vsens*vperiod;
1044 Pts2d(i).SetCoord(U1,V1);
1045 myProjIsDone = Standard_True;
1047 if(!myProjIsDone && uperiod) {
1048 Standard_Real Uinf, Usup, Uaux;
1049 Uinf = Surf->Surface().FirstUParameter();
1050 Usup = Surf->Surface().LastUParameter();
1051 if((Usup - U0) > (U0 - Uinf))
1052 Uaux = 2*Uinf - U0 + uperiod;
1054 Uaux = 2*Usup - U0 - uperiod;
1055 Extrema_GenLocateExtPS locext(pntproj,
1057 Uaux, V0, TolU, TolV);
1058 if (locext.IsDone())
1059 if (locext.SquareDistance() < DistTol3d * DistTol3d) { //OCC217
1060 //if (locext.SquareDistance() < Tol3d * Tol3d) {
1061 (locext.Point()).Parameter(u,v);
1062 if((Usup - U0) > (U0 - Uinf))
1067 U1 = U0 + usens*uperiod;
1068 V1 = V0 + vsens*vperiod;
1069 Pts2d(i).SetCoord(U1,V1);
1070 myProjIsDone = Standard_True;
1073 if(!myProjIsDone && vperiod) {
1074 Standard_Real Vinf, Vsup, Vaux;
1075 Vinf = Surf->Surface().FirstVParameter();
1076 Vsup = Surf->Surface().LastVParameter();
1077 if((Vsup - V0) > (V0 - Vinf))
1078 Vaux = 2*Vinf - V0 + vperiod;
1080 Vaux = 2*Vsup - V0 - vperiod;
1081 Extrema_GenLocateExtPS locext(pntproj,
1083 U0, Vaux, TolU, TolV) ;
1084 if (locext.IsDone())
1085 if (locext.SquareDistance() < DistTol3d * DistTol3d) { //OCC217
1086 //if (locext.SquareDistance() < Tol3d * Tol3d) {
1087 (locext.Point()).Parameter(u,v);
1088 if((Vsup - V0) > (V0 - Vinf))
1093 U1 = U0 + usens*uperiod;
1094 V1 = V0 + vsens*vperiod;
1095 Pts2d(i).SetCoord(U1,V1);
1096 myProjIsDone = Standard_True;
1099 if(!myProjIsDone && uperiod && vperiod) {
1100 Standard_Real Uaux, Vaux;
1101 if((Usup - U0) > (U0 - Uinf))
1102 Uaux = 2*Uinf - U0 + uperiod;
1104 Uaux = 2*Usup - U0 - uperiod;
1105 if((Vsup - V0) > (V0 - Vinf))
1106 Vaux = 2*Vinf - V0 + vperiod;
1108 Vaux = 2*Vsup - V0 - vperiod;
1109 Extrema_GenLocateExtPS locext(pntproj,
1111 Uaux, Vaux, TolU, TolV);
1112 if (locext.IsDone())
1113 if (locext.SquareDistance() < DistTol3d * DistTol3d) {
1114 //if (locext.SquareDistance() < Tol3d * Tol3d) {
1115 (locext.Point()).Parameter(u,v);
1116 if((Usup - U0) > (U0 - Uinf))
1120 if((Vsup - V0) > (V0 - Vinf))
1125 U1 = U0 + usens*uperiod;
1126 V1 = V0 + vsens*vperiod;
1127 Pts2d(i).SetCoord(U1,V1);
1128 myProjIsDone = Standard_True;
1132 Extrema_ExtPS ext(pntproj, Surf->Surface(), TolU, TolV) ;
1134 Dist2Min = ext.SquareDistance(1);
1135 Standard_Integer GoodValue = 1;
1136 for ( j = 2 ; j <= ext.NbExt() ; j++ )
1137 if( Dist2Min > ext.SquareDistance(j)) {
1138 Dist2Min = ext.SquareDistance(j);
1141 if (Dist2Min < DistTol3d * DistTol3d) {
1142 //if (Dist2Min < Tol3d * Tol3d) {
1143 (ext.Point(GoodValue)).Parameter(u,v);
1145 if((U0 - u) > (2*uperiod/3)) {
1149 if((u - U0) > (2*uperiod/3)) {
1153 if((V0 - v) > (vperiod/2)) {
1157 if((v - V0) > (vperiod/2)) {
1161 U1 = U0 + usens*uperiod;
1162 V1 = V0 + vsens*vperiod;
1163 Pts2d(i).SetCoord(U1,V1);
1164 myProjIsDone = Standard_True;
1172 // -- Pnts2d is transformed into Geom2d_BSplineCurve, with the help of Param and Mult
1174 myBSpline = new Geom2d_BSplineCurve(Pts2d,Param,Mult,1);
1175 Geom2dAdaptor_Curve GAC(myBSpline);
1176 Handle(Adaptor2d_HCurve2d) IC2d = new Geom2dAdaptor_HCurve(GAC);
1179 // sprintf(name,"%s_%d","build",compteur++);
1180 // DrawTrSurf::Set(name,myBSpline);
1185 // Modified by Sergey KHROMOV - Thu Apr 18 10:57:50 2002 Begin
1186 // Standard_NoSuchObject_Raise_if(1,"ProjLib_Compu: build echec");
1187 // Modified by Sergey KHROMOV - Thu Apr 18 10:57:51 2002 End
1188 return Handle(Adaptor2d_HCurve2d)();
1190 myProjIsDone = Standard_False;
1191 // Modified by Sergey KHROMOV - Thu Apr 18 10:58:01 2002 Begin
1192 // Standard_NoSuchObject_Raise_if(1,"ProjLib_ComputeOnPS: build echec");
1193 // Modified by Sergey KHROMOV - Thu Apr 18 10:58:02 2002 End
1194 return Handle(Adaptor2d_HCurve2d)();
1200 //=======================================================================
1201 //function : ProjLib_ProjectUsingInitialCurve2d
1203 //=======================================================================
1204 Handle(Geom2d_BSplineCurve)
1205 ProjLib_ComputeApproxOnPolarSurface::
1206 ProjectUsingInitialCurve2d(const Handle(Adaptor3d_HCurve)& Curve,
1207 const Handle(Adaptor3d_HSurface)& Surf,
1208 const Handle(Adaptor2d_HCurve2d)& InitCurve2d)
1211 Standard_Real Tol3d = myTolerance;
1212 Standard_Real DistTol3d = 1.0*Tol3d;
1213 Standard_Real TolU = Surf->UResolution(Tol3d), TolV = Surf->VResolution(Tol3d);
1214 Standard_Real Tol2d = Sqrt(TolU*TolU + TolV*TolV);
1217 GeomAbs_SurfaceType TheTypeS = Surf->GetType();
1218 GeomAbs_CurveType TheTypeC = Curve->GetType();
1219 // Handle(Standard_Type) TheTypeS = Surf->DynamicType();
1220 // Handle(Standard_Type) TheTypeC = Curve->DynamicType(); // si on a :
1221 // if(TheTypeS == STANDARD_TYPE(Geom_BSplineSurface)) {
1222 if(TheTypeS == GeomAbs_Plane) {
1224 gp_Pln Plane = Surf->Plane();
1225 if(TheTypeC == GeomAbs_BSplineCurve) {
1226 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
1227 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1228 for(i = 1;i <= Curve->NbPoles();i++) {
1229 ElSLib::Parameters( Plane, BSC->Pole(i), S, T);
1230 Poles2d(i).SetCoord(S,T);
1232 TColStd_Array1OfReal Knots(1, BSC->NbKnots());
1234 TColStd_Array1OfInteger Mults(1, BSC->NbKnots());
1235 BSC->Multiplicities(Mults);
1236 if(BSC->IsRational()) {
1237 TColStd_Array1OfReal Weights(1, BSC->NbPoles());
1238 BSC->Weights(Weights);
1239 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1240 BSC->Degree(), BSC->IsPeriodic()) ;
1242 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1243 BSC->Degree(), BSC->IsPeriodic()) ;
1246 if(TheTypeC == GeomAbs_BezierCurve) {
1247 Handle(Geom_BezierCurve) BC = Curve->Bezier();
1248 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1249 for(i = 1;i <= Curve->NbPoles();i++) {
1250 ElSLib::Parameters( Plane, BC->Pole(i), S, T);
1251 Poles2d(i).SetCoord(S,T);
1253 TColStd_Array1OfReal Knots(1, 2);
1254 Knots.SetValue(1,0.0);
1255 Knots.SetValue(2,1.0);
1256 TColStd_Array1OfInteger Mults(1, 2);
1257 Mults.Init(BC->NbPoles());
1258 if(BC->IsRational()) {
1259 TColStd_Array1OfReal Weights(1, BC->NbPoles());
1260 BC->Weights(Weights);
1261 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1262 BC->Degree(), BC->IsPeriodic()) ;
1264 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1265 BC->Degree(), BC->IsPeriodic()) ;
1268 if(TheTypeS == GeomAbs_BSplineSurface) {
1269 Handle(Geom_BSplineSurface) BSS = Surf->BSpline();
1270 if((BSS->MaxDegree() == 1) &&
1271 (BSS->NbUPoles() == 2) &&
1272 (BSS->NbVPoles() == 2)) {
1273 gp_Pnt p11 = BSS->Pole(1,1);
1274 gp_Pnt p12 = BSS->Pole(1,2);
1275 gp_Pnt p21 = BSS->Pole(2,1);
1276 gp_Pnt p22 = BSS->Pole(2,2);
1279 if(V1.IsEqual(V2,Tol3d,Tol3d/(p11.Distance(p12)*180/M_PI))){ //OCC217
1280 //if(V1.IsEqual(V2,myTolerance,myTolerance/(p11.Distance(p12)*180/M_PI))){
1281 // so the polar surface is plane
1282 // and if it is enough to projet the poles of Curve
1283 Standard_Integer Dist2Min = IntegerLast();
1286 //Standard_Real TolU = Surf->UResolution(myTolerance)
1287 // , TolV = Surf->VResolution(myTolerance);
1289 if(TheTypeC == GeomAbs_BSplineCurve) {
1290 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
1291 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1292 for(i = 1;i <= Curve->NbPoles();i++) {
1293 myProjIsDone = Standard_False;
1294 Dist2Min = IntegerLast();
1295 Extrema_GenLocateExtPS extrloc(BSC->Pole(i),Surf->Surface(),(p11.X()+p22.X())/2,
1296 (p11.Y()+p22.Y())/2,TolU,TolV) ;
1297 if (extrloc.IsDone()) {
1298 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1299 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1300 //if (Dist2Min < myTolerance * myTolerance) {
1301 (extrloc.Point()).Parameter(u,v);
1302 Poles2d(i).SetCoord(u,v);
1303 myProjIsDone = Standard_True;
1312 TColStd_Array1OfReal Knots(1, BSC->NbKnots());
1314 TColStd_Array1OfInteger Mults(1, BSC->NbKnots());
1315 BSC->Multiplicities(Mults);
1316 if(BSC->IsRational()) {
1317 TColStd_Array1OfReal Weights(1, BSC->NbPoles());
1318 BSC->Weights(Weights);
1319 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1320 BSC->Degree(), BSC->IsPeriodic()) ;
1322 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1323 BSC->Degree(), BSC->IsPeriodic()) ;
1328 if(TheTypeC == GeomAbs_BezierCurve) {
1329 Handle(Geom_BezierCurve) BC = Curve->Bezier();
1330 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1331 for(i = 1;i <= Curve->NbPoles();i++) {
1332 Dist2Min = IntegerLast();
1333 Extrema_GenLocateExtPS extrloc(BC->Pole(i),Surf->Surface(),0.5,
1335 if (extrloc.IsDone()) {
1336 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1337 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1338 //if (Dist2Min < myTolerance * myTolerance) {
1339 (extrloc.Point()).Parameter(u,v);
1340 Poles2d(i).SetCoord(u,v);
1341 myProjIsDone = Standard_True;
1347 myProjIsDone = Standard_False;
1351 TColStd_Array1OfReal Knots(1, 2);
1352 Knots.SetValue(1,0.0);
1353 Knots.SetValue(2,1.0);
1354 TColStd_Array1OfInteger Mults(1, 2);
1355 Mults.Init(BC->NbPoles());
1356 if(BC->IsRational()) {
1357 TColStd_Array1OfReal Weights(1, BC->NbPoles());
1358 BC->Weights(Weights);
1359 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1360 BC->Degree(), BC->IsPeriodic()) ;
1362 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1363 BC->Degree(), BC->IsPeriodic()) ;
1369 else if(TheTypeS == GeomAbs_BezierSurface) {
1370 Handle(Geom_BezierSurface) BS = Surf->Bezier();
1371 if((BS->MaxDegree() == 1) &&
1372 (BS->NbUPoles() == 2) &&
1373 (BS->NbVPoles() == 2)) {
1374 gp_Pnt p11 = BS->Pole(1,1);
1375 gp_Pnt p12 = BS->Pole(1,2);
1376 gp_Pnt p21 = BS->Pole(2,1);
1377 gp_Pnt p22 = BS->Pole(2,2);
1380 if(V1.IsEqual(V2,Tol3d,Tol3d/(p11.Distance(p12)*180/M_PI))){ //OCC217
1381 //if (V1.IsEqual(V2,myTolerance,myTolerance/(p11.Distance(p12)*180/M_PI))){
1382 // and if it is enough to project the poles of Curve
1383 Standard_Integer Dist2Min = IntegerLast();
1386 //Standard_Real TolU = Surf->UResolution(myTolerance)
1387 // , TolV = Surf->VResolution(myTolerance);
1390 if(TheTypeC == GeomAbs_BSplineCurve) {
1391 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
1392 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1393 for(i = 1;i <= Curve->NbPoles();i++) {
1394 myProjIsDone = Standard_False;
1395 Dist2Min = IntegerLast();
1396 Extrema_GenLocateExtPS extrloc(BSC->Pole(i),Surf->Surface(),(p11.X()+p22.X())/2,
1397 (p11.Y()+p22.Y())/2,TolU,TolV) ;
1398 if (extrloc.IsDone()) {
1399 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1400 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1401 //if (Dist2Min < myTolerance * myTolerance) {
1402 (extrloc.Point()).Parameter(u,v);
1403 Poles2d(i).SetCoord(u,v);
1404 myProjIsDone = Standard_True;
1413 TColStd_Array1OfReal Knots(1, BSC->NbKnots());
1415 TColStd_Array1OfInteger Mults(1, BSC->NbKnots());
1416 BSC->Multiplicities(Mults);
1417 if(BSC->IsRational()) {
1418 TColStd_Array1OfReal Weights(1, BSC->NbPoles());
1419 BSC->Weights(Weights);
1420 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1421 BSC->Degree(), BSC->IsPeriodic()) ;
1423 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1424 BSC->Degree(), BSC->IsPeriodic()) ;
1429 if(TheTypeC == GeomAbs_BezierCurve) {
1430 Handle(Geom_BezierCurve) BC = Curve->Bezier();
1431 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1432 for(i = 1;i <= Curve->NbPoles();i++) {
1433 Dist2Min = IntegerLast();
1434 Extrema_GenLocateExtPS extrloc(BC->Pole(i),Surf->Surface(),0.5,
1436 if (extrloc.IsDone()) {
1437 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1438 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1439 //if (Dist2Min < myTolerance * myTolerance) {
1440 (extrloc.Point()).Parameter(u,v);
1441 Poles2d(i).SetCoord(u,v);
1442 myProjIsDone = Standard_True;
1448 myProjIsDone = Standard_False;
1452 TColStd_Array1OfReal Knots(1, 2);
1453 Knots.SetValue(1,0.0);
1454 Knots.SetValue(2,1.0);
1455 TColStd_Array1OfInteger Mults(1, 2);
1456 Mults.Init(BC->NbPoles());
1457 if(BC->IsRational()) {
1458 TColStd_Array1OfReal Weights(1, BC->NbPoles());
1459 BC->Weights(Weights);
1460 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1461 BC->Degree(), BC->IsPeriodic()) ;
1463 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1464 BC->Degree(), BC->IsPeriodic()) ;
1471 ProjLib_PolarFunction F(Curve, Surf, InitCurve2d, Tol3d) ; //OCC217
1472 //ProjLib_PolarFunction F(Curve, Surf, InitCurve2d, myTolerance) ;
1475 Standard_Integer Nb = 50;
1477 Standard_Real U, U1, U2;
1478 U1 = F.FirstParameter();
1479 U2 = F.LastParameter();
1481 TColgp_Array1OfPnt2d DummyPoles(1,Nb+1);
1482 TColStd_Array1OfReal DummyKnots(1,Nb+1);
1483 TColStd_Array1OfInteger DummyMults(1,Nb+1);
1486 DummyMults(Nb+1) = 2;
1487 for (Standard_Integer ij = 0; ij <= Nb; ij++) {
1488 U = (Nb-ij)*U1 + ij*U2;
1490 DummyPoles(ij+1) = F.Value(U);
1491 DummyKnots(ij+1) = ij;
1493 Handle(Geom2d_BSplineCurve) DummyC2d =
1494 new Geom2d_BSplineCurve(DummyPoles, DummyKnots, DummyMults, 1);
1495 Standard_CString Temp = "bs2d";
1497 DrawTrSurf::Set(Temp,DummyC2d);
1499 // DrawTrSurf::Set((Standard_CString ) "bs2d",DummyC2d);
1500 Handle(Geom2dAdaptor_HCurve) DDD =
1501 Handle(Geom2dAdaptor_HCurve)::DownCast(InitCurve2d);
1505 DrawTrSurf::Set(Temp,DDD->ChangeCurve2d().Curve());
1507 // DrawTrSurf::Set((Standard_CString ) "initc2d",DDD->ChangeCurve2d().Curve());
1510 Standard_Integer Deg1,Deg2;
1516 Approx_FitAndDivide2d Fit(F,Deg1,Deg2,Tol3d,Tol2d, //OCC217
1517 //Approx_FitAndDivide2d Fit(F,Deg1,Deg2,myTolerance,myTolerance,
1520 if(Fit.IsAllApproximated()) {
1522 Standard_Integer NbCurves = Fit.NbMultiCurves();
1523 Standard_Integer MaxDeg = 0;
1524 // To transform the MultiCurve into BSpline, it is required that all
1525 // Bezier constituing it have the same degree -> Calculation of MaxDeg
1526 Standard_Integer NbPoles = 1;
1527 for (i = 1; i <= NbCurves; i++) {
1528 Standard_Integer Deg = Fit.Value(i).Degree();
1529 MaxDeg = Max ( MaxDeg, Deg);
1532 NbPoles = MaxDeg * NbCurves + 1; //Tops on the BSpline
1533 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
1535 TColgp_Array1OfPnt2d TempPoles( 1, MaxDeg + 1);//to augment the degree
1537 TColStd_Array1OfReal Knots( 1, NbCurves + 1); //Nodes of the BSpline
1539 Standard_Integer Compt = 1;
1540 for (i = 1; i <= NbCurves; i++) {
1541 Fit.Parameters(i, Knots(i), Knots(i+1));
1542 AppParCurves_MultiCurve MC = Fit.Value( i); //Load the Ith Curve
1543 TColgp_Array1OfPnt2d Poles2d( 1, MC.Degree() + 1);//Retrieve the tops
1544 MC.Curve(1, Poles2d);
1546 //Eventual augmentation of the degree
1547 Standard_Integer Inc = MaxDeg - MC.Degree();
1549 // BSplCLib::IncreaseDegree( Inc, Poles2d, PLib::NoWeights(),
1550 BSplCLib::IncreaseDegree( MaxDeg, Poles2d, PLib::NoWeights(),
1551 TempPoles, PLib::NoWeights());
1552 //update of tops of the PCurve
1553 for (Standard_Integer j = 1 ; j <= MaxDeg + 1; j++) {
1554 Poles.SetValue( Compt, TempPoles( j));
1559 //update of tops of the PCurve
1560 for (Standard_Integer j = 1 ; j <= MaxDeg + 1; j++) {
1561 Poles.SetValue( Compt, Poles2d( j));
1569 //update of fields of ProjLib_Approx
1570 Standard_Integer NbKnots = NbCurves + 1;
1572 // The start and end nodes are not correct : Cf: opening of the interval
1573 //Knots( 1) -= 1.e-9;
1574 //Knots(NbKnots) += 1.e-9;
1577 TColStd_Array1OfInteger Mults( 1, NbKnots);
1579 Mults.SetValue( 1, MaxDeg + 1);
1580 Mults.SetValue(NbKnots, MaxDeg + 1);
1581 myProjIsDone = Standard_True;
1582 Handle(Geom2d_BSplineCurve) Dummy =
1583 new Geom2d_BSplineCurve(Poles,Knots,Mults,MaxDeg);
1585 // try to smoother the Curve GeomAbs_C1.
1587 Standard_Boolean OK = Standard_True;
1589 for (Standard_Integer ij = 2; ij < NbKnots; ij++) {
1590 OK = OK && Dummy->RemoveKnot(ij,MaxDeg-1,Tol3d); //OCC217
1591 //OK = OK && Dummy->RemoveKnot(ij,MaxDeg-1,myTolerance);
1595 cout << "ProjLib_ComputeApproxOnPolarSurface : Smoothing echoue"<<endl;
1600 return Handle(Geom2d_BSplineCurve)();
1603 //=======================================================================
1604 //function : BSpline
1606 //=======================================================================
1608 Handle(Geom2d_BSplineCurve)
1609 ProjLib_ComputeApproxOnPolarSurface::BSpline() const
1612 // Modified by Sergey KHROMOV - Thu Apr 18 11:16:46 2002 End
1613 // Standard_NoSuchObject_Raise_if
1615 // "ProjLib_ComputeApproxOnPolarSurface:BSpline");
1616 // Modified by Sergey KHROMOV - Thu Apr 18 11:16:47 2002 End
1620 //=======================================================================
1621 //function : Curve2d
1623 //=======================================================================
1625 Handle(Geom2d_Curve)
1626 ProjLib_ComputeApproxOnPolarSurface::Curve2d() const
1629 Standard_NoSuchObject_Raise_if
1631 "ProjLib_ComputeApproxOnPolarSurface:2ndCurve2d");
1636 //=======================================================================
1639 //=======================================================================
1641 Standard_Boolean ProjLib_ComputeApproxOnPolarSurface::IsDone() const
1644 return myProjIsDone;