summaryrefslogtreecommitdiff
path: root/src/TopOpeBRepTool/TopOpeBRepTool_CurveTool.cxx
blob: ae3888810def39d1b14cf117087148970d79e399 (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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
// File:	TopOpeBRepTool_CurveTool.cxx
// Created:	Thu Jun 24 18:45:58 1993
// Author:	Jean Yves LEBEY
//		<jyl@zerox>

#include <TopOpeBRepTool_CurveTool.ixx>

#include <ProjLib_ProjectedCurve.hxx>
#include <BRepApprox_Approx.hxx>
#include <BRepApprox_ApproxLine.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <Geom_BSplineCurve.hxx>
#include <Geom2d_Line.hxx>
#include <Geom2d_Circle.hxx>
#include <Geom2d_Ellipse.hxx>
#include <Geom2d_Parabola.hxx>
#include <Geom2d_Hyperbola.hxx>
#include <Geom2d_BSplineCurve.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <GeomLib_CheckBSplineCurve.hxx>
#include <GeomLib_Check2dBSplineCurve.hxx>
#include <TColStd_Array1OfInteger.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <GeomAdaptor_Curve.hxx>
#include <BRepAdaptor_HSurface.hxx>
#include <GeomAdaptor_HCurve.hxx>
#include <GeomAbs_SurfaceType.hxx>
#include <gp_Pln.hxx>
#include <gp_Pnt2d.hxx>
#include <ElSLib.hxx>
#include <Standard_NotImplemented.hxx>
#include <Standard_ProgramError.hxx>
#include <OSD_Chronometer.hxx>

#include <GeomTools_Curve2dSet.hxx>
#include <TColStd_Array1OfBoolean.hxx>
#include <gp_Lin.hxx>
#include <gce_MakeLin.hxx>
#include <gp_Lin2d.hxx>
#include <gce_MakeLin2d.hxx>
#include <BRepTools.hxx>
#include <Precision.hxx>
#include <gce_MakeCirc.hxx>
#include <gp_Vec.hxx>
//#include <Approx.hxx>
#include <Geom_Surface.hxx>
#include <TopLoc_Location.hxx>
#include <Geom_RectangularTrimmedSurface.hxx>
#include <BRep_Tool.hxx>

Standard_IMPORT Standard_Boolean TopOpeBRepTool_GettracePCURV();
Standard_IMPORT Standard_Boolean TopOpeBRepTool_GettraceCHKBSPL();

#ifdef DEB
#include <TopOpeBRepTool_KRO.hxx>
TOPKRO KRO_CURVETOOL_APPRO("approximation");
Standard_IMPORT Standard_Boolean TopOpeBRepTool_GettraceKRO();
#endif
//#define DRAW
//#define IFV 
#define CurveImprovement
#ifdef DRAW
#include <DrawTrSurf.hxx>
static Standard_Integer NbCalls = 0;
#endif
//=======================================================================
//function : CurveTool
//purpose  : 
//=======================================================================

TopOpeBRepTool_CurveTool::TopOpeBRepTool_CurveTool()
{}

//=======================================================================
//function : CurveTool
//purpose  : 
//=======================================================================

TopOpeBRepTool_CurveTool::TopOpeBRepTool_CurveTool
(const TopOpeBRepTool_OutCurveType O)
{
  TopOpeBRepTool_GeomTool GT(O);
  SetGeomTool(GT);
}

//=======================================================================
//function : CurveTool
//purpose  : 
//=======================================================================

TopOpeBRepTool_CurveTool::TopOpeBRepTool_CurveTool
(const TopOpeBRepTool_GeomTool& GT)
{
  SetGeomTool(GT);
}

//=======================================================================
//function : ChangeGeomTool
//purpose  : 
//=======================================================================

TopOpeBRepTool_GeomTool& TopOpeBRepTool_CurveTool::ChangeGeomTool()
{
  return myGeomTool;
}

//=======================================================================
//function : GetGeomTool
//purpose  : 
//=======================================================================

const TopOpeBRepTool_GeomTool& TopOpeBRepTool_CurveTool::GetGeomTool()const
{
  return myGeomTool;
}

//=======================================================================
//function : SetGeomTool
//purpose  : 
//=======================================================================

void TopOpeBRepTool_CurveTool::SetGeomTool
(const TopOpeBRepTool_GeomTool& GT)
{
  myGeomTool.Define(GT);
}

//-----------------------------------------------------------------------
//function : MakePCurve
//purpose  : 
//-----------------------------------------------------------------------
Standard_EXPORT Handle(Geom2d_Curve) MakePCurve(const ProjLib_ProjectedCurve& PC)
{
  Handle(Geom2d_Curve) C2D;
  switch (PC.GetType()) {
  case GeomAbs_Line : C2D = new Geom2d_Line(PC.Line()); break;
  case GeomAbs_Circle : C2D = new Geom2d_Circle(PC.Circle()); break;
  case GeomAbs_Ellipse : C2D = new Geom2d_Ellipse(PC.Ellipse()); break;
  case GeomAbs_Parabola : C2D = new Geom2d_Parabola(PC.Parabola()); break;
  case GeomAbs_Hyperbola : C2D = new Geom2d_Hyperbola(PC.Hyperbola()); break;
  case GeomAbs_BSplineCurve : C2D = PC.BSpline(); break;
  case GeomAbs_BezierCurve : case GeomAbs_OtherCurve : default :
    Standard_NotImplemented::Raise("CurveTool::MakePCurve");
    break;
  }
  return C2D;
}

//------------------------------------------------------------------
static Standard_Boolean CheckApproxResults
  (const BRepApprox_Approx& Approx)
//------------------------------------------------------------------
{
  const AppParCurves_MultiBSpCurve& amc = Approx.Value(1); 
  Standard_Integer np = amc.NbPoles();
  Standard_Integer nc = amc.NbCurves();
  if (np < 2 || nc < 1) return Standard_False;

  // check the knots for coincidence
  const TColStd_Array1OfReal& knots = amc.Knots();
  for (Standard_Integer i = knots.Lower(); i < knots.Upper(); i++) {
    if (knots(i+1) - knots(i) <= Epsilon(knots(i))) {
      return Standard_False;
    }
  }
  return Standard_True;
} 

//------------------------------------------------------------------
static Standard_Boolean CheckPCurve
  (const Handle(Geom2d_Curve)& aPC, const TopoDS_Face& aFace)
//------------------------------------------------------------------
// check if points of the pcurve are out of the face bounds
{
  const Standard_Integer NPoints = 23;
  Standard_Real umin,umax,vmin,vmax;

  BRepTools::UVBounds(aFace, umin, umax, vmin, vmax);
  Standard_Real tolU = Max ((umax-umin)*0.01, Precision::Confusion());
  Standard_Real tolV = Max ((vmax-vmin)*0.01, Precision::Confusion());
  Standard_Real fp = aPC->FirstParameter();
  Standard_Real lp = aPC->LastParameter();
  Standard_Real step = (lp-fp)/(NPoints+1);

  // adjust domain for periodic surfaces
  TopLoc_Location aLoc;
  Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
  if (aSurf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface)))
    aSurf = (Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf))->BasisSurface();

  gp_Pnt2d pnt = aPC->Value((fp+lp)/2);
  Standard_Real u,v;
  pnt.Coord(u,v);

  if (aSurf->IsUPeriodic()) {
    Standard_Real aPer = aSurf->UPeriod();
    Standard_Integer nshift = (Standard_Integer) ((u-umin)/aPer);
    if (u < umin+aPer*nshift) nshift--;
    umin += aPer*nshift;
    umax += aPer*nshift;
  }
  if (aSurf->IsVPeriodic()) {
    Standard_Real aPer = aSurf->VPeriod();
    Standard_Integer nshift = (Standard_Integer) ((v-vmin)/aPer);
    if (v < vmin+aPer*nshift) nshift--;
    vmin += aPer*nshift;
    vmax += aPer*nshift;
  }

  Standard_Integer i;
  for (i=1; i <= NPoints; i++) {
    Standard_Real p = fp + i * step;
    pnt = aPC->Value(p);
    pnt.Coord(u,v);
    if (umin-u > tolU || u-umax > tolU ||
        vmin-v > tolV || v-vmax > tolV)
      return Standard_False;
  }
  return Standard_True;
} 

