Salome HOME
Mantis issue 0021200: Problem of performance when doing a partition. A fix by PKV.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Builder_2.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        GEOMAlgo_Builder_2.cxx
24 // Author:      Peter KURNEV
25
26 #include <GEOMAlgo_Builder.hxx>
27
28 #include <TColStd_IndexedMapOfInteger.hxx>
29 #include <TColStd_ListOfInteger.hxx>
30
31 #include <TopAbs_Orientation.hxx>
32
33 #include <TopoDS.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Compound.hxx>
38
39 #include <TopTools_IndexedMapOfShape.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_MapOfShape.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
43
44 #include <TopExp.hxx>
45 #include <TopExp_Explorer.hxx>
46
47 #include <BRep_Tool.hxx>
48 #include <BRep_Builder.hxx>
49 #include <BRepAlgo_Image.hxx>
50 #include <BRepTools.hxx>
51
52 #include <IntTools_Context.hxx>
53 #include <IntTools_FClass2d.hxx>
54
55 #include <BooleanOperations_OnceExplorer.hxx>
56 #include <BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger.hxx>
57 #include <BOPTools_ListOfPaveBlock.hxx>
58 #include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
59 #include <BOPTools_CArray1OfSSInterference.hxx>
60 #include <BOPTools_SSInterference.hxx>
61 #include <BOPTools_SequenceOfCurves.hxx>
62 #include <BOPTools_Curve.hxx>
63 #include <BOPTools_ListOfPaveBlock.hxx>
64 #include <BOPTools_PaveBlock.hxx>
65 #include <BOPTools_Tools3D.hxx>
66 #include <BOPTools_CArray1OfVSInterference.hxx>
67 #include <BOPTools_VSInterference.hxx>
68 #include <BOPTools_ESInterference.hxx>
69 #include <BOPTools_CArray1OfESInterference.hxx>
70
71 #include <NMTDS_ShapesDataStructure.hxx>
72 #include <NMTDS_InterfPool.hxx>
73
74 #include <NMTTools_PaveFiller.hxx>
75 #include <NMTTools_ListOfCoupleOfShape.hxx>
76 #include <NMTTools_Tools.hxx>
77 #include <NMTTools_CoupleOfShape.hxx>
78 #include <NMTTools_IndexedDataMapOfShapeIndexedMapOfShape.hxx>
79 #include <NMTTools_Tools.hxx>
80 #include <NMTTools_ListIteratorOfListOfCommonBlock.hxx>
81 #include <NMTTools_ListOfCommonBlock.hxx>
82 #include <NMTTools_CommonBlock.hxx>
83 #include <NMTTools_IndexedDataMapOfIndexedMapOfInteger.hxx>
84 //
85 #include <GEOMAlgo_Tools3D.hxx>
86 #include <GEOMAlgo_WireEdgeSet.hxx>
87 #include <GEOMAlgo_BuilderFace.hxx>
88
89 #include <GEOMAlgo_ShapeSet.hxx>
90 //
91 #include <NMTDS_BoxBndTree.hxx>
92 #include <NCollection_UBTreeFiller.hxx>
93 #include <Bnd_Box.hxx>
94 #include <BRepBndLib.hxx>
95 #include <TopTools_DataMapOfIntegerShape.hxx>
96 #include <TColStd_ListOfInteger.hxx>
97 #include <TColStd_ListIteratorOfListOfInteger.hxx>
98
99 static
100   void UpdateCandidates(const Standard_Integer ,
101                         const Standard_Integer ,
102                         NMTTools_IndexedDataMapOfIndexedMapOfInteger& );
103
104 //=======================================================================
105 //function : FillImagesFaces
106 //purpose  :
107 //=======================================================================
108   void GEOMAlgo_Builder::FillImagesFaces()
109 {
110   myErrorStatus=0;
111   //
112   FillIn2DParts();
113   BuildSplitFaces();
114   FillSameDomainFaces();
115   FillImagesFaces1();
116   FillInternalVertices();
117 }
118
119 //=======================================================================
120 // function: FillIn2DParts
121 // purpose:
122 //=======================================================================
123   void GEOMAlgo_Builder::FillIn2DParts()
124 {
125   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
126   NMTTools_PaveFiller* pPF=myPaveFiller;
127   NMTDS_InterfPool* pIP=pPF->IP();
128   BOPTools_CArray1OfSSInterference& aFFs=pIP->SSInterferences();
129   NMTTools_CommonBlockPool& aCBP=pPF->ChangeCommonBlockPool();
130   //
131   Standard_Integer  j, nSpIn, nSpSc, aNbCurves;
132   Standard_Integer aNbS, nF, aNbCBP, n1, n2, aNbFFs, aNbSpIn;
133   TopTools_MapOfShape  aMFence;
134   TopTools_ListOfShape aLSpIn;
135   TopoDS_Face aF;
136   NMTTools_ListIteratorOfListOfCommonBlock aItCB;
137   BOPTools_ListIteratorOfListOfPaveBlock aItPB;
138   //
139   myInParts.Clear();
140   //
141   aNbFFs=aFFs.Extent();
142   aNbCBP=aCBP.Extent();
143   //
144   aNbS=aDS.NumberOfShapesOfTheObject();
145   for (nF=1; nF<=aNbS; ++nF) {
146     if (aDS.GetShapeType(nF)!=TopAbs_FACE) {
147       continue;
148     }
149     //
150     aF=TopoDS::Face(aDS.Shape(nF));
151     //
152     aMFence.Clear();
153     aLSpIn.Clear();
154     //
155     // 1. In Parts
156     for (j=1; j<=aNbCBP; ++j) {
157       NMTTools_ListOfCommonBlock& aLCB=aCBP(j);
158       aItCB.Initialize(aLCB);
159       for (; aItCB.More(); aItCB.Next()) {
160         NMTTools_CommonBlock& aCB=aItCB.Value();
161         if (aCB.IsPaveBlockOnFace(nF)) {
162           const BOPTools_PaveBlock& aPB1=aCB.PaveBlock1();
163           nSpIn=aPB1.Edge();
164           const TopoDS_Shape& aSpIn=aDS.Shape(nSpIn);
165           if (aMFence.Add(aSpIn)){
166             aLSpIn.Append(aSpIn);
167           }
168         }
169       }
170     }
171     //
172     // 2. Section Parts
173     for (j=1; j<=aNbFFs; ++j) {
174       BOPTools_SSInterference& aFF=aFFs(j);
175       aFF.Indices(n1, n2);
176       if (!(n1==nF || n2==nF)) {
177         continue;
178       }
179       BOPTools_SequenceOfCurves& aSC=aFF.Curves();
180       aNbCurves=aSC.Length();
181       if (!aNbCurves) {
182         continue;
183       }
184       //
185       const BOPTools_Curve& aBC=aSC(1);
186       const BOPTools_ListOfPaveBlock& aLPB=aBC.NewPaveBlocks();
187       aItPB.Initialize(aLPB);
188       for (; aItPB.More(); aItPB.Next()) {
189         const BOPTools_PaveBlock& aPBSc=aItPB.Value();
190         nSpSc=aPBSc.Edge();
191         const TopoDS_Shape& aSpSc=aDS.Shape(nSpSc);
192         if (aMFence.Add(aSpSc)){
193           aLSpIn.Append(aSpSc);
194         }
195       }
196     }
197     aNbSpIn=aLSpIn.Extent();
198     if (aNbSpIn) {
199       myInParts.Add(aF, aLSpIn);
200     }
201   }//for (nF=1; nF<=aNbS; ++nF) {
202 }
203
204 //=======================================================================
205 // function: BuildSplitFaces
206 // purpose:
207 //=======================================================================
208   void GEOMAlgo_Builder::BuildSplitFaces()
209 {
210   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
211   NMTTools_PaveFiller* pPF=myPaveFiller;
212   NMTDS_InterfPool* pIP=pPF->IP();
213   BOPTools_CArray1OfSSInterference& aFFs=pIP->SSInterferences();
214   IntTools_Context& aCtx= pPF->ChangeContext();
215   //
216   Standard_Boolean bToReverse, bIsClosed, bIsDegenerated;
217   Standard_Integer i, aNb, aNbF, nF;
218   TopTools_MapOfShape aMFence;
219   TColStd_IndexedMapOfInteger aMFP;
220   TopExp_Explorer anExp;
221   TopoDS_Face aFF;
222   TopoDS_Edge aSp, aEE;
223   TopTools_ListIteratorOfListOfShape aIt;
224   TopAbs_Orientation anOriF, anOriE;
225   //
226   mySplitFaces.Clear();
227   //
228   // 1. Select Faces to process (MFP)
229   aNb=aDS.NumberOfShapesOfTheObject();
230   for (i=1; i<=aNb; ++i) {
231     const TopoDS_Shape& aF=aDS.Shape(i);
232     if (aF.ShapeType()!=TopAbs_FACE) {
233       continue;
234     }
235     if (!aMFence.Add(aF)) {
236       continue;
237     }
238     //
239     if (myInParts.Contains(aF)) {
240       aMFP.Add(i);
241       continue;
242     }
243     //
244     anExp.Init(aF, TopAbs_EDGE);
245     for (; anExp.More(); anExp.Next()) {
246       const TopoDS_Shape& aE=anExp.Current();
247       if (myImages.HasImage(aE)) {
248         aMFP.Add(i);
249         break;
250       }
251     }
252     //
253     //===
254     {
255       Standard_Integer aNbFFs, aNbSE, j, n1, n2;
256       //
257       aNbFFs=aFFs.Extent();
258       for (j=1; j<=aNbFFs; ++j) {
259         BOPTools_SSInterference& aFFj=aFFs(j);
260         aFFj.Indices(n1, n2);
261         if (!(n1==i || n2==i)) {
262           continue;
263         }
264         //
265         const TColStd_ListOfInteger& aLSE=aFFj.SharedEdges();
266         aNbSE=aLSE.Extent();
267         if (aNbSE) {
268           aMFP.Add(i);
269           break;
270         }
271       }
272     }
273     //===
274     //
275   }// for (i=1; i<=aNb; ++i)
276   //
277   // 2. ProcessFaces
278   aNbF=aMFP.Extent();
279   for (i=1; i<=aNbF; ++i) {
280     nF=aMFP(i);
281     const TopoDS_Face& aF=TopoDS::Face(aDS.Shape(nF));
282     anOriF=aF.Orientation();
283     aFF=aF;
284     aFF.Orientation(TopAbs_FORWARD);
285     //
286     aMFence.Clear();
287     //
288     // 2.1. Fill WES
289     GEOMAlgo_WireEdgeSet aWES;
290     aWES.SetFace(aFF);
291     //
292     //  2.1.1. Add Split parts
293     anExp.Init(aFF, TopAbs_EDGE);
294     for (; anExp.More(); anExp.Next()) {
295       const TopoDS_Edge& aE=TopoDS::Edge(anExp.Current());
296       anOriE=aE.Orientation();
297       //
298       if (!myImages.HasImage(aE)) {
299         if (anOriE==TopAbs_INTERNAL) {
300           aEE=aE;
301           aEE.Orientation(TopAbs_FORWARD);
302           aWES.AddStartElement(aEE);
303           aEE.Orientation(TopAbs_REVERSED);
304           aWES.AddStartElement(aEE);
305         }
306         else {
307           aWES.AddStartElement(aE);
308         }
309         continue;
310       }
311       //
312       bIsDegenerated=BRep_Tool::Degenerated(aE);
313       bIsClosed=BRep_Tool::IsClosed(aE, aF);
314       //
315       const TopTools_ListOfShape& aLIE=myImages.Image(aE);
316       aIt.Initialize(aLIE);
317       for (; aIt.More(); aIt.Next()) {
318         aSp=TopoDS::Edge(aIt.Value());
319         //
320         if (bIsDegenerated) {
321           aSp.Orientation(anOriE);
322           aWES.AddStartElement(aSp);
323           continue;
324         }
325         //
326         if (anOriE==TopAbs_INTERNAL) {
327           aSp.Orientation(TopAbs_FORWARD);
328           aWES.AddStartElement(aSp);
329           aSp.Orientation(TopAbs_REVERSED);
330           aWES.AddStartElement(aSp);
331           continue;
332         }
333         //
334         if (bIsClosed){
335           if (aMFence.Add(aSp)) {
336             //
337             if (!BRep_Tool::IsClosed(aSp, aF)){
338               BOPTools_Tools3D::DoSplitSEAMOnFace(aSp, aF);
339             }
340             //
341             aSp.Orientation(TopAbs_FORWARD);
342             aWES.AddStartElement(aSp);
343             aSp.Orientation(TopAbs_REVERSED);
344             aWES.AddStartElement(aSp);
345           }
346           continue;
347         }// if (aMFence.Add(aSp))
348         //
349         aSp.Orientation(anOriE);
350         bToReverse=BOPTools_Tools3D::IsSplitToReverse1(aSp, aE, aCtx);
351         if (bToReverse) {
352           aSp.Reverse();
353         }
354         aWES.AddStartElement(aSp);
355       }// for (; aIt.More(); aIt.Next()) {
356     }// for (; anExp.More(); anExp.Next()) {
357     //
358     // 2.1.2. Add In2D Parts
359     if (myInParts.Contains(aF)) {
360       const TopTools_ListOfShape& aLE=myInParts.FindFromKey(aF);
361       aIt.Initialize(aLE);
362       for (; aIt.More(); aIt.Next()) {
363         aSp=TopoDS::Edge(aIt.Value());
364         //
365         aSp.Orientation(TopAbs_FORWARD);
366         aWES.AddStartElement(aSp);
367         //
368         aSp.Orientation(TopAbs_REVERSED);
369         aWES.AddStartElement(aSp);
370       }
371     }
372     //
373     // 2.2. Build images Faces
374     TopTools_ListOfShape aLFR;
375     GEOMAlgo_ShapeSet aS1, aS2;
376     //
377     const TopTools_ListOfShape& aSE=aWES.StartElements();
378     aS1.Add(aSE);
379     aS2.Add(aFF, TopAbs_EDGE);
380     if (aS1.IsEqual(aS2)) {
381       aLFR.Append(aF);
382     }
383     else {
384       GEOMAlgo_BuilderFace aBF;
385       //
386       aBF.SetFace(aFF);
387       aBF.SetContext(aCtx);
388       aBF.SetShapes(aSE);
389       // <-DEB
390       aBF.Perform();
391       //
392       const TopTools_ListOfShape& aLF=aBF.Areas();
393       aIt.Initialize(aLF);
394       for (; aIt.More(); aIt.Next()) {
395         TopoDS_Shape& aFR=aIt.Value();
396         if (anOriF==TopAbs_REVERSED) {
397           aFR.Orientation(TopAbs_REVERSED);
398         }
399         aLFR.Append(aFR);
400       }
401     }
402     //
403     // 2.3. Collect draft images Faces
404     mySplitFaces.Bind(aF, aLFR);
405   }//for (i=1; i<=aNbF; ++i)
406 }
407
408 //=======================================================================
409 // function: FillSameDomainFaces
410 // purpose:
411 //=======================================================================
412   void GEOMAlgo_Builder::FillSameDomainFaces()
413 {
414   Standard_Boolean bIsSDF, bHasImage1, bHasImage2;
415   Standard_Integer i, j, aNbFF, nF1, nF2, aNbPBInOn, aNbC, aNbSE;
416   Standard_Integer aNbF1, aNbF2, i2s, aNbSD;
417   TopTools_MapOfShape aMFence;
418   TopTools_ListOfShape aLSempty;
419   TopTools_ListIteratorOfListOfShape aItF1, aItF2;
420   NMTTools_ListOfCoupleOfShape aLCS;
421   //
422   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
423   NMTTools_PaveFiller* pPF=myPaveFiller;
424   NMTDS_InterfPool* pIP=pPF->IP();
425   BOPTools_CArray1OfSSInterference& aFFs=pIP->SSInterferences();
426   IntTools_Context& aCtx= pPF->ChangeContext();
427   //
428   //
429   //mySameDomainShapes.Clear();
430   //
431   // 1. For each FF find among images of faces
432   //    all pairs of same domain faces (SDF) [=> aLCS]
433   aNbFF=aFFs.Extent();
434   for (i=1; i<=aNbFF; ++i) {
435     BOPTools_SSInterference& aFF=aFFs(i);
436     aFF.Indices(nF1, nF2);
437     //
438     const TopoDS_Face& aF1=TopoDS::Face(aDS.Shape(nF1));
439     const TopoDS_Face& aF2=TopoDS::Face(aDS.Shape(nF2));
440     //
441     // if there are no in/on 2D split parts the faces nF1, nF2
442     // can not be SDF
443     const BOPTools_ListOfPaveBlock& aLPBInOn=aFF.PaveBlocks();
444     aNbPBInOn=aLPBInOn.Extent();
445     //
446     //===
447     const TColStd_ListOfInteger& aLSE=aFF.SharedEdges();
448     aNbSE=aLSE.Extent();
449     if (!aNbPBInOn && !aNbSE) {
450       continue;
451     }
452     //===
453     //
454     // if there is at least one section edge between faces nF1, nF2
455     // they can not be SDF
456     BOPTools_SequenceOfCurves& aSC=aFF.Curves();
457     aNbC=aSC.Length();
458     if (aNbC) {
459       continue;
460     }
461     //
462     // the faces are suspected to be SDF.
463     // Try to find SDF among images of nF1, nF2
464     aMFence.Clear();
465     //
466     //XXXXXXXXXXXXXf
467     bHasImage1=mySplitFaces.HasImage(aF1);
468     bHasImage2=mySplitFaces.HasImage(aF2);
469     //
470     const TopTools_ListOfShape& aLF1r=(bHasImage1)? mySplitFaces.Image(aF1) : aLSempty;
471     const TopTools_ListOfShape& aLF2r=(bHasImage2)? mySplitFaces.Image(aF2) : aLSempty;
472     //
473     TopTools_DataMapOfIntegerShape aMIS;
474     TColStd_ListIteratorOfListOfInteger aItLI;
475     NMTDS_BoxBndTreeSelector aSelector;
476     NMTDS_BoxBndTree aBBTree;
477     NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
478     //
479     aNbF1=aLF1r.Extent();
480     aNbF2=aLF2r.Extent();
481     const TopTools_ListOfShape& aLF1=(aNbF1<aNbF2)? aLF1r : aLF2r;
482     const TopTools_ListOfShape& aLF2=(aNbF1<aNbF2)? aLF2r : aLF1r;
483     //
484     // 1. aTreeFiller
485     aItF2.Initialize(aLF2);
486     for (i2s=1; aItF2.More(); aItF2.Next(), ++i2s) {
487       Bnd_Box aBoxF2s;
488       //
489       const TopoDS_Face& aF2s=*((TopoDS_Face*)(&aItF2.Value()));
490       //
491       BRepBndLib::Add(aF2s, aBoxF2s);
492       //
493       aMIS.Bind(i2s, aF2s);
494       //
495       aTreeFiller.Add(i2s, aBoxF2s);
496     }//for (i2s=1; aItF2.More(); aItF2.Next(), ++i2s) {
497     //
498     aTreeFiller.Fill();
499     //
500     // 2.
501     aItF1.Initialize(aLF1);
502     for (j=1; aItF1.More(); aItF1.Next(), ++j) {
503       Bnd_Box aBoxF1x;
504       //
505       const TopoDS_Face& aF1x=*((TopoDS_Face*)(&aItF1.Value()));
506       //
507       BRepBndLib::Add(aF1x, aBoxF1x);
508       //
509       aSelector.Clear();
510       aSelector.SetBox(aBoxF1x);
511       aNbSD=aBBTree.Select(aSelector);
512       if (!aNbSD) {
513         continue;
514       }
515       //
516       const TColStd_ListOfInteger& aLI=aSelector.Indices();
517       aItLI.Initialize(aLI);
518       for (; aItLI.More(); aItLI.Next()) {
519         i2s=aItLI.Value();
520         const TopoDS_Face& aF2y=*((TopoDS_Face*)(&aMIS.Find(i2s)));
521         bIsSDF=NMTTools_Tools::AreFacesSameDomain(aF1x, aF2y, aCtx);
522         if (bIsSDF) {
523           if (aMFence.Contains(aF1x) ||
524               aMFence.Contains(aF2y)) {
525             continue;
526           }
527           aMFence.Add(aF1x);
528           aMFence.Add(aF2y);
529           //
530           NMTTools_CoupleOfShape aCS;
531           //
532           aCS.SetShape1(aF1x);
533           aCS.SetShape2(aF2y);
534           aLCS.Append(aCS);
535           //
536           if (aF1x==aF1) {
537             if (!mySplitFaces.HasImage(aF1)) {
538               mySplitFaces.Bind(aF1, aF1);
539             }
540           }
541           if (aF2y==aF2) {
542             if (!mySplitFaces.HasImage(aF2)) {
543               mySplitFaces.Bind(aF2, aF2);
544             }
545           }
546           break;
547         }//if (bIsSDF) {
548       }//for (; aItLI.More(); aItLI.Next()) {
549     }//for (; aItF1.More(); aItF1.Next()) {
550   }//for (i=1; i<=aNbFF; ++i)
551   //XXXXXXXXXXXXXt
552   aNbC=aLCS.Extent();
553   if (!aNbC) {
554     return;
555   }
556   //
557   // 2. Find Chains
558   NMTTools_IndexedDataMapOfShapeIndexedMapOfShape aMC;
559   //
560   NMTTools_Tools::FindChains(aLCS, aMC);
561   //
562   // 3. Fill the map of SDF mySameDomainFaces
563   aNbC=aMC.Extent();
564   for (i=1; i<=aNbC; ++i) {
565     const TopoDS_Shape& aF=aMC.FindKey(i);
566     const TopTools_IndexedMapOfShape& aMSDF=aMC(i);
567     //
568     aNbFF=aMSDF.Extent();
569     for (j=1; j<=aNbFF; ++j) {
570       const TopoDS_Shape& aFSD=aMSDF(j);
571       mySameDomainShapes.Add(aFSD, aF);
572     }
573   }
574   //
575 }
576
577 //=======================================================================
578 // function: FillImagesFaces1
579 // purpose:
580 //=======================================================================
581   void GEOMAlgo_Builder::FillImagesFaces1()
582 {
583   Standard_Integer i, aNb, iSense, aNbLFx;
584   TopoDS_Face aF, aFSp, aFSD;
585   TopTools_ListOfShape aLFx;
586   TopTools_ListIteratorOfListOfShape aIt;
587   //
588   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
589   //
590   aNb=aDS.NumberOfShapesOfTheObject();
591   for (i=1; i<=aNb; ++i) {
592     const TopoDS_Shape& aS=aDS.Shape(i);
593     if (aS.ShapeType()!=TopAbs_FACE) {
594       continue;
595     }
596     //
597     if (!mySplitFaces.HasImage(aS)) {
598       continue;
599     }
600     //
601     aF=TopoDS::Face(aS);
602     //
603     aLFx.Clear();
604     const TopTools_ListOfShape& aLF=mySplitFaces.Image(aF);
605     aIt.Initialize(aLF);
606     for (; aIt.More(); aIt.Next()) {
607       aFSp=TopoDS::Face(aIt.Value());
608       if (!mySameDomainShapes.Contains(aFSp)) {
609         aLFx.Append(aFSp);
610       }
611       else {
612         const TopoDS_Shape& aSx=mySameDomainShapes.FindFromKey(aFSp);
613         aFSD=TopoDS::Face(aSx);
614         iSense=GEOMAlgo_Tools3D::Sense(aFSp, aFSD);
615         if (iSense<0) {
616           aFSD.Reverse();
617         }
618         aLFx.Append(aFSD);
619       }
620     }
621     if (!myImages.HasImage(aF)) {
622       aNbLFx=aLFx.Extent();
623       if (aNbLFx==1) {
624         const TopoDS_Shape& aFx=aLFx.First();
625         if (aF.IsSame(aFx)) {
626           continue;
627         }
628       }
629       myImages.Bind(aF, aLFx);
630     }
631   }
632 }
633
634 //=======================================================================
635 // function: FillInternalVertices
636 // purpose:
637 //=======================================================================
638   void GEOMAlgo_Builder::FillInternalVertices()
639 {
640   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
641   NMTTools_PaveFiller* pPF=myPaveFiller;
642   NMTDS_InterfPool* pIP=pPF->IP();
643   IntTools_Context& aCtx= pPF->ChangeContext();
644   //
645   BOPTools_CArray1OfSSInterference& aFFs=pIP->SSInterferences();
646   BOPTools_CArray1OfVSInterference& aVFs=pIP->VSInterferences();
647   BOPTools_CArray1OfESInterference& aEFs=pIP->ESInterferences();
648   const NMTTools_IndexedDataMapOfIndexedMapOfInteger& aMAV=pPF->AloneVertices();
649   //
650   Standard_Boolean bHasImage;
651   Standard_Integer i, j, nF, aNbS, nV, nVSD, n1, n2, iFlag;
652   Standard_Integer aNbVFs, aNbAVF, aNbEFs, aNbVC, aNbE, aNbV;
653   Standard_Real aU1, aU2, aTol;
654   NMTTools_IndexedDataMapOfIndexedMapOfInteger aMFMV;
655   TopTools_MapOfShape aMFence;
656   TopTools_ListIteratorOfListOfShape aIt, aItV;
657   BRep_Builder aBB;
658   //
659   // 1. Collect face-vertex candidates [aMFMV]
660   //
661   // 1.1. VFs
662   aNbVFs=aVFs.Extent();
663   for (i=1; i<=aNbVFs; ++i) {
664     const BOPTools_VSInterference& aVS=aVFs(i);
665     aVS.Indices(n1, n2);
666     nF=n2;
667     nV=n1;
668     if (aDS.Shape(n1).ShapeType()==TopAbs_FACE) {
669       nF=n1;
670       nV=n2;
671     }
672     nVSD=pPF->FindSDVertex(nV);
673     if (nVSD) {
674       nV=nVSD;
675     }
676     //
677     UpdateCandidates(nF, nV, aMFMV);
678   }
679   //
680   // 1.2 EFs
681   aNbEFs=aEFs.Extent();
682   for (i=1; i<=aNbEFs; ++i) {
683     const BOPTools_ESInterference& aEF=aEFs(i);
684     aEF.Indices(n1, n2);
685     nV=aEF.NewShape();
686     if (!nV) {
687       continue;
688     }
689     const TopoDS_Shape& aSnew=aDS.Shape(nV);
690     if (aSnew.ShapeType()!=TopAbs_VERTEX) {
691       continue;
692     }
693     //
694     nF=(aDS.Shape(n1).ShapeType()==TopAbs_FACE) ? n1 : n2;
695     nVSD=pPF->FindSDVertex(nV);
696     if (nVSD) {
697       nV=nVSD;
698     }
699     UpdateCandidates(nF, nV, aMFMV);
700   }
701   //
702   aNbS=aDS.NumberOfShapesOfTheObject();
703   for (nF=1; nF<=aNbS; ++nF) {
704     const TopoDS_Shape& aF=aDS.Shape(nF);
705     //
706     if (aF.ShapeType()!=TopAbs_FACE) {
707       continue;
708     }
709     if (!aMFence.Add(aF)) {
710       continue;
711     }
712     //
713     const TopoDS_Face& aFF=TopoDS::Face(aF);
714     aTol=BRep_Tool::Tolerance(aFF);
715     //
716     // 1.3 FFs
717     if (aMAV.Contains(nF)) {
718       const TColStd_IndexedMapOfInteger& aMAVF=aMAV.FindFromKey(nF);
719       aNbAVF=aMAVF.Extent();
720       for (j=1; j<=aNbAVF; ++j) {
721         nV=aMAVF(j);
722         nVSD=pPF->FindSDVertex(nV);
723         if (nVSD) {
724           nV=nVSD;
725         }
726         //
727         UpdateCandidates(nF, nV, aMFMV);
728       }
729     }
730     //
731     // 1.4 Internal vertices of the face nF
732     BooleanOperations_OnceExplorer aExp(aDS);
733     aExp.Init(nF, TopAbs_VERTEX);
734     for (; aExp.More(); aExp.Next()) {
735       nV=aExp.Current();
736       const TopoDS_Shape& aV=aDS.Shape(nV);
737       if (aV.Orientation()==TopAbs_INTERNAL) {
738         nVSD=pPF->FindSDVertex(nV);
739         if (nVSD) {
740           nV=nVSD;
741         }
742         //
743         UpdateCandidates(nF, nV, aMFMV);
744       }
745     }
746     //
747     // 2. Process face nF
748     if (!aMFMV.Contains(nF)) {
749       continue;
750     }
751     //
752     const TColStd_IndexedMapOfInteger& aMVC=aMFMV.FindFromKey(nF);
753     aNbVC=aMVC.Extent();
754     if (!aNbVC) {
755       continue;
756     }
757     //
758     // 2.1 Refine candidates
759     TopTools_IndexedDataMapOfShapeListOfShape aMVE;
760     TopTools_ListOfShape aLV;
761     //
762     bHasImage=myImages.HasImage(aF);
763     if (bHasImage) {
764       const TopTools_ListOfShape& aLFx=myImages.Image(aF);
765       aIt.Initialize(aLFx);
766       for (; aIt.More(); aIt.Next()) {
767         const TopoDS_Shape& aFx=aIt.Value();
768         TopExp::MapShapesAndAncestors(aFx, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
769       }
770     }
771     else {
772       Standard_Boolean bFaceToProcess;
773       //
774       TopExp::MapShapesAndAncestors(aF, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
775       bFaceToProcess=Standard_False;
776       for (j=1; j<=aNbVC; ++j) {
777         nV=aMVC(j);
778         const TopoDS_Shape& aV=aDS.Shape(nV);
779         if (!aMVE.Contains(aV)) {
780           bFaceToProcess=!bFaceToProcess;
781           break;
782         }
783       }
784       if (!bFaceToProcess) {
785         continue;
786       }
787     }// else
788     //
789     for (j=1; j<=aNbVC; ++j) {
790       nV=aMVC(j);
791       const TopoDS_Shape& aV=aDS.Shape(nV);
792       if (aMVE.Contains(aV)) {
793         const TopTools_ListOfShape& aLE=aMVE.FindFromKey(aV);
794         aNbE=aLE.Extent();
795         if (aNbE) {
796           continue;
797         }
798       }
799       aLV.Append(aV);
800     }
801     //
802     aNbV=aLV.Extent();
803     if (aNbV) {
804       //  3. Try to put vertices into the face(s)
805       aItV.Initialize(aLV);
806       for (; aItV.More(); aItV.Next()) {
807         TopoDS_Vertex aV=TopoDS::Vertex(aItV.Value());
808         aV.Orientation(TopAbs_INTERNAL);
809         //
810         bHasImage=myImages.HasImage(aF);
811         if (bHasImage) {
812           const TopTools_ListOfShape& aLFx=myImages.Image(aF);
813           aIt.Initialize(aLFx);
814           for (; aIt.More(); aIt.Next()) {
815             TopoDS_Face aFx=TopoDS::Face(aIt.Value());
816             // update classifier
817             IntTools_FClass2d& aClsf=aCtx.FClass2d(aFx);
818             aClsf.Init(aFx, aTol);
819             //
820             iFlag=aCtx.ComputeVS (aV, aFx, aU1, aU2);
821             if (!iFlag) {
822               aBB.Add(aFx, aV);
823               break;
824             }
825           }
826         }
827         else {
828           const TopoDS_Face& aFx=TopoDS::Face(aF);
829           // update classifier
830           IntTools_FClass2d& aClsf=aCtx.FClass2d(aFx);
831           aClsf.Init(aFx, aTol);
832           //
833           iFlag=aCtx.ComputeVS (aV, aFx, aU1, aU2);
834           if (!iFlag) {
835             TopoDS_Face aFz;
836             //
837             GEOMAlgo_Tools3D::CopyFace(aFx, aFz);
838             aBB.Add(aFz, aV);
839             myImages.Bind(aF, aFz);
840           }
841         }
842       }// for (; aItV.More(); aItV.Next()) {
843     }// if (aNbV) {
844   }// for (nF=1; nF<=aNb; ++nF) {
845 }
846
847 //=======================================================================
848 // function: UpdateCandidates
849 // purpose:
850 //=======================================================================
851 void UpdateCandidates(const Standard_Integer theNF,
852                       const Standard_Integer theNV,
853                        NMTTools_IndexedDataMapOfIndexedMapOfInteger& theMFMV)
854 {
855   if (theMFMV.Contains(theNF)) {
856     TColStd_IndexedMapOfInteger& aMV=theMFMV.ChangeFromKey(theNF);
857     aMV.Add(theNV);
858   }
859   else {
860     TColStd_IndexedMapOfInteger aMV;
861     aMV.Add(theNV);
862     theMFMV.Add(theNF, aMV);
863   }
864 }
865
866 /*
867     {
868       TopoDS_Compound aCx;
869       BRep_Builder aBBx;
870       TopTools_ListIteratorOfListOfShape aItx;
871       //
872       aBBx.MakeCompound(aCx);
873       aBBx.Add(aCx, aFF);
874       aItx.Initialize(aSE);
875       for (; aItx.More(); aItx.Next()) {
876         TopoDS_Shape& aEx=aItx.Value();
877         aBBx.Add(aCx, aEx);
878       }
879       int a=0;
880     }
881     */