summaryrefslogtreecommitdiff
path: root/src/Hermit/Hermit.cxx
blob: 70bded248f6a0c6aca410eb8a3316e8781682cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// File:	Hermit.cxx
// Created:	Wed Jan 15 16:24:52 1997
// Author:	Stagiaire Francois DUMONT
//		<dum@brunox.paris1.matra-dtv.fr>


#include <Hermit.ixx>
#include <Geom_BSplineCurve.hxx>
#include <Geom2d_BSplineCurve.hxx>
#include <BSplCLib.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_HArray1OfReal.hxx>
#include <TColStd_Array1OfInteger.hxx>
#include <TColStd_HArray1OfInteger.hxx>
#include <PLib.hxx>
#include <Standard_Boolean.hxx>
#include <gp_Pnt2d.hxx>
#include <TColgp_Array1OfPnt2d.hxx>
#include <Standard_DimensionError.hxx>
#include <Standard_Real.hxx>
#include <TCollection_CompareOfReal.hxx>
#include <SortTools_QuickSortOfReal.hxx>
#include <Precision.hxx>

//=======================================================================
//function : HermiteCoeff
//purpose  : calculate  the Hermite coefficients of degree 3 from BS and
//    	     store them in TAB(4 coefficients)
//=======================================================================

static void HermiteCoeff(const Handle(Geom_BSplineCurve)& BS,
			 TColStd_Array1OfReal&            TAB)
     
{
  TColStd_Array1OfReal    Knots(1,BS->NbKnots());         
  TColStd_Array1OfReal    Weights(1,BS->NbPoles()); 
  TColStd_Array1OfInteger Mults(1,BS->NbKnots());
  Standard_Integer        Degree,Index0,Index1;                     // denominateur value for u=0 & u=1
  Standard_Real           Denom0,Denom1,                            // denominator value for u=0 & u=1
                          Deriv0,Deriv1 ;                           // derivative denominator value for u=0 & 1
  Standard_Boolean        Periodic;
  
  BS->Knots(Knots);
  BSplCLib::Reparametrize(0.0,1.0,Knots);                           //affinity on the nodal vector
  BS->Weights(Weights);
  BS->Multiplicities(Mults);
  Degree   = BS->Degree();
  Periodic = BS->IsPeriodic();
  Index0   = BS->FirstUKnotIndex();
  Index1   = BS->LastUKnotIndex()-1;

  BSplCLib::D1(0.0,Index0,Degree,Periodic,Weights,BSplCLib::NoWeights(),Knots,Mults,Denom0,Deriv0);
  BSplCLib::D1(1.0,Index1,Degree,Periodic,Weights,BSplCLib::NoWeights(),Knots,Mults,Denom1,Deriv1);
  TAB(0) = 1/Denom0;                                                //Hermit coefficients
  TAB(1) = -Deriv0/(Denom0*Denom0);
  TAB(2) = -Deriv1/(Denom1*Denom1);
  TAB(3) = 1/Denom1;

}

//=======================================================================
//function : HermiteCoeff
//purpose  : calculate  the Hermite coefficients of degree 3 from BS and
//    	     store them in TAB(4 coefficients)
//=======================================================================

static void HermiteCoeff(const Handle(Geom2d_BSplineCurve)& BS,
			 TColStd_Array1OfReal&              TAB)
     
{
  TColStd_Array1OfReal    Knots(1,BS->NbKnots());         
  TColStd_Array1OfReal    Weights(1,BS->NbPoles()); 
  TColStd_Array1OfInteger Mults(1,BS->NbKnots());
  Standard_Integer        Degree,Index0,Index1;
  Standard_Real           Denom0,Denom1,                            // denominateur value for u=0 & u=1
                          Deriv0,Deriv1 ;                           // denominator value for u=0 & u=1
  Standard_Boolean        Periodic;                                 // derivative denominatur value for u=0 & 1
  
  BS->Knots(Knots);
  BSplCLib::Reparametrize(0.0,1.0,Knots);                           //affinity on the nodal vector
  BS->Weights(Weights);
  BS->Multiplicities(Mults);
  Degree   = BS->Degree();
  Periodic = BS->IsPeriodic();
  Index0   = BS->FirstUKnotIndex();
  Index1   = BS->LastUKnotIndex()-1;

  BSplCLib::D1(0.0,Index0,Degree,Periodic,Weights,BSplCLib::NoWeights(),Knots,Mults,Denom0,Deriv0);
  BSplCLib::D1(1.0,Index1,Degree,Periodic,Weights,BSplCLib::NoWeights(),Knots,Mults,Denom1,Deriv1);
  TAB(0) = 1/Denom0;                                                //Hermit coefficients
  TAB(1) = -Deriv0/(Denom0*Denom0);
  TAB(2) = -Deriv1/(Denom1*Denom1);
  TAB(3) = 1/Denom1;

}

//=======================================================================
//function : SignDenom
//purpose  : give the sign of Herm(0) True=Positive
//=======================================================================

static Standard_Boolean SignDenom(const TColgp_Array1OfPnt2d& Poles)

{
  Standard_Boolean Result;
  
  if (Poles(0).Y()<0)                                             
    Result=Standard_False;                                        
  else  Result=Standard_True;
  return Result;
}