//------------------------------------------------------------------
static Handle(Geom_Curve) MakeCurve3DfromWLineApprox
(const BRepApprox_Approx& Approx,const Standard_Integer )
//------------------------------------------------------------------
{
  const AppParCurves_MultiBSpCurve& amc = Approx.Value(1); 
  Standard_Integer np = amc.NbPoles();
  //Standard_Integer nc = amc.NbCurves();
  TColgp_Array1OfPnt poles3d(1,np);
  Standard_Integer ic = 1;
  //for (ic=1; ic<=nc; ic++) {
     //if (ic == CI) {
      amc.Curve(ic,poles3d);
     //}
  //}

  const TColStd_Array1OfReal& knots = amc.Knots();
  const TColStd_Array1OfInteger& mults = amc.Multiplicities();
  Standard_Integer degree = amc.Degree();
  Handle(Geom_Curve) C3D = new Geom_BSplineCurve(poles3d,knots,mults,degree);
  return C3D;
} 

//------------------------------------------------------------------
static Handle(Geom2d_Curve) MakeCurve2DfromWLineApproxAndPlane
(const BRepApprox_Approx& Approx,const gp_Pln& Pl) 
//------------------------------------------------------------------
{ 
  const AppParCurves_MultiBSpCurve& amc = Approx.Value(1); 
  Standard_Integer np = amc.NbPoles();
  TColgp_Array1OfPnt2d poles2d(1,np);
  TColgp_Array1OfPnt poles3d(1,np);
  amc.Curve(1,poles3d);
  for(Standard_Integer i=1; i<=np; i++) { 
    Standard_Real U,V; ElSLib::Parameters(Pl,poles3d.Value(i),U,V);
    poles2d.SetValue(i,gp_Pnt2d(U,V));
  }
  const TColStd_Array1OfReal& knots = amc.Knots();
  const TColStd_Array1OfInteger& mults = amc.Multiplicities();
  Standard_Integer degree = amc.Degree();
  Handle(Geom2d_Curve) C2D = new Geom2d_BSplineCurve(poles2d,knots,mults,degree);
  return C2D;
}

