summaryrefslogtreecommitdiff
path: root/src/BOP/BOP_SolidSolid_1.cxx
blob: 5d8df0844f44224d75f17e394eb9a78cb053bf6c (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
// File:	BOP_SolidSolid_1.cxx
// Created:	Tue Jun 29 12:29:57 2004
// Author:	Mikhail KLOKOV
//		<mkk@kurox>


#include <BOP_SolidSolid.ixx>

#include <TColStd_IndexedMapOfInteger.hxx>
#include <TColStd_MapOfInteger.hxx>

#include <TopoDS.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_Solid.hxx>
#include <TopoDS_Compound.hxx>

#include <BooleanOperations_ShapesDataStructure.hxx>
#include <BooleanOperations_StateOfShape.hxx>
#include <BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger.hxx>

#include <BOPTools_SSInterference.hxx>
#include <BOPTools_InterferencePool.hxx>
#include <BOPTools_CArray1OfSSInterference.hxx>
#include <BOPTools_PaveFiller.hxx>
#include <BOPTools_DSFiller.hxx>
#include <BOPTools_PaveBlock.hxx>
#include <BOPTools_CommonBlock.hxx>
#include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
#include <BOPTools_ListIteratorOfListOfCommonBlock.hxx>
#include <BOPTools_Curve.hxx>

#include <BOPTools_Tools3D.hxx>

#include <IntTools_Context.hxx>

#include <BOP_BuilderTools.hxx>

#include <TColStd_ListIteratorOfListOfInteger.hxx>
#include <TColStd_DataMapOfIntegerListOfInteger.hxx>

#include <TopTools_DataMapOfShapeInteger.hxx>
#include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
#include <BOPTools_StateFiller.hxx>
#include <gp_Dir.hxx>
#include <BRep_Builder.hxx>

static
Standard_Integer GetIndex(const TopoDS_Shape& theShape, 
			  const BooleanOperations_ShapesDataStructure& theDS);

static
void GetAttachedFaces(const Standard_Integer   theEdgeIndex,
		      const Standard_Integer   theFaceIndex,
		      const BOPTools_DSFiller&        theDSFiller,
		      TColStd_ListOfInteger&   theListOfFaces);

static
void GetStatesOfAdjacentFaces(const TColStd_ListOfInteger& theListOfFacesToCheck,
			      const BOPTools_DSFiller&     theDSFiller,
			      TColStd_MapOfInteger&        theMapOfUsedIndices,
			      Standard_Boolean&            bFoundINOUT,
			      Standard_Boolean&            bFoundININ,
			      Standard_Boolean&            bFoundOUTOUT);

static
Standard_Boolean ComputeStateForAnalyticalSurfaces(const Standard_Integer theFaceIndex,
						   const Standard_Integer theBaseFaceIndex,
						   const BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& theFFMap,
						   const BOPTools_DSFiller& theDSFiller,
						   TopAbs_State& theState);

static 
Standard_Boolean IsEdgeValidForFace(const Standard_Integer theEdgeIndex,
				    const Standard_Integer theFaceIndex,
				    BOPTools_SSInterference& theFF,
				    const BOPTools_DSFiller& theDSFiller);

//=================================================================================
// function: PrepareFaceSplits
// purpose: 
//=================================================================================
void BOP_SolidSolid::PrepareFaceSplits() 
{
  const BooleanOperations_ShapesDataStructure& aDS = myDSFiller->DS();
  BooleanOperations_ShapesDataStructure*       pDS = (BooleanOperations_ShapesDataStructure*)&aDS;
  BOPTools_InterferencePool* pIntrPool = (BOPTools_InterferencePool*)&myDSFiller->InterfPool();
  BOPTools_CArray1OfSSInterference& aFFs = pIntrPool->SSInterferences();

  Standard_Integer i, aNb, nF1, iRank;
  BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger aFFMap;
  BOP_BuilderTools::DoMap(aFFs, aFFMap);
  TopTools_ListOfShape aListOfNewFaces;

  TColStd_DataMapOfIntegerListOfInteger& aMapOfFaceSplits = myDSFiller->ChangeSplitFacePool();
  
  aNb=aFFMap.Extent();

  for (i=1; i<=aNb; i++) {
    // 

    nF1 = aFFMap.FindKey(i);
    TopoDS_Face aF1 = TopoDS::Face(aDS.Shape(nF1));
    
    iRank   = aDS.Rank(nF1);

    TopTools_DataMapOfShapeInteger aMapOfEdgeIndex;
    TopTools_ListOfShape aListOfSplits;

    if(SplitFace(nF1, aMapOfEdgeIndex, aListOfSplits)) {

      DoInternalVertices(nF1, aListOfSplits);

      TopTools_ListIteratorOfListOfShape aFaceIt(aListOfSplits);

      for(; aFaceIt.More(); aFaceIt.Next()) {
	TopoDS_Shape aShapeF = aFaceIt.Value();
	BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
	pDS->InsertShapeAndAncestorsSuccessors(aShapeF, anASSeq);
	//
	Standard_Integer aNewFaceIndex = pDS->NumberOfInsertedShapes();
	//
	pDS->SetState(aNewFaceIndex, BooleanOperations_ON);

	if(!aMapOfFaceSplits.IsBound(nF1)) {
          TColStd_ListOfInteger thelist;
	  aMapOfFaceSplits.Bind(nF1, thelist);
	}
	aMapOfFaceSplits.ChangeFind(nF1).Append(aNewFaceIndex);

	TopAbs_State aState = TopAbs_ON;
	Standard_Boolean bFoundFaceState = Standard_False;

	if(PropagateFaceStateByEdges(aShapeF, aMapOfEdgeIndex, aState)) {

	  if(aState != TopAbs_ON) { // can not determine correctly ON state
	    BooleanOperations_StateOfShape aConvertedState = BOPTools_StateFiller::ConvertState(aState);

	    pDS->SetState(aNewFaceIndex, aConvertedState);

	    bFoundFaceState = Standard_True;
	  }
	}

	if(!bFoundFaceState) {
	  // 
	  if(ComputeStateByInsidePoints(aNewFaceIndex, nF1, iRank, aFFMap, aState)) {

	    if(aState != TopAbs_ON) {
	      BooleanOperations_StateOfShape aConvertedState = BOPTools_StateFiller::ConvertState(aState);

	      pDS->SetState(aNewFaceIndex, aConvertedState);
	    }
	    bFoundFaceState = Standard_True;
	  }
	}

	if(!bFoundFaceState) {

	  if(ComputeStateForAnalyticalSurfaces(aNewFaceIndex, nF1, aFFMap, *myDSFiller, aState)) {
	    if(aState != TopAbs_ON) {
	      BooleanOperations_StateOfShape aConvertedState = BOPTools_StateFiller::ConvertState(aState);

	      pDS->SetState(aNewFaceIndex, aConvertedState);
	      bFoundFaceState = Standard_True;
	    }
	  }
	}
      }
    }
  }
  // end for
}

// =====================================================================================================================
//  function: PropagateFaceStateByEdges
//  purpose:
// =====================================================================================================================
Standard_Boolean BOP_SolidSolid::PropagateFaceStateByEdges(const TopoDS_Shape& theFace,
							   const TopTools_DataMapOfShapeInteger& theMapOfEdgeIndex,
							   TopAbs_State& theState) 
{
  TopAbs_State aState = TopAbs_UNKNOWN;

  const BooleanOperations_ShapesDataStructure& aDS              = myDSFiller->DS();

  if(theFace.IsNull() || (theFace.ShapeType() != TopAbs_FACE))
    return Standard_False;
  TopoDS_Face aF1 = TopoDS::Face(theFace);

  Standard_Boolean bFoundNotON = Standard_False;
  BooleanOperations_StateOfShape aFoundState = BooleanOperations_ON;
  Standard_Boolean bIsINOUT = Standard_False;
  
  TopExp_Explorer anExpE(aF1, TopAbs_EDGE);

  for(; anExpE.More(); anExpE.Next()) {
    const TopoDS_Shape& anEdge = anExpE.Current();

    Standard_Integer nE = 0;

    if(theMapOfEdgeIndex.IsBound(anEdge)) {
      nE = theMapOfEdgeIndex(anEdge);
    }
    else {
      nE = aDS.ShapeIndex(anEdge, 1);
      nE = (nE == 0) ? aDS.ShapeIndex(anEdge, 2) : nE;
    }

    if(nE == 0)
      continue;

    BooleanOperations_StateOfShape anEdgeState = aDS.GetState(nE);

    if((anEdgeState == BooleanOperations_IN) ||
       (anEdgeState == BooleanOperations_OUT)) {

      if(!bFoundNotON) {
	bFoundNotON = Standard_True;
	aFoundState = anEdgeState;
      }
	
      if(aFoundState != anEdgeState) {
	bIsINOUT = Standard_True;
	break;
      }
    }
  }

  if(!bIsINOUT && bFoundNotON) {
    if(aFoundState == BooleanOperations_IN)
      aState = TopAbs_IN;
    else if(aFoundState == BooleanOperations_OUT)
      aState = TopAbs_OUT;     
  }

  if(aState == TopAbs_UNKNOWN)
    return Standard_False;

  theState = aState;

  return Standard_True;
}

// =====================================================================================================================
//  function: ComputeStateByInsidePoints
//  purpose:
// =====================================================================================================================
Standard_Boolean BOP_SolidSolid::ComputeStateByInsidePoints(const Standard_Integer theFaceIndex,
							    const Standard_Integer theBaseFaceIndex,
							    const Standard_Integer theFaceRank,
							    const BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& theFFMap,
							    TopAbs_State& theState) 

{
  TopAbs_State aState = TopAbs_ON;
  const BooleanOperations_ShapesDataStructure& aDS = myDSFiller->DS();
  BOPTools_InterferencePool* pIntrPool=(BOPTools_InterferencePool*)&myDSFiller->InterfPool();
  const BOPTools_PaveFiller& aPaveFiller = myDSFiller->PaveFiller();
  BOPTools_PaveFiller*       pPaveFiller = (BOPTools_PaveFiller*)&aPaveFiller;
  BOPTools_CArray1OfSSInterference& aFFs=pIntrPool->SSInterferences();

  if(theFaceIndex == 0)
    return Standard_False;

  const TopoDS_Shape& aS = aDS.Shape(theFaceIndex);

  if(aS.IsNull())
    return Standard_False;
  TopoDS_Face aFace = TopoDS::Face(aS);
  //
  //
  Standard_Integer i = 0, j = 0, aNb = 0;
  aNb = theFFMap.Extent();

  for (i=1; i<=aNb; i++) {
    // 
    Standard_Integer nF1 = theFFMap.FindKey(i);

    if(nF1 != theBaseFaceIndex)
      continue;

    const TColStd_IndexedMapOfInteger& aFFIndicesMap=theFFMap.FindFromIndex(i);
    Standard_Integer aNbj = aFFIndicesMap.Extent();

    for (j=1; j<=aNbj; j++) {
      Standard_Integer iFF = aFFIndicesMap(j);
      BOPTools_SSInterference& aFF = aFFs(iFF);
      Standard_Boolean bIsTouchCase = aFF.IsTangentFaces();

      if (bIsTouchCase) {
	Standard_Integer nF2 = aFF.OppositeIndex(nF1);
	const TopoDS_Face& aF2 = TopoDS::Face(aDS.Shape(nF2));

	if(BOPTools_Tools3D::CheckSameDomainFaceInside(aFace, aF2, pPaveFiller->ChangeContext())) {
	  theState = TopAbs_ON;
	  return Standard_True;
	}
      }
    }
  }
  const TopoDS_Shape& aTool = (theFaceRank == 1) ? aDS.Tool() : aDS.Object();
  TopoDS_Solid aRefSolid;

  if(aTool.ShapeType() == TopAbs_SOLID)
    aRefSolid = TopoDS::Solid(aTool);
  else {
    BRep_Builder aBB;
    aBB.MakeSolid(aRefSolid);

    TopExp_Explorer anExpSH(aTool, TopAbs_SHELL);

    for(; anExpSH.More(); anExpSH.Next()) {
      TopoDS_Shape aShell = anExpSH.Current();
      aBB.Add(aShell, aRefSolid);
    }
  }

  if(!BOPTools_Tools3D::ComputeFaceState(aFace, aRefSolid, pPaveFiller->ChangeContext(), aState)) {
    return Standard_False;
  }
  theState = aState;

  return Standard_True;
}

// =====================================================================================================================
//  function: TakeOnSplit
//  purpose:
// =====================================================================================================================
Standard_Boolean BOP_SolidSolid::TakeOnSplit(const Standard_Integer theFaceIndex,
					     const Standard_Integer theBaseFaceIndex) const
{
  Standard_Boolean bTake = Standard_False;

  Standard_Boolean binout = Standard_False;
  Standard_Boolean binin = Standard_False;
  Standard_Boolean boutout  = Standard_False;

  TColStd_MapOfInteger aMapOfUsedIndices;
  TColStd_ListOfInteger aListOfFacesToCheck;
  aListOfFacesToCheck.Append(theFaceIndex);

  GetStatesOfAdjacentFaces(aListOfFacesToCheck, *myDSFiller, aMapOfUsedIndices, binout, binin, boutout);
  
  switch(myOperation) {
  case BOP_FUSE: {
    if(binout || (!binin && !boutout)) {
      bTake = Standard_True;
    }
    break;
  }
  case BOP_COMMON: {
    if(binout || (!binin && !boutout)) {
      bTake = Standard_True;
    }
    break;
  }
  case BOP_CUT: {
    if((binin || boutout) && !binout) {
      bTake = Standard_True;
    }
    break;
  }
  case BOP_CUT21: {
    if((binin || boutout) && !binout) {
      bTake = Standard_True;
    }
    break;
  }
  default: {
    break;
  }
  }
  return bTake;
}

// ------------------------------------------------------------------------------------
// static function: GetIndex
// purpose:
// ------------------------------------------------------------------------------------
Standard_Integer GetIndex(const TopoDS_Shape& theShape, 
			  const BooleanOperations_ShapesDataStructure& theDS)
{
  Standard_Integer anIndex = 0, i = 0;

  anIndex = theDS.ShapeIndex(theShape, 1);
  anIndex = (anIndex == 0) ? theDS.ShapeIndex(theShape, 2) : anIndex;

  if(anIndex == 0) {

    for (i = theDS.NumberOfSourceShapes() + 1; i <= theDS.NumberOfInsertedShapes(); i++) {
      if(theShape.IsSame(theDS.Shape(i))) {
	anIndex = i;
	break;
      }
    }
  }

  return anIndex;
}

// ------------------------------------------------------------------------------------
// static function: GetAttachedFaces
// purpose:
// ------------------------------------------------------------------------------------
void GetAttachedFaces(const Standard_Integer   theEdgeIndex,
		      const Standard_Integer   theFaceIndex,
		      const BOPTools_DSFiller&        theDSFiller,
		      TColStd_ListOfInteger&   theListOfFaces)
{
  theListOfFaces.Clear();
  const BooleanOperations_ShapesDataStructure& aDS = theDSFiller.DS();
  const TColStd_DataMapOfIntegerListOfInteger& aMap = theDSFiller.SplitFacePool();

  Standard_Integer i = 0;

  for(i = 1; i <= aDS.NumberOfInsertedShapes(); i++) {

    if(aDS.GetShapeType(i) == TopAbs_FACE) {
      TColStd_ListOfInteger aListOfFaceIndex;

      if(!aMap.IsBound(i)) {
	if(theFaceIndex == i)
	  continue;
	aListOfFaceIndex.Append(i);
      }
      else {
	TColStd_ListIteratorOfListOfInteger anIttmp(aMap.Find(i));

	for(; anIttmp.More(); anIttmp.Next()) {
	  if(theFaceIndex == anIttmp.Value())
	    continue;
	  aListOfFaceIndex.Append(anIttmp.Value());
	}
      }

      TColStd_ListIteratorOfListOfInteger anIt(aListOfFaceIndex);
      
      for(; anIt.More(); anIt.Next()) {
	if(anIt.Value() <= 0)
	  continue;
	const TopoDS_Shape& aFace = aDS.Shape(anIt.Value());
	TopExp_Explorer anExpE(aFace, TopAbs_EDGE);

	for(; anExpE.More(); anExpE.Next()) {
	  const TopoDS_Shape& anEdge = anExpE.Current();
	  Standard_Integer nE = GetIndex(anEdge, aDS);

	  if(theEdgeIndex == nE) {
	    theListOfFaces.Append(anIt.Value());
	    break;
	  }
	}
      }
    }
  }  
}

// ------------------------------------------------------------------------------------
// static function: GetStatesOfAdjacentFaces
// purpose:
// ------------------------------------------------------------------------------------
void GetStatesOfAdjacentFaces(const TColStd_ListOfInteger& theListOfFacesToCheck,
			      const BOPTools_DSFiller&     theDSFiller,
			      TColStd_MapOfInteger&        theMapOfUsedIndices,
			      Standard_Boolean&            bFoundINOUT,
			      Standard_Boolean&            bFoundININ,
			      Standard_Boolean&            bFoundOUTOUT)
{

  const BooleanOperations_ShapesDataStructure& aDS = theDSFiller.DS();
  TColStd_ListOfInteger aLisOfON;
  TColStd_ListIteratorOfListOfInteger anItF(theListOfFacesToCheck);

  for(; anItF.More(); anItF.Next()) {
    Standard_Integer nF = anItF.Value();

    if(theMapOfUsedIndices.Contains(nF)) {
      continue;
    }
    theMapOfUsedIndices.Add(nF);

    TopoDS_Shape aFace = aDS.Shape(nF);

    TopExp_Explorer anExpE(aFace, TopAbs_EDGE);

    for(; anExpE.More(); anExpE.Next()) {
      const TopoDS_Shape& anEdge = anExpE.Current();
      Standard_Integer nE = 0;
      nE = GetIndex(anEdge, aDS);

      if(nE <= 0)
	continue;

      if(theMapOfUsedIndices.Contains(nE))
	continue;
      theMapOfUsedIndices.Add(nE);
      TColStd_ListOfInteger aListOfFaces, aListOfIN, aListOfOUT;
      GetAttachedFaces(nE, nF, theDSFiller, aListOfFaces);
    
      TColStd_ListIteratorOfListOfInteger anIt(aListOfFaces);

      for(; anIt.More(); anIt.Next()) {
	if(theMapOfUsedIndices.Contains(anIt.Value()))
	    continue;

	// 	if((aDS.GetState(anIt.Value()) != BooleanOperations_IN) &&
	// 	   (aDS.GetState(anIt.Value()) != BooleanOperations_OUT))
	if(aDS.GetState(anIt.Value()) == BooleanOperations_ON)
	  aLisOfON.Append(anIt.Value());

	if(aDS.GetState(anIt.Value()) == BooleanOperations_IN)
	  aListOfIN.Append(anIt.Value());
	else if(aDS.GetState(anIt.Value()) == BooleanOperations_OUT)
	  aListOfOUT.Append(anIt.Value());
      }
      bFoundINOUT  = bFoundINOUT || (!aListOfIN.IsEmpty() && !aListOfOUT.IsEmpty());
      bFoundININ   = bFoundININ  || (!aListOfIN.IsEmpty() && aListOfOUT.IsEmpty());
      bFoundOUTOUT = bFoundOUTOUT || (aListOfIN.IsEmpty() && !aListOfOUT.IsEmpty());
    }
  }

  if(!aLisOfON.IsEmpty() && (theMapOfUsedIndices.Extent() <= aDS.NumberOfInsertedShapes())) {
    GetStatesOfAdjacentFaces(aLisOfON, theDSFiller, theMapOfUsedIndices, bFoundINOUT, bFoundININ, bFoundOUTOUT);
  }
}

// ------------------------------------------------------------------------------------
// static function: ComputeStateForAnalyticalSurfaces
// purpose:
// ------------------------------------------------------------------------------------
Standard_Boolean ComputeStateForAnalyticalSurfaces(const Standard_Integer theFaceIndex,
						   const Standard_Integer theBaseFaceIndex,
						   const BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& theFFMap,
						   const BOPTools_DSFiller& theDSFiller,
						   TopAbs_State& theState)
{
  TopAbs_State aState = TopAbs_ON;
  const BooleanOperations_ShapesDataStructure& aDS = theDSFiller.DS();
  BOPTools_InterferencePool* pIntrPool=(BOPTools_InterferencePool*)&theDSFiller.InterfPool();
  const BOPTools_PaveFiller& aPaveFiller = theDSFiller.PaveFiller();
  BOPTools_PaveFiller*       pPaveFiller = (BOPTools_PaveFiller*)&aPaveFiller;
  BOPTools_CArray1OfSSInterference& aFFs=pIntrPool->SSInterferences();

  if(theFaceIndex == 0)
    return Standard_False;

  const TopoDS_Shape& aS = aDS.Shape(theFaceIndex);

  if(aS.IsNull())
    return Standard_False;
  TopoDS_Face aFace = TopoDS::Face(aS);

  Standard_Integer j = 0;
  Standard_Boolean bFound = Standard_False;

  if (theFFMap.Contains(theBaseFaceIndex)) {
    // 
    Standard_Integer nF1 = theBaseFaceIndex;

    const TColStd_IndexedMapOfInteger& aFFIndicesMap=theFFMap.FindFromKey(theBaseFaceIndex);
    Standard_Integer aNbj = aFFIndicesMap.Extent();

    for (j=1; (!bFound) && (j<=aNbj); j++) {
      Standard_Integer iFF = aFFIndicesMap(j);
      BOPTools_SSInterference& aFF = aFFs(iFF);
      Standard_Boolean bIsTouchCase = aFF.IsTangentFaces();

      if (!bIsTouchCase) {
	Standard_Integer nF2 = aFF.OppositeIndex(nF1);
	const TopoDS_Face& aF2 = TopoDS::Face(aDS.Shape(nF2));
	//
	TopExp_Explorer anExpE(aFace, TopAbs_EDGE);

	for(; anExpE.More(); anExpE.Next()) {
	  TopoDS_Edge aSp = TopoDS::Edge(anExpE.Current());

	  Standard_Boolean bTestEdge = Standard_False;

	  Standard_Integer  nE = GetIndex(aSp, aDS);
	  bTestEdge = IsEdgeValidForFace(nE, nF2, aFF, theDSFiller);

	  if(bTestEdge) {
	    TopAbs_State aStPF = TopAbs_ON;

	    Standard_Boolean bAnalytic = Standard_False;
	    Standard_Real aTolTangent, aTolR;
	    //
	    aTolTangent=0.002;
	    aTolR=0.0000001;

	    bAnalytic = BOPTools_Tools3D::TreatedAsAnalytic(aF2, aSp, aFace, 
							    aTolTangent, aTolR, 
							    aStPF, pPaveFiller->ChangeContext());
	    if(bAnalytic) {
	      aState = aStPF;
	      bFound = Standard_True;
	      break;
	    }
	    else {
	      gp_Dir aDBF1, aDNF2;

	      BOPTools_Tools3D::GetBiNormal (aSp, aFace, aDBF1);
	      BOPTools_Tools3D::GetNormalToFaceOnEdge (aSp, aF2, aDNF2);
	      
	      Standard_Real aTolScPr, aScPr;
	    
	      aTolScPr=1.e-7;
	      aScPr=aDBF1*aDNF2;

	      if (fabs(aScPr) > aTolScPr) {
		aStPF=TopAbs_OUT;
	      
		if (aScPr<0.) {
		  aStPF=TopAbs_IN;
		}
		aState = aStPF;
		bFound = Standard_True;
		break;
	      }
	    }
	  }
	  // end if(bTestEdge)
	}
      }
    }
  }

  if(!bFound) {
    return Standard_False;
  }
  theState = aState;
  return Standard_True;
}

// ------------------------------------------------------------------------------------
// static function: IsEdgeValidForFace
// purpose:
// ------------------------------------------------------------------------------------
Standard_Boolean IsEdgeValidForFace(const Standard_Integer theEdgeIndex,
				    const Standard_Integer theFaceIndex,
				    BOPTools_SSInterference& theFF,
				    const BOPTools_DSFiller& theDSFiller)
{

  const BOPTools_PaveFiller&                   aPaveFiller      = theDSFiller.PaveFiller();
  BOPTools_PaveFiller*                         pPaveFiller      = (BOPTools_PaveFiller*)&aPaveFiller;
  BOPTools_CommonBlockPool&                    aCBPool          = pPaveFiller->ChangeCommonBlockPool();

  BOPTools_SequenceOfCurves& aSCvs=theFF.Curves();
  Standard_Integer aNbCurves=aSCvs.Length();
  Standard_Integer i = 0;

  for (i=1; i<=aNbCurves; i++) {
    BOPTools_Curve& aBC=aSCvs(i);
    const BOPTools_ListOfPaveBlock& aSectEdges=aBC.NewPaveBlocks();

    BOPTools_ListIteratorOfListOfPaveBlock aPBIt(aSectEdges);

    for (; aPBIt.More(); aPBIt.Next()) {
      BOPTools_PaveBlock& aPB = aPBIt.Value();

      if(theEdgeIndex == aPB.Edge())
	return Standard_True;
    }
  }

  
  for(i = 1; i <= aCBPool.Length(); i++) {
    const BOPTools_ListOfCommonBlock& aCBList = aCBPool.Value(i);
    BOPTools_ListIteratorOfListOfCommonBlock anItCB(aCBList);

    for (; anItCB.More(); anItCB.Next()) {
      BOPTools_CommonBlock& aCB=anItCB.Value();
      Standard_Integer nFace = aCB.Face();

      if(nFace == theFaceIndex) {
	const BOPTools_PaveBlock& aPB1 = aCB.PaveBlock1();
	const BOPTools_PaveBlock& aPB2 = aCB.PaveBlock2();

	if((theEdgeIndex == aPB1.Edge()) ||
	   (theEdgeIndex == aPB2.Edge())) {
	  return Standard_True;
	}
      }
    }
  }
  

  return Standard_False;
}