//=======================================================================
//function : Polemax
//purpose  : give the max and the min of the Poles (by their index) 
//=======================================================================


static void Polemax(const TColgp_Array1OfPnt2d& Poles,
		    Standard_Integer&           min,
		    Standard_Integer&           max)

{
//  Standard_Integer i,index=0;
  Standard_Integer i;
  Standard_Real Max,Min;                                         //intermediate value of max and min ordinates
  min=0;max=0;                                                   //initialisation of the indices
  
  Min=Poles(0).Y();                                              //initialisation of the intermediate value
  Max=Poles(0).Y();
  for (i=1;i<=(Poles.Length()-1);i++){
    if (Poles(i).Y()<Min){
      Min=Poles(i).Y();
      min=i;
    }
    if (Poles(i).Y()>Max){
      Max=Poles(i).Y();
      max=i;
    } 
  }
}

//=======================================================================
//function : PolyTest
//purpose  : give the knots U4 and U5 to insert to a(u)
//=======================================================================
   

static void PolyTest(const TColStd_Array1OfReal&         Herm,
		     const Handle(Geom_BSplineCurve)&    BS,
		     Standard_Real&                      U4,
		     Standard_Real&                      U5,
		     Standard_Integer&                   boucle, 
		     const Standard_Real                 TolPoles,
//		     const Standard_Real                 TolKnots,
		     const Standard_Real                 ,
		     const Standard_Real                 Ux, 
		     const Standard_Real                 Uy)