//------------------------------------------------------------------
static Handle(Geom2d_Curve) MakeCurve2DfromWLineApprox
(const BRepApprox_Approx& Approx,const Standard_Integer CI)
//------------------------------------------------------------------
{
  const AppParCurves_MultiBSpCurve& amc = Approx.Value(1); 
  Standard_Integer np = amc.NbPoles();
  TColgp_Array1OfPnt2d poles2d(1,np);
  Standard_Integer nc = amc.NbCurves();
  for (Standard_Integer ic=1; ic<=nc; ic++) if (ic == CI) amc.Curve(ic,poles2d);
  const TColStd_Array1OfReal& knots = amc.Knots();
  const TColStd_Array1OfInteger& mults = amc.Multiplicities();
  Standard_Integer degree = amc.Degree();
  Handle(Geom2d_Curve) C2D = new Geom2d_BSplineCurve(poles2d,knots,mults,degree);
  return C2D;
}  


//=======================================================================
//function : MakeCurves
//purpose  : 
//=======================================================================

Standard_Boolean  TopOpeBRepTool_CurveTool::MakeCurves
(const Standard_Real parmin, const Standard_Real parmax,
 const Handle(Geom_Curve)& C3D, 
 const Handle(Geom2d_Curve)& PC1, const Handle(Geom2d_Curve)& PC2, 
 const TopoDS_Shape& S1, const TopoDS_Shape& S2, 
 Handle(Geom_Curve)& C3Dnew,
 Handle(Geom2d_Curve)& PC1new, Handle(Geom2d_Curve)& PC2new,
 Standard_Real& TolReached3d, Standard_Real& TolReached2d) const
{
  Standard_Boolean CompC3D = myGeomTool.CompC3D();

  //cout << "MakeCurves begin" << endl;
  if (!CompC3D) return Standard_False;

  Standard_Boolean CompPC1 = myGeomTool.CompPC1();
  Standard_Boolean CompPC2 = myGeomTool.CompPC2();
  Standard_Real tol3d,tol2d;
  Standard_Boolean RelativeTol;
  myGeomTool.GetTolerances(tol3d,tol2d,RelativeTol);
  Standard_Integer NbPntMax = myGeomTool.NbPntMax();

#ifdef DEB
  if (TopOpeBRepTool_GettraceKRO()) KRO_CURVETOOL_APPRO.Start();
#endif

//----------------------------------
///*
#ifdef IFV
  char name[16];
  char *nm = &name[0];
  sprintf(name,"C3D_%d",++NbCalls);
  DrawTrSurf::Set(nm, C3D);
  sprintf(name,"PC1_%d",NbCalls);
  DrawTrSurf::Set(nm, PC1);
  sprintf(name,"PC2_%d",NbCalls);
  DrawTrSurf::Set(nm, PC2);
#endif
//*/
//---------------------------------------------

  Standard_Integer iparmin = (Standard_Integer)parmin;
  Standard_Integer iparmax = (Standard_Integer)parmax;

  Handle(Geom_BSplineCurve)&   HC3D = *((Handle(Geom_BSplineCurve)*)&C3D);
  Handle(Geom2d_BSplineCurve)& HPC1 = *((Handle(Geom2d_BSplineCurve)*)&PC1);
  Handle(Geom2d_BSplineCurve)& HPC2 = *((Handle(Geom2d_BSplineCurve)*)&PC2);

//--------------------- IFV - "improving" initial curves
#ifdef CurveImprovement
  Standard_Integer nbpol = HC3D->NbPoles();
  //cout <<"nbpol = " << nbpol << endl;
  if(nbpol > 100) {
    TColgp_Array1OfPnt PolC3D(1, nbpol);
    TColgp_Array1OfPnt2d PolPC1(1, nbpol);
    TColgp_Array1OfPnt2d PolPC2(1, nbpol);
    TColStd_Array1OfBoolean IsValid(1, nbpol);
    IsValid.Init(Standard_True);
    Standard_Real tol = Max(1.e-10, 100.*tol3d*tol3d); //tol *= tol; - square distance
    Standard_Real tl2d = tol*(tol2d*tol2d)/(tol3d*tol3d);
    Standard_Real def = tol;
    Standard_Real def2d = tol2d;
    HC3D->Poles(PolC3D);
    if(CompPC1) HPC1->Poles(PolPC1);
    if(CompPC2) HPC2->Poles(PolPC2);
    
    Standard_Integer ip = 1, NbPol = 1;
    Standard_Real d, d1, d2;
    gp_Pnt P = PolC3D(ip);
    gp_Pnt2d P1, P2;
    if(CompPC1) P1 = PolPC1(ip);
    if(CompPC2) P2 = PolPC2(ip);

    for(ip = 2; ip <= nbpol; ip++) {
      if( IsValid(ip) ) {
	d = P.SquareDistance(PolC3D(ip));
	if(CompPC1) {d1 = P1.SquareDistance(PolPC1(ip));} else {d1 = 0.;}
	if(CompPC2) {d2 = P2.SquareDistance(PolPC2(ip));} else {d2 = 0.;}
	if(d > tol || d1 > tl2d || d2 > tl2d ) {
	  Standard_Real dd=0.;
	  if(ip < nbpol ) dd = P.Distance(PolC3D(ip+1));
	  if(ip < nbpol && dd < 10.*tol) {
	    gce_MakeLin mkL(P, PolC3D(ip+1));
	    if(mkL.IsDone()) {
	      gp_Lin L = mkL.Value();
	      d = L.SquareDistance(PolC3D(ip));
	      if(CompPC1) {
		gp_Lin2d L1 = gce_MakeLin2d(P1, PolPC1(ip+1));
		d1 = L1.SquareDistance(PolPC1(ip));
	      }
	      else d1 = 0.;
	      if(CompPC2) {
		gp_Lin2d L2 = gce_MakeLin2d(P2, PolPC2(ip+1));
		d2 = L2.SquareDistance(PolPC2(ip));
	      }
	      else d2 = 0.;
	    
	      if(d > def || d1 > def2d || d2 > def2d ) {
		NbPol++;
		P = PolC3D(ip);
		if(CompPC1) P1 = PolPC1(ip);
		if(CompPC2) P2 = PolPC2(ip);
	      }
	      else {
		IsValid(ip) = Standard_False;
	      }
	    }
	    else {
	      IsValid(ip+1) = Standard_False;
	      NbPol++;
	      P = PolC3D(ip);
	      if(CompPC1) P1 = PolPC1(ip);
	      if(CompPC2) P2 = PolPC2(ip);
	    }
	  }
	  else {
	    NbPol++;
	    P = PolC3D(ip);
	    if(CompPC1) P1 = PolPC1(ip);
	    if(CompPC2) P2 = PolPC2(ip);
	  }
	}
	else {
	  IsValid(ip) = Standard_False;
	}
      }
    }
 
    if(NbPol < 2) {IsValid(nbpol) = Standard_True; NbPol++;}
    
    if(NbPol < nbpol) {
#ifdef IFV
      cout << "NbPol < nbpol : " << NbPol << " " <<  nbpol << endl;
#endif
      TColgp_Array1OfPnt Polc3d(1, NbPol);
      TColgp_Array1OfPnt2d Polpc1(1, NbPol);
      TColgp_Array1OfPnt2d Polpc2(1, NbPol);
      TColStd_Array1OfReal knots(1,NbPol);
      TColStd_Array1OfInteger mults(1,NbPol);
      mults.Init(1); mults(1) = 2; mults(NbPol) = 2;
      Standard_Integer count = 0;
      for(ip = 1; ip <= nbpol; ip++) {
	if(IsValid(ip)) {
	  count++;
	  Polc3d(count) = PolC3D(ip);
	  if(CompPC1) Polpc1(count) = PolPC1(ip);
	  if(CompPC2) Polpc2(count) = PolPC2(ip);
	  knots(count) = count;
	}
      }

      Polc3d(NbPol) = PolC3D(nbpol);
      if(CompPC1) Polpc1(NbPol) = PolPC1(nbpol);
      if(CompPC2) Polpc2(NbPol) = PolPC2(nbpol);
      
      HC3D = new Geom_BSplineCurve(Polc3d, knots, mults, 1);
      if(CompPC1) HPC1 = new Geom2d_BSplineCurve(Polpc1, knots, mults, 1);
      if(CompPC2) HPC2 = new Geom2d_BSplineCurve(Polpc2, knots, mults, 1);
      iparmax = NbPol;

#ifdef IFV
      sprintf(name,"C3Dmod_%d",NbCalls);
      nm = &name[0];
      DrawTrSurf::Set(nm, HC3D);
      sprintf(name,"PC1mod_%d",NbCalls);
      nm = &name[0];
      DrawTrSurf::Set(nm, HPC1);
      sprintf(name,"PC2mod_%d",NbCalls);
      nm = &name[0];
      DrawTrSurf::Set(nm, HPC2);
#endif

    }
  }
//--------------- IFV - end "improving"
#endif


  BRepApprox_Approx Approx;

  Standard_Integer degmin = 4;
  Standard_Integer degmax = 8;
  Approx_ParametrizationType parametrization = Approx_ChordLength;

  Standard_Integer npol = HC3D->NbPoles();
  TColgp_Array1OfPnt Polc3d(1, npol);
  TColStd_Array1OfReal par(1, npol);
  HC3D->Poles(Polc3d);
  gp_Pnt P = Polc3d(1);

  Standard_Boolean IsBad = Standard_False;
  Standard_Integer ip;
  Standard_Real ksi = 0., kc, kcprev = 0.;
  Standard_Real dist;
  par(1) = 0.;
  for(ip = 2; ip <= npol; ip++) {
    dist = P.Distance(Polc3d(ip));

    if(dist < Precision::Confusion()) {
      IsBad = Standard_True;
      break;
    }

    par(ip) = par(ip-1) + dist; 
    P = Polc3d(ip);
  }

  if(!IsBad) {

    TColStd_Array1OfReal Curvature(1, npol);

    if(npol > 3) {
      Standard_Integer ic = 1;
      for(ip = 2; ip <= npol-1; ip += npol-3) {
	gp_Vec v1(Polc3d(ip-1),Polc3d(ip));
	gp_Vec v2(Polc3d(ip),Polc3d(ip+1));
	if(!v1.IsParallel(v2, 1.e-4)) {
	  gce_MakeCirc mc(Polc3d(ip-1),Polc3d(ip),Polc3d(ip+1));
	  gp_Circ cir = mc.Value();
	  kc = 1./cir.Radius();
	}
	else kc = 0.;
    
	Curvature(ic) = kc;
	ic = npol;
      }
    }
    else if(npol == 3) {
      ip = 2;
      gp_Vec v1(Polc3d(ip-1),Polc3d(ip));
      gp_Vec v2(Polc3d(ip),Polc3d(ip+1));
      if(!v1.IsParallel(v2, 1.e-4)) {
	gce_MakeCirc mc(Polc3d(ip-1),Polc3d(ip),Polc3d(ip+1));
	gp_Circ cir = mc.Value();
	kc = 1./cir.Radius();
      }
      else kc = 0.;
      Curvature(1) = Curvature(npol) = kc;
    }
    else {
      Curvature(1) = Curvature(npol) = 0.;
    }
    
    ip = 1;
    Standard_Real dt = par(ip+1) - par(ip);
    Standard_Real dx = (Polc3d(ip+1).X() - Polc3d(ip).X())/dt,
                  dy = (Polc3d(ip+1).Y() - Polc3d(ip).Y())/dt,
                  dz = (Polc3d(ip+1).Z() - Polc3d(ip).Z())/dt;
    Standard_Real dx1, dy1, dz1, d2x, d2y, d2z, d2t;

    for(ip = 2; ip <= npol-1; ip++) {
      dt = par(ip+1) - par(ip);
      dx1 = (Polc3d(ip+1).X() - Polc3d(ip).X())/dt;
      dy1 = (Polc3d(ip+1).Y() - Polc3d(ip).Y())/dt,
      dz1 = (Polc3d(ip+1).Z() - Polc3d(ip).Z())/dt;
      d2t = 2./(par(ip+1) - par(ip-1));
      d2x = d2t*(dx1 - dx);
      d2y = d2t*(dy1 - dy);
      d2z = d2t*(dz1 - dz);
      Curvature(ip) = Sqrt(d2x*d2x + d2y*d2y + d2z*d2z);
      dx = dx1; dy = dy1; dz = dz1;
    }

    Standard_Real crit = 1000.; // empirical criterion !!!

    dt = par(2) - par(1);
    kcprev = (Curvature(2) - Curvature(1))/dt; 
    for(ip = 2; ip <= npol-1; ip++) {
      dt = par(ip+1) - par(ip);
      kc = (Curvature(ip+1) - Curvature(ip))/dt;
      ksi = ksi + Abs(kc - kcprev);
      if(ksi > crit) {IsBad = Standard_True;break;}
      kc = kcprev; 
    }

  }
  //cout << NbCalls << " ksi = " << ksi << endl;
  //cout << "IsBad = " << IsBad << endl;

  if(IsBad){
    Standard_Real tt = Min(10.*tol3d,Precision::Approximation());
    tol2d = tt * tol2d / tol3d;
    tol3d = tt;
    NbPntMax = 40;
    degmax = 4;
    parametrization = Approx_Centripetal;
  }
 
  Standard_Integer nitmax = 0; // use projection only
  Standard_Boolean withtangency = Standard_True;
  
  Standard_Boolean compminmaxUV = Standard_True;
  BRepAdaptor_Surface BAS1(TopoDS::Face(S1),compminmaxUV);
  BRepAdaptor_Surface BAS2(TopoDS::Face(S2),compminmaxUV);


  Handle(BRepApprox_ApproxLine) AL;
  AL = new BRepApprox_ApproxLine(HC3D,HPC1,HPC2);

    Approx.SetParameters(tol3d,tol2d,RelativeTol,degmin,degmax,nitmax,NbPntMax,withtangency,
			 parametrization);

    if     (CompC3D && CompPC1 && BAS1.GetType() == GeomAbs_Plane) { 
      //-- The curve X,Y,Z and U2,V2 is approximated
      Approx.Perform(BAS1,BAS2,AL,CompC3D,Standard_False,CompPC2,iparmin,iparmax);
    }
    
    else if(CompC3D && CompPC2 && BAS2.GetType() == GeomAbs_Plane) {
      //-- The curve X,Y,Z and U1,V1 is approximated
      Approx.Perform(BAS1,BAS2,AL,CompC3D,CompPC1,Standard_False,iparmin,iparmax);
    }

    else { 
      Approx.Perform(BAS1,BAS2,AL,CompC3D,CompPC1,CompPC2,iparmin,iparmax);
    }

  // MSV Nov 9, 2001: do not raise exception in the case of failure,
  //                  but return status

  Standard_Boolean done = Approx.IsDone();
  done = done && CheckApproxResults(Approx);

  if (done) {
    if     (CompC3D && CompPC1 && BAS1.GetType() == GeomAbs_Plane) { 
      C3Dnew = ::MakeCurve3DfromWLineApprox(Approx,1);
      PC1new = ::MakeCurve2DfromWLineApproxAndPlane(Approx,BAS1.Plane());
      if (CompPC2) PC2new = ::MakeCurve2DfromWLineApprox(Approx,2);
    }
    else if(CompC3D && CompPC2 && BAS2.GetType() == GeomAbs_Plane) {
      C3Dnew = ::MakeCurve3DfromWLineApprox(Approx,1);
      if (CompPC1) PC1new = ::MakeCurve2DfromWLineApprox(Approx,2);
      PC2new = ::MakeCurve2DfromWLineApproxAndPlane(Approx,BAS2.Plane());
    }
    else { 
      if (CompC3D) C3Dnew = ::MakeCurve3DfromWLineApprox(Approx,1);
      if (CompPC1) PC1new = ::MakeCurve2DfromWLineApprox(Approx,2);
      if (CompPC2) {
        Standard_Integer i32 = (CompPC1) ? 3 : 2;
        PC2new = ::MakeCurve2DfromWLineApprox(Approx,i32);
      }
    }

    // check the pcurves relatively the faces bounds
    if (CompPC1)
      done = done && CheckPCurve(PC1new,TopoDS::Face(S1));
    if (CompPC2)
      done = done && CheckPCurve(PC2new,TopoDS::Face(S2));
  }

  if (!done) {
    if (CompC3D) C3Dnew.Nullify();
    if (CompPC1) PC1new.Nullify();
    if (CompPC2) PC2new.Nullify();
    return Standard_False;
  }

#ifdef IFV
    sprintf(name,"C3Dnew_%d", NbCalls);
    nm = &name[0];
    DrawTrSurf::Set(nm, C3Dnew);
    if (CompPC1) {
      sprintf(name,"PC1new_%d", NbCalls);
      nm = &name[0];
      DrawTrSurf::Set(nm, PC1new);
    }
    if (CompPC2) {
      sprintf(name,"PC2new_%d", NbCalls);
      nm = &name[0];
      DrawTrSurf::Set(nm, PC2new);
    }

#endif

  TolReached3d = Approx.TolReached3d();
  TolReached2d = Approx.TolReached2d();
#ifdef IFV
  cout << NbCalls << " : Tol = " << TolReached3d << " " << TolReached2d << endl;
#endif

  Standard_Boolean bf, bl;

  const Handle(Geom_BSplineCurve)& Curve = Handle(Geom_BSplineCurve)::DownCast(C3Dnew);
  if(!Curve.IsNull()) {
    GeomLib_CheckBSplineCurve cbsc(Curve, 1.e-7, 0.1);
    cbsc.NeedTangentFix(bf, bl);

#ifdef DEB
    if (TopOpeBRepTool_GettraceCHKBSPL()) {
      if(bf || bl) {
	cout<<"Problem orientation GeomLib_CheckBSplineCurve : First = "<<bf;
	cout<<" Last = "<<bl<<endl;
      }
    }
#endif
    cbsc.FixTangent(bf, bl);
  }
  
  const Handle(Geom2d_BSplineCurve)& Curve2df = Handle(Geom2d_BSplineCurve)::DownCast(PC1new);
  if(!Curve2df.IsNull()) {
    GeomLib_Check2dBSplineCurve cbsc2df(Curve2df, 1.e-7, 0.1);
    cbsc2df.NeedTangentFix(bf, bl);
#ifdef DEB
    if (TopOpeBRepTool_GettraceCHKBSPL()) {
      if(bf || bl) {
	cout<<"Problem orientation GeomLib_CheckBSplineCurve : First = "<<bf;
	cout<<" Last = "<<bl<<endl;
      }
    }
#endif
    cbsc2df.FixTangent(bf, bl);
  }
  
  const Handle(Geom2d_BSplineCurve)& Curve2ds = Handle(Geom2d_BSplineCurve)::DownCast(PC2new);
  if(!Curve2ds.IsNull()) {
    GeomLib_Check2dBSplineCurve cbsc2ds(Curve2ds, 1.e-7, 0.1);
    cbsc2ds.NeedTangentFix(bf, bl);
#ifdef DEB
    if (TopOpeBRepTool_GettraceCHKBSPL()) {
      if(bf || bl) {
	cout<<"Problem orientation GeomLib_CheckBSplineCurve : First = "<<bf;
	cout<<" Last = "<<bl<<endl;
      }
    }
#endif
    cbsc2ds.FixTangent(bf, bl);
  }
  
#ifdef DEB
  if (TopOpeBRepTool_GettraceKRO()) KRO_CURVETOOL_APPRO.Stop();
#endif
//  cout << "MakeCurves end" << endl;

  return Standard_True;
}


