summaryrefslogtreecommitdiff
path: root/src/TopOpeBRep/TopOpeBRep_ShapeIntersector.cxx
blob: f12ac4986d16529d3a0cbe4b14d326d20fa92ee4 (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
// File:	TopOpeBRep_ShapeIntersector.cxx
// Created:	Fri May  7 17:36:28 1993
// Author:	Jean Yves LEBEY
//		<jyl@topsn3>

#include <TopOpeBRep_ShapeIntersector.ixx>

#include <TopAbs.hxx>
#include <Bnd_Box.hxx>
#include <TopOpeBRepTool_box.hxx>
#include <TopOpeBRep_define.hxx>

#ifdef DEB
Standard_IMPORT Standard_Boolean TopOpeBRep_GettraceSI(); 
Standard_IMPORT Standard_Boolean TopOpeBRep_GetcontextFFOR();
Standard_IMPORT Standard_Integer SAVFFi1; // FacesIntersector
Standard_IMPORT Standard_Integer SAVFFi2; // FacesIntersector
Standard_IMPORT void TopOpeBRep_SettraceEEFF(const Standard_Boolean b);
Standard_IMPORT Standard_Boolean TopOpeBRep_GettraceEEFF(const Standard_Integer e1,const Standard_Integer e2,const Standard_Integer f1,const Standard_Integer f2);
void seteeff(const Standard_Boolean b,const Standard_Integer e1,const Standard_Integer e2, const Standard_Integer f1,const Standard_Integer f2)
{cout<<"b,e1,e2,f1,f2 : "<<b<<" "<<e1<<","<<e2<<","<<f1<<","<<f2<<endl;TopOpeBRep_SettraceEEFF(b);}
void seteefft(const Standard_Integer e1,const Standard_Integer e2, const Standard_Integer f1,const Standard_Integer f2) {seteeff(Standard_True,e1,e2,f1,f2);}
void seteefff(const Standard_Integer e1,const Standard_Integer e2, const Standard_Integer f1,const Standard_Integer f2) {seteeff(Standard_False,e1,e2,f1,f2);}
#endif

// modified by NIZHNY-OFV  Thu Apr 18 17:15:38 2002 (S)
#include <TopoDS.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Solid.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Edge.hxx>
#include <TopExp_Explorer.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopExp.hxx>
#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <BRepLib_MakeEdge.hxx>
#include <BRepLib_MakeWire.hxx>
#include <BRepLib_MakeFace.hxx>
#include <BRep_Builder.hxx>
#include <BRepAdaptor_Surface.hxx>
static Standard_Integer OneShapeIsHalfSpace(const TopoDS_Shape& S1,const TopoDS_Shape& S2);
static TopoDS_Solid GetNewSolid(const TopoDS_Shape& S, TopoDS_Face& F);
// modified by NIZHNY-OFV  Thu Apr 18 17:16:45 2002 (F)

//=======================================================================
//function : TopOpeBRep_ShapeIntersector
//purpose  : 
//=======================================================================

TopOpeBRep_ShapeIntersector::TopOpeBRep_ShapeIntersector()
{
  Reset();
  myFFIntersector.GetTolerances(myTol1,myTol2);
  myHBoxTool = FBOX_GetHBoxTool();
  myFaceScanner.ChangeBoxSort().SetHBoxTool(myHBoxTool);
  myEdgeScanner.ChangeBoxSort().SetHBoxTool(myHBoxTool);
}

//=======================================================================
//function : Reset
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::Reset()
{
  myIntersectionDone = Standard_False;

  myFFDone = Standard_False;
  myFFSameDomain = Standard_False;
  myEEFFDone = Standard_False;
  myEFDone = Standard_False;
  myFEDone = Standard_False;
  myEEDone = Standard_False;

  myFFInit = Standard_False;
  myEEFFInit = Standard_False;
  myEFInit = Standard_False;
  myFEInit = Standard_False;
  myEEInit = Standard_False;
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::Init
(const TopoDS_Shape& S1, const TopoDS_Shape& S2)
{
  Reset();
  myShape1 = S1;
  myShape2 = S2;
}

//=======================================================================
//function : SetIntersectionDone
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::SetIntersectionDone()
{
  myIntersectionDone = (myFFDone || 
			myEEFFDone || 
			myFEDone || 
			myEFDone || 
			myEEDone);
}


//=======================================================================
//function : CurrentGeomShape
//purpose  : 
//=======================================================================

const TopoDS_Shape& TopOpeBRep_ShapeIntersector::CurrentGeomShape
  (const Standard_Integer Index) const
{
  if ( myIntersectionDone ) {
    if      (myFFDone) {
      if      ( Index == 1 ) return myFaceScanner.Current();
      else if ( Index == 2 ) return myFaceExplorer.Current();
    }
    else if (myEEFFDone) {
      if      ( Index == 1 ) return myEdgeScanner.Current();
      else if ( Index == 2 ) return myEdgeExplorer.Current();
    }
    else if (myFEDone) {
      if      ( Index == 1 ) return myFaceScanner.Current();
      else if ( Index == 2 ) return myEdgeExplorer.Current();
    }
    else if (myEFDone) {
      if      ( Index == 1 ) return myEdgeScanner.Current();
      else if ( Index == 2 ) return myFaceExplorer.Current();
    }
    else if (myEEDone) {
      if      ( Index == 1 ) return myEdgeScanner.Current();
      else if ( Index == 2 ) return myEdgeExplorer.Current();
    }
  }

  Standard_Failure::Raise("CurrentGeomShape : no intersection");
  TopoDS_Shape* bid = new TopoDS_Shape();
  return *bid;
}
//modified by NIZNHY-PKV Fri Sep 24 11:02:59 1999 from
//=======================================================================
//function : RejectedFaces
//purpose  : 
//=======================================================================
void TopOpeBRep_ShapeIntersector::RejectedFaces (const TopoDS_Shape& anObj,
						 const TopoDS_Shape& aReference,
						 TopTools_ListOfShape& aListOfShape)
{

  Standard_Integer isHalfSpace = OneShapeIsHalfSpace( anObj, aReference );
  if( isHalfSpace != 0 )
    {
      TopoDS_Face newRejectFace;
      TopoDS_Solid newSolid;
      aListOfShape.Clear();
      
      if( isHalfSpace == 1 )
	{
	  newSolid = GetNewSolid( anObj, newRejectFace );
	  Init( newSolid, aReference );

	  TopAbs_ShapeEnum tscann = TopAbs_SOLID;
	  TopAbs_ShapeEnum texplo = TopAbs_FACE;
	  myFaceScanner.Clear();
	  myFaceScanner.AddBoxesMakeCOB( aReference, tscann );
	  myFaceExplorer.Init( newSolid, texplo );
  
	  for(; myFaceExplorer.More(); myFaceExplorer.Next())
	    {
	      TopOpeBRepTool_BoxSort& aBS = myFaceScanner.ChangeBoxSort();
	      if(!aBS.Compare(myFaceExplorer.Current()).More())
		{
		  const TopoDS_Shape& aS=myFaceExplorer.Current();
		  aListOfShape.Append (aS);
		}
	    }

	  texplo = TopAbs_EDGE;
	  myFaceScanner.Clear();
	  myFaceScanner.AddBoxesMakeCOB( aReference, tscann );
	  myFaceExplorer.Init( newSolid, texplo );

	  for(; myFaceExplorer.More(); myFaceExplorer.Next())
	    {
	      TopOpeBRepTool_BoxSort& aBS = myFaceScanner.ChangeBoxSort();
	      if(!aBS.Compare(myFaceExplorer.Current()).More())
		{
		  const TopoDS_Shape& aS=myFaceExplorer.Current();
		  aListOfShape.Append (aS);
		}
	    }
	}
      else
	{
	  newSolid = GetNewSolid( aReference, newRejectFace );
	  Init( anObj, newSolid );

	  TopAbs_ShapeEnum tscann = TopAbs_SOLID;
	  TopAbs_ShapeEnum texplo = TopAbs_FACE;
	  myFaceScanner.Clear();
	  myFaceScanner.AddBoxesMakeCOB( newSolid, tscann );
	  myFaceExplorer.Init( anObj, texplo);
  
	  for(; myFaceExplorer.More(); myFaceExplorer.Next())
	    {
	      TopOpeBRepTool_BoxSort& aBS = myFaceScanner.ChangeBoxSort();
	      if(!aBS.Compare(myFaceExplorer.Current()).More())
		{
		  const TopoDS_Shape& aS=myFaceExplorer.Current();
		  aListOfShape.Append (aS);
		}
	    }

	  texplo = TopAbs_EDGE;
	  myFaceScanner.Clear();
	  myFaceScanner.AddBoxesMakeCOB( newSolid, tscann );
	  myFaceExplorer.Init( anObj, texplo );

	  for(; myFaceExplorer.More(); myFaceExplorer.Next())
	    {
	      TopOpeBRepTool_BoxSort& aBS = myFaceScanner.ChangeBoxSort();
	      if(!aBS.Compare(myFaceExplorer.Current()).More())
		{
		  const TopoDS_Shape& aS=myFaceExplorer.Current();
		  aListOfShape.Append (aS);
		}
	    }
	}
      // remove all shapes of < newRejectFace > from list
      TopExp_Explorer ExpRF(newRejectFace, TopAbs_EDGE);
      for(; ExpRF.More(); ExpRF.Next() )
	{
	  const TopoDS_Edge& edgef = TopoDS::Edge( ExpRF.Current() );
	  TopTools_ListIteratorOfListOfShape it( aListOfShape );
	  for(; it.More(); it.Next() )
	    {
	      const TopoDS_Shape& shape = it.Value();

	      if( shape.ShapeType() != TopAbs_EDGE )
		continue;

	      const TopoDS_Edge& edgel = TopoDS::Edge( shape );
	      if( edgef.IsSame( edgel ) )
		{
		  aListOfShape.Remove(it);
		  break;
		}
	    }
	}
      TopTools_ListIteratorOfListOfShape it( aListOfShape );
      for(; it.More(); it.Next() )
	{
	  const TopoDS_Shape& shape = it.Value();

	  if( shape.ShapeType() != TopAbs_FACE )
	    continue;

	  const TopoDS_Face& facel = TopoDS::Face( shape );
	  if( facel.IsSame( newRejectFace ) )
	    {
	      aListOfShape.Remove(it);
	      break;
	    }
	}
      
      Init(anObj, aReference);
      return;
    }

  Init(anObj, aReference);

  aListOfShape.Clear(); 
  //find faces to reject
  
  TopAbs_ShapeEnum tscann = TopAbs_SOLID;
  TopAbs_ShapeEnum texplo = TopAbs_FACE;
  myFaceScanner.Clear();
  myFaceScanner.AddBoxesMakeCOB(aReference,tscann);
  myFaceExplorer.Init(anObj,texplo);
  
  for(; myFaceExplorer.More(); myFaceExplorer.Next()) {
    TopOpeBRepTool_BoxSort& aBS = myFaceScanner.ChangeBoxSort();
    if(!aBS.Compare(myFaceExplorer.Current()).More()) {
      const TopoDS_Shape& aS=myFaceExplorer.Current();
      aListOfShape.Append (aS);
    }
  }

  //modified by NIZHNY-MZV  Wed Apr  5 09:45:17 2000
  texplo = TopAbs_EDGE;
  myFaceScanner.Clear();
  myFaceScanner.AddBoxesMakeCOB(aReference,tscann);
  myFaceExplorer.Init(anObj,texplo);

  for(; myFaceExplorer.More(); myFaceExplorer.Next()) {
    TopOpeBRepTool_BoxSort& aBS = myFaceScanner.ChangeBoxSort();
    if(!aBS.Compare(myFaceExplorer.Current()).More()) {
      const TopoDS_Shape& aS=myFaceExplorer.Current();
      aListOfShape.Append (aS);
    }
  }

  //modified by NIZHNY-MZV  Wed Apr  5 09:45:17 2000
/*  texplo = TopAbs_VERTEX;
  myFaceScanner.Clear();
  myFaceScanner.AddBoxesMakeCOB(aReference,tscann);
  myFaceExplorer.Init(anObj,texplo);

  for(; myFaceExplorer.More(); myFaceExplorer.Next()) {
    TopOpeBRepTool_BoxSort& aBS = myFaceScanner.ChangeBoxSort();
    if(!aBS.Compare(myFaceExplorer.Current()).More()) {
      const TopoDS_Shape& aS=myFaceExplorer.Current();
      aListOfShape.Append (aS);
    }
  }
*/
}
//modified by NIZNHY-PKV Fri Sep 24 11:03:02 1999 to




//=======================================================================
//function : InitIntersection
//purpose  : 
//=======================================================================

  void TopOpeBRep_ShapeIntersector::InitIntersection (const TopoDS_Shape& S1, const TopoDS_Shape& S2)
{
  Init(S1,S2);

  InitFFIntersection();
  if ( MoreFFCouple() ) return;

  InitFEIntersection();
  if ( MoreFECouple() ) return;

  InitEFIntersection();
  if ( MoreEFCouple() ) return;
}


//=======================================================================
//function : InitIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::InitIntersection
  (const TopoDS_Shape& S1, const TopoDS_Shape& S2,
   const TopoDS_Face&  F1, const TopoDS_Face&  F2)
{
  Init(S1,S2);

  myEEFace1 = F1;
  myEEFace2 = F2;

  InitEEIntersection();
}


//=======================================================================
//function : MoreIntersection
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRep_ShapeIntersector::MoreIntersection() const
{
  Standard_Boolean res = myIntersectionDone;

#ifdef DEB
  if (TopOpeBRep_GettraceSI() && res) {
    if      ( myFFDone )   cout<<"FF : ";
    else if ( myEEFFDone ) cout<<"    EE : ";
    DumpCurrent(1);
    DumpCurrent(2);
    if      ( myFFDone && myFFSameDomain ) cout<<"(FF SameDomain)";
    else if ( myEEFFDone )                 cout<<"(EE of FF SameDomain)";
    else if ( myEEDone )                   cout<<"EE : ";
    cout<<endl;
    if (myEEFFDone) {
      Standard_Integer ie1 = myEdgeScanner.Index();
      Standard_Integer ie2 = myEdgeExplorer.Index();
      Standard_Integer if1 = myFaceScanner.Index();
      Standard_Integer if2 = myFaceExplorer.Index();
      cout<<"    trc teeff 1 "<<ie1<<" "<<ie2<<" "<<if1<<" "<<if2<<"; # ie1 ie2 if1 if2"<<endl;
      Standard_Boolean b = TopOpeBRep_GettraceEEFF(ie1,ie2,if1,if2);
      if (b) seteefft(ie1,ie2,if1,if2);
      else   seteefff(ie1,ie2,if1,if2);
    }
  }    
#endif

  return res;
}


//=======================================================================
//function : DumpCurrent
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::DumpCurrent(const Standard_Integer K) const
{
#ifdef DEB
  if      ( myFFDone ) {
    if      ( K == 1 ) myFaceScanner.DumpCurrent(cout);
    else if ( K == 2 ) myFaceExplorer.DumpCurrent(cout);
  }
  else if ( myEEFFDone ) {
    if      ( K == 1 ) myEdgeScanner.DumpCurrent(cout);
    else if ( K == 2 ) myEdgeExplorer.DumpCurrent(cout);
  }
  else if ( myFEDone ) {
    if      ( K == 1 ) myFaceScanner.DumpCurrent(cout);
    else if ( K == 2 ) myEdgeExplorer.DumpCurrent(cout);
  }
  else if ( myEFDone ) {
    if      ( K == 1 ) myEdgeScanner.DumpCurrent(cout);
    else if ( K == 2 ) myFaceExplorer.DumpCurrent(cout);
  }
  else if ( myEEDone ) {
    if      ( K == 1 ) myEdgeScanner.DumpCurrent(cout);
    else if ( K == 2 ) myEdgeExplorer.DumpCurrent(cout);
  }
#endif
}

//=======================================================================
//function : Index
//purpose  : 
//=======================================================================

Standard_Integer TopOpeBRep_ShapeIntersector::Index
(const Standard_Integer K)const
{
  Standard_Integer i = 0;
#ifdef DEB
  if      ( myFFDone ) {
    if      ( K == 1 ) i = myFaceScanner.Index();
    else if ( K == 2 ) i = myFaceExplorer.Index();
  }
  else if ( myEEFFDone ) {
    if      ( K == 1 ) i = myEdgeScanner.Index();
    else if ( K == 2 ) i = myEdgeExplorer.Index();
  }
  else if ( myFEDone ) {
    if      ( K == 1 ) i = myFaceScanner.Index();
    else if ( K == 2 ) i = myEdgeExplorer.Index();
  }
  else if ( myEFDone ) {
    if      ( K == 1 ) i = myEdgeScanner.Index();
    else if ( K == 2 ) i = myFaceExplorer.Index();
  }
  else if ( myEEDone ) {
    if      ( K == 1 ) i = myEdgeScanner.Index();
    else if ( K == 2 ) i = myEdgeExplorer.Index();
  }
#endif
  return i;
}


//=======================================================================
//function : NextIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::NextIntersection()
{
  myIntersectionDone = Standard_False;

  if (myFFSameDomain) {
    // precedant etat du More() : 2 faces samedomain
    myFFDone = Standard_False;
    myFFSameDomain = Standard_False;
    InitEEFFIntersection();
    FindEEFFIntersection();
    if ( !myIntersectionDone ) {
      NextFFCouple();
      FindFFIntersection();
    }
  }
  else if (myFFDone) {
    NextFFCouple();
    FindFFIntersection();
  }
  else if ( myEEFFDone ) {
    NextEEFFCouple();
    FindEEFFIntersection();
    if ( !myIntersectionDone ) {
      NextFFCouple();
      FindFFIntersection();
    }
  }
  else if ( myFEDone ) {
    NextFECouple();
    FindFEIntersection();
  }
  else if ( myEFDone ) {
    NextEFCouple();
    FindEFIntersection();
  }
   else if ( myEEDone ) {
    NextEECouple();
    FindEEIntersection();
  }

  if ( !myIntersectionDone ) {
    InitFFIntersection();
  }
    
  if ( !myIntersectionDone ) {
    InitFEIntersection();
  }
    
  if ( !myIntersectionDone ) {
    InitEFIntersection();
  }

  if ( !myIntersectionDone ) {
    if ( !myEEFace1.IsNull() && !myEEFace2.IsNull() ) {
      InitEEIntersection();
    }
  }

}


// ========
// FFFFFFFF
// ========


//=======================================================================
//function : InitFFIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::InitFFIntersection()
{
 if ( !myFFInit) { 
   TopAbs_ShapeEnum tscann = TopAbs_FACE;
   TopAbs_ShapeEnum texplo = TopAbs_FACE;
   myFaceScanner.Clear();
   myFaceScanner.AddBoxesMakeCOB(myShape1,tscann);
   myFaceExplorer.Init(myShape2,texplo);
   myFaceScanner.Init(myFaceExplorer);
   FindFFIntersection();
 }
 myFFInit = Standard_True;
}


//=======================================================================
//function : FindFFIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::FindFFIntersection()
{
  myFFDone = Standard_False;
  myFFSameDomain = Standard_False ;

  while ( MoreFFCouple() ) {

    // The two candidate intersecting GeomShapes GS1,GS2 and their types t1,t2
    const TopoDS_Shape& GS1 = myFaceScanner.Current();
    const TopoDS_Shape& GS2 = myFaceExplorer.Current();

#ifdef DEB
    SAVFFi1 = myFaceScanner.Index(); SAVFFi2 = myFaceExplorer.Index(); 
    if (TopOpeBRep_GettraceSI()) {
      cout<<"?? FF : ";
      myFaceScanner.DumpCurrent(cout); myFaceExplorer.DumpCurrent(cout);
      cout<<endl;
    }    
#endif

    const TopOpeBRepTool_BoxSort& BS = myFaceScanner.BoxSort();
    const Bnd_Box& B1 = BS.Box(GS1);
    const Bnd_Box& B2 = BS.Box(GS2);
    myFFIntersector.Perform(GS1,GS2,B1,B2);
    Standard_Boolean ok = myFFIntersector.IsDone(); //xpu210998
    if (!ok) {
      NextFFCouple();
      continue;
    }

    myFFSameDomain = myFFIntersector.SameDomain();

    if (myFFSameDomain) {
      myFFDone = Standard_True;
      break;
    }
    else {
      myFFDone = ! (myFFIntersector.IsEmpty());
      
      // update face/face intersection tolerances
      if (myFFDone) {
	Standard_Real tol1,tol2;
	myFFIntersector.GetTolerances(tol1,tol2);
	myTol1 = Max(myTol1,tol1); myTol2 = Max(myTol2,tol2);
      }
      
      if ( myFFDone ) {
	break;
      }
    }

    NextFFCouple();
  }

  SetIntersectionDone();
}


//=======================================================================
//function : MoreFFCouple
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRep_ShapeIntersector::MoreFFCouple() const
{
  Standard_Boolean more1 = myFaceScanner.More();
  Standard_Boolean more2 = myFaceExplorer.More();
  return (more1 && more2);
}


//=======================================================================
//function : NextFFCouple
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::NextFFCouple()
{
  myFaceScanner.Next();
  Standard_Boolean b1,b2;

  b1 = (!myFaceScanner.More());
  b2 = (myFaceExplorer.More());
  while ( b1 && b2 ) {
    myFaceExplorer.Next();
    myFaceScanner.Init(myFaceExplorer);
    b1 = (!myFaceScanner.More());
    b2 = (myFaceExplorer.More());
  }

}


// ========
// EEFFEEFF
// ========


//=======================================================================
//function : InitEEFFIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::InitEEFFIntersection()
{
  // prepare exploration of the edges of the two current SameDomain faces 
  TopoDS_Shape face1 = myFaceScanner.Current(); // -26-08-96
  TopoDS_Shape face2 = myFaceExplorer.Current(); // -26-08-96

#ifdef DEB
  if (TopOpeBRep_GetcontextFFOR()) {
    face1.Orientation(TopAbs_FORWARD); //-05/07
    face2.Orientation(TopAbs_FORWARD); //-05/07
    cout<<"ctx : InitEEFFIntersection : faces FORWARD"<<endl;
  }
#endif

  const TopOpeBRepTool_BoxSort& BS = myFaceScanner.BoxSort();
  const Bnd_Box& B1 = BS.Box(face1);
  const Bnd_Box& B2 = BS.Box(face2);
  myEEIntersector.SetFaces(face1,face2,B1,B2);
  
  TopAbs_ShapeEnum tscann = TopAbs_EDGE;
  TopAbs_ShapeEnum texplo = TopAbs_EDGE;
  myEdgeScanner.Clear();
  myEdgeScanner.AddBoxesMakeCOB(face1,tscann);
  myEdgeExplorer.Init(face2,texplo);
  myEdgeScanner.Init(myEdgeExplorer);
  
  myEEFFInit = Standard_True;
}


//=======================================================================
//function : FindEEFFIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::FindEEFFIntersection()
{
  myEEFFDone = Standard_False;
  while ( MoreEEFFCouple() ) {
    const TopoDS_Shape& GS1 = myEdgeScanner.Current();
    const TopoDS_Shape& GS2 = myEdgeExplorer.Current();
    myEEIntersector.Perform(GS1,GS2);

#ifdef DEB
    if (TopOpeBRep_GettraceSI() && myEEIntersector.IsEmpty()) {
      cout<<"    EE : ";
      myEdgeScanner.DumpCurrent(cout);
      myEdgeExplorer.DumpCurrent(cout);
      cout<<"(EE of FF SameDomain)";
      cout<<" : EMPTY INTERSECTION";
      cout<<endl;
    }    
#endif

    myEEFFDone = ! (myEEIntersector.IsEmpty());
    if (myEEFFDone) break;
    else NextEEFFCouple();
  }
  SetIntersectionDone();
}


//=======================================================================
//function : MoreEEFFCouple
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRep_ShapeIntersector::MoreEEFFCouple() const
{
  Standard_Boolean more1 = myEdgeScanner.More();
  Standard_Boolean more2 = myEdgeExplorer.More();
  return (more1 && more2);
}


//=======================================================================
//function : NextEEFFCouple
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::NextEEFFCouple()
{
  myEdgeScanner.Next();
  while ( ! myEdgeScanner.More() && myEdgeExplorer.More() ) {
    myEdgeExplorer.Next();
    myEdgeScanner.Init(myEdgeExplorer);
  }
}


// ========
// FEFEFEFE
// ========


//=======================================================================
//function : InitFEIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::InitFEIntersection()
{
 if ( !myFEInit) { 
   TopAbs_ShapeEnum tscann = TopAbs_FACE;
   TopAbs_ShapeEnum texplo = TopAbs_EDGE;
   myFaceScanner.Clear(); 
   myFaceScanner.AddBoxesMakeCOB(myShape1,tscann);
   myEdgeExplorer.Init(myShape2,texplo,TopAbs_FACE); // NYI defaut de spec
   myFaceScanner.Init(myEdgeExplorer);
   FindFEIntersection();
 }
 myFEInit = Standard_True;
}


//=======================================================================
//function : FindFEIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::FindFEIntersection()
{
  myFEDone = Standard_False;
  while ( MoreFECouple() ) {
    const TopoDS_Shape& GS1 = myFaceScanner.Current();
    const TopoDS_Shape& GS2 = myEdgeExplorer.Current();
    myFEIntersector.Perform(GS1,GS2);
    myFEDone = ! (myFEIntersector.IsEmpty());
    if (myFEDone) break;
    else NextFECouple();
  }
  SetIntersectionDone();
}


//=======================================================================
//function : MoreFECouple
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRep_ShapeIntersector::MoreFECouple() const
{
  Standard_Boolean more1 = myFaceScanner.More();
  Standard_Boolean more2 = myEdgeExplorer.More();
  return (more1 && more2);
}


//=======================================================================
//function : NextFECouple
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::NextFECouple()
{
  myFaceScanner.Next();
  while ( ! myFaceScanner.More() && myEdgeExplorer.More() ) {
    myEdgeExplorer.Next();
    myFaceScanner.Init(myEdgeExplorer);
  }
}


// ========
// EFEFEFEF
// ========


//=======================================================================
//function : InitEFIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::InitEFIntersection()
{
 if ( !myEFInit) { 
   TopAbs_ShapeEnum tscann = TopAbs_EDGE;
   TopAbs_ShapeEnum texplo = TopAbs_FACE;
   myEdgeScanner.Clear(); 
   myEdgeScanner.AddBoxesMakeCOB(myShape1,tscann, TopAbs_FACE); // NYI defaut de spec
   myFaceExplorer.Init(myShape2,texplo);
   myEdgeScanner.Init(myFaceExplorer);
   FindEFIntersection();
 }
 myEFInit = Standard_True;
} 
 
//=======================================================================
//function : FindEFIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::FindEFIntersection()
{
  myEFDone = Standard_False;
  while ( MoreEFCouple() ) {
    const TopoDS_Shape& GS1 = myEdgeScanner.Current();
    const TopoDS_Shape& GS2 = myFaceExplorer.Current();
    myFEIntersector.Perform(GS2,GS1);
    myEFDone = ! (myFEIntersector.IsEmpty());
    if (myEFDone) break;
    else NextEFCouple();
  }
  SetIntersectionDone();
}


//=======================================================================
//function : MoreEFCouple
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRep_ShapeIntersector::MoreEFCouple() const
{
  Standard_Boolean more1 = myEdgeScanner.More();
  Standard_Boolean more2 = myFaceExplorer.More();
  return (more1 && more2);
}


//=======================================================================
//function : NextEFCouple
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::NextEFCouple()
{
  myEdgeScanner.Next();
  while ( ! myEdgeScanner.More() && myFaceExplorer.More() ) {
    myFaceExplorer.Next();
    myEdgeScanner.Init(myFaceExplorer);
  }
}

// ========
// EEEEEEEE
// ========

//=======================================================================
//function : InitEEIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::InitEEIntersection()
{
  if ( ! myEEInit ) {
    TopoDS_Shape face1 = myEEFace1.Oriented(TopAbs_FORWARD);
    TopoDS_Shape face2 = myEEFace2.Oriented(TopAbs_FORWARD);
    const TopOpeBRepTool_BoxSort& BS = myFaceScanner.BoxSort();
    const Bnd_Box& B1 = BS.Box(face1);
    const Bnd_Box& B2 = BS.Box(face2);
    myEEIntersector.SetFaces(face1,face2,B1,B2);

    TopAbs_ShapeEnum tscann = TopAbs_EDGE;
    TopAbs_ShapeEnum texplo = TopAbs_EDGE;
    myEdgeScanner.Clear();
    myEdgeScanner.AddBoxesMakeCOB(myShape1,tscann);
    myEdgeExplorer.Init(myShape2,texplo);
    myEdgeScanner.Init(myEdgeExplorer);
    FindEEIntersection();
  }
  myEEInit = Standard_True;
}


//=======================================================================
//function : FindEEIntersection
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::FindEEIntersection()
{
  myEEDone = Standard_False;
  while ( MoreEECouple() ) {
    const TopoDS_Shape& GS1 = myEdgeScanner.Current();
    const TopoDS_Shape& GS2 = myEdgeExplorer.Current();
    myEEIntersector.Perform(GS1,GS2);
    myEEDone = ! (myEEIntersector.IsEmpty());
    if (myEEDone) break;
    else NextEECouple();
  }
  SetIntersectionDone();
}


//=======================================================================
//function : MoreEECouple
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRep_ShapeIntersector::MoreEECouple() const
{
  Standard_Boolean more1 = myEdgeScanner.More();
  Standard_Boolean more2 = myEdgeExplorer.More();
  return (more1 && more2);
}


//=======================================================================
//function : NextEECouple
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::NextEECouple()
{
  myEdgeScanner.Next();
  while ( ! myEdgeScanner.More() && myEdgeExplorer.More() ) {
    myEdgeExplorer.Next();
    myEdgeScanner.Init(myEdgeExplorer);
  }
}


//=======================================================================
//function : Shape
//purpose  : 
//=======================================================================

const TopoDS_Shape& TopOpeBRep_ShapeIntersector::Shape
  ( const Standard_Integer Index )const
{
  if      ( Index == 1 ) return myShape1;
  else if ( Index == 2 ) return myShape2;

  Standard_Failure::Raise("ShapeIntersector : no shape");
  TopoDS_Shape* bid = new TopoDS_Shape();
  return *bid;
}


//=======================================================================
//function : ChangeFacesIntersector
//purpose  : 
//=======================================================================

TopOpeBRep_FacesIntersector& 
TopOpeBRep_ShapeIntersector::ChangeFacesIntersector()
{ return myFFIntersector; }


//=======================================================================
//function : ChangeEdgesIntersector
//purpose  : 
//=======================================================================

TopOpeBRep_EdgesIntersector& 
TopOpeBRep_ShapeIntersector::ChangeEdgesIntersector()
{ return myEEIntersector; }


//=======================================================================
//function : ChangeFaceEdgeIntersector
//purpose  : 
//=======================================================================

TopOpeBRep_FaceEdgeIntersector& 
TopOpeBRep_ShapeIntersector::ChangeFaceEdgeIntersector()
{ return myFEIntersector; }


//=======================================================================
//function : GetTolerances
//purpose  : 
//=======================================================================

void TopOpeBRep_ShapeIntersector::GetTolerances(Standard_Real& tol1,
						Standard_Real& tol2)const
{ tol1 = myTol1; tol2 = myTol2; }




//=======================================================================
//function : IsHalfSpaceShape
//purpose  : tries to define if one of solids is a half space object
//           returns:
//                    0 - no half spaces ( default )
//                    1 - half space is S1
//                    2 - half space is S2
//=======================================================================
static Standard_Integer OneShapeIsHalfSpace(const TopoDS_Shape& S1,const TopoDS_Shape& S2)
{
  Standard_Integer result = 0;

  if( S1.ShapeType() == TopAbs_SOLID && S2.ShapeType() == TopAbs_SOLID )
    {
      TopExp_Explorer ExpSol1(S1, TopAbs_FACE);
      TopExp_Explorer ExpSol2(S2, TopAbs_FACE);
      Standard_Integer NbFacesSol1 = 0;
      Standard_Integer NbFacesSol2 = 0;

      for(; ExpSol1.More(); ExpSol1.Next() )
	NbFacesSol1++;

      for(; ExpSol2.More(); ExpSol2.Next() )
	NbFacesSol2++;

      if( NbFacesSol1 == 0 || NbFacesSol2 == 0 )  // strange solids!!!
	return result;
      if( NbFacesSol1 == 1 && NbFacesSol2 == 1 )  // both shapes are half spaces ???
	return result;

      if( (NbFacesSol1 == 1 && NbFacesSol2 >= 2) || (NbFacesSol2 == 1 && NbFacesSol1 >= 2) )
	{
	  // if one solid has shell consisted of only a face but other one has valid closed
	  // shell we can detect current boolean operation as operation with half space object.
	  // if shell of second solid is not valid too we cann't detect what kind of objects
	  // we try to perform. in this case we do nothing and just return. 

	  // but before we must avoid shperes, toruses and solids with a face biult on spherical surfaces
	  // of revolution (SSRFS) - solids with shell of one face:
	  // sphere (U: 0, 2PI) (V: -PI/2, PI/2),
	  // torus  (U: 0, 2PI) (V: 0, 2PI).
	  // SSRFS  (U period = (PI), 2PI) (V period = (PI), 2PI)
	  // these solids are not halfspaces.

	  TopExp_Explorer SolidExplorer;
	  TopoDS_Face testFace;

	  if( NbFacesSol1 == 1 )
	    {
	      for( SolidExplorer.Init(S1, TopAbs_FACE); SolidExplorer.More(); SolidExplorer.Next() )
		testFace = TopoDS::Face( SolidExplorer.Current() );
	    }
	  else
	    {
	      for( SolidExplorer.Init(S2, TopAbs_FACE); SolidExplorer.More(); SolidExplorer.Next() )
		testFace = TopoDS::Face( SolidExplorer.Current() );
	    }

	  BRepAdaptor_Surface FSurf( testFace );
	  Standard_Boolean SolidIsSphereOrTorus = Standard_False;
	  
	  if( FSurf.GetType() == GeomAbs_Sphere ||  FSurf.GetType() == GeomAbs_Torus )
	    {
	      Standard_Real minU = FSurf.FirstUParameter();
	      Standard_Real maxU = FSurf.LastUParameter();
	      Standard_Real minV = FSurf.FirstVParameter();
	      Standard_Real maxV = FSurf.LastVParameter();
	      Standard_Boolean yesU = ( Abs(minU - 0.) < 1.e-9 && Abs(maxU - 2*PI) < 1.e-9 );
	      Standard_Boolean yesV = ( FSurf.GetType() == GeomAbs_Sphere ) ?
		( Abs(minV - (-PI/2.)) < 1.e-9 && Abs(maxV - PI/2.) < 1.e-9 ) :
		( Abs(minV - 0.) < 1.e-9 && Abs(maxV - 2*PI) < 1.e-9 );
	      SolidIsSphereOrTorus = ( yesU && yesV );
	    }

	  if( FSurf.GetType() == GeomAbs_SurfaceOfRevolution )
	    {
	      Standard_Boolean areBothPeriodic = ( FSurf.IsUPeriodic() && FSurf.IsVPeriodic() );
	      if( areBothPeriodic )
		{
		  Standard_Boolean yesU = ( Abs(FSurf.UPeriod() - PI) < 1.e-9 || Abs(FSurf.UPeriod() - 2*PI) < 1.e-9 );
		  Standard_Boolean yesV = ( Abs(FSurf.VPeriod() - PI) < 1.e-9 || Abs(FSurf.VPeriod() - 2*PI) < 1.e-9 );
		  SolidIsSphereOrTorus = ( yesU && yesV );
		}
	    }

	  if( SolidIsSphereOrTorus )
	      return result;

	  Standard_Boolean SecondShellOk = Standard_True;
	  TopTools_IndexedDataMapOfShapeListOfShape aMapEF;
	  aMapEF.Clear();
	  Standard_Integer NbEdges = 0, NbFaces = 0, iE = 0;

	  if( NbFacesSol1 == 1 )
	    TopExp::MapShapesAndAncestors(S2, TopAbs_EDGE, TopAbs_FACE, aMapEF);
	  else
	    TopExp::MapShapesAndAncestors(S1, TopAbs_EDGE, TopAbs_FACE, aMapEF);

	  NbEdges = aMapEF.Extent();
	  for( iE = 1; iE <= NbEdges; iE++)
	    {
	      const TopTools_ListOfShape& listFaces = aMapEF.FindFromIndex( iE );
	      NbFaces = listFaces.Extent();
	      if( NbFaces != 2 )
		{
		  SecondShellOk = Standard_False;
		  break;
		}
	    }
	  aMapEF.Clear();

	  if( SecondShellOk )
	    result = (NbFacesSol1 == 1) ? 1 : 2;
	}
      else
	{
	  // ************************** IMPORTANT !!! ************************************
	  // both shells are of more than 2 faces. if both solids have invalid shells
	  // we do nothing and just return. on the other hand if only one shell is valid
	  // currently we should suppose that solid with invalid shell is a half space too,
	  // but this is not always true.
	  //
	  // so this suggestion must be developed carefully. while we don't classify it!
	  // *****************************************************************************
	}
#ifdef DEB
      if( result != 0 )
	cout << "# one of the SOLIDs probably is a HALF SPACE" << endl;
#endif
    }

  return result;
}

//=======================================================================
//function : GetNewSolid
//purpose  : rebuild halfspace solid adding new "face on infinity"
//           to build correct bounding box to classify carefully
//           "rejected shapes".
//=======================================================================
static TopoDS_Solid GetNewSolid(const TopoDS_Shape& S, TopoDS_Face& F)
{
  // "new solid" is a new halfspace solid consists of two faces now: the first face is a face
  // used to build halfspace solid and the second face is a new "face on infinity" specially
  // created to constuct correct bounding box around halfspace solid with bounds more wide than
  // previous one.

  // the following algorithm is used:
  // 1. get face used to build halfspace solid and calculate its normal, Min/Max (U,V) parameters,
  //    four points lying on the surface of the face inside its restrictions, surface, tolerance
  //    and location.
  // 2. define the direction into the halfspace solid and project four points along this direction
  //    on infinite distance. then use those points to create "face on infinity".
  // 3. build new shell with two new faces and new "halfspace solid".
  // 4. return this solid and "face on infinity" to remove it and all its subshapes from the list
  //    of rejected shapes.

  TopExp_Explorer ShapeExplorer;

  TopoDS_Face hsFace;
  
  for( ShapeExplorer.Init(S, TopAbs_FACE); ShapeExplorer.More(); ShapeExplorer.Next() )
    hsFace = TopoDS::Face( ShapeExplorer.Current() );

  BRepAdaptor_Surface ASurf( hsFace );
  
  Standard_Real MinU = ASurf.FirstUParameter();
  Standard_Real MaxU = ASurf.LastUParameter();
  Standard_Real MinV = ASurf.FirstVParameter();
  Standard_Real MaxV = ASurf.LastUParameter();

  Standard_Real MidU = (MaxU + MinU) * 0.5;
  Standard_Real MidV = (MaxV + MinV) * 0.5;

  gp_Pnt MidP;
  gp_Vec SurfDU, SurfDV;
  ASurf.D1( MidU, MidV, MidP, SurfDU, SurfDV );
  
  gp_Vec Normal = SurfDU.Crossed( SurfDV );

  if( hsFace.Orientation() == TopAbs_FORWARD )
    Normal *= -1.e+10;
  else
    Normal *= 1.e+10;

  Standard_Real Pu1 = MinU + Abs( (MaxU - MinU) / 4. );
  Standard_Real Pu2 = MinU + Abs( (MaxU - MinU) / 4. * 3. );
  Standard_Real Pv1 = MinV + Abs( (MaxV - MinV) / 4. );
  Standard_Real Pv2 = MinV + Abs( (MaxV - MinV) / 4. * 3. );

  gp_Pnt P1, P2, P3, P4;
  ASurf.D0( Pu1, Pv1, P1 );
  ASurf.D0( Pu1, Pv2, P2 );
  ASurf.D0( Pu2, Pv1, P3 );
  ASurf.D0( Pu2, Pv2, P4 ); 

  P1.Translate( Normal );
  P2.Translate( Normal );
  P3.Translate( Normal );
  P4.Translate( Normal );

  BRepLib_MakeEdge mke1( P1, P2 );
  BRepLib_MakeEdge mke2( P2, P4 );
  BRepLib_MakeEdge mke3( P4, P3 );
  BRepLib_MakeEdge mke4( P3, P1 );

  TopoDS_Edge e1 = mke1.Edge();
  TopoDS_Edge e2 = mke2.Edge();
  TopoDS_Edge e3 = mke3.Edge();
  TopoDS_Edge e4 = mke4.Edge();

  BRepLib_MakeWire mkw( e1, e2, e3, e4 );
  TopoDS_Wire w = mkw.Wire();

  BRepLib_MakeFace mkf( w );
  TopoDS_Face infFace = mkf.Face();
  
  TopoDS_Shell newShell;
  TopoDS_Solid newSolid;

  BRep_Builder newShellBuilder;
  newShellBuilder.MakeShell( newShell );
  newShellBuilder.Add( newShell, hsFace );
  newShellBuilder.Add( newShell, infFace );

  BRep_Builder newSolidBuilder;
  newSolidBuilder.MakeSolid( newSolid );
  newSolidBuilder.Add( newSolid, newShell );

  F = infFace;
  return newSolid;
}