{
  Standard_Integer               index,i,                
                                 I1=0,I2=0,I3=0,I4=0;    //knots index
  TColgp_Array1OfPnt2d           Polesinit(0,3) ;        
  Handle(TColStd_HArray1OfReal)  Knots;                  //array of the BSpline knots + the ones inserted
  Standard_Integer               cas=0,mark=0,dercas=0,  //loop marks
                                 min,max;                //Pole min and max indices
  Standard_Real                  Us1,Us2,a;              //bounderies value of the knots to be inserted
//  gp_Pnt2d                       P ;
  TCollection_CompareOfReal      Comp;
  
  U4=0.0;U5=1.0;                                         //default value
  if (Ux!=1.0){
    BS->LocateU(Ux,0.0,I1,I2);                          //localization of the inserted knots 
    if (Uy!=0.0)
      BS->LocateU(Uy,0.0,I3,I4);
  }

  if (I1==I2)                                            //definition and filling of the 
    if((I3==I4)||(I3==0)){                                //array of knots
      Knots=new TColStd_HArray1OfReal(1,BS->NbKnots());
      for (i=1;i<=BS->NbKnots();i++)
	Knots->SetValue(i,BS->Knot(i));
    }
    else{
      Knots=new TColStd_HArray1OfReal(1,BS->NbKnots()+1);
      for (i=1;i<=BS->NbKnots();i++)
	Knots->SetValue(i,BS->Knot(i));
      Knots->SetValue(BS->NbKnots()+1,Uy);
    }
  else{
    if((I3==I4)||(I3==0)){
      Knots=new TColStd_HArray1OfReal(1,BS->NbKnots()+1);
      for (i=1;i<=BS->NbKnots();i++)
	Knots->SetValue(i,BS->Knot(i));
      Knots->SetValue(BS->NbKnots()+1,Ux);
    }
    else{
      Knots=new TColStd_HArray1OfReal(1,BS->NbKnots()+2);
      for (i=1;i<=BS->NbKnots();i++)
	Knots->SetValue(i,BS->Knot(i));
      Knots->SetValue(BS->NbKnots()+1,Ux);
      Knots->SetValue(BS->NbKnots()+2,Uy);
    }
  }

  TColStd_Array1OfReal knots(1,Knots->Length());
  knots=Knots->ChangeArray1();
  SortTools_QuickSortOfReal::Sort(knots,Comp);        //sort of the array of knots

  Polesinit(0).SetCoord(0.0,Herm(0));                 //poles of the Hermite polynome in the BSpline form
  Polesinit(1).SetCoord(0.0,Herm(0)+Herm(1)/3.0);
  Polesinit(2).SetCoord(0.0,Herm(3)-Herm(2)/3.0);
  Polesinit(3).SetCoord(0.0,Herm(3));

                              //loop to check the tolerances on poles
  if (TolPoles!=0.0){
    Polemax(Polesinit,min,max);
    Standard_Real  Polemin=Polesinit(min).Y();
    Standard_Real  Polemax=Polesinit(max).Y();
    if (((Polemax)>=((1/TolPoles)*Polemin))||((Polemin==0.0)&&(Polemax>=(1/TolPoles)))){
      if (Polesinit(0).Y()>=(1/TolPoles)*Polesinit(3).Y()||Polesinit(0).Y()<=TolPoles*Polesinit(3).Y())
	Standard_DimensionError::Raise("Hermit Impossible Tolerance");       
      if ((max==0)||(max==3))                                                
	for (i=0;i<=3;i++)
	  Polesinit(i).SetCoord(0.0,(Polesinit(i).Y()-TolPoles*Polemax));
      if ((max==1)||(max==2)) {
	if ((min==0)||(min==3))                                             
	  for (i=0;i<=3;i++)
	    Polesinit(i).SetCoord(0.0,(Polesinit(i).Y()-(1/TolPoles)*Polemin));
	else{                                                                  
	  if ((TolPoles*Polemax<Polesinit(0).Y())&&(TolPoles*Polemax<Polesinit(3).Y())){
	    for (i=0;i<=3;i++)                                             
	      Polesinit(i).SetCoord(0.0,(Polesinit(i).Y()-TolPoles*Polemax));
	    mark=1;
	  }
	  if ((1/TolPoles*Polemin>Polesinit(0).Y())&&(1/TolPoles*Polemin>Polesinit(3).Y())&&(mark==0)){
	    for (i=0;i<=3;i++)                                             
	      Polesinit(i).SetCoord(0.0,(Polesinit(i).Y()-1/TolPoles*Polemin));
	    mark=1;
	  }
	  if (mark==0){
	    Standard_Real Pole0,Pole3;
	    Pole0=Polesinit(0).Y();
	    Pole3=Polesinit(3).Y();
	    if (Pole0<3){                         
	      a=Log10(Pole3/Pole0);                      
	      if (boucle==2)                      
		for (i=0;i<=3;i++)                                                    
		  Polesinit(i).SetCoord(0.0, Polesinit(i).Y()-(Pole3*(Pow(10.0,(-0.5*Log10(TolPoles)-a/2.0))))); 
	      if (boucle==1){
		for (i=0;i<=3;i++)                                                    
		  Polesinit(i).SetCoord(0.0, Polesinit(i).Y()-(Pole0*(Pow(10.0,(a/2.0+0.5*Log10(TolPoles)))))); 
		dercas=1;
	      }
	    }
	    if (Pole0>Pole3){                         
	      a=Log10(Pole0/Pole3);                           
	      if (boucle==2)                      
		for (i=0;i<=3;i++)                                                    
		  Polesinit(i).SetCoord(0.0, Polesinit(i).Y()-(Pole0*(Pow(10.0,(-0.5*Log10(TolPoles)-a/2.0))))); 
	      if (boucle==1){
		for (i=0;i<=3;i++)                                                    
		  Polesinit(i).SetCoord(0.0, Polesinit(i).Y()-(Pole3*(Pow(10.0,(a/2.0+0.5*Log10(TolPoles)))))); 
		dercas=1;
	      }
	    }
	  }
	}
      }
    }
  }                          //end of the loop
  
  if (!SignDenom(Polesinit))                       //invertion of the polynome sign
    for (index=0;index<=3;index++)
      Polesinit(index).SetCoord(0.0,-Polesinit(index).Y());
  
                                //loop of positivity
  if ((Polesinit(1).Y()<0.0)&&(Polesinit(2).Y()>=0.0)){              
    Us1=Polesinit(0).Y()/(Polesinit(0).Y()-Polesinit(1).Y());
    if (boucle==2)
      Us1=Us1*knots(2);
    if (boucle==1)
      if (Ux!=0.0)
	Us1=Us1*Ux;
    BSplCLib::LocateParameter(3,knots,Us1,Standard_False,1,knots.Length(),I1,Us1);
    if (I1<2)
      U4=Us1;
    else
      U4=knots(I1);
  }
  
  if ((Polesinit(1).Y()>=0.0)&&(Polesinit(2).Y()<0.0)){
    Us2=Polesinit(2).Y()/(Polesinit(2).Y()-Polesinit(3).Y());
    if (boucle==2)
      Us2=knots(knots.Length()-1)+Us2*(1-knots(knots.Length()-1));
    if (boucle==1)
      if (Ux!=0.0)
	Us2=Uy+Us2*(1-Uy);
    BSplCLib::LocateParameter(3,knots,Us2,Standard_False,1,knots.Length(),I1,Us2);
    if (I1>=(knots.Length()-1))
      U5=Us2;
    else
      U5=knots(I1+1);
  }
  
  if (dercas==1)
    boucle++;
  
  if ((Polesinit(1).Y()<0.0)&&(Polesinit(2).Y()<0.0)){
    Us1=Polesinit(0).Y()/(Polesinit(0).Y()-Polesinit(1).Y());
    Us2=Polesinit(2).Y()/(Polesinit(2).Y()-Polesinit(3).Y());
    if (boucle!=0)
      if (Ux!=0.0){
	Us1=Us1*Ux;
	Us2=Uy+Us2*(1-Uy);
      }
    if (Us2<=Us1){                                   
      BSplCLib::LocateParameter(3,knots,Us1,Standard_False,1,knots.Length(),I1,Us1);
      if (knots(I1)>=Us2)                                    //insertion of one knot for the two poles
	U4=knots(I1);
      else{
	if (I1>=2){                                          //insertion to the left and
	  U4=knots(I1);                                      //to the right without a new knot
	  BSplCLib::LocateParameter(3,knots,Us2,Standard_False,1,knots.Length(),I3,Us2);
	  if (I3<(BS->NbKnots()-1)){
	    U5=knots(I3+1);
	    cas=1;
	  }
	}
	if(cas==0)                                          //insertion of only one new knot
	  U4=(Us1+Us2)/2;
      }
    }
    else{                                                      //insertion of two knots
      BSplCLib::LocateParameter(3,knots,Us1,Standard_False,1,knots.Length(),I1,Us1);
      if (I1>=2)                                                 
	U4=knots(I1);
      else
	U4=Us1;
      BSplCLib::LocateParameter(3,knots,Us2,Standard_False,1,knots.Length(),I3,Us2);
      if (I3<(BS->NbKnots()-1))
	U5=knots(I3+1);
      else
	U5=Us2;
    }
  }
} 

