1 // Created on: 1995-04-24
2 // Created by: Modelistation
3 // Copyright (c) 1995-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 #include <ChFiDS_FilSpine.ixx>
18 #include <ChFiDS_ListIteratorOfListOfHElSpine.hxx>
19 #include <Precision.hxx>
20 #include <TColStd_HArray1OfBoolean.hxx>
21 #include <TColStd_Array1OfInteger.hxx>
23 #include <TColgp_Array1OfPnt2d.hxx>
25 #include <Law_ListIteratorOfLaws.hxx>
26 #include <Law_Constant.hxx>
28 #include <Law_Interpol.hxx>
29 #include <Standard_DomainError.hxx>
30 #include <Law_Composite.hxx>
32 //=======================================================================
33 //function : ChFiDS_FilSpine
35 //=======================================================================
37 ChFiDS_FilSpine::ChFiDS_FilSpine() {}
39 ChFiDS_FilSpine::ChFiDS_FilSpine(const Standard_Real Tol) :
43 //=======================================================================
46 //=======================================================================
48 void ChFiDS_FilSpine::Reset(const Standard_Boolean AllData)
50 ChFiDS_Spine::Reset(AllData);
54 else //Complete parandrad
56 Standard_Real spinedeb = FirstParameter();
57 Standard_Real spinefin = LastParameter();
59 gp_XY FirstUandR = parandrad.First();
60 gp_XY LastUandR = parandrad.Last();
61 if (Abs( spinedeb - FirstUandR.X() ) > gp::Resolution())
63 FirstUandR.SetX( spinedeb );
64 parandrad.Prepend( FirstUandR );
66 if (Abs( spinefin - LastUandR.X() ) > gp::Resolution())
68 LastUandR.SetX( spinefin );
69 parandrad.Append( LastUandR );
73 parandrad(parandrad.Length()).SetY( parandrad(1).Y() );
77 //=======================================================================
78 //function : SetRadius
80 //=======================================================================
82 void ChFiDS_FilSpine::SetRadius(const Standard_Real Radius,
85 splitdone = Standard_False;
86 Standard_Integer IE = Index(E);
87 gp_XY FirstUandR( 0., Radius ), LastUandR( 1., Radius );
88 SetRadius( FirstUandR, IE );
89 SetRadius( LastUandR, IE );
92 //=======================================================================
93 //function : UnSetRadius
95 //=======================================================================
97 void ChFiDS_FilSpine::UnSetRadius(const TopoDS_Edge& E)
99 splitdone = Standard_False;
100 Standard_Integer IE = Index(E);
102 Standard_Real Uf = FirstParameter(IE);
103 Standard_Real Ul = LastParameter(IE);
104 Standard_Integer ifirst = 0, ilast = 0;
105 for (Standard_Integer i = 1; i <= parandrad.Length(); i++)
107 if (Abs(parandrad(i).X()-Uf) <= gp::Resolution())
109 if (Abs(parandrad(i).X()-Ul) <= gp::Resolution())
112 if (ifirst != 0 && ilast != 0)
113 parandrad.Remove( ifirst, ilast );
116 //=======================================================================
117 //function : SetRadius
119 //=======================================================================
121 void ChFiDS_FilSpine::SetRadius(const Standard_Real Radius,
122 const TopoDS_Vertex& V)
124 Standard_Real npar = Absc(V);
125 gp_XY UandR( npar, Radius );
126 SetRadius( UandR, 0 );
129 //=======================================================================
130 //function : SetRadius
132 //=======================================================================
134 void ChFiDS_FilSpine::SetRadius(const Standard_Real Radius)
137 gp_XY FirstUandR( FirstParameter(), Radius );
138 gp_XY LastUandR( LastParameter(), Radius );
139 SetRadius( FirstUandR, 0 );
140 SetRadius( LastUandR, 0 );
143 //=======================================================================
144 //function : SetRadius
146 //=======================================================================
148 void ChFiDS_FilSpine::SetRadius(const gp_XY& UandR,
149 const Standard_Integer IinC)
156 Standard_Real Uf = FirstParameter(IinC);
157 Standard_Real Ul = LastParameter(IinC);
158 W = Uf + UandR.X()*( Ul - Uf );
161 gp_XY pr( W, UandR.Y() );
163 for(i = 1; i <= parandrad.Length(); i++){
164 if(parandrad.Value(i).X() == W) {
165 parandrad.ChangeValue(i).SetY( UandR.Y() );
166 if (!splitdone) return;
169 else if(parandrad.Value(i).X() > W) {
170 parandrad.InsertBefore(i,pr);
171 if (!splitdone) return;
175 if (i == parandrad.Length()+1) parandrad.Append(pr);
177 //si le split est done il faut rejouer la law
178 //correspondant au parametre W
180 ChFiDS_ListIteratorOfListOfHElSpine It(elspines);
181 Law_ListIteratorOfLaws Itl(laws);
182 Handle(ChFiDS_HElSpine) Els = It.Value();
183 if (Els->IsPeriodic()) Itl.Value() = ComputeLaw(Els);
185 for (; It.More(); It.Next(), Itl.Next()) {
187 Standard_Real uf = Els->FirstParameter();
188 Standard_Real ul = Els->LastParameter();
189 if(uf <= W && W <= ul) {
190 Itl.Value() = ComputeLaw(Els);
197 //=======================================================================
198 //function : UnSetRadius
200 //=======================================================================
202 void ChFiDS_FilSpine::UnSetRadius(const TopoDS_Vertex& V)
204 Standard_Real npar = Absc(V);
205 for(Standard_Integer i = 1; i <= parandrad.Length(); i++){
206 if(parandrad.Value(i).X() == npar) {
213 //=======================================================================
214 //function : SetRadius
216 //=======================================================================
218 void ChFiDS_FilSpine::SetRadius(const Handle(Law_Function)& C,
219 const Standard_Integer /*IinC*/)
221 splitdone = Standard_False;
222 Handle(Law_Composite) prout = new Law_Composite();
223 Law_Laws& lst = prout->ChangeLaws();
229 //=======================================================================
230 //function : IsConstant
232 //=======================================================================
234 Standard_Boolean ChFiDS_FilSpine::IsConstant()const
236 if (parandrad.IsEmpty())
237 return Standard_False;
239 Standard_Boolean isconst = Standard_True;
240 Standard_Real Radius = parandrad(1).Y();
241 for (Standard_Integer i = 2; i <= parandrad.Length(); i++)
242 if (Abs( Radius - parandrad(i).Y() ) > Precision::Confusion())
244 isconst = Standard_False;
250 //=======================================================================
251 //function : IsConstant
253 //=======================================================================
255 Standard_Boolean ChFiDS_FilSpine::IsConstant(const Standard_Integer IE)const
257 Standard_Real Uf = FirstParameter(IE);
258 Standard_Real Ul = LastParameter(IE);
260 Standard_Real StartRad = 0.0, par, rad;
262 for (i = 1; i < parandrad.Length(); i++)
264 par = parandrad(i).X();
265 rad = parandrad(i).Y();
266 Standard_Real nextpar = parandrad(i+1).X();
267 if (Abs( Uf-par ) <= gp::Resolution() ||
268 (par < Uf && Uf < nextpar && nextpar-Uf > gp::Resolution()))
274 for (i++; i <= parandrad.Length(); i++)
276 par = parandrad(i).X();
277 rad = parandrad(i).Y();
278 if (Abs( rad-StartRad ) > Precision::Confusion())
279 return Standard_False;
280 if (Abs( Ul-par ) <= gp::Resolution())
281 return Standard_True;
283 return Standard_True;
285 return Standard_True;
288 //=======================================================================
291 //=======================================================================
293 Standard_Real ChFiDS_FilSpine::Radius(const TopoDS_Edge& E)const
295 Standard_Integer IE = Index(E);
299 //=======================================================================
302 //=======================================================================
304 Standard_Real ChFiDS_FilSpine::Radius(const Standard_Integer IE)const
306 Standard_Real Uf = FirstParameter(IE);
307 Standard_Real Ul = LastParameter(IE);
309 Standard_Real StartRad = 0., par, rad;
311 for (i = 1; i < parandrad.Length(); i++)
313 par = parandrad(i).X();
314 rad = parandrad(i).Y();
315 Standard_Real nextpar = parandrad(i+1).X();
316 if (Abs( Uf-par ) <= gp::Resolution() ||
317 (par < Uf && Uf < nextpar && nextpar-Uf > gp::Resolution()))
323 for (i++; i <= parandrad.Length(); i++)
325 par = parandrad(i).X();
326 rad = parandrad(i).Y();
327 if (Abs( rad-StartRad ) > Precision::Confusion())
328 Standard_DomainError::Raise("Edge is not constant");
329 if (Abs( Ul-par ) <= gp::Resolution())
337 //=======================================================================
340 //=======================================================================
342 Standard_Real ChFiDS_FilSpine::Radius()const
344 if (!IsConstant()) Standard_DomainError::Raise("Spine is not constant");
345 return parandrad(1).Y();
348 //=======================================================================
349 //function : AppendElSpine
351 //=======================================================================
353 void ChFiDS_FilSpine::AppendElSpine(const Handle(ChFiDS_HElSpine)& Els)
355 ChFiDS_Spine::AppendElSpine(Els);
359 //=======================================================================
360 //function : AppendLaw
362 //=======================================================================
364 void ChFiDS_FilSpine::AppendLaw(const Handle(ChFiDS_HElSpine)& Els)
366 Handle(Law_Composite) l = ComputeLaw(Els);
370 static void mklaw(Law_Laws& res,
371 const TColgp_SequenceOfXY& pr,
372 const Standard_Real curdeb,
373 const Standard_Real curfin,
374 const Standard_Real Rdeb,
375 const Standard_Real Rfin,
376 const Standard_Boolean recadre,
377 const Standard_Real deb,
378 const Standard_Real fin,
379 const Standard_Real tol3d)
381 TColgp_SequenceOfXY npr;
382 Standard_Real rad = Rdeb, raf = Rfin;
383 Standard_Boolean yaunpointsurledeb = Standard_False;
384 Standard_Boolean yaunpointsurlefin = Standard_False;
386 for (Standard_Integer i = 1; i <= pr.Length(); i++){
387 const gp_XY& cur = pr.Value(i);
388 Standard_Real wcur = cur.X();
389 if(recadre) wcur = ElCLib::InPeriod(wcur,deb,fin);
390 if( curdeb - tol3d <= wcur && wcur <= curfin + tol3d) {
391 if(wcur - curdeb < tol3d) {
392 yaunpointsurledeb = Standard_True;
394 if(Rdeb < 0.) rad = cur.Y();
395 ncur.SetCoord(curdeb,rad);
398 else if(curfin - wcur < tol3d) {
399 yaunpointsurlefin = Standard_True;
401 if(Rfin < 0.) raf = cur.Y();
402 ncur.SetCoord(curfin,raf);
405 else npr.Append(gp_XY(wcur,cur.Y()));
411 if( Rdeb < 0. && Rfin <0. )
412 Standard_DomainError::Raise("Impossible to create the law");
413 else if(Rdeb < 0. || Rfin <0.){
414 Standard_Real r = (Rfin<0.)? Rdeb : Rfin;
415 Handle(Law_Constant) loi = new Law_Constant();
416 loi->Set(r,curdeb,curfin);
420 Handle(Law_S) loi = new Law_S();
421 loi->Set(curdeb,Rdeb,curfin,Rfin);
426 if(!yaunpointsurledeb && Rdeb >= 0.) npr.Append(gp_XY(curdeb,Rdeb));
427 if(!yaunpointsurlefin && Rfin >= 0.) npr.Append(gp_XY(curfin,Rfin));
428 Standard_Integer nbp = npr.Length();
429 // for(Standard_Integer i = 1; i < nbp; i++){
431 for(i = 1; i < nbp; i++){
432 for(Standard_Integer j = i + 1; j <= nbp; j++){
433 if(npr.Value(i).X() > npr.Value(j).X()){
434 gp_XY temp = npr.Value(i);
435 npr.ChangeValue(i) = npr.Value(j);
436 npr.ChangeValue(j) = temp;
440 //Duplicates are removed.
441 Standard_Boolean fini = (nbp <= 1);
444 if(fabs(npr.Value(i).X() - npr.Value(i+1).X()) < tol3d) {
453 Handle(Law_Constant) loi = new Law_Constant();
454 loi->Set(npr.First().Y(),curdeb,npr.First().X());
458 TColgp_Array1OfPnt2d tpr(1,nbp);
459 for (Standard_Integer l = 1; l <= nbp; l++) {
460 tpr(l).SetXY(npr.Value(l));
462 Handle(Law_Interpol) curloi = new Law_Interpol();
463 curloi->Set(tpr,0.,0.,Standard_False);
467 Handle(Law_Constant) loi = new Law_Constant();
468 loi->Set(npr.Last().Y(),npr.Last().X(),curfin);
474 //=======================================================================
475 //function : ComputeLaw
477 //=======================================================================
479 Handle(Law_Composite) ChFiDS_FilSpine::ComputeLaw
480 (const Handle(ChFiDS_HElSpine)& Els)
482 Standard_Real tol3d = Precision::Confusion();
483 Standard_Real deb,fin,curdeb,curfin;
484 curdeb = deb = Els->FirstParameter();
485 curfin = fin = Els->LastParameter();
486 Standard_Integer ideb = Index(deb,Standard_True);
487 Standard_Integer ifin = Index(fin,Standard_False);
488 Standard_Integer len = NbEdges();
489 // if the spine is periodic, attention to the index and parameters
490 Standard_Real spinedeb = FirstParameter();
491 Standard_Real spinefin = LastParameter();
493 Standard_Integer nbed = ifin - ideb + 1;
494 Standard_Integer biddeb = ideb, bidfin = ifin;
496 Handle(Law_Composite) loi = new Law_Composite();
497 Law_Laws& list = loi->ChangeLaws();
498 Standard_Real Rdeb = 0., Rfin = 0., Rcur;
499 Standard_Integer icur = 1;
501 Handle(Law_Constant) lastloi;
502 Standard_Boolean lawencours = Standard_False;
506 if(deb < 0 && ideb > ifin) bidfin += len;
507 else if(fin > LastParameter(len) && ideb > ifin) bidfin += len;
508 nbed = bidfin - biddeb + 1;
510 TColStd_Array1OfInteger ind(1,nbed);
511 Standard_Integer j = 1;
512 for(Standard_Integer i = biddeb; i <= bidfin; i++){
513 ind(j++) = ((i - 1)%len) + 1;
516 if(Els->IsPeriodic()){
517 // A pereodic composite is created at range, which is eventually
518 // offset relatively to the elspine, to avoid a single point at
521 //Is there a constant edge?
522 // for(Standard_Integer k = 1; k <= len; k++){
524 for( k = 1; k <= len; k++){
525 if (IsConstant(k)){ // yes !
526 spinedeb = deb = curdeb = FirstParameter(k);
527 spinefin = fin = deb + Period();
528 for(Standard_Integer l = 1; l <= len; l++){
529 ind(l) = ((k + l -2)%len) + 1;
531 Rdeb = Rfin = Radius(k);
533 if(len == 1) curfin = LastParameter(k);//because InPeriod will make 0.!!!
534 else curfin = ElCLib::InPeriod(LastParameter(k),spinedeb,spinefin);
535 Handle(Law_Constant) curloi = new Law_Constant();
536 curloi->Set(Rdeb,curdeb,curfin);
543 if(parandrad.IsEmpty())
544 Standard_DomainError::Raise("Radius not defined");
545 Standard_Integer nbp = parandrad.Length();
547 deb = parandrad.First().X();
548 fin = deb + Period();
549 if(parandrad.Last().X() - fin < - tol3d) nbp++;
552 TColgp_Array1OfPnt2d pr(1,nbp);
553 for (Standard_Integer l = 1; l < nbp; l++) {
554 pr(l).SetXY(parandrad(l));
556 pr(nbp).SetCoord(fin,pr(1).Y());
557 Handle(Law_Interpol) curloi = new Law_Interpol();
558 curloi->Set(pr,Standard_True);
563 else if(IsPeriodic()){
565 if (IsConstant(ind(1))) {
566 Rdeb = Radius(ind(1));
567 curfin = LastParameter(ind(1));
568 curfin = ElCLib::InPeriod(curfin,spinedeb + tol3d, spinefin + tol3d);
569 curfin = Min(fin,curfin);
570 Handle(Law_Constant) curloi = new Law_Constant();
571 curloi->Set(Rdeb,curdeb,curfin);
577 // There is inevitably kpart right before!
578 Standard_Integer iprec = (ind(1) - 1);
579 if(iprec == 0) iprec = len;
580 if (IsConstant(iprec)){
581 Rdeb = Radius(iprec);
583 else Standard_DomainError::Raise("AppendLaw : previous constant is missing!");
584 lawencours = Standard_True;
586 // the raduis at end.
587 if (IsConstant(ind(nbed))) Rfin = Radius(ind(nbed));
589 // There is inevitably kpart right after!
590 Standard_Integer isuiv = (ind(nbed) + 1);
591 if(isuiv == len + 1) isuiv = 1;
592 if (IsConstant(isuiv)) {
593 Rfin = Radius(isuiv);
595 else Standard_DomainError::Raise("AppendLaw : next constant is missing!");
599 // the radius at start.
600 if (IsConstant(ind(1))) {
601 Rdeb = Radius(ind(1));
602 curfin = Min(fin,LastParameter(ind(1)));
603 Handle(Law_Constant) curloi = new Law_Constant();
604 curloi->Set(Rdeb,curdeb,curfin);
611 if (IsConstant(ind(1) - 1)){
612 Rdeb = Radius(ind(1) - 1);
614 else Standard_DomainError::Raise("AppendLaw : previous constant is missing");
616 else if(parandrad.IsEmpty()){
617 Standard_DomainError::Raise("AppendLaw : no radius on vertex");
620 lawencours = Standard_True;
622 // the radius at end.
623 if (IsConstant(ind(nbed))) Rfin = Radius(ind(nbed));
626 if (IsConstant(ind(nbed) + 1)) Rfin = Radius(ind(nbed) + 1);
627 else Standard_DomainError::Raise("AppendLaw : next constant is missing");
629 else if(parandrad.IsEmpty()){
630 Standard_DomainError::Raise("AppendLaw : no radius on vertex");
636 // There are infos on the extremities of the elspine,
637 // all edges are parsed
638 for(; icur <= nbed; icur++){
639 if (IsConstant(ind(icur))) {
640 Rcur = Radius(ind(icur));
643 mklaw(temp,parandrad,curdeb,curfin,Rdeb,Rcur,
644 IsPeriodic(),spinedeb,spinefin,tol3d);
646 lawencours = Standard_False;
649 curfin = LastParameter(ind(icur));
651 curfin = ElCLib::InPeriod(curfin,spinedeb + tol3d, spinefin + tol3d);
652 if(ind(icur) == ind(nbed)){
653 // Attention the curfin can be wrong if the last edge passes
654 // above the origin periodic spline.
655 Standard_Real biddeb = FirstParameter(ind(icur));
656 biddeb = ElCLib::InPeriod(biddeb,spinedeb + tol3d, spinefin + tol3d);
657 if(biddeb >= curfin) curfin = fin;
658 else curfin = Min(fin,curfin);
660 else curfin = Min(fin,curfin);
662 if((curfin - curdeb) > tol3d){
664 Handle(Law_Constant) curloi = new Law_Constant();
665 curloi->Set(Rdeb,curdeb,curfin);
671 curfin = LastParameter(ind(icur));
673 curfin = ElCLib::InPeriod(curfin,spinedeb + tol3d, spinefin + tol3d);
674 curfin = Min(fin,curfin);
675 lawencours = Standard_True;
676 if(ind(icur) == ind(nbed)){
677 // Attention the curfin can be wrong if the last edge passes
678 // above the origin periodic spline.
680 Standard_Real biddeb = FirstParameter(ind(icur));
681 curfin = LastParameter(ind(icur));
682 biddeb = ElCLib::InPeriod(biddeb,spinedeb + tol3d, spinefin + tol3d);
683 curfin = ElCLib::InPeriod(curfin,spinedeb + tol3d, spinefin + tol3d);
684 if(biddeb >= curfin) curfin = fin;
685 else curfin = Min(fin,curfin);
687 // or if it is the end of spine with extension.
688 else if(ind(icur) == len) curfin = fin;
690 mklaw(temp,parandrad,curdeb,curfin,Rdeb,Rfin,
691 IsPeriodic(),spinedeb,spinefin,tol3d);
696 if(!lastloi.IsNull()) list.Append(lastloi);
700 //=======================================================================
703 //=======================================================================
705 Handle(Law_Composite) ChFiDS_FilSpine::Law(const Handle(ChFiDS_HElSpine)& Els) const
707 ChFiDS_ListIteratorOfListOfHElSpine Itsp(elspines);
708 Law_ListIteratorOfLaws Itl(laws);
709 for(; Itsp.More(); Itsp.Next(), Itl.Next()){
710 if(Els == Itsp.Value()){
711 return Handle(Law_Composite)::DownCast(Itl.Value());
714 return Handle(Law_Composite)();
717 //=======================================================================
720 //=======================================================================
722 Handle(Law_Function)& ChFiDS_FilSpine::ChangeLaw(const TopoDS_Edge& E)
725 Standard_DomainError::Raise("ChFiDS_FilSpine::ChangeLaw : the limits are not up-to-date");
727 Standard_Integer IE = Index(E);
728 if (IsConstant(IE)) {
729 Standard_DomainError::Raise("ChFiDS_FilSpine::ChangeLaw : no law on constant edges");
731 Handle(ChFiDS_HElSpine) hsp = ElSpine(IE);
732 Standard_Real w = 0.5*(FirstParameter(IE) + LastParameter(IE));
733 Handle(Law_Composite) lc = Law(hsp);
734 return lc->ChangeElementaryLaw(w);
738 //=======================================================================
741 //=======================================================================
743 Standard_Real ChFiDS_FilSpine::MaxRadFromSeqAndLaws()const
745 Standard_Real MaxRad = 0.;
747 for (Standard_Integer i = 1; i <= parandrad.Length(); i++)
748 if (parandrad(i).Y() > MaxRad)
749 MaxRad = parandrad(i).Y();
751 Law_ListIteratorOfLaws itl( laws );
752 for (; itl.More(); itl.Next())
754 Handle(Law_Function) law = itl.Value();
755 Standard_Real fpar, lpar, par, delta, rad;
756 law->Bounds( fpar, lpar );
757 delta = (lpar - fpar)*0.2;
758 for (Standard_Integer i = 0; i <= 4; i++)
760 par = fpar + i*delta;
761 rad = law->Value(par);
765 rad = law->Value(lpar);