//=======================================================================
//function : MakeBSpline1fromPnt
//purpose  : 
//=======================================================================

Handle(Geom_Curve) TopOpeBRepTool_CurveTool::MakeBSpline1fromPnt
(const TColgp_Array1OfPnt& Points)
{
  Standard_Integer Degree = 1;
  
  Standard_Integer i,nbpoints = Points.Length();
  Standard_Integer nbknots = nbpoints - Degree +1;
  
  //  First compute the parameters
  //  Standard_Real length = 0.;
  //  TColStd_Array1OfReal parameters(1,nbpoints);
  //  for (i = 1; i < nbpoints; i++) {
  //    parameters(i) = length;
  //    Standard_Real dist = Points(i).Distance(Points(i+1));
  //    length += dist;
  //  }
  //  parameters(nbpoints) = length;
  
  // knots and multiplicities
  TColStd_Array1OfReal knots(1,nbknots);
  TColStd_Array1OfInteger mults(1,nbknots);
  mults.Init(1);
  mults(1) = mults(nbknots) = Degree + 1;
  
  //  knots(1) = 0;
  //  for (i=2;i<nbknots;i++) knots(i) = (parameters(i) + parameters(i+1)) /2.;
  //  knots(nbknots) = length;
  
  // take point index as parameter : JYL 01/AUG/94
  for (i = 1; i <= nbknots; i++) knots(i) = (Standard_Real)i;
  Handle(Geom_Curve) C = new Geom_BSplineCurve(Points,knots,mults,Degree);
  return C;
}


