summaryrefslogtreecommitdiff
path: root/src/TopOpeBRepTool/TopOpeBRepTool_RegularizeW.cxx
blob: 6ae6baf3914c47e44faf2fc000d635a65926eaef (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
// File:	TopOpeBRepTool_RegularizeW.cxx
// Created:	Wed Dec 23 15:34:12 1998
// Author:      Xuan PHAM PHU
//		<xpu@poulopox.paris1.matra-dtv.fr>


#include <TopOpeBRepTool.hxx>
#include <TopOpeBRepTool_EXPORT.hxx>
#include <TopOpeBRepTool_CLASSI.hxx>
#include <TopOpeBRepTool_REGUW.hxx>
#include <TopOpeBRepTool_TOOL.hxx>
#include <TopOpeBRepTool_define.hxx>
#include <TopExp_Explorer.hxx>
#include <BRep_Tool.hxx>
#include <TopAbs.hxx>
#include <TopoDS.hxx>

#ifdef DRAW
#include <TopOpeBRepTool_DRAW.hxx>
#endif

#define SAME     (-1)
#define DIFF     (-2)
#define UNKNOWN  ( 0)
#define oneINtwo ( 1)
#define twoINone ( 2)

#define M_FORWARD(sta)  (sta == TopAbs_FORWARD)
#define M_REVERSED(sta) (sta == TopAbs_REVERSED)
#define M_INTERNAL(sta) (sta == TopAbs_INTERNAL)
#define M_EXTERNAL(sta) (sta == TopAbs_EXTERNAL)

#ifdef DEB
Standard_EXPORT Standard_Boolean TopOpeBRepTool_GettraceREGUFA(); 
static TopTools_IndexedMapOfShape STATIC_mapw,STATIC_mapf;
static Standard_Integer FUN_adds(const TopoDS_Shape& s) {
  TopAbs_ShapeEnum typ = s.ShapeType();
  TCollection_AsciiString aa; Standard_Integer is = 0;
  if (typ == TopAbs_WIRE)   {aa = TCollection_AsciiString("wi"); is = STATIC_mapw.Add(s); }
  if (typ == TopAbs_FACE)   {aa = TCollection_AsciiString("fa"); is = STATIC_mapf.Add(s); }
#ifdef DRAW 
  FUN_tool_draw(aa,s,is);  
#endif
  return is;
}
Standard_EXPORT void FUN_tool_coutsta(const Standard_Integer& sta, const Standard_Integer& i1, const Standard_Integer& i2)
{
  switch (sta) {
  case SAME:
    cout<<i1<<" gives SAME bnd with "<<i2<<endl; break;
  case DIFF:
    cout<<i1<<" gives  OUT bnd with "<<i2<<endl; break;
  case oneINtwo:
    cout<<i1<<" is IN "<<i2<<endl; break;
  case twoINone:
    cout<<i2<<" is IN "<<i1<<endl; break;
  }
}
#endif

Standard_EXPORT void FUN_addOwlw(const TopoDS_Shape& Ow, const TopTools_ListOfShape& lw, TopTools_ListOfShape& lresu);

//=======================================================================
//function : RegularizeWires
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRepTool::RegularizeWires(const TopoDS_Face& theFace,
				    TopTools_DataMapOfShapeListOfShape& mapoldWnewW,
				    TopTools_DataMapOfShapeListOfShape& ESplits) // (e,esp); esp = splits of e
{
  if (theFace.IsNull()) return Standard_False;
 TopoDS_Shape aLocalShape = theFace.Oriented(TopAbs_FORWARD);  
  TopoDS_Face aFace = TopoDS::Face(aLocalShape);
//  TopoDS_Face aFace = TopoDS::Face(theFace.Oriented(TopAbs_FORWARD));

  TopOpeBRepTool_REGUW REGUW(aFace);
  REGUW.SetOwNw(mapoldWnewW);
  REGUW.SetEsplits(ESplits);

//  Standard_Boolean hasregu = Standard_False;
  TopExp_Explorer exw(aFace, TopAbs_WIRE);
  for (; exw.More(); exw.Next()) {   
    const TopoDS_Shape& W = exw.Current(); 
    REGUW.Init(W);
    Standard_Boolean ok = REGUW.MapS();
    if (!ok) return Standard_False;
    ok = REGUW.SplitEds();
    if (!ok) return Standard_False;
    ok = REGUW.REGU();
    if (!ok) return Standard_False;
  }

  REGUW.GetEsplits(ESplits); 
  REGUW.GetOwNw(mapoldWnewW);
  return Standard_True;
}

//=======================================================================
//function : Regularize
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRepTool::Regularize(const TopoDS_Face& theFace,
			       TopTools_ListOfShape& aListOfFaces,
			       TopTools_DataMapOfShapeListOfShape& ESplits) 
{   
  TopOpeBRepTool_REGUW REGUW(theFace);
  aListOfFaces.Clear();
  TopTools_DataMapOfShapeListOfShape mapoldWnewW;
  Standard_Boolean regu = TopOpeBRepTool::RegularizeWires(theFace,mapoldWnewW,ESplits);
  if (regu) {
    regu = TopOpeBRepTool::RegularizeFace(theFace,mapoldWnewW,aListOfFaces);
  }
  return regu;
}

//**********************************************************************
//   classifying wires
//**********************************************************************

// ------------------------------------------------------------
// ------------------ classifying wires -----------------------

// function : bFgreaterbFF (F,FF)
// purpose  : returns False if <F>'s 3d bounding box smaller than <FF>'s

// function : mkBnd2d (W,F,B2d)
// purpose  : get 2d bounding box <B2d> of wire <W>'s UV  
//            representation on face <F>.

// function : classiBnd2d (B,ismaller)
// purpose  : compare the two 2d bounding boxes of array <B>
//            if found, B<ismaller> is the smaller one and returns IN
//            else if boxes are disjoint, returns OUT
//            else return UNKNOWN
// function : classiBnd2d (B)
// purpose  : returns SAME,DIFF,UNKNOWN,oneINtwo or twoINone


// function : mkboundedF(W,boundedF)
// purpose  : 

// function : FindAPointInTheFace(F,u,v)
// purpose  : 

// function : GetFP2d(W,boundedF,p2d)
// purpose  : computes <boundedF> the bounded face built up with wire <W>
//            and <p2d> a point in <boundedF>
//            

// function : classiwithp2d(wi)
// purpose  : classify wires (wi(k),k = 1,2)
//            prequesitory : wires of <wi> are not intersecting each other.

// function : ClassifW(F,mapoldWnewW,mapWlow)
// purpose  : all wires described in <mapoldWnewW> are on face <F>
//            <mapoldWnewW> = map with :
//             key = a wire of <F>
//             item = the splits of the previous wire (can be an empty list)
//            the aim is to get map <mapWlow> with :
//             key = a new face's boundary
//             item = wires dexcribing holes in the previous face 
//                    (can be an empty list) 
// ------------------------------------------------------------

/*static TopAbs_State FUN_tool_classiBnd2d(const Bnd_Array1OfBox2d& B,Standard_Integer& ismaller,
				 const Standard_Boolean chklarge = Standard_True)
{
  // purpose : 
  //   Getting <ismaller>, index of the smallest Bnd Box
  //   if B(i) is IN B(j): ismaller = i,
  //                       return IN.
  //   else: ismaller = 1,
  //         if B(1) and B(2) are disjoint, return OUT
  //         if B(1) and B(2) are same, return ON
  //         else return UNKNOWN.
  ismaller = 1;
  
  TColStd_Array2OfReal UV(1,2, 1,4);
  for (Standard_Integer i = 1; i <= 2; i++)
    //      (Umin(i), Vmin(i), Umax(i), Vmax(i))
    B(i).Get(UV(i,1), UV(i,3), UV(i,2), UV(i,4));

#ifdef DEB
  Standard_Boolean trc = Standard_False;
  if (trc) {
    for (Standard_Integer i = 1; i <= 2; i++)
      cout<<"B("<<i<<") = ("<<UV(i,1)<<" "<<UV(i,3)<<" "<<UV(i,2)<<" "<<UV(i,4)<<")"<<endl;
  }
#endif

  Standard_Boolean smaller, same;
  Standard_Integer ii, jj;
  Standard_Real tol = 1.e-6;

  Standard_Boolean disjoint = Standard_False;
  for (Standard_Integer k = 1; k <= 3; k+=2) { 
    for (i = 1; i <= 2; i++) {
      ii = i; jj = (i == 1) ?  2 : 1;         
      //  diff = Umin<ii> - Umax<jj> : k = 1
      //  diff = Vmin<ii> - Vmax<jj> : k = 3
      Standard_Real diff = UV(ii,k) - UV(jj,k+1);
      // IMPORTANT : for splitted faces sharing same edge, use
      // chklarge = True.
      disjoint = chklarge ? (diff >= -tol) : (diff > 0.);
      if (disjoint) {ismaller = 1; return TopAbs_OUT;}
    }
  }
  
  for (i = 1; i <= 2; i++) {
    ii = i; jj = (i == 1) ? 2 : 1; 
    smaller = same = Standard_True;
    for (Standard_Integer k = 1; k <= 3; k += 2){         
      //  diff = Umin<ii> - Umin<jj> : k = 1        
      //  diff = Vmin<ii> - Vmin<jj> : k = 3 
      Standard_Real diff = UV(ii,k) - UV(jj,k);  
      smaller = chklarge ? (smaller && (diff > -tol)) : (smaller && (diff > 0.));
      same = same && (Abs(diff) <= tol);
    }    
    for (k = 2; k <= 4; k +=2){      
      //  diff = Umax<ii> - Umax<jj> : k = 2        
      //  diff = Vmax<ii> - Vmax<jj> : k = 4
      Standard_Real diff = UV(ii,k) - UV(jj,k); 
      smaller = chklarge ? (smaller && (diff < tol)) : (smaller && (diff < 0.));
      same = same && (Abs(diff) <= tol);
    }

    if (same) return TopAbs_ON;
    if (smaller) {
      ismaller = ii;
      return TopAbs_IN;
    }
  }
  return TopAbs_UNKNOWN;
}

#define SAME     (-1)
#define DIFF     (-2)
#define UNKNOWN  ( 0)
#define oneINtwo ( 1)
#define twoINone ( 2)

Standard_EXPORT Standard_Integer FUN_tool_classiBnd2d(const Bnd_Array1OfBox2d& B,
					 const Standard_Boolean chklarge = Standard_True)
{
  Standard_Integer ismaller;
  TopAbs_State sta = FUN_tool_classiBnd2d(B, ismaller, chklarge);
  Standard_Integer res = -10;
  switch (sta) {
  case TopAbs_IN :
    res = ismaller; break;
  case TopAbs_OUT :
    res = DIFF; break;
  case TopAbs_ON :
    res = SAME; break;
  case TopAbs_UNKNOWN :
    res = UNKNOWN; break;
  }
  return res;
}

static Standard_Boolean FUN_tool_mkboundedF(const TopoDS_Wire& W, TopoDS_Face& boundedF)
{
  BRepLib_MakeFace mf(W, Standard_False);
  Standard_Boolean done = mf.IsDone();
  if (done) boundedF = mf.Face();
  return done;
}
Standard_Boolean FUN_tool_FindAPointInTheFace(const TopoDS_Face& F,
				 Standard_Real& u, Standard_Real& v)
{
  Standard_Boolean ok = BRepClass3d_SolidExplorer::FindAPointInTheFace(F,u,v);
  return ok;
}
static Standard_Boolean FUN_tool_GetFP2d(const TopoDS_Shape& W,
			    TopoDS_Shape& boundedF, gp_Pnt2d& p2d)
{
  Standard_Boolean ok = FUN_tool_mkboundedF(TopoDS::Wire(W), TopoDS::Face(boundedF)); 
  if (!ok) return Standard_False;
  
  Standard_Real u,v; ok = FUN_tool_FindAPointInTheFace(TopoDS::Face(boundedF),u,v);
  if (!ok) return Standard_False;
  p2d = gp_Pnt2d(u,v);
  return Standard_True;
}
static Standard_Integer FUN_tool_classiwithp2d(const TopTools_Array1OfShape& wi)
{
  Standard_Integer stares = UNKNOWN;
  TopTools_Array1OfShape fa(1,2);
  TColgp_Array1OfPnt     p3d(1,2);
  for (Standard_Integer k = 1; k <= 2; k++) {
    gp_Pnt2d p2d;
    Standard_Boolean ok = FUN_tool_GetFP2d(wi(k), fa(k), p2d);
    if (!ok) return UNKNOWN;
    BRepAdaptor_Surface BS(TopoDS::Face(fa(k)));
    p3d(k) = BS.Value(p2d.X(),p2d.Y());
  }

  TopAbs_State sta;
  Standard_Integer i,j; i = j = 0;
  for (Standard_Integer nite = 1; nite <= 2; nite++) {
    i = nite;
    j = (i == 1) ? 2 : 1;
    TopoDS_Face f = TopoDS::Face(fa(j));
    const gp_Pnt p = p3d(i);
    Standard_Real tol = BRep_Tool::Tolerance(f);
    BRepClass_FaceClassifier Fclass(f, p, tol);
    sta = Fclass.State();
    if (sta == TopAbs_IN) break;
  }
  switch (sta) {
  case TopAbs_IN :
    stares = i; break;
  case TopAbs_OUT :
    stares = DIFF; break;
  case TopAbs_ON :
  case TopAbs_UNKNOWN :
    stares = UNKNOWN; break;
  }
  return stares;
}

Standard_EXPORT Standard_Boolean FUN_tool_ClassifW(const TopoDS_Face& F,
				      const TopTools_DataMapOfShapeListOfShape& mapoldWnewW,
				      TopTools_DataMapOfShapeListOfShape& mapWlow)
{
  // NYI : create maps to store Bnd_Box2d, and faces.

#ifdef DEB
  Standard_Boolean trc = TopOpeBRepTool_GettraceREGUFA();
  if (trc) cout<<"** ClassifW :"<<endl;
  STATIC_mapw.Clear();    
#endif  

  // Purpose :
  // --------
  // Filling the map <mapWlow> : with (key + item) = new face,
  //  item = (newface has holes) ? list of wires IN the wire key: empty list

  // prequesitory : <mapoldWnewW> binds (non splitted wire of <F>, emptylos)
  //                                    (splitted wire of <F>, splits of the wire)

  // Mapping :
  // --------
  // Filling <oldW> : list of wires of <F>
  // Filling <mapWlow> : with (non-splitted old wire, emptylos),
  //                          (split of old wire, emptylos)
  TopTools_ListOfShape oldW;
  Standard_Integer noldW = mapoldWnewW.Extent();
  Standard_Boolean oneoldW = (noldW == 1);
  TopTools_DataMapIteratorOfDataMapOfShapeListOfShape ite(mapoldWnewW);
  TopTools_ListOfShape emptylos;

  // --------------
  // * noldW == 1 :
  // --------------
  if (oneoldW) {
    const TopoDS_Wire& oldwi = TopoDS::Wire(ite.Key());
    const TopTools_ListOfShape& low = ite.Value();
    Standard_Integer nw = low.Extent();
    if (nw == 0) {mapWlow.Bind(oldwi,emptylos); return Standard_True;}
    if (nw == 1) {mapWlow.Bind(low.First(),emptylos); return Standard_True;}

    // <complWoldw> = {(newwire, emptylos)}
    TopTools_DataMapOfShapeListOfShape complWoldw;
    TopTools_ListIteratorOfListOfShape itlw(low);
    for (; itlw.More(); itlw.Next()) complWoldw.Bind(itlw.Value(), emptylos);    

    // iteration on <complWoldw> :
    Standard_Integer ncompl = complWoldw.Extent();
    Standard_Boolean go = Standard_True;
    Standard_Integer nite = 0, nitemax = Standard_Integer(ncompl*(ncompl-1)/2);
    while (go && (nite <= nitemax)){
      Bnd_Array1OfBox2d Bnd2d(1,2);
      TopTools_Array1OfShape wi(1,2);
      
      TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itmap(complWoldw);
      wi(1) = itmap.Key();
      if (ncompl == 1) {
	mapWlow.Bind(wi(1),itmap.Value()); 
	break;
      }
      FUN_tool_mkBnd2d(wi(1), F, Bnd2d(1));      
      Standard_Boolean OUTall = Standard_False;
      Standard_Boolean oneINother = Standard_False; 
      Standard_Integer sma,gre; // dummy if !oneINother

      for (; itmap.More(); itmap.Next()) {	
	wi(2) = itmap.Key();
	if (wi(1).IsSame(wi(2))) continue;
	FUN_tool_mkBnd2d(wi(2), F, Bnd2d(2));

	// sta : wi(1) / wi(2)
	Standard_Integer sta = FUN_tool_classiBnd2d(Bnd2d);
	nite++;
	if ((sta == SAME)||(sta == UNKNOWN)) sta = FUN_tool_classiwithp2d(wi);
#ifdef DEB	
	if (trc) {cout<<"#wi :";FUN_tool_coutsta(sta,FUN_adds(wi(1)),FUN_adds(wi(2)));}
#endif		
	if ((sta == SAME)||(sta == UNKNOWN)) return Standard_False;    	 
	if ((sta == DIFF) && itmap.More()) {OUTall = Standard_True; continue;}// w(1) OUT w(2)    
	sma = (sta == oneINtwo) ? 1 : 2;
	gre = (sta == oneINtwo) ? 2 : 1;	
	oneINother = Standard_True;
	break;
      } // itmap

      if (oneINother) {
	// Updating map <complWoldw> with : 
	//  - key = wi(gre), 
	//    item += wi(sma) && item += item(wi(sma))
	//  - unbinding key = (wi(sma))
	TopTools_ListOfShape& lwgre = complWoldw.ChangeFind(wi(gre));
	lwgre.Append(wi(sma));	  
	TopTools_ListIteratorOfListOfShape itwsma(complWoldw.Find(wi(sma)));
	for (; itwsma.More(); itwsma.Next()) lwgre.Append(itwsma.Value());
	complWoldw.UnBind(wi(sma));
      }
      else if (OUTall) {mapWlow.Bind(wi(1),emptylos); complWoldw.UnBind(wi(1));}
      else return Standard_False;

      ncompl = complWoldw.Extent();
      go = (ncompl >= 1);
    }
    return Standard_True;
  } // oneoldW


  // -------------
  // * noldW > 1 :
  // -------------
  for (; ite.More(); ite.Next()){
    const TopoDS_Wire& oldwi = TopoDS::Wire(ite.Key());
    const TopTools_ListOfShape& low = ite.Value();
    TopTools_ListIteratorOfListOfShape itlow(low);
    if (low.IsEmpty()) mapWlow.Bind(oldwi, emptylos);
    else
      for (; itlow.More(); itlow.Next()) mapWlow.Bind(itlow.Value(), emptylos);
    oldW.Append(oldwi);
  }
    
  // classifying wires of <mapoldWnewW> :
  // -----------------------------------
  // <Owi>            : old wires
  // <OBnd2d>         : old wires' bounding boxes
  // <Owhassp>(k) : Owi(k) has splits

  TopTools_ListOfShape oldWcopy; oldWcopy.Assign(oldW); 
  for (TopTools_ListIteratorOfListOfShape itoldW(oldW); itoldW.More(); itoldW.Next()) { 
 
    TopTools_Array1OfShape Owi(1,2);
    Bnd_Array1OfBox2d OBnd2d(1,2);
    TColStd_Array1OfBoolean Owhassp(1,2);

    Owi(1) = itoldW.Value();
    if (oldWcopy.Extent() <1) break; 
    oldWcopy.RemoveFirst();
    
    Owhassp(1) = !mapoldWnewW.Find(Owi(1)).IsEmpty();
    Standard_Boolean Owi1notkey = !mapWlow.IsBound(Owi(1));
    if (Owi1notkey && !Owhassp(1)) continue;
    
    FUN_tool_mkBnd2d(Owi(1), F, OBnd2d(1));      

    // Classifying oldwire(i) with oldwires(j): j = i+1..nwiresofF 
    Standard_Integer osma,ogre,osta;
    TopTools_ListIteratorOfListOfShape itoldWcopy(oldWcopy);
    for (; itoldWcopy.More(); itoldWcopy.Next()) {

      TopTools_Array1OfListOfShape newwi(1,2); 
      Owi(2) = TopoDS::Wire(itoldWcopy.Value()); 
      Standard_Boolean Owi2notkey = !mapWlow.IsBound(Owi(2));
      Owhassp(2) = !mapoldWnewW.Find(Owi(2)).IsEmpty(); 
      if (Owi2notkey && !Owhassp(2)) continue;	
      FUN_tool_mkBnd2d(Owi(2), F, OBnd2d(2));
      
      // <osma>, <ogre> :
      // ----------------
      // Classifying Ow<i> with Ow<j> :
      osta = FUN_tool_classiBnd2d(OBnd2d);
      if ((osta == SAME)||(osta == UNKNOWN)) osta = FUN_tool_classiwithp2d(Owi);
#ifdef DEB	
      if (trc) {cout<<"wi : "; FUN_tool_coutsta(osta,FUN_adds(Owi(1)),FUN_adds(Owi(2)));}
#endif		
      if ((osta == SAME)||(osta == UNKNOWN)) return Standard_False;      
      if (osta == DIFF)                    continue; // Ow(1), Ow(2) are disjoint		
      // Owi<sma> is IN Owi<grea>
      osma = (osta == oneINtwo) ? 1 : 2;
      ogre = (osta == oneINtwo) ? 2 : 1;

      // Owhassp<k>  : newwi<k> = splits (Owi<k>)
      // !Owhassp<k> : newwi<k> = Owi<k> 
      for (Standard_Integer i = 1; i <= 2; i++) {
	const TopoDS_Shape& owi = Owi(i);
	if (!Owhassp(i)) newwi(i).Append(owi);
	else             newwi(i) = mapoldWnewW.Find(owi);
      }

      //
      // classifying wires of newwi<sma> with wires of newwi<gre> :
      //
      Standard_Integer sta, sma, gre;
      TopTools_ListIteratorOfListOfShape itnwi(newwi(osma));
      for (; itnwi.More(); itnwi.Next()) {
	// <wi>    : new wires
	// <Bnd2d> : new wires' bounding boxes
	TopTools_Array1OfShape wi(1,2);
	Bnd_Array1OfBox2d Bnd2d(1,2);
	
	wi(1) = itnwi.Value(); // wi(1) in {newwi(osma)}
	Standard_Boolean wi1notkey = !mapWlow.IsBound(wi(1));
	if (wi1notkey) continue;

	if (!Owhassp(osma)) Bnd2d(1).Add(OBnd2d(osma));
	else               FUN_tool_mkBnd2d(wi(1), F, Bnd2d(1));
	
	TopTools_ListIteratorOfListOfShape itnwj(newwi(ogre));
	for (; itnwj.More(); itnwj.Next()) {

	  wi(2) = itnwj.Value(); // wi(2) in {newwi(ogre)}
	  Standard_Boolean wi2notkey = !mapWlow.IsBound(wi(2));
	  if (wi2notkey) continue;

	  // empty the bounding box
	  Bnd_Box2d newB2d;	  
	  if (!Owhassp(ogre)) newB2d.Add(OBnd2d(ogre));
	  else                FUN_tool_mkBnd2d(wi(2), F, newB2d);
	   FUN_tool_UpdateBnd2d(Bnd2d(2),newB2d);

	  // Classifying wi(1)  with wi(2) :
	  sta = FUN_tool_classiBnd2d(Bnd2d); 
#ifdef DEB
	  if (trc) {cout<<"wi : "; FUN_tool_coutsta(sta,STATIC_mapw.FindIndex(wi(1)),
						    STATIC_mapw.FindIndex(wi(2)));}
#endif
	  if ((sta == SAME)||(sta == UNKNOWN)) sta = FUN_tool_classiwithp2d(wi);
	  if ((sta == SAME)||(sta == UNKNOWN)) return Standard_False;
	  if (sta == DIFF)                   continue;	
	  // wi<sma> is IN wi<grea>
	  sma = (sta == oneINtwo) ? 1 : 2;
	  gre = (sta == oneINtwo) ? 2 : 1;	
	  
	  // Updating map <mapWlow> with : 
	  //  - key = wi(gre), 
	  //    item += wi(sma) && item += item(wi(sma))
	  //  - unbinding key = (wi(sma))
	  TopTools_ListOfShape& lwgre = mapWlow.ChangeFind(wi(gre));
	  lwgre.Append(wi(sma));	  
	  TopTools_ListIteratorOfListOfShape itwsma(mapWlow.Find(wi(sma)));
	  for (; itwsma.More(); itwsma.Next()) lwgre.Append(itwsma.Value());
	  mapWlow.UnBind(wi(sma));	    
	  break; 
	  // wi<sma> IN wi<gre>, wi<sma> is OUT {newwi<gre>} / wi<gre>
	  // wi<sma> is classified / all newwires.
	} 
      } // itnwi(newwi(sma))
    }// itoldWcopy   
  } // itoldW 
  return Standard_True;
}
// ------------------------------------------------------------
// -------------------- building up faces ---------------------
// ------------------------------------------------------------

Standard_EXPORT Standard_Boolean FUN_tool_MakeFaces(const TopoDS_Face& theFace,
				       TopTools_DataMapOfShapeListOfShape& mapWlow,
				       TopTools_ListOfShape& aListOfFaces)
{
#ifdef DEB
  Standard_Boolean trc = TopOpeBRepTool_GettraceREGUFA();
  if (trc) cout<<"** MakeFaces :"<<endl;
#endif
  Standard_Boolean toreverse = M_REVERSED(theFace.Orientation());
  TopoDS_Face F = TopoDS::Face(theFace.Oriented(TopAbs_FORWARD));
  BRep_Builder BB;

  TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itm(mapWlow);
  for (; itm.More(); itm.Next()) {
    const TopoDS_Wire& wi = TopoDS::Wire(itm.Key());  
    TopoDS_Shape FF = F.EmptyCopied(); BB.Add(FF,wi);
//    BB.MakeFace(FF); // put a TShape
      
    TopTools_ListIteratorOfListOfShape itlow(itm.Value());      
    for (; itlow.More(); itlow.Next()) {
      const TopoDS_Wire& wwi = TopoDS::Wire(itlow.Value());
      BB.Add(FF,wwi);
    }

    if (toreverse) FF.Orientation(TopAbs_REVERSED);
    aListOfFaces.Append(FF);
  }

#ifdef DEB
  if (trc) {
    cout<<"sp(fa"<<FUN_adds(theFace)<<")=";
    TopTools_ListIteratorOfListOfShape it(aListOfFaces);
    for (; it.More(); it.Next()) cout<<" fa"<<FUN_adds(it.Value());
    cout<<endl;
  }
#endif

  return Standard_True;
}*/

Standard_EXPORT Standard_Boolean FUN_tool_ClassifW(const TopoDS_Face& F,
				      const TopTools_DataMapOfShapeListOfShape& mapOwNw,
				      TopTools_DataMapOfShapeListOfShape& mapWlow)
{  
#ifdef DEB
  Standard_Boolean trc = TopOpeBRepTool_GettraceREGUFA();
  if (trc) cout<<"** ClassifW :"<<endl;
#endif  
  Standard_Real tolF = BRep_Tool::Tolerance(F);
  Standard_Real toluv = TopOpeBRepTool_TOOL::TolUV(F,tolF);
  TopoDS_Shape aLocalShape = F.Oriented(TopAbs_FORWARD);  
  TopoDS_Face FFOR = TopoDS::Face(aLocalShape);
//  TopoDS_Face FFOR = TopoDS::Face(F.Oriented(TopAbs_FORWARD));
  TopOpeBRepTool_CLASSI CLASSI; CLASSI.Init2d(FFOR);

  TopTools_ListOfShape null;
  TopTools_ListOfShape oldW;
  Standard_Integer noldW = mapOwNw.Extent();
  TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itm(mapOwNw);
  
  // noldW = 1
  // ---------
  if (noldW == 1) {
    const TopTools_ListOfShape& low = itm.Value();
    Standard_Boolean ok = CLASSI.Classilist(low,mapWlow);
    if (!ok) return Standard_False;
    return Standard_True;
  }

  // noldW > 1
  // ---------
  TopTools_ListOfShape lOws;
  for (; itm.More(); itm.Next()){
    const TopoDS_Shape& owi = itm.Key(); 
    lOws.Append(owi);
    const TopTools_ListOfShape& low = itm.Value();
    TopTools_ListOfShape lwresu; FUN_addOwlw(owi,low,lwresu);
    TopTools_ListIteratorOfListOfShape itw(lwresu);
    for (; itw.More(); itw.Next()) mapWlow.Bind(itw.Value(), null);    
  }//itm(mapOwNw)

  TopTools_MapOfShape mapdone;
  Standard_Integer nOw = noldW;
  Standard_Integer nite = 0, nitemax = Standard_Integer(nOw*(nOw-1)/2);
  while (nite <= nitemax){
    nOw = lOws.Extent();
    if (nOw == 0) break;

    TopTools_ListIteratorOfListOfShape itOw(lOws);
    const TopoDS_Shape& Ow1 = itOw.Value();
    Standard_Boolean isb1 = mapWlow.IsBound(Ow1);
    isb1 = isb1 || !mapdone.Contains(Ow1);
    if (!isb1) continue; 

    const TopTools_ListOfShape& lw1 = mapOwNw.Find(Ow1);
    if (nOw == 1) { 
      // all wires of <mapWs> have been treated, except the last one
      // if (nw1 == 0) mapWlow binds already (Ow1,null); 
      // else         {mapWlow binds already (w1k,null), w1k in lw1} 
      break;
    }//nOw == 1

    itOw.Next();
    Standard_Boolean OUTall = Standard_False;
    TopoDS_Shape Ow2;
    Standard_Integer sta12 = UNKNOWN;
    for (; itOw.More(); itOw.Next()){
      Ow2 = itOw.Value();
      Standard_Boolean isb2 = mapWlow.IsBound(Ow2);
      isb2 = isb2 || !mapdone.Contains(Ow2);
      if (!isb2) continue;
      Standard_Integer stabnd2d12 = CLASSI.ClassiBnd2d(Ow1,Ow2,toluv,Standard_True);
      sta12 = CLASSI.Classip2d(Ow1,Ow2, stabnd2d12);
      if      (sta12 == DIFF)                       {OUTall = Standard_True; continue;}
      else if ((sta12 == UNKNOWN)||(sta12 == SAME)) return Standard_False;
      break;
    }
    if (OUTall) {
      // if (nw1 == 0) mapWlow binds already (Ow1,null); 
      // else         {mapWlow binds already (w1k,null), w1k in lw1} 
      TopTools_ListOfShape ldone; FUN_addOwlw(Ow1,lw1,ldone);
      TopTools_ListIteratorOfListOfShape itw(ldone);
      for (; itw.More(); itw.Next()) mapdone.Add(itw.Value());
#ifdef DEB
      if (trc) cout<<"old wires :wi"<<FUN_adds(Ow1)<<" is OUT all old wires"<<endl;
#endif      
      lOws.RemoveFirst();
    }//OUTall    
    else {
#ifdef DEB
      if (trc) {cout<<"old wires :wi -> ";
		FUN_tool_coutsta(sta12,FUN_adds(Ow1),FUN_adds(Ow2));
		cout<<endl;}
#endif
      const TopTools_ListOfShape& lw2 = mapOwNw.Find(Ow2);
     
      TopTools_ListOfShape lw1r; FUN_addOwlw(Ow1,lw1,lw1r);
      TopTools_ListOfShape lw2r; FUN_addOwlw(Ow2,lw2,lw2r);
      TopTools_ListOfShape lgre,lsma;
      if (sta12 == oneINtwo) {lgre.Append(lw2r); lsma.Append(lw1r);}
      if (sta12 == twoINone) {lgre.Append(lw1r); lsma.Append(lw2r);}
      
      TopTools_ListIteratorOfListOfShape itsma(lsma);
      for (; itsma.More(); itsma.Next()){
	const TopoDS_Shape& wsma = itsma.Value();
	Standard_Boolean isbsma = mapWlow.IsBound(wsma);
	isbsma = isbsma || !mapdone.Contains(wsma);
	if (!isbsma) continue;
	
	TopTools_ListIteratorOfListOfShape itgre(lgre);
	for (; itgre.More(); itgre.Next()){
	  const TopoDS_Shape& wgre = itgre.Value();
	  Standard_Boolean isbgre = mapWlow.IsBound(wgre);
	  isbgre = isbgre || !mapdone.Contains(wgre);
	  if (!isbgre) continue;
	
	  Standard_Integer stabnd2d = CLASSI.ClassiBnd2d(wsma,wgre,toluv,Standard_True);
	  Standard_Integer sta = CLASSI.Classip2d(wsma,wgre, stabnd2d);
#ifdef DEB
      if (trc) {cout<<" wires :wi -> ";
		FUN_tool_coutsta(sta,FUN_adds(wsma),FUN_adds(wgre));
		cout<<endl;}
#endif
	
	  if      (sta == DIFF) continue;
	  else if (sta == oneINtwo) {// wsma IN wgre
	    mapWlow.ChangeFind(wgre).Append(mapWlow.ChangeFind(wsma));
	    mapWlow.UnBind(wsma);
	  }
	  else if (sta == twoINone) {// wgre IN wsma
	    mapWlow.ChangeFind(wsma).Append(mapWlow.ChangeFind(wgre));
	    mapWlow.UnBind(wgre);
	  }
	  else return Standard_False;
	}//itgre
      }//itsma
      lOws.RemoveFirst(); 
    } //!OUTall
  }//nite
  return Standard_True;
}

//=======================================================================
//function : RegularizeFace
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRepTool::RegularizeFace(const TopoDS_Face& theFace,
				   const TopTools_DataMapOfShapeListOfShape& mapoldWnewW,
				   TopTools_ListOfShape& newFaces) 
{  
  // <mapWlow> 
  // ---------
  // key = wire <w>,
  // item = if the new face has holes, the item contains wires
  //        classified IN the area described by the boundary <w>
  //        on <aFace>,
  //        else : the item is an empty list, <w> describes the
  //        whole new face.  
  TopTools_DataMapOfShapeListOfShape mapWlow;
  
  // Classifying  wires :
  // -------------------
  //  Standard_Boolean classifok = FUN_tool_ClassifW(theFace, mapoldWnewW, mapWlow);
  TopoDS_Shape aLocalShape = theFace.Oriented(TopAbs_FORWARD);
  TopoDS_Face aFace = TopoDS::Face(aLocalShape);
//  TopoDS_Face aFace = TopoDS::Face(theFace.Oriented(TopAbs_FORWARD));
  
  Standard_Boolean classifok = FUN_tool_ClassifW(aFace, mapoldWnewW, mapWlow);  
  if (!classifok) return Standard_False;
  
  // <aListOfFaces>
  // -------------  
  Standard_Boolean facesbuilt = TopOpeBRepTool_TOOL::WireToFace(theFace, mapWlow, newFaces); 
  if (!facesbuilt) return Standard_False;  
  return Standard_True;
}