//=======================================================================
//function : PolyTest
//purpose  : give the knots U4 and U5 to insert to a(u)
//=======================================================================
   

static void PolyTest(const TColStd_Array1OfReal&        Herm,
		     const Handle(Geom2d_BSplineCurve)& BS,
		     Standard_Real&                     U4,
		     Standard_Real&                     U5,
		     Standard_Integer&                  boucle,
		     const Standard_Real                TolPoles,
//		     const Standard_Real                TolKnots,
		     const Standard_Real                ,
		     const Standard_Real                Ux, 
		     const Standard_Real                Uy)

{
  Standard_Integer               index,i,
                                 I1=0,I2=0,I3=0,I4=0;    //knots index
  TColgp_Array1OfPnt2d           Polesinit(0,3) ;
  Handle(TColStd_HArray1OfReal)  Knots;                  //array of the BSpline knots + the ones inserted
  Standard_Integer               cas=0,mark=0,dercas=0,  //loop marks
                                 min,max;                //Pole min and max indices
  Standard_Real                  Us1,Us2,a;              //bounderies value of the knots to be inserted
//  gp_Pnt2d                       P ;
  TCollection_CompareOfReal      Comp;
  
  U4=0.0;U5=1.0;                                         //default value
  if (Ux!=1.0){
    BS->LocateU(Ux,0.0,I1,I2);                          //localization of the inserted knots
    if (Uy!=0.0)
      BS->LocateU(Uy,0.0,I3,I4);
  }

  if (I1==I2)                                            //definition and filling of the 
    if((I3==I4)||(I3==0)){                               //array of knots
      Knots=new TColStd_HArray1OfReal(1,BS->NbKnots());
      for (i=1;i<=BS->NbKnots();i++)
	Knots->SetValue(i,BS->Knot(i));
    }
    else{
      Knots=new TColStd_HArray1OfReal(1,BS->NbKnots()+1);
      for (i=1;i<=BS->NbKnots();i++)
	Knots->SetValue(i,BS->Knot(i));
      Knots->SetValue(BS->NbKnots()+1,Uy);
    }
  else{
    if((I3==I4)||(I3==0)){
      Knots=new TColStd_HArray1OfReal(1,BS->NbKnots()+1);
      for (i=1;i<=BS->NbKnots();i++)
	Knots->SetValue(i,BS->Knot(i));
      Knots->SetValue(BS->NbKnots()+1,Ux);
    }
    else{
      Knots=new TColStd_HArray1OfReal(1,BS->NbKnots()+2);
      for (i=1;i<=BS->NbKnots();i++)
	Knots->SetValue(i,BS->Knot(i));
      Knots->SetValue(BS->NbKnots()+1,Ux);
      Knots->SetValue(BS->NbKnots()+2,Uy);
    }
  }

  TColStd_Array1OfReal knots(1,Knots->Length());
  knots=Knots->ChangeArray1();
  SortTools_QuickSortOfReal::Sort(knots,Comp);     //sort of the array of knots

  Polesinit(0).SetCoord(0.0,Herm(0));              //poles of the Hermite polynome in the BSpline form
  Polesinit(1).SetCoord(0.0,Herm(0)+Herm(1)/3.0);
  Polesinit(2).SetCoord(0.0,Herm(3)-Herm(2)/3.0);
  Polesinit(3).SetCoord(0.0,Herm(3));

                              //loop to check the tolerances on poles
  if (TolPoles!=0.0){
    Polemax(Polesinit,min,max);
    Standard_Real  Polemin=Polesinit(min).Y();
    Standard_Real  Polemax=Polesinit(max).Y();
    if (((Polemax)>=((1/TolPoles)*Polemin))||((Polemin==0.0)&&(Polemax>=(1/TolPoles)))){
      if (Polesinit(0).Y()>=(1/TolPoles)*Polesinit(3).Y()||Polesinit(0).Y()<=TolPoles*Polesinit(3).Y())
	Standard_DimensionError::Raise("Hermit Impossible Tolerance");       
      if ((max==0)||(max==3))                                                
	for (i=0;i<=3;i++)
	  Polesinit(i).SetCoord(0.0,(Polesinit(i).Y()-TolPoles*Polemax));
      if ((max==1)||(max==2)) {
	if ((min==0)||(min==3))                                             
	  for (i=0;i<=3;i++)
	    Polesinit(i).SetCoord(0.0,(Polesinit(i).Y()-(1/TolPoles)*Polemin));
	else{                                                                  
	  if ((TolPoles*Polemax<Polesinit(0).Y())&&(TolPoles*Polemax<Polesinit(3).Y())){
	    for (i=0;i<=3;i++)                                             
	      Polesinit(i).SetCoord(0.0,(Polesinit(i).Y()-TolPoles*Polemax));
	    mark=1;
	  }
	  if ((1/TolPoles*Polemin>Polesinit(0).Y())&&(1/TolPoles*Polemin>Polesinit(3).Y())&&(mark==0)){
	    for (i=0;i<=3;i++)                                             
	      Polesinit(i).SetCoord(0.0,(Polesinit(i).Y()-1/TolPoles*Polemin));
	    mark=1;
	  }
	  if (mark==0){
	    Standard_Real Pole0,Pole3;
	    Pole0=Polesinit(0).Y();
	    Pole3=Polesinit(3).Y();
	    if (Pole0<3){                         
	      a=Log10(Pole3/Pole0);                      
	      if (boucle==2)                      
		for (i=0;i<=3;i++)                                                    
		  Polesinit(i).SetCoord(0.0, Polesinit(i).Y()-(Pole3*(Pow(10.0,(-0.5*Log10(TolPoles)-a/2.0))))); 
	      if (boucle==1){
		for (i=0;i<=3;i++)                                                    
		  Polesinit(i).SetCoord(0.0, Polesinit(i).Y()-(Pole0*(Pow(10.0,(a/2.0+0.5*Log10(TolPoles)))))); 
		dercas=1;
	      }
	    }
	    if (Pole0>Pole3){                         
	      a=Log10(Pole0/Pole3);                           
	      if (boucle==2)                      
		for (i=0;i<=3;i++)                                                    
		  Polesinit(i).SetCoord(0.0, Polesinit(i).Y()-(Pole0*(Pow(10.0,(-0.5*Log10(TolPoles)-a/2.0))))); 
	      if (boucle==1){
		for (i=0;i<=3;i++)                                                    
		  Polesinit(i).SetCoord(0.0, Polesinit(i).Y()-(Pole3*(Pow(10.0,(a/2.0+0.5*Log10(TolPoles)))))); 
		dercas=1;
	      }
	    }
	  }
	}
      }
    }
  }                          //end of the loop
  
  if (!SignDenom(Polesinit))                       //invertion of the polynome sign
    for (index=0;index<=3;index++)
      Polesinit(index).SetCoord(0.0,-Polesinit(index).Y());
  
                                //boucle de positivite
  if ((Polesinit(1).Y()<0.0)&&(Polesinit(2).Y()>=0.0)){
    Us1=Polesinit(0).Y()/(Polesinit(0).Y()-Polesinit(1).Y());
    if (boucle==2)
      Us1=Us1*knots(2);
    if (boucle==1)
      if (Ux!=0.0)
	Us1=Us1*Ux;
    BSplCLib::LocateParameter(3,knots,Us1,Standard_False,1,knots.Length(),I1,Us1);
    if (I1<2)
      U4=Us1;
    else
      U4=knots(I1);
  }
  
  if ((Polesinit(1).Y()>=0.0)&&(Polesinit(2).Y()<0.0)){
    Us2=Polesinit(2).Y()/(Polesinit(2).Y()-Polesinit(3).Y());
    if (boucle==2)
      Us2=knots(knots.Length()-1)+Us2*(1-knots(knots.Length()-1));
    if (boucle==1)
      if (Ux!=0.0)
	Us2=Uy+Us2*(1-Uy);
    BSplCLib::LocateParameter(3,knots,Us2,Standard_False,1,knots.Length(),I1,Us2);
    if (I1>=(knots.Length()-1))
      U5=Us2;
    else
      U5=knots(I1+1);
  }
  
  if (dercas==1)
    boucle++;
  
  if ((Polesinit(1).Y()<0.0)&&(Polesinit(2).Y()<0.0)){
    Us1=Polesinit(0).Y()/(Polesinit(0).Y()-Polesinit(1).Y());
    Us2=Polesinit(2).Y()/(Polesinit(2).Y()-Polesinit(3).Y());
    if (boucle!=0)
      if (Ux!=0.0){
	Us1=Us1*Ux;
	Us2=Uy+Us2*(1-Uy);
      }
    if (Us2<=Us1){                                   
      BSplCLib::LocateParameter(3,knots,Us1,Standard_False,1,knots.Length(),I1,Us1);
      if (knots(I1)>=Us2)                                    //insertion of one knot for the two poles
	U4=knots(I1);
      else{
	if (I1>=2){                                          //insertion to the left and
	  U4=knots(I1);                                    //to the right without a new knot
	  BSplCLib::LocateParameter(3,knots,Us2,Standard_False,1,knots.Length(),I3,Us2);
	  if (I3<(BS->NbKnots()-1)){
	    U5=knots(I3+1);
	    cas=1;
	  }
	}
	if(cas==0)                                          //insertion of only one new knot
	  U4=(Us1+Us2)/2;
      }
    }
    else{                                                      //insertion of two knots
      BSplCLib::LocateParameter(3,knots,Us1,Standard_False,1,knots.Length(),I1,Us1);
      if (I1>=2)                                                 
	U4=knots(I1);
      else
	U4=Us1;
      BSplCLib::LocateParameter(3,knots,Us2,Standard_False,1,knots.Length(),I3,Us2);
      if (I3<(BS->NbKnots()-1))
	U5=knots(I3+1);
      else
	U5=Us2;
    }
  }
} 
                               
