1 // Created on: 1993-03-25
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 // Passage en classe persistante - 23/01/91
18 // Modif suite a la deuxieme revue de projet toolkit Geometry -23/01/91
20 // Actuellement pour les champs de la courbe le tableau des poles est
21 // declare de 1 a NbPoles et le tableau des poids est declare de 1 a NbPoles
24 // Revised RLE Aug 19 1993
25 // Suppressed Swaps, added Init, removed typedefs
27 #define No_Standard_OutOfRange
28 #define No_Standard_DimensionError
31 #include <BSplCLib.hxx>
32 #include <Geom2d_BezierCurve.hxx>
33 #include <Geom2d_Geometry.hxx>
35 #include <gp_Pnt2d.hxx>
36 #include <gp_Trsf2d.hxx>
37 #include <gp_Vec2d.hxx>
40 #include <Standard_ConstructionError.hxx>
41 #include <Standard_DimensionError.hxx>
42 #include <Standard_OutOfRange.hxx>
43 #include <Standard_RangeError.hxx>
44 #include <Standard_Type.hxx>
45 #include <TColStd_Array1OfInteger.hxx>
46 #include <TColStd_Array1OfReal.hxx>
48 //=======================================================================
50 //purpose : check rationality of an array of weights
51 //=======================================================================
52 static Standard_Boolean Rational(const TColStd_Array1OfReal& W)
54 Standard_Integer i, n = W.Length();
55 Standard_Boolean rat = Standard_False;
56 for (i = 1; i < n; i++) {
57 rat = Abs(W(i) - W(i+1)) > gp::Resolution();
64 //=======================================================================
65 //function : Geom2d_BezierCurve
67 //=======================================================================
69 Geom2d_BezierCurve::Geom2d_BezierCurve
70 (const TColgp_Array1OfPnt2d& Poles):
71 validcache(0), parametercache(0.), spanlenghtcache(1.)
75 Handle(TColgp_HArray1OfPnt2d) npoles =
76 new TColgp_HArray1OfPnt2d(1,Poles.Length());
78 npoles->ChangeArray1() = Poles;
82 Handle(TColStd_HArray1OfReal)());
86 //=======================================================================
87 //function : Geom2d_BezierCurve
89 //=======================================================================
91 Geom2d_BezierCurve::Geom2d_BezierCurve
92 (const TColgp_Array1OfPnt2d& Poles,
93 const TColStd_Array1OfReal& Weights):
94 validcache(0), parametercache(0.), spanlenghtcache(1.)
99 Handle(TColgp_HArray1OfPnt2d) npoles =
100 new TColgp_HArray1OfPnt2d(1,Poles.Length());
102 npoles->ChangeArray1() = Poles;
107 Standard_Integer nbpoles = Poles.Length();
109 if (Weights.Length() != nbpoles)
110 Standard_ConstructionError::Raise();
113 for (i = 1; i <= nbpoles; i++) {
114 if (Weights(i) <= gp::Resolution()) {
115 Standard_ConstructionError::Raise();
119 // check really rational
120 Standard_Boolean rat = Rational(Weights);
123 Handle(TColStd_HArray1OfReal) nweights;
125 nweights = new TColStd_HArray1OfReal(1,nbpoles);
126 nweights->ChangeArray1() = Weights;
130 Init(npoles,nweights);
134 //=======================================================================
135 //function : Increase
136 //purpose : increase degree
137 //=======================================================================
139 void Geom2d_BezierCurve::Increase (const Standard_Integer Deg)
141 if (Deg == Degree()) return;
143 Standard_ConstructionError_Raise_if
145 Deg > Geom2d_BezierCurve::MaxDegree(), "Geom2d_BezierCurve::Increase");
147 Handle(TColgp_HArray1OfPnt2d) npoles =
148 new TColgp_HArray1OfPnt2d(1,Deg+1);
150 Handle(TColStd_HArray1OfReal) nweights;
152 TColStd_Array1OfReal bidknots(1,2); bidknots(1) = 0.; bidknots(2) = 1.;
153 TColStd_Array1OfInteger bidmults(1,2); bidmults.Init(Degree() + 1);
156 nweights = new TColStd_HArray1OfReal(1,Deg+1);
157 BSplCLib::IncreaseDegree(Degree(), Deg, 0,
158 poles->Array1(),&weights->Array1(),
160 npoles->ChangeArray1(),&nweights->ChangeArray1(),
164 BSplCLib::IncreaseDegree(Degree(), Deg, 0,
166 BSplCLib::NoWeights(),
168 npoles->ChangeArray1(),
169 BSplCLib::NoWeights(),
173 Init(npoles,nweights);
177 //=======================================================================
178 //function : MaxDegree
180 //=======================================================================
182 Standard_Integer Geom2d_BezierCurve::MaxDegree ()
184 return BSplCLib::MaxDegree();
188 //=======================================================================
189 //function : InsertPoleAfter
191 //=======================================================================
193 void Geom2d_BezierCurve::InsertPoleAfter
194 (const Standard_Integer Index,
196 const Standard_Real Weight)
198 Standard_Integer nbpoles = NbPoles();
200 Standard_ConstructionError_Raise_if
201 (nbpoles >= Geom2d_BezierCurve::MaxDegree() ||
202 Weight <= gp::Resolution(),
203 "Geom2d_BezierCurve::InsertPoleAfter" );
205 Standard_OutOfRange_Raise_if
206 (Index < 0 || Index > nbpoles,
207 "Geom2d_BezierCurve::InsertPoleAfter");
212 Handle(TColgp_HArray1OfPnt2d) npoles =
213 new TColgp_HArray1OfPnt2d(1,nbpoles+1);
215 TColgp_Array1OfPnt2d& newpoles = npoles->ChangeArray1();
216 const TColgp_Array1OfPnt2d& oldpoles = poles->Array1();
218 for (i = 1; i <= Index; i++)
219 newpoles(i) = oldpoles(i);
221 newpoles(Index+1) = P;
223 for (i = Index+1; i <= nbpoles; i++)
224 newpoles(i+1) = oldpoles(i);
228 Handle(TColStd_HArray1OfReal) nweights;
229 Standard_Boolean rat = IsRational() || Abs(Weight-1.) > gp::Resolution();
232 nweights = new TColStd_HArray1OfReal(1,nbpoles+1);
233 TColStd_Array1OfReal& newweights = nweights->ChangeArray1();
235 for (i = 1; i <= Index; i++)
237 newweights(i) = weights->Value(i);
241 newweights(Index+1) = Weight;
243 for (i = Index+1; i <= nbpoles; i++)
245 newweights(i+1) = weights->Value(i);
247 newweights(i+1) = 1.;
251 Init(npoles,nweights);
255 //=======================================================================
256 //function : InsertPoleBefore
258 //=======================================================================
260 void Geom2d_BezierCurve::InsertPoleBefore
261 (const Standard_Integer Index,
263 const Standard_Real Weight)
265 InsertPoleAfter(Index-1,P,Weight);
269 //=======================================================================
270 //function : RemovePole
272 //=======================================================================
274 void Geom2d_BezierCurve::RemovePole
275 (const Standard_Integer Index)
277 Standard_Integer nbpoles = NbPoles();
279 Standard_ConstructionError_Raise_if
280 (nbpoles <= 2 , "Geom2d_BezierCurve::RemovePole" );
282 Standard_OutOfRange_Raise_if
283 (Index < 1 || Index > nbpoles,
284 "Geom2d_BezierCurve::RemovePole");
289 Handle(TColgp_HArray1OfPnt2d) npoles =
290 new TColgp_HArray1OfPnt2d(1,nbpoles-1);
292 TColgp_Array1OfPnt2d& newpoles = npoles->ChangeArray1();
293 const TColgp_Array1OfPnt2d& oldpoles = poles->Array1();
295 for (i = 1; i < Index; i++)
296 newpoles(i) = oldpoles(i);
298 for (i = Index+1; i <= nbpoles; i++)
299 newpoles(i-1) = oldpoles(i);
303 Handle(TColStd_HArray1OfReal) nweights;
306 nweights = new TColStd_HArray1OfReal(1,nbpoles-1);
307 TColStd_Array1OfReal& newweights = nweights->ChangeArray1();
308 const TColStd_Array1OfReal& oldweights = weights->Array1();
310 for (i = 1; i < Index; i++)
311 newweights(i) = oldweights(i);
313 for (i = Index+1; i <= nbpoles; i++)
314 newweights(i-1) = oldweights(i);
317 Init(npoles,nweights);
321 //=======================================================================
324 //=======================================================================
326 void Geom2d_BezierCurve::Reverse ()
329 Standard_Integer i, nbpoles = NbPoles();
330 TColgp_Array1OfPnt2d & cpoles = poles->ChangeArray1();
333 for (i = 1; i <= nbpoles / 2; i++) {
335 cpoles(i) = cpoles(nbpoles-i+1);
336 cpoles(nbpoles-i+1) = P;
341 TColStd_Array1OfReal & cweights = weights->ChangeArray1();
343 for (i = 1; i <= nbpoles / 2; i++) {
345 cweights(i) = cweights(nbpoles-i+1);
346 cweights(nbpoles-i+1) = w;
350 UpdateCoefficients();
354 //=======================================================================
355 //function : ReversedParameter
357 //=======================================================================
359 Standard_Real Geom2d_BezierCurve::ReversedParameter
360 ( const Standard_Real U) const
366 //=======================================================================
369 //=======================================================================
371 void Geom2d_BezierCurve::Segment
372 (const Standard_Real U1, const Standard_Real U2)
374 closed = (Abs(Value(U1).Distance (Value(U2))) <= gp::Resolution());
376 // WARNING : when calling trimming be carefull that the cache
377 // is computed regarding 0.0e0 and not 1.0e0
381 PLib::Trimming(U1,U2,coeffs->ChangeArray1(),&wcoeffs->ChangeArray1());
382 PLib::CoefficientsPoles(coeffs->Array1(),&wcoeffs->Array1(),
383 poles->ChangeArray1(),&weights->ChangeArray1());
386 PLib::Trimming(U1,U2,coeffs->ChangeArray1(), PLib::NoWeights());
387 PLib::CoefficientsPoles(coeffs->Array1(), PLib::NoWeights(),
388 poles->ChangeArray1(), PLib::NoWeights());
390 UpdateCoefficients();
394 //=======================================================================
397 //=======================================================================
399 void Geom2d_BezierCurve::SetPole
400 (const Standard_Integer Index,
403 Standard_OutOfRange_Raise_if (Index < 1 || Index > NbPoles(),
404 "Geom2d_BezierCurve::SetPole");
406 TColgp_Array1OfPnt2d& cpoles = poles->ChangeArray1();
409 if (Index == 1 || Index == cpoles.Length()) {
410 closed = (cpoles(1).Distance(cpoles(NbPoles())) <= gp::Resolution());
413 UpdateCoefficients();
417 //=======================================================================
420 //=======================================================================
422 void Geom2d_BezierCurve::SetPole
423 (const Standard_Integer Index,
425 const Standard_Real Weight)
428 SetWeight(Index,Weight);
432 //=======================================================================
433 //function : SetWeight
435 //=======================================================================
437 void Geom2d_BezierCurve::SetWeight
438 (const Standard_Integer Index,
439 const Standard_Real Weight)
441 Standard_Integer nbpoles = NbPoles();
443 Standard_OutOfRange_Raise_if
444 (Index < 1 || Index > nbpoles,
445 "Geom2d_BezierCurve::SetWeight");
446 Standard_ConstructionError_Raise_if
447 (Weight <= gp::Resolution (),
448 "Geom2d_BezierCurve::SetWeight");
451 // compute new rationality
452 Standard_Boolean wasrat = IsRational();
454 // a weight of 1. does not turn to rational
455 if (Abs(Weight - 1.) <= gp::Resolution()) return;
458 weights = new TColStd_HArray1OfReal(1,nbpoles);
459 wcoeffs = new TColStd_HArray1OfReal(1,nbpoles);
463 TColStd_Array1OfReal & cweights = weights->ChangeArray1();
464 cweights(Index) = Weight;
466 // is it turning into non rational
468 if (!Rational(cweights)) {
474 UpdateCoefficients();
478 //=======================================================================
479 //function : IsClosed
481 //=======================================================================
483 Standard_Boolean Geom2d_BezierCurve::IsClosed () const
489 //=======================================================================
492 //=======================================================================
494 Standard_Boolean Geom2d_BezierCurve::IsCN (const Standard_Integer ) const
496 return Standard_True;
500 //=======================================================================
501 //function : IsPeriodic
503 //=======================================================================
505 Standard_Boolean Geom2d_BezierCurve::IsPeriodic () const
507 return Standard_False;
511 //=======================================================================
512 //function : IsRational
514 //=======================================================================
516 Standard_Boolean Geom2d_BezierCurve::IsRational () const
518 return !weights.IsNull();
522 //=======================================================================
523 //function : Continuity
525 //=======================================================================
527 GeomAbs_Shape Geom2d_BezierCurve::Continuity () const
533 //=======================================================================
536 //=======================================================================
538 Standard_Integer Geom2d_BezierCurve::Degree () const
540 return poles->Length()-1;
544 //=======================================================================
547 //=======================================================================
549 void Geom2d_BezierCurve::D0 (const Standard_Real U, gp_Pnt2d& P ) const
551 // Idee lumineuse sacrifiee sur l autel des performances.
553 // if(!CoefficientsOK(U))
554 // ((Geom2d_BezierCurve*)(void*)this)->UpdateCoefficients(U);
556 BSplCLib::CacheD0(U,Degree(),parametercache,spanlenghtcache,
557 coeffs->Array1(),&wcoeffs->Array1(),P);
559 BSplCLib::CacheD0(U,Degree(),parametercache,spanlenghtcache,
560 coeffs->Array1(), BSplCLib::NoWeights(), P);
563 //=======================================================================
566 //=======================================================================
568 void Geom2d_BezierCurve::D1(const Standard_Real U,
572 // Idee lumineuse sacrifiee sur l autel des performances.
574 // if(!CoefficientsOK(U))
575 // ((Geom2d_BezierCurve*)(void*)this)->UpdateCoefficients(U);
577 BSplCLib::CacheD1(U,Degree(),parametercache,spanlenghtcache,
578 coeffs->Array1(),&wcoeffs->Array1(),P,V1);
580 BSplCLib::CacheD1(U,Degree(),parametercache,spanlenghtcache,
581 coeffs->Array1(), BSplCLib::NoWeights(), P,V1);
584 //=======================================================================
587 //=======================================================================
589 void Geom2d_BezierCurve::D2 (const Standard_Real U,
594 // Idee lumineuse sacrifiee sur l autel des performances.
596 // if(!CoefficientsOK(U))
597 // ((Geom2d_BezierCurve*)(void*)this)->UpdateCoefficients(U);
599 BSplCLib::CacheD2(U,Degree(),parametercache,spanlenghtcache,
600 coeffs->Array1(),&wcoeffs->Array1(),P,V1,V2);
602 BSplCLib::CacheD2(U,Degree(),parametercache,spanlenghtcache,
603 coeffs->Array1(), BSplCLib::NoWeights(), P,V1,V2);
606 //=======================================================================
609 //=======================================================================
611 void Geom2d_BezierCurve::D3 (const Standard_Real U,
617 // Idee lumineuse sacrifiee sur l autel des performances.
619 // if(!CoefficientsOK(U))
620 // ((Geom2d_BezierCurve*)(void*)this)->UpdateCoefficients(U);
622 BSplCLib::CacheD3(U,Degree(),parametercache,spanlenghtcache,
623 coeffs->Array1(),&wcoeffs->Array1(),P,V1,V2,V3);
625 BSplCLib::CacheD3(U,Degree(),parametercache,spanlenghtcache,
626 coeffs->Array1(), BSplCLib::NoWeights(), P,V1,V2,V3);
629 //=======================================================================
632 //=======================================================================
634 gp_Vec2d Geom2d_BezierCurve::DN (const Standard_Real U,
635 const Standard_Integer N) const
637 Standard_RangeError_Raise_if (N < 1, "Geom2d_BezierCurve::DN");
640 TColStd_Array1OfReal bidknots(1,2); bidknots(1) = 0.; bidknots(2) = 1.;
641 TColStd_Array1OfInteger bidmults(1,2); bidmults.Init(Degree() + 1);
644 BSplCLib::DN(U,N,0,Degree(),Standard_False,
645 poles->Array1(),&weights->Array1(),
646 bidknots,&bidmults,V);
648 BSplCLib::DN(U,N,0,Degree(),Standard_False,
650 BSplCLib::NoWeights(),
651 bidknots,&bidmults,V);
655 //=======================================================================
656 //function : EndPoint
658 //=======================================================================
660 gp_Pnt2d Geom2d_BezierCurve::EndPoint () const
662 return poles->Value (poles->Upper());
666 //=======================================================================
667 //function : FirstParameter
669 //=======================================================================
671 Standard_Real Geom2d_BezierCurve::FirstParameter () const
677 //=======================================================================
678 //function : LastParameter
680 //=======================================================================
682 Standard_Real Geom2d_BezierCurve::LastParameter () const
688 //=======================================================================
691 //=======================================================================
693 Standard_Integer Geom2d_BezierCurve::NbPoles () const
695 return poles->Length();
699 //=======================================================================
702 //=======================================================================
704 gp_Pnt2d Geom2d_BezierCurve::Pole (const Standard_Integer Index) const
706 Standard_OutOfRange_Raise_if (Index < 1 || Index > poles->Length(),
707 "Geom2d_BezierCurve::Pole");
708 return poles->Value(Index);
712 //=======================================================================
715 //=======================================================================
717 void Geom2d_BezierCurve::Poles (TColgp_Array1OfPnt2d& P) const
719 Standard_DimensionError_Raise_if (P.Length() != poles->Length(),
720 "Geom2d_BezierCurve::Poles");
725 //=======================================================================
726 //function : StartPoint
728 //=======================================================================
730 gp_Pnt2d Geom2d_BezierCurve::StartPoint () const
732 return poles->Value(1);
736 //=======================================================================
739 //=======================================================================
741 Standard_Real Geom2d_BezierCurve::Weight
742 (const Standard_Integer Index) const
744 Standard_OutOfRange_Raise_if (Index < 1 || Index > weights->Length(),
745 "Geom2d_BezierCurve::Weight");
747 return weights->Value(Index);
753 //=======================================================================
756 //=======================================================================
758 void Geom2d_BezierCurve::Weights
759 (TColStd_Array1OfReal& W) const
762 Standard_Integer nbpoles = NbPoles();
763 Standard_DimensionError_Raise_if (W.Length() != nbpoles,
764 "Geom2d_BezierCurve::Weights");
766 W = weights->Array1();
769 for (i = 1; i <= nbpoles; i++)
775 //=======================================================================
776 //function : Transform
778 //=======================================================================
780 void Geom2d_BezierCurve::Transform (const gp_Trsf2d& T)
782 Standard_Integer nbpoles = NbPoles();
783 TColgp_Array1OfPnt2d & cpoles = poles->ChangeArray1();
785 for (Standard_Integer i = 1; i <= nbpoles; i++)
786 cpoles (i).Transform(T);
788 UpdateCoefficients();
792 //=======================================================================
793 //function : Resolution
795 //=======================================================================
797 void Geom2d_BezierCurve::Resolution(const Standard_Real ToleranceUV,
798 Standard_Real & UTolerance)
801 TColStd_Array1OfReal bidflatknots(1, 2*(Degree()+1));
802 for(Standard_Integer i = 1; i <= Degree()+1; i++){
803 bidflatknots(i) = 0.;
804 bidflatknots(i + Degree() +1) = 1.;
808 BSplCLib::Resolution(poles->Array1(),
817 BSplCLib::Resolution(poles->Array1(),
818 BSplCLib::NoWeights(),
827 UTolerance = ToleranceUV * maxderivinv;
831 //=======================================================================
834 //=======================================================================
836 Handle(Geom2d_Geometry) Geom2d_BezierCurve::Copy() const {
838 Handle(Geom2d_BezierCurve) C;
840 C = new Geom2d_BezierCurve (poles->Array1(),weights->Array1());
842 C = new Geom2d_BezierCurve (poles->Array1());
847 //=======================================================================
850 //=======================================================================
852 void Geom2d_BezierCurve::Init
853 (const Handle(TColgp_HArray1OfPnt2d)& Poles,
854 const Handle(TColStd_HArray1OfReal)& Weights)
856 Standard_Integer nbpoles = Poles->Length();
858 const TColgp_Array1OfPnt2d& cpoles = Poles->Array1();
859 closed = cpoles(1).Distance(cpoles(nbpoles)) <= gp::Resolution();
862 rational = !Weights.IsNull();
866 coeffs = new TColgp_HArray1OfPnt2d (1,nbpoles);
870 wcoeffs = new TColStd_HArray1OfReal (1, nbpoles, 0.0);
877 UpdateCoefficients();
881 //=======================================================================
882 //function : CoefficientsOK
884 //=======================================================================
886 //Standard_Boolean Geom2d_BezierCurve::CoefficientsOK(const Standard_Real U)const
887 Standard_Boolean Geom2d_BezierCurve::CoefficientsOK(const Standard_Real )const
889 return (validcache) ;
894 //=======================================================================
895 //function : UpdateCoefficients
897 //=======================================================================
899 //void Geom2d_BezierCurve::UpdateCoefficients(const Standard_Real U)
900 void Geom2d_BezierCurve::UpdateCoefficients(const Standard_Real )
905 // Idee lumineuse sacrifiee sur l autel des performances.
906 // if (U >= 1.) parametercache = 1.;
907 TColStd_Array1OfReal bidflatknots(BSplCLib::FlatBezierKnots(Degree()),
910 BSplCLib::BuildCache(parametercache,spanlenghtcache,0,Degree(),
911 bidflatknots,poles->Array1(),&weights->Array1(),
912 coeffs->ChangeArray1(),&wcoeffs->ChangeArray1());
914 BSplCLib::BuildCache(parametercache,spanlenghtcache,0,Degree(),
915 bidflatknots,poles->Array1(),
916 BSplCLib::NoWeights(),
917 coeffs->ChangeArray1(),
918 BSplCLib::NoWeights());