summaryrefslogtreecommitdiff
path: root/src/MAT2d/MAT2d_Tool2d.cxx
blob: 7d7167ba53c9c990a481643a92c660bfb9522a43 (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
// File:	MAT2d_Tool2d.cxx
// Created:	Mon Jul 12 18:10:23 1993
// Author:	Yves FRICAUD
//		<yfr@phylox>


#define Debug(expr)  cout<<" MAT2d_Tool2d.cxx  :  expr :"<<expr<<endl;

#ifdef DRAW
#include <DBRep.hxx>
#include <DrawTrSurf.hxx>
#include <stdio.h>
#endif

#ifdef DRAW
#include <Draw_Appli.hxx>
#include <DrawTrSurf_Curve2d.hxx>
#include <GCE2d_MakeSegment.hxx>
#include <DrawTrSurf.hxx>
#endif

#include <MAT2d_Tool2d.ixx>
#include <MAT2d_MiniPath.hxx>
#include <MAT2d_Connexion.hxx>
#include <MAT2d_SequenceOfSequenceOfGeometry.hxx>
#include <MAT_Edge.hxx>
#include <Bisector_Curve.hxx>
#include <Bisector_BisecAna.hxx>
#include <Bisector_BisecCC.hxx>
#include <Bisector_Bisec.hxx>
#include <Bisector_Inter.hxx>
#include <IntRes2d_Domain.hxx>
#include <Extrema_ExtPC2d.hxx>
#include <Geom2dInt_GInter.hxx>
#include <IntRes2d_IntersectionPoint.hxx>
#include <IntRes2d_IntersectionSegment.hxx>
#include <Geom2d_Geometry.hxx>
#include <Geom2d_Point.hxx>
#include <Geom2d_Line.hxx>
#include <Geom2d_Circle.hxx>
#include <Geom2d_Curve.hxx>
#include <Geom2d_Parabola.hxx>
#include <Geom2d_Hyperbola.hxx>
#include <Geom2d_Ellipse.hxx>
#include <Geom2d_CartesianPoint.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <Geom2dAdaptor_Curve.hxx>
#include <gp_Lin2d.hxx>
#include <gp_Hypr2d.hxx>
#include <gp_Parab2d.hxx>
#include <gp_Elips2d.hxx>
#include <gp_Circ2d.hxx>
#include <gp_Vec2d.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <ElCLib.hxx>
#include <StdFail_NotDone.hxx>
#include <Standard_NotImplemented.hxx>
#include <Precision.hxx>

#ifdef DRAW
  static Handle(DrawTrSurf_Curve2d) draw;
#endif
#ifdef DEB
  static void MAT2d_DrawCurve(const Handle(Geom2d_Curve)& aCurve,
			      const Standard_Integer      Indice);
  static Standard_Boolean Store = Standard_False;
#endif

//=====================================================================
//  static functions 
//=====================================================================
static IntRes2d_Domain Domain
  (const Handle(Geom2d_TrimmedCurve)& Bisector1,
   const Standard_Real                Tolerance);

static Handle(Standard_Type) Type (const Handle(Geom2d_Geometry)& acurve);

static Standard_Boolean AreNeighbours(const Standard_Integer IEdge1,
				      const Standard_Integer IEdge2,
				      const Standard_Integer NbEdge);

static void SetTrim(Bisector_Bisec&  Bis , Handle(Geom2d_Curve)& Line1);

static Standard_Real MAT2d_TOLCONF = 1.e-7;

//============================================================================
//function : 
//purpose  :
//============================================================================
MAT2d_Tool2d::MAT2d_Tool2d()
{
  theDirection         = 1.;
  theNumberOfBisectors = 0;
  theNumberOfVecs      = 0;
  theNumberOfPnts      = 0;
}

//=============================================================================
//function : InitItems
//purpose  :
//=============================================================================
void  MAT2d_Tool2d::InitItems(const Handle(MAT2d_Circuit)& EquiCircuit) 
{
  theGeomBisectors.Clear();
  theGeomPnts.Clear();
  theGeomVecs.Clear();
  theLinesLength.Clear();
  theNumberOfBisectors = 0;
  theNumberOfVecs      = 0;
  theNumberOfPnts      = 0; 
   
  theCircuit = EquiCircuit;
}
			
//=============================================================================
//function : Sense
//purpose  :
//=============================================================================
void MAT2d_Tool2d::Sense(const MAT_Side aside)
{
  if(aside == MAT_Left) theDirection =  1.;
  else                  theDirection = -1.;
}

//=============================================================================
//function : NumberOfItems
//purpose  :
//=============================================================================
Standard_Integer MAT2d_Tool2d::NumberOfItems() const
{
  return theCircuit->NumberOfItems();
}

//=============================================================================
//function : ToleranceOfConfusion
//purpose  :
//=============================================================================
Standard_Real MAT2d_Tool2d::ToleranceOfConfusion() const
{
  return 2*MAT2d_TOLCONF;
}

//=============================================================================
//function : FirstPoint
//purpose  :
//=============================================================================
Standard_Integer MAT2d_Tool2d::FirstPoint(const Standard_Integer anitem,
					        Standard_Real&   dist  ) 
{
  Handle(Geom2d_Curve) curve;
  Handle(Geom2d_Point) point;
  theNumberOfPnts++;

  if (theCircuit->ConnexionOn(anitem)){
    gp_Pnt2d P1 = theCircuit->Connexion(anitem)->PointOnFirst();
    gp_Pnt2d P2 = theCircuit->Connexion(anitem)->PointOnSecond();
    theGeomPnts.Bind(theNumberOfPnts,gp_Pnt2d((P1.X() + P2.X())*0.5,
					      (P1.Y() + P2.Y())*0.5));
    dist = P1.Distance(P2)*0.5;
    return theNumberOfPnts;
  }

  Handle(Standard_Type) type;
  type = theCircuit->Value(anitem)->DynamicType();
  dist = 0.;

  if ( type != STANDARD_TYPE(Geom2d_CartesianPoint)){
    curve = Handle(Geom2d_Curve)::DownCast(theCircuit->Value(anitem));
    theGeomPnts.Bind(theNumberOfPnts,curve->Value(curve->FirstParameter()));
  }
  else{
    point = Handle(Geom2d_Point)::DownCast(theCircuit->Value(anitem));
    theGeomPnts.Bind(theNumberOfPnts,point->Pnt2d());
  }
  return theNumberOfPnts;
}

//=============================================================================
//function : TangentBefore
//purpose  :
//=============================================================================
Standard_Integer MAT2d_Tool2d::TangentBefore(const Standard_Integer anitem) 
{
  Standard_Integer     item;
  Handle(Geom2d_Curve) curve;
  theNumberOfVecs++;
  
  item  = (anitem == theCircuit->NumberOfItems()) ? 1 : (anitem + 1);
  if (theCircuit->ConnexionOn(item)){
    Standard_Real x1,y1,x2,y2;
    theCircuit->Connexion(item)->PointOnFirst().Coord(x1,y1);
    theCircuit->Connexion(item)->PointOnSecond().Coord(x2,y2);
    theGeomVecs.Bind(theNumberOfVecs,gp_Vec2d((x2-x1),(y2-y1)));
    return theNumberOfVecs;
  }

  Handle(Standard_Type) type;
  type = theCircuit->Value(anitem)->DynamicType();
  if ( type != STANDARD_TYPE(Geom2d_CartesianPoint)){
    curve = Handle(Geom2d_Curve)::DownCast(theCircuit->Value(anitem));
    theGeomVecs.Bind(theNumberOfVecs,curve->DN(curve->LastParameter(),1));
  }
  else {
    curve = Handle(Geom2d_Curve)::DownCast(theCircuit->Value(item));
    theGeomVecs.Bind(theNumberOfVecs,curve->DN(curve->FirstParameter(),1));
  }

  return theNumberOfVecs;
}

//=============================================================================
//function : TangentAfter
//purpose  :
//=============================================================================
Standard_Integer MAT2d_Tool2d::TangentAfter(const Standard_Integer anitem)
{
  Standard_Integer     item;
  Handle(Geom2d_Curve) curve;
  gp_Vec2d             thevector;
  theNumberOfVecs++;

  if (theCircuit->ConnexionOn(anitem)){
    Standard_Real x1,y1,x2,y2;
    theCircuit->Connexion(anitem)->PointOnFirst().Coord(x1,y1);
    theCircuit->Connexion(anitem)->PointOnSecond().Coord(x2,y2);
    theGeomVecs.Bind(theNumberOfVecs,gp_Vec2d((x1-x2),(y1-y2)));
    return theNumberOfVecs;
  }

  Handle(Standard_Type) type;
  type = theCircuit->Value(anitem)->DynamicType();
  if ( type != STANDARD_TYPE(Geom2d_CartesianPoint)){
    curve     = Handle(Geom2d_Curve)::DownCast(theCircuit->Value(anitem));
    thevector = curve->DN(curve->FirstParameter(),1);
  }
  else {
    item      = (anitem == 1) ? theCircuit->NumberOfItems() : (anitem - 1);
    curve     = Handle(Geom2d_Curve)::DownCast(theCircuit->Value(item));
    thevector = curve->DN(curve->LastParameter(),1);
  }
  theGeomVecs.Bind(theNumberOfVecs,thevector.Reversed());
  return theNumberOfVecs;
}

//=============================================================================
//function : Tangent
//purpose  :
//=============================================================================
Standard_Integer MAT2d_Tool2d::Tangent(const Standard_Integer bisector)
{
  theNumberOfVecs++;
  theGeomVecs.Bind(theNumberOfVecs,GeomBis(bisector).Value()
		   ->DN(GeomBis(bisector).Value()
			->LastParameter(),1));
  return theNumberOfVecs;
}

//=============================================================================
//function : CreateBisector
//purpose  :
//=============================================================================
void MAT2d_Tool2d::CreateBisector(const Handle(MAT_Bisector)& abisector)
{
  Handle(Geom2d_Point)    point1,point2;
  Handle(Geom2d_Geometry) elt1,elt2;
  Bisector_Bisec          bisector;
  Standard_Real           tolerance    = MAT2d_TOLCONF ;

  Standard_Integer edge1number  = abisector->FirstEdge()->EdgeNumber();
  Standard_Integer edge2number  = abisector->SecondEdge()->EdgeNumber();
  Standard_Boolean ontheline    = AreNeighbours(edge1number,
						edge2number,
						NumberOfItems());
  Standard_Boolean InitialNeighbour = ontheline;

  if(theCircuit->ConnexionOn(edge2number)) ontheline = Standard_False;

  elt1 = theCircuit->Value(edge1number);
  elt2 = theCircuit->Value(edge2number);

  Handle(Standard_Type) type1;
  type1 = theCircuit->Value(edge1number)->DynamicType();
  Handle(Standard_Type) type2;
  type2 = theCircuit->Value(edge2number)->DynamicType();
  Handle(Geom2d_Curve)  item1;
  Handle(Geom2d_Curve)  item2;

  if ( type1 != STANDARD_TYPE(Geom2d_CartesianPoint)){
    item1 = Handle(Geom2d_Curve)::DownCast(elt1);
  }

  if ( type2 != STANDARD_TYPE(Geom2d_CartesianPoint)){
    item2 = Handle(Geom2d_Curve)::DownCast(elt2);
  }

#ifdef DEB
  Standard_Boolean Affich = Standard_False;
  if (Affich) {
    cout<<endl; 
    cout<<"BISECTOR number :  "<<theNumberOfBisectors+1<<endl;
    cout<<"  Item 1 : "<<endl;
    cout<<edge1number<<endl;
    cout<<endl;
//    elt1->Dump(1,1);
    cout<<endl;
    cout<<"  Item 2 : "<<endl;
    cout<<edge2number<<endl;
    cout<<endl;
//  elt2->Dump(1,1);
    cout<<endl;
  }
#endif

  if(type1 != STANDARD_TYPE(Geom2d_CartesianPoint) && 
     type2 != STANDARD_TYPE(Geom2d_CartesianPoint)) {
    bisector.Perform(item1,item2,
		     GeomPnt (abisector->IssuePoint()),
		     GeomVec (abisector->FirstVector()),
		     GeomVec (abisector->SecondVector()),
		     theDirection,tolerance,ontheline);
  }
  else if(type1 == STANDARD_TYPE(Geom2d_CartesianPoint) && 
	  type2 == STANDARD_TYPE(Geom2d_CartesianPoint)) {
    point1 = Handle(Geom2d_Point)::DownCast(elt1);
    point2 = Handle(Geom2d_Point)::DownCast(elt2);
    bisector.Perform(point1,point2,
		     GeomPnt (abisector->IssuePoint()),
		     GeomVec (abisector->FirstVector()),
		     GeomVec (abisector->SecondVector()),
		     theDirection,tolerance,ontheline);
  }
  else if(type1 == STANDARD_TYPE(Geom2d_CartesianPoint)) {
    point1 = Handle(Geom2d_Point)::DownCast(elt1);
    bisector.Perform(point1,item2,
		     GeomPnt (abisector->IssuePoint()),
		     GeomVec (abisector->FirstVector()),
		     GeomVec (abisector->SecondVector()),
		     theDirection,tolerance,ontheline);
  }
  else {
    point2 = Handle(Geom2d_Point)::DownCast(elt2);
    bisector.Perform(item1,point2,
		     GeomPnt (abisector->IssuePoint()),
		     GeomVec (abisector->FirstVector()),
		     GeomVec (abisector->SecondVector()),
		     theDirection,tolerance,ontheline);
  }

  //------------------------------
  // Restriction de la bisectrice.
  //-----------------------------
  TrimBisec(bisector,edge1number,InitialNeighbour,1);
  TrimBisec(bisector,edge2number,InitialNeighbour,2);

  theNumberOfBisectors++;
  theGeomBisectors.Bind(theNumberOfBisectors,bisector);

  abisector->BisectorNumber(theNumberOfBisectors);
  abisector->Sense(1);

#ifdef DEB
  Standard_Boolean AffichDraw = Standard_False;
  if (AffichDraw) Dump(abisector->BisectorNumber(),1);
  if (Store) {    
    Handle(Standard_Type) Type1 = Type(bisector.Value()->BasisCurve());    
    Handle(Geom2d_Curve)  BasisCurve;
    if (Type1 == STANDARD_TYPE(Bisector_BisecAna)) {
      BasisCurve = Handle(Bisector_BisecAna)
	::DownCast(bisector.Value()->BasisCurve())->Geom2dCurve();
#ifdef DRAW
      char  *name = new char[100];
      sprintf(name,"BISSEC_%d",abisector->BisectorNumber());
      DrawTrSurf::Set(name,BasisCurve);
      delete [] name;
#endif
    }
  }
#endif
}

//=============================================================================
//function : TrimBisec
//purpose  : Restriction de la bisectrice.
//           Restriction des bissectrice separant deux elements lies par une
//           connexion ou l un au moins des elements est un cercle.
//           Cette restriction est necessaire a la logique de l algorithme.
//=============================================================================
void MAT2d_Tool2d::TrimBisec (      Bisector_Bisec&  B1,
			      const Standard_Integer IndexEdge,
			      const Standard_Boolean InitialNeighbour,
			      const Standard_Integer StartOrEnd      ) const
{
  Handle(Geom2d_Curve)        Curve;
  Handle(Geom2d_TrimmedCurve) LineSupportDomain,Line;
  Handle(Geom2d_Line)         Line1,Line2;
  
  //gp_Vec2d             Tan1,Tan2;
  gp_Pnt2d             Ori; //PEdge;
  Standard_Integer     INext;
  INext = (IndexEdge == theCircuit->NumberOfItems()) ? 1  : (IndexEdge + 1);
  
  Handle(Standard_Type) EdgeType = theCircuit->Value(IndexEdge)->DynamicType();
  
  if (EdgeType != STANDARD_TYPE(Geom2d_CartesianPoint)) {
    if(!InitialNeighbour) {
      Curve = Handle(Geom2d_TrimmedCurve)
	::DownCast(theCircuit->Value(IndexEdge))->BasisCurve();
      EdgeType = Curve->DynamicType();
      //-------------------------------------------------------------------
      // si l edge est liee a sa voisine  precedente par une connexion.
      //-------------------------------------------------------------------
      if (theCircuit->ConnexionOn(IndexEdge) && StartOrEnd == 1){
	if (EdgeType == STANDARD_TYPE(Geom2d_Circle)) {
	  Ori = Handle(Geom2d_Circle)::DownCast(Curve)->Location();
	  gp_Pnt2d P2 = theCircuit->Connexion(IndexEdge)->PointOnFirst();
	  Line1       = new Geom2d_Line (Ori,gp_Dir2d(P2.X() - Ori.X(),
						      P2.Y() - Ori.Y()));
	}     
      }
      //-----------------------------------------------------------------------
      // Si l edge est liee a sa voisine suivante par une connexion.
      //-----------------------------------------------------------------------
      if (theCircuit->ConnexionOn(INext) && StartOrEnd == 2){
	if (EdgeType == STANDARD_TYPE(Geom2d_Circle)) {
	  Ori = Handle(Geom2d_Circle)::DownCast(Curve)->Location();
	  gp_Pnt2d P2 = theCircuit->Connexion(INext)->PointOnSecond();
	  Line2       = new Geom2d_Line (Ori,gp_Dir2d(P2.X() - Ori.X(),
						      P2.Y() - Ori.Y()));
	}
      }
      if (Line1.IsNull() && Line2.IsNull()) return;

      //-----------------------------------------------------------------------
      // Restriction de la bisectrice par les demi-droites liees aux connexions
      // si elles existent.
      //-----------------------------------------------------------------------
      if (!Line1.IsNull()) {
	Line = new Geom2d_TrimmedCurve(Line1,0.,Precision::Infinite());
	SetTrim(B1,Line);
      }
      if (!Line2.IsNull()) {
	Line = new Geom2d_TrimmedCurve(Line2,0.,Precision::Infinite());
	SetTrim(B1,Line);
      }
    }
  }
}

//=============================================================================
//function : TrimBisector
//purpose  :
//=============================================================================
Standard_Boolean MAT2d_Tool2d::TrimBisector
  (const Handle(MAT_Bisector)& abisector)
{
  Standard_Real param = abisector->FirstParameter();

#ifdef DEB
  Standard_Boolean Affich = Standard_False;
  if (Affich) cout<<"TRIM de "<<abisector->BisectorNumber()<<endl;
#endif

  Handle(Geom2d_TrimmedCurve) 
    bisector = Handle(Geom2d_TrimmedCurve)
      ::DownCast(ChangeGeomBis(abisector->BisectorNumber()).ChangeValue());
  
  if(bisector->BasisCurve()->IsPeriodic() && param == Precision::Infinite()) {
    param = bisector->FirstParameter() + 2*PI;
  }
  if (param > bisector->BasisCurve()->LastParameter()) {
   param = bisector->BasisCurve()->LastParameter(); 
  }
  if(bisector->FirstParameter() == param) return Standard_False;

  bisector->SetTrim(bisector->FirstParameter(),param);
  return Standard_True;
}

//=============================================================================
//function : TrimBisector
//purpose  :
//=============================================================================
Standard_Boolean MAT2d_Tool2d::TrimBisector
  (const Handle(MAT_Bisector)& abisector,
   const Standard_Integer      apoint)
{
  Standard_Real Param;
  Handle(Geom2d_TrimmedCurve)
    Bisector = Handle(Geom2d_TrimmedCurve)::
      DownCast(ChangeGeomBis(abisector->BisectorNumber()).ChangeValue());

  Handle(Bisector_Curve) Bis = Handle(Bisector_Curve)::
    DownCast(Bisector->BasisCurve());

//  Param = ParameterOnCurve(Bisector,theGeomPnts.Value(apoint));
  Param = Bis->Parameter(GeomPnt (apoint));

  if (Bisector->BasisCurve()->IsPeriodic()) {
    if (Bisector->FirstParameter() > Param) Param = Param + 2*PI;
  }
  if(Bisector->FirstParameter() >= Param)return Standard_False;
  if(Bisector->LastParameter()  <  Param)return Standard_False;
  Bisector->SetTrim(Bisector->FirstParameter(),Param);

#ifdef DEB
  Standard_Boolean Affich = Standard_False;
  if (Affich) MAT2d_DrawCurve(Bisector,2);
#endif  

  return Standard_True;
}

//=============================================================================
//function : Projection
//purpose  :
//=============================================================================
Standard_Boolean MAT2d_Tool2d::Projection (const Standard_Integer IEdge   ,
					   const gp_Pnt2d&        PCom    ,
					         Standard_Real&   Distance) 
     const
{  
  gp_Pnt2d                    PEdge;
  Handle(Geom2d_Geometry)     Elt    = theCircuit->Value(IEdge);
  Handle(Standard_Type)       Type   = Elt->DynamicType();	
  Handle(Geom2d_TrimmedCurve) Curve; 
  Standard_Integer            INext;   
  Standard_Real               Eps = MAT2d_TOLCONF;//*10.;

  if (Type == STANDARD_TYPE(Geom2d_CartesianPoint)) {	
    PEdge     = Handle(Geom2d_Point)::DownCast(Elt)->Pnt2d();
    Distance  =	PCom.Distance(PEdge); 	
  }
  else {
    Distance = Precision::Infinite();
    Curve    = Handle(Geom2d_TrimmedCurve)::DownCast(Elt);	
    //-----------------------------------------------------------------------
    // Calcul des parametres MinMax sur l edge si celui ci est lies a ses
    // voisins par des connexions la courbe de calcul est limitee par 
    // celles_ci. 	  
    //-----------------------------------------------------------------------
    Standard_Real ParamMin = Curve->FirstParameter();
    Standard_Real ParamMax = Curve->LastParameter();
    if (theCircuit->ConnexionOn(IEdge)) {
      ParamMin = theCircuit->Connexion(IEdge)->ParameterOnSecond(); 
    }
    INext = (IEdge == theCircuit->NumberOfItems()) ? 1 : (IEdge + 1);
    if (theCircuit->ConnexionOn(INext)) {
      ParamMax = theCircuit->Connexion(INext)->ParameterOnFirst(); 
      if (Curve->BasisCurve()->IsPeriodic()){
	ElCLib::AdjustPeriodic(0.,2*PI,Eps,ParamMin,ParamMax);
      }
    }
    //---------------------------------------------------------------------
    // Constuction de la courbe pour les extremas et ajustement des bornes.
    //---------------------------------------------------------------------
    Geom2dAdaptor_Curve C1(Curve);
    GeomAbs_CurveType TypeC1 = C1.GetType();
    if (TypeC1 == GeomAbs_Circle) {
      Standard_Real R       = C1.Circle().Radius();
      Standard_Real EpsCirc = Eps;
      if ( R < 1.)  EpsCirc = Eps/R;
      if (!((ParamMax - ParamMin + 2*EpsCirc) < 2*PI)) {
	ParamMax = ParamMax + EpsCirc; ParamMin = ParamMin - EpsCirc;
      }
    }
    else {
      ParamMax = ParamMax + Eps; ParamMin = ParamMin - Eps; 
    }
    //-----------------------------------------------------
    // Calcul des extremas et stockage minimum de distance.
    //-----------------------------------------------------
    Extrema_ExtPC2d Extremas(PCom,C1,ParamMin,ParamMax);
    if (Extremas.IsDone()){
      if (Extremas.NbExt() == 0 ) return Standard_False; // Pas de solution!
      for (Standard_Integer i = 1; i <= Extremas.NbExt(); i++) {
	if (Extremas.SquareDistance(i) < Distance * Distance) {
	  Distance      = sqrt (Extremas.SquareDistance(i));
	}
      }
    }
    else {
      if (TypeC1 == GeomAbs_Circle) {
	Distance = C1.Circle().Radius();
      }
    }
  }
  return Standard_True;
}

//=============================================================================
//function : IsSameDistance
// purpose :
//=============================================================================
Standard_Boolean MAT2d_Tool2d::IsSameDistance (
   const Handle(MAT_Bisector)& BisectorOne,
   const Handle(MAT_Bisector)& BisectorTwo,
   const gp_Pnt2d&             PCom,
   Standard_Real&              Distance) const
{
  TColStd_Array1OfReal Dist(1,4);
  Standard_Integer     IEdge1,IEdge2,IEdge3,IEdge4;

  IEdge1 = BisectorOne->FirstEdge() ->EdgeNumber();
  IEdge2 = BisectorOne->SecondEdge()->EdgeNumber();
  IEdge3 = BisectorTwo->FirstEdge() ->EdgeNumber();
  IEdge4 = BisectorTwo->SecondEdge()->EdgeNumber();

  Projection(IEdge1,PCom,Dist(1));
  Projection(IEdge2,PCom,Dist(2));

  if      (IEdge3 == IEdge1) Dist(3)  = Dist(1);
  else if (IEdge3 == IEdge2) Dist(3)  = Dist(2);  
  else                       Projection(IEdge3,PCom,Dist(3));

  if      (IEdge4 == IEdge1) Dist(4)  = Dist(1);
  else if (IEdge4 == IEdge2) Dist(4)  = Dist(2);  
  else                       Projection(IEdge4,PCom,Dist(4));

#ifdef DEB
  Standard_Boolean Affich = Standard_False;
  if (Affich)
    for (Standard_Integer j = 1; j <= 4;j++){
      cout <<"Distance number : "<<j<<" is :"<< Dist(j)<<endl;
    }
#endif

  Standard_Real EpsDist = MAT2d_TOLCONF*100. ;
  Distance = Dist(1);
  for (Standard_Integer i = 1; i <= 4; i++){
    if (Abs(Dist(i) - Distance) > EpsDist) {
      Distance = Precision::Infinite();
      return Standard_False;
    }
  }
  return Standard_True;
}

//=============================================================================
//function : IntersectBisector
//purpose  :
//=============================================================================
Standard_Real MAT2d_Tool2d::IntersectBisector (
   const Handle(MAT_Bisector)& BisectorOne,
   const Handle(MAT_Bisector)& BisectorTwo,
   Standard_Integer&           IntPnt)
{
  Standard_Real    Tolerance     = MAT2d_TOLCONF;
  Standard_Real    Param1,Param2;
  Standard_Real    Parama,Paramb;
  Standard_Real    Distance = Precision::Infinite(),DistanceMini;
  Standard_Boolean SolutionValide;
  gp_Pnt2d         PointSolution;

  Handle(Geom2d_TrimmedCurve)
    Bisector1 = Handle(Geom2d_TrimmedCurve)
      ::DownCast(ChangeGeomBis(BisectorOne->BisectorNumber()).ChangeValue());

  Handle(Geom2d_TrimmedCurve) 
    Bisector2 = Handle(Geom2d_TrimmedCurve)
      ::DownCast(ChangeGeomBis(BisectorTwo->BisectorNumber()).ChangeValue());

  if(Bisector1.IsNull() || Bisector2.IsNull()) return Precision::Infinite();

  //-------------------------------------------------------------------------
  // Si les deux bissectrices separent des elements consecutifs et qu elles
  // sont issues des connexions C1 et C2.
  // Si C1 est la reverse de C2 ,alors les deux bissectrices sont issues
  // du meme point. Dans ce cas l intersection n est pas validee.
  //-------------------------------------------------------------------------
  Standard_Integer IS1 = BisectorOne->SecondEdge()->EdgeNumber();
  Standard_Integer IS2 = BisectorTwo->SecondEdge()->EdgeNumber();
  Standard_Integer IF1 = BisectorOne->FirstEdge() ->EdgeNumber();
  Standard_Integer IF2 = BisectorTwo->FirstEdge() ->EdgeNumber();
  
  if (AreNeighbours(IF1,IS1,NumberOfItems()) && 
      AreNeighbours(IF2,IS2,NumberOfItems()) &&
      theCircuit->ConnexionOn(IS2)           && 
      theCircuit->ConnexionOn(IS1)             ) {
    Handle(MAT2d_Connexion) C1,C2;
    C1 = theCircuit->Connexion(IS1);
    C2 = theCircuit->Connexion(IS2); 
    if (C2->IndexFirstLine() == C1->IndexSecondLine() &&
	C1->IndexFirstLine() == C2->IndexSecondLine()  )
      return Precision::Infinite();
  }

  // -----------------------------------------
  // Construction des domaines d intersection.
  // -----------------------------------------
  IntRes2d_Domain Domain1 = Domain(Bisector1,Tolerance);
  IntRes2d_Domain Domain2 = Domain(Bisector2,Tolerance);

  if (Domain1.LastParameter() - Domain1.FirstParameter()  < Tolerance) 
     return Precision::Infinite();
  if (Domain2.LastParameter() - Domain2.FirstParameter()  < Tolerance) 
     return Precision::Infinite();

#ifdef DEB
  Standard_Boolean Affich = Standard_False;
  if (Affich) {
    cout<<endl;
    cout<<"INTERSECTION de "<<BisectorOne->BisectorNumber()<<
                   " et de "<<BisectorTwo->BisectorNumber()<<endl;
    cout<<"  Bisector 1 : "<<endl;
//    (Bisector1->BasisCurve())->Dump(-1,1);
    cout<<endl;
    Debug(Domain1.FirstParameter());
    Debug(Domain1.LastParameter());
    cout<<"-----------------"<<endl;
    cout<<"  Bisector 2 : "<<endl;
//    (Bisector2->BasisCurve())->Dump(-1,1);
    cout<<endl;
    Debug(Domain2.FirstParameter());
    Debug(Domain2.LastParameter());
    cout<<"-----------------"<<endl;
  }
#endif

// -------------------------
// Calcul de l intersection.
// -------------------------

  Bisector_Inter Intersect;
  Intersect.Perform (GeomBis(BisectorOne->BisectorNumber()),Domain1,
		     GeomBis(BisectorTwo->BisectorNumber()),Domain2,
		     Tolerance,Tolerance,Standard_True);

//  Geom2dInt_GInter Intersect;
//  Intersect.Perform(Bisector1,Domain1,Bisector2,Domain2,Tolerance,Tolerance);

// -------------------------------------------------------------------------
// Exploitation du resultat de l intersection et selection du point solution
// equidistant des deux edges et le plus proche en parametre de l origine 
// des bissectrices.
// -------------------------------------------------------------------------

  if(!Intersect.IsDone()) return Precision::Infinite();

  if(Intersect.IsEmpty()) return Precision::Infinite();

  DistanceMini   = Precision::Infinite();
  Param1         = Precision::Infinite();
  Param2         = Precision::Infinite();
  SolutionValide = Standard_False;

  if(Intersect.NbSegments() >= 1) {              
    Standard_Real    MaxSegmentLength = 10.*Tolerance;
    for (Standard_Integer i=1;i<=Intersect.NbSegments();i++) {
      IntRes2d_IntersectionSegment Segment     = Intersect.Segment(i);
      Standard_Boolean             PointRetenu = Standard_False;
      gp_Pnt2d                     PointOnSegment;
      // ----------------------------------------------------------------
      // Si les segments sont petits, recherche des points sur le segment
      // equidistants des edges.
      // ----------------------------------------------------------------
      if ((Segment.HasFirstPoint() && Segment.HasLastPoint())) { 
	gp_Pnt2d      P1,P2;
	Standard_Real SegmentLength;
	P1 = Segment.FirstPoint().Value();
	P2 = Segment.LastPoint().Value();
	SegmentLength = P1.Distance(P2);
	if (SegmentLength <= Tolerance) {
	  PointOnSegment = P1;
	  if(IsSameDistance(BisectorOne,BisectorTwo,
			    PointOnSegment,Distance)) 
	    PointRetenu = Standard_True;
	}
	else if (SegmentLength <= MaxSegmentLength) {
	  gp_Dir2d  Dir(P2.X()-P1.X(),P2.Y()-P1.Y());
	  Standard_Real Dist = 0.;  
	  while (Dist <= SegmentLength + Tolerance){
	    PointOnSegment = P1.Translated(Dist*Dir);
	    if(IsSameDistance(BisectorOne,BisectorTwo,
			      PointOnSegment,Distance)) {
	      PointRetenu = Standard_True;
	      break;
	    }
	    Dist = Dist + Tolerance;
	  }
	}
      }  

      // ----------------------------------------------------------------
      // Sauvegarde du point equidistant des edges de plus petit 
      // parametre sur les bissectrices.
      // ----------------------------------------------------------------
      if(PointRetenu) {
	Parama = Handle(Bisector_Curve)::DownCast(Bisector1->BasisCurve())
	  ->Parameter(PointOnSegment);
	Paramb = Handle(Bisector_Curve)::DownCast(Bisector2->BasisCurve())
	  ->Parameter(PointOnSegment);
	if(Parama < Param1 && Paramb < Param2) {
	  Param1         = Parama;
	  Param2         = Paramb;
	  DistanceMini   = Distance;
	  PointSolution  = PointOnSegment;
	  SolutionValide = Standard_True;
	}
      }
    }
  }

  if(Intersect.NbPoints() != 1) {
    for(Standard_Integer i=1; i<=Intersect.NbPoints(); i++) {
      if(IsSameDistance(BisectorOne,BisectorTwo,
			Intersect.Point(i).Value(),Distance) &&
	 Distance > Tolerance                                   ) {
	Parama = Intersect.Point(i).ParamOnFirst();
	Paramb = Intersect.Point(i).ParamOnSecond();
	if (Parama < Param1 && Paramb < Param2) {
	  Param1         = Parama;
	  Param2         = Paramb;
	  DistanceMini   = Distance;
	  PointSolution  = Intersect.Point(i).Value();
	  SolutionValide = Standard_True;
	}
      }
    }
  }
  else {
    PointSolution  = Intersect.Point(1).Value();
    Param1         = Intersect.Point(1).ParamOnFirst();
    Param2         = Intersect.Point(1).ParamOnSecond();
    SolutionValide = IsSameDistance(BisectorOne,BisectorTwo,
				    PointSolution,DistanceMini);
  }

  if (!SolutionValide) return Precision::Infinite();
  theNumberOfPnts++;
  theGeomPnts.Bind(theNumberOfPnts,PointSolution);
  IntPnt = theNumberOfPnts;

  //-----------------------------------------------------------------------
  // Si le point d intersection est quasi confondue avec une des extremites
  // de l une ou l autre des bisectrices, l intersection n est pas validee.
  //
  // SAUF si une des bisectrices est issue d une connexion et que les 
  // edges separes par les bissectrices sont des voisines sur le contour
  // initiales.
  // en effet le milieu de la connexion P qui est l origine d une des 
  // bissectrices peut etre sur l autre bissectrice. 
  // P est donc point d intersection
  // et la bissectrice issue de la connexion est de longueur nulle.
  // (ex : un rectangle dans un rectangle ou la connexion est entre un coin
  // et un cote).
  //-----------------------------------------------------------------------

  Standard_Integer IndexEdge1,IndexEdge2,IndexEdge3,IndexEdge4;
  Standard_Boolean ExtremiteControle = Standard_True;

  IndexEdge1 = BisectorOne->FirstEdge() ->EdgeNumber();
  IndexEdge2 = BisectorOne->SecondEdge()->EdgeNumber();
  IndexEdge3 = BisectorTwo->FirstEdge() ->EdgeNumber();
  IndexEdge4 = BisectorTwo->SecondEdge()->EdgeNumber();
  
  if (theCircuit->ConnexionOn(IndexEdge2)){
    // --------------------------------------
    // BisectorOne est issue d une connexion.  
    // --------------------------------------
   if (AreNeighbours(IndexEdge1,IndexEdge2,NumberOfItems()) && 
       AreNeighbours(IndexEdge3,IndexEdge4,NumberOfItems()) && 
       IndexEdge2 == IndexEdge3                               ){
      ExtremiteControle = Standard_False;
      Param1             = Param1 + Tolerance;
    }
  }
  
  if (theCircuit->ConnexionOn(IndexEdge4)){
    // --------------------------------------
    // BisectorTwo est issue d une connexion.   
    // --------------------------------------
    if (AreNeighbours(IndexEdge1,IndexEdge2,NumberOfItems()) && 
	AreNeighbours(IndexEdge3,IndexEdge4,NumberOfItems()) &&
	IndexEdge2 == IndexEdge3                               ){
      ExtremiteControle = Standard_False;
      Param2            = Param2 + Tolerance;
    }
  }
 
  if (ExtremiteControle) {
    if(Bisector1->StartPoint().Distance(PointSolution) < Tolerance ||
       Bisector2->StartPoint().Distance(PointSolution) < Tolerance  ) 
      return Precision::Infinite();
  }

  if(BisectorOne->SecondParameter() < Precision::Infinite() &&
     BisectorOne->SecondParameter() < Param1*(1. - Tolerance )) 
    return Precision::Infinite();
  
  if(BisectorTwo->FirstParameter() < Precision::Infinite() &&
     BisectorTwo->FirstParameter() < Param2*(1.- Tolerance)) 
    return Precision::Infinite();

  BisectorOne->SecondParameter(Param1);
  BisectorTwo->FirstParameter (Param2);
  
#ifdef DEB
  if (Affich) {
    cout<<"   coordonnees    : "<<GeomPnt  (IntPnt).X()<<" "
                                <<GeomPnt  (IntPnt).Y()<<endl;
    cout<<"   parametres     : "<<Param1<<" "<<Param2<<endl;
    cout<<"   distancemini   : "<<DistanceMini<<endl;
  }
#endif
  
  return DistanceMini;
}

//=============================================================================
//function : Distance
//purpose  :
//=============================================================================
Standard_Real MAT2d_Tool2d::Distance(const Handle(MAT_Bisector)& Bis,
				     const Standard_Real         Param1,
				     const Standard_Real         Param2) const
{
  Standard_Real Dist = Precision::Infinite();

  if (Param1 != Precision::Infinite() && Param2 != Precision::Infinite()) {
    gp_Pnt2d P1 = GeomBis(Bis->BisectorNumber()).Value()->Value(Param1);
    gp_Pnt2d P2 = GeomBis(Bis->BisectorNumber()).Value()->Value(Param2);
    Dist        = P1.Distance(P2);
  }
  return Dist;
}

//=============================================================================
//function : Dump
//purpose  :
//=============================================================================
#ifndef DEB
void MAT2d_Tool2d::Dump(const Standard_Integer ,
			const Standard_Integer ) const
{
  Standard_NotImplemented::Raise();
#else
void MAT2d_Tool2d::Dump(const Standard_Integer bisector,
			const Standard_Integer) const
{
  if(bisector == -1) return;
  if(bisector > theNumberOfBisectors) return;

  Handle(Geom2d_Curve) thebisector = GeomBis(bisector).Value(); 

  MAT2d_DrawCurve(thebisector,3);

#endif
}


//=============================================================================
//function : GeomBis
//purpose  :
//=============================================================================
const Bisector_Bisec&  MAT2d_Tool2d::GeomBis (const Standard_Integer Index) 
     const
{
  return theGeomBisectors.Find(Index);
}

//=============================================================================
//function : ChangeGeomBis
//purpose  :
//=============================================================================
Bisector_Bisec&  MAT2d_Tool2d::ChangeGeomBis(const Standard_Integer Index)
{
  return theGeomBisectors.ChangeFind(Index);
}


//=============================================================================
//function : GeomElt
//purpose  :
//=============================================================================
Handle(Geom2d_Geometry)  MAT2d_Tool2d::GeomElt(const Standard_Integer Index)
                                                                        const 
{
  return  theCircuit->Value(Index);
}


//=============================================================================
//function : GeomPnt
//purpose  :
//=============================================================================
const gp_Pnt2d&  MAT2d_Tool2d::GeomPnt(const Standard_Integer Index) const
{
  return theGeomPnts.Find(Index);
}

//=============================================================================
//function : GeomVec
//purpose  :
//=============================================================================
const gp_Vec2d&  MAT2d_Tool2d::GeomVec(const Standard_Integer Index)const 
{
  return theGeomVecs.Find(Index);
}

//=============================================================================
//function : Circuit
//purpose  :
//=============================================================================
Handle(MAT2d_Circuit) MAT2d_Tool2d::Circuit()const 
{
  return theCircuit;
}

//=============================================================================
//function : BisecFusion
//purpose  :
//=============================================================================
void MAT2d_Tool2d::BisecFusion(const Standard_Integer I1,
			       const Standard_Integer I2) 
{
  Standard_Real               DU,UL1,UF1;
  Handle(Geom2d_TrimmedCurve) Bisector1;
  Handle(Geom2d_TrimmedCurve) Bisector2;

  Bisector1 = Handle(Geom2d_TrimmedCurve)::DownCast(GeomBis(I1).Value());
  Bisector2 = Handle(Geom2d_TrimmedCurve)::DownCast(GeomBis(I2).Value());
  UF1       = Bisector1->FirstParameter();
  UL1       = Bisector1->LastParameter();

  Handle(Standard_Type) Type1 = Bisector1->BasisCurve()->DynamicType();
  if (Type1 == STANDARD_TYPE(Bisector_BisecCC)) {
    //------------------------------------------------------------------------------------
    // les bissectrice courbe/courbe sont  construites avec un point de depart
    // elles ne peuvent pas etre trimes par un point se trouvant de l autre cote du
    // point de depart.
    // pour faire la fusion des deux bissectrices on reconstruit la bissectrice entre les
    // deux courbes avec comme point de depart le dernier point de la Bisector2.
    // on trime ensuite la courbe par le dernier point de Bisector1.
    //------------------------------------------------------------------------------------
    Standard_Real            Tolerance    = MAT2d_TOLCONF;
    Bisector_Bisec           Bis;
    gp_Vec2d                 VBid(1,0);
    gp_Pnt2d                 P2   = Bisector2->Value(Bisector2->LastParameter());     
    gp_Pnt2d                 P1   = Bisector1->Value(Bisector1->LastParameter());   
    Handle(Bisector_BisecCC) BCC1 = Handle(Bisector_BisecCC)::DownCast(Bisector1->BasisCurve());

    Bis.Perform(BCC1->Curve(2), BCC1->Curve(1), P2, VBid, VBid, 
		theDirection, Tolerance, Standard_False); 

    Bisector1 = Handle(Geom2d_TrimmedCurve)::DownCast(Bis.Value());
    BCC1      = Handle(Bisector_BisecCC)   ::DownCast(Bisector1->BasisCurve());	
    UF1       = BCC1->FirstParameter();
    UL1       = BCC1->Parameter(P1);
    Bisector1->SetTrim(UF1,UL1);
    theGeomBisectors.Bind(I1,Bis);
  }
  else {
    DU        = Bisector2->LastParameter() - Bisector2->FirstParameter();
    UF1       = UF1 - DU;

    Handle(Bisector_BisecAna) BAna = Handle(Bisector_BisecAna)::DownCast(Bisector1->BasisCurve());
//---------------------------- uncomment if new method Bisector_BisecAna::SetTrim(f,l) is not used
//    Handle(Geom2d_Curve) C2d = BAna->Geom2dCurve();
//    Handle(Geom2d_TrimmedCurve) trimC2d = new Geom2d_TrimmedCurve(C2d, UF1, UL1);
//    BAna->Init(trimC2d);
//--------------------------- end
    BAna->SetTrim(UF1,UL1); // put comment if SetTrim(f,l) is not used

    Bisector1->SetTrim(UF1,UL1);
  }
}

//=============================================================================
//function : Type
//purpose  :
//=============================================================================
static Handle(Standard_Type) Type(const Handle(Geom2d_Geometry)& aGeom) 
{
  Handle(Standard_Type) type = aGeom->DynamicType();
  Handle(Geom2d_Curve)  curve;

  if (type == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
    curve = Handle(Geom2d_TrimmedCurve)::DownCast(aGeom)->BasisCurve();
    type  = curve->DynamicType();
  }
  return type;
}

//==========================================================================
//function : AreNeighbours
//purpose  : Return TRUE si IEdge1 et IEdge2 correspondent a des elements 
//           consecutifs sur un contour ferme de NbEdge elements.
//==========================================================================
Standard_Boolean AreNeighbours(const Standard_Integer IEdge1,
			       const Standard_Integer IEdge2,
			       const Standard_Integer NbEdge)
{
  if      (Abs(IEdge1 - IEdge2) == 1)         return Standard_True;
  else if (Abs(IEdge1 - IEdge2) == NbEdge -1) return Standard_True;
  else                                        return Standard_False; 
}

//==========================================================================
//function : SetTrim
//purpose  :
//==========================================================================
void SetTrim(Bisector_Bisec& Bis, Handle(Geom2d_Curve)& Line1)
{  
  Geom2dInt_GInter Intersect; 
  Standard_Real    Distance;  
  Standard_Real    Tolerance = MAT2d_TOLCONF;  
  Handle(Geom2d_TrimmedCurve) Bisector = 
    Handle(Geom2d_TrimmedCurve)::DownCast(Bis.ChangeValue());

  IntRes2d_Domain  Domain1   = Domain(Bisector,Tolerance);
  Standard_Real    UB1       = Bisector->FirstParameter();
  Standard_Real    UB2       = Bisector->LastParameter();
 
  gp_Pnt2d         FirstPointBisector = Bisector->Value(UB1);
  Standard_Real    UTrim              = Precision::Infinite();

  Geom2dAdaptor_Curve AdapBisector(Bisector);
  Geom2dAdaptor_Curve AdapLine1   (Line1);
  Intersect.Perform(AdapBisector, Domain1, 
		    AdapLine1, Tolerance, Tolerance);

  if (Intersect.IsDone() && !Intersect.IsEmpty()) {
    for (Standard_Integer i = 1; i <= Intersect.NbPoints(); i++) {
      gp_Pnt2d PInt = Intersect.Point(i).Value();
      Distance      = FirstPointBisector.Distance(PInt);
      if (Distance > 10.*Tolerance                     && 
	  Intersect.Point(i).ParamOnFirst() < UTrim ) {
	 UTrim = Intersect.Point(i).ParamOnFirst();
      }
    }
  } 
  // ------------------------------------------------------------------------
  // Restriction de la Bissectrice par le point d intersection de plus petit
  // parametre.
  // ------------------------------------------------------------------------
  if (UTrim < UB2 && UTrim > UB1) Bisector->SetTrim(UB1,UTrim);
}

//==========================================================================
//function : Domain
//purpose  :
//==========================================================================
IntRes2d_Domain  Domain(const Handle(Geom2d_TrimmedCurve)& Bisector1,
			const Standard_Real                Tolerance)
{
  Standard_Real Param1 = Bisector1->FirstParameter();
  Standard_Real Param2 = Bisector1->LastParameter();
  if(Param2 > 10000.) {
    Param2 = 10000.;
    Handle(Standard_Type) Type1 = Type(Bisector1->BasisCurve());    
    Handle(Geom2d_Curve)  BasisCurve;
    if (Type1 == STANDARD_TYPE(Bisector_BisecAna)) {
      BasisCurve = Handle(Bisector_BisecAna)
	::DownCast(Bisector1->BasisCurve())->Geom2dCurve();
      Type1      = BasisCurve->DynamicType();
    }
    gp_Parab2d gpParabola;
    gp_Hypr2d  gpHyperbola;
    Standard_Real Focus;
    Standard_Real Limit = 50000.;
    if (Type1 == STANDARD_TYPE(Geom2d_Parabola)) {
      gpParabola = Handle(Geom2d_Parabola)::DownCast(BasisCurve)->Parab2d();
      Focus = gpParabola.Focal();
      Standard_Real Val1 = Sqrt(Limit*Focus);
      Standard_Real Val2 = Sqrt(Limit*Limit);
      Param2 = (Val1 <= Val2 ? Val1:Val2);
    }
    else if (Type1 == STANDARD_TYPE(Geom2d_Hyperbola)) {
      gpHyperbola = Handle(Geom2d_Hyperbola)::DownCast(BasisCurve)->Hypr2d();
      Standard_Real Majr  = gpHyperbola.MajorRadius();
      Standard_Real Minr  = gpHyperbola.MinorRadius();
      Standard_Real Valu1 = Limit/Majr;
      Standard_Real Valu2 = Limit/Minr;
      Standard_Real Val1  = Log(Valu1+Sqrt(Valu1*Valu1-1));
      Standard_Real Val2  = Log(Valu2+Sqrt(Valu2*Valu2+1));
      Param2 = (Val1 <= Val2 ? Val1:Val2);
    }
  }
 
  IntRes2d_Domain Domain1(Bisector1->Value(Param1),Param1,Tolerance,
			  Bisector1->Value(Param2),Param2,Tolerance);
  if(Bisector1->BasisCurve()->IsPeriodic()) {
    Domain1.SetEquivalentParameters(0.,2.*PI);
  }
  return Domain1;
}

#ifdef DEB
//==========================================================================
//function : MAT2d_DrawCurve
//purpose  : Affichage d une courbe <aCurve> de Geom2d. dans une couleur
//           definie par <Indice>.
//            Indice = 1 jaune,
//            Indice = 2 bleu,
//            Indice = 3 rouge,
//            Indice = 4 vert.
//==========================================================================
void MAT2d_DrawCurve(const Handle(Geom2d_Curve)& aCurve,
		     const Standard_Integer      Indice)
{  
  Handle(Standard_Type)      type = aCurve->DynamicType();
  Handle(Geom2d_Curve)       curve,CurveDraw;
#ifdef DRAW
  Handle(DrawTrSurf_Curve2d) dr;
  Draw_Color                 Couleur;
#endif

  if (type == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
    curve = Handle(Geom2d_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
    type = curve->DynamicType();    
    // PB de representation des courbes semi_infinies.
    gp_Parab2d gpParabola;
    gp_Hypr2d  gpHyperbola;
    Standard_Real Focus;
    Standard_Real Limit = 50000.;
    Standard_Real delta = 400;

    // PB de representation des courbes semi_infinies.
    if (aCurve->LastParameter() == Precision::Infinite()) {
      
      if (type == STANDARD_TYPE(Geom2d_Parabola)) {
	gpParabola = Handle(Geom2d_Parabola)::DownCast(curve)->Parab2d();
	Focus = gpParabola.Focal();
	Standard_Real Val1 = Sqrt(Limit*Focus);
	Standard_Real Val2 = Sqrt(Limit*Limit);
	              delta= (Val1 <= Val2 ? Val1:Val2);
      }
      else if (type == STANDARD_TYPE(Geom2d_Hyperbola)) {
	gpHyperbola = Handle(Geom2d_Hyperbola)::DownCast(curve)->Hypr2d();
	Standard_Real Majr  = gpHyperbola.MajorRadius();
	Standard_Real Minr  = gpHyperbola.MinorRadius();
	Standard_Real Valu1 = Limit/Majr;
	Standard_Real Valu2 = Limit/Minr;
	Standard_Real Val1  = Log(Valu1+Sqrt(Valu1*Valu1-1));
	Standard_Real Val2  = Log(Valu2+Sqrt(Valu2*Valu2+1));
	              delta  = (Val1 <= Val2 ? Val1:Val2);
      }
      CurveDraw = new Geom2d_TrimmedCurve(aCurve,
					  aCurve->FirstParameter(),
					  aCurve->FirstParameter() + delta);
    }
    else {
      CurveDraw = aCurve;
    }
    // fin PB.
  }
  else {
    CurveDraw = aCurve;
  }

#ifdef DRAW
  if      (Indice == 1) Couleur = Draw_jaune;
  else if (Indice == 2) Couleur = Draw_bleu;
  else if (Indice == 3) Couleur = Draw_rouge;
  else if (Indice == 4) Couleur = Draw_vert;

  if (type == STANDARD_TYPE(Geom2d_Circle))
    dr = new DrawTrSurf_Curve2d(CurveDraw,Couleur,30);
  else if (type  == STANDARD_TYPE(Geom2d_Line))
    dr = new DrawTrSurf_Curve2d(CurveDraw,Couleur,2);
  else
    dr = new DrawTrSurf_Curve2d(CurveDraw,Couleur,500);

  dout << dr;
  dout.Flush();
#endif
}

#endif