//=======================================================================
//function : InsertKnots
//purpose  : insert the knots in BS knot sequence if they are not null. 
//======================================================================= 
 
static void InsertKnots(Handle(Geom2d_BSplineCurve)& BS,
			const Standard_Real          U4,
			const Standard_Real          U5)

{
  if (U4!=0.0)                                  //insertion of :0 knot if U4=0
    BS->InsertKnot(U4);	                        //              1 knot if U4=U5 
  if ((U5!=1.0)&&(U5!=U4))                      //              2 knots otherwise
    BS->InsertKnot(U5);
  
}

//=======================================================================
//function : MovePoles
//purpose  : move the poles above the x axis
//======================================================================= 

static void MovePoles(Handle(Geom2d_BSplineCurve)& BS)

{
  gp_Pnt2d  P ;
//  Standard_Integer i,index; 
  Standard_Integer i; 

  for (i=3;i<=(BS->NbPoles()-2);i++){
    P.SetCoord(1,(BS->Pole(i).Coord(1)));           //raising of the no constrained poles to
    P.SetCoord(2,(BS->Pole(1).Coord(2)));           //the first pole level          
    BS->SetPole(i,P);
  }
}

//=======================================================================
//function : Solution
//purpose  :
//======================================================================= 

Handle(Geom2d_BSplineCurve) Hermit::Solution(const Handle(Geom_BSplineCurve)& BS,
					     const Standard_Real              TolPoles,
					     const Standard_Real              TolKnots)
     
