1 // Created on: 1993-09-07
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
21 // modified by NIZHNY-OFV Thu Jan 20 11:04:19 2005
23 #include <ProjLib_ComputeApprox.hxx>
25 #include <GeomAbs_SurfaceType.hxx>
26 #include <GeomAbs_CurveType.hxx>
27 #include <AppCont_Function2d.hxx>
28 #include <Convert_CompBezierCurves2dToBSplineCurve2d.hxx>
31 #include <BSplCLib.hxx>
32 #include <Standard_NoSuchObject.hxx>
33 #include <Geom_UndefinedDerivative.hxx>
35 #include <gp_Trsf.hxx>
36 #include <Precision.hxx>
37 #include <Approx_FitAndDivide2d.hxx>
38 #include <AppParCurves_MultiCurve.hxx>
39 #include <Handle_Adaptor3d_HCurve.hxx>
40 #include <Adaptor3d_HCurve.hxx>
41 #include <Handle_Adaptor3d_HSurface.hxx>
42 #include <Adaptor3d_HSurface.hxx>
43 #include <TColgp_Array1OfPnt2d.hxx>
44 #include <TColgp_Array1OfPnt.hxx>
45 #include <TColStd_Array1OfReal.hxx>
46 #include <TColStd_Array1OfInteger.hxx>
47 #include <Geom_BSplineCurve.hxx>
48 #include <Geom_BezierCurve.hxx>
49 #include <Geom2d_BSplineCurve.hxx>
50 #include <Geom2d_BezierCurve.hxx>
53 #include <DrawTrSurf.hxx>
56 static Standard_Boolean AffichValue = Standard_False;
60 void Parameters(const Handle(Adaptor3d_HCurve)& myCurve,
61 const Handle(Adaptor3d_HSurface)& mySurface,
63 const Standard_Integer iFirst,
64 const Standard_Real aTolU,
68 //=======================================================================
71 //=======================================================================
73 static inline Standard_Boolean IsEqual(Standard_Real Check,Standard_Real With,Standard_Real Toler)
75 return ((Abs(Check - With) < Toler) ? Standard_True : Standard_False);
79 //=======================================================================
82 //=======================================================================
84 static gp_Pnt2d Function_Value(const Standard_Real U,
85 const Handle(Adaptor3d_HCurve)& myCurve,
86 const Handle(Adaptor3d_HSurface)& mySurface,
87 const Standard_Real U1,
88 const Standard_Real U2,
89 const Standard_Real V1,
90 const Standard_Real V2,
91 const Standard_Boolean UCouture,
92 const Standard_Boolean VCouture )
94 Standard_Real S = 0., T = 0.;
96 gp_Pnt P3d = myCurve->Value(U);
97 GeomAbs_SurfaceType SType = mySurface->GetType();
103 gp_Pln Plane = mySurface->Plane();
104 ElSLib::Parameters( Plane, P3d, S, T);
107 case GeomAbs_Cylinder:
109 gp_Cylinder Cylinder = mySurface->Cylinder();
110 ElSLib::Parameters( Cylinder, P3d, S, T);
115 gp_Cone Cone = mySurface->Cone();
116 ElSLib::Parameters( Cone, P3d, S, T);
121 gp_Sphere Sphere = mySurface->Sphere();
122 ElSLib::Parameters(Sphere, P3d, S, T);
127 gp_Torus Torus = mySurface->Torus();
128 ElSLib::Parameters( Torus, P3d, S, T);
132 Standard_NoSuchObject::Raise("ProjLib_ComputeApprox::Value");
136 S = ElCLib::InPeriod(S, U1, U2);
140 if(SType == GeomAbs_Sphere) {
141 if ( Abs( S - U1 ) > M_PI ) {
145 S = ElCLib::InPeriod(S, U1, U2);
147 T = ElCLib::InPeriod(T, V1, V2);
150 return gp_Pnt2d(S, T);
152 //=======================================================================
155 //=======================================================================
156 static Standard_Boolean Function_D1( const Standard_Real U,
159 const Handle(Adaptor3d_HCurve)& myCurve,
160 const Handle(Adaptor3d_HSurface)& mySurface,
161 const Standard_Real U1,
162 const Standard_Real U2,
163 const Standard_Real V1,
164 const Standard_Real V2,
165 const Standard_Boolean UCouture,
166 const Standard_Boolean VCouture )
169 Standard_Real dU, dV;
171 P = Function_Value(U,myCurve,mySurface,U1,U2,V1,V2,UCouture,VCouture);
173 GeomAbs_SurfaceType Type = mySurface->GetType();
178 case GeomAbs_Cylinder:
184 myCurve->D1(U,P3d,T);
185 mySurface->D1(P.X(),P.Y(),P3d,D1U,D1V);
189 Standard_Real Nu = D1U.SquareMagnitude();
190 Standard_Real Nv = D1V.SquareMagnitude();
192 if ( Nu < Epsilon(1.) || Nv < Epsilon(1.))
193 return Standard_False;
197 D = gp_Vec2d( dU, dV);
202 return Standard_False;
205 return Standard_True;
208 //=======================================================================
209 //function : Function_SetUVBounds
211 //=======================================================================
212 static void Function_SetUVBounds(Standard_Real& myU1,
216 Standard_Boolean& UCouture,
217 Standard_Boolean& VCouture,
218 const Handle(Adaptor3d_HCurve)& myCurve,
219 const Handle(Adaptor3d_HSurface)& mySurface)
221 Standard_Real W1, W2, W;
224 W1 = myCurve->FirstParameter();
225 W2 = myCurve->LastParameter ();
227 // on ouvre l`intervalle
230 P1 = myCurve->Value(W1);
231 P2 = myCurve->Value(W2);
232 P = myCurve->Value(W);
234 switch ( mySurface->GetType()) {
237 gp_Cone Cone = mySurface->Cone();
238 VCouture = Standard_False;
240 switch( myCurve->GetType() ){
241 case GeomAbs_Parabola:
242 case GeomAbs_Hyperbola:
243 case GeomAbs_Ellipse:{
244 Standard_Real U1, U2, V1, V2, U , V;
245 ElSLib::Parameters( Cone, P1, U1, V1);
246 ElSLib::Parameters( Cone, P2, U2, V2);
247 ElSLib::Parameters( Cone, P , U , V );
250 if ( ( U1 < U && U < U2 ) && !myCurve->IsClosed() ) {
251 UCouture = Standard_False;
254 UCouture = Standard_True;
255 myU2 = myU1 + 2*M_PI;
261 Standard_Real U1, V1, U , V, Delta = 0., d = 0., pmin = W1, pmax = W1, dmax = 0., Uf, Ul;
262 ElSLib::Parameters( Cone, P1, U1, V1);
263 ElSLib::Parameters( Cone, P2, Ul, V1);
264 myU1 = U1; myU2 = U1; Uf = U1;
265 Standard_Real Step = .1;
266 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
268 Step = (W2 - W1) / (nbp - 1);
269 Standard_Boolean isclandper = (!(myCurve->IsClosed()) && !(myCurve->IsPeriodic()));
270 for(Standard_Real par = W1 + Step; par <= W2; par += Step) {
271 if(!isclandper) par += Step;
272 P = myCurve->Value(par);
273 ElSLib::Parameters( Cone, P, U, V);
277 if( ( (IsEqual(U,(2*M_PI),1.e-10) && (U1 >= 0. && U1 <= M_PI)) &&
278 (IsEqual(U,Ul,1.e-10) && !IsEqual(Uf,0.,1.e-10)) ) && isclandper ) U = 0.;
279 else Delta -= 2*M_PI;
284 if( ( (IsEqual(U,0.,1.e-10) && (U1 >= M_PI && U1 <= (2*M_PI))) &&
285 (IsEqual(U,Ul,1.e-10) && !IsEqual(Uf,(2*M_PI),1.e-10)) ) && isclandper ) U = 2*M_PI;
286 else Delta += 2*M_PI;
290 dmax = Max(dmax, Abs(d));
291 if(U < myU1) {myU1 = U; pmin = par;}
292 if(U > myU2) {myU2 = U; pmax = par;}
296 if(!(Abs(pmin - W1) <= Precision::PConfusion() || Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
297 if(!(Abs(pmax - W1) <= Precision::PConfusion() || Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
299 if((myU1 >=0. && myU1 <= 2*M_PI) && (myU2 >=0. && myU2 <= 2*M_PI) ) UCouture = Standard_False;
301 U = ( myU1 + myU2 ) /2.;
304 UCouture = Standard_True;
308 }// switch curve type
312 case GeomAbs_Cylinder: {
313 gp_Cylinder Cylinder = mySurface->Cylinder();
314 VCouture = Standard_False;
316 if (myCurve->GetType() == GeomAbs_Ellipse) {
318 Standard_Real U1, U2, V1, V2, U , V;
319 ElSLib::Parameters( Cylinder, P1, U1, V1);
320 ElSLib::Parameters( Cylinder, P2, U2, V2);
321 ElSLib::Parameters( Cylinder, P , U , V );
325 if ( !myCurve->IsClosed()) {
326 if ( myU1 < U && U < myU2) {
327 U = ( myU1 + myU2 ) /2.;
332 U = ( myU1 + myU2 ) /2.;
342 UCouture = Standard_True;
348 myCurve->D1(W1,P3d,T);
349 mySurface->D1(U1,U2,P3d,D1U,D1V);
350 Standard_Real dU = T.Dot(D1U);
352 UCouture = Standard_True;
354 myU2 = myU1 + 2*M_PI;
363 Standard_Real U1, V1, U , V;
364 ElSLib::Parameters( Cylinder, P1, U1, V1);
365 Standard_Real Step = .1, Delta = 0.;
366 Standard_Real eps = M_PI, dmax = 0., d = 0.;
367 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
369 Step = (W2 - W1) / (nbp - 1);
370 myU1 = U1; myU2 = U1;
371 Standard_Real pmin = W1, pmax = W1, plim = W2+.1*Step;
372 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
373 P = myCurve->Value(par);
374 ElSLib::Parameters( Cylinder, P, U, V);
389 dmax = Max(dmax, Abs(d));
390 if(U < myU1) {myU1 = U; pmin = par;}
391 if(U > myU2) {myU2 = U; pmax = par;}
395 if(!(Abs(pmin - W1) <= Precision::PConfusion() ||
396 Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
397 if(!(Abs(pmax - W1) <= Precision::PConfusion() ||
398 Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
400 if((myU1 >=0. && myU1 <= 2*M_PI) &&
401 (myU2 >=0. && myU2 <= 2*M_PI) ) {
402 UCouture = Standard_False;
405 U = ( myU1 + myU2 ) /2.;
408 UCouture = Standard_True;
414 case GeomAbs_Sphere:{
415 VCouture = Standard_False;
416 gp_Sphere SP = mySurface->Sphere();
417 if ( myCurve->GetType() == GeomAbs_Circle) {
418 UCouture = Standard_True;
420 // on cherche a savoir le nombre de fois que la couture est
422 // si 0 ou 2 fois : la PCurve est fermee et dans l`intervalle
423 // [Uc-PI, Uc+PI] (Uc: U du centre du cercle)
424 // si 1 fois : la PCurve est ouverte et dans l`intervalle
427 // pour determiner le nombre de solution, on resoud le systeme
428 // x^2 + y^2 + z^2 = R^2 (1)
429 // A x + B y + C z + D = 0 (2)
432 // REM : (1) (2) : equation du cercle
433 // (1) (3) (4) : equation de la couture.
434 Standard_Integer NbSolutions = 0;
435 Standard_Real A, B, C, D, R, Tol = 1.e-10;
436 Standard_Real U1, U2, V1, V2, aTPC;
439 aTPC=Precision::PConfusion();
441 gp_Circ Circle = myCurve->Circle();
442 Trsf.SetTransformation(SP.Position());
443 Circle.Transform(Trsf);
446 gp_Pln Plane( gp_Ax3(Circle.Position()));
447 Plane.Coefficients(A,B,C,D);
452 if ( ( R - Abs(D/A)) > Tol) NbSolutions = 2;
453 else if ( Abs(R - Abs(D/A))< Tol) NbSolutions = 1;
454 else NbSolutions = 0;
459 Standard_Real delta = R*R*(A*A+C*C) - D*D;
461 if ( Abs(delta) < Tol*Tol) {
462 if ( A*D > 0.) NbSolutions = 1;
464 else if ( delta > 0) {
469 if ( xx > Tol) NbSolutions++;
472 if ( xx > Tol) NbSolutions++;
478 Standard_Real UU = 0.;
479 ElSLib::Parameters(SP, P1, U1, V1);
480 ElSLib::Parameters(SP, P2, U2, V1);
481 ElSLib::Parameters(SP, P, UU, V1);
482 Standard_Real UUmi = Min(Min(U1,UU),Min(UU,U2));
483 Standard_Real UUma = Max(Max(U1,UU),Max(UU,U2));
484 Standard_Boolean reCalc = ((UUmi >= 0. && UUmi <= M_PI) && (UUma >= 0. && UUma <= M_PI));
487 ElSLib::Parameters(SP, P1, U1, V1);//*
489 Parameters(myCurve, mySurface, P1, 1, aTPC, U1, V1);
492 P2 = myCurve->Value(W1+M_PI/8);
493 ElSLib::Parameters(SP,P2,U2,V2);
495 if ( NbSolutions == 1) {
496 if ( Abs(U1-U2) > M_PI) { // on traverse la couture
506 else { // on ne traverse pas la couture
517 else { // 0 ou 2 solutions
518 gp_Pnt Center = Circle.Location();
520 ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
525 // eval the VCouture.
526 if ( (C==0) || Abs(Abs(D/C)-R) > 1.e-10) {
527 VCouture = Standard_False;
530 VCouture = Standard_True;
531 UCouture = Standard_True;
535 myV2 = 3 * M_PI / 2.;
538 myV1 = -3 * M_PI / 2.;
542 // si P1.Z() vaut +/- R on est sur le sommet : pas significatif.
543 gp_Pnt pp = P1.Transformed(Trsf);
545 if ( Abs( Abs(pp.Z()) - R) < Tol) {
546 gp_Pnt Center = Circle.Location();
548 ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
551 VCouture = Standard_False;
554 ElSLib::Parameters(SP,P1,U1,V1);//*
556 Parameters(myCurve, mySurface, P1, 1, aTPC, U1, V1);
558 P2 = myCurve->Value(W1+M_PI/8);
559 ElSLib::Parameters(SP,P2,U2,V2);
561 if ( Abs(U1-U2) > M_PI) { // on traverse la couture
571 else { // on ne traverse pas la couture
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);
593 myU1 = Min(UU1,myU1);
594 myU2 = Max(UU2,myU2);
598 }//if ( myCurve->GetType() == GeomAbs_Circle)
601 Standard_Real U1, V1, U , V;
602 ElSLib::Parameters( SP, P1, U1, V1);
603 Standard_Real Step = .1, Delta = 0.;
604 Standard_Real eps = M_PI, dmax = 0., d = 0.;
605 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
607 Step = (W2 - W1) / (nbp - 1);
608 myU1 = U1; myU2 = U1;
609 Standard_Real pmin = W1, pmax = W1, plim = W2+.1*Step;
610 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
611 P = myCurve->Value(par);
612 ElSLib::Parameters( SP, P, U, V);
627 dmax = Max(dmax, Abs(d));
628 if(U < myU1) {myU1 = U; pmin = par;}
629 if(U > myU2) {myU2 = U; pmax = par;}
633 if(!(Abs(pmin - W1) <= Precision::PConfusion() ||
634 Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
635 if(!(Abs(pmax - W1) <= Precision::PConfusion() ||
636 Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
638 if((myU1 >=0. && myU1 <= 2*M_PI) &&
639 (myU2 >=0. && myU2 <= 2*M_PI) ) {
642 UCouture = Standard_False;
645 U = ( myU1 + myU2 ) /2.;
648 UCouture = Standard_True;
651 VCouture = Standard_False;
657 gp_Torus TR = mySurface->Torus();
658 Standard_Real U1, V1, U , V;
659 ElSLib::Parameters( TR, P1, U1, V1);
660 Standard_Real Step = .1, DeltaU = 0., DeltaV = 0.;
661 Standard_Real eps = M_PI, dmaxU = 0., dU = 0., dmaxV = 0., dV = 0.;
662 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
664 Step = (W2 - W1) / (nbp - 1);
665 myU1 = U1; myU2 = U1;
666 myV1 = V1; myV2 = V1;
667 Standard_Real pminU = W1, pmaxU = W1, pminV = W1, pmaxV = W1,
669 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
670 P = myCurve->Value(par);
671 ElSLib::Parameters( TR, P, U, V);
700 dmaxU = Max(dmaxU, Abs(dU));
701 dmaxV = Max(dmaxV, Abs(dV));
702 if(U < myU1) {myU1 = U; pminU = par;}
703 if(U > myU2) {myU2 = U; pmaxU = par;}
704 if(V < myV1) {myV1 = V; pminV = par;}
705 if(V > myV2) {myV2 = V; pmaxV = par;}
710 if(!(Abs(pminU - W1) <= Precision::PConfusion() ||
711 Abs(pminU - W2) <= Precision::PConfusion()) ) myU1 -= dmaxU*.5;
712 if(!(Abs(pmaxU - W1) <= Precision::PConfusion() ||
713 Abs(pmaxU - W2) <= Precision::PConfusion()) ) myU2 += dmaxU*.5;
714 if(!(Abs(pminV - W1) <= Precision::PConfusion() ||
715 Abs(pminV - W2) <= Precision::PConfusion()) ) myV1 -= dmaxV*.5;
716 if(!(Abs(pmaxV - W1) <= Precision::PConfusion() ||
717 Abs(pmaxV - W2) <= Precision::PConfusion()) ) myV2 += dmaxV*.5;
719 if((myU1 >=0. && myU1 <= 2*M_PI) &&
720 (myU2 >=0. && myU2 <= 2*M_PI) ) {
723 UCouture = Standard_False;
726 U = ( myU1 + myU2 ) /2.;
729 UCouture = Standard_True;
731 if((myV1 >=0. && myV1 <= 2*M_PI) &&
732 (myV2 >=0. && myV2 <= 2*M_PI) ) {
733 VCouture = Standard_False;
736 V = ( myV1 + myV2 ) /2.;
739 VCouture = Standard_True;
747 UCouture = Standard_False;
748 VCouture = Standard_False;
754 //=======================================================================
755 //function : Parameters
757 //=======================================================================
758 void Parameters(const Handle(Adaptor3d_HCurve)& myCurve,
759 const Handle(Adaptor3d_HSurface)& mySurface,
761 const Standard_Integer iFirst,
762 const Standard_Real aTolU,
766 Standard_Real aTwoPI, aU1, aV1, aU2, aV2, aRSp, aTol3D;
767 Standard_Real aTF, aTL, aT2, dT;
768 GeomAbs_SurfaceType aSType;
769 GeomAbs_CurveType aCType;
774 aSType=mySurface->GetType();
775 aCType=myCurve->GetType();
777 if (aSType==GeomAbs_Sphere && aCType==GeomAbs_Circle) {
778 gp_Sphere aSp=mySurface->Sphere();
783 aTF = myCurve->FirstParameter();
784 aTL = myCurve->LastParameter ();
785 dT=myCurve->Resolution(aTol3D);
787 ElSLib::Parameters(aSp, aP1, aU1, aV1);
788 if (fabs(aU)<aTolU || fabs(aU-aTwoPI)<aTolU){
794 aP2=myCurve->Value(aT2);
795 ElSLib::Parameters(aSp, aP2, aU2, aV2);
807 //=======================================================================
808 //classn : ProjLib_Function
810 //=======================================================================
811 class ProjLib_Function : public AppCont_Function2d
813 Handle(Adaptor3d_HCurve) myCurve;
814 Handle(Adaptor3d_HSurface) mySurface;
818 Standard_Real myU1,myU2,myV1,myV2;
819 Standard_Boolean UCouture,VCouture;
821 ProjLib_Function(const Handle(Adaptor3d_HCurve)& C,
822 const Handle(Adaptor3d_HSurface)& S) :
823 myCurve(C), mySurface(S),
828 UCouture(Standard_False),
829 VCouture(Standard_False)
830 {Function_SetUVBounds(myU1,myU2,myV1,myV2,UCouture,VCouture,myCurve,mySurface);}
832 Standard_Real FirstParameter() const
833 {return (myCurve->FirstParameter() + 1.e-9);}
835 Standard_Real LastParameter() const
836 {return (myCurve->LastParameter() -1.e-9);}
839 gp_Pnt2d Value(const Standard_Real t) const
840 {return Function_Value(t,myCurve,mySurface,myU1,myU2,myV1,myV2,UCouture,VCouture);}
842 Standard_Boolean D1(const Standard_Real t, gp_Pnt2d& P, gp_Vec2d& V) const
843 {return Function_D1(t,P,V,myCurve,mySurface,myU1,myU2,myV1,myV2,UCouture,VCouture);}
846 //=======================================================================
847 //function : ProjLib_ComputeApprox
849 //=======================================================================
851 ProjLib_ComputeApprox::ProjLib_ComputeApprox
852 (const Handle(Adaptor3d_HCurve) & C,
853 const Handle(Adaptor3d_HSurface) & S,
854 const Standard_Real Tol )
856 // if the surface is a plane and the curve a BSpline or a BezierCurve,
857 // don`t make an Approx but only the projection of the poles.
859 myTolerance = Max(Precision::PApproximation(),Tol);
860 Standard_Integer NbKnots, NbPoles ;
861 GeomAbs_CurveType CType = C->GetType();
862 GeomAbs_SurfaceType SType = S->GetType();
864 Standard_Boolean SurfIsAnal = (SType != GeomAbs_BSplineSurface) &&
865 (SType != GeomAbs_BezierSurface) &&
866 (SType != GeomAbs_OtherSurface) ;
868 Standard_Boolean CurvIsAnal = (CType != GeomAbs_BSplineCurve) &&
869 (CType != GeomAbs_BezierCurve) &&
870 (CType != GeomAbs_OtherCurve) ;
872 Standard_Boolean simplecase = SurfIsAnal && CurvIsAnal;
874 if (CType == GeomAbs_BSplineCurve &&
875 SType == GeomAbs_Plane ) {
877 // get the poles and eventually the weights
878 Handle(Geom_BSplineCurve) BS = C->BSpline();
879 NbPoles = BS->NbPoles();
880 TColgp_Array1OfPnt P3d( 1, NbPoles);
881 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
882 TColStd_Array1OfReal Weights( 1, NbPoles);
883 if ( BS->IsRational()) BS->Weights(Weights);
885 gp_Pln Plane = S->Plane();
887 for ( Standard_Integer i = 1; i <= NbPoles; i++) {
888 ElSLib::Parameters( Plane, P3d(i), U, V);
889 Poles.SetValue(i,gp_Pnt2d(U,V));
891 NbKnots = BS->NbKnots();
892 TColStd_Array1OfReal Knots(1,NbKnots);
893 TColStd_Array1OfInteger Mults(1,NbKnots);
895 BS->Multiplicities(Mults) ;
896 // get the knots and mults if BSplineCurve
897 if ( BS->IsRational()) {
898 myBSpline = new Geom2d_BSplineCurve(Poles,
906 myBSpline = new Geom2d_BSplineCurve(Poles,
913 else if (CType == GeomAbs_BezierCurve &&
914 SType == GeomAbs_Plane ) {
916 // get the poles and eventually the weights
917 Handle(Geom_BezierCurve) BezierCurvePtr = C->Bezier() ;
918 NbPoles = BezierCurvePtr->NbPoles();
919 TColgp_Array1OfPnt P3d( 1, NbPoles);
920 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
921 TColStd_Array1OfReal Weights( 1, NbPoles);
922 if ( BezierCurvePtr->IsRational()) {
923 BezierCurvePtr->Weights(Weights);
925 BezierCurvePtr->Poles( P3d);
927 // project the 3D-Poles on the plane
929 gp_Pln Plane = S->Plane();
931 for ( Standard_Integer i = 1; i <= NbPoles; i++) {
932 ElSLib::Parameters( Plane, P3d(i), U, V);
933 Poles.SetValue(i,gp_Pnt2d(U,V));
935 if ( BezierCurvePtr->IsRational()) {
936 myBezier = new Geom2d_BezierCurve(Poles, Weights);
939 myBezier = new Geom2d_BezierCurve(Poles);
943 ProjLib_Function F( C, S);
947 Standard_Integer Nb = 20;
948 Standard_Real U1, U2, dU, U;
949 U1 = F.FirstParameter();
950 U2 = F.LastParameter();
951 dU = ( U2 - U1) / Nb;
952 TColStd_Array1OfInteger Mults(1,Nb+1);
953 TColStd_Array1OfReal Knots(1,Nb+1);
954 TColgp_Array1OfPnt2d Poles(1,Nb+1);
955 for ( Standard_Integer i = 1; i <= Nb+1; i++) {
957 Poles(i) = F.Value(U);
965 char* ResultName = "Result";
966 DrawTrSurf::Set(ResultName,new Geom2d_BSplineCurve(Poles,Knots,Mults,1));
967 // DrawTrSurf::Set("Result",new Geom2d_BSplineCurve(Poles,Knots,Mults,1));
973 Standard_Integer Deg1, Deg2;
983 Approx_FitAndDivide2d Fit(F,Deg1,Deg2,myTolerance,myTolerance,
985 if(Fit.IsAllApproximated()) {
987 Standard_Integer NbCurves = Fit.NbMultiCurves();
989 // on essaie de rendre la courbe au moins C1
990 Convert_CompBezierCurves2dToBSplineCurve2d Conv;
993 Standard_Real Tol3d,Tol2d;
994 for (i = 1; i <= NbCurves; i++) {
995 Fit.Error(i,Tol3d, Tol2d);
996 myTolerance = Max(myTolerance, Tol2d);
997 AppParCurves_MultiCurve MC = Fit.Value( i); //Charge la Ieme Curve
998 TColgp_Array1OfPnt2d Poles2d( 1, MC.Degree() + 1);//Recupere les poles
999 MC.Curve(1, Poles2d);
1001 Conv.AddCurve(Poles2d);
1004 //mise a jour des fields de ProjLib_Approx
1007 NbPoles = Conv.NbPoles();
1008 NbKnots = Conv.NbKnots();
1011 if(NbPoles <= 0 || NbPoles > 100000)
1013 if(NbKnots <= 0 || NbKnots > 100000)
1016 TColgp_Array1OfPnt2d NewPoles(1,NbPoles);
1017 TColStd_Array1OfReal NewKnots(1,NbKnots);
1018 TColStd_Array1OfInteger NewMults(1,NbKnots);
1020 Conv.KnotsAndMults(NewKnots,NewMults);
1021 Conv.Poles(NewPoles);
1023 BSplCLib::Reparametrize(C->FirstParameter(),
1027 // il faut recadrer les poles de debut et de fin:
1028 // ( Car pour les problemes de couture, on a du ouvrir l`intervalle
1029 // de definition de la courbe.)
1030 // On choisit de calculer ces poles par prolongement de la courbe
1036 U = C->FirstParameter() - 1.e-9;
1042 BSplCLib::NoWeights(),
1046 NewPoles.SetValue(1,P);
1047 U = C->LastParameter() + 1.e-9;
1053 BSplCLib::NoWeights(),
1057 NewPoles.SetValue(NbPoles,P);
1058 myBSpline = new Geom2d_BSplineCurve (NewPoles,
1064 Standard_Integer NbCurves = Fit.NbMultiCurves();
1066 Standard_Real Tol3d,Tol2d;
1067 Fit.Error(NbCurves,Tol3d, Tol2d);
1068 myTolerance = Tol2d;
1073 Standard_Real UFirst = F.FirstParameter();
1074 gp_Pnt P3d = C->Value( UFirst );
1075 Standard_Real u = 0., v = 0.;
1080 gp_Pln Plane = S->Plane();
1081 ElSLib::Parameters( Plane, P3d, u, v );
1084 case GeomAbs_Cylinder:
1086 gp_Cylinder Cylinder = S->Cylinder();
1087 ElSLib::Parameters( Cylinder, P3d, u, v );
1092 gp_Cone Cone = S->Cone();
1093 ElSLib::Parameters( Cone, P3d, u, v );
1096 case GeomAbs_Sphere:
1098 gp_Sphere Sphere = S->Sphere();
1099 ElSLib::Parameters( Sphere, P3d, u, v );
1104 gp_Torus Torus = S->Torus();
1105 ElSLib::Parameters( Torus, P3d, u, v );
1109 Standard_NoSuchObject::Raise("ProjLib_ComputeApprox::Value");
1111 Standard_Boolean ToMirror = Standard_False;
1112 Standard_Real du = 0., dv = 0.;
1113 Standard_Integer number;
1116 if (SType == GeomAbs_Sphere && Abs(u-F.myU1) > M_PI)
1118 ToMirror = Standard_True;
1122 Standard_Real newV = ElCLib::InPeriod( v, F.myV1, F.myV2 );
1123 number = (Standard_Integer) (Floor((newV-v)/(F.myV2-F.myV1)));
1124 dv -= number*(F.myV2-F.myV1);
1126 if (F.UCouture || F.VCouture && SType == GeomAbs_Sphere)
1128 gp_Pnt2d P2d = F.Value( UFirst );
1129 number = (Standard_Integer) (Floor((P2d.X()-u)/M_PI + Epsilon(M_PI)));
1133 if (!myBSpline.IsNull())
1135 if (du != 0. || dv != 0.)
1136 myBSpline->Translate( gp_Vec2d(du,dv) );
1139 gp_Ax2d Axe( gp_Pnt2d(0.,0.), gp_Dir2d(1.,0.) );
1140 myBSpline->Mirror( Axe );
1146 //=======================================================================
1147 //function : BSpline
1149 //=======================================================================
1151 Handle(Geom2d_BSplineCurve) ProjLib_ComputeApprox::BSpline() const
1157 //=======================================================================
1160 //=======================================================================
1162 Handle(Geom2d_BezierCurve) ProjLib_ComputeApprox::Bezier() const
1169 //=======================================================================
1170 //function : Tolerance
1172 //=======================================================================
1174 Standard_Real ProjLib_ComputeApprox::Tolerance() const