//=======================================================================
//function : MakeBSpline1fromPnt2d
//purpose  : 
//=======================================================================

Handle(Geom2d_Curve) TopOpeBRepTool_CurveTool::MakeBSpline1fromPnt2d
(const TColgp_Array1OfPnt2d& Points)
{
  Standard_Integer Degree = 1;
  
  Standard_Integer i,nbpoints = Points.Length();
  Standard_Integer nbknots = nbpoints - Degree +1;
  
  //  First compute the parameters
  //  Standard_Real length = 0;
  //  TColStd_Array1OfReal parameters(1,nbpoints);
  //  for (i = 1; i < nbpoints; i++) {
  //    parameters(i) = length;
  //    Standard_Real dist = Points(i).Distance(Points(i+1));
  //    length += dist;
  //  }
  //  parameters(nbpoints) = length;
  
  // knots and multiplicities
  TColStd_Array1OfReal knots(1,nbknots);
  TColStd_Array1OfInteger mults(1,nbknots);
  mults.Init(1);
  mults(1) = mults(nbknots) = Degree + 1;
  
  //  knots(1) = 0;
  //  for (i=2;i<nbknots;i++) knots(i) = (parameters(i) + parameters(i+1)) /2.;
  //  knots(nbknots) = length;
  
  // take point index as parameter : JYL 01/AUG/94
  for (i = 1; i <= nbknots; i++) knots(i) = (Standard_Real)i;
  Handle(Geom2d_Curve) C = new Geom2d_BSplineCurve(Points,knots,mults,Degree);
  return C;
}

