1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
15 #include <BSplSLib.hxx>
16 #include <Convert_GridPolynomialToPoles.hxx>
17 #include <Geom_BezierSurface.hxx>
18 #include <Geom_BSplineSurface.hxx>
19 #include <Geom_OsculatingSurface.hxx>
20 #include <Geom_Surface.hxx>
22 #include <Precision.hxx>
23 #include <TColgp_Array1OfPnt.hxx>
24 #include <TColgp_Array2OfPnt.hxx>
25 #include <TColgp_Array2OfVec.hxx>
26 #include <TColgp_HArray2OfPnt.hxx>
27 #include <TColStd_Array1OfInteger.hxx>
28 #include <TColStd_Array1OfReal.hxx>
29 #include <TColStd_HArray1OfInteger.hxx>
30 #include <TColStd_HArray1OfReal.hxx>
31 #include <TColStd_HArray2OfInteger.hxx>
33 IMPLEMENT_STANDARD_RTTIEXT(Geom_OsculatingSurface,MMgt_TShared)
35 //=======================================================================
36 //function : Geom_OffsetOsculatingSurface
38 //=======================================================================
39 Geom_OsculatingSurface::Geom_OsculatingSurface()
42 myAlong.Init(Standard_False);
44 //=======================================================================
45 //function : Geom_OffsetOsculatingSurface
47 //=======================================================================
49 Geom_OsculatingSurface::Geom_OsculatingSurface(const Handle(Geom_Surface)& BS,
50 const Standard_Real Tol)
56 //=======================================================================
59 //=======================================================================
61 void Geom_OsculatingSurface::Init(const Handle(Geom_Surface)& BS,
62 const Standard_Real Tol)
66 Standard_Real TolMin=0.;//consider all singularities below Tol, not just above 1.e-12 (id23943)
67 Standard_Boolean OsculSurf = Standard_True;
68 myBasisSurf = Handle(Geom_Surface)::DownCast(BS->Copy());
69 myOsculSurf1 = new Geom_HSequenceOfBSplineSurface();
70 myOsculSurf2 = new Geom_HSequenceOfBSplineSurface();
71 if ((BS->IsKind(STANDARD_TYPE(Geom_BSplineSurface))) ||
72 (BS->IsKind(STANDARD_TYPE(Geom_BezierSurface))))
74 Standard_Real U1=0,U2=0,V1=0,V2=0;
76 Standard_Integer i = 1;
77 BS->Bounds(U1,U2,V1,V2);
78 myAlong.SetValue(1,IsQPunctual(BS,V1,GeomAbs_IsoV,TolMin,Tol));
79 myAlong.SetValue(2,IsQPunctual(BS,V2,GeomAbs_IsoV,TolMin,Tol));
80 myAlong.SetValue(3,IsQPunctual(BS,U1,GeomAbs_IsoU,TolMin,Tol));
81 myAlong.SetValue(4,IsQPunctual(BS,U2,GeomAbs_IsoU,TolMin,Tol));
83 cout<<myAlong(1)<<endl<<myAlong(2)<<endl<<myAlong(3)<<endl<<myAlong(4)<<endl;
85 if (myAlong(1) || myAlong(2) || myAlong(3) || myAlong(4))
87 Handle(Geom_BSplineSurface) InitSurf, L,S;
88 if (BS->IsKind(STANDARD_TYPE(Geom_BezierSurface)))
90 Handle(Geom_BezierSurface) BzS = Handle(Geom_BezierSurface)::DownCast(BS);
91 TColgp_Array2OfPnt P(1,BzS->NbUPoles(),1,BzS->NbVPoles());
92 TColStd_Array1OfReal UKnots(1,2);
93 TColStd_Array1OfReal VKnots(1,2);
94 TColStd_Array1OfInteger UMults(1,2);
95 TColStd_Array1OfInteger VMults(1,2);
98 UKnots.SetValue(i,(i-1));
99 VKnots.SetValue(i,(i-1));
100 UMults.SetValue(i,BzS->UDegree()+1);
101 VMults.SetValue(i,BzS->VDegree()+1);
104 InitSurf = new Geom_BSplineSurface(P,UKnots,VKnots,
113 InitSurf = Handle(Geom_BSplineSurface)::DownCast(myBasisSurf);
116 cout<<"UDEG: "<<InitSurf->UDegree()<<endl;
117 cout<<"VDEG: "<<InitSurf->VDegree()<<endl;
120 if(IsAlongU() && IsAlongV()) ClearOsculFlags();
121 // Standard_ConstructionError_Raise_if((IsAlongU() && IsAlongV()),"Geom_OsculatingSurface");
122 if ((IsAlongU() && InitSurf->VDegree()>1) ||
123 (IsAlongV() && InitSurf->UDegree()>1))
125 myKdeg = new TColStd_HSequenceOfInteger();
126 Standard_Integer k=0;
127 Standard_Boolean IsQPunc;
128 Standard_Integer UKnot,VKnot;
129 if (myAlong(1) || myAlong(2))
131 for (i=1;i<InitSurf->NbUKnots();i++)
135 S = InitSurf; k=0; IsQPunc=Standard_True;
140 OsculSurf = BuildOsculatingSurface(V1,UKnot,VKnot,S,L);
141 if(!OsculSurf) break;
144 cout<<"1.k = "<<k<<endl;
146 IsQPunc=IsQPunctual(L,V1,GeomAbs_IsoV,0.,Tol);
153 myOsculSurf1->Append(L);
155 ClearOsculFlags(); //myAlong.SetValue(1,Standard_False);
156 if (myAlong(2) && OsculSurf)
158 S = InitSurf; k=0; IsQPunc=Standard_True;
160 VKnot=InitSurf->NbVKnots()-1;
164 OsculSurf = BuildOsculatingSurface(V2,UKnot,VKnot,S,L);
165 if(!OsculSurf) break;
168 cout<<"2.k = "<<k<<endl;
170 IsQPunc=IsQPunctual(L,V2,GeomAbs_IsoV,0.,Tol);
177 myOsculSurf2->Append(L);
185 S = InitSurf; k=0; IsQPunc=Standard_True;
187 VKnot=InitSurf->NbVKnots()-1;
190 OsculSurf = BuildOsculatingSurface(V2,UKnot,VKnot,S,L);
191 if(!OsculSurf) break;
194 cout<<"2.k = "<<k<<endl;
196 IsQPunc=IsQPunctual(L,V2,GeomAbs_IsoV,0.,Tol);
204 myOsculSurf2->Append(L);
208 ClearOsculFlags(); //myAlong.SetValue(2,Standard_False);
212 if (myAlong(3) || myAlong(4))
214 for (i=1;i<InitSurf->NbVKnots();i++)
218 S = InitSurf; k=0; IsQPunc=Standard_True;
223 OsculSurf = BuildOsculatingSurface(U1,UKnot,VKnot,S,L);
224 if(!OsculSurf) break;
227 cout<<"1.k = "<<k<<endl;
229 IsQPunc=IsQPunctual(L,U1,GeomAbs_IsoU,0.,Tol);
235 myOsculSurf1->Append(L);
237 ClearOsculFlags(); //myAlong.SetValue(3,Standard_False);
238 if (myAlong(4) && OsculSurf )
240 S = InitSurf; k=0; IsQPunc=Standard_True;
241 UKnot=InitSurf->NbUKnots()-1;
245 OsculSurf = BuildOsculatingSurface(U2,UKnot,VKnot,S,L);
246 if(!OsculSurf) break;
249 cout<<"2.k = "<<k<<endl;
251 IsQPunc=IsQPunctual(L,U2,GeomAbs_IsoU,0.,Tol);
258 myOsculSurf2->Append(L);
265 S = InitSurf; k=0; IsQPunc=Standard_True;
266 UKnot=InitSurf->NbUKnots()-1;
270 OsculSurf = BuildOsculatingSurface(U2,UKnot,VKnot,S,L);
271 if(!OsculSurf) break;
274 cout<<"2.k = "<<k<<endl;
276 IsQPunc=IsQPunctual(L,U2,GeomAbs_IsoU,0.,Tol);
283 myOsculSurf2->Append(L);
287 ClearOsculFlags(); //myAlong.SetValue(4,Standard_False);
302 //=======================================================================
303 //function : BasisSurface
305 //=======================================================================
307 Handle(Geom_Surface) Geom_OsculatingSurface::BasisSurface() const
312 //=======================================================================
313 //function : Tolerance
315 //=======================================================================
317 Standard_Real Geom_OsculatingSurface::Tolerance() const
322 //=======================================================================
323 //function : UOscSurf
325 //=======================================================================
327 Standard_Boolean Geom_OsculatingSurface::UOscSurf
328 (const Standard_Real U,
329 const Standard_Real V,
331 Handle(Geom_BSplineSurface)& L) const
333 Standard_Boolean along = Standard_False;
334 if (myAlong(1) || myAlong(2))
336 Standard_Integer NU = 1, NV = 1;
337 Standard_Real u1,u2,v1,v2;
339 myBasisSurf->Bounds(u1,u2,v1,v2);
340 Standard_Integer NbUK,NbVK;
341 Standard_Boolean isToSkipSecond = Standard_False;
342 if (myBasisSurf->IsKind(STANDARD_TYPE(Geom_BSplineSurface)))
344 Handle(Geom_BSplineSurface) BSur =
345 Handle(Geom_BSplineSurface)::DownCast (myBasisSurf);
346 NbUK = BSur->NbUKnots();
347 NbVK = BSur->NbVKnots();
348 TColStd_Array1OfReal UKnots(1,NbUK);
349 TColStd_Array1OfReal VKnots(1,NbVK);
350 BSur->UKnots(UKnots);
351 BSur->VKnots(VKnots);
352 BSplCLib::Hunt(UKnots,U,NU);
353 BSplCLib::Hunt(VKnots,V,NV);
355 if (NU >= NbUK) NU=NbUK-1;
356 if (NbVK==2 && NV==1)
357 // Need to find the closest end
358 if (VKnots(NbVK)-V > V-VKnots(1))
359 isToSkipSecond = Standard_True;
361 else {NU = 1; NV = 1 ; NbVK = 2 ;}
363 if (myAlong(1) && NV == 1)
365 L = *((Handle(Geom_BSplineSurface)*)& myOsculSurf1->Value(NU));
366 along = Standard_True;
368 if (myAlong(2) && (NV == NbVK-1) && !isToSkipSecond)
370 // t means that derivative vector of osculating surface is opposite
371 // to the original. This happens when (v-t)^k is negative, i.e.
372 // difference between degrees (k) is odd and t is the last parameter
373 if (myKdeg->Value(NU)%2) t = Standard_True;
374 L = *((Handle(Geom_BSplineSurface)*)& myOsculSurf2->Value(NU));
375 along = Standard_True;
381 //=======================================================================
382 //function : VOscSurf
384 //=======================================================================
386 Standard_Boolean Geom_OsculatingSurface::VOscSurf
387 (const Standard_Real U,
388 const Standard_Real V,
390 Handle(Geom_BSplineSurface)& L) const
392 Standard_Boolean along = Standard_False;
393 if (myAlong(3) || myAlong(4))
395 Standard_Integer NU = 1, NV = 1;
396 Standard_Real u1,u2,v1,v2;
398 myBasisSurf->Bounds(u1,u2,v1,v2);
399 Standard_Integer NbUK,NbVK;
400 Standard_Boolean isToSkipSecond = Standard_False;
401 if (myBasisSurf->IsKind(STANDARD_TYPE(Geom_BSplineSurface)))
403 Handle(Geom_BSplineSurface) BSur =
404 Handle(Geom_BSplineSurface)::DownCast (myBasisSurf);
405 NbUK = BSur->NbUKnots();
406 NbVK = BSur->NbVKnots();
407 TColStd_Array1OfReal UKnots(1,NbUK);
408 TColStd_Array1OfReal VKnots(1,NbVK);
409 BSur->UKnots(UKnots);
410 BSur->VKnots(VKnots);
411 BSplCLib::Hunt(UKnots,U,NU);
412 BSplCLib::Hunt(VKnots,V,NV);
414 if (NV >= NbVK) NV=NbVK-1;
415 if (NbUK==2 && NU==1)
416 // Need to find the closest end
417 if (UKnots(NbUK)-U > U-UKnots(1))
418 isToSkipSecond = Standard_True;
420 else {NU = 1; NV = 1 ; NbUK = 2;}
422 if (myAlong(3) && NU == 1)
424 L = *((Handle(Geom_BSplineSurface)*)& myOsculSurf1->Value(NV));
425 along = Standard_True;
427 if (myAlong(4) && (NU == NbUK-1) && !isToSkipSecond)
429 if (myKdeg->Value(NV)%2) t = Standard_True;
430 L = *((Handle(Geom_BSplineSurface)*)& myOsculSurf2->Value(NV));
431 along = Standard_True;
437 //=======================================================================
438 //function : BuildOsculatingSurface
440 //=======================================================================
442 Standard_Boolean Geom_OsculatingSurface::BuildOsculatingSurface
443 (const Standard_Real Param,
444 const Standard_Integer SUKnot,
445 const Standard_Integer SVKnot,
446 const Handle(Geom_BSplineSurface)& BS,
447 Handle(Geom_BSplineSurface)& BSpl) const
449 Standard_Integer i, j;
450 Standard_Boolean OsculSurf=Standard_True;
452 cout<<"t = "<<Param<<endl;
453 cout<<"======================================"<<endl<<endl;
457 Standard_Integer MinDegree,
459 Standard_Real udeg, vdeg;
460 udeg = BS->UDegree();
461 vdeg = BS->VDegree();
462 if( (IsAlongU() && vdeg <=1) || (IsAlongV() && udeg <=1))
465 cout<<" surface osculatrice nulle "<<endl;
467 //throw Standard_ConstructionError("Geom_OsculatingSurface");
468 OsculSurf=Standard_False;
472 MinDegree = (Standard_Integer ) Min(udeg,vdeg) ;
473 MaxDegree = (Standard_Integer ) Max(udeg,vdeg) ;
475 TColgp_Array2OfPnt cachepoles(1, MaxDegree + 1, 1, MinDegree + 1);
478 // for polynomial grid
479 Standard_Integer MaxUDegree, MaxVDegree;
480 Standard_Integer UContinuity, VContinuity;
482 Handle(TColStd_HArray2OfInteger) NumCoeffPerSurface =
483 new TColStd_HArray2OfInteger(1, 1, 1, 2);
484 Handle(TColStd_HArray1OfReal) PolynomialUIntervals =
485 new TColStd_HArray1OfReal(1, 2);
486 Handle(TColStd_HArray1OfReal) PolynomialVIntervals =
487 new TColStd_HArray1OfReal(1, 2);
488 Handle(TColStd_HArray1OfReal) TrueUIntervals =
489 new TColStd_HArray1OfReal(1, 2);
490 Handle(TColStd_HArray1OfReal) TrueVIntervals =
491 new TColStd_HArray1OfReal(1, 2);
492 MaxUDegree = (Standard_Integer ) udeg;
493 MaxVDegree = (Standard_Integer ) vdeg;
497 PolynomialUIntervals->ChangeValue(i) = i-1;
498 PolynomialVIntervals->ChangeValue(i) = i-1;
499 TrueUIntervals->ChangeValue(i) = BS->UKnot(SUKnot+i-1);
500 TrueVIntervals->ChangeValue(i) = BS->VKnot(SVKnot+i-1);
504 Standard_Integer OscUNumCoeff=0, OscVNumCoeff=0;
508 cout<<">>>>>>>>>>> AlongU"<<endl;
510 OscUNumCoeff = (Standard_Integer ) udeg + 1;
511 OscVNumCoeff = (Standard_Integer ) vdeg;
516 cout<<">>>>>>>>>>> AlongV"<<endl;
518 OscUNumCoeff = (Standard_Integer ) udeg;
519 OscVNumCoeff = (Standard_Integer ) vdeg + 1;
521 NumCoeffPerSurface->ChangeValue(1,1) = OscUNumCoeff;
522 NumCoeffPerSurface->ChangeValue(1,2) = OscVNumCoeff;
523 Standard_Integer nbc = NumCoeffPerSurface->Value(1,1)*NumCoeffPerSurface->Value(1,2)*3;
527 return Standard_False;
530 Handle(TColStd_HArray1OfReal) Coefficients = new TColStd_HArray1OfReal(1, nbc);
531 // end for polynomial grid
533 // building the cache
534 Standard_Integer ULocalIndex, VLocalIndex;
535 Standard_Real ucacheparameter, vcacheparameter,uspanlength, vspanlength;
536 TColgp_Array2OfPnt NewPoles(1, BS->NbUPoles(), 1, BS->NbVPoles());
538 Standard_Integer aUfKnotsLength = BS->NbUPoles() + BS->UDegree() + 1;
539 Standard_Integer aVfKnotsLength = BS->NbVPoles() + BS->VDegree() + 1;
541 if(BS->IsUPeriodic())
543 TColStd_Array1OfInteger aMults(1, BS->NbUKnots());
544 BS->UMultiplicities(aMults);
545 aUfKnotsLength = BSplCLib::KnotSequenceLength(aMults, BS->UDegree(), Standard_True);
548 if(BS->IsVPeriodic())
550 TColStd_Array1OfInteger aMults(1, BS->NbVKnots());
551 BS->VMultiplicities(aMults);
552 aVfKnotsLength = BSplCLib::KnotSequenceLength(aMults, BS->VDegree(), Standard_True);
555 TColStd_Array1OfReal UFlatKnots(1, aUfKnotsLength);
556 TColStd_Array1OfReal VFlatKnots(1, aVfKnotsLength);
558 BS->UKnotSequence(UFlatKnots);
559 BS->VKnotSequence(VFlatKnots);
563 for(j = 1; j <= SVKnot; j++) VLocalIndex += BS->VMultiplicity(j);
564 for(i = 1; i <= SUKnot; i++) ULocalIndex += BS->UMultiplicity(i);
565 ucacheparameter = BS->UKnot(SUKnot);
566 vcacheparameter = BS->VKnot(SVKnot);
567 vspanlength = BS->VKnot(SVKnot + 1) - BS->VKnot(SVKnot);
568 uspanlength = BS->UKnot(SUKnot + 1) - BS->UKnot(SUKnot);
570 // On se ramene toujours a un parametrage tel que localement ce soit l'iso
571 // u=0 ou v=0 qui soit degeneree
573 Standard_Boolean IsVNegative = Param > vcacheparameter + vspanlength/2;
574 Standard_Boolean IsUNegative = Param > ucacheparameter + uspanlength/2;
576 if (IsAlongU() && (Param > vcacheparameter + vspanlength/2))
577 vcacheparameter = vcacheparameter + vspanlength;
578 if (IsAlongV() && (Param > ucacheparameter + uspanlength/2))
579 ucacheparameter = ucacheparameter + uspanlength;
581 BSplSLib::BuildCache(ucacheparameter,
594 BSplSLib::NoWeights(),
596 BSplSLib::NoWeights());
597 Standard_Integer m, n, index;
598 TColgp_Array2OfPnt OscCoeff(1,OscUNumCoeff , 1, OscVNumCoeff);
604 for(n = 1; n <= udeg + 1; n++)
605 for(m = 1; m <= vdeg; m++)
606 OscCoeff(n,m) = cachepoles(n,m+1) ;
610 for(n = 1; n <= udeg + 1; n++)
611 for(m = 1; m <= vdeg; m++)
612 OscCoeff(n,m) = cachepoles(m+1,n) ;
614 if (IsVNegative) PLib::VTrimming(-1,0,OscCoeff,PLib::NoWeights2());
617 for(n = 1; n <= udeg + 1; n++)
618 for(m = 1; m <= vdeg; m++)
620 Coefficients->ChangeValue(index++) = OscCoeff(n,m).X();
621 Coefficients->ChangeValue(index++) = OscCoeff(n,m).Y();
622 Coefficients->ChangeValue(index++) = OscCoeff(n,m).Z();
630 for(n = 1; n <= udeg; n++)
631 for(m = 1; m <= vdeg + 1; m++)
632 OscCoeff(n,m) = cachepoles(n+1,m);
636 for(n = 1; n <= udeg; n++)
637 for(m = 1; m <= vdeg + 1; m++)
638 OscCoeff(n,m) = cachepoles(m,n+1);
640 if (IsUNegative) PLib::UTrimming(-1,0,OscCoeff,PLib::NoWeights2());
642 for(n = 1; n <= udeg; n++)
643 for(m = 1; m <= vdeg + 1; m++)
645 Coefficients->ChangeValue(index++) = OscCoeff(n,m).X();
646 Coefficients->ChangeValue(index++) = OscCoeff(n,m).Y();
647 Coefficients->ChangeValue(index++) = OscCoeff(n,m).Z();
651 if (IsAlongU()) MaxVDegree--;
652 if (IsAlongV()) MaxUDegree--;
656 Convert_GridPolynomialToPoles Data(1,1,
663 PolynomialUIntervals,
664 PolynomialVIntervals,
668 // Handle(Geom_BSplineSurface) BSpl =
669 BSpl =new Geom_BSplineSurface(Data.Poles()->Array2(),
670 Data.UKnots()->Array1(),
671 Data.VKnots()->Array1(),
672 Data.UMultiplicities()->Array1(),
673 Data.VMultiplicities()->Array1(),
678 cout<<"^====================================^"<<endl<<endl;
686 //=======================================================================
687 //function : IsQPunctual
689 //=======================================================================
691 Standard_Boolean Geom_OsculatingSurface::IsQPunctual
692 (const Handle(Geom_Surface)& S,
693 const Standard_Real Param,
694 const GeomAbs_IsoType IT,
695 const Standard_Real TolMin,
696 const Standard_Real TolMax) const
698 Standard_Real U1=0,U2=0,V1=0,V2=0,T;
699 Standard_Boolean Along = Standard_True;
700 S->Bounds(U1,U2,V1,V2);
703 Standard_Real Step,D1NormMax;
704 if (IT == GeomAbs_IsoV)
708 for (T=U1;T<=U2;T=T+Step)
710 S->D1(T,Param,P,D1U,D1V);
711 D1NormMax=Max(D1NormMax,D1U.Magnitude());
715 cout << " D1NormMax = " << D1NormMax << endl;
717 if (D1NormMax >TolMax || D1NormMax < TolMin )
718 Along = Standard_False;
724 for (T=V1;T<=V2;T=T+Step)
726 S->D1(Param,T,P,D1U,D1V);
727 D1NormMax=Max(D1NormMax,D1V.Magnitude());
730 cout << " D1NormMax = " << D1NormMax << endl;
732 if (D1NormMax >TolMax || D1NormMax < TolMin )
733 Along = Standard_False;
740 //=======================================================================
741 //function : HasOscSurf
743 //=======================================================================
745 Standard_Boolean Geom_OsculatingSurface::HasOscSurf() const
747 return (myAlong(1) || myAlong(2) || myAlong(3) || myAlong(4));
750 //=======================================================================
751 //function : IsAlongU
753 //=======================================================================
755 Standard_Boolean Geom_OsculatingSurface::IsAlongU() const
757 return (myAlong(1) || myAlong(2));
759 //=======================================================================
760 //function : IsAlongV
762 //=======================================================================
764 Standard_Boolean Geom_OsculatingSurface::IsAlongV() const
766 return (myAlong(3) || myAlong(4));
770 //=======================================================================
771 //function : IsGetSeqOfL1
773 //=======================================================================
775 const Geom_SequenceOfBSplineSurface& Geom_OsculatingSurface::GetSeqOfL1() const
777 return myOsculSurf1->Sequence();
779 //=======================================================================
780 //function : IsGetSeqOfL2
782 //=======================================================================
784 const Geom_SequenceOfBSplineSurface& Geom_OsculatingSurface::GetSeqOfL2() const
786 return myOsculSurf2->Sequence();
788 //=======================================================================
789 //function : ClearOsculFlags
791 //=======================================================================
793 void Geom_OsculatingSurface::ClearOsculFlags()
795 myAlong.SetValue(1,Standard_False);
796 myAlong.SetValue(2,Standard_False);
797 myAlong.SetValue(3,Standard_False);
798 myAlong.SetValue(4,Standard_False);