summaryrefslogtreecommitdiff
path: root/src/BRepBlend/BRepBlend_SurfRstLineBuilder.cxx
blob: e7bd06b90e4caf4669a5c6af2d8e72b7b92bd301 (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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
// File:	BRepBlend_SurfRstLineBuilder.cxx
// Created:	Fri Jan 24 10:39:44 1997
// Author:	Jacques GOUSSARD
// Author:	Laurent BOURESCHE
//		<lbo@pomalox.paris1.matra-dtv.fr>

#include <stdio.h>

#include <BRepBlend_SurfRstLineBuilder.ixx>
#include <BRepBlend_BlendTool.hxx>
#include <TopAbs.hxx>
#include <IntSurf.hxx>
#include <math_FunctionSetRoot.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Pnt.hxx>
#include <gp_Vec2d.hxx>
#include <gp_Vec.hxx>

#ifdef DEB
#include <TColStd_Array1OfInteger.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColgp_Array1OfPnt2d.hxx>
#include <TColgp_Array1OfVec.hxx>
#include <TColgp_Array1OfVec2d.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <Geom_BSplineCurve.hxx>
#ifdef DRAW
#include <DrawTrSurf.hxx>
#endif
static Standard_Integer IndexOfSection = 0;
extern Standard_Boolean Blend_GettraceDRAWSECT(); 
// for debug : visualisation of the section
static Standard_Boolean BBPP(const Standard_Real param,
			     Blend_SurfRstFunction& Func,
			     const math_Vector& sol,
			     const Standard_Real tol,
			     Blend_Point& BP)
{
  if(!Func.IsSolution(sol,tol)) return 0;
  gp_Pnt pnts = Func.PointOnS();
  gp_Pnt pntrst = Func.PointOnRst();
  gp_Pnt2d p2ds = Func.Pnt2dOnS();
  gp_Pnt2d p2drst = Func.Pnt2dOnRst();
  Standard_Real w = Func.ParameterOnRst();
  BP = Blend_Point(pnts,pntrst,param,
		   p2ds.X(),p2ds.Y(),
		   p2drst.X(),p2drst.Y(),w);
  return 1;
}
static void tracederiv(Blend_SurfRstFunction& Func,
		       const Blend_Point& BP1,
		       const Blend_Point& BP2)
{
  Standard_Integer hp,hk,hd,hp2d,i;
  Func.GetShape(hp,hk,hd,hp2d);
  TColgp_Array1OfPnt TP1(1,hp);
  TColgp_Array1OfVec TDP1(1,hp);
  TColgp_Array1OfPnt2d TP2d1(1,hp2d);
  TColgp_Array1OfVec2d TDP2d1(1,hp2d);
  TColStd_Array1OfReal TW1(1,hp);
  TColStd_Array1OfReal TDW1(1,hp);
  Func.Section(BP1,TP1,TDP1,TP2d1,TDP2d1,TW1,TDW1);

  TColgp_Array1OfPnt TP2(1,hp);
  TColgp_Array1OfVec TDP2(1,hp);
  TColgp_Array1OfPnt2d TP2d2(1,hp2d);
  TColgp_Array1OfVec2d TDP2d2(1,hp2d);
  TColStd_Array1OfReal TW2(1,hp);
  TColStd_Array1OfReal TDW2(1,hp);
  Func.Section(BP2,TP2,TDP2,TP2d2,TDP2d2,TW2,TDW2);

  Standard_Real param1 = BP1.Parameter();
  Standard_Real param2 = BP2.Parameter();
  Standard_Real scal = 1./(param1-param2);

  cout<<endl;
  cout<<"control derivatives at point : "<<param1<<endl;

  for(i = 1; i <= hp; i++){
    cout<<endl;
    cout<<"point : "<<i<<endl;
    cout<<"dx calculated : "<<TDP1(i).X()<<endl;
    cout<<"dx estimated  : "<<scal*(TP1(i).X()-TP2(i).X())<<endl;
    cout<<"dy calculated : "<<TDP1(i).Y()<<endl;
    cout<<"dy estimated  : "<<scal*(TP1(i).Y()-TP2(i).Y())<<endl;
    cout<<"dz calculated : "<<TDP1(i).Z()<<endl;
    cout<<"dz estimated  : "<<scal*(TP1(i).Z()-TP2(i).Z())<<endl;
    cout<<"dw calculated : "<<TDW1(i)<<endl;
    cout<<"dw estimated  : "<<scal*(TW1(i)-TW2(i))<<endl;
  }
  for(i = 1; i <= hp2d; i++){
    cout<<endl;
    cout<<"point 2d : "<<i<<endl;
    cout<<"dx calculated : "<<TDP2d1(i).X()<<endl;
    cout<<"dx estimated  : "<<scal*(TP2d1(i).X()-TP2d2(i).X())<<endl;
    cout<<"dy calculated : "<<TDP2d1(i).Y()<<endl;
    cout<<"dy estimated  : "<<scal*(TP2d1(i).Y()-TP2d2(i).Y())<<endl;
  }
}

static void Drawsect(const Standard_Real param,
		     Blend_SurfRstFunction& Func)
{
  gp_Pnt pnts = Func.PointOnS();
  gp_Pnt pntrst = Func.PointOnRst();
  gp_Pnt2d p2ds = Func.Pnt2dOnS();
  gp_Pnt2d p2drst = Func.Pnt2dOnRst();
  Standard_Real w = Func.ParameterOnRst();
  Blend_Point BP(pnts,pntrst,param,
		 p2ds.X(),p2ds.Y(),
		 p2drst.X(),p2drst.Y(),w);
  Standard_Integer hp,hk,hd,hp2d;
  Func.GetShape(hp,hk,hd,hp2d);
  TColStd_Array1OfReal TK(1,hk);
  Func.Knots(TK);
  TColStd_Array1OfInteger TMul(1,hk);
  Func.Mults(TMul);
  TColgp_Array1OfPnt TP(1,hp);
  TColgp_Array1OfPnt2d TP2d(1,hp2d);
  TColStd_Array1OfReal TW(1,hp);
  Func.Section(BP,TP,TP2d,TW);
  Handle(Geom_BSplineCurve) sect = new Geom_BSplineCurve
    (TP,TW,TK,TMul,hd);
  IndexOfSection++;
#ifdef DRAW
  char tname[100];
  Standard_CString name = tname ;
  sprintf(name,"%s_%d","Section",IndexOfSection);
  DrawTrSurf::Set(name,sect);
#endif
}
#endif

//=======================================================================
//function :  ArcToRecadre
//purpose  : Find a suitable arc
//           PrevIndex is used to reject an already tested arc
//=======================================================================

Standard_Integer BRepBlend_SurfRstLineBuilder::
   ArcToRecadre(const math_Vector& sol,
		const Standard_Integer PrevIndex, 
		gp_Pnt2d& lastpt2d,
		gp_Pnt2d& pt2d,
		Standard_Real& ponarc) 
{
  Standard_Integer IndexSol = 0,  nbarc = 0;
  Standard_Boolean ok = Standard_False;
  Standard_Boolean byinter = (line->NbPoints() != 0), okinter = 0;
  Standard_Real distmin = RealLast();
  Standard_Real uprev = 0,vprev = 0, prm = 0.0 , dist = 0.0;

  if(byinter) previousP.ParametersOnS(uprev,vprev);
  pt2d.SetCoord(sol(1),sol(2));
  lastpt2d.SetCoord(uprev,vprev);
  domain1->Init();

  while (domain1->More()) {
    nbarc++; ok = 0;
    if(byinter) { 
      ok = okinter = BRepBlend_BlendTool::Inters(pt2d,lastpt2d,
						 surf1,
						 domain1->Value(),prm,dist); 
    }
    if(!ok) ok = BRepBlend_BlendTool::Project(pt2d,surf1,
					      domain1->Value(),prm,dist);

    if (ok && (nbarc != PrevIndex) ) {
      if (dist<distmin || okinter) {
	distmin = dist;
	ponarc = prm;
	IndexSol = nbarc;
	if(okinter && (PrevIndex==0) ) break;
      }
    }
    domain1->Next();
  }
  return IndexSol;
}

//=======================================================================
//function : BRepBlend_SurfRstLineBuilder
//purpose  : 
//=======================================================================

BRepBlend_SurfRstLineBuilder::BRepBlend_SurfRstLineBuilder
(const Handle(Adaptor3d_HSurface)&  Surf1,
 const Handle(Adaptor3d_TopolTool)& Domain1,
 const Handle(Adaptor3d_HSurface)&  Surf2,
 const Handle(Adaptor2d_HCurve2d)&  Rst,
 const Handle(Adaptor3d_TopolTool)& Domain2):
 sol(1,3),surf1(Surf1), domain1(Domain1),
 surf2(Surf2), rst(Rst), domain2(Domain2)
{
}

//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

void BRepBlend_SurfRstLineBuilder::Perform(Blend_SurfRstFunction&  Func,
					   Blend_FuncInv&          Finv,
					   Blend_SurfPointFuncInv& FinvP,
					   Blend_SurfCurvFuncInv&  FinvC,
					   const Standard_Real     Pdep,
					   const Standard_Real     Pmax,
					   const Standard_Real     MaxStep,
					   const Standard_Real     TolGuide,
					   const math_Vector&      ParDep,
					   const Standard_Real     Tolesp,
					   const Standard_Real     Fleche,
					   const Standard_Boolean  Appro) 
{
  done = Standard_False;
  iscomplete = Standard_False;
  comptra = Standard_False;
  line = new BRepBlend_Line();
  tolesp = Abs(Tolesp);
  tolgui = Abs(TolGuide);
  fleche = Abs(Fleche);
  rebrou = Standard_False;
  pasmax = Abs(MaxStep);
  
  if (Pmax-Pdep >= 0.) {
    sens = 1.;
  }
  else {
    sens = -1.;
  }
  
  Blend_Status State;
  
  param = Pdep;
  Func.Set(param);
  
  if (Appro) {
    TopAbs_State siturst,situs;
    Standard_Boolean decroch;
    math_Vector tolerance(1,3),infbound(1,3),supbound(1,3);
    Func.GetTolerance(tolerance,tolesp);
    Func.GetBounds(infbound,supbound);
    math_FunctionSetRoot rsnld(Func,tolerance,30);
    
    rsnld.Perform(Func,ParDep,infbound,supbound);
    
    if (!rsnld.IsDone()) {
      return;
    }
    rsnld.Root(sol);
    if (!CheckInside(Func,siturst,situs,decroch)) {
      return;
    }
  }
  else {
    sol = ParDep;
  }

  State = TestArret(Func,Standard_False,Blend_OK);
  if (State!=Blend_OK) {
    return;
  }
#ifdef DEB
  if (Blend_GettraceDRAWSECT()){
    Drawsect(param,Func);
  }
#endif
  // Update the line.
  line->Append(previousP);
  Standard_Real U,V;
  previousP.ParametersOnS(U,V);
//  W = previousP.ParameterOnC();
  
  BRepBlend_Extremity ptf1(previousP.PointOnS(),
			   U,V,previousP.Parameter(),tolesp);
  BRepBlend_Extremity ptf2(previousP.PointOnC(),
			   U,V,previousP.Parameter(),tolesp);
  if (!previousP.IsTangencyPoint()) {
    ptf1.SetTangent(previousP.TangentOnS());
    ptf2.SetTangent(previousP.TangentOnC());
  }
  if (sens>0.) {    
    line->SetStartPoints(ptf1, ptf2);    
  }
  else {
    line->SetEndPoints(ptf1, ptf2);
    
  }
  InternalPerform(Func,Finv,FinvP,FinvC,Pmax);
  done = Standard_True;
}

//=======================================================================
//function : PerformFirstSection
//purpose  : 
//=======================================================================

Standard_Boolean BRepBlend_SurfRstLineBuilder::PerformFirstSection
(Blend_SurfRstFunction&  Func,
 Blend_FuncInv&          Finv,
 Blend_SurfPointFuncInv& FinvP,
 Blend_SurfCurvFuncInv&  FinvC,
 const Standard_Real     Pdep,
 const Standard_Real     Pmax,
 const math_Vector&      ParDep,
 const Standard_Real     Tolesp,
 const Standard_Real     TolGuide,
 const Standard_Boolean  RecRst,
 const Standard_Boolean  RecP,
 const Standard_Boolean  RecS,
 Standard_Real&          Psol,   
 math_Vector&            ParSol)
{
  done = Standard_False;
  iscomplete = Standard_False;
  comptra = Standard_False;
  line = new BRepBlend_Line();
  tolesp = Abs(Tolesp);
  tolgui = Abs(TolGuide);
  rebrou = Standard_False;
  
  if (Pmax-Pdep >= 0.) {
    sens = 1.;
  }
  else {
    sens = -1.;
  }
#ifndef DEB
  Blend_Status State = Blend_OnRst12;
  Standard_Real trst = 0.;
#else
  Blend_Status State;
  Standard_Real trst;
#endif  
  Standard_Boolean recadp,recadrst,recads;
  Standard_Real wp,wrst,ws;
  Standard_Real U = 0,V = 0;
  math_Vector infbound(1,3),supbound(1,3),tolerance(1,3);
  math_Vector solinvp(1,3),solinvrst(1,4),solinvs(1,3);
  Handle(Adaptor3d_HVertex) Vtxp,Vtxrst,Vtxs,Vtxc;
  Standard_Boolean IsVtxp = 0,IsVtxrst = 0,IsVtxs = 0;
  Handle(Adaptor2d_HCurve2d) Arc;
  wp = wrst = ws = Pmax;
  param = Pdep;
  Func.Set(param);
  Func.GetTolerance(tolerance,tolesp);
  Func.GetBounds(infbound,supbound);

  math_FunctionSetRoot rsnld(Func,tolerance,30);
  rsnld.Perform(Func,ParDep,infbound,supbound);
  if (!rsnld.IsDone()) return Standard_False;
  rsnld.Root(sol);

  recads = RecS && Recadre(FinvC,solinvs,Arc,IsVtxs,Vtxs);
  if (recads) {
    ws = solinvs(1);
  }
  recadp = RecP && Recadre(FinvP,solinvp,IsVtxp,Vtxp);
  if (recadp) {
    wp = solinvp(1);
  }
  recadrst = RecRst && Recadre(Func,Finv,solinvrst,IsVtxrst,Vtxrst);
  if (recadrst) {
    wrst = solinvrst(2);
  }
  if (!recads && !recadp && !recadrst) return Standard_False;
  if (recadp && recadrst) {
    if(sens*(wrst-wp) > tolgui){ //first one leaves the domain
      wrst = wp;
      U = solinvp(2);
      V = solinvp(3);
      trst = BRepBlend_BlendTool::Parameter(Vtxp,rst);
      IsVtxrst = IsVtxp;
      Vtxrst = Vtxp;
    }
    else{                        
      U = solinvrst(3);
      V = solinvrst(4);
      trst = solinvrst(1);
    }
  }
  else if(recadp){
    wrst = wp;
    U = solinvp(2);
    V = solinvp(3);
    trst = BRepBlend_BlendTool::Parameter(Vtxp,rst);
    IsVtxrst = IsVtxp;
    Vtxrst = Vtxp;
    recadrst = Standard_True;
  }
  else if(recadrst){
    U = solinvrst(3);
    V = solinvrst(4);
    trst = solinvrst(1);
  }
  if(recads && recadrst){
    if(Abs(ws - wrst) < tolgui){
      State = Blend_OnRst12;
      param = 0.5*(ws+wrst);
      sol(1) = U;
      sol(2) = V;
      sol(3) = solinvs(2);
    }
    else if(sens*(ws-wrst)<0){
      // ground on surf
      State = Blend_OnRst1;
      param = ws;
      Arc->Value(solinvs(3)).Coord(U,V);
      sol(1) = U;
      sol(2) = V;
      sol(3) = solinvs(2);
    }
    else{
      // ground on rst
      State = Blend_OnRst2;
      param = wrst;
      sol(1) = U;
      sol(2) = V;
      sol(3) = trst;
    }
    Func.Set(param);
  }
  else if(recads){
    // ground on surf
    State = Blend_OnRst1;
    param = ws;
    Arc->Value(solinvs(3)).Coord(U,V);
    sol(1) = U;
    sol(2) = V;
    sol(3) = solinvs(2);
    Func.Set(param);
  }
  else if(recadrst){
    // ground on rst
    State = Blend_OnRst2;
    param = wrst;
    sol(1) = U;
    sol(2) = V;
    sol(3) = trst;
    Func.Set(param);
  }
  State = TestArret(Func,Standard_False,State);
  Psol = param;
  ParSol = sol;
  return Standard_True;
}  

//=======================================================================
//function : Complete
//purpose  : 
//=======================================================================

Standard_Boolean BRepBlend_SurfRstLineBuilder::Complete(Blend_SurfRstFunction&  Func,
							Blend_FuncInv&          Finv,
							Blend_SurfPointFuncInv& FinvP,
							Blend_SurfCurvFuncInv&  FinvC,
							const Standard_Real     Pmin) 
{
  if (!done) {StdFail_NotDone::Raise();}
  if (iscomplete) {return Standard_True;}
  if (sens >0.) {
    previousP = line->Point(1);
  }
  else {
    previousP = line->Point(line->NbPoints());
  }
  sens = -sens;
  param = previousP.Parameter();
  previousP.ParametersOnS(sol(1),sol(2));
  sol(3) = previousP.ParameterOnC();

  InternalPerform(Func,Finv,FinvP,FinvC,Pmin);
  iscomplete = Standard_True;
  return Standard_True;
}

//=======================================================================
//function : InternalPerform
//purpose  : 
//=======================================================================

void BRepBlend_SurfRstLineBuilder::InternalPerform(Blend_SurfRstFunction&  Func,
						   Blend_FuncInv&          Finv,
						   Blend_SurfPointFuncInv& FinvP,
						   Blend_SurfCurvFuncInv&  FinvC,
						   const Standard_Real     Bound) 
{
  Standard_Real stepw = pasmax;
  Standard_Integer nbp = line->NbPoints();
  if(nbp >= 2){ //The last step is reproduced if it is not too small.
    if(sens < 0.){
      stepw = (line->Point(2).Parameter() - line->Point(1).Parameter());
    }
    else{
      stepw = (line->Point(nbp).Parameter() - line->Point(nbp - 1).Parameter());
    }
    stepw = Max(stepw,100.*tolgui);
  }
  Standard_Real parprec = param;
  if (sens*(parprec - Bound) >= -tolgui) {
    return;
  }
#ifndef DEB
  Blend_Status State = Blend_OnRst12;
#else
  Blend_Status State;
#endif
  TopAbs_State situonc = TopAbs_UNKNOWN,situons = TopAbs_UNKNOWN;
  Standard_Boolean decroch = false;
  Standard_Boolean Arrive = false,recadp = false,recadrst = false,recads = false,echecrecad = false;
  Standard_Real wp,wrst,ws;
  Standard_Real U = 0,V = 0;
#ifndef DEB
  Standard_Real  trst = 0.;
#else
  Standard_Real  trst;
#endif
  math_Vector infbound(1,3),supbound(1,3);
  math_Vector parinit(1,3),tolerance(1,3);
  math_Vector solinvp(1,3),solinvrst(1,4),solinvs(1,3);
  Handle(Adaptor3d_HVertex) Vtxp,Vtxrst,Vtxs,Vtxc;
  Standard_Boolean IsVtxp = 0,IsVtxrst = 0,IsVtxs = 0;
  BRepBlend_Extremity Extrst,Exts;
  Handle(Adaptor2d_HCurve2d) Arc;

  //IntSurf_Transition Tline,Tarc;

  Func.GetTolerance(tolerance,tolesp);
  Func.GetBounds(infbound,supbound);

  math_FunctionSetRoot rsnld(Func,tolerance,30);
  parinit = sol;

  Arrive = Standard_False;
  param = parprec + sens*stepw;
  if(sens *(param - Bound) > 0.) {
    stepw = sens*(Bound - parprec)*0.5;
    param = parprec + sens*stepw;
  }

  while (!Arrive) {
    Standard_Boolean bonpoint = 1;
#if 0
    //debdebdebdebdebdeb
    Func.Set(param);
    rsnld.Perform(Func,parinit,infbound,supbound);
    if (rsnld.IsDone()) {
      rsnld.Root(sol);
      Blend_Point bp1;
      if(BBPP(param,Func,sol,tolesp,bp1)){
	Standard_Real dw = 1.e-10;
	Func.Set(param+dw);
	rsnld.Perform(Func,parinit,infbound,supbound);
	if (rsnld.IsDone()) {
	  rsnld.Root(sol);
	  Blend_Point bp2;
	  if(BBPP(param+dw,Func,sol,tolesp,bp2)){
	    tracederiv(Func,bp1,bp2);
	  }
	}
      }
    }
    //debdebdebdebdebdeb
#endif
    Func.Set(param);
    rsnld.Perform(Func,parinit,infbound,supbound);
    
    if (rsnld.IsDone()) {
      rsnld.Root(sol);
      if(!CheckInside(Func,situonc,situons,decroch) && line->NbPoints() == 1){
	State = Blend_StepTooLarge;
	bonpoint = 0;
      }
    }
    else {
      State = Blend_StepTooLarge;
      bonpoint = 0;
    }
    if(bonpoint){
      wp = wrst = ws = Bound;
      recadp = recadrst = recads = Standard_False;
      echecrecad = Standard_False;
      if (situons == TopAbs_OUT || situons == TopAbs_ON) {
	// pb inverse rst/rst
	recads = Recadre(FinvC,solinvs,Arc,IsVtxs,Vtxs);
	if (recads) {
	  ws = solinvs(1);
	  // It is necessary to reevaluate the deviation (BUC60360)
	  gp_Vec t, n;
	  Func.Set(ws);
	  Arc->Value(solinvs(3)).Coord(U,V);
	  sol(1) = U;
	  sol(2) = V;
	  sol(3) = solinvs(2);
	  decroch = Func.Decroch(sol, t, n); 
	}
	else {
	  echecrecad = Standard_True;
	}
      }
      if (situonc == TopAbs_OUT || situonc == TopAbs_ON) {
	// pb inverse point/surf
	recadp = Recadre(FinvP,solinvp,IsVtxp,Vtxp);
	if (recadp) {
	  wp = solinvp(1);
	}
	else {
	  echecrecad = Standard_True;
	}
      }
      if (decroch) {
	// pb inverse rst/surf
	recadrst = Recadre(Func,Finv,solinvrst,IsVtxrst,Vtxrst);
	if (recadrst) {
	  wrst = solinvrst(2);
	}
	else {
	  echecrecad = Standard_True;
	}
      }
      decroch = 0;
      if(recadp || recads || recadrst) echecrecad = Standard_False; 
      if (!echecrecad) {
	if (recadp && recadrst) {
	  if(sens*(wrst-wp) > tolgui){ //first one leaves the domain
	    wrst = wp;
	    U = solinvp(2);
	    V = solinvp(3);
	    trst = BRepBlend_BlendTool::Parameter(Vtxp,rst);
	    IsVtxrst = IsVtxp;
	    Vtxrst = Vtxp;
	  }
	  else{                        
	    decroch = 1;
	    U = solinvrst(3);
	    V = solinvrst(4);
	    trst = solinvrst(1);
	  }
	}
	else if(recadp){
	  wrst = wp;
	  U = solinvp(2);
	  V = solinvp(3);
	  trst = BRepBlend_BlendTool::Parameter(Vtxp,rst);
	  IsVtxrst = IsVtxp;
	  Vtxrst = Vtxp;
	  recadrst = Standard_True;
	}
	else if(recadrst){
	  decroch = 1;
	  U = solinvrst(3);
	  V = solinvrst(4);
	  trst = solinvrst(1);
	}
	if(recads && recadrst){
	  if(Abs(ws - wrst) < tolgui){
	    State = Blend_OnRst12;
	    param = 0.5*(ws+wrst);
	    sol(1) = U;
	    sol(2) = V;
	    sol(3) = solinvs(3);
	  }
	  else if(sens*(ws-wrst)<0){
	    // ground on surf
	    decroch = 0;
	    State = Blend_OnRst1;
	    param = ws;
	    Arc->Value(solinvs(3)).Coord(U,V);
	    sol(1) = U;
	    sol(2) = V;
	    sol(3) = solinvs(2);
	  }
	  else{
	    // ground on rst
	    State = Blend_OnRst2;
	    param = wrst;
	    sol(1) = U;
	    sol(2) = V;
	    sol(3) = trst;
	  }
	  Func.Set(param);
	}
	else if(recads){
	  // ground on surf
	  State = Blend_OnRst1;
	  param = ws;
	  Arc->Value(solinvs(3)).Coord(U,V);
	  sol(1) = U;
	  sol(2) = V;
	  sol(3) = solinvs(2);
	  Func.Set(param);
	}
	else if(recadrst){
	  // ground on rst
	  State = Blend_OnRst2;
	  param = wrst;
	  sol(1) = U;
	  sol(2) = V;
	  sol(3) = trst;
	  Func.Set(param);
	}
	else {
	  State = Blend_OK;
	}
	State = TestArret(Func,Standard_True,State);
      }
      else{
	// Failed reframing. Leave with PointsConfondus
#if DEB
	cout<<"SurfRstLineBuilder : failed reframing"<<endl;
#endif
	State = Blend_SamePoints;
      }
    }
    
    switch (State) {
    case Blend_OK :
      {
#ifdef DEB
	if (Blend_GettraceDRAWSECT()){
	  Drawsect(param,Func);
	}
#endif
	// Update the line.
	if (sens>0.) {
	  line->Append(previousP);
	}
	else {
	  line->Prepend(previousP);
	}
	parinit = sol;
	parprec = param;
	
	if (param == Bound) {
	  Arrive = Standard_True;
	  Exts.SetValue(previousP.PointOnS(),
			sol(1),sol(2),
			previousP.Parameter(),tolesp);
	  MakeExtremity(Extrst,Standard_False,rst,sol(3),IsVtxrst,Vtxrst);
	  // Indicate end on Bound.
	}
	else {
	  param = param + sens*stepw;
	  if (sens*(param - Bound) > - tolgui) {
	    param = Bound;
	  }
	}
      }
      break;
      
    case Blend_StepTooLarge :
      {
	stepw = stepw/2.;
	if (Abs(stepw) < tolgui) {
	  previousP.ParametersOnS(U,V);
	  Exts.SetValue(previousP.PointOnS(),U,V,
			previousP.Parameter(),tolesp);
	  Extrst.SetValue(previousP.PointOnC(),
			  previousP.ParameterOnC(),
			  previousP.Parameter(),tolesp);
	  Arrive = Standard_True;
	  if (line->NbPoints()>=2) {
	    // Indicate that one stops during the processing
#if DEB
	    cout<<"SurfRstLineBuilder : No advancement in the processing"<<endl;
#endif
	  }
	}
	else {
	  param = parprec + sens*stepw;  // no risk to exceed Bound.
	}
      }
      break;
      
    case Blend_StepTooSmall :
      {
#ifdef DEB
	if (Blend_GettraceDRAWSECT()){
	  Drawsect(param,Func);
	}
#endif
	// Update the line.
	if (sens>0.) {
	  line->Append(previousP);
	}
	else {
	  line->Prepend(previousP);
	}
	parinit = sol;
	parprec = param;
	
	stepw = Min(1.5*stepw,pasmax);
	if (param == Bound) {
	  Arrive = Standard_True;
	  Exts.SetValue(previousP.PointOnS(),sol(1),sol(2),
			previousP.Parameter(),tolesp);
	  MakeExtremity(Extrst,Standard_False,rst,sol(3),IsVtxrst,Vtxrst);
	  // Indicate end on Bound.
	}
	else {
	  param = param + sens*stepw;
	  if (sens*(param - Bound) > - tolgui) {
	    param = Bound;
	  }
	}
      }
      break;
      
    case Blend_OnRst1  :
      {
#ifdef DEB
	if (Blend_GettraceDRAWSECT()){
	  Drawsect(param,Func);
	}
#endif
	if (sens>0.) {
	  line->Append(previousP);
	}
	else {
	  line->Prepend(previousP);
	}
	MakeExtremity(Exts,Standard_True,Arc,solinvs(3),IsVtxs,Vtxs);
	MakeExtremity(Extrst,Standard_False,rst,sol(3),IsVtxrst,Vtxrst);
	Arrive = Standard_True;
      }
      break;
      
    case Blend_OnRst2  :
      {
#ifdef DEB
	if (Blend_GettraceDRAWSECT()){
	  Drawsect(param,Func);
	}
#endif
	if (sens>0.) {
	  line->Append(previousP);
	}
	else {
	  line->Prepend(previousP);
	}
	Exts.SetValue(previousP.PointOnS(),sol(1),sol(2),
		      previousP.Parameter(),tolesp);
	MakeExtremity(Extrst,Standard_False,rst,sol(3),IsVtxrst,Vtxrst);
	Arrive = Standard_True;
      }
      break;
      
    case Blend_OnRst12  :
      {
#ifdef DEB
	if (Blend_GettraceDRAWSECT()){
	  Drawsect(param,Func);
	}
#endif
	if (sens>0.) {
	  line->Append(previousP);
	}
	else {
	  line->Prepend(previousP);
	}
	MakeExtremity(Exts,Standard_True,Arc,solinvs(1),IsVtxs,Vtxs);
	MakeExtremity(Extrst,Standard_False,rst,sol(3),IsVtxrst,Vtxrst);
	Arrive = Standard_True;
      }
      break;
      
    case Blend_SamePoints :
      {
	// Stop
#if DEB
	cout << "SurfRstLineBuilder Points mixed in the processing" << endl;
#endif
	previousP.ParametersOnS(U,V);
	Exts.SetValue(previousP.PointOnS(),U,V,
		      previousP.Parameter(),tolesp);
	Extrst.SetValue(previousP.PointOnC(),
			previousP.ParameterOnC(),
			previousP.Parameter(),tolesp);
	Arrive = Standard_True;
      }
      break;
#ifndef DEB
    default:
      break;
#endif
    }
    if (Arrive) {
      if (sens > 0.) {
	line->SetEndPoints(Exts,Extrst);
	decrochfin = decroch;
      }
      else {
	line->SetStartPoints(Exts,Extrst);
	decrochdeb = decroch;
      }
    }
  }
}

//=======================================================================
//function : Recadre
//purpose  : Reframe section Surface / Restriction
//=======================================================================

Standard_Boolean BRepBlend_SurfRstLineBuilder::Recadre(Blend_SurfCurvFuncInv&    FinvC,
						       math_Vector&              Solinv,
						       Handle(Adaptor2d_HCurve2d)& Arc,
						       Standard_Boolean&         IsVtx,
						       Handle(Adaptor3d_HVertex)&  Vtx) 
{
  Standard_Boolean recadre = Standard_False;
  gp_Pnt2d pt2d, lastpt2d;
  Standard_Integer IndexSol, nbarc;
  Standard_Real pmin;

  IndexSol = ArcToRecadre(sol, 0, lastpt2d, pt2d, pmin);
 
  IsVtx = Standard_False;
  if (IndexSol == 0) {
    return Standard_False;
  }

  domain1->Init();
  nbarc = 1;
  while (nbarc < IndexSol) {
    nbarc++;
    domain1->Next();
  }
  Arc = domain1->Value();

  FinvC.Set(Arc);
  
  math_Vector toler(1,3),infb(1,3),supb(1,3);
  FinvC.GetTolerance(toler,tolesp);
  FinvC.GetBounds(infb,supb);
  Solinv(1) = param;
  Solinv(2) = sol(3);
  Solinv(3) = pmin;
  
  math_FunctionSetRoot rsnld(FinvC,toler,30);
  rsnld.Perform(FinvC,Solinv,infb,supb);

  if (!rsnld.IsDone()) {
#if DEB
    cout << "SurfRstLineBuilder : RSNLD not done "<< endl << endl;
#endif
  }
  else {
      // It is necessary to check the value of the function
    rsnld.Root(Solinv);
    recadre = FinvC.IsSolution(Solinv,tolesp);
  }

  // In case of fail, it is checked if another arc 
  // can be useful (case of output at the proximity of a vertex)
  if (!recadre) {

    IndexSol =  ArcToRecadre(sol, IndexSol, 
			     lastpt2d, pt2d, pmin);
    if (IndexSol == 0) {
      return Standard_False; // No other solution
    }

    domain1->Init();
    nbarc = 1;
    while (nbarc < IndexSol) {
      nbarc++;
      domain1->Next();
    }

    Arc = domain1->Value();
    FinvC.Set(Arc);
  
    FinvC.GetTolerance(toler,tolesp);
    FinvC.GetBounds(infb,supb);

    Solinv(3) = pmin;
  
    math_FunctionSetRoot rsnld(FinvC,toler,30);
    rsnld.Perform(FinvC,Solinv,infb,supb);

    if (!rsnld.IsDone()) {
#if DEB
      cout << "SurfRstLineBuilder : RSNLD not done "<< endl << endl;
#endif
    }
    else {
      // It is necessary to check the value of the function
      rsnld.Root(Solinv);
      recadre = FinvC.IsSolution(Solinv,tolesp);
    }
  }  

  if (recadre) {
    Standard_Real w = Solinv(2);
    if(w < rst->FirstParameter() - toler(2)||
       w > rst->LastParameter() + toler(2)){
      return Standard_False;
    }
    domain1->Initialize(Arc);
    domain1->InitVertexIterator();
    IsVtx = !domain1->MoreVertex();
    while (!IsVtx) {
      Vtx = domain1->Vertex();
      if (Abs(BRepBlend_BlendTool::Parameter(Vtx,Arc)-Solinv(3)) <=
	  BRepBlend_BlendTool::Tolerance(Vtx,Arc)) {
	IsVtx = Standard_True;
      }
      else {
	domain1->NextVertex();
	IsVtx = !domain1->MoreVertex();
      }
    }
    if (!domain1->MoreVertex()) {
      IsVtx = Standard_False;
    }
    return Standard_True;
  } 
  return Standard_False;
}

//=======================================================================
//function : Recadre
//purpose  : 
//=======================================================================

Standard_Boolean BRepBlend_SurfRstLineBuilder::Recadre(Blend_SurfRstFunction&   Func,
						       Blend_FuncInv&           Finv,
						       math_Vector&             Solinv,
						       Standard_Boolean&        IsVtx,
						       Handle(Adaptor3d_HVertex)& Vtx) 
{
  math_Vector toler(1,4),infb(1,4),supb(1,4);
  Finv.GetTolerance(toler,tolesp);
  Finv.GetBounds(infb,supb);
  Solinv(1) = sol(3);
  Solinv(2) = param;
  Solinv(3) = sol(1);
  Solinv(4) = sol(2);

  math_FunctionSetRoot rsnld(Finv,toler,30);
  rsnld.Perform(Finv,Solinv,infb,supb);
  if (!rsnld.IsDone()) {
#if DEB
    cout << "SurfRstLineBuilder :RSNLD not done "<< endl;
#endif
    return Standard_False;
  }
  rsnld.Root(Solinv);
  
  if(Finv.IsSolution(Solinv,tolesp)){
    gp_Pnt2d p2d(Solinv(3),Solinv(4));
    TopAbs_State situ = domain1->Classify(p2d,Min(toler(3),toler(4)),0);
    if ((situ != TopAbs_IN) && (situ != TopAbs_ON)) {
      return Standard_False;
    }
    domain2->Initialize(rst);
    domain2->InitVertexIterator();
    IsVtx = !domain2->MoreVertex();
    while (!IsVtx) {
      Vtx = domain2->Vertex();
      if (Abs(BRepBlend_BlendTool::Parameter(Vtx,rst)-Solinv(1)) <=
	  BRepBlend_BlendTool::Tolerance(Vtx,rst)) {
	IsVtx = Standard_True;
      }
      else {
	domain2->NextVertex();
	IsVtx = !domain2->MoreVertex();
      }
    }
    if (!domain2->MoreVertex()) {
      IsVtx = Standard_False;
    }
    // The section is recalculated by direct resolution, otherwise 
    // incoherences between the parameter and the ground caused by yawn are returned.

    math_Vector infbound(1,3),supbound(1,3);
    math_Vector parinit(1,3),tolerance(1,3);
    Func.GetTolerance(tolerance,tolesp);
    Func.GetBounds(infbound,supbound);

    math_FunctionSetRoot rsnld2(Func,tolerance,30);
    parinit(1) = Solinv(3);
    parinit(2) = Solinv(4);
    parinit(3) = Solinv(1);
    Func.Set(Solinv(2));
    rsnld2.Perform(Func,parinit,infbound,supbound);
    if(!rsnld2.IsDone()) return Standard_False;
    rsnld2.Root(parinit);
    Solinv(3) = parinit(1);
    Solinv(4) = parinit(2);
    Solinv(1) = parinit(3);
    return Standard_True;
  }
  return Standard_False;
}

//=======================================================================
//function : Recadre
//purpose  : 
//=======================================================================

Standard_Boolean BRepBlend_SurfRstLineBuilder::Recadre(Blend_SurfPointFuncInv&  FinvP,
						       math_Vector&             Solinv,
						       Standard_Boolean&        IsVtx,
						       Handle(Adaptor3d_HVertex)& Vtx) 
{
  // Le point.
  gp_Pnt2d p2drst;
  Standard_Real firstrst = rst->FirstParameter();
  Standard_Real lastrst = rst->LastParameter();
  Standard_Real wpoint = firstrst;
  if((sol(3) - firstrst) > (lastrst - sol(3))) wpoint = lastrst;
  p2drst = rst->Value(wpoint);
  gp_Pnt thepoint = surf2->Value(p2drst.X(),p2drst.Y());

  FinvP.Set(thepoint);
  math_Vector toler(1,3),infb(1,3),supb(1,3);
  FinvP.GetTolerance(toler,tolesp);
  FinvP.GetBounds(infb,supb);
  Solinv(1) = param;
  Solinv(2) = sol(1);
  Solinv(3) = sol(2);

  math_FunctionSetRoot rsnld(FinvP,toler,30);
  rsnld.Perform(FinvP,Solinv,infb,supb);
  if (!rsnld.IsDone()) {
#if DEB
    cout << "SurfRstLineBuilder :RSNLD not done "<< endl;
#endif
    return Standard_False;
  }
  rsnld.Root(Solinv);
  
  if(FinvP.IsSolution(Solinv,tolesp)){
    gp_Pnt2d p2d(Solinv(2),Solinv(3));
    TopAbs_State situ = domain1->Classify(p2d,Min(toler(2),toler(3)),0);
    if ((situ != TopAbs_IN) && (situ != TopAbs_ON)) {
      return Standard_False;
    }
    domain2->Initialize(rst);
    domain2->InitVertexIterator();
    IsVtx = !domain2->MoreVertex();
    while (!IsVtx) {
      Vtx = domain2->Vertex();
      if (Abs(BRepBlend_BlendTool::Parameter(Vtx,rst)-wpoint) <=
	  BRepBlend_BlendTool::Tolerance(Vtx,rst)) {
	IsVtx = Standard_True;
      }
      else {
	domain2->NextVertex();
	IsVtx = !domain2->MoreVertex();
      }
    }
    if (!domain2->MoreVertex()) {
      IsVtx = Standard_False;
    }
    return Standard_True;
  }
  return Standard_False;
}

//=======================================================================
//function : Transition
//purpose  : 
//=======================================================================

void BRepBlend_SurfRstLineBuilder::Transition(const Standard_Boolean          OnFirst,
					      const Handle(Adaptor2d_HCurve2d)& Arc,
					      const Standard_Real             Param,
					      IntSurf_Transition&             TLine,
					      IntSurf_Transition&             TArc) 
{
  Standard_Boolean computetranstionaveclacorde = 0;
  gp_Vec tgline;
  Blend_Point prevprev;

  if(previousP.IsTangencyPoint()){
    if(line->NbPoints() < 2) return;
    computetranstionaveclacorde = 1;
    if(sens < 0){
      prevprev = line->Point(2);
    }
    else {
      prevprev = line->Point(line->NbPoints() - 1);
    }
  }
  gp_Pnt2d p2d;
  gp_Vec2d dp2d;
  
  gp_Pnt pbid;
  gp_Vec d1u,d1v,normale,tgrst;
  
  Arc->D1(Param,p2d,dp2d);
  if (OnFirst) {
    surf1->D1(p2d.X(),p2d.Y(),pbid,d1u,d1v);
    if(!computetranstionaveclacorde) tgline = previousP.TangentOnS1();
    else tgline = gp_Vec(prevprev.PointOnS(),previousP.PointOnS());
  }
  else {
    surf2->D1(p2d.X(),p2d.Y(),pbid,d1u,d1v);
    if(!computetranstionaveclacorde) tgline = previousP.TangentOnS2();
    else tgline = gp_Vec(prevprev.PointOnC(),previousP.PointOnC());
  }
  
  tgrst.SetLinearForm(dp2d.X(),d1u,dp2d.Y(),d1v);
  normale = d1u.Crossed(d1v);
  
  IntSurf::MakeTransition(tgline,tgrst,normale,TLine,TArc);
}

//=======================================================================
//function : MakeExtremity
//purpose  : 
//=======================================================================

void BRepBlend_SurfRstLineBuilder::MakeExtremity(BRepBlend_Extremity&            Extrem,
						 const Standard_Boolean          OnFirst,
						 const Handle(Adaptor2d_HCurve2d)& Arc,
						 const Standard_Real             Param,
						 const Standard_Boolean          IsVtx,
						 const Handle(Adaptor3d_HVertex)&  Vtx) 
{
  IntSurf_Transition Tline,Tarc;
  Standard_Real prm;
  Handle(Adaptor3d_TopolTool) Iter;
  if (OnFirst) {
    Extrem.SetValue(previousP.PointOnS(),
		    sol(1),sol(2),
		    previousP.Parameter(),tolesp);
    if (!previousP.IsTangencyPoint()) 
      Extrem.SetTangent(previousP.TangentOnS());
    Iter = domain1;
  }
  else {
    Extrem.SetValue(previousP.PointOnC(),
		    sol(3),
		    previousP.Parameter(),tolesp);
    if (!previousP.IsTangencyPoint()) 
      Extrem.SetTangent(previousP.TangentOnC());    
    Iter = domain2;
  }
  
  Iter->Init();
  if (!IsVtx) {
    Transition(OnFirst,Arc,Param,Tline,Tarc);
    Extrem.AddArc(Arc,Param,Tline,Tarc);
  }
  else {
    Extrem.SetVertex(Vtx);
    while (Iter->More()) {
//#ifndef DEB
      Handle(Adaptor2d_HCurve2d) arc = Iter->Value();
//#else
//      Handle(Adaptor2d_HCurve2d)& arc = Iter->Value();
//#endif
      if (arc != Arc) {
	Iter->Initialize(arc);
	Iter->InitVertexIterator();
	while (Iter->MoreVertex()) {
	  if (Iter->Identical(Vtx,Iter->Vertex())) {
	    prm = BRepBlend_BlendTool::Parameter(Vtx,arc);
	    Transition(OnFirst,arc,prm,Tline,Tarc);
	    Extrem.AddArc(arc,prm,Tline,Tarc);
	  }
	  Iter->NextVertex();
	}
      }
      else {
	Transition(OnFirst,arc,Param,Tline,Tarc);
	Extrem.AddArc(arc,Param,Tline,Tarc);
      }
      Iter->Next();
    }
  }
}

//=======================================================================
//function : CheckDeflectionOnSurf
//purpose  : 
//=======================================================================

Blend_Status BRepBlend_SurfRstLineBuilder::CheckDeflectionOnSurf(const Blend_Point& CurPoint)
{
  //Controls 3d of Blend_CSWalking.

  // rule by tests in U4 corresponds to 11.478 d
  const Standard_Real CosRef3D = 0.98;
  Standard_Real Cosi=0, Cosi2=0;
  Standard_Boolean curpointistangent = CurPoint.IsTangencyPoint();
  Standard_Boolean prevpointistangent = previousP.IsTangencyPoint();

  gp_Pnt Psurf = CurPoint.PointOnS();
  gp_Vec Tgsurf;
  if(!curpointistangent){
    Tgsurf = CurPoint.TangentOnS();
  }
  gp_Pnt prevP = previousP.PointOnS();
  gp_Vec prevTg;
  if(!prevpointistangent){
    prevTg = previousP.TangentOnS();
  }
#ifndef DEB
  Standard_Real Norme,prevNorme = 0.;
#else
  Standard_Real Norme,prevNorme;
#endif
  gp_Vec Corde(prevP,Psurf);
  Norme = Corde.SquareMagnitude();
//  if(!curpointistangent) curNorme = Tgsurf.SquareMagnitude();
  if(!prevpointistangent) prevNorme = prevTg.SquareMagnitude();

  if (Norme <= tolesp*tolesp){
    // it can be necessary to force same point
    return Blend_SamePoints;
  }
  if(!prevpointistangent){
    if(prevNorme <= tolesp*tolesp) {
      return Blend_SamePoints;
    }
    Cosi = sens*Corde*prevTg;
    if (Cosi <0.) { // angle 3d>pi/2. --> return back
      return Blend_Backward;
    }
    
    Cosi2 = Cosi * Cosi / prevNorme / Norme;
    if (Cosi2 < CosRef3D) { 
      return Blend_StepTooLarge;
    }
  }
  
  if(!curpointistangent){
    // Check if it is necessary to control the sign of prevtg*Tgsurf
    Cosi = sens*Corde*Tgsurf;
    Cosi2 = Cosi * Cosi / Tgsurf.SquareMagnitude() / Norme;
    if (Cosi2 < CosRef3D || Cosi < 0.) { 
      return Blend_StepTooLarge;
    }
  }  

  if(!curpointistangent && !prevpointistangent){
    // Estimation of the current arrow
    Standard_Real FlecheCourante = 
      (prevTg.Normalized().XYZ()-Tgsurf.Normalized().XYZ()).SquareModulus()*Norme/64.;
    
    if (FlecheCourante <= 0.25*fleche*fleche) {
      return Blend_StepTooSmall;
    }
    if (FlecheCourante > fleche*fleche) {
      // not too great : 
      return Blend_StepTooLarge;
    }
  }
  return Blend_OK;
}


//=======================================================================
//function : CheckDeflectionOnRst
//purpose  : 
//=======================================================================

Blend_Status BRepBlend_SurfRstLineBuilder::CheckDeflectionOnRst(const Blend_Point& CurPoint)
{
  //Controls 3D of Blend_CSWalking.

  // rule by tests in U4 corresponds to 11.478 d
  const Standard_Real CosRef3D = 0.98;
  Standard_Real Cosi, Cosi2;
  Standard_Boolean curpointistangent = CurPoint.IsTangencyPoint();
  Standard_Boolean prevpointistangent = previousP.IsTangencyPoint();

  gp_Pnt Psurf = CurPoint.PointOnC();
  gp_Vec Tgsurf;
  if(!curpointistangent){
    Tgsurf = CurPoint.TangentOnC();
  }
  gp_Pnt prevP = previousP.PointOnC();
  gp_Vec prevTg;
  if(!prevpointistangent){
    prevTg = previousP.TangentOnC();
  }
#ifndef DEB
  Standard_Real Norme,prevNorme = 0.;
#else
  Standard_Real Norme,prevNorme;
#endif
  gp_Vec Corde(prevP,Psurf);
  Norme = Corde.SquareMagnitude();
//  if(!curpointistangent) curNorme = Tgsurf.SquareMagnitude();
  if(!prevpointistangent) prevNorme = prevTg.SquareMagnitude();

  if (Norme <= tolesp*tolesp){
    // it can be necessary to force same point
    return Blend_SamePoints;
  }
  if(!prevpointistangent){
    if(prevNorme <= tolesp*tolesp) {
      return Blend_SamePoints;
    }
    Cosi = sens*Corde*prevTg;
    if (Cosi <0.) { // angle 3d>pi/2. --> return back
      return Blend_Backward;
    }
    
    Cosi2 = Cosi * Cosi / prevNorme / Norme;
    if (Cosi2 < CosRef3D) { 
      return Blend_StepTooLarge;
    }
  }
  
  if(!curpointistangent){
    // Check if it is necessary to control the sign of prevtg*Tgsurf
    Cosi = sens*Corde*Tgsurf;
    Cosi2 = Cosi * Cosi / Tgsurf.SquareMagnitude() / Norme;
    if (Cosi2 < CosRef3D || Cosi < 0.) { 
      return Blend_StepTooLarge;
    }
  }  

  if(!curpointistangent && !prevpointistangent){
    // Estimation of the current arrow
    Standard_Real FlecheCourante = 
      (prevTg.Normalized().XYZ()-Tgsurf.Normalized().XYZ()).SquareModulus()*Norme/64.;
    
    if (FlecheCourante <= 0.25*fleche*fleche) {
      return Blend_StepTooSmall;
    }
    if (FlecheCourante > fleche*fleche) {
      // not too great
      return Blend_StepTooLarge;
    }
  }
  return Blend_OK;
}

static IntSurf_TypeTrans ConvOrToTra(const TopAbs_Orientation O)
{
  if(O == TopAbs_FORWARD) return IntSurf_In;
  return IntSurf_Out;
}

//=======================================================================
//function : TestArret
//purpose  : 
//=======================================================================

Blend_Status BRepBlend_SurfRstLineBuilder::TestArret(Blend_SurfRstFunction& Func,
						     const Standard_Boolean TestDeflection,
						     const Blend_Status     State) 
{
  gp_Pnt pts,ptrst;
  gp_Pnt2d pt2drst;
  gp_Vec tgs,tgrst;
  gp_Vec2d tg2ds,tg2drst;
  Blend_Status StateS,StateRst;
#ifndef DEB
  IntSurf_TypeTrans tras = IntSurf_Undecided, trarst = IntSurf_Undecided;
#else
  IntSurf_TypeTrans tras,trarst;
#endif
  Blend_Point curpoint;

  if (Func.IsSolution(sol,tolesp)) {
    Standard_Boolean curpointistangent = Func.IsTangencyPoint();
    pts = Func.PointOnS();
    ptrst = Func.PointOnRst();
    pt2drst = Func.Pnt2dOnRst();
    if(curpointistangent){
      curpoint.SetValue(pts,ptrst,param,sol(1),sol(2),
			pt2drst.X(),pt2drst.Y(),sol(3));
    }
    else{
      tgs     = Func.TangentOnS();
      tgrst   = Func.TangentOnRst();
      tg2ds   = Func.Tangent2dOnS();
      tg2drst = Func.Tangent2dOnRst();

      curpoint.SetValue(pts,ptrst,param,sol(1),sol(2),
			pt2drst.X(),pt2drst.Y(),sol(3),
			tgs,tgrst,tg2ds,tg2drst);
    }
    if (TestDeflection) {
      StateS = CheckDeflectionOnSurf(curpoint);
      StateRst = CheckDeflectionOnRst(curpoint);
    }
    else {
      StateS = StateRst = Blend_OK;
    }
    if (StateS == Blend_Backward) {
      StateS = Blend_StepTooLarge;
      rebrou= Standard_True;
    }
    if (StateRst == Blend_Backward) {
      StateRst = Blend_StepTooLarge;
      rebrou = Standard_True;
    }
    if (StateS == Blend_StepTooLarge ||
	StateRst == Blend_StepTooLarge) {
      return Blend_StepTooLarge;
    }

    if (!comptra && !curpointistangent) {
      gp_Vec tgsecs,nors;
      Func.Decroch(sol,nors,tgsecs);
      nors.Normalize();
      Standard_Real testra = tgsecs.Dot(nors.Crossed(tgs));
      if (Abs(testra) > tolesp) {
	if (testra < 0.) {
	  tras = IntSurf_In;
	}
	else if (testra >0.) {
	  tras = IntSurf_Out;
	}
	gp_Pnt2d p2drstref; gp_Vec2d tg2drstref;
	rst->D1(sol(3),p2drstref,tg2drstref);
	testra = tg2drst.Dot(tg2drstref);
	TopAbs_Orientation Or = domain2->Orientation(rst);
	if (Abs(testra) > 1.e-8) {
	  if (testra < 0.) {
	    trarst = ConvOrToTra(TopAbs::Reverse(Or));
	  }
	  else if (testra >0.) {
	    trarst = ConvOrToTra(Or);
	  }
	  comptra = Standard_True;
	  line->Set(tras,trarst);
	}
      }
    }
    if (StateS == Blend_OK ||
	StateRst == Blend_OK ) {
      previousP = curpoint;
      return State;
    }
    if (StateS == Blend_StepTooSmall &&
	StateRst == Blend_StepTooSmall) {
      previousP = curpoint;
      if (State == Blend_OK) {
	return Blend_StepTooSmall;
      }
      else {
	return State;
      }
    }
    if (State == Blend_OK) {
      return Blend_SamePoints;
    }
    else {
      return State;
    }
  }
  return Blend_StepTooLarge;
}

//=======================================================================
//function : CheckInside
//purpose  : 
//=======================================================================

Standard_Boolean BRepBlend_SurfRstLineBuilder::CheckInside(Blend_SurfRstFunction& Func,
							   TopAbs_State&          SituOnC,
							   TopAbs_State&          SituOnS,
							   Standard_Boolean&      Decroch)
{
  math_Vector tolerance(1,3);
  Func.GetTolerance(tolerance,tolesp);
  //face pcurve.
  Standard_Real w = sol(3);
  if(w < rst->FirstParameter() - tolerance(3)||
     w > rst->LastParameter() + tolerance(3)){
    SituOnC = TopAbs_OUT;
  }
  else if (w > rst->FirstParameter() &&
	   w < rst->LastParameter()){
    SituOnC = TopAbs_IN;
  }
  else SituOnC = TopAbs_ON;

  //face surface
  gp_Pnt2d p2d(sol(1),sol(2));
  SituOnS = domain1->Classify(p2d,Min(tolerance(1),tolerance(2)),0);

  //lost contact
  gp_Vec tgs,nors;
  Decroch = Func.Decroch(sol,tgs,nors);

  return (SituOnC == TopAbs_IN && SituOnS == TopAbs_IN && !Decroch);
}