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
9 // under the terms of the GNU Lesser General Public 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 <Handle_Adaptor3d_HCurve.hxx>
36 #include <Adaptor3d_HCurve.hxx>
37 #include <Handle_Adaptor3d_HSurface.hxx>
38 #include <Adaptor3d_HSurface.hxx>
39 #include <TColgp_Array1OfPnt2d.hxx>
40 #include <TColgp_Array1OfPnt.hxx>
41 #include <TColStd_Array1OfReal.hxx>
42 #include <TColStd_Array1OfInteger.hxx>
43 #include <Geom_BSplineCurve.hxx>
44 #include <Geom_BezierCurve.hxx>
45 #include <Geom2d_BSplineCurve.hxx>
46 #include <Geom2d_BezierCurve.hxx>
49 #include <DrawTrSurf.hxx>
52 static Standard_Boolean AffichValue = Standard_False;
56 void Parameters(const Handle(Adaptor3d_HCurve)& myCurve,
57 const Handle(Adaptor3d_HSurface)& mySurface,
59 const Standard_Integer iFirst,
60 const Standard_Real aTolU,
64 //=======================================================================
67 //=======================================================================
69 static inline Standard_Boolean IsEqual(Standard_Real Check,Standard_Real With,Standard_Real Toler)
71 return ((Abs(Check - With) < Toler) ? Standard_True : Standard_False);
75 //=======================================================================
78 //=======================================================================
80 static gp_Pnt2d Function_Value(const Standard_Real U,
81 const Handle(Adaptor3d_HCurve)& myCurve,
82 const Handle(Adaptor3d_HSurface)& mySurface,
83 const Standard_Real U1,
84 const Standard_Real U2,
85 const Standard_Real V1,
86 const Standard_Real V2,
87 const Standard_Boolean UCouture,
88 const Standard_Boolean VCouture )
90 Standard_Real S = 0., T = 0.;
92 gp_Pnt P3d = myCurve->Value(U);
93 GeomAbs_SurfaceType SType = mySurface->GetType();
99 gp_Pln Plane = mySurface->Plane();
100 ElSLib::Parameters( Plane, P3d, S, T);
103 case GeomAbs_Cylinder:
105 gp_Cylinder Cylinder = mySurface->Cylinder();
106 ElSLib::Parameters( Cylinder, P3d, S, T);
111 gp_Cone Cone = mySurface->Cone();
112 ElSLib::Parameters( Cone, P3d, S, T);
117 gp_Sphere Sphere = mySurface->Sphere();
118 ElSLib::Parameters(Sphere, P3d, S, T);
123 gp_Torus Torus = mySurface->Torus();
124 ElSLib::Parameters( Torus, P3d, S, T);
128 Standard_NoSuchObject::Raise("ProjLib_ComputeApprox::Value");
133 S = ElCLib::InPeriod(S, U1, U2);
137 if(SType == GeomAbs_Sphere) {
138 if ( Abs( S - U1 ) > M_PI ) {
143 S = ElCLib::InPeriod(S, U1, U2);
146 T = ElCLib::InPeriod(T, V1, V2);
149 return gp_Pnt2d(S, T);
151 //=======================================================================
154 //=======================================================================
155 static Standard_Boolean Function_D1( const Standard_Real U,
158 const Handle(Adaptor3d_HCurve)& myCurve,
159 const Handle(Adaptor3d_HSurface)& mySurface,
160 const Standard_Real U1,
161 const Standard_Real U2,
162 const Standard_Real V1,
163 const Standard_Real V2,
164 const Standard_Boolean UCouture,
165 const Standard_Boolean VCouture )
168 Standard_Real dU, dV;
170 P = Function_Value(U,myCurve,mySurface,U1,U2,V1,V2,UCouture,VCouture);
172 GeomAbs_SurfaceType Type = mySurface->GetType();
177 case GeomAbs_Cylinder:
183 myCurve->D1(U,P3d,T);
184 mySurface->D1(P.X(),P.Y(),P3d,D1U,D1V);
188 Standard_Real Nu = D1U.SquareMagnitude();
189 Standard_Real Nv = D1V.SquareMagnitude();
191 if ( Nu < Epsilon(1.) || Nv < Epsilon(1.))
192 return Standard_False;
196 D = gp_Vec2d( dU, dV);
201 return Standard_False;
204 return Standard_True;
207 //=======================================================================
208 //function : Function_SetUVBounds
210 //=======================================================================
211 static void Function_SetUVBounds(Standard_Real& myU1,
215 Standard_Boolean& UCouture,
216 Standard_Boolean& VCouture,
217 const Handle(Adaptor3d_HCurve)& myCurve,
218 const Handle(Adaptor3d_HSurface)& mySurface)
220 Standard_Real W1, W2, W;
223 W1 = myCurve->FirstParameter();
224 W2 = myCurve->LastParameter ();
226 // on ouvre l`intervalle
229 P1 = myCurve->Value(W1);
230 P2 = myCurve->Value(W2);
231 P = myCurve->Value(W);
233 switch ( mySurface->GetType()) {
236 gp_Cone Cone = mySurface->Cone();
237 VCouture = Standard_False;
239 switch( myCurve->GetType() ){
240 case GeomAbs_Parabola:
241 case GeomAbs_Hyperbola:
242 case GeomAbs_Ellipse:{
243 Standard_Real U1, U2, V1, V2, U , V;
244 ElSLib::Parameters( Cone, P1, U1, V1);
245 ElSLib::Parameters( Cone, P2, U2, V2);
246 ElSLib::Parameters( Cone, P , U , V );
249 if ( ( U1 < U && U < U2 ) && !myCurve->IsClosed() ) {
250 UCouture = Standard_False;
253 UCouture = Standard_True;
254 myU2 = myU1 + 2*M_PI;
260 Standard_Real U1, V1, U , V, Delta = 0., d = 0., pmin = W1, pmax = W1, dmax = 0., Uf, Ul;
261 ElSLib::Parameters( Cone, P1, U1, V1);
262 ElSLib::Parameters( Cone, P2, Ul, V1);
263 myU1 = U1; myU2 = U1; Uf = U1;
264 Standard_Real Step = .1;
265 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
267 Step = (W2 - W1) / (nbp - 1);
268 Standard_Boolean isclandper = (!(myCurve->IsClosed()) && !(myCurve->IsPeriodic()));
269 for(Standard_Real par = W1 + Step; par <= W2; par += Step) {
270 if(!isclandper) par += Step;
271 P = myCurve->Value(par);
272 ElSLib::Parameters( Cone, P, U, V);
276 if( ( (IsEqual(U,(2*M_PI),1.e-10) && (U1 >= 0. && U1 <= M_PI)) &&
277 (IsEqual(U,Ul,1.e-10) && !IsEqual(Uf,0.,1.e-10)) ) && isclandper ) U = 0.;
278 else Delta -= 2*M_PI;
283 if( ( (IsEqual(U,0.,1.e-10) && (U1 >= M_PI && U1 <= (2*M_PI))) &&
284 (IsEqual(U,Ul,1.e-10) && !IsEqual(Uf,(2*M_PI),1.e-10)) ) && isclandper ) U = 2*M_PI;
285 else Delta += 2*M_PI;
289 dmax = Max(dmax, Abs(d));
290 if(U < myU1) {myU1 = U; pmin = par;}
291 if(U > myU2) {myU2 = U; pmax = par;}
295 if(!(Abs(pmin - W1) <= Precision::PConfusion() || Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
296 if(!(Abs(pmax - W1) <= Precision::PConfusion() || Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
298 if((myU1 >=0. && myU1 <= 2*M_PI) && (myU2 >=0. && myU2 <= 2*M_PI) ) UCouture = Standard_False;
300 U = ( myU1 + myU2 ) /2.;
303 UCouture = Standard_True;
307 }// switch curve type
311 case GeomAbs_Cylinder: {
312 gp_Cylinder Cylinder = mySurface->Cylinder();
313 VCouture = Standard_False;
315 if (myCurve->GetType() == GeomAbs_Ellipse) {
317 Standard_Real U1, U2, V1, V2, U , V;
318 ElSLib::Parameters( Cylinder, P1, U1, V1);
319 ElSLib::Parameters( Cylinder, P2, U2, V2);
320 ElSLib::Parameters( Cylinder, P , U , V );
324 if ( !myCurve->IsClosed()) {
325 if ( myU1 < U && U < myU2) {
326 U = ( myU1 + myU2 ) /2.;
331 U = ( myU1 + myU2 ) /2.;
341 UCouture = Standard_True;
347 myCurve->D1(W1,P3d,T);
348 mySurface->D1(U1,U2,P3d,D1U,D1V);
349 Standard_Real dU = T.Dot(D1U);
351 UCouture = Standard_True;
353 myU2 = myU1 + 2*M_PI;
362 Standard_Real U1, V1, U , V;
363 ElSLib::Parameters( Cylinder, P1, U1, V1);
364 Standard_Real Step = .1, Delta = 0.;
365 Standard_Real eps = M_PI, dmax = 0., d = 0.;
366 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
368 Step = (W2 - W1) / (nbp - 1);
369 myU1 = U1; myU2 = U1;
370 Standard_Real pmin = W1, pmax = W1, plim = W2+.1*Step;
371 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
372 P = myCurve->Value(par);
373 ElSLib::Parameters( Cylinder, P, U, V);
388 dmax = Max(dmax, Abs(d));
389 if(U < myU1) {myU1 = U; pmin = par;}
390 if(U > myU2) {myU2 = U; pmax = par;}
394 if(!(Abs(pmin - W1) <= Precision::PConfusion() ||
395 Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
396 if(!(Abs(pmax - W1) <= Precision::PConfusion() ||
397 Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
399 if((myU1 >=0. && myU1 <= 2*M_PI) &&
400 (myU2 >=0. && myU2 <= 2*M_PI) ) {
401 UCouture = Standard_False;
404 U = ( myU1 + myU2 ) /2.;
407 UCouture = Standard_True;
413 case GeomAbs_Sphere:{
414 VCouture = Standard_False;
415 gp_Sphere SP = mySurface->Sphere();
416 if ( myCurve->GetType() == GeomAbs_Circle) {
417 UCouture = Standard_True;
419 // on cherche a savoir le nombre de fois que la couture est
421 // si 0 ou 2 fois : la PCurve est fermee et dans l`intervalle
422 // [Uc-PI, Uc+PI] (Uc: U du centre du cercle)
423 // si 1 fois : la PCurve est ouverte et dans l`intervalle
426 // pour determiner le nombre de solution, on resoud le systeme
427 // x^2 + y^2 + z^2 = R^2 (1)
428 // A x + B y + C z + D = 0 (2)
431 // REM : (1) (2) : equation du cercle
432 // (1) (3) (4) : equation de la couture.
433 Standard_Integer NbSolutions = 0;
434 Standard_Real A, B, C, D, R, Tol = 1.e-10;
435 Standard_Real U1, U2, V1, V2;
438 gp_Circ Circle = myCurve->Circle();
439 Trsf.SetTransformation(SP.Position());
440 Circle.Transform(Trsf);
443 gp_Pln Plane( gp_Ax3(Circle.Position()));
444 Plane.Coefficients(A,B,C,D);
449 if ( ( R - Abs(D/A)) > Tol) NbSolutions = 2;
450 else if ( Abs(R - Abs(D/A))< Tol) NbSolutions = 1;
451 else NbSolutions = 0;
456 Standard_Real delta = R*R*(A*A+C*C) - D*D;
458 if ( Abs(delta) < Tol*Tol) {
459 if ( A*D > 0.) NbSolutions = 1;
461 else if ( delta > 0) {
466 if ( xx > Tol) NbSolutions++;
469 if ( xx > Tol) NbSolutions++;
475 Standard_Real UU = 0.;
476 ElSLib::Parameters(SP, P1, U1, V1);
477 Standard_Real eps = 2.*Epsilon(1.);
478 Standard_Real dt = Max(Precision::PConfusion(), 0.01*(W2-W1));
481 //May be U1 must be equal 2*PI?
482 gp_Pnt Pd = myCurve->Value(W1+dt);
483 Standard_Real ud, vd;
484 ElSLib::Parameters(SP, Pd, ud, vd);
485 if(Abs(U1 - ud) > M_PI)
490 else if(Abs(2.*M_PI - U1) < eps)
493 gp_Pnt Pd = myCurve->Value(W1+dt);
494 Standard_Real ud, vd;
495 ElSLib::Parameters(SP, Pd, ud, vd);
496 if(Abs(U1 - ud) > M_PI)
502 ElSLib::Parameters(SP, P2, U2, V1);
505 //May be U2 must be equal 2*PI?
506 gp_Pnt Pd = myCurve->Value(W2-dt);
507 Standard_Real ud, vd;
508 ElSLib::Parameters(SP, Pd, ud, vd);
509 if(Abs(U2 - ud) > M_PI)
514 else if(Abs(2.*M_PI - U2) < eps)
517 gp_Pnt Pd = myCurve->Value(W2-dt);
518 Standard_Real ud, vd;
519 ElSLib::Parameters(SP, Pd, ud, vd);
520 if(Abs(U2 - ud) > M_PI)
526 ElSLib::Parameters(SP, P, UU, V1);
527 Standard_Real UUmi = Min(Min(U1,UU),Min(UU,U2));
528 Standard_Real UUma = Max(Max(U1,UU),Max(UU,U2));
529 Standard_Boolean reCalc = ((UUmi >= 0. && UUmi <= M_PI) && (UUma >= 0. && UUma <= M_PI));
531 P2 = myCurve->Value(W1+M_PI/8);
532 ElSLib::Parameters(SP,P2,U2,V2);
534 if ( NbSolutions == 1) {
535 if ( Abs(U1-U2) > M_PI) { // on traverse la couture
545 else { // on ne traverse pas la couture
556 else { // 0 ou 2 solutions
557 gp_Pnt Center = Circle.Location();
559 ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
564 // eval the VCouture.
565 if ( (C==0) || Abs(Abs(D/C)-R) > 1.e-10) {
566 VCouture = Standard_False;
569 VCouture = Standard_True;
570 UCouture = Standard_True;
574 myV2 = 3 * M_PI / 2.;
577 myV1 = -3 * M_PI / 2.;
581 // si P1.Z() vaut +/- R on est sur le sommet : pas significatif.
582 gp_Pnt pp = P1.Transformed(Trsf);
584 if ( Abs( Abs(pp.Z()) - R) < Tol) {
585 gp_Pnt Center = Circle.Location();
587 ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
590 VCouture = Standard_False;
595 myV1 = -1.e+100; myV2 = 1.e+100;
596 Standard_Real UU1 = myU1, UU2 = myU2;
597 if((Abs(UU1) <= (2.*M_PI) && Abs(UU2) <= (2.*M_PI)) && NbSolutions == 1 && reCalc) {
598 gp_Pnt Center = Circle.Location();
600 ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
602 myU1 = Min(UU1,myU1);
603 myU2 = myU1 + 2.*M_PI;
607 }//if ( myCurve->GetType() == GeomAbs_Circle)
610 Standard_Real U1, V1, U , V;
611 ElSLib::Parameters( SP, P1, U1, V1);
612 Standard_Real Step = .1, Delta = 0.;
613 Standard_Real eps = M_PI, dmax = 0., d = 0.;
614 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
616 Step = (W2 - W1) / (nbp - 1);
617 myU1 = U1; myU2 = U1;
618 Standard_Real pmin = W1, pmax = W1, plim = W2+.1*Step;
619 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
620 P = myCurve->Value(par);
621 ElSLib::Parameters( SP, P, U, V);
636 dmax = Max(dmax, Abs(d));
637 if(U < myU1) {myU1 = U; pmin = par;}
638 if(U > myU2) {myU2 = U; pmax = par;}
642 if(!(Abs(pmin - W1) <= Precision::PConfusion() ||
643 Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
644 if(!(Abs(pmax - W1) <= Precision::PConfusion() ||
645 Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
647 if((myU1 >=0. && myU1 <= 2*M_PI) &&
648 (myU2 >=0. && myU2 <= 2*M_PI) ) {
651 UCouture = Standard_False;
654 U = ( myU1 + myU2 ) /2.;
657 UCouture = Standard_True;
660 VCouture = Standard_False;
666 gp_Torus TR = mySurface->Torus();
667 Standard_Real U1, V1, U , V;
668 ElSLib::Parameters( TR, P1, U1, V1);
669 Standard_Real Step = .1, DeltaU = 0., DeltaV = 0.;
670 Standard_Real eps = M_PI, dmaxU = 0., dU = 0., dmaxV = 0., dV = 0.;
671 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
673 Step = (W2 - W1) / (nbp - 1);
674 myU1 = U1; myU2 = U1;
675 myV1 = V1; myV2 = V1;
676 Standard_Real pminU = W1, pmaxU = W1, pminV = W1, pmaxV = W1,
678 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
679 P = myCurve->Value(par);
680 ElSLib::Parameters( TR, P, U, V);
709 dmaxU = Max(dmaxU, Abs(dU));
710 dmaxV = Max(dmaxV, Abs(dV));
711 if(U < myU1) {myU1 = U; pminU = par;}
712 if(U > myU2) {myU2 = U; pmaxU = par;}
713 if(V < myV1) {myV1 = V; pminV = par;}
714 if(V > myV2) {myV2 = V; pmaxV = par;}
719 if(!(Abs(pminU - W1) <= Precision::PConfusion() ||
720 Abs(pminU - W2) <= Precision::PConfusion()) ) myU1 -= dmaxU*.5;
721 if(!(Abs(pmaxU - W1) <= Precision::PConfusion() ||
722 Abs(pmaxU - W2) <= Precision::PConfusion()) ) myU2 += dmaxU*.5;
723 if(!(Abs(pminV - W1) <= Precision::PConfusion() ||
724 Abs(pminV - W2) <= Precision::PConfusion()) ) myV1 -= dmaxV*.5;
725 if(!(Abs(pmaxV - W1) <= Precision::PConfusion() ||
726 Abs(pmaxV - W2) <= Precision::PConfusion()) ) myV2 += dmaxV*.5;
728 if((myU1 >=0. && myU1 <= 2*M_PI) &&
729 (myU2 >=0. && myU2 <= 2*M_PI) ) {
732 UCouture = Standard_False;
735 U = ( myU1 + myU2 ) /2.;
738 UCouture = Standard_True;
740 if((myV1 >=0. && myV1 <= 2*M_PI) &&
741 (myV2 >=0. && myV2 <= 2*M_PI) ) {
742 VCouture = Standard_False;
745 V = ( myV1 + myV2 ) /2.;
748 VCouture = Standard_True;
756 UCouture = Standard_False;
757 VCouture = Standard_False;
764 //=======================================================================
765 //classn : ProjLib_Function
767 //=======================================================================
768 class ProjLib_Function : public AppCont_Function2d
770 Handle(Adaptor3d_HCurve) myCurve;
771 Handle(Adaptor3d_HSurface) mySurface;
775 Standard_Real myU1,myU2,myV1,myV2;
776 Standard_Boolean UCouture,VCouture;
778 ProjLib_Function(const Handle(Adaptor3d_HCurve)& C,
779 const Handle(Adaptor3d_HSurface)& S) :
780 myCurve(C), mySurface(S),
785 UCouture(Standard_False),
786 VCouture(Standard_False)
787 {Function_SetUVBounds(myU1,myU2,myV1,myV2,UCouture,VCouture,myCurve,mySurface);}
789 Standard_Real FirstParameter() const
790 {return (myCurve->FirstParameter() + 1.e-9);}
792 Standard_Real LastParameter() const
793 {return (myCurve->LastParameter() -1.e-9);}
796 gp_Pnt2d Value(const Standard_Real t) const
797 {return Function_Value(t,myCurve,mySurface,myU1,myU2,myV1,myV2,UCouture,VCouture);}
799 Standard_Boolean D1(const Standard_Real t, gp_Pnt2d& P, gp_Vec2d& V) const
800 {return Function_D1(t,P,V,myCurve,mySurface,myU1,myU2,myV1,myV2,UCouture,VCouture);}
803 //=======================================================================
804 //function : ProjLib_ComputeApprox
806 //=======================================================================
808 ProjLib_ComputeApprox::ProjLib_ComputeApprox
809 (const Handle(Adaptor3d_HCurve) & C,
810 const Handle(Adaptor3d_HSurface) & S,
811 const Standard_Real Tol )
813 // if the surface is a plane and the curve a BSpline or a BezierCurve,
814 // don`t make an Approx but only the projection of the poles.
816 myTolerance = Max(Precision::PApproximation(),Tol);
817 Standard_Integer NbKnots, NbPoles ;
818 GeomAbs_CurveType CType = C->GetType();
819 GeomAbs_SurfaceType SType = S->GetType();
821 Standard_Boolean SurfIsAnal = (SType != GeomAbs_BSplineSurface) &&
822 (SType != GeomAbs_BezierSurface) &&
823 (SType != GeomAbs_OtherSurface) ;
825 Standard_Boolean CurvIsAnal = (CType != GeomAbs_BSplineCurve) &&
826 (CType != GeomAbs_BezierCurve) &&
827 (CType != GeomAbs_OtherCurve) ;
829 Standard_Boolean simplecase = SurfIsAnal && CurvIsAnal;
831 if (CType == GeomAbs_BSplineCurve &&
832 SType == GeomAbs_Plane ) {
834 // get the poles and eventually the weights
835 Handle(Geom_BSplineCurve) BS = C->BSpline();
836 NbPoles = BS->NbPoles();
837 TColgp_Array1OfPnt P3d( 1, NbPoles);
838 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
839 TColStd_Array1OfReal Weights( 1, NbPoles);
840 if ( BS->IsRational()) BS->Weights(Weights);
842 gp_Pln Plane = S->Plane();
844 for ( Standard_Integer i = 1; i <= NbPoles; i++) {
845 ElSLib::Parameters( Plane, P3d(i), U, V);
846 Poles.SetValue(i,gp_Pnt2d(U,V));
848 NbKnots = BS->NbKnots();
849 TColStd_Array1OfReal Knots(1,NbKnots);
850 TColStd_Array1OfInteger Mults(1,NbKnots);
852 BS->Multiplicities(Mults) ;
853 // get the knots and mults if BSplineCurve
854 if ( BS->IsRational()) {
855 myBSpline = new Geom2d_BSplineCurve(Poles,
863 myBSpline = new Geom2d_BSplineCurve(Poles,
870 else if (CType == GeomAbs_BezierCurve &&
871 SType == GeomAbs_Plane ) {
873 // get the poles and eventually the weights
874 Handle(Geom_BezierCurve) BezierCurvePtr = C->Bezier() ;
875 NbPoles = BezierCurvePtr->NbPoles();
876 TColgp_Array1OfPnt P3d( 1, NbPoles);
877 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
878 TColStd_Array1OfReal Weights( 1, NbPoles);
879 if ( BezierCurvePtr->IsRational()) {
880 BezierCurvePtr->Weights(Weights);
882 BezierCurvePtr->Poles( P3d);
884 // project the 3D-Poles on the plane
886 gp_Pln Plane = S->Plane();
888 for ( Standard_Integer i = 1; i <= NbPoles; i++) {
889 ElSLib::Parameters( Plane, P3d(i), U, V);
890 Poles.SetValue(i,gp_Pnt2d(U,V));
892 if ( BezierCurvePtr->IsRational()) {
893 myBezier = new Geom2d_BezierCurve(Poles, Weights);
896 myBezier = new Geom2d_BezierCurve(Poles);
900 ProjLib_Function F( C, S);
904 Standard_Integer Nb = 20;
905 Standard_Real U1, U2, dU, U;
906 U1 = F.FirstParameter();
907 U2 = F.LastParameter();
908 dU = ( U2 - U1) / Nb;
909 TColStd_Array1OfInteger Mults(1,Nb+1);
910 TColStd_Array1OfReal Knots(1,Nb+1);
911 TColgp_Array1OfPnt2d Poles(1,Nb+1);
912 for ( Standard_Integer i = 1; i <= Nb+1; i++) {
914 Poles(i) = F.Value(U);
922 char* ResultName = "Result";
923 DrawTrSurf::Set(ResultName,new Geom2d_BSplineCurve(Poles,Knots,Mults,1));
924 // DrawTrSurf::Set("Result",new Geom2d_BSplineCurve(Poles,Knots,Mults,1));
930 Standard_Integer Deg1, Deg2;
940 Approx_FitAndDivide2d Fit(F,Deg1,Deg2,myTolerance,myTolerance,
942 if(Fit.IsAllApproximated()) {
944 Standard_Integer NbCurves = Fit.NbMultiCurves();
946 // on essaie de rendre la courbe au moins C1
947 Convert_CompBezierCurves2dToBSplineCurve2d Conv;
950 Standard_Real Tol3d,Tol2d;
951 for (i = 1; i <= NbCurves; i++) {
952 Fit.Error(i,Tol3d, Tol2d);
953 myTolerance = Max(myTolerance, Tol2d);
954 AppParCurves_MultiCurve MC = Fit.Value( i); //Charge la Ieme Curve
955 TColgp_Array1OfPnt2d Poles2d( 1, MC.Degree() + 1);//Recupere les poles
956 MC.Curve(1, Poles2d);
958 Conv.AddCurve(Poles2d);
961 //mise a jour des fields de ProjLib_Approx
964 NbPoles = Conv.NbPoles();
965 NbKnots = Conv.NbKnots();
968 if(NbPoles <= 0 || NbPoles > 100000)
970 if(NbKnots <= 0 || NbKnots > 100000)
973 TColgp_Array1OfPnt2d NewPoles(1,NbPoles);
974 TColStd_Array1OfReal NewKnots(1,NbKnots);
975 TColStd_Array1OfInteger NewMults(1,NbKnots);
977 Conv.KnotsAndMults(NewKnots,NewMults);
978 Conv.Poles(NewPoles);
980 BSplCLib::Reparametrize(C->FirstParameter(),
984 // il faut recadrer les poles de debut et de fin:
985 // ( Car pour les problemes de couture, on a du ouvrir l`intervalle
986 // de definition de la courbe.)
987 // On choisit de calculer ces poles par prolongement de la courbe
993 U = C->FirstParameter() - 1.e-9;
999 BSplCLib::NoWeights(),
1003 NewPoles.SetValue(1,P);
1004 U = C->LastParameter() + 1.e-9;
1010 BSplCLib::NoWeights(),
1014 NewPoles.SetValue(NbPoles,P);
1015 myBSpline = new Geom2d_BSplineCurve (NewPoles,
1021 Standard_Integer NbCurves = Fit.NbMultiCurves();
1023 Standard_Real Tol3d,Tol2d;
1024 Fit.Error(NbCurves,Tol3d, Tol2d);
1025 myTolerance = Tol2d;
1030 Standard_Real UFirst = F.FirstParameter();
1031 gp_Pnt P3d = C->Value( UFirst );
1032 Standard_Real u = 0., v = 0.;
1037 gp_Pln Plane = S->Plane();
1038 ElSLib::Parameters( Plane, P3d, u, v );
1041 case GeomAbs_Cylinder:
1043 gp_Cylinder Cylinder = S->Cylinder();
1044 ElSLib::Parameters( Cylinder, P3d, u, v );
1049 gp_Cone Cone = S->Cone();
1050 ElSLib::Parameters( Cone, P3d, u, v );
1053 case GeomAbs_Sphere:
1055 gp_Sphere Sphere = S->Sphere();
1056 ElSLib::Parameters( Sphere, P3d, u, v );
1061 gp_Torus Torus = S->Torus();
1062 ElSLib::Parameters( Torus, P3d, u, v );
1066 Standard_NoSuchObject::Raise("ProjLib_ComputeApprox::Value");
1068 Standard_Boolean ToMirror = Standard_False;
1069 Standard_Real du = 0., dv = 0.;
1070 Standard_Integer number;
1073 if (SType == GeomAbs_Sphere && Abs(u-F.myU1) > M_PI)
1075 ToMirror = Standard_True;
1079 Standard_Real newV = ElCLib::InPeriod( v, F.myV1, F.myV2 );
1080 number = (Standard_Integer) (Floor((newV-v)/(F.myV2-F.myV1)));
1081 dv -= number*(F.myV2-F.myV1);
1083 if (F.UCouture || (F.VCouture && SType == GeomAbs_Sphere))
1085 gp_Pnt2d P2d = F.Value( UFirst );
1086 number = (Standard_Integer) (Floor((P2d.X()-u)/M_PI + Epsilon(M_PI)));
1090 if (!myBSpline.IsNull())
1092 if (du != 0. || dv != 0.)
1093 myBSpline->Translate( gp_Vec2d(du,dv) );
1096 gp_Ax2d Axe( gp_Pnt2d(0.,0.), gp_Dir2d(1.,0.) );
1097 myBSpline->Mirror( Axe );
1103 //=======================================================================
1104 //function : BSpline
1106 //=======================================================================
1108 Handle(Geom2d_BSplineCurve) ProjLib_ComputeApprox::BSpline() const
1114 //=======================================================================
1117 //=======================================================================
1119 Handle(Geom2d_BezierCurve) ProjLib_ComputeApprox::Bezier() const
1126 //=======================================================================
1127 //function : Tolerance
1129 //=======================================================================
1131 Standard_Real ProjLib_ComputeApprox::Tolerance() const