summaryrefslogtreecommitdiff
path: root/src/TopOpeBRepTool/TopOpeBRepTool_ShapeClassifier.cxx
blob: e3e44d9925492033e6582e36f41f68cd10aed33c (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
// File:	TopOpeBRepTool_ShapeClassifier.cxx
// Created:	Tue Feb  1 18:18:54 1994
// Author:	Jean Yves LEBEY
//		<jyl@phobox>

#include <TopOpeBRepTool_ShapeClassifier.ixx>
#include <BRepClass_FaceClassifier.hxx>
#include <BRepTopAdaptor_FClass2d.hxx>
#include <BRep_Tool.hxx>
#include <TopoDS.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <Geom_Curve.hxx>
#include <Geom2d_Curve.hxx>
#include <gp_Vec2d.hxx>
#include <Precision.hxx>
#include <Standard_ProgramError.hxx>
#include <BRepClass_FacePassiveClassifier.hxx>
#include <BRepClass3d_SolidExplorer.hxx>
#include <BRepClass_Edge.hxx>
#include <BRepAdaptor_Curve2d.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <TopOpeBRepTool_CurveTool.hxx>
#include <TopOpeBRepTool_define.hxx>
#include <TopOpeBRepTool_2d.hxx>

//=======================================================================
//function : TopOpeBRepTool_ShapeClassifier
//purpose  : 
//=======================================================================

TopOpeBRepTool_ShapeClassifier::TopOpeBRepTool_ShapeClassifier() :
myP3Ddef(Standard_False),myP2Ddef(Standard_False)
{
}

//=======================================================================
//function : TopOpeBRepTool_ShapeClassifier
//purpose  : 
//=======================================================================

TopOpeBRepTool_ShapeClassifier::TopOpeBRepTool_ShapeClassifier
(const TopoDS_Shape& SRef) :myP3Ddef(Standard_False),myP2Ddef(Standard_False)
{
  myRef = SRef;
}

//=======================================================================
//function : ClearAll
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::ClearAll()
{
  ClearCurrent();
  mySolidClassifier.Clear();
}

//=======================================================================
//function : ClearCurrent
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::ClearCurrent()
{
  mySameDomain = -1;
  myS.Nullify();
  myRef.Nullify();
  myAvS.Nullify();
  myMapAvS.Clear();
  mymre.Clear();
  mymren = 0;
  mymredone = Standard_False;
  myState = TopAbs_UNKNOWN;
  myEdge.Nullify();
  myFace.Nullify();
  myP3Ddef = myP2Ddef = Standard_False;
}

//=======================================================================
//function : SameDomain
//purpose  : 
//=======================================================================

Standard_Integer TopOpeBRepTool_ShapeClassifier::SameDomain() const
{
  return mySameDomain;
}

//=======================================================================
//function : SameDomain
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::SameDomain(const Standard_Integer sam)
{
  mySameDomain = sam;
}

//=======================================================================
//function : SetReference
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::SetReference(const TopoDS_Shape& SRef)
{
  myRef = SRef;
  MapRef();
}

//=======================================================================
//function : MapRef
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::MapRef()
{
  mymre.Clear();  
  mymren = 0;
  if (myRef.ShapeType() == TopAbs_FACE && mySameDomain == 1) {
    TopExp::MapShapes(myRef,TopAbs_EDGE,mymre);
    mymren = mymre.Extent();
    if (mymren == 1) {
      TopExp_Explorer x(myRef,TopAbs_EDGE);
      const TopoDS_Edge& E = TopoDS::Edge(x.Current());
      TopoDS_Vertex v1,v2;TopExp::Vertices(E,v1,v2);
      if (v1.IsSame(v2)) mymren = 0; 
    }
  }
  mymredone = Standard_True;
}
  
//=======================================================================
//function : StateShapeShape
//purpose  : 
//=======================================================================

TopAbs_State TopOpeBRepTool_ShapeClassifier::StateShapeShape
(const TopoDS_Shape& S,const TopoDS_Shape& SRef,const Standard_Integer samedomain)  
{
  ClearCurrent();
  mySameDomain = samedomain;
  myS = S;
  myAvS.Nullify();
  myPAvLS = NULL;
  myRef = SRef;
  Perform();
  return myState;
}

//=======================================================================
//function : StateShapeShape
//purpose  : 
//=======================================================================

TopAbs_State TopOpeBRepTool_ShapeClassifier::StateShapeShape
(const TopoDS_Shape& S,const TopoDS_Shape& AvS,const TopoDS_Shape& SRef)  
{
  ClearCurrent();
  myS = S;
  myAvS = AvS;
  myPAvLS = NULL;
  myRef = SRef;
  Perform();
  return myState;
}

//=======================================================================
//function : StateShapeShape
//purpose  : 
//=======================================================================

TopAbs_State TopOpeBRepTool_ShapeClassifier::StateShapeShape
(const TopoDS_Shape& S, const TopTools_ListOfShape& AvLS,const TopoDS_Shape& SRef)  
{
  ClearCurrent();
  myS = S;
  myAvS.Nullify();
  myPAvLS = (TopTools_ListOfShape*)&AvLS;
  myRef = SRef;
  Perform();
  return myState;
}

//=======================================================================
//function : StateShapeReference
//purpose  : 
//=======================================================================

TopAbs_State TopOpeBRepTool_ShapeClassifier::StateShapeReference
(const TopoDS_Shape& S,const TopoDS_Shape& AvS)
{
  myS = S;
  myAvS = AvS;
  myPAvLS = NULL;
  Perform();
  return myState;
}

//=======================================================================
//function : StateShapeReference
//purpose  : 
//=======================================================================

TopAbs_State TopOpeBRepTool_ShapeClassifier::StateShapeReference
(const TopoDS_Shape& S,const TopTools_ListOfShape& AvLS)
{
  myS = S;
  myAvS.Nullify();
  myPAvLS = (TopTools_ListOfShape*)&AvLS;
  Perform();
  return myState;
}

//=======================================================================
//function : ChangeSolidClassifier
//purpose  : 
//=======================================================================

TopOpeBRepTool_SolidClassifier& TopOpeBRepTool_ShapeClassifier::ChangeSolidClassifier()
{
  return mySolidClassifier;
}

//=======================================================================
//function : FindEdge
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::FindEdge()
{
  myEdge.Nullify();
  myFace.Nullify();

  TopAbs_ShapeEnum t = myS.ShapeType();
  if ( t < TopAbs_FACE ) { // compsolid .. shell
    FindFace(myS);
    FindEdge(myFace);
  }
  else {
    FindEdge(myS);
  }
}

//=======================================================================
//function : FindEdge
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::FindEdge(const TopoDS_Shape& S)
{
  myEdge.Nullify();
  Standard_Boolean isavls = HasAvLS();
  Standard_Boolean isavs = (! myAvS.IsNull());
  if (S.IsNull()) return;

  TopAbs_ShapeEnum tS = S.ShapeType();
  TopExp_Explorer eex;
  if ( ! myFace.IsNull() ) eex.Init(myFace,TopAbs_EDGE);
  else eex.Init(S,TopAbs_EDGE);

  for(; eex.More(); eex.Next()) {
    const TopoDS_Edge& E = TopoDS::Edge(eex.Current());
    Standard_Boolean toavoid = Standard_False;
    if ( isavls || isavs ) {
      toavoid = toavoid || myMapAvS.Contains(E);
      if (!myAvS.IsNull()) toavoid = toavoid || E.IsSame(myAvS);
    }      
    else if ( BRep_Tool::Degenerated(E) ) toavoid = ( tS != TopAbs_EDGE );
    if ( toavoid ) continue;
    myEdge = E;
    break;
  }
}

//=======================================================================
//function : FindFace
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::FindFace(const TopoDS_Shape& S)
{
  myFace.Nullify();
  Standard_Boolean isavls = HasAvLS();
  Standard_Boolean isavs = (! myAvS.IsNull());
  TopExp_Explorer fex(S,TopAbs_FACE);
  for (; fex.More(); fex.Next()) {
    const TopoDS_Face& F = TopoDS::Face(fex.Current());
    Standard_Boolean toavoid = Standard_False;
    if ( isavls || isavs ) { 
      toavoid = toavoid || myMapAvS.Contains(F);
      if (!myAvS.IsNull()) toavoid = toavoid || F.IsSame(myAvS);
    }      
    if ( toavoid ) continue;
    myFace = F;
    break;
  }
}

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

void TopOpeBRepTool_ShapeClassifier::Perform()
{
  myState = TopAbs_UNKNOWN;
  if (myS.IsNull()) return;
  if (myRef.IsNull()) return;

  if (!mymredone) {
    MapRef();
  }

  if ( !myAvS.IsNull() ) {
    // tAvS = FACE,EDGE --> map(AvS,EDGE)
    // rejet des aretes de myAvS comme arete de classification
    // (le rejet simple de myAvS est insuffisant (connexite))
    myMapAvS.Clear();
    TopAbs_ShapeEnum tAvS = myAvS.ShapeType();
    if ( tAvS == TopAbs_FACE ) { 
      myMapAvS.Add(myAvS);
      TopExp::MapShapes(myAvS,TopAbs_EDGE,myMapAvS);
    }
    else if ( tAvS == TopAbs_EDGE ) {
      TopExp::MapShapes(myAvS,TopAbs_EDGE,myMapAvS);
    }
  }
  else if ( HasAvLS() ) {
    // tAvS = FACE,EDGE --> map(AvS,EDGE)
    // rejet des aretes de myPAvLS comme arete de classification
    // (le rejet simple de myPAvLS est insuffisant (connexite))
    myMapAvS.Clear();
    TopAbs_ShapeEnum tAvS = myPAvLS->First().ShapeType();
    if ( tAvS == TopAbs_FACE ) {
      TopTools_ListIteratorOfListOfShape it((*myPAvLS));
      for (; it.More(); it.Next() ) {
	const TopoDS_Shape& S = it.Value();
	myMapAvS.Add(S);
	TopExp::MapShapes(S,TopAbs_EDGE,myMapAvS);
      }
    }
    else if ( tAvS == TopAbs_EDGE ) {
      TopTools_ListIteratorOfListOfShape it((*myPAvLS));
      for (; it.More(); it.Next() ) {
	const TopoDS_Shape& S = it.Value();
	TopExp::MapShapes(S,TopAbs_EDGE,myMapAvS);
      }
    }
  }
  else {
    if ( myS.ShapeType() == TopAbs_FACE ) {
      myP3Ddef = BRepClass3d_SolidExplorer::FindAPointInTheFace
	(TopoDS::Face(myS),myP3D);
    }
  }

  TopAbs_ShapeEnum tS = myS.ShapeType();
  TopAbs_ShapeEnum tR = myRef.ShapeType();

  if ( tS == TopAbs_VERTEX ) {
    if ( tR <= TopAbs_SOLID ) {
      gp_Pnt P3D = BRep_Tool::Pnt(TopoDS::Vertex(myS));
      StateP3DReference(P3D);
    }
  }
  else if ( tS == TopAbs_EDGE ) {
    if ( tR == TopAbs_FACE || tR <= TopAbs_SOLID ) {
      FindEdge();
      StateEdgeReference();
    }
  }
  else if ( tS == TopAbs_WIRE ) {
    if ( tR == TopAbs_FACE || tR <= TopAbs_SOLID ) {
      FindEdge();
      StateEdgeReference();
    }
  }
  else if ( tS == TopAbs_FACE ) {
    if ( tR == TopAbs_FACE ) {
      FindEdge();
      if ( mySameDomain == 1 ) {
        StateEdgeReference();
      }
      else {
	if (!myP3Ddef) {
	  myP3Ddef = BRepClass3d_SolidExplorer::FindAPointInTheFace
	    (TopoDS::Face(myS),myP3D);
	}
	if (myP3Ddef) {
	  StateP3DReference(myP3D);
	}
	else {
	  myState = TopAbs_UNKNOWN;
	  Standard_ProgramError::Raise("TopOpeBRepTool_ShapeClassifier !P3Ddef");
	}
      }
    }
    else if ( tR <= TopAbs_SOLID ) {
      FindEdge();
      if (myP3Ddef) {
	StateP3DReference(myP3D);
      }
      else {
	StateEdgeReference();
      }
    }
  }
  else if ( tS == TopAbs_SHELL ) {
    if ( tR <= TopAbs_SOLID ) {
      FindEdge();
      StateEdgeReference();
    }
  }
  else if ( tS == TopAbs_SOLID ) {
    if ( tR <= TopAbs_SOLID ) {
      FindEdge();
      StateEdgeReference();
    }
  }
  else {
    Standard_ProgramError::Raise("StateShapeShape : bad operands");
  }
  
  // take orientation of reference shape in account
  TopAbs_Orientation oriRef = myRef.Orientation();
  if (oriRef == TopAbs_EXTERNAL || oriRef == TopAbs_INTERNAL ) {
    if (myState == TopAbs_IN) myState = TopAbs_OUT;
  }

}


//=======================================================================
//function : StateEdgeReference
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::StateEdgeReference()
{
  myState = TopAbs_UNKNOWN;

  if(myEdge.IsNull())
    return;
  if(myRef.IsNull())
    return;

  Handle(Geom_Curve) C3D;
  gp_Pnt P3D;
  Standard_Real f3d,l3d;

  Handle(Geom2d_Curve) C2D;
  gp_Pnt2d P2D;
  Standard_Real f2d,l2d,tol2d;
  
  TopAbs_ShapeEnum tR = myRef.ShapeType();
  // myEdge est une arete de myS, pas de myRef
  if( tR == TopAbs_FACE )
    {
      const TopoDS_Face& F = TopoDS::Face(myRef);
      if(mySameDomain)
	{
	  Standard_Boolean trimCurve = Standard_True;
	  C2D = FC2D_CurveOnSurface(myEdge,F,f2d,l2d,tol2d,trimCurve);

	  if(C2D.IsNull())
	    Standard_ProgramError::Raise("StateShapeShape : no 2d curve");

	  Standard_Real t = 0.127956477;
	  Standard_Real p = (1-t)*f2d + t*l2d;
	  P2D = C2D->Value(p);

#ifdef DEB
	  C3D = BRep_Tool::Curve(myEdge,f3d,l3d);
	  if(!C3D.IsNull())
	    P3D = C3D->Value(p);
#endif
	  StateP2DReference(P2D);
	  return;
	}
      else
	{ // myEdge/myRef=face en 3d
	  C3D = BRep_Tool::Curve(myEdge,f3d,l3d);

	  if(C3D.IsNull())
	    Standard_ProgramError::Raise("StateShapeShape : no 3d curve");

	  Standard_Real t = 0.127956477;
	  Standard_Real p = (1-t)*f3d + t*l3d;
	  P3D = C3D->Value(p);
	  StateP3DReference(P3D);
	  return;
	}
    }
  else if( tR <= TopAbs_SOLID )
    {
      Standard_Boolean degen = BRep_Tool::Degenerated(myEdge);
      if( degen )
	{
	  const TopoDS_Vertex& v = TopExp::FirstVertex(myEdge);
	  P3D = BRep_Tool::Pnt(v);
	  StateP3DReference(P3D);
	  return;
	}
      else
	{
	  C3D = BRep_Tool::Curve(myEdge,f3d,l3d);

	  if (C3D.IsNull())
	    Standard_ProgramError::Raise("StateShapeShape : no 3d curve");

	  Standard_Real t = 0.127956477;
	  Standard_Real p = (1-t)*f3d + t*l3d;
	  P3D = C3D->Value(p);
	  StateP3DReference(P3D);
	  return;
	}
    }
  else
    Standard_ProgramError::Raise("StateShapeShape : bad operands");
}


//=======================================================================
//function : StateP2DReference
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::StateP2DReference
(const gp_Pnt2d& P2D)
{
  myState = TopAbs_UNKNOWN;
  if (myRef.IsNull()) return;
  TopAbs_ShapeEnum tR = myRef.ShapeType();

  if ( tR == TopAbs_FACE ) {
    if (mymren == 1) {
      TopExp_Explorer x;
      for(x.Init(myRef,TopAbs_EDGE);x.More();x.Next()) {
//      for(TopExp_Explorer x(myRef,TopAbs_EDGE);x.More();x.Next()) {
	TopAbs_Orientation o = x.Current().Orientation();
//	if      (o == TopAbs_EXTERNAL) myState == TopAbs_OUT;
	if      (o == TopAbs_EXTERNAL) myState = TopAbs_OUT;
//	else if (o == TopAbs_INTERNAL) myState == TopAbs_IN;
	else if (o == TopAbs_INTERNAL) myState = TopAbs_IN;
	else {
#ifdef DEB
	  cout<<"StateP2DReference o<>E,I"<<endl;
#endif
	  break;
	}
      }
    }
    else {
      myP2D = P2D;
      myP2Ddef = Standard_True;
      TopoDS_Face F = TopoDS::Face(myRef);
      F.Orientation(TopAbs_FORWARD);
      Standard_Real  TolClass  = 1e-8;
      BRepTopAdaptor_FClass2d FClass2d(F,TolClass);
      myState = FClass2d.Perform(P2D);
    }
  }
  else {
    Standard_ProgramError::Raise("StateShapeShape : bad operands");
  }
}

//=======================================================================
//function : StateP3DReference
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::StateP3DReference(const gp_Pnt& P3D)
{
  myState = TopAbs_UNKNOWN;
  if (myRef.IsNull()) return;
  TopAbs_ShapeEnum tR = myRef.ShapeType();

  if ( tR == TopAbs_SOLID ) {
    myP3D = P3D;
    myP3Ddef = Standard_True;
    const TopoDS_Solid& SO = TopoDS::Solid(myRef);
    Standard_Real tol3d = Precision::Confusion();
    mySolidClassifier.Classify(SO,P3D,tol3d); 
    myState = mySolidClassifier.State();
  }
  else if ( tR < TopAbs_SOLID ) {
    myP3D = P3D;
    myP3Ddef = Standard_True;
    TopExp_Explorer ex;
    for (ex.Init(myRef,TopAbs_SOLID);ex.More();ex.Next()) {
//    for (TopExp_Explorer ex(myRef,TopAbs_SOLID);ex.More();ex.Next()) {
      const TopoDS_Solid& SO = TopoDS::Solid(ex.Current());
      Standard_Real tol3d = Precision::Confusion();
      mySolidClassifier.Classify(SO,P3D,tol3d); 
      myState = mySolidClassifier.State();
      if (myState == TopAbs_IN || myState == TopAbs_ON) {
	break;
      }
    }
  }
  else {
    Standard_ProgramError::Raise("StateShapeShape : bad operands");
  }
}

//=======================================================================
//function : State
//purpose  : 
//=======================================================================

TopAbs_State TopOpeBRepTool_ShapeClassifier::State() const
{
  return myState;
}

//=======================================================================
//function : P3D
//purpose  : 
//=======================================================================

const gp_Pnt& TopOpeBRepTool_ShapeClassifier::P3D() const
{
  if (myP3Ddef) { 
    return myP3D;
  }
  Standard_ProgramError::Raise("ShapeClassifier::P3D undefined");
  return myP3D;
}

//=======================================================================
//function : P2D
//purpose  : 
//=======================================================================

const gp_Pnt2d& TopOpeBRepTool_ShapeClassifier::P2D() const 
{
  if (myP2Ddef) {
    return myP2D;
  }
  Standard_ProgramError::Raise("ShapeClassifier::P2D undefined");
  return myP2D;
}

//=======================================================================
//function : HasAvLS
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRepTool_ShapeClassifier::HasAvLS() const
{
  Standard_Boolean hasavls = (myPAvLS) ? (!myPAvLS->IsEmpty()) : Standard_False;
  return hasavls;
}

#if 0
//=======================================================================
//function : FindEdge
//purpose  : 
//=======================================================================

void TopOpeBRepTool_ShapeClassifier::FindEdge(const TopoDS_Shape& S)
{
  myEdge.Nullify();
  Standard_Boolean isavs = (! myAvS.IsNull());
  Standard_Boolean isavls = HasAvLS();
  Standard_Boolean isav = (isavs || isavls);

  if (S.IsNull()) return;
  TopAbs_ShapeEnum tS = S.ShapeType();

  TopExp_Explorer eex;
  if ( ! myFace.IsNull() ) eex.Init(myFace,TopAbs_EDGE);
  else eex.Init(S,TopAbs_EDGE);

  for(; eex.More(); eex.Next()) {
    const TopoDS_Edge& E = TopoDS::Edge(eex.Current());
    if ( isav ) {
      Standard_Boolean toavoid = Standard_False;
      if ( isavls ) toavoid = myMapAvS.Contains(E);
      else if ( isavs )	toavoid = E.IsSame(myAvS);      
      if ( toavoid ) continue;
    }
    else if ( BRep_Tool::Degenerated(E) ) {
      if ( tS != TopAbs_EDGE ) continue;
    }
    myEdge = E;
    break;
  }
}

static Standard_Boolean FindAPointInTheFace
(const TopoDS_Face& _face,gp_Pnt& APoint,Standard_Real& u,Standard_Real& v) 
{ 
  TopoDS_Face face=_face;
  face.Orientation(TopAbs_FORWARD);

  TopExp_Explorer     faceexplorer;
  BRepAdaptor_Curve2d c;
  gp_Vec2d T;
  gp_Pnt2d P;
  Standard_Boolean Ok = Standard_False;
  Standard_Integer nbiter=0;
  Standard_Real myParamOnEdge = 0.5;
  do { 
    nbiter++;
    if(myParamOnEdge==0.5)  myParamOnEdge = 0.4;
    else if(myParamOnEdge==0.4)  myParamOnEdge = 0.6; 
    else if(myParamOnEdge==0.6)  myParamOnEdge = 0.3; 
    else if(myParamOnEdge==0.3)  myParamOnEdge = 0.7; 
    else if(myParamOnEdge==0.7)  myParamOnEdge = 0.2; 
    else if(myParamOnEdge==0.2)  myParamOnEdge = 0.8; 
    else if(myParamOnEdge==0.8)  myParamOnEdge = 0.1; 
    else if(myParamOnEdge==0.1)  myParamOnEdge = 0.9;
    else { myParamOnEdge*=0.5; } 
    
    for (faceexplorer.Init(face,TopAbs_EDGE); 
	 faceexplorer.More(); 
	 faceexplorer.Next()) {
      TopoDS_Edge Edge = TopoDS::Edge(faceexplorer.Current());
      c.Initialize(Edge,face);
      Standard_Integer nbinterval = c.NbIntervals(GeomAbs_C1); 
      c.D1((c.LastParameter() - c.FirstParameter()) * myParamOnEdge + c.FirstParameter(),P,T);
      
      Standard_Real x=T.X();
      Standard_Real y=T.Y();
      //-- cout<<"Param:"<<(c.IntervalFirst() + c.IntervalLast()) * param<<" U:"<<P.X()<<" V:"<<P.Y();
      //-- cout<<" tguv x:"<<x<<" , y:"<<y<<endl;
      
      
      if(Edge.Orientation() == TopAbs_FORWARD) { 
	T.SetCoord(-y,x);
      }
      else { 
	T.SetCoord(y,-x);
      }
      
      Standard_Real ParamInit = RealLast();
      Standard_Real TolInit   = 0.00001;
      Standard_Boolean APointExist = Standard_False;
      
      BRepClass_FacePassiveClassifier FClassifier;
      
      T.Normalize();
      P.SetCoord(P.X()+TolInit*T.X(),P.Y()+TolInit*T.Y());
      FClassifier.Reset(gp_Lin2d(P,T),ParamInit,RealEpsilon());   //-- Longueur et Tolerance #######
      
      TopExp_Explorer otherfaceexplorer;
      for (otherfaceexplorer.Init(face,TopAbs_EDGE); 
	   otherfaceexplorer.More(); 
	   otherfaceexplorer.Next()) {
	TopoDS_Edge OtherEdge = TopoDS::Edge(otherfaceexplorer.Current());
	if((OtherEdge.Orientation() == TopAbs_EXTERNAL)) { 
	}
	else { 
	  BRepClass_Edge AEdge(OtherEdge,face);
	  FClassifier.Compare(AEdge,OtherEdge.Orientation());
	  if(FClassifier.ClosestIntersection()) { 
	    //-- cout<<" ---> Edge : "<<FClassifier.Parameter()<<endl;
	    if(ParamInit > FClassifier.Parameter()) { 
	      ParamInit = FClassifier.Parameter();
	      APointExist = Standard_True;
	    }
	  }
	}
      }
      if(APointExist) { 
	ParamInit*=0.5;
	u = P.X() + ParamInit* T.X();
	v = P.Y() + ParamInit* T.Y();
	BRepAdaptor_Surface  s;
	Standard_Boolean computerestriction = Standard_False;
	s.Initialize(face,computerestriction);
	s.D0(u,v,APoint);
	//-- cout<<" u="<<u<<" v="<<v<<"  -> ("<<APoint.X()<<","<<APoint.Y()<<","<<APoint.Z()<<endl;
	return(Standard_True);
      }
    }
  }
  while(nbiter<100);
  return(Standard_False);
}

#endif