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) ;
871 Standard_Real aMinSqDist = RealLast();
874 for (i = 1; i <= aExtPS.NbExt(); i++)
876 Standard_Real aSqDist = aExtPS.SquareDistance(i);
877 if (aSqDist < aMinSqDist)
878 aMinSqDist = aSqDist;
881 if (aMinSqDist > DistTol3d * DistTol3d) //try to project with less tolerance
883 TolU = Min(TolU, Precision::PConfusion());
884 TolV = Min(TolV, Precision::PConfusion());
885 aExtPS.Initialize(Surf->Surface(),
886 Surf->Surface().FirstUParameter(), Surf->Surface().LastUParameter(),
887 Surf->Surface().FirstVParameter(), Surf->Surface().LastVParameter(),
889 aExtPS.Perform(pntproj);
892 if( aExtPS.IsDone() && aExtPS.NbExt() >= 1 ) {
894 Standard_Integer GoodValue = 1;
896 for ( i = 1 ; i <= aExtPS.NbExt() ; i++ ) {
897 if( aExtPS.SquareDistance(i) < DistTol3d * DistTol3d ) {
898 if( aExtPS.SquareDistance(i) <= 1.e-18 ) {
899 aExtPS.Point(i).Parameter(u,v);
901 Standard_Boolean isSame = Standard_False;
902 for( j = 1; j <= Sols.Length(); j++ ) {
903 if( p2d.SquareDistance( Sols.Value(j) ) <= 1.e-18 ) {
904 isSame = Standard_True;
908 if( !isSame ) Sols.Append( p2d );
910 if( Dist2Min > aExtPS.SquareDistance(i) ) {
911 Dist2Min = aExtPS.SquareDistance(i);
917 if( Sols.Length() > 1 ) areManyZeros = Standard_True;
919 if( Dist2Min <= DistTol3d * DistTol3d) {
920 if( !areManyZeros ) {
921 aExtPS.Point(GoodValue).Parameter(u,v);
922 Pts2d(1).SetCoord(u,v);
923 myProjIsDone = Standard_True;
926 Standard_Integer nbSols = Sols.Length();
927 Standard_Real Dist2Max = -1.e+200;
928 for( i = 1; i <= nbSols; i++ ) {
929 const gp_Pnt2d& aP1 = Sols.Value(i);
930 for( j = i+1; j <= nbSols; j++ ) {
931 const gp_Pnt2d& aP2 = Sols.Value(j);
932 Standard_Real aDist2 = aP1.SquareDistance(aP2);
933 if( aDist2 > Dist2Max ) Dist2Max = aDist2;
936 Standard_Real aMaxT2 = Max(TolU,TolV);
938 if( Dist2Max > aMaxT2 ) {
939 Standard_Integer tPp = 0;
940 for( i = 1; i <= 5; i++ ) {
941 Standard_Integer nbExtOk = 0;
942 Standard_Integer indExt = 0;
943 Standard_Integer iT = 1 + (NbOfPnts - 1)/5*i;
944 Curve->D0( Param.Value(iT), pntproj );
945 Extrema_ExtPS aTPS( pntproj, Surf->Surface(), TolU, TolV );
947 if( aTPS.IsDone() && aTPS.NbExt() >= 1 ) {
948 for( j = 1 ; j <= aTPS.NbExt() ; j++ ) {
949 if( aTPS.SquareDistance(j) < DistTol3d * DistTol3d ) {
951 if( aTPS.SquareDistance(j) < Dist2Min ) {
952 Dist2Min = aTPS.SquareDistance(j);
960 aTPS.Point(indExt).Parameter(u,v);
966 gp_Pnt2d aPp = gp_Pnt2d(u,v);
969 Standard_Boolean isFound = Standard_False;
971 Curve->D0( Param.Value(tPp+j), pntproj );
972 Extrema_ExtPS aTPS( pntproj, Surf->Surface(), TolU, TolV );
974 Standard_Integer indExt = 0;
975 if( aTPS.IsDone() && aTPS.NbExt() >= 1 ) {
976 for( i = 1 ; i <= aTPS.NbExt() ; i++ ) {
977 if( aTPS.SquareDistance(i) < DistTol3d * DistTol3d && aTPS.SquareDistance(i) < Dist2Min ) {
978 Dist2Min = aTPS.SquareDistance(i);
980 isFound = Standard_True;
985 aTPS.Point(indExt).Parameter(u,v);
990 if( (tPp+j) > NbOfPnts ) break;
994 gp_Vec2d atV(aPp,aPn);
995 Standard_Boolean isChosen = Standard_False;
996 for( i = 1; i <= nbSols; i++ ) {
997 const gp_Pnt2d& aP1 = Sols.Value(i);
998 gp_Vec2d asV(aP1,aPp);
999 if( asV.Dot(atV) > 0. ) {
1000 isChosen = Standard_True;
1001 Pts2d(1).SetCoord(aP1.X(),aP1.Y());
1002 myProjIsDone = Standard_True;
1007 aExtPS.Point(GoodValue).Parameter(u,v);
1008 Pts2d(1).SetCoord(u,v);
1009 myProjIsDone = Standard_True;
1013 aExtPS.Point(GoodValue).Parameter(u,v);
1014 Pts2d(1).SetCoord(u,v);
1015 myProjIsDone = Standard_True;
1019 aExtPS.Point(GoodValue).Parameter(u,v);
1020 Pts2d(1).SetCoord(u,v);
1021 myProjIsDone = Standard_True;
1025 aExtPS.Point(GoodValue).Parameter(u,v);
1026 Pts2d(1).SetCoord(u,v);
1027 myProjIsDone = Standard_True;
1032 // calculate the following points with GenLocate_ExtPS
1033 // (and store the result and each parameter in a sequence)
1034 Standard_Integer usens = 0, vsens = 0;
1035 // to know the position relatively to the period
1036 Standard_Real U0 = u, V0 = v, U1 = u, V1 = v, uperiod =0, vperiod = 0;
1037 // U0 and V0 are the points in the initialized period
1038 // (period with u and v),
1039 // U1 and V1 are the points for construction of poles
1042 if(Surf->IsUPeriodic() || Surf->IsUClosed()) {
1043 uperiod = Surf->LastUParameter() - Surf->FirstUParameter();
1046 if(Surf->IsVPeriodic() || Surf->IsVClosed()) {
1047 vperiod = Surf->LastVParameter() - Surf->FirstVParameter();
1050 for ( i = 2 ; i <= NbOfPnts ; i++)
1052 myProjIsDone = Standard_False;
1053 Dist2Min = RealLast();
1054 Curve->D0(Param.Value(i), pntproj);
1055 Extrema_GenLocateExtPS aLocateExtPS
1056 (pntproj, Surf->Surface(), U0, V0, TolU, TolV) ;
1058 if (aLocateExtPS.IsDone())
1059 if (aLocateExtPS.SquareDistance() < DistTol3d * DistTol3d) { //OCC217
1060 //if (aLocateExtPS.SquareDistance() < Tol3d * Tol3d) {
1061 (aLocateExtPS.Point()).Parameter(U0,V0);
1062 U1 = U0 + usens*uperiod;
1063 V1 = V0 + vsens*vperiod;
1064 Pts2d(i).SetCoord(U1,V1);
1065 myProjIsDone = Standard_True;
1067 if(!myProjIsDone && uperiod) {
1068 Standard_Real Uinf, Usup, Uaux;
1069 Uinf = Surf->Surface().FirstUParameter();
1070 Usup = Surf->Surface().LastUParameter();
1071 if((Usup - U0) > (U0 - Uinf))
1072 Uaux = 2*Uinf - U0 + uperiod;
1074 Uaux = 2*Usup - U0 - uperiod;
1075 Extrema_GenLocateExtPS locext(pntproj,
1077 Uaux, V0, TolU, TolV);
1078 if (locext.IsDone())
1079 if (locext.SquareDistance() < DistTol3d * DistTol3d) { //OCC217
1080 //if (locext.SquareDistance() < Tol3d * Tol3d) {
1081 (locext.Point()).Parameter(u,v);
1082 if((Usup - U0) > (U0 - Uinf))
1087 U1 = U0 + usens*uperiod;
1088 V1 = V0 + vsens*vperiod;
1089 Pts2d(i).SetCoord(U1,V1);
1090 myProjIsDone = Standard_True;
1093 if(!myProjIsDone && vperiod) {
1094 Standard_Real Vinf, Vsup, Vaux;
1095 Vinf = Surf->Surface().FirstVParameter();
1096 Vsup = Surf->Surface().LastVParameter();
1097 if((Vsup - V0) > (V0 - Vinf))
1098 Vaux = 2*Vinf - V0 + vperiod;
1100 Vaux = 2*Vsup - V0 - vperiod;
1101 Extrema_GenLocateExtPS locext(pntproj,
1103 U0, Vaux, TolU, TolV) ;
1104 if (locext.IsDone())
1105 if (locext.SquareDistance() < DistTol3d * DistTol3d) { //OCC217
1106 //if (locext.SquareDistance() < Tol3d * Tol3d) {
1107 (locext.Point()).Parameter(u,v);
1108 if((Vsup - V0) > (V0 - Vinf))
1113 U1 = U0 + usens*uperiod;
1114 V1 = V0 + vsens*vperiod;
1115 Pts2d(i).SetCoord(U1,V1);
1116 myProjIsDone = Standard_True;
1119 if(!myProjIsDone && uperiod && vperiod) {
1120 Standard_Real Uaux, Vaux;
1121 if((Usup - U0) > (U0 - Uinf))
1122 Uaux = 2*Uinf - U0 + uperiod;
1124 Uaux = 2*Usup - U0 - uperiod;
1125 if((Vsup - V0) > (V0 - Vinf))
1126 Vaux = 2*Vinf - V0 + vperiod;
1128 Vaux = 2*Vsup - V0 - vperiod;
1129 Extrema_GenLocateExtPS locext(pntproj,
1131 Uaux, Vaux, TolU, TolV);
1132 if (locext.IsDone())
1133 if (locext.SquareDistance() < DistTol3d * DistTol3d) {
1134 //if (locext.SquareDistance() < Tol3d * Tol3d) {
1135 (locext.Point()).Parameter(u,v);
1136 if((Usup - U0) > (U0 - Uinf))
1140 if((Vsup - V0) > (V0 - Vinf))
1145 U1 = U0 + usens*uperiod;
1146 V1 = V0 + vsens*vperiod;
1147 Pts2d(i).SetCoord(U1,V1);
1148 myProjIsDone = Standard_True;
1152 Extrema_ExtPS ext(pntproj, Surf->Surface(), TolU, TolV) ;
1154 Dist2Min = ext.SquareDistance(1);
1155 Standard_Integer GoodValue = 1;
1156 for ( j = 2 ; j <= ext.NbExt() ; j++ )
1157 if( Dist2Min > ext.SquareDistance(j)) {
1158 Dist2Min = ext.SquareDistance(j);
1161 if (Dist2Min < DistTol3d * DistTol3d) {
1162 //if (Dist2Min < Tol3d * Tol3d) {
1163 (ext.Point(GoodValue)).Parameter(u,v);
1165 if((U0 - u) > (2*uperiod/3)) {
1169 if((u - U0) > (2*uperiod/3)) {
1173 if((V0 - v) > (vperiod/2)) {
1177 if((v - V0) > (vperiod/2)) {
1181 U1 = U0 + usens*uperiod;
1182 V1 = V0 + vsens*vperiod;
1183 Pts2d(i).SetCoord(U1,V1);
1184 myProjIsDone = Standard_True;
1192 // -- Pnts2d is transformed into Geom2d_BSplineCurve, with the help of Param and Mult
1194 myBSpline = new Geom2d_BSplineCurve(Pts2d,Param,Mult,1);
1195 Geom2dAdaptor_Curve GAC(myBSpline);
1196 Handle(Adaptor2d_HCurve2d) IC2d = new Geom2dAdaptor_HCurve(GAC);
1199 // sprintf(name,"%s_%d","build",compteur++);
1200 // DrawTrSurf::Set(name,myBSpline);
1205 // Modified by Sergey KHROMOV - Thu Apr 18 10:57:50 2002 Begin
1206 // Standard_NoSuchObject_Raise_if(1,"ProjLib_Compu: build echec");
1207 // Modified by Sergey KHROMOV - Thu Apr 18 10:57:51 2002 End
1208 return Handle(Adaptor2d_HCurve2d)();
1210 myProjIsDone = Standard_False;
1211 // Modified by Sergey KHROMOV - Thu Apr 18 10:58:01 2002 Begin
1212 // Standard_NoSuchObject_Raise_if(1,"ProjLib_ComputeOnPS: build echec");
1213 // Modified by Sergey KHROMOV - Thu Apr 18 10:58:02 2002 End
1214 return Handle(Adaptor2d_HCurve2d)();
1220 //=======================================================================
1221 //function : ProjLib_ProjectUsingInitialCurve2d
1223 //=======================================================================
1224 Handle(Geom2d_BSplineCurve)
1225 ProjLib_ComputeApproxOnPolarSurface::
1226 ProjectUsingInitialCurve2d(const Handle(Adaptor3d_HCurve)& Curve,
1227 const Handle(Adaptor3d_HSurface)& Surf,
1228 const Handle(Adaptor2d_HCurve2d)& InitCurve2d)
1231 Standard_Real Tol3d = myTolerance;
1232 Standard_Real DistTol3d = 1.0*Tol3d;
1233 Standard_Real TolU = Surf->UResolution(Tol3d), TolV = Surf->VResolution(Tol3d);
1234 Standard_Real Tol2d = Sqrt(TolU*TolU + TolV*TolV);
1237 GeomAbs_SurfaceType TheTypeS = Surf->GetType();
1238 GeomAbs_CurveType TheTypeC = Curve->GetType();
1239 // Handle(Standard_Type) TheTypeS = Surf->DynamicType();
1240 // Handle(Standard_Type) TheTypeC = Curve->DynamicType(); // si on a :
1241 // if(TheTypeS == STANDARD_TYPE(Geom_BSplineSurface)) {
1242 if(TheTypeS == GeomAbs_Plane) {
1244 gp_Pln Plane = Surf->Plane();
1245 if(TheTypeC == GeomAbs_BSplineCurve) {
1246 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
1247 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1248 for(i = 1;i <= Curve->NbPoles();i++) {
1249 ElSLib::Parameters( Plane, BSC->Pole(i), S, T);
1250 Poles2d(i).SetCoord(S,T);
1252 TColStd_Array1OfReal Knots(1, BSC->NbKnots());
1254 TColStd_Array1OfInteger Mults(1, BSC->NbKnots());
1255 BSC->Multiplicities(Mults);
1256 if(BSC->IsRational()) {
1257 TColStd_Array1OfReal Weights(1, BSC->NbPoles());
1258 BSC->Weights(Weights);
1259 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1260 BSC->Degree(), BSC->IsPeriodic()) ;
1262 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1263 BSC->Degree(), BSC->IsPeriodic()) ;
1266 if(TheTypeC == GeomAbs_BezierCurve) {
1267 Handle(Geom_BezierCurve) BC = Curve->Bezier();
1268 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1269 for(i = 1;i <= Curve->NbPoles();i++) {
1270 ElSLib::Parameters( Plane, BC->Pole(i), S, T);
1271 Poles2d(i).SetCoord(S,T);
1273 TColStd_Array1OfReal Knots(1, 2);
1274 Knots.SetValue(1,0.0);
1275 Knots.SetValue(2,1.0);
1276 TColStd_Array1OfInteger Mults(1, 2);
1277 Mults.Init(BC->NbPoles());
1278 if(BC->IsRational()) {
1279 TColStd_Array1OfReal Weights(1, BC->NbPoles());
1280 BC->Weights(Weights);
1281 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1282 BC->Degree(), BC->IsPeriodic()) ;
1284 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1285 BC->Degree(), BC->IsPeriodic()) ;
1288 if(TheTypeS == GeomAbs_BSplineSurface) {
1289 Handle(Geom_BSplineSurface) BSS = Surf->BSpline();
1290 if((BSS->MaxDegree() == 1) &&
1291 (BSS->NbUPoles() == 2) &&
1292 (BSS->NbVPoles() == 2)) {
1293 gp_Pnt p11 = BSS->Pole(1,1);
1294 gp_Pnt p12 = BSS->Pole(1,2);
1295 gp_Pnt p21 = BSS->Pole(2,1);
1296 gp_Pnt p22 = BSS->Pole(2,2);
1299 if(V1.IsEqual(V2,Tol3d,Tol3d/(p11.Distance(p12)*180/M_PI))){ //OCC217
1300 //if(V1.IsEqual(V2,myTolerance,myTolerance/(p11.Distance(p12)*180/M_PI))){
1301 // so the polar surface is plane
1302 // and if it is enough to projet the poles of Curve
1303 Standard_Integer Dist2Min = IntegerLast();
1306 //Standard_Real TolU = Surf->UResolution(myTolerance)
1307 // , TolV = Surf->VResolution(myTolerance);
1309 if(TheTypeC == GeomAbs_BSplineCurve) {
1310 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
1311 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1312 for(i = 1;i <= Curve->NbPoles();i++) {
1313 myProjIsDone = Standard_False;
1314 Dist2Min = IntegerLast();
1315 Extrema_GenLocateExtPS extrloc(BSC->Pole(i),Surf->Surface(),(p11.X()+p22.X())/2,
1316 (p11.Y()+p22.Y())/2,TolU,TolV) ;
1317 if (extrloc.IsDone()) {
1318 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1319 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1320 //if (Dist2Min < myTolerance * myTolerance) {
1321 (extrloc.Point()).Parameter(u,v);
1322 Poles2d(i).SetCoord(u,v);
1323 myProjIsDone = Standard_True;
1332 TColStd_Array1OfReal Knots(1, BSC->NbKnots());
1334 TColStd_Array1OfInteger Mults(1, BSC->NbKnots());
1335 BSC->Multiplicities(Mults);
1336 if(BSC->IsRational()) {
1337 TColStd_Array1OfReal Weights(1, BSC->NbPoles());
1338 BSC->Weights(Weights);
1339 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1340 BSC->Degree(), BSC->IsPeriodic()) ;
1342 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1343 BSC->Degree(), BSC->IsPeriodic()) ;
1348 if(TheTypeC == GeomAbs_BezierCurve) {
1349 Handle(Geom_BezierCurve) BC = Curve->Bezier();
1350 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1351 for(i = 1;i <= Curve->NbPoles();i++) {
1352 Dist2Min = IntegerLast();
1353 Extrema_GenLocateExtPS extrloc(BC->Pole(i),Surf->Surface(),0.5,
1355 if (extrloc.IsDone()) {
1356 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1357 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1358 //if (Dist2Min < myTolerance * myTolerance) {
1359 (extrloc.Point()).Parameter(u,v);
1360 Poles2d(i).SetCoord(u,v);
1361 myProjIsDone = Standard_True;
1367 myProjIsDone = Standard_False;
1371 TColStd_Array1OfReal Knots(1, 2);
1372 Knots.SetValue(1,0.0);
1373 Knots.SetValue(2,1.0);
1374 TColStd_Array1OfInteger Mults(1, 2);
1375 Mults.Init(BC->NbPoles());
1376 if(BC->IsRational()) {
1377 TColStd_Array1OfReal Weights(1, BC->NbPoles());
1378 BC->Weights(Weights);
1379 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1380 BC->Degree(), BC->IsPeriodic()) ;
1382 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1383 BC->Degree(), BC->IsPeriodic()) ;
1389 else if(TheTypeS == GeomAbs_BezierSurface) {
1390 Handle(Geom_BezierSurface) BS = Surf->Bezier();
1391 if((BS->MaxDegree() == 1) &&
1392 (BS->NbUPoles() == 2) &&
1393 (BS->NbVPoles() == 2)) {
1394 gp_Pnt p11 = BS->Pole(1,1);
1395 gp_Pnt p12 = BS->Pole(1,2);
1396 gp_Pnt p21 = BS->Pole(2,1);
1397 gp_Pnt p22 = BS->Pole(2,2);
1400 if(V1.IsEqual(V2,Tol3d,Tol3d/(p11.Distance(p12)*180/M_PI))){ //OCC217
1401 //if (V1.IsEqual(V2,myTolerance,myTolerance/(p11.Distance(p12)*180/M_PI))){
1402 // and if it is enough to project the poles of Curve
1403 Standard_Integer Dist2Min = IntegerLast();
1406 //Standard_Real TolU = Surf->UResolution(myTolerance)
1407 // , TolV = Surf->VResolution(myTolerance);
1410 if(TheTypeC == GeomAbs_BSplineCurve) {
1411 Handle(Geom_BSplineCurve) BSC = Curve->BSpline();
1412 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1413 for(i = 1;i <= Curve->NbPoles();i++) {
1414 myProjIsDone = Standard_False;
1415 Dist2Min = IntegerLast();
1416 Extrema_GenLocateExtPS extrloc(BSC->Pole(i),Surf->Surface(),(p11.X()+p22.X())/2,
1417 (p11.Y()+p22.Y())/2,TolU,TolV) ;
1418 if (extrloc.IsDone()) {
1419 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1420 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1421 //if (Dist2Min < myTolerance * myTolerance) {
1422 (extrloc.Point()).Parameter(u,v);
1423 Poles2d(i).SetCoord(u,v);
1424 myProjIsDone = Standard_True;
1433 TColStd_Array1OfReal Knots(1, BSC->NbKnots());
1435 TColStd_Array1OfInteger Mults(1, BSC->NbKnots());
1436 BSC->Multiplicities(Mults);
1437 if(BSC->IsRational()) {
1438 TColStd_Array1OfReal Weights(1, BSC->NbPoles());
1439 BSC->Weights(Weights);
1440 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1441 BSC->Degree(), BSC->IsPeriodic()) ;
1443 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1444 BSC->Degree(), BSC->IsPeriodic()) ;
1449 if(TheTypeC == GeomAbs_BezierCurve) {
1450 Handle(Geom_BezierCurve) BC = Curve->Bezier();
1451 TColgp_Array1OfPnt2d Poles2d(1,Curve->NbPoles());
1452 for(i = 1;i <= Curve->NbPoles();i++) {
1453 Dist2Min = IntegerLast();
1454 Extrema_GenLocateExtPS extrloc(BC->Pole(i),Surf->Surface(),0.5,
1456 if (extrloc.IsDone()) {
1457 Dist2Min = (Standard_Integer ) extrloc.SquareDistance();
1458 if (Dist2Min < DistTol3d * DistTol3d) { //OCC217
1459 //if (Dist2Min < myTolerance * myTolerance) {
1460 (extrloc.Point()).Parameter(u,v);
1461 Poles2d(i).SetCoord(u,v);
1462 myProjIsDone = Standard_True;
1468 myProjIsDone = Standard_False;
1472 TColStd_Array1OfReal Knots(1, 2);
1473 Knots.SetValue(1,0.0);
1474 Knots.SetValue(2,1.0);
1475 TColStd_Array1OfInteger Mults(1, 2);
1476 Mults.Init(BC->NbPoles());
1477 if(BC->IsRational()) {
1478 TColStd_Array1OfReal Weights(1, BC->NbPoles());
1479 BC->Weights(Weights);
1480 return new Geom2d_BSplineCurve(Poles2d, Weights, Knots, Mults,
1481 BC->Degree(), BC->IsPeriodic()) ;
1483 return new Geom2d_BSplineCurve(Poles2d, Knots, Mults,
1484 BC->Degree(), BC->IsPeriodic()) ;
1491 ProjLib_PolarFunction F(Curve, Surf, InitCurve2d, Tol3d) ; //OCC217
1492 //ProjLib_PolarFunction F(Curve, Surf, InitCurve2d, myTolerance) ;
1495 Standard_Integer Nb = 50;
1497 Standard_Real U, U1, U2;
1498 U1 = F.FirstParameter();
1499 U2 = F.LastParameter();
1501 TColgp_Array1OfPnt2d DummyPoles(1,Nb+1);
1502 TColStd_Array1OfReal DummyKnots(1,Nb+1);
1503 TColStd_Array1OfInteger DummyMults(1,Nb+1);
1506 DummyMults(Nb+1) = 2;
1507 for (Standard_Integer ij = 0; ij <= Nb; ij++) {
1508 U = (Nb-ij)*U1 + ij*U2;
1510 DummyPoles(ij+1) = F.Value(U);
1511 DummyKnots(ij+1) = ij;
1513 Handle(Geom2d_BSplineCurve) DummyC2d =
1514 new Geom2d_BSplineCurve(DummyPoles, DummyKnots, DummyMults, 1);
1515 Standard_CString Temp = "bs2d";
1517 DrawTrSurf::Set(Temp,DummyC2d);
1519 // DrawTrSurf::Set((Standard_CString ) "bs2d",DummyC2d);
1520 Handle(Geom2dAdaptor_HCurve) DDD =
1521 Handle(Geom2dAdaptor_HCurve)::DownCast(InitCurve2d);
1525 DrawTrSurf::Set(Temp,DDD->ChangeCurve2d().Curve());
1527 // DrawTrSurf::Set((Standard_CString ) "initc2d",DDD->ChangeCurve2d().Curve());
1530 Standard_Integer Deg1,Deg2;
1536 Approx_FitAndDivide2d Fit(F,Deg1,Deg2,Tol3d,Tol2d, //OCC217
1537 //Approx_FitAndDivide2d Fit(F,Deg1,Deg2,myTolerance,myTolerance,
1540 if(Fit.IsAllApproximated()) {
1542 Standard_Integer NbCurves = Fit.NbMultiCurves();
1543 Standard_Integer MaxDeg = 0;
1544 // To transform the MultiCurve into BSpline, it is required that all
1545 // Bezier constituing it have the same degree -> Calculation of MaxDeg
1546 Standard_Integer NbPoles = 1;
1547 for (i = 1; i <= NbCurves; i++) {
1548 Standard_Integer Deg = Fit.Value(i).Degree();
1549 MaxDeg = Max ( MaxDeg, Deg);
1552 NbPoles = MaxDeg * NbCurves + 1; //Tops on the BSpline
1553 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
1555 TColgp_Array1OfPnt2d TempPoles( 1, MaxDeg + 1);//to augment the degree
1557 TColStd_Array1OfReal Knots( 1, NbCurves + 1); //Nodes of the BSpline
1559 Standard_Integer Compt = 1;
1560 for (i = 1; i <= NbCurves; i++) {
1561 Fit.Parameters(i, Knots(i), Knots(i+1));
1562 AppParCurves_MultiCurve MC = Fit.Value( i); //Load the Ith Curve
1563 TColgp_Array1OfPnt2d Poles2d( 1, MC.Degree() + 1);//Retrieve the tops
1564 MC.Curve(1, Poles2d);
1566 //Eventual augmentation of the degree
1567 Standard_Integer Inc = MaxDeg - MC.Degree();
1569 // BSplCLib::IncreaseDegree( Inc, Poles2d, PLib::NoWeights(),
1570 BSplCLib::IncreaseDegree( MaxDeg, Poles2d, PLib::NoWeights(),
1571 TempPoles, PLib::NoWeights());
1572 //update of tops of the PCurve
1573 for (Standard_Integer j = 1 ; j <= MaxDeg + 1; j++) {
1574 Poles.SetValue( Compt, TempPoles( j));
1579 //update of tops of the PCurve
1580 for (Standard_Integer j = 1 ; j <= MaxDeg + 1; j++) {
1581 Poles.SetValue( Compt, Poles2d( j));
1589 //update of fields of ProjLib_Approx
1590 Standard_Integer NbKnots = NbCurves + 1;
1592 // The start and end nodes are not correct : Cf: opening of the interval
1593 //Knots( 1) -= 1.e-9;
1594 //Knots(NbKnots) += 1.e-9;
1597 TColStd_Array1OfInteger Mults( 1, NbKnots);
1599 Mults.SetValue( 1, MaxDeg + 1);
1600 Mults.SetValue(NbKnots, MaxDeg + 1);
1601 myProjIsDone = Standard_True;
1602 Handle(Geom2d_BSplineCurve) Dummy =
1603 new Geom2d_BSplineCurve(Poles,Knots,Mults,MaxDeg);
1605 // try to smoother the Curve GeomAbs_C1.
1607 Standard_Boolean OK = Standard_True;
1609 for (Standard_Integer ij = 2; ij < NbKnots; ij++) {
1610 OK = OK && Dummy->RemoveKnot(ij,MaxDeg-1,Tol3d); //OCC217
1611 //OK = OK && Dummy->RemoveKnot(ij,MaxDeg-1,myTolerance);
1615 cout << "ProjLib_ComputeApproxOnPolarSurface : Smoothing echoue"<<endl;
1620 return Handle(Geom2d_BSplineCurve)();
1623 //=======================================================================
1624 //function : BSpline
1626 //=======================================================================
1628 Handle(Geom2d_BSplineCurve)
1629 ProjLib_ComputeApproxOnPolarSurface::BSpline() const
1632 // Modified by Sergey KHROMOV - Thu Apr 18 11:16:46 2002 End
1633 // Standard_NoSuchObject_Raise_if
1635 // "ProjLib_ComputeApproxOnPolarSurface:BSpline");
1636 // Modified by Sergey KHROMOV - Thu Apr 18 11:16:47 2002 End
1640 //=======================================================================
1641 //function : Curve2d
1643 //=======================================================================
1645 Handle(Geom2d_Curve)
1646 ProjLib_ComputeApproxOnPolarSurface::Curve2d() const
1649 Standard_NoSuchObject_Raise_if
1651 "ProjLib_ComputeApproxOnPolarSurface:2ndCurve2d");
1656 //=======================================================================
1659 //=======================================================================
1661 Standard_Boolean ProjLib_ComputeApproxOnPolarSurface::IsDone() const
1664 return myProjIsDone;