//=======================================================================
//function : IsProjectable
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRepTool_CurveTool::IsProjectable
(const TopoDS_Shape& S, const Handle(Geom_Curve)& C3D)
{
  const TopoDS_Face& F = TopoDS::Face(S);
  Standard_Boolean compminmaxUV = Standard_False;
  BRepAdaptor_Surface BAS(F,compminmaxUV);
  GeomAbs_SurfaceType suty = BAS.GetType();
  GeomAdaptor_Curve GAC(C3D);
  GeomAbs_CurveType cuty = GAC.GetType();

  // --------
  // avoid projection of 3d curve on surface in case
  // of a quadric (ellipse,hyperbola,parabola) on a cone.
  // Projection fails when the curve in not fully inside the UV domain 
  // of the cone : only part of 2d curve is built.
  // NYI : projection of quadric on cone (crossing cone domain)
  // --------
  
  Standard_Boolean projectable = Standard_True;
  if ( suty == GeomAbs_Cone ) {
    if( (cuty == GeomAbs_Ellipse) || 
       ( cuty == GeomAbs_Hyperbola) || 
       ( cuty == GeomAbs_Parabola) ) {
      projectable = Standard_False;
    }
  }
  else if ( suty == GeomAbs_Cylinder ) {
    if (cuty == GeomAbs_Ellipse) {
      projectable = Standard_False;
    }
  }
  else if ( suty == GeomAbs_Sphere ) {
    if (cuty == GeomAbs_Circle) {
      projectable = Standard_False;
    }
  }
  else if ( suty == GeomAbs_Torus ) {
    if (cuty == GeomAbs_Circle) {
      projectable = Standard_False;
    }
  }
  
#ifdef DEB
  if (TopOpeBRepTool_GettracePCURV()) {
    cout<<"--- IsProjectable : "; 
    if (projectable) cout<<"projectable"<<endl;
    else cout<<"NOT projectable"<<endl;
  }
#endif
  
  return projectable;
}


