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_Function.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");
124 S = ElCLib::InPeriod(S, U1, U2);
129 if(SType == GeomAbs_Sphere) {
130 if ( Abs( S - U1 ) > M_PI ) {
135 S = ElCLib::InPeriod(S, U1, U2);
138 T = ElCLib::InPeriod(T, V1, V2);
141 return gp_Pnt2d(S, T);
143 //=======================================================================
146 //=======================================================================
147 static Standard_Boolean Function_D1( const Standard_Real U,
150 const Handle(Adaptor3d_HCurve)& myCurve,
151 const Handle(Adaptor3d_HSurface)& mySurface,
152 const Standard_Real U1,
153 const Standard_Real U2,
154 const Standard_Real V1,
155 const Standard_Real V2,
156 const Standard_Boolean UCouture,
157 const Standard_Boolean VCouture )
160 Standard_Real dU, dV;
162 P = Function_Value(U,myCurve,mySurface,U1,U2,V1,V2,UCouture,VCouture);
164 GeomAbs_SurfaceType Type = mySurface->GetType();
169 case GeomAbs_Cylinder:
175 myCurve->D1(U,P3d,T);
176 mySurface->D1(P.X(),P.Y(),P3d,D1U,D1V);
180 Standard_Real Nu = D1U.SquareMagnitude();
181 Standard_Real Nv = D1V.SquareMagnitude();
183 if ( Nu < Epsilon(1.) || Nv < Epsilon(1.))
184 return Standard_False;
188 D = gp_Vec2d( dU, dV);
193 return Standard_False;
196 return Standard_True;
199 //=======================================================================
200 //function : Function_SetUVBounds
202 //=======================================================================
203 static void Function_SetUVBounds(Standard_Real& myU1,
207 Standard_Boolean& UCouture,
208 Standard_Boolean& VCouture,
209 const Handle(Adaptor3d_HCurve)& myCurve,
210 const Handle(Adaptor3d_HSurface)& mySurface)
212 Standard_Real W1, W2, W;
215 W1 = myCurve->FirstParameter();
216 W2 = myCurve->LastParameter ();
218 // on ouvre l`intervalle
221 P1 = myCurve->Value(W1);
222 P2 = myCurve->Value(W2);
223 P = myCurve->Value(W);
225 switch ( mySurface->GetType()) {
228 Standard_Real tol = Epsilon(1.);
229 Standard_Real ptol = Precision::PConfusion();
230 gp_Cone Cone = mySurface->Cone();
231 VCouture = Standard_False;
232 //Calculation of cone parameters for P == ConeApex often produces wrong
234 gp_Pnt ConeApex = Cone.Apex();
235 if(ConeApex.XYZ().IsEqual(P1.XYZ(), tol))
238 P1 = myCurve->Value(W1);
240 if(ConeApex.XYZ().IsEqual(P2.XYZ(), tol))
243 P2 = myCurve->Value(W2);
245 if(ConeApex.XYZ().IsEqual(P.XYZ(), tol))
248 P = myCurve->Value(W);
251 switch( myCurve->GetType() ){
252 case GeomAbs_Parabola:
253 case GeomAbs_Hyperbola:
254 case GeomAbs_Ellipse:{
255 Standard_Real U1, U2, V1, V2, U , V;
256 ElSLib::Parameters( Cone, P1, U1, V1);
257 ElSLib::Parameters( Cone, P2, U2, V2);
258 ElSLib::Parameters( Cone, P , U , V );
261 if ( ( U1 < U && U < U2 ) && !myCurve->IsClosed() ) {
262 UCouture = Standard_False;
265 UCouture = Standard_True;
266 myU2 = myU1 + 2*M_PI;
272 Standard_Real U1, V1, U , V, Delta = 0., d = 0., pmin = W1, pmax = W1, dmax = 0., Uf, Ul;
273 ElSLib::Parameters( Cone, P1, U1, V1);
274 ElSLib::Parameters( Cone, P2, Ul, V1);
275 myU1 = U1; myU2 = U1; Uf = U1;
276 Standard_Real Step = .1;
277 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
278 if(myCurve->GetType() == GeomAbs_Line)
283 Step = (W2 - W1) / (nbp - 1);
284 Standard_Boolean isclandper = (!(myCurve->IsClosed()) && !(myCurve->IsPeriodic()));
285 Standard_Boolean isFirst = Standard_True;
286 for(Standard_Real par = W1 + Step; par <= W2; par += Step)
288 if(!isclandper) par += Step;
289 P = myCurve->Value(par);
290 ElSLib::Parameters( Cone, P, U, V);
295 if( ( (IsEqual(U,(2*M_PI),1.e-10) && (U1 >= 0. && U1 <= M_PI)) &&
296 (IsEqual(U,Ul,1.e-10) && !IsEqual(Uf,0.,1.e-10)) ) && isclandper )
300 // Protection against first-last point on seam.
303 else if (par + Step >= W2)
313 if( ( (IsEqual(U,0.,1.e-10) && (U1 >= M_PI && U1 <= (2*M_PI))) &&
314 (IsEqual(U,Ul,1.e-10) && !IsEqual(Uf,(2*M_PI),1.e-10)) ) && isclandper )
318 // Protection against first-last point on seam.
321 else if (par + Step >= W2)
329 dmax = Max(dmax, Abs(d));
330 if(U < myU1) {myU1 = U; pmin = par;}
331 if(U > myU2) {myU2 = U; pmax = par;}
333 isFirst = Standard_False;
334 } // for(Standard_Real par = W1 + Step; par <= W2; par += Step)
336 if(!(Abs(pmin - W1) <= Precision::PConfusion() || Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
337 if(!(Abs(pmax - W1) <= Precision::PConfusion() || Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
339 if((myU1 >=0. && myU1 <= 2*M_PI) && (myU2 >=0. && myU2 <= 2*M_PI) ) UCouture = Standard_False;
341 U = ( myU1 + myU2 ) /2.;
344 UCouture = Standard_True;
348 }// switch curve type
352 case GeomAbs_Cylinder: {
353 gp_Cylinder Cylinder = mySurface->Cylinder();
354 VCouture = Standard_False;
356 if (myCurve->GetType() == GeomAbs_Ellipse) {
358 Standard_Real U1, U2, V1, V2, U , V;
359 ElSLib::Parameters( Cylinder, P1, U1, V1);
360 ElSLib::Parameters( Cylinder, P2, U2, V2);
361 ElSLib::Parameters( Cylinder, P , U , V );
365 if ( !myCurve->IsClosed()) {
366 if ( myU1 < U && U < myU2) {
367 U = ( myU1 + myU2 ) /2.;
372 U = ( myU1 + myU2 ) /2.;
382 UCouture = Standard_True;
388 myCurve->D1(W1,P3d,T);
389 mySurface->D1(U1,U2,P3d,D1U,D1V);
390 Standard_Real dU = T.Dot(D1U);
392 UCouture = Standard_True;
394 myU2 = myU1 + 2*M_PI;
403 Standard_Real U1, V1, U , V;
404 ElSLib::Parameters( Cylinder, P1, U1, V1);
405 Standard_Real Step = .1, Delta = 0.;
406 Standard_Real eps = M_PI, dmax = 0., d = 0.;
407 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
409 Step = (W2 - W1) / (nbp - 1);
410 myU1 = U1; myU2 = U1;
411 Standard_Real pmin = W1, pmax = W1, plim = W2+.1*Step;
412 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
413 P = myCurve->Value(par);
414 ElSLib::Parameters( Cylinder, P, U, V);
429 dmax = Max(dmax, Abs(d));
430 if(U < myU1) {myU1 = U; pmin = par;}
431 if(U > myU2) {myU2 = U; pmax = par;}
435 if(!(Abs(pmin - W1) <= Precision::PConfusion() ||
436 Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
437 if(!(Abs(pmax - W1) <= Precision::PConfusion() ||
438 Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
440 if((myU1 >=0. && myU1 <= 2*M_PI) &&
441 (myU2 >=0. && myU2 <= 2*M_PI) ) {
442 UCouture = Standard_False;
445 U = ( myU1 + myU2 ) /2.;
448 UCouture = Standard_True;
454 case GeomAbs_Sphere:{
455 VCouture = Standard_False;
456 gp_Sphere SP = mySurface->Sphere();
457 if ( myCurve->GetType() == GeomAbs_Circle) {
458 UCouture = Standard_True;
460 // on cherche a savoir le nombre de fois que la couture est
462 // si 0 ou 2 fois : la PCurve est fermee et dans l`intervalle
463 // [Uc-PI, Uc+PI] (Uc: U du centre du cercle)
464 // si 1 fois : la PCurve est ouverte et dans l`intervalle
467 // pour determiner le nombre de solution, on resoud le systeme
468 // x^2 + y^2 + z^2 = R^2 (1)
469 // A x + B y + C z + D = 0 (2)
472 // REM : (1) (2) : equation du cercle
473 // (1) (3) (4) : equation de la couture.
474 Standard_Integer NbSolutions = 0;
475 Standard_Real A, B, C, D, R, Tol = 1.e-10;
476 Standard_Real U1, U2, V1, V2;
479 gp_Circ Circle = myCurve->Circle();
480 Trsf.SetTransformation(SP.Position());
481 Circle.Transform(Trsf);
484 gp_Pln Plane( gp_Ax3(Circle.Position()));
485 Plane.Coefficients(A,B,C,D);
490 if ( ( R - Abs(D/A)) > Tol) NbSolutions = 2;
491 else if ( Abs(R - Abs(D/A))< Tol) NbSolutions = 1;
492 else NbSolutions = 0;
497 Standard_Real delta = R*R*(A*A+C*C) - D*D;
499 if ( Abs(delta) < Tol*Tol) {
500 if ( A*D > 0.) NbSolutions = 1;
502 else if ( delta > 0) {
507 if ( xx > Tol) NbSolutions++;
510 if ( xx > Tol) NbSolutions++;
516 Standard_Real UU = 0.;
517 ElSLib::Parameters(SP, P1, U1, V1);
518 Standard_Real eps = 10.*Epsilon(1.);
519 Standard_Real dt = Max(Precision::PConfusion(), 0.01*(W2-W1));
522 //May be U1 must be equal 2*PI?
523 gp_Pnt Pd = myCurve->Value(W1+dt);
524 Standard_Real ud, vd;
525 ElSLib::Parameters(SP, Pd, ud, vd);
526 if(Abs(U1 - ud) > M_PI)
531 else if(Abs(2.*M_PI - U1) < eps)
534 gp_Pnt Pd = myCurve->Value(W1+dt);
535 Standard_Real ud, vd;
536 ElSLib::Parameters(SP, Pd, ud, vd);
537 if(Abs(U1 - ud) > M_PI)
543 ElSLib::Parameters(SP, P2, U2, V1);
546 //May be U2 must be equal 2*PI?
547 gp_Pnt Pd = myCurve->Value(W2-dt);
548 Standard_Real ud, vd;
549 ElSLib::Parameters(SP, Pd, ud, vd);
550 if(Abs(U2 - ud) > M_PI)
555 else if(Abs(2.*M_PI - U2) < eps)
558 gp_Pnt Pd = myCurve->Value(W2-dt);
559 Standard_Real ud, vd;
560 ElSLib::Parameters(SP, Pd, ud, vd);
561 if(Abs(U2 - ud) > M_PI)
567 ElSLib::Parameters(SP, P, UU, V1);
568 //+This fragment was the reason of bug # 26008.
569 //+It has been deleted on April, 03 2015.
570 //Standard_Real UUmi = Min(Min(U1,UU),Min(UU,U2));
571 //Standard_Real UUma = Max(Max(U1,UU),Max(UU,U2));
572 //Standard_Boolean reCalc = ((UUmi >= 0. && UUmi <= M_PI) && (UUma >= 0. && UUma <= M_PI));
574 P2 = myCurve->Value(W1+M_PI/8);
575 ElSLib::Parameters(SP,P2,U2,V2);
577 if ( NbSolutions == 1) {
578 if ( Abs(U1-U2) > M_PI) { // on traverse la couture
588 else { // on ne traverse pas la couture
599 else { // 0 ou 2 solutions
600 gp_Pnt Center = Circle.Location();
602 ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
607 // eval the VCouture.
608 if ( (C==0) || Abs(Abs(D/C)-R) > 1.e-10) {
609 VCouture = Standard_False;
612 VCouture = Standard_True;
613 UCouture = Standard_True;
617 myV2 = 3 * M_PI / 2.;
620 myV1 = -3 * M_PI / 2.;
624 // si P1.Z() vaut +/- R on est sur le sommet : pas significatif.
625 gp_Pnt pp = P1.Transformed(Trsf);
627 if ( Abs( Abs(pp.Z()) - R) < Tol) {
628 gp_Pnt Center = Circle.Location();
630 ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
633 VCouture = Standard_False;
638 myV1 = -1.e+100; myV2 = 1.e+100;
640 //+This fragment was the reason of bug # 26008.
641 //+It has been deleted on April, 03 2015.
642 //Standard_Real UU1 = myU1, UU2 = myU2;
643 //if((Abs(UU1) <= (2.*M_PI) && Abs(UU2) <= (2.*M_PI)) && NbSolutions == 1 && reCalc) {
644 // gp_Pnt Center = Circle.Location();
645 // Standard_Real U,V;
646 // ElSLib::SphereParameters(gp_Ax3(gp::XOY()),1,Center, U, V);
648 // myU1 = Min(UU1,myU1);
649 // myU2 = myU1 + 2.*M_PI;
653 }//if ( myCurve->GetType() == GeomAbs_Circle)
656 Standard_Real U1, V1, U , V;
657 ElSLib::Parameters( SP, P1, U1, V1);
658 Standard_Real Step = .1, Delta = 0.;
659 Standard_Real eps = M_PI, dmax = 0., d = 0.;
660 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
662 Step = (W2 - W1) / (nbp - 1);
663 myU1 = U1; myU2 = U1;
664 Standard_Real pmin = W1, pmax = W1, plim = W2+.1*Step;
665 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
666 P = myCurve->Value(par);
667 ElSLib::Parameters( SP, P, U, V);
682 dmax = Max(dmax, Abs(d));
683 if(U < myU1) {myU1 = U; pmin = par;}
684 if(U > myU2) {myU2 = U; pmax = par;}
688 if(!(Abs(pmin - W1) <= Precision::PConfusion() ||
689 Abs(pmin - W2) <= Precision::PConfusion()) ) myU1 -= dmax*.5;
690 if(!(Abs(pmax - W1) <= Precision::PConfusion() ||
691 Abs(pmax - W2) <= Precision::PConfusion()) ) myU2 += dmax*.5;
693 if((myU1 >=0. && myU1 <= 2*M_PI) &&
694 (myU2 >=0. && myU2 <= 2*M_PI) ) {
697 UCouture = Standard_False;
700 U = ( myU1 + myU2 ) /2.;
703 UCouture = Standard_True;
706 VCouture = Standard_False;
712 gp_Torus TR = mySurface->Torus();
713 Standard_Real U1, V1, U , V;
714 ElSLib::Parameters( TR, P1, U1, V1);
715 Standard_Real Step = .1, DeltaU = 0., DeltaV = 0.;
716 Standard_Real eps = M_PI, dmaxU = 0., dU = 0., dmaxV = 0., dV = 0.;
717 Standard_Integer nbp = (Standard_Integer)((W2 - W1) / Step + 1);
719 Step = (W2 - W1) / (nbp - 1);
720 myU1 = U1; myU2 = U1;
721 myV1 = V1; myV2 = V1;
722 Standard_Real pminU = W1, pmaxU = W1, pminV = W1, pmaxV = W1,
724 for(Standard_Real par = W1 + Step; par <= plim; par += Step) {
725 P = myCurve->Value(par);
726 ElSLib::Parameters( TR, P, U, V);
755 dmaxU = Max(dmaxU, Abs(dU));
756 dmaxV = Max(dmaxV, Abs(dV));
757 if(U < myU1) {myU1 = U; pminU = par;}
758 if(U > myU2) {myU2 = U; pmaxU = par;}
759 if(V < myV1) {myV1 = V; pminV = par;}
760 if(V > myV2) {myV2 = V; pmaxV = par;}
765 if(!(Abs(pminU - W1) <= Precision::PConfusion() ||
766 Abs(pminU - W2) <= Precision::PConfusion()) ) myU1 -= dmaxU*.5;
767 if(!(Abs(pmaxU - W1) <= Precision::PConfusion() ||
768 Abs(pmaxU - W2) <= Precision::PConfusion()) ) myU2 += dmaxU*.5;
769 if(!(Abs(pminV - W1) <= Precision::PConfusion() ||
770 Abs(pminV - W2) <= Precision::PConfusion()) ) myV1 -= dmaxV*.5;
771 if(!(Abs(pmaxV - W1) <= Precision::PConfusion() ||
772 Abs(pmaxV - W2) <= Precision::PConfusion()) ) myV2 += dmaxV*.5;
774 if((myU1 >=0. && myU1 <= 2*M_PI) &&
775 (myU2 >=0. && myU2 <= 2*M_PI) ) {
778 UCouture = Standard_False;
781 U = ( myU1 + myU2 ) /2.;
784 UCouture = Standard_True;
786 if((myV1 >=0. && myV1 <= 2*M_PI) &&
787 (myV2 >=0. && myV2 <= 2*M_PI) ) {
788 VCouture = Standard_False;
791 V = ( myV1 + myV2 ) /2.;
794 VCouture = Standard_True;
802 UCouture = Standard_False;
803 VCouture = Standard_False;
810 //=======================================================================
811 //classn : ProjLib_Function
813 //=======================================================================
814 class ProjLib_Function : public AppCont_Function
816 Handle(Adaptor3d_HCurve) myCurve;
817 Handle(Adaptor3d_HSurface) mySurface;
818 Standard_Boolean myIsPeriodic[2];
819 Standard_Real myPeriod[2];
822 Standard_Real myU1,myU2,myV1,myV2;
823 Standard_Boolean UCouture,VCouture;
825 ProjLib_Function(const Handle(Adaptor3d_HCurve)& C,
826 const Handle(Adaptor3d_HSurface)& S)
833 UCouture(Standard_False),
834 VCouture(Standard_False)
838 Function_SetUVBounds(myU1,myU2,myV1,myV2,UCouture,VCouture,myCurve,mySurface);
839 myIsPeriodic[0] = mySurface->IsUPeriodic();
840 myIsPeriodic[1] = mySurface->IsVPeriodic();
843 myPeriod[0] = mySurface->UPeriod();
848 myPeriod[1] = mySurface->VPeriod();
853 void PeriodInformation(const Standard_Integer theDimIdx,
854 Standard_Boolean& IsPeriodic,
855 Standard_Real& thePeriod) const
857 IsPeriodic = myIsPeriodic[theDimIdx - 1];
858 thePeriod = myPeriod[theDimIdx - 1];
861 Standard_Real FirstParameter() const
863 return (myCurve->FirstParameter());
866 Standard_Real LastParameter() const
868 return (myCurve->LastParameter());
871 Standard_Boolean Value(const Standard_Real theT,
872 NCollection_Array1<gp_Pnt2d>& thePnt2d,
873 NCollection_Array1<gp_Pnt>& /*thePnt*/) const
875 thePnt2d(1) = Function_Value(theT, myCurve, mySurface, myU1, myU2, myV1, myV2, UCouture, VCouture);
876 return Standard_True;
879 gp_Pnt2d Value(const Standard_Real theT) const
881 return Function_Value(theT, myCurve, mySurface, myU1, myU2, myV1, myV2, UCouture, VCouture);
884 Standard_Boolean D1(const Standard_Real theT,
885 NCollection_Array1<gp_Vec2d>& theVec2d,
886 NCollection_Array1<gp_Vec>& /*theVec*/) const
890 Standard_Boolean isOk = Function_D1(theT, aPnt2d,aVec2d, myCurve, mySurface, myU1, myU2, myV1, myV2, UCouture, VCouture);
891 theVec2d(1) = aVec2d;
896 //=======================================================================
897 //function : ProjLib_ComputeApprox
899 //=======================================================================
901 ProjLib_ComputeApprox::ProjLib_ComputeApprox
902 (const Handle(Adaptor3d_HCurve) & C,
903 const Handle(Adaptor3d_HSurface) & S,
904 const Standard_Real Tol )
906 // if the surface is a plane and the curve a BSpline or a BezierCurve,
907 // don`t make an Approx but only the projection of the poles.
909 myTolerance = Max(Precision::PApproximation(),Tol);
910 Standard_Integer NbKnots, NbPoles ;
911 GeomAbs_CurveType CType = C->GetType();
912 GeomAbs_SurfaceType SType = S->GetType();
914 Standard_Boolean SurfIsAnal = (SType != GeomAbs_BSplineSurface) &&
915 (SType != GeomAbs_BezierSurface) &&
916 (SType != GeomAbs_OtherSurface) ;
918 Standard_Boolean CurvIsAnal = (CType != GeomAbs_BSplineCurve) &&
919 (CType != GeomAbs_BezierCurve) &&
920 (CType != GeomAbs_OffsetCurve) &&
921 (CType != GeomAbs_OtherCurve) ;
923 Standard_Boolean simplecase = SurfIsAnal && CurvIsAnal;
925 if (CType == GeomAbs_BSplineCurve &&
926 SType == GeomAbs_Plane ) {
928 // get the poles and eventually the weights
929 Handle(Geom_BSplineCurve) BS = C->BSpline();
930 NbPoles = BS->NbPoles();
931 TColgp_Array1OfPnt P3d( 1, NbPoles);
932 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
933 TColStd_Array1OfReal Weights( 1, NbPoles);
934 if ( BS->IsRational()) BS->Weights(Weights);
936 gp_Pln Plane = S->Plane();
938 for ( Standard_Integer i = 1; i <= NbPoles; i++) {
939 ElSLib::Parameters( Plane, P3d(i), U, V);
940 Poles.SetValue(i,gp_Pnt2d(U,V));
942 NbKnots = BS->NbKnots();
943 TColStd_Array1OfReal Knots(1,NbKnots);
944 TColStd_Array1OfInteger Mults(1,NbKnots);
946 BS->Multiplicities(Mults) ;
947 // get the knots and mults if BSplineCurve
948 if ( BS->IsRational()) {
949 myBSpline = new Geom2d_BSplineCurve(Poles,
957 myBSpline = new Geom2d_BSplineCurve(Poles,
964 else if (CType == GeomAbs_BezierCurve &&
965 SType == GeomAbs_Plane ) {
967 // get the poles and eventually the weights
968 Handle(Geom_BezierCurve) BezierCurvePtr = C->Bezier() ;
969 NbPoles = BezierCurvePtr->NbPoles();
970 TColgp_Array1OfPnt P3d( 1, NbPoles);
971 TColgp_Array1OfPnt2d Poles( 1, NbPoles);
972 TColStd_Array1OfReal Weights( 1, NbPoles);
973 if ( BezierCurvePtr->IsRational()) {
974 BezierCurvePtr->Weights(Weights);
976 BezierCurvePtr->Poles( P3d);
978 // project the 3D-Poles on the plane
980 gp_Pln Plane = S->Plane();
982 for ( Standard_Integer i = 1; i <= NbPoles; i++) {
983 ElSLib::Parameters( Plane, P3d(i), U, V);
984 Poles.SetValue(i,gp_Pnt2d(U,V));
986 if ( BezierCurvePtr->IsRational()) {
987 myBezier = new Geom2d_BezierCurve(Poles, Weights);
990 myBezier = new Geom2d_BezierCurve(Poles);
994 ProjLib_Function F( C, S);
997 //if ( AffichValue) {
998 // Standard_Integer Nb = 20;
999 // Standard_Real U1, U2, dU, U;
1000 // U1 = F.FirstParameter();
1001 // U2 = F.LastParameter();
1002 // dU = ( U2 - U1) / Nb;
1003 // TColStd_Array1OfInteger Mults(1,Nb+1);
1004 // TColStd_Array1OfReal Knots(1,Nb+1);
1005 // TColgp_Array1OfPnt2d Poles(1,Nb+1);
1006 // for ( Standard_Integer i = 1; i <= Nb+1; i++) {
1007 // U = U1 + (i-1)*dU;
1008 // Poles(i) = F.Value(U);
1009 // cout << "i = " << i << ": U = " << U <<
1010 // ", p(" << Poles(i).X() << ", " << Poles(i).Y() << ");" << endl;
1017 //2D-curve for showing in DRAW
1018 // Handle(Geom2d_Curve) aCC = new Geom2d_BSplineCurve(Poles,Knots,Mults,1);
1019 // AffichValue = Standard_False;
1024 Standard_Integer Deg1, Deg2;
1034 Approx_FitAndDivide2d Fit(F,Deg1,Deg2,myTolerance,myTolerance,
1036 if(Fit.IsAllApproximated()) {
1038 Standard_Integer NbCurves = Fit.NbMultiCurves();
1040 // on essaie de rendre la courbe au moins C1
1041 Convert_CompBezierCurves2dToBSplineCurve2d Conv;
1044 Standard_Real Tol3d,Tol2d;
1045 for (i = 1; i <= NbCurves; i++) {
1046 Fit.Error(i,Tol3d, Tol2d);
1047 myTolerance = Max(myTolerance, Tol2d);
1048 AppParCurves_MultiCurve MC = Fit.Value( i); //Charge la Ieme Curve
1049 TColgp_Array1OfPnt2d Poles2d( 1, MC.Degree() + 1);//Recupere les poles
1050 MC.Curve(1, Poles2d);
1052 Conv.AddCurve(Poles2d);
1055 //mise a jour des fields de ProjLib_Approx
1057 NbPoles = Conv.NbPoles();
1058 NbKnots = Conv.NbKnots();
1060 if(NbPoles <= 0 || NbPoles > 100000)
1062 if(NbKnots <= 0 || NbKnots > 100000)
1065 TColgp_Array1OfPnt2d NewPoles(1,NbPoles);
1066 TColStd_Array1OfReal NewKnots(1,NbKnots);
1067 TColStd_Array1OfInteger NewMults(1,NbKnots);
1069 Conv.KnotsAndMults(NewKnots,NewMults);
1070 Conv.Poles(NewPoles);
1072 BSplCLib::Reparametrize(C->FirstParameter(),
1077 for (int i = 1; i <= NbPoles; i++)
1079 cout << NewPoles.Value(i).X() << " " << NewPoles.Value(i).Y() << endl;
1083 // il faut recadrer les poles de debut et de fin:
1084 // ( Car pour les problemes de couture, on a du ouvrir l`intervalle
1085 // de definition de la courbe.)
1086 // On choisit de calculer ces poles par prolongement de la courbe
1088 myBSpline = new Geom2d_BSplineCurve (NewPoles,
1094 Standard_Integer NbCurves = Fit.NbMultiCurves();
1096 Standard_Real Tol3d,Tol2d;
1097 Fit.Error(NbCurves,Tol3d, Tol2d);
1098 myTolerance = Tol2d;
1103 Standard_Real UFirst = F.FirstParameter();
1104 gp_Pnt P3d = C->Value( UFirst );
1105 Standard_Real u = 0., v = 0.;
1110 gp_Pln Plane = S->Plane();
1111 ElSLib::Parameters( Plane, P3d, u, v );
1114 case GeomAbs_Cylinder:
1116 gp_Cylinder Cylinder = S->Cylinder();
1117 ElSLib::Parameters( Cylinder, P3d, u, v );
1122 gp_Cone Cone = S->Cone();
1123 ElSLib::Parameters( Cone, P3d, u, v );
1126 case GeomAbs_Sphere:
1128 gp_Sphere Sphere = S->Sphere();
1129 ElSLib::Parameters( Sphere, P3d, u, v );
1134 gp_Torus Torus = S->Torus();
1135 ElSLib::Parameters( Torus, P3d, u, v );
1139 Standard_NoSuchObject::Raise("ProjLib_ComputeApprox::Value");
1141 Standard_Boolean ToMirror = Standard_False;
1142 Standard_Real du = 0., dv = 0.;
1143 Standard_Integer number;
1146 if (SType == GeomAbs_Sphere && Abs(u-F.myU1) > M_PI)
1148 ToMirror = Standard_True;
1152 Standard_Real newV = ElCLib::InPeriod( v, F.myV1, F.myV2 );
1153 number = (Standard_Integer) (Floor((newV-v)/(F.myV2-F.myV1)));
1154 dv -= number*(F.myV2-F.myV1);
1156 if (F.UCouture || (F.VCouture && SType == GeomAbs_Sphere))
1158 Standard_Real aNbPer;
1159 gp_Pnt2d P2d = F.Value(UFirst);
1161 du = (du < 0) ? (du - Precision::PConfusion()) :
1162 (du + Precision::PConfusion());
1163 modf(du/M_PI, &aNbPer);
1164 number = (Standard_Integer)aNbPer;
1168 if (!myBSpline.IsNull())
1170 if (du != 0. || dv != 0.)
1171 myBSpline->Translate( gp_Vec2d(du,dv) );
1174 gp_Ax2d Axe( gp_Pnt2d(0.,0.), gp_Dir2d(1.,0.) );
1175 myBSpline->Mirror( Axe );
1181 //=======================================================================
1182 //function : BSpline
1184 //=======================================================================
1186 Handle(Geom2d_BSplineCurve) ProjLib_ComputeApprox::BSpline() const
1192 //=======================================================================
1195 //=======================================================================
1197 Handle(Geom2d_BezierCurve) ProjLib_ComputeApprox::Bezier() const
1204 //=======================================================================
1205 //function : Tolerance
1207 //=======================================================================
1209 Standard_Real ProjLib_ComputeApprox::Tolerance() const