{
  TColStd_Array1OfReal       Herm(0,3);
  Standard_Real              Upos1=0.0, Upos2=1.0,   //positivity knots
                             Ux=0.0,    Uy=1.0,
                             Utol1=0.0, Utol2=1.0,   //tolerance knots
                             Uint1=0.0, Uint2=1.0;   //tolerance knots for the first loop
  Standard_Integer           boucle=1;               //loop mark
  TColStd_Array1OfReal       Knots(1,2);             
  TColStd_Array1OfInteger    Multiplicities(1,2);
  TColgp_Array1OfPnt2d       Poles(1,4);
  Standard_Integer           zeroboucle = 0 ;
  HermiteCoeff(BS,Herm);                             //computation of the Hermite coefficient

  Poles(1).SetCoord(0.0,Herm(0));                    //poles of the Hermite polynome in the BSpline form
  Poles(2).SetCoord(0.0,Herm(0)+Herm(1)/3.0);
  Poles(3).SetCoord(0.0,Herm(3)-Herm(2)/3.0);
  Poles(4).SetCoord(0.0,Herm(3));
  Knots(1)=0.0;
  Knots(2)=1.0;
  Multiplicities(1)=4;
  Multiplicities(2)=4;

  Handle(Geom2d_BSplineCurve)  BS1=new Geom2d_BSplineCurve(Poles,Knots,Multiplicities,3);//creation of the basic
  Handle(Geom2d_BSplineCurve)  BS2=new Geom2d_BSplineCurve(Poles,Knots,Multiplicities,3);//BSpline without modif

  PolyTest(Herm,BS,Upos1,Upos2,zeroboucle,Precision::Confusion(),Precision::Confusion(),1.0,0.0);//computation of the positivity knots
  InsertKnots(BS2,Upos1,Upos2);                      //and insertion
    
  if (Upos1!=0.0)
    if (Upos2!=1.0){
      Ux=Min(Upos1,Upos2);
      Uy=Max(Upos1,Upos2);
    }
    else{
      Ux=Upos1;
      Uy=Upos1;
    }
  else{
    Ux=Upos2;
    Uy=Upos2;
  }

  Herm(0)=BS2->Pole(1).Y();                           //computation of the Hermite coefficient on the
  Herm(1)=3*(BS2->Pole(2).Y()-BS2->Pole(1).Y());      //positive BSpline
  Herm(2)=3*(BS2->Pole(BS2->NbPoles()).Y()-BS2->Pole(BS2->NbPoles()-1).Y());
  Herm(3)=BS2->Pole(BS2->NbPoles()).Y();
  
  PolyTest(Herm,BS,Utol1,Utol2,boucle,TolPoles,TolKnots,Ux,Uy);          //computation of the tolerance knots
  InsertKnots(BS2,Utol1,Utol2);                                          //and insertion
  
  if (boucle==2){                                     //insertion of two knots
    Herm(0)=BS2->Pole(1).Y();
    Herm(1)=3*(BS2->Pole(2).Y()-BS2->Pole(1).Y());
    Herm(2)=3*(BS2->Pole(BS2->NbPoles()).Y()-BS2->Pole(BS2->NbPoles()-1).Y());
    Herm(3)=BS2->Pole(BS2->NbPoles()).Y();
    if (Utol1==0.0){
      Uint2=Utol2;
      PolyTest(Herm,BS,Utol1,Utol2,boucle,TolPoles,TolKnots,Uint2,0.0);
    }
    else{
      Uint1=Utol1;
      PolyTest(Herm,BS,Utol1,Utol2,boucle,TolPoles,TolKnots,Uint1,0.0);
    }
    InsertKnots(BS2,Utol1,Utol2);
  }
  if ((BS2->Knot(2)<TolKnots)||(BS2->Knot(BS2->NbKnots()-1)>(1-TolKnots))) //checking of the knots tolerance
    Standard_DimensionError::Raise("Hermit Impossible Tolerance");
  else{
    if ((Upos2==1.0)&&(Utol2==1.0)&&(Uint2==1.0))    //test on the final inserted knots
      InsertKnots(BS1,BS2->Knot(2),1.0);
    else{
      if ((Upos1==0.0)&&(Utol1==0.0)&&(Uint1==0.0))
	InsertKnots(BS1,BS2->Knot(BS2->NbKnots()-1),1.0);
      else
	InsertKnots(BS1,BS2->Knot(BS2->NbKnots()-1),BS2->Knot(2));
    }
    MovePoles(BS1);                                  //relocation of the no-contrained knots
  }
  return BS1;
}