//=======================================================================
//function : MakePCurveOnFace
//purpose  : 
//=======================================================================

Handle(Geom2d_Curve) TopOpeBRepTool_CurveTool::MakePCurveOnFace
(const TopoDS_Shape& S,
 const Handle(Geom_Curve)& C3D,
 Standard_Real& TolReached2d,
 const Standard_Real first, const Standard_Real last)
 
{
  Standard_Boolean trim = Standard_False;
  if (first < last)
    trim = Standard_True;

  const TopoDS_Face& F = TopoDS::Face(S);
  Standard_Boolean compminmaxUV = Standard_False;
  BRepAdaptor_Surface BAS(F,compminmaxUV);
  GeomAdaptor_Curve GAC;
  if (trim) GAC.Load(C3D,first,last);
  else      GAC.Load(C3D);
  Handle(BRepAdaptor_HSurface) BAHS = new BRepAdaptor_HSurface(BAS);
  Handle(GeomAdaptor_HCurve) BAHC = new GeomAdaptor_HCurve(GAC);
  ProjLib_ProjectedCurve projcurv(BAHS,BAHC);
  Handle(Geom2d_Curve) C2D = ::MakePCurve(projcurv);
  TolReached2d = projcurv.GetTolerance();

  Standard_Real UMin, UMax, VMin, VMax;
  BRepTools::UVBounds(F,UMin, UMax, VMin, VMax);

  Standard_Real f = GAC.FirstParameter();
  Standard_Real l = GAC.LastParameter();
  Standard_Real t =(f+l)*.5;
  gp_Pnt2d pC2D; C2D->D0(t,pC2D);
  Standard_Real u2 = pC2D.X();
  Standard_Real v2 = pC2D.Y();

  if (BAS.GetType() == GeomAbs_Sphere) {
    // MSV: consider quasiperiodic shift of pcurve
    Standard_Real VFirst = BAS.FirstVParameter();
    Standard_Real VLast = BAS.LastVParameter();
    Standard_Boolean mincond = v2 < VFirst;
    Standard_Boolean maxcond = v2 > VLast;
    if (mincond || maxcond) {
      Handle(Geom2d_Curve) PCT = Handle(Geom2d_Curve)::DownCast(C2D->Copy());
      // make mirror relative to the isoline of apex -PI/2 or PI/2
      gp_Trsf2d aTrsf;
      gp_Pnt2d po(0,-PI/2);
      if (maxcond) po.SetY(PI/2);
      aTrsf.SetMirror(gp_Ax2d(po, gp_Dir2d(1,0)));
      PCT->Transform(aTrsf);
      // add translation along U direction on PI
      gp_Vec2d vec(PI,0);
      Standard_Real UFirst = BAS.FirstUParameter();
      if (u2-UFirst-PI > -1e-7) vec.Reverse();
      PCT->Translate(vec);
      C2D = PCT;
      // recompute the test point
      C2D->D0(t,pC2D);
      u2 = pC2D.X();
      v2 = pC2D.Y();
    }
  }

  Standard_Real du = 0.;
  if (BAHS->IsUPeriodic()) {
    //modified by NIZHNY-MZV  Thu Mar 30 10:03:15 2000
    Standard_Boolean mincond = (UMin - u2 > 1e-7) ? Standard_True : Standard_False;
    Standard_Boolean maxcond = (u2 - UMax > 1e-7) ? Standard_True : Standard_False;
    Standard_Boolean decalu = mincond || maxcond;
    if (decalu) du = ( mincond ) ? BAHS->UPeriod() : -BAHS->UPeriod();
    //Standard_Boolean decalu = ( u2 < UMin || u2 > UMax);
    //if (decalu) du = ( u2 < UMin ) ? BAHS->UPeriod() : -BAHS->UPeriod();
  }
  Standard_Real dv = 0.;
  if (BAHS->IsVPeriodic()) {
    //modified by NIZHNY-MZV  Thu Mar 30 10:06:24 2000
    Standard_Boolean mincond = (VMin - v2 > 1e-7) ? Standard_True : Standard_False;
    Standard_Boolean maxcond = (v2 - VMax > 1e-7) ? Standard_True : Standard_False;
    Standard_Boolean decalv = mincond || maxcond;
    if (decalv) dv = ( mincond ) ? BAHS->VPeriod() : -BAHS->VPeriod();
    //Standard_Boolean decalv = ( v2 < VMin || v2 > VMax);
    //if (decalv) dv = ( v2 < VMin ) ? BAHS->VPeriod() : -BAHS->VPeriod();
  }
  
  if ( du != 0. || dv != 0.) {
    Handle(Geom2d_Curve) PCT = Handle(Geom2d_Curve)::DownCast(C2D->Copy());
    PCT->Translate(gp_Vec2d(du,dv));
    C2D = PCT;
  }

  return C2D;
}