1 // Created on: 1993-09-07
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // modified by NIZHNY-OFV Thu Jan 20 11:04:19 2005
19 #include <ProjLib_ComputeApprox.hxx>
21 #include <GeomAbs_SurfaceType.hxx>
22 #include <GeomAbs_CurveType.hxx>
23 #include <AppCont_Function2d.hxx>
24 #include <Convert_CompBezierCurves2dToBSplineCurve2d.hxx>
27 #include <BSplCLib.hxx>
28 #include <Standard_NoSuchObject.hxx>
29 #include <Geom_UndefinedDerivative.hxx>
31 #include <gp_Trsf.hxx>
32 #include <Precision.hxx>
33 #include <Approx_FitAndDivide2d.hxx>
34 #include <AppParCurves_MultiCurve.hxx>
35 #include <Adaptor3d_HCurve.hxx>
36 #include <Adaptor3d_HSurface.hxx>
37 #include <TColgp_Array1OfPnt2d.hxx>
38 #include <TColgp_Array1OfPnt.hxx>
39 #include <TColStd_Array1OfReal.hxx>
40 #include <TColStd_Array1OfInteger.hxx>
41 #include <Geom_BSplineCurve.hxx>
42 #include <Geom_BezierCurve.hxx>
43 #include <Geom2d_BSplineCurve.hxx>
44 #include <Geom2d_BezierCurve.hxx>
48 #include <DrawTrSurf.hxx>
51 static Standard_Boolean AffichValue = Standard_False;
54 //=======================================================================
57 //=======================================================================
59 static inline Standard_Boolean IsEqual(Standard_Real Check,Standard_Real With,Standard_Real Toler)
61 return ((Abs(Check - With) < Toler) ? Standard_True : Standard_False);
65 //=======================================================================
68 //=======================================================================
70 static gp_Pnt2d Function_Value(const Standard_Real U,
71 const Handle(Adaptor3d_HCurve)& myCurve,
72 const Handle(Adaptor3d_HSurface)& mySurface,
73 const Standard_Real U1,
74 const Standard_Real U2,
75 const Standard_Real V1,
76 const Standard_Real V2,
77 const Standard_Boolean UCouture,
78 const Standard_Boolean VCouture )
80 Standard_Real S = 0., T = 0.;
82 gp_Pnt P3d = myCurve->Value(U);
83 GeomAbs_SurfaceType SType = mySurface->GetType();
89 gp_Pln Plane = mySurface->Plane();
90 ElSLib::Parameters( Plane, P3d, S, T);
93 case GeomAbs_Cylinder:
95 gp_Cylinder Cylinder = mySurface->Cylinder();
96 ElSLib::Parameters( Cylinder, P3d, S, T);
101 gp_Cone Cone = mySurface->Cone();
102 ElSLib::Parameters( Cone, P3d, S, T);
107 gp_Sphere Sphere = mySurface->Sphere();
108 ElSLib::Parameters(Sphere, P3d, S, T);
113 gp_Torus Torus = mySurface->Torus();
114 ElSLib::Parameters( Torus, P3d, S, T);
118 Standard_NoSuchObject::Raise("ProjLib_ComputeApprox::Value");
123 S = ElCLib::InPeriod(S, U1, U2);
127 if(SType == GeomAbs_Sphere) {
128 if ( Abs( S - U1 ) > M_PI ) {
133 S = ElCLib::InPeriod(S, U1, U2);
136 T = ElCLib::InPeriod(T, V1, V2);
139 return gp_Pnt2d(S, T);
141 //=======================================================================
144 //=======================================================================
145 static Standard_Boolean Function_D1( const Standard_Real U,
148 const Handle(Adaptor3d_HCurve)& myCurve,
149 const Handle(Adaptor3d_HSurface)& mySurface,
150 const Standard_Real U1,
151 const Standard_Real U2,
152 const Standard_Real V1,
153 const Standard_Real V2,
154 const Standard_Boolean UCouture,
155 const Standard_Boolean VCouture )
158 Standard_Real dU, dV;
160 P = Function_Value(U,myCurve,mySurface,U1,U2,V1,V2,UCouture,VCouture);
162 GeomAbs_SurfaceType Type = mySurface->GetType();
167 case GeomAbs_Cylinder:
173 myCurve->D1(U,P3d,T);
174 mySurface->D1(P.X(),P.Y(),P3d,D1U,D1V);
178 Standard_Real Nu = D1U.SquareMagnitude();
179 Standard_Real Nv = D1V.SquareMagnitude();
181 if ( Nu < Epsilon(1.) || Nv < Epsilon(1.))
182 return Standard_False;
186 D = gp_Vec2d( dU, dV);
191 return Standard_False;
194 return Standard_True;
197 //=======================================================================
198 //function : Function_SetUVBounds
200 //=======================================================================
201 static void Function_SetUVBounds(Standard_Real& myU1,
205 Standard_Boolean& UCouture,
206 Standard_Boolean& VCouture,
207 const Handle(Adaptor3d_HCurve)& myCurve,
208 const Handle(Adaptor3d_HSurface)& mySurface)
210 Standard_Real W1, W2, W;
213 W1 = myCurve->FirstParameter();
214 W2 = myCurve->LastParameter ();
216 // on ouvre l`intervalle
219 P1 = myCurve->Value(W1);
220 P2 = myCurve->Value(W2);
221 P = myCurve->Value(W);
223 switch ( mySurface->GetType()) {
226 gp_Cone Cone = mySurface->Cone();
227 VCouture = Standard_False;
229 switch( myCurve->GetType() ){
230 case GeomAbs_Parabola:
231 case GeomAbs_Hyperbola:
232 case GeomAbs_Ellipse:{
233 Standard_Real U1, U2, V1, V2, U , V;
234 ElSLib::Parameters( Cone, P1, U1, V1);
235 ElSLib::Parameters( Cone, P2, U2, V2);
236 ElSLib::Parameters( Cone, P , U , V );
239 if ( ( U1 < U && U < U2 ) && !myCurve->IsClosed() ) {
240 UCouture = Standard_False;
243 UCouture = Standard_True;
244 myU2 = myU1 + 2*M_PI;
250 Standard_Real U1, V1, U , V, Delta = 0., d = 0., pmin = W1, pmax = W1, dmax = 0., Uf, Ul;
251 ElSLib::Parameters( Cone, P1, U1, V1);
252 ElSLib::Parameters( Cone, P2, Ul, V1);
253 myU1 = U1; myU2 = U1; Uf = U1;
254 Standard_Real Step = .1;
255 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
257 Step = (W2 - W1) / (nbp - 1);
258 Standard_Boolean isclandper = (!(myCurve->IsClosed()) && !(myCurve->IsPeriodic()));
259 for(Standard_Real par = W1 + Step; par <= W2; par += Step) {
260 if(!isclandper) par += Step;
261 P = myCurve->Value(par);
262 ElSLib::Parameters( Cone, P, U, V);
266 if( ( (IsEqual(U,(2*M_PI),1.e-10) && (U1 >= 0. && U1 <= M_PI)) &&
267 (IsEqual(U,Ul,1.e-10) && !IsEqual(Uf,0.,1.e-10)) ) && isclandper ) U = 0.;
268 else Delta -= 2*M_PI;
273 if( ( (IsEqual(U,0.,1.e-10) && (U1 >= M_PI && U1 <= (2*M_PI))) &&
274 (IsEqual(U,Ul,1.e-10) && !IsEqual(Uf,(2*M_PI),1.e-10)) ) && isclandper ) U = 2*M_PI;
275 else Delta += 2*M_PI;
279 dmax = Max(dmax, Abs(d));
280 if(U < myU1) {myU1 = U; pmin = par;}
281 if(U > myU2) {myU2 = U; pmax = par;}
285 if(!(Abs(pmin - W1) <= Precision::PConfusion() || Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
286 if(!(Abs(pmax - W1) <= Precision::PConfusion() || Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
288 if((myU1 >=0. && myU1 <= 2*M_PI) && (myU2 >=0. && myU2 <= 2*M_PI) ) UCouture = Standard_False;
290 U = ( myU1 + myU2 ) /2.;
293 UCouture = Standard_True;
297 }// switch curve type
301 case GeomAbs_Cylinder: {
302 gp_Cylinder Cylinder = mySurface->Cylinder();
303 VCouture = Standard_False;
305 if (myCurve->GetType() == GeomAbs_Ellipse) {
307 Standard_Real U1, U2, V1, V2, U , V;
308 ElSLib::Parameters( Cylinder, P1, U1, V1);
309 ElSLib::Parameters( Cylinder, P2, U2, V2);
310 ElSLib::Parameters( Cylinder, P , U , V );
314 if ( !myCurve->IsClosed()) {
315 if ( myU1 < U && U < myU2) {
316 U = ( myU1 + myU2 ) /2.;
321 U = ( myU1 + myU2 ) /2.;
331 UCouture = Standard_True;
337 myCurve->D1(W1,P3d,T);
338 mySurface->D1(U1,U2,P3d,D1U,D1V);
339 Standard_Real dU = T.Dot(D1U);
341 UCouture = Standard_True;
343 myU2 = myU1 + 2*M_PI;
352 Standard_Real U1, V1, U , V;
353 ElSLib::Parameters( Cylinder, P1, U1, V1);
354 Standard_Real Step = .1, Delta = 0.;
355 Standard_Real eps = M_PI, dmax = 0., d = 0.;
356 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
358 Step = (W2 - W1) / (nbp - 1);
359 myU1 = U1; myU2 = U1;
360 Standard_Real pmin = W1, pmax = W1, plim = W2+.1*Step;
361 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
362 P = myCurve->Value(par);
363 ElSLib::Parameters( Cylinder, P, U, V);
378 dmax = Max(dmax, Abs(d));
379 if(U < myU1) {myU1 = U; pmin = par;}
380 if(U > myU2) {myU2 = U; pmax = par;}
384 if(!(Abs(pmin - W1) <= Precision::PConfusion() ||
385 Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
386 if(!(Abs(pmax - W1) <= Precision::PConfusion() ||
387 Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
389 if((myU1 >=0. && myU1 <= 2*M_PI) &&
390 (myU2 >=0. && myU2 <= 2*M_PI) ) {
391 UCouture = Standard_False;
394 U = ( myU1 + myU2 ) /2.;
397 UCouture = Standard_True;
403 case GeomAbs_Sphere:{
404 VCouture = Standard_False;
405 gp_Sphere SP = mySurface->Sphere();
406 if ( myCurve->GetType() == GeomAbs_Circle) {
407 UCouture = Standard_True;
409 // on cherche a savoir le nombre de fois que la couture est
411 // si 0 ou 2 fois : la PCurve est fermee et dans l`intervalle
412 // [Uc-PI, Uc+PI] (Uc: U du centre du cercle)
413 // si 1 fois : la PCurve est ouverte et dans l`intervalle
416 // pour determiner le nombre de solution, on resoud le systeme
417 // x^2 + y^2 + z^2 = R^2 (1)
418 // A x + B y + C z + D = 0 (2)
421 // REM : (1) (2) : equation du cercle
422 // (1) (3) (4) : equation de la couture.
423 Standard_Integer NbSolutions = 0;
424 Standard_Real A, B, C, D, R, Tol = 1.e-10;
425 Standard_Real U1, U2, V1, V2;
428 gp_Circ Circle = myCurve->Circle();
429 Trsf.SetTransformation(SP.Position());
430 Circle.Transform(Trsf);
433 gp_Pln Plane( gp_Ax3(Circle.Position()));
434 Plane.Coefficients(A,B,C,D);
439 if ( ( R - Abs(D/A)) > Tol) NbSolutions = 2;
440 else if ( Abs(R - Abs(D/A))< Tol) NbSolutions = 1;
441 else NbSolutions = 0;
446 Standard_Real delta = R*R*(A*A+C*C) - D*D;
448 if ( Abs(delta) < Tol*Tol) {
449 if ( A*D > 0.) NbSolutions = 1;
451 else if ( delta > 0) {
456 if ( xx > Tol) NbSolutions++;
459 if ( xx > Tol) NbSolutions++;
465 Standard_Real UU = 0.;
466 ElSLib::Parameters(SP, P1, U1, V1);
467 Standard_Real eps = 10.*Epsilon(1.);
468 Standard_Real dt = Max(Precision::PConfusion(), 0.01*(W2-W1));
471 //May be U1 must be equal 2*PI?
472 gp_Pnt Pd = myCurve->Value(W1+dt);
473 Standard_Real ud, vd;
474 ElSLib::Parameters(SP, Pd, ud, vd);
475 if(Abs(U1 - ud) > M_PI)
480 else if(Abs(2.*M_PI - U1) < eps)
483 gp_Pnt Pd = myCurve->Value(W1+dt);
484 Standard_Real ud, vd;
485 ElSLib::Parameters(SP, Pd, ud, vd);
486 if(Abs(U1 - ud) > M_PI)
492 ElSLib::Parameters(SP, P2, U2, V1);
495 //May be U2 must be equal 2*PI?
496 gp_Pnt Pd = myCurve->Value(W2-dt);
497 Standard_Real ud, vd;
498 ElSLib::Parameters(SP, Pd, ud, vd);
499 if(Abs(U2 - ud) > M_PI)
504 else if(Abs(2.*M_PI - U2) < eps)
507 gp_Pnt Pd = myCurve->Value(W2-dt);
508 Standard_Real ud, vd;
509 ElSLib::Parameters(SP, Pd, ud, vd);
510 if(Abs(U2 - ud) > M_PI)
516 ElSLib::Parameters(SP, P, UU, V1);
517 Standard_Real UUmi = Min(Min(U1,UU),Min(UU,U2));
518 Standard_Real UUma = Max(Max(U1,UU),Max(UU,U2));
519 Standard_Boolean reCalc = ((UUmi >= 0. && UUmi <= M_PI) && (UUma >= 0. && UUma <= M_PI));
521 P2 = myCurve->Value(W1+M_PI/8);
522 ElSLib::Parameters(SP,P2,U2,V2);
524 if ( NbSolutions == 1) {
525 if ( Abs(U1-U2) > M_PI) { // on traverse la couture
535 else { // on ne traverse pas la couture
546 else { // 0 ou 2 solutions
547 gp_Pnt Center = Circle.Location();
549 ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
554 // eval the VCouture.
555 if ( (C==0) || Abs(Abs(D/C)-R) > 1.e-10) {
556 VCouture = Standard_False;
559 VCouture = Standard_True;
560 UCouture = Standard_True;
564 myV2 = 3 * M_PI / 2.;
567 myV1 = -3 * M_PI / 2.;
571 // si P1.Z() vaut +/- R on est sur le sommet : pas significatif.
572 gp_Pnt pp = P1.Transformed(Trsf);
574 if ( Abs( Abs(pp.Z()) - R) < Tol) {
575 gp_Pnt Center = Circle.Location();
577 ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
580 VCouture = Standard_False;
585 myV1 = -1.e+100; myV2 = 1.e+100;
586 Standard_Real UU1 = myU1, UU2 = myU2;
587 if((Abs(UU1) <= (2.*M_PI) && Abs(UU2) <= (2.*M_PI)) && NbSolutions == 1 && reCalc) {
588 gp_Pnt Center = Circle.Location();
590 ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
592 myU1 = Min(UU1,myU1);
593 myU2 = myU1 + 2.*M_PI;
597 }//if ( myCurve->GetType() == GeomAbs_Circle)
600 Standard_Real U1, V1, U , V;
601 ElSLib::Parameters( SP, P1, U1, V1);
602 Standard_Real Step = .1, Delta = 0.;
603 Standard_Real eps = M_PI, dmax = 0., d = 0.;
604 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
606 Step = (W2 - W1) / (nbp - 1);
607 myU1 = U1; myU2 = U1;
608 Standard_Real pmin = W1, pmax = W1, plim = W2+.1*Step;
609 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
610 P = myCurve->Value(par);
611 ElSLib::Parameters( SP, P, U, V);
626 dmax = Max(dmax, Abs(d));
627 if(U < myU1) {myU1 = U; pmin = par;}
628 if(U > myU2) {myU2 = U; pmax = par;}
632 if(!(Abs(pmin - W1) <= Precision::PConfusion() ||
633 Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
634 if(!(Abs(pmax - W1) <= Precision::PConfusion() ||
635 Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
637 if((myU1 >=0. && myU1 <= 2*M_PI) &&
638 (myU2 >=0. && myU2 <= 2*M_PI) ) {
641 UCouture = Standard_False;
644 U = ( myU1 + myU2 ) /2.;
647 UCouture = Standard_True;
650 VCouture = Standard_False;
656 gp_Torus TR = mySurface->Torus();
657 Standard_Real U1, V1, U , V;
658 ElSLib::Parameters( TR, P1, U1, V1);
659 Standard_Real Step = .1, DeltaU = 0., DeltaV = 0.;
660 Standard_Real eps = M_PI, dmaxU = 0., dU = 0., dmaxV = 0., dV = 0.;
661 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
663 Step = (W2 - W1) / (nbp - 1);
664 myU1 = U1; myU2 = U1;
665 myV1 = V1; myV2 = V1;
666 Standard_Real pminU = W1, pmaxU = W1, pminV = W1, pmaxV = W1,
668 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
669 P = myCurve->Value(par);
670 ElSLib::Parameters( TR, P, U, V);
699 dmaxU = Max(dmaxU, Abs(dU));
700 dmaxV = Max(dmaxV, Abs(dV));
701 if(U < myU1) {myU1 = U; pminU = par;}
702 if(U > myU2) {myU2 = U; pmaxU = par;}
703 if(V < myV1) {myV1 = V; pminV = par;}
704 if(V > myV2) {myV2 = V; pmaxV = par;}
709 if(!(Abs(pminU - W1) <= Precision::PConfusion() ||
710 Abs(pminU - W2) <= Precision::PConfusion()) ) myU1 -= dmaxU*.5;
711 if(!(Abs(pmaxU - W1) <= Precision::PConfusion() ||
712 Abs(pmaxU - W2) <= Precision::PConfusion()) ) myU2 += dmaxU*.5;
713 if(!(Abs(pminV - W1) <= Precision::PConfusion() ||
714 Abs(pminV - W2) <= Precision::PConfusion()) ) myV1 -= dmaxV*.5;
715 if(!(Abs(pmaxV - W1) <= Precision::PConfusion() ||
716 Abs(pmaxV - W2) <= Precision::PConfusion()) ) myV2 += dmaxV*.5;
718 if((myU1 >=0. && myU1 <= 2*M_PI) &&
719 (myU2 >=0. && myU2 <= 2*M_PI) ) {
722 UCouture = Standard_False;
725 U = ( myU1 + myU2 ) /2.;
728 UCouture = Standard_True;
730 if((myV1 >=0. && myV1 <= 2*M_PI) &&
731 (myV2 >=0. && myV2 <= 2*M_PI) ) {
732 VCouture = Standard_False;
735 V = ( myV1 + myV2 ) /2.;
738 VCouture = Standard_True;
746 UCouture = Standard_False;
747 VCouture = Standard_False;
754 //=======================================================================
755 //classn : ProjLib_Function
757 //=======================================================================
758 class ProjLib_Function : public AppCont_Function2d
760 Handle(Adaptor3d_HCurve) myCurve;
761 Handle(Adaptor3d_HSurface) mySurface;
765 Standard_Real myU1,myU2,myV1,myV2;
766 Standard_Boolean UCouture,VCouture;
768 ProjLib_Function(const Handle(Adaptor3d_HCurve)& C,
769 const Handle(Adaptor3d_HSurface)& S) :
770 myCurve(C), mySurface(S),
775 UCouture(Standard_False),
776 VCouture(Standard_False)
777 {Function_SetUVBounds(myU1,myU2,myV1,myV2,UCouture,VCouture,myCurve,mySurface);}
779 Standard_Real FirstParameter() const
780 {return (myCurve->FirstParameter() + 1.e-9);}
782 Standard_Real LastParameter() const
783 {return (myCurve->LastParameter() -1.e-9);}
786 gp_Pnt2d Value(const Standard_Real t) const
787 {return Function_Value(t,myCurve,mySurface,myU1,myU2,myV1,myV2,UCouture,VCouture);}
789 Standard_Boolean D1(const Standard_Real t, gp_Pnt2d& P, gp_Vec2d& V) const
790 {return Function_D1(t,P,V,myCurve,mySurface,myU1,myU2,myV1,myV2,UCouture,VCouture);}
793 //=======================================================================
794 //function : ProjLib_ComputeApprox
796 //=======================================================================
798 ProjLib_ComputeApprox::ProjLib_ComputeApprox
799 (const Handle(Adaptor3d_HCurve) & C,
800 const Handle(Adaptor3d_HSurface) & S,
801 const Standard_Real Tol )
803 // if the surface is a plane and the curve a BSpline or a BezierCurve,
804 // don`t make an Approx but only the projection of the poles.
806 myTolerance = Max(Precision::PApproximation(),Tol);
807 Standard_Integer NbKnots, NbPoles ;
808 GeomAbs_CurveType CType = C->GetType();
809 GeomAbs_SurfaceType SType = S->GetType();
811 Standard_Boolean SurfIsAnal = (SType != GeomAbs_BSplineSurface) &&
812 (SType != GeomAbs_BezierSurface) &&
813 (SType != GeomAbs_OtherSurface) ;
815 Standard_Boolean CurvIsAnal = (CType != GeomAbs_BSplineCurve) &&
816 (CType != GeomAbs_BezierCurve) &&
817 (CType != GeomAbs_OtherCurve) ;
819 Standard_Boolean simplecase = SurfIsAnal && CurvIsAnal;
821 if (CType == GeomAbs_BSplineCurve &&
822 SType == GeomAbs_Plane ) {
824 // get the poles and eventually the weights
825 Handle(Geom_BSplineCurve) BS = C->BSpline();
826 NbPoles = BS->NbPoles();
827 TColgp_Array1OfPnt P3d( 1, NbPoles);
828 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
829 TColStd_Array1OfReal Weights( 1, NbPoles);
830 if ( BS->IsRational()) BS->Weights(Weights);
832 gp_Pln Plane = S->Plane();
834 for ( Standard_Integer i = 1; i <= NbPoles; i++) {
835 ElSLib::Parameters( Plane, P3d(i), U, V);
836 Poles.SetValue(i,gp_Pnt2d(U,V));
838 NbKnots = BS->NbKnots();
839 TColStd_Array1OfReal Knots(1,NbKnots);
840 TColStd_Array1OfInteger Mults(1,NbKnots);
842 BS->Multiplicities(Mults) ;
843 // get the knots and mults if BSplineCurve
844 if ( BS->IsRational()) {
845 myBSpline = new Geom2d_BSplineCurve(Poles,
853 myBSpline = new Geom2d_BSplineCurve(Poles,
860 else if (CType == GeomAbs_BezierCurve &&
861 SType == GeomAbs_Plane ) {
863 // get the poles and eventually the weights
864 Handle(Geom_BezierCurve) BezierCurvePtr = C->Bezier() ;
865 NbPoles = BezierCurvePtr->NbPoles();
866 TColgp_Array1OfPnt P3d( 1, NbPoles);
867 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
868 TColStd_Array1OfReal Weights( 1, NbPoles);
869 if ( BezierCurvePtr->IsRational()) {
870 BezierCurvePtr->Weights(Weights);
872 BezierCurvePtr->Poles( P3d);
874 // project the 3D-Poles on the plane
876 gp_Pln Plane = S->Plane();
878 for ( Standard_Integer i = 1; i <= NbPoles; i++) {
879 ElSLib::Parameters( Plane, P3d(i), U, V);
880 Poles.SetValue(i,gp_Pnt2d(U,V));
882 if ( BezierCurvePtr->IsRational()) {
883 myBezier = new Geom2d_BezierCurve(Poles, Weights);
886 myBezier = new Geom2d_BezierCurve(Poles);
890 ProjLib_Function F( C, S);
894 Standard_Integer Nb = 20;
895 Standard_Real U1, U2, dU, U;
896 U1 = F.FirstParameter();
897 U2 = F.LastParameter();
898 dU = ( U2 - U1) / Nb;
899 TColStd_Array1OfInteger Mults(1,Nb+1);
900 TColStd_Array1OfReal Knots(1,Nb+1);
901 TColgp_Array1OfPnt2d Poles(1,Nb+1);
902 for ( Standard_Integer i = 1; i <= Nb+1; i++) {
904 Poles(i) = F.Value(U);
912 char* ResultName = "Result";
913 DrawTrSurf::Set(ResultName,new Geom2d_BSplineCurve(Poles,Knots,Mults,1));
914 // DrawTrSurf::Set("Result",new Geom2d_BSplineCurve(Poles,Knots,Mults,1));
920 Standard_Integer Deg1, Deg2;
930 Approx_FitAndDivide2d Fit(F,Deg1,Deg2,myTolerance,myTolerance,
932 if(Fit.IsAllApproximated()) {
934 Standard_Integer NbCurves = Fit.NbMultiCurves();
936 // on essaie de rendre la courbe au moins C1
937 Convert_CompBezierCurves2dToBSplineCurve2d Conv;
940 Standard_Real Tol3d,Tol2d;
941 for (i = 1; i <= NbCurves; i++) {
942 Fit.Error(i,Tol3d, Tol2d);
943 myTolerance = Max(myTolerance, Tol2d);
944 AppParCurves_MultiCurve MC = Fit.Value( i); //Charge la Ieme Curve
945 TColgp_Array1OfPnt2d Poles2d( 1, MC.Degree() + 1);//Recupere les poles
946 MC.Curve(1, Poles2d);
948 Conv.AddCurve(Poles2d);
951 //mise a jour des fields de ProjLib_Approx
954 NbPoles = Conv.NbPoles();
955 NbKnots = Conv.NbKnots();
958 if(NbPoles <= 0 || NbPoles > 100000)
960 if(NbKnots <= 0 || NbKnots > 100000)
963 TColgp_Array1OfPnt2d NewPoles(1,NbPoles);
964 TColStd_Array1OfReal NewKnots(1,NbKnots);
965 TColStd_Array1OfInteger NewMults(1,NbKnots);
967 Conv.KnotsAndMults(NewKnots,NewMults);
968 Conv.Poles(NewPoles);
970 BSplCLib::Reparametrize(C->FirstParameter(),
974 // il faut recadrer les poles de debut et de fin:
975 // ( Car pour les problemes de couture, on a du ouvrir l`intervalle
976 // de definition de la courbe.)
977 // On choisit de calculer ces poles par prolongement de la courbe
983 U = C->FirstParameter() - 1.e-9;
989 BSplCLib::NoWeights(),
993 NewPoles.SetValue(1,P);
994 U = C->LastParameter() + 1.e-9;
1000 BSplCLib::NoWeights(),
1004 NewPoles.SetValue(NbPoles,P);
1005 myBSpline = new Geom2d_BSplineCurve (NewPoles,
1011 Standard_Integer NbCurves = Fit.NbMultiCurves();
1013 Standard_Real Tol3d,Tol2d;
1014 Fit.Error(NbCurves,Tol3d, Tol2d);
1015 myTolerance = Tol2d;
1020 Standard_Real UFirst = F.FirstParameter();
1021 gp_Pnt P3d = C->Value( UFirst );
1022 Standard_Real u = 0., v = 0.;
1027 gp_Pln Plane = S->Plane();
1028 ElSLib::Parameters( Plane, P3d, u, v );
1031 case GeomAbs_Cylinder:
1033 gp_Cylinder Cylinder = S->Cylinder();
1034 ElSLib::Parameters( Cylinder, P3d, u, v );
1039 gp_Cone Cone = S->Cone();
1040 ElSLib::Parameters( Cone, P3d, u, v );
1043 case GeomAbs_Sphere:
1045 gp_Sphere Sphere = S->Sphere();
1046 ElSLib::Parameters( Sphere, P3d, u, v );
1051 gp_Torus Torus = S->Torus();
1052 ElSLib::Parameters( Torus, P3d, u, v );
1056 Standard_NoSuchObject::Raise("ProjLib_ComputeApprox::Value");
1058 Standard_Boolean ToMirror = Standard_False;
1059 Standard_Real du = 0., dv = 0.;
1060 Standard_Integer number;
1063 if (SType == GeomAbs_Sphere && Abs(u-F.myU1) > M_PI)
1065 ToMirror = Standard_True;
1069 Standard_Real newV = ElCLib::InPeriod( v, F.myV1, F.myV2 );
1070 number = (Standard_Integer) (Floor((newV-v)/(F.myV2-F.myV1)));
1071 dv -= number*(F.myV2-F.myV1);
1073 if (F.UCouture || (F.VCouture && SType == GeomAbs_Sphere))
1075 gp_Pnt2d P2d = F.Value( UFirst );
1076 number = (Standard_Integer) (Floor((P2d.X()-u)/M_PI + Epsilon(M_PI)));
1080 if (!myBSpline.IsNull())
1082 if (du != 0. || dv != 0.)
1083 myBSpline->Translate( gp_Vec2d(du,dv) );
1086 gp_Ax2d Axe( gp_Pnt2d(0.,0.), gp_Dir2d(1.,0.) );
1087 myBSpline->Mirror( Axe );
1093 //=======================================================================
1094 //function : BSpline
1096 //=======================================================================
1098 Handle(Geom2d_BSplineCurve) ProjLib_ComputeApprox::BSpline() const
1104 //=======================================================================
1107 //=======================================================================
1109 Handle(Geom2d_BezierCurve) ProjLib_ComputeApprox::Bezier() const
1116 //=======================================================================
1117 //function : Tolerance
1119 //=======================================================================
1121 Standard_Real ProjLib_ComputeApprox::Tolerance() const