//=======================================================================
// Solution
//======================================================================= 

Handle(Geom2d_BSplineCurve) Hermit::Solution(const Handle(Geom2d_BSplineCurve)& BS,
					     const Standard_Real                TolPoles,
					     const Standard_Real                TolKnots)

{
  TColStd_Array1OfReal       Herm(0,3);
  Standard_Real              Upos1=0.0, Upos2=1.0,   //positivity knots
                             Ux=0.0,    Uy=1.0, 
                             Utol1=0.0, Utol2=1.0,   //tolerance knots
                             Uint1=0.0, Uint2=1.0;   //tolerance knots for the first loop
  Standard_Integer           boucle=1;               //loop mark
  TColStd_Array1OfReal       Knots(1,2);             
  TColStd_Array1OfInteger    Multiplicities(1,2);
  TColgp_Array1OfPnt2d       Poles(1,4);
  Standard_Integer           zeroboucle = 0 ;
  HermiteCoeff(BS,Herm);                             //computation of the Hermite coefficient

  Poles(1).SetCoord(0.0,Herm(0));                    //poles of the Hermite polynome in the BSpline form
  Poles(2).SetCoord(0.0,Herm(0)+Herm(1)/3.0);
  Poles(3).SetCoord(0.0,Herm(3)-Herm(2)/3.0);
  Poles(4).SetCoord(0.0,Herm(3));
  Knots(1)=0.0;
  Knots(2)=1.0;
  Multiplicities(1)=4;
  Multiplicities(2)=4;

  Handle(Geom2d_BSplineCurve)  BS1=new Geom2d_BSplineCurve(Poles,Knots,Multiplicities,3);//creation of the basic
  Handle(Geom2d_BSplineCurve)  BS2=new Geom2d_BSplineCurve(Poles,Knots,Multiplicities,3);//BSpline without modif

  PolyTest(Herm,BS,Upos1,Upos2,zeroboucle,Precision::Confusion(),Precision::Confusion(),1.0,0.0);//computation of the positivity knots
  InsertKnots(BS2,Upos1,Upos2);                      //and insertion
    
  if (Upos1!=0.0)
    if (Upos2!=1.0){
      Ux=Min(Upos1,Upos2);
      Uy=Max(Upos1,Upos2);
    }
    else{
      Ux=Upos1;
      Uy=Upos1;
    }
  else{
    Ux=Upos2;
    Uy=Upos2;
  }
  
  Herm(0)=BS2->Pole(1).Y();                           //computation of the Hermite coefficient on the
  Herm(1)=3*(BS2->Pole(2).Y()-BS2->Pole(1).Y());      //positive BSpline
  Herm(2)=3*(BS2->Pole(BS2->NbPoles()).Y()-BS2->Pole(BS2->NbPoles()-1).Y());
  Herm(3)=BS2->Pole(BS2->NbPoles()).Y();
  
  PolyTest(Herm,BS,Utol1,Utol2,boucle,TolPoles,TolKnots,Ux,Uy);          //computation of the tolerance knots
  InsertKnots(BS2,Utol1,Utol2);                                          //and insertion
  
  if (boucle==2){                                      //insertion of two knots
    Herm(0)=BS2->Pole(1).Y();
    Herm(1)=3*(BS2->Pole(2).Y()-BS2->Pole(1).Y());
    Herm(2)=3*(BS2->Pole(BS2->NbPoles()).Y()-BS2->Pole(BS2->NbPoles()-1).Y());
    Herm(3)=BS2->Pole(BS2->NbPoles()).Y();
    if (Utol1==0.0){
      Uint2=Utol2;
      PolyTest(Herm,BS,Utol1,Utol2,boucle,TolPoles,TolKnots,Uint2,0.0);
    }
    else{
      Uint1=Utol1;
      PolyTest(Herm,BS,Utol1,Utol2,boucle,TolPoles,TolKnots,Uint1,0.0);
    }
    InsertKnots(BS2,Utol1,Utol2);
  }
  if ((BS2->Knot(2)<TolKnots)||(BS2->Knot(BS2->NbKnots()-1)>(1-TolKnots))) //checking of the knots tolerance
    Standard_DimensionError::Raise("Hermit Impossible Tolerance");
  else{
    if ((Upos2==1.0)&&(Utol2==1.0)&&(Uint2==1.0))    //test on the final inserted knots
      InsertKnots(BS1,BS2->Knot(2),1.0);
    else{
      if ((Upos1==0.0)&&(Utol1==0.0)&&(Uint1==0.0))
	InsertKnots(BS1,BS2->Knot(BS2->NbKnots()-1),1.0);
      else
	InsertKnots(BS1,BS2->Knot(BS2->NbKnots()-1),BS2->Knot(2));
    }
    MovePoles(BS1);                          //relocation of the no-contrained knots
  }
  return BS1;
}

//=======================================================================
//function : Solutionbis
//purpose  :
//======================================================================= 

void Hermit::Solutionbis(const Handle(Geom_BSplineCurve)& BS,
			 Standard_Real &                  Knotmin,
			 Standard_Real &                  Knotmax,
			 const Standard_Real              TolPoles,
			 const Standard_Real              TolKnots)
     
{
  TColStd_Array1OfReal       Herm(0,3);
  Standard_Real              Upos1=0.0, Upos2=1.0,   //positivity knots
                             Ux=0.0,    Uy=1.0, 
                             Utol1=0.0, Utol2=1.0,   //tolerance knots
                             Uint1=0.0, Uint2=1.0;   //tolerance knots for the first loop
  Standard_Integer           boucle=1;               //loop mark
  TColStd_Array1OfReal       Knots(1,2);             
  TColStd_Array1OfInteger    Multiplicities(1,2);
  TColgp_Array1OfPnt2d       Poles(1,4);
  Standard_Integer           zeroboucle = 0 ;
  HermiteCoeff(BS,Herm);                             //computation of the Hermite coefficient
  
  Poles(1).SetCoord(0.0,Herm(0));                    //poles of the Hermite polynome in the BSpline form
  Poles(2).SetCoord(0.0,Herm(0)+Herm(1)/3.0);
  Poles(3).SetCoord(0.0,Herm(3)-Herm(2)/3.0);
  Poles(4).SetCoord(0.0,Herm(3));
  Knots(1)=0.0;
  Knots(2)=1.0;
  Multiplicities(1)=4;
  Multiplicities(2)=4;
  
  Handle(Geom2d_BSplineCurve)  BS2=new Geom2d_BSplineCurve(Poles,Knots,Multiplicities,3);//creation of the basic
                                                                                         //BSpline without modif
  
  PolyTest(Herm,BS,Upos1,Upos2,zeroboucle,Precision::Confusion(),Precision::Confusion(),1.0,0.0);//computation of the positivity knots
  InsertKnots(BS2,Upos1,Upos2);                      //and insertion
  
  if (Upos1!=0.0)
    if (Upos2!=1.0){
      Ux=Min(Upos1,Upos2);
      Uy=Max(Upos1,Upos2);
    }
    else{
      Ux=Upos1;
      Uy=Upos1;
    }
  else{
    Ux=Upos2;
    Uy=Upos2;
  }
  
  Herm(0)=BS2->Pole(1).Y();                           //computation of the Hermite coefficient on the
  Herm(1)=3*(BS2->Pole(2).Y()-BS2->Pole(1).Y());      //positive BSpline
  Herm(2)=3*(BS2->Pole(BS2->NbPoles()).Y()-BS2->Pole(BS2->NbPoles()-1).Y());
  Herm(3)=BS2->Pole(BS2->NbPoles()).Y();
  
  PolyTest(Herm,BS,Utol1,Utol2,boucle,TolPoles,TolKnots,Ux,Uy);          //computation of the tolerance knots
  InsertKnots(BS2,Utol1,Utol2);                                          //and insertion
  
  if (boucle==2){                                      //insertion of two knots
    Herm(0)=BS2->Pole(1).Y();
    Herm(1)=3*(BS2->Pole(2).Y()-BS2->Pole(1).Y());
    Herm(2)=3*(BS2->Pole(BS2->NbPoles()).Y()-BS2->Pole(BS2->NbPoles()-1).Y());
    Herm(3)=BS2->Pole(BS2->NbPoles()).Y();
    if (Utol1==0.0){
      Uint2=Utol2;
      PolyTest(Herm,BS,Utol1,Utol2,boucle,TolPoles,TolKnots,Uint2,0.0);
    }
    else{
      Uint1=Utol1;
      PolyTest(Herm,BS,Utol1,Utol2,boucle,TolPoles,TolKnots,Uint1,0.0);
    }
    InsertKnots(BS2,Utol1,Utol2);
  }
  if ((BS2->Knot(2)<TolKnots)||(BS2->Knot(BS2->NbKnots()-1)>(1-TolKnots))) //checking of the knots tolerance
    Standard_DimensionError::Raise("Hermit Impossible Tolerance");
  else{
    if ((Upos2==1.0)&&(Utol2==1.0)&&(Uint2==1.0))    //test on the final inserted knots
      Knotmin=BS2->Knot(2);
    else{
      if ((Upos1==0.0)&&(Utol1==0.0)&&(Uint1==0.0))
	Knotmax=BS2->Knot(BS2->NbKnots()-1);
      else{
	Knotmin=BS2->Knot(2);
	Knotmax=BS2->Knot(BS2->NbKnots()-1);
      }
    }  
  }
}