1 // Created on: 1995-04-24
2 // Created by: Modelistation
3 // Copyright (c) 1995-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.
23 #include <ChFiDS_FilSpine.ixx>
24 #include <ChFiDS_ListIteratorOfListOfHElSpine.hxx>
25 #include <Precision.hxx>
26 #include <TColStd_HArray1OfBoolean.hxx>
27 #include <TColStd_Array1OfInteger.hxx>
29 #include <TColgp_Array1OfPnt2d.hxx>
31 #include <Law_ListIteratorOfLaws.hxx>
32 #include <Law_Constant.hxx>
34 #include <Law_Interpol.hxx>
35 #include <Standard_DomainError.hxx>
37 //=======================================================================
38 //function : ChFiDS_FilSpine
40 //=======================================================================
42 ChFiDS_FilSpine::ChFiDS_FilSpine() {}
44 ChFiDS_FilSpine::ChFiDS_FilSpine(const Standard_Real Tol) :
48 //=======================================================================
51 //=======================================================================
53 void ChFiDS_FilSpine::Reset(const Standard_Boolean AllData)
55 ChFiDS_Spine::Reset(AllData);
59 else //Complete parandrad
61 Standard_Real spinedeb = FirstParameter();
62 Standard_Real spinefin = LastParameter();
64 gp_XY FirstUandR = parandrad.First();
65 gp_XY LastUandR = parandrad.Last();
66 if (Abs( spinedeb - FirstUandR.X() ) > gp::Resolution())
68 FirstUandR.SetX( spinedeb );
69 parandrad.Prepend( FirstUandR );
71 if (Abs( spinefin - LastUandR.X() ) > gp::Resolution())
73 LastUandR.SetX( spinefin );
74 parandrad.Append( LastUandR );
78 parandrad(parandrad.Length()).SetY( parandrad(1).Y() );
82 //=======================================================================
83 //function : SetRadius
85 //=======================================================================
87 void ChFiDS_FilSpine::SetRadius(const Standard_Real Radius,
90 splitdone = Standard_False;
91 Standard_Integer IE = Index(E);
92 gp_XY FirstUandR( 0., Radius ), LastUandR( 1., Radius );
93 SetRadius( FirstUandR, IE );
94 SetRadius( LastUandR, IE );
97 //=======================================================================
98 //function : UnSetRadius
100 //=======================================================================
102 void ChFiDS_FilSpine::UnSetRadius(const TopoDS_Edge& E)
104 splitdone = Standard_False;
105 Standard_Integer IE = Index(E);
107 Standard_Real Uf = FirstParameter(IE);
108 Standard_Real Ul = LastParameter(IE);
109 Standard_Integer ifirst = 0, ilast = 0;
110 for (Standard_Integer i = 1; i <= parandrad.Length(); i++)
112 if (Abs(parandrad(i).X()-Uf) <= gp::Resolution())
114 if (Abs(parandrad(i).X()-Ul) <= gp::Resolution())
117 if (ifirst != 0 && ilast != 0)
118 parandrad.Remove( ifirst, ilast );
121 //=======================================================================
122 //function : SetRadius
124 //=======================================================================
126 void ChFiDS_FilSpine::SetRadius(const Standard_Real Radius,
127 const TopoDS_Vertex& V)
129 Standard_Real npar = Absc(V);
130 gp_XY UandR( npar, Radius );
131 SetRadius( UandR, 0 );
134 //=======================================================================
135 //function : SetRadius
137 //=======================================================================
139 void ChFiDS_FilSpine::SetRadius(const Standard_Real Radius)
142 gp_XY FirstUandR( FirstParameter(), Radius );
143 gp_XY LastUandR( LastParameter(), Radius );
144 SetRadius( FirstUandR, 0 );
145 SetRadius( LastUandR, 0 );
148 //=======================================================================
149 //function : SetRadius
151 //=======================================================================
153 void ChFiDS_FilSpine::SetRadius(const gp_XY& UandR,
154 const Standard_Integer IinC)
161 Standard_Real Uf = FirstParameter(IinC);
162 Standard_Real Ul = LastParameter(IinC);
163 W = Uf + UandR.X()*( Ul - Uf );
166 gp_XY pr( W, UandR.Y() );
168 for(i = 1; i <= parandrad.Length(); i++){
169 if(parandrad.Value(i).X() == W) {
170 parandrad.ChangeValue(i).SetY( UandR.Y() );
171 if (!splitdone) return;
174 else if(parandrad.Value(i).X() > W) {
175 parandrad.InsertBefore(i,pr);
176 if (!splitdone) return;
180 if (i == parandrad.Length()+1) parandrad.Append(pr);
182 //si le split est done il faut rejouer la law
183 //correspondant au parametre W
185 ChFiDS_ListIteratorOfListOfHElSpine It(elspines);
186 Law_ListIteratorOfLaws Itl(laws);
187 Handle(ChFiDS_HElSpine) Els = It.Value();
188 if (Els->IsPeriodic()) Itl.Value() = ComputeLaw(Els);
190 Standard_Real nW = W;
192 nW = ElCLib::InPeriod(W,FirstParameter(),LastParameter());
193 for (; It.More(); It.Next(), Itl.Next()) {
195 Standard_Real uf = Els->FirstParameter();
196 Standard_Real ul = Els->LastParameter();
197 if(uf <= W && W <= ul) {
198 Itl.Value() = ComputeLaw(Els);
205 //=======================================================================
206 //function : UnSetRadius
208 //=======================================================================
210 void ChFiDS_FilSpine::UnSetRadius(const TopoDS_Vertex& V)
212 Standard_Real npar = Absc(V);
213 for(Standard_Integer i = 1; i <= parandrad.Length(); i++){
214 if(parandrad.Value(i).X() == npar) {
221 //=======================================================================
222 //function : SetRadius
224 //=======================================================================
226 void ChFiDS_FilSpine::SetRadius(const Handle(Law_Function)& C,
227 const Standard_Integer IinC)
229 splitdone = Standard_False;
230 Handle(Law_Composite) prout = new Law_Composite();
231 Law_Laws& lst = prout->ChangeLaws();
237 //=======================================================================
238 //function : IsConstant
240 //=======================================================================
242 Standard_Boolean ChFiDS_FilSpine::IsConstant()const
244 if (parandrad.IsEmpty())
245 return Standard_False;
247 Standard_Boolean isconst = Standard_True;
248 Standard_Real Radius = parandrad(1).Y();
249 for (Standard_Integer i = 2; i <= parandrad.Length(); i++)
250 if (Abs( Radius - parandrad(i).Y() ) > Precision::Confusion())
252 isconst = Standard_False;
258 //=======================================================================
259 //function : IsConstant
261 //=======================================================================
263 Standard_Boolean ChFiDS_FilSpine::IsConstant(const Standard_Integer IE)const
265 Standard_Real Uf = FirstParameter(IE);
266 Standard_Real Ul = LastParameter(IE);
268 Standard_Real StartRad, par, rad;
270 for (i = 1; i < parandrad.Length(); i++)
272 par = parandrad(i).X();
273 rad = parandrad(i).Y();
274 Standard_Real nextpar = parandrad(i+1).X();
275 if (Abs( Uf-par ) <= gp::Resolution() ||
276 par < Uf && Uf < nextpar && nextpar-Uf > gp::Resolution())
282 for (i++; i <= parandrad.Length(); i++)
284 par = parandrad(i).X();
285 rad = parandrad(i).Y();
286 if (Abs( rad-StartRad ) > Precision::Confusion())
287 return Standard_False;
288 if (Abs( Ul-par ) <= gp::Resolution())
289 return Standard_True;
291 return Standard_True;
293 return Standard_True;
296 //=======================================================================
299 //=======================================================================
301 Standard_Real ChFiDS_FilSpine::Radius(const TopoDS_Edge& E)const
303 Standard_Integer IE = Index(E);
307 //=======================================================================
310 //=======================================================================
312 Standard_Real ChFiDS_FilSpine::Radius(const Standard_Integer IE)const
314 Standard_Real Uf = FirstParameter(IE);
315 Standard_Real Ul = LastParameter(IE);
317 Standard_Real StartRad, par, rad;
319 for (i = 1; i < parandrad.Length(); i++)
321 par = parandrad(i).X();
322 rad = parandrad(i).Y();
323 Standard_Real nextpar = parandrad(i+1).X();
324 if (Abs( Uf-par ) <= gp::Resolution() ||
325 par < Uf && Uf < nextpar && nextpar-Uf > gp::Resolution())
331 for (i++; i <= parandrad.Length(); i++)
333 par = parandrad(i).X();
334 rad = parandrad(i).Y();
335 if (Abs( rad-StartRad ) > Precision::Confusion())
336 Standard_DomainError::Raise("Edge is not constant");
337 if (Abs( Ul-par ) <= gp::Resolution())
345 //=======================================================================
348 //=======================================================================
350 Standard_Real ChFiDS_FilSpine::Radius()const
352 if (!IsConstant()) Standard_DomainError::Raise("Spine is not constant");
353 return parandrad(1).Y();
356 //=======================================================================
357 //function : AppendElSpine
359 //=======================================================================
361 void ChFiDS_FilSpine::AppendElSpine(const Handle(ChFiDS_HElSpine)& Els)
363 ChFiDS_Spine::AppendElSpine(Els);
367 //=======================================================================
368 //function : AppendLaw
370 //=======================================================================
372 void ChFiDS_FilSpine::AppendLaw(const Handle(ChFiDS_HElSpine)& Els)
374 Handle(Law_Composite) l = ComputeLaw(Els);
378 static void mklaw(Law_Laws& res,
379 const TColgp_SequenceOfXY& pr,
380 const Standard_Real curdeb,
381 const Standard_Real curfin,
382 const Standard_Real Rdeb,
383 const Standard_Real Rfin,
384 const Standard_Boolean recadre,
385 const Standard_Real deb,
386 const Standard_Real fin,
387 const Standard_Real tol3d)
389 TColgp_SequenceOfXY npr;
390 Standard_Real rad = Rdeb, raf = Rfin;
391 Standard_Boolean yaunpointsurledeb = Standard_False;
392 Standard_Boolean yaunpointsurlefin = Standard_False;
394 for (Standard_Integer i = 1; i <= pr.Length(); i++){
395 const gp_XY& cur = pr.Value(i);
396 Standard_Real wcur = cur.X();
397 if(recadre) wcur = ElCLib::InPeriod(wcur,deb,fin);
398 if( curdeb - tol3d <= wcur && wcur <= curfin + tol3d) {
399 if(wcur - curdeb < tol3d) {
400 yaunpointsurledeb = Standard_True;
402 if(Rdeb < 0.) rad = cur.Y();
403 ncur.SetCoord(curdeb,rad);
406 else if(curfin - wcur < tol3d) {
407 yaunpointsurlefin = Standard_True;
409 if(Rfin < 0.) raf = cur.Y();
410 ncur.SetCoord(curfin,raf);
413 else npr.Append(gp_XY(wcur,cur.Y()));
419 if( Rdeb < 0. && Rfin <0. )
420 Standard_DomainError::Raise("Impossible to create the law");
421 else if(Rdeb < 0. || Rfin <0.){
422 Standard_Real r = (Rfin<0.)? Rdeb : Rfin;
423 Handle(Law_Constant) loi = new Law_Constant();
424 loi->Set(r,curdeb,curfin);
428 Handle(Law_S) loi = new Law_S();
429 loi->Set(curdeb,Rdeb,curfin,Rfin);
434 if(!yaunpointsurledeb && Rdeb >= 0.) npr.Append(gp_XY(curdeb,Rdeb));
435 if(!yaunpointsurlefin && Rfin >= 0.) npr.Append(gp_XY(curfin,Rfin));
436 Standard_Integer nbp = npr.Length();
437 // for(Standard_Integer i = 1; i < nbp; i++){
439 for(i = 1; i < nbp; i++){
440 for(Standard_Integer j = i + 1; j <= nbp; j++){
441 if(npr.Value(i).X() > npr.Value(j).X()){
442 gp_XY temp = npr.Value(i);
443 npr.ChangeValue(i) = npr.Value(j);
444 npr.ChangeValue(j) = temp;
448 //Duplicates are removed.
449 Standard_Boolean fini = (nbp <= 1);
452 if(fabs(npr.Value(i).X() - npr.Value(i+1).X()) < tol3d) {
461 Handle(Law_Constant) loi = new Law_Constant();
462 loi->Set(npr.First().Y(),curdeb,npr.First().X());
466 TColgp_Array1OfPnt2d tpr(1,nbp);
467 for (Standard_Integer l = 1; l <= nbp; l++) {
468 tpr(l).SetXY(npr.Value(l));
470 Handle(Law_Interpol) curloi = new Law_Interpol();
471 curloi->Set(tpr,0.,0.,Standard_False);
475 Handle(Law_Constant) loi = new Law_Constant();
476 loi->Set(npr.Last().Y(),npr.Last().X(),curfin);
482 //=======================================================================
483 //function : ComputeLaw
485 //=======================================================================
487 Handle(Law_Composite) ChFiDS_FilSpine::ComputeLaw
488 (const Handle(ChFiDS_HElSpine)& Els)
490 Standard_Real tol3d = Precision::Confusion();
491 Standard_Real deb,fin,curdeb,curfin;
492 curdeb = deb = Els->FirstParameter();
493 curfin = fin = Els->LastParameter();
494 Standard_Integer ideb = Index(deb,Standard_True);
495 Standard_Integer ifin = Index(fin,Standard_False);
496 Standard_Integer len = NbEdges();
497 // if the spine is periodic, attention to the index and parameters
498 Standard_Real spinedeb = FirstParameter();
499 Standard_Real spinefin = LastParameter();
501 Standard_Integer nbed = ifin - ideb + 1;
502 Standard_Integer biddeb = ideb, bidfin = ifin;
504 Handle(Law_Composite) loi = new Law_Composite();
505 Law_Laws& list = loi->ChangeLaws();
506 Standard_Real Rdeb = 0., Rfin = 0., Rcur;
507 Standard_Integer icur = 1;
509 Handle(Law_Constant) lastloi;
510 Standard_Boolean lawencours = Standard_False;
514 if(deb < 0 && ideb > ifin) bidfin += len;
515 else if(fin > LastParameter(len) && ideb > ifin) bidfin += len;
516 nbed = bidfin - biddeb + 1;
518 TColStd_Array1OfInteger ind(1,nbed);
519 Standard_Integer j = 1;
520 for(Standard_Integer i = biddeb; i <= bidfin; i++){
521 ind(j++) = ((i - 1)%len) + 1;
524 if(Els->IsPeriodic()){
525 // A pereodic composite is created at range, which is eventually
526 // offset relatively to the elspine, to avoid a single point at
529 //Is there a constant edge?
530 // for(Standard_Integer k = 1; k <= len; k++){
532 for( k = 1; k <= len; k++){
533 if (IsConstant(k)){ // yes !
534 spinedeb = deb = curdeb = FirstParameter(k);
535 spinefin = fin = deb + Period();
536 for(Standard_Integer l = 1; l <= len; l++){
537 ind(l) = ((k + l -2)%len) + 1;
539 Rdeb = Rfin = Radius(k);
541 if(len == 1) curfin = LastParameter(k);//because InPeriod will make 0.!!!
542 else curfin = ElCLib::InPeriod(LastParameter(k),spinedeb,spinefin);
543 Handle(Law_Constant) curloi = new Law_Constant();
544 curloi->Set(Rdeb,curdeb,curfin);
551 if(parandrad.IsEmpty())
552 Standard_DomainError::Raise("Radius not defined");
553 Standard_Integer nbp = parandrad.Length();
555 deb = parandrad.First().X();
556 fin = deb + Period();
557 if(parandrad.Last().X() - fin < - tol3d) nbp++;
560 TColgp_Array1OfPnt2d pr(1,nbp);
561 for (Standard_Integer l = 1; l < nbp; l++) {
562 pr(l).SetXY(parandrad(l));
564 pr(nbp).SetCoord(fin,pr(1).Y());
565 Handle(Law_Interpol) curloi = new Law_Interpol();
566 curloi->Set(pr,Standard_True);
571 else if(IsPeriodic()){
573 if (IsConstant(ind(1))) {
574 Rdeb = Radius(ind(1));
575 curfin = LastParameter(ind(1));
576 curfin = ElCLib::InPeriod(curfin,spinedeb + tol3d, spinefin + tol3d);
577 curfin = Min(fin,curfin);
578 Handle(Law_Constant) curloi = new Law_Constant();
579 curloi->Set(Rdeb,curdeb,curfin);
585 // There is inevitably kpart right before!
586 Standard_Integer iprec = (ind(1) - 1);
587 if(iprec == 0) iprec = len;
588 if (IsConstant(iprec)){
589 Rdeb = Radius(iprec);
591 else Standard_DomainError::Raise("AppendLaw : previous constant is missing!");
592 lawencours = Standard_True;
594 // the raduis at end.
595 if (IsConstant(ind(nbed))) Rfin = Radius(ind(nbed));
597 // There is inevitably kpart right after!
598 Standard_Integer isuiv = (ind(nbed) + 1);
599 if(isuiv == len + 1) isuiv = 1;
600 if (IsConstant(isuiv)) {
601 Rfin = Radius(isuiv);
603 else Standard_DomainError::Raise("AppendLaw : next constant is missing!");
607 // the radius at start.
608 if (IsConstant(ind(1))) {
609 Rdeb = Radius(ind(1));
610 curfin = Min(fin,LastParameter(ind(1)));
611 Handle(Law_Constant) curloi = new Law_Constant();
612 curloi->Set(Rdeb,curdeb,curfin);
619 if (IsConstant(ind(1) - 1)){
620 Rdeb = Radius(ind(1) - 1);
622 else Standard_DomainError::Raise("AppendLaw : previous constant is missing");
624 else if(parandrad.IsEmpty()){
625 Standard_DomainError::Raise("AppendLaw : no radius on vertex");
628 lawencours = Standard_True;
630 // the radius at end.
631 if (IsConstant(ind(nbed))) Rfin = Radius(ind(nbed));
634 if (IsConstant(ind(nbed) + 1)) Rfin = Radius(ind(nbed) + 1);
635 else Standard_DomainError::Raise("AppendLaw : next constant is missing");
637 else if(parandrad.IsEmpty()){
638 Standard_DomainError::Raise("AppendLaw : no radius on vertex");
644 // There are infos on the extremities of the elspine,
645 // all edges are parsed
646 for(; icur <= nbed; icur++){
647 if (IsConstant(ind(icur))) {
648 Rcur = Radius(ind(icur));
651 mklaw(temp,parandrad,curdeb,curfin,Rdeb,Rcur,
652 IsPeriodic(),spinedeb,spinefin,tol3d);
654 lawencours = Standard_False;
657 curfin = LastParameter(ind(icur));
659 curfin = ElCLib::InPeriod(curfin,spinedeb + tol3d, spinefin + tol3d);
660 if(ind(icur) == ind(nbed)){
661 // Attention the curfin can be wrong if the last edge passes
662 // above the origin periodic spline.
663 Standard_Real biddeb = FirstParameter(ind(icur));
664 biddeb = ElCLib::InPeriod(biddeb,spinedeb + tol3d, spinefin + tol3d);
665 if(biddeb >= curfin) curfin = fin;
666 else curfin = Min(fin,curfin);
668 else curfin = Min(fin,curfin);
670 if((curfin - curdeb) > tol3d){
672 Handle(Law_Constant) curloi = new Law_Constant();
673 curloi->Set(Rdeb,curdeb,curfin);
679 curfin = LastParameter(ind(icur));
681 curfin = ElCLib::InPeriod(curfin,spinedeb + tol3d, spinefin + tol3d);
682 curfin = Min(fin,curfin);
683 lawencours = Standard_True;
684 if(ind(icur) == ind(nbed)){
685 // Attention the curfin can be wrong if the last edge passes
686 // above the origin periodic spline.
688 Standard_Real biddeb = FirstParameter(ind(icur));
689 curfin = LastParameter(ind(icur));
690 biddeb = ElCLib::InPeriod(biddeb,spinedeb + tol3d, spinefin + tol3d);
691 curfin = ElCLib::InPeriod(curfin,spinedeb + tol3d, spinefin + tol3d);
692 if(biddeb >= curfin) curfin = fin;
693 else curfin = Min(fin,curfin);
695 // or if it is the end of spine with extension.
696 else if(ind(icur) == len) curfin = fin;
698 mklaw(temp,parandrad,curdeb,curfin,Rdeb,Rfin,
699 IsPeriodic(),spinedeb,spinefin,tol3d);
704 if(!lastloi.IsNull()) list.Append(lastloi);
708 //=======================================================================
711 //=======================================================================
713 Handle(Law_Composite) ChFiDS_FilSpine::Law(const Handle(ChFiDS_HElSpine)& Els) const
715 ChFiDS_ListIteratorOfListOfHElSpine Itsp(elspines);
716 Law_ListIteratorOfLaws Itl(laws);
717 for(; Itsp.More(); Itsp.Next(), Itl.Next()){
718 if(Els == Itsp.Value()){
719 return Handle(Law_Composite)::DownCast(Itl.Value());
722 return Handle(Law_Composite)();
725 //=======================================================================
728 //=======================================================================
730 Handle(Law_Function)& ChFiDS_FilSpine::ChangeLaw(const TopoDS_Edge& E)
733 Standard_DomainError::Raise("ChFiDS_FilSpine::ChangeLaw : the limits are not up-to-date");
735 Standard_Integer IE = Index(E);
736 if (IsConstant(IE)) {
737 Standard_DomainError::Raise("ChFiDS_FilSpine::ChangeLaw : no law on constant edges");
739 Handle(ChFiDS_HElSpine) hsp = ElSpine(IE);
740 Standard_Real w = 0.5*(FirstParameter(IE) + LastParameter(IE));
741 Handle(Law_Composite) lc = Law(hsp);
742 return lc->ChangeElementaryLaw(w);
746 //=======================================================================
749 //=======================================================================
751 Standard_Real ChFiDS_FilSpine::MaxRadFromSeqAndLaws()const
753 Standard_Real MaxRad = 0.;
755 for (Standard_Integer i = 1; i <= parandrad.Length(); i++)
756 if (parandrad(i).Y() > MaxRad)
757 MaxRad = parandrad(i).Y();
759 Law_ListIteratorOfLaws itl( laws );
760 for (; itl.More(); itl.Next())
762 Handle(Law_Function) law = itl.Value();
763 Standard_Real fpar, lpar, par, delta, rad;
764 law->Bounds( fpar, lpar );
765 delta = (lpar - fpar)*0.2;
766 for (Standard_Integer i = 0; i <= 4; i++)
768 par = fpar + i*delta;
769 rad = law->Value(par);
773 rad = law->Value(lpar);