Salome HOME
0021024: EDF 1537 GEOM: IDs of the subshapes of a shape
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Builder_2.cxx
1 // Copyright (C) 2007-2011  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 #include <TopTools_DataMapOfShapeInteger.hxx>
99
100 static
101   void UpdateCandidates(const Standard_Integer ,
102                         const Standard_Integer ,
103                         NMTTools_IndexedDataMapOfIndexedMapOfInteger& );
104
105
106 //=======================================================================
107 //function : FillImagesFaces
108 //purpose  :
109 //=======================================================================
110   void GEOMAlgo_Builder::FillImagesFaces()
111 {
112   myErrorStatus=0;
113   //
114   FillIn2DParts();
115   BuildSplitFaces();
116   FillSameDomainFaces();
117   FillImagesFaces1();
118   FillInternalVertices();
119 }
120
121 //=======================================================================
122 // function: FillIn2DParts
123 // purpose:
124 //=======================================================================
125   void GEOMAlgo_Builder::FillIn2DParts()
126 {
127   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
128   NMTTools_PaveFiller* pPF=myPaveFiller;
129   NMTDS_InterfPool* pIP=pPF->IP();
130   BOPTools_CArray1OfSSInterference& aFFs=pIP->SSInterferences();
131   NMTTools_CommonBlockPool& aCBP=pPF->ChangeCommonBlockPool();
132   //
133   Standard_Integer  j, nSpIn, nSpSc, aNbCurves;
134   Standard_Integer aNbS, nF, aNbCBP, n1, n2, aNbFFs, aNbSpIn;
135   TopTools_MapOfShape  aMFence;
136   TopTools_ListOfShape aLSpIn;
137   TopoDS_Face aF;
138   NMTTools_ListIteratorOfListOfCommonBlock aItCB;
139   BOPTools_ListIteratorOfListOfPaveBlock aItPB;
140   //
141   myInParts.Clear();
142   //
143   aNbFFs=aFFs.Extent();
144   aNbCBP=aCBP.Extent();
145   //
146   aNbS=aDS.NumberOfShapesOfTheObject();
147   for (nF=1; nF<=aNbS; ++nF) {
148     if (aDS.GetShapeType(nF)!=TopAbs_FACE) {
149       continue;
150     }
151     //
152     aF=TopoDS::Face(aDS.Shape(nF));
153     //
154     aMFence.Clear();
155     aLSpIn.Clear();
156     //
157     // 1. In Parts
158     //modified by NIZNHY-PKV Fri Oct 14 13:58:00 2011f 
159     BOPTools_ListOfPaveBlock aLPBIn;
160     //
161     pPF->RealSplitsInFace(nF, aLPBIn);
162     //
163     aItPB.Initialize(aLPBIn);
164     for (; aItPB.More(); aItPB.Next()) {
165       const BOPTools_PaveBlock& aPB1=aItPB.Value();
166       nSpIn=aPB1.Edge();
167       const TopoDS_Shape& aSpIn=aDS.Shape(nSpIn);
168       aLSpIn.Append(aSpIn);
169     }
170     /*
171     for (j=1; j<=aNbCBP; ++j) {
172       NMTTools_ListOfCommonBlock& aLCB=aCBP(j);
173       aItCB.Initialize(aLCB);
174       for (; aItCB.More(); aItCB.Next()) {
175         NMTTools_CommonBlock& aCB=aItCB.Value();
176         if (aCB.IsPaveBlockOnFace(nF)) {
177           const BOPTools_PaveBlock& aPB1=aCB.PaveBlock1();
178           nSpIn=aPB1.Edge();
179           const TopoDS_Shape& aSpIn=aDS.Shape(nSpIn);
180           if (aMFence.Add(aSpIn)){
181             aLSpIn.Append(aSpIn);
182           }
183         }
184       }
185     }
186     */
187     //modified by NIZNHY-PKV Fri Oct 14 13:58:08 2011t
188     //
189     // 2. Section Parts
190     for (j=1; j<=aNbFFs; ++j) {
191       BOPTools_SSInterference& aFF=aFFs(j);
192       aFF.Indices(n1, n2);
193       if (!(n1==nF || n2==nF)) {
194         continue;
195       }
196       BOPTools_SequenceOfCurves& aSC=aFF.Curves();
197       aNbCurves=aSC.Length();
198       if (!aNbCurves) {
199         continue;
200       }
201       //
202       const BOPTools_Curve& aBC=aSC(1);
203       const BOPTools_ListOfPaveBlock& aLPB=aBC.NewPaveBlocks();
204       aItPB.Initialize(aLPB);
205       for (; aItPB.More(); aItPB.Next()) {
206         const BOPTools_PaveBlock& aPBSc=aItPB.Value();
207         nSpSc=aPBSc.Edge();
208         const TopoDS_Shape& aSpSc=aDS.Shape(nSpSc);
209         if (aMFence.Add(aSpSc)){
210           aLSpIn.Append(aSpSc);
211         }
212       }
213     }
214     aNbSpIn=aLSpIn.Extent();
215     if (aNbSpIn) {
216       myInParts.Add(aF, aLSpIn);
217     }
218   }//for (nF=1; nF<=aNbS; ++nF) {
219 }
220
221 //=======================================================================
222 // function: BuildSplitFaces
223 // purpose:
224 //=======================================================================
225   void GEOMAlgo_Builder::BuildSplitFaces()
226 {
227   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
228   NMTTools_PaveFiller* pPF=myPaveFiller;
229   NMTDS_InterfPool* pIP=pPF->IP();
230   BOPTools_CArray1OfSSInterference& aFFs=pIP->SSInterferences();
231   IntTools_Context& aCtx= pPF->ChangeContext();
232   //
233   Standard_Boolean bToReverse, bIsClosed, bIsDegenerated;
234   Standard_Integer i, aNb, aNbF, nF;
235   TopTools_MapOfShape aMFence;
236   TColStd_IndexedMapOfInteger aMFP;
237   TopExp_Explorer anExp;
238   TopoDS_Face aFF;
239   TopoDS_Edge aSp, aEE;
240   TopTools_ListIteratorOfListOfShape aIt;
241   TopAbs_Orientation anOriF, anOriE;
242   //
243   mySplitFaces.Clear();
244   //
245   // 1. Select Faces to process (MFP)
246   aNb=aDS.NumberOfShapesOfTheObject();
247   for (i=1; i<=aNb; ++i) {
248     const TopoDS_Shape& aF=aDS.Shape(i);
249     if (aF.ShapeType()!=TopAbs_FACE) {
250       continue;
251     }
252     if (!aMFence.Add(aF)) {
253       continue;
254     }
255     //
256     if (myInParts.Contains(aF)) {
257       aMFP.Add(i);
258       continue;
259     }
260     //
261     anExp.Init(aF, TopAbs_EDGE);
262     for (; anExp.More(); anExp.Next()) {
263       const TopoDS_Shape& aE=anExp.Current();
264       if (myImages.HasImage(aE)) {
265         aMFP.Add(i);
266         break;
267       }
268     }
269     //
270     //===
271     {
272       Standard_Integer aNbFFs, aNbSE, j, n1, n2;
273       //
274       aNbFFs=aFFs.Extent();
275       for (j=1; j<=aNbFFs; ++j) {
276         BOPTools_SSInterference& aFFj=aFFs(j);
277         aFFj.Indices(n1, n2);
278         if (!(n1==i || n2==i)) {
279           continue;
280         }
281         //
282         const TColStd_ListOfInteger& aLSE=aFFj.SharedEdges();
283         aNbSE=aLSE.Extent();
284         if (aNbSE) {
285           aMFP.Add(i);
286           break;
287         }
288       }
289     }
290     //===
291     //
292   }// for (i=1; i<=aNb; ++i)
293   //
294   // 2. ProcessFaces
295   aNbF=aMFP.Extent();
296   for (i=1; i<=aNbF; ++i) {
297     nF=aMFP(i);
298     const TopoDS_Face& aF=TopoDS::Face(aDS.Shape(nF));
299     anOriF=aF.Orientation();
300     aFF=aF;
301     aFF.Orientation(TopAbs_FORWARD);
302     //
303     aMFence.Clear();
304     //
305     // 2.1. Fill WES
306     GEOMAlgo_WireEdgeSet aWES;
307     aWES.SetFace(aFF);
308     //
309     //  2.1.1. Add Split parts
310     anExp.Init(aFF, TopAbs_EDGE);
311     for (; anExp.More(); anExp.Next()) {
312       const TopoDS_Edge& aE=TopoDS::Edge(anExp.Current());
313       anOriE=aE.Orientation();
314       //
315       if (!myImages.HasImage(aE)) {
316         if (anOriE==TopAbs_INTERNAL) {
317           aEE=aE;
318           aEE.Orientation(TopAbs_FORWARD);
319           aWES.AddStartElement(aEE);
320           aEE.Orientation(TopAbs_REVERSED);
321           aWES.AddStartElement(aEE);
322         }
323         else {
324           aWES.AddStartElement(aE);
325         }
326         continue;
327       }
328       //
329       bIsDegenerated=BRep_Tool::Degenerated(aE);
330       bIsClosed=BRep_Tool::IsClosed(aE, aF);
331       //
332       const TopTools_ListOfShape& aLIE=myImages.Image(aE);
333       aIt.Initialize(aLIE);
334       for (; aIt.More(); aIt.Next()) {
335         aSp=TopoDS::Edge(aIt.Value());
336         //
337         if (bIsDegenerated) {
338           aSp.Orientation(anOriE);
339           aWES.AddStartElement(aSp);
340           continue;
341         }
342         //
343         if (anOriE==TopAbs_INTERNAL) {
344           aSp.Orientation(TopAbs_FORWARD);
345           aWES.AddStartElement(aSp);
346           aSp.Orientation(TopAbs_REVERSED);
347           aWES.AddStartElement(aSp);
348           continue;
349         }
350         //
351         if (bIsClosed){
352           if (aMFence.Add(aSp)) {
353             //
354             if (!BRep_Tool::IsClosed(aSp, aF)){
355               BOPTools_Tools3D::DoSplitSEAMOnFace(aSp, aF);
356             }
357             //
358             aSp.Orientation(TopAbs_FORWARD);
359             aWES.AddStartElement(aSp);
360             aSp.Orientation(TopAbs_REVERSED);
361             aWES.AddStartElement(aSp);
362           }
363           continue;
364         }// if (aMFence.Add(aSp))
365         //
366         aSp.Orientation(anOriE);
367         bToReverse=BOPTools_Tools3D::IsSplitToReverse1(aSp, aE, aCtx);
368         if (bToReverse) {
369           aSp.Reverse();
370         }
371         aWES.AddStartElement(aSp);
372       }// for (; aIt.More(); aIt.Next()) {
373     }// for (; anExp.More(); anExp.Next()) {
374     //
375     // 2.1.2. Add In2D Parts
376     if (myInParts.Contains(aF)) {
377       const TopTools_ListOfShape& aLE=myInParts.FindFromKey(aF);
378       aIt.Initialize(aLE);
379       for (; aIt.More(); aIt.Next()) {
380         aSp=TopoDS::Edge(aIt.Value());
381         //
382         aSp.Orientation(TopAbs_FORWARD);
383         aWES.AddStartElement(aSp);
384         //
385         aSp.Orientation(TopAbs_REVERSED);
386         aWES.AddStartElement(aSp);
387       }
388     }
389     //
390     // 2.2. Build images Faces
391     TopTools_ListOfShape aLFR;
392     GEOMAlgo_ShapeSet aS1, aS2;
393     //
394     const TopTools_ListOfShape& aSE=aWES.StartElements();
395     aS1.Add(aSE);
396     aS2.Add(aFF, TopAbs_EDGE);
397     if (aS1.IsEqual(aS2)) {
398       aLFR.Append(aF);
399     }
400     else {
401       GEOMAlgo_BuilderFace aBF;
402       //
403       aBF.SetFace(aFF);
404       aBF.SetContext(aCtx);
405       aBF.SetShapes(aSE);
406       // <-DEB
407       aBF.Perform();
408       //
409       const TopTools_ListOfShape& aLF=aBF.Areas();
410       aIt.Initialize(aLF);
411       for (; aIt.More(); aIt.Next()) {
412         TopoDS_Shape& aFR=aIt.Value();
413         if (anOriF==TopAbs_REVERSED) {
414           aFR.Orientation(TopAbs_REVERSED);
415         }
416         aLFR.Append(aFR);
417       }
418     }
419     //
420     // 2.3. Collect draft images Faces
421     mySplitFaces.Bind(aF, aLFR);
422   }//for (i=1; i<=aNbF; ++i)
423 }
424
425 //=======================================================================
426 // function: FillSameDomainFaces
427 // purpose:
428 //=======================================================================
429   void GEOMAlgo_Builder::FillSameDomainFaces()
430 {
431   Standard_Boolean bIsSDF, bHasImage1, bHasImage2, bForward;
432   Standard_Integer i, j, aNbFF, nF1, nF2, aNbPBInOn, aNbC, aNbSE;
433   Standard_Integer aNbF1, aNbF2, i2s, aNbSD;
434   TopTools_MapOfShape aMFence;
435   TopTools_ListOfShape aLX1, aLX2;
436   TopTools_ListIteratorOfListOfShape aItF1, aItF2;
437   NMTTools_ListOfCoupleOfShape aLCS;
438   //
439   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
440   NMTTools_PaveFiller* pPF=myPaveFiller;
441   NMTDS_InterfPool* pIP=pPF->IP();
442   BOPTools_CArray1OfSSInterference& aFFs=pIP->SSInterferences();
443   IntTools_Context& aCtx= pPF->ChangeContext();
444   //
445   //
446   //mySameDomainShapes.Clear();
447   //
448   // 1. For each FF find among images of faces
449   //    all pairs of same domain faces (SDF) [=> aLCS]
450   aNbFF=aFFs.Extent();
451   for (i=1; i<=aNbFF; ++i) {
452     BOPTools_SSInterference& aFF=aFFs(i);
453     aFF.Indices(nF1, nF2);
454     //
455     const TopoDS_Face& aF1=TopoDS::Face(aDS.Shape(nF1));
456     const TopoDS_Face& aF2=TopoDS::Face(aDS.Shape(nF2));
457     //
458     // if there are no in/on 2D split parts the faces nF1, nF2
459     // can not be SDF
460     const BOPTools_ListOfPaveBlock& aLPBInOn=aFF.PaveBlocks();
461     aNbPBInOn=aLPBInOn.Extent();
462     //
463     //===
464     const TColStd_ListOfInteger& aLSE=aFF.SharedEdges();
465     aNbSE=aLSE.Extent();
466     if (!aNbPBInOn && !aNbSE) {
467       continue;
468     }
469     //===
470     //
471     // if there is at least one section edge between faces nF1, nF2
472     // they can not be SDF
473     BOPTools_SequenceOfCurves& aSC=aFF.Curves();
474     aNbC=aSC.Length();
475     if (aNbC) {
476       continue;
477     }
478     //
479     // the faces are suspected to be SDF.
480     // Try to find SDF among images of nF1, nF2
481     aMFence.Clear();
482     //
483     //--------------------------------------------------------
484     bHasImage1=mySplitFaces.HasImage(aF1);
485     bHasImage2=mySplitFaces.HasImage(aF2);
486     //
487     aLX1.Clear();
488     if (!bHasImage1) {
489       aLX1.Append(aF1);
490     }
491     //
492     aLX2.Clear();
493     if (!bHasImage2) {
494       aLX2.Append(aF2);
495     }
496     //
497     const TopTools_ListOfShape& aLF1r=(bHasImage1)? mySplitFaces.Image(aF1) : aLX1;
498     const TopTools_ListOfShape& aLF2r=(bHasImage2)? mySplitFaces.Image(aF2) : aLX2;
499     //
500     TopTools_DataMapOfIntegerShape aMIS;
501     TColStd_ListIteratorOfListOfInteger aItLI;
502     NMTDS_BoxBndTreeSelector aSelector;
503     NMTDS_BoxBndTree aBBTree;
504     NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
505     //
506     aNbF1=aLF1r.Extent();
507     aNbF2=aLF2r.Extent();
508     bForward=(aNbF1<aNbF2);
509     //
510     const TopTools_ListOfShape& aLF1=bForward ? aLF1r : aLF2r;
511     const TopTools_ListOfShape& aLF2=bForward ? aLF2r : aLF1r;
512     //
513     // 1. aTreeFiller
514     aItF2.Initialize(aLF2);
515     for (i2s=1; aItF2.More(); aItF2.Next(), ++i2s) {
516       Bnd_Box aBoxF2s;
517       //
518       const TopoDS_Face& aF2s=*((TopoDS_Face*)(&aItF2.Value()));
519       //
520       BRepBndLib::Add(aF2s, aBoxF2s);
521       //
522       aMIS.Bind(i2s, aF2s);
523       //
524       aTreeFiller.Add(i2s, aBoxF2s);
525     }//for (i2s=1; aItF2.More(); aItF2.Next(), ++i2s) {
526     //
527     aTreeFiller.Fill();
528     //
529     // 2.
530     aItF1.Initialize(aLF1);
531     for (j=1; aItF1.More(); aItF1.Next(), ++j) {
532       Bnd_Box aBoxF1x;
533       //
534       const TopoDS_Face& aF1x=*((TopoDS_Face*)(&aItF1.Value()));
535       //
536       BRepBndLib::Add(aF1x, aBoxF1x);
537       //
538       aSelector.Clear();
539       aSelector.SetBox(aBoxF1x);
540       aNbSD=aBBTree.Select(aSelector);
541       if (!aNbSD) {
542         continue;
543       }
544       //
545       const TColStd_ListOfInteger& aLI=aSelector.Indices();
546       aItLI.Initialize(aLI);
547       for (; aItLI.More(); aItLI.Next()) {
548         i2s=aItLI.Value();
549         const TopoDS_Face& aF2y=*((TopoDS_Face*)(&aMIS.Find(i2s)));
550         //
551         bIsSDF=NMTTools_Tools::AreFacesSameDomain(aF1x, aF2y, aCtx);
552         if (bIsSDF) {
553           if (aMFence.Contains(aF1x) || aMFence.Contains(aF2y)) {
554             continue;
555           }
556           aMFence.Add(aF1x);
557           aMFence.Add(aF2y);
558           //
559           NMTTools_CoupleOfShape aCS;
560           //
561           aCS.SetShape1(aF1x);
562           aCS.SetShape2(aF2y);
563           aLCS.Append(aCS);
564           //
565           if (bForward) {
566             if (aF1x==aF1) {
567               if (!mySplitFaces.HasImage(aF1)) {
568                 mySplitFaces.Bind(aF1, aF1);
569               }
570             }
571             if (aF2y==aF2) {
572               if (!mySplitFaces.HasImage(aF2)) {
573                 mySplitFaces.Bind(aF2, aF2);
574               }
575             }
576           }
577           else {
578             if (aF1x==aF2) {
579               if (!mySplitFaces.HasImage(aF2)) {
580                 mySplitFaces.Bind(aF2, aF2);
581               }
582             }
583             if (aF2y==aF1) {
584               if (!mySplitFaces.HasImage(aF1)) {
585                 mySplitFaces.Bind(aF1, aF1);
586               }
587             }
588           }
589           //
590           break;
591         }//if (bIsSDF) {
592       }//for (; aItLI.More(); aItLI.Next()) {
593     }//for (; aItF1.More(); aItF1.Next()) {
594   }//for (i=1; i<=aNbFF; ++i)
595   //-------------------------------------------------------------
596   aNbC=aLCS.Extent();
597   if (!aNbC) {
598     return;
599   }
600   //
601   // 2. Find Chains
602   NMTTools_IndexedDataMapOfShapeIndexedMapOfShape aMC;
603   //
604   NMTTools_Tools::FindChains(aLCS, aMC); 
605   //
606   //modified by NIZNHY-PKV Wed Oct 12 13:33:59 2011f
607   Standard_Boolean bIsImage;
608   Standard_Integer aIx, aIxMin, aNbMSDF, k, aNbMFj;
609   TopoDS_Shape aFOld, aFSDmin;
610   TopTools_IndexedMapOfShape aMFj;
611   TopTools_DataMapOfShapeInteger aDMSI;
612   //
613   aItF1.Initialize(myShapes);
614   for (j=1; aItF1.More(); aItF1.Next(), ++j) {
615     const TopoDS_Shape& aSj=aItF1.Value();
616     aMFj.Clear();
617     TopExp::MapShapes(aSj, TopAbs_FACE, aMFj);
618     aNbMFj=aMFj.Extent();
619     for (k=1; k<=aNbMFj; ++k) {
620       const TopoDS_Shape& aFk=aMFj(k);
621       if (!aDMSI.IsBound(aFk)) {
622         aDMSI.Bind(aFk, j);
623       }
624     }
625   }
626   //
627   //modified by NIZNHY-PKV Wed Oct 12 13:34:01 2011t
628   //
629   // 3. Fill the map of SDF mySameDomainFaces
630   aNbC=aMC.Extent();
631   for (i=1; i<=aNbC; ++i) {
632    // const TopoDS_Shape& aF=aMC.FindKey(i);
633     const TopTools_IndexedMapOfShape& aMSDF=aMC(i);
634     //
635     //modified by NIZNHY-PKV Wed Oct 12 13:25:16 2011f
636     aNbMSDF=aMSDF.Extent();
637     for (j=1; j<=aNbMSDF; ++j) {
638       const TopoDS_Shape& aFSD=aMSDF(j);
639       bIsImage=mySplitFaces.IsImage(aFSD);
640       aFOld=aFSD;
641       if (bIsImage) {
642         aFOld=mySplitFaces.ImageFrom(aFSD);
643       }
644       //
645       aIx=aDMSI.Find(aFOld);
646       if (j==1) {
647         aIxMin=aIx;
648         aFSDmin=aFSD;
649         continue;
650       }
651       else {
652         if (aIx<aIxMin) {
653           aIxMin=aIx;
654           aFSDmin=aFSD;
655         }
656       }
657     }
658     //
659     for (j=1; j<=aNbMSDF; ++j) {
660       const TopoDS_Shape& aFSD=aMSDF(j);
661       mySameDomainShapes.Add(aFSD, aFSDmin);
662     }
663     /*
664     aNbFF=aMSDF.Extent();
665     for (j=1; j<=aNbFF; ++j) {
666       const TopoDS_Shape& aFSD=aMSDF(j);
667       mySameDomainShapes.Add(aFSD, aF);
668     }
669     */
670     //modified by NIZNHY-PKV Wed Oct 12 13:25:18 2011t
671   }
672   //
673 }
674
675 //=======================================================================
676 // function: FillImagesFaces1
677 // purpose:
678 //=======================================================================
679   void GEOMAlgo_Builder::FillImagesFaces1()
680 {
681   Standard_Integer i, aNb, iSense, aNbLFx;
682   TopoDS_Face aF, aFSp, aFSD;
683   TopTools_ListOfShape aLFx;
684   TopTools_ListIteratorOfListOfShape aIt;
685   //
686   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
687   //
688   aNb=aDS.NumberOfShapesOfTheObject();
689   for (i=1; i<=aNb; ++i) {
690     const TopoDS_Shape& aS=aDS.Shape(i);
691     if (aS.ShapeType()!=TopAbs_FACE) {
692       continue;
693     }
694     //
695     if (!mySplitFaces.HasImage(aS)) {
696       continue;
697     }
698     //
699     aF=*((TopoDS_Face*)&aS);
700     //
701     aLFx.Clear();
702     const TopTools_ListOfShape& aLF=mySplitFaces.Image(aF);
703     aIt.Initialize(aLF);
704     for (; aIt.More(); aIt.Next()) {
705       aFSp=*((TopoDS_Face*)(&aIt.Value()));
706       if (!mySameDomainShapes.Contains(aFSp)) {
707         aLFx.Append(aFSp);
708       }
709       else {
710         const TopoDS_Shape& aSx=mySameDomainShapes.FindFromKey(aFSp);
711         aFSD=*((TopoDS_Face*)(&aSx));
712         iSense=GEOMAlgo_Tools3D::Sense(aFSp, aFSD);
713         if (iSense<0) {
714           aFSD.Reverse();
715         }
716         aLFx.Append(aFSD);
717       }
718     }
719     //
720     if (!myImages.HasImage(aF)) {
721       aNbLFx=aLFx.Extent();
722       if (aNbLFx==1) {
723         const TopoDS_Shape& aFx=aLFx.First();
724         if (aF.IsSame(aFx)) {
725           continue;
726         }
727       }
728       myImages.Bind(aF, aLFx);
729     }
730   }
731 }
732
733 //=======================================================================
734 // function: FillInternalVertices
735 // purpose:
736 //=======================================================================
737   void GEOMAlgo_Builder::FillInternalVertices()
738 {
739   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
740   NMTTools_PaveFiller* pPF=myPaveFiller;
741   NMTDS_InterfPool* pIP=pPF->IP();
742   IntTools_Context& aCtx= pPF->ChangeContext();
743   //
744   /*BOPTools_CArray1OfSSInterference& aFFs=*/pIP->SSInterferences();
745   BOPTools_CArray1OfVSInterference& aVFs=pIP->VSInterferences();
746   BOPTools_CArray1OfESInterference& aEFs=pIP->ESInterferences();
747   const NMTTools_IndexedDataMapOfIndexedMapOfInteger& aMAV=pPF->AloneVertices();
748   //
749   Standard_Boolean bHasImage;
750   Standard_Integer i, j, nF, aNbS, nV, nVSD, n1, n2, iFlag;
751   Standard_Integer aNbVFs, aNbAVF, aNbEFs, aNbVC, aNbE, aNbV;
752   Standard_Real aU1, aU2, aTol;
753   NMTTools_IndexedDataMapOfIndexedMapOfInteger aMFMV;
754   TopTools_MapOfShape aMFence;
755   TopTools_ListIteratorOfListOfShape aIt, aItV;
756   BRep_Builder aBB;
757   //
758   // 1. Collect face-vertex candidates [aMFMV]
759   //
760   // 1.1. VFs
761   aNbVFs=aVFs.Extent();
762   for (i=1; i<=aNbVFs; ++i) {
763     const BOPTools_VSInterference& aVS=aVFs(i);
764     aVS.Indices(n1, n2);
765     nF=n2;
766     nV=n1;
767     if (aDS.Shape(n1).ShapeType()==TopAbs_FACE) {
768       nF=n1;
769       nV=n2;
770     }
771     nVSD=pPF->FindSDVertex(nV);
772     if (nVSD) {
773       nV=nVSD;
774     }
775     //
776     UpdateCandidates(nF, nV, aMFMV);
777   }
778   //
779   // 1.2 EFs
780   aNbEFs=aEFs.Extent();
781   for (i=1; i<=aNbEFs; ++i) {
782     const BOPTools_ESInterference& aEF=aEFs(i);
783     aEF.Indices(n1, n2);
784     nV=aEF.NewShape();
785     if (!nV) {
786       continue;
787     }
788     const TopoDS_Shape& aSnew=aDS.Shape(nV);
789     if (aSnew.ShapeType()!=TopAbs_VERTEX) {
790       continue;
791     }
792     //
793     nF=(aDS.Shape(n1).ShapeType()==TopAbs_FACE) ? n1 : n2;
794     nVSD=pPF->FindSDVertex(nV);
795     if (nVSD) {
796       nV=nVSD;
797     }
798     UpdateCandidates(nF, nV, aMFMV);
799   }
800   //
801   aNbS=aDS.NumberOfShapesOfTheObject();
802   for (nF=1; nF<=aNbS; ++nF) {
803     const TopoDS_Shape& aF=aDS.Shape(nF);
804     //
805     if (aF.ShapeType()!=TopAbs_FACE) {
806       continue;
807     }
808     if (!aMFence.Add(aF)) {
809       continue;
810     }
811     //
812     const TopoDS_Face& aFF=TopoDS::Face(aF);
813     aTol=BRep_Tool::Tolerance(aFF);
814     //
815     // 1.3 FFs
816     if (aMAV.Contains(nF)) {
817       const TColStd_IndexedMapOfInteger& aMAVF=aMAV.FindFromKey(nF);
818       aNbAVF=aMAVF.Extent();
819       for (j=1; j<=aNbAVF; ++j) {
820         nV=aMAVF(j);
821         nVSD=pPF->FindSDVertex(nV);
822         if (nVSD) {
823           nV=nVSD;
824         }
825         //
826         UpdateCandidates(nF, nV, aMFMV);
827       }
828     }
829     //
830     // 1.4 Internal vertices of the face nF
831     BooleanOperations_OnceExplorer aExp(aDS);
832     aExp.Init(nF, TopAbs_VERTEX);
833     for (; aExp.More(); aExp.Next()) {
834       nV=aExp.Current();
835       const TopoDS_Shape& aV=aDS.Shape(nV);
836       if (aV.Orientation()==TopAbs_INTERNAL) {
837         nVSD=pPF->FindSDVertex(nV);
838         if (nVSD) {
839           nV=nVSD;
840         }
841         //
842         UpdateCandidates(nF, nV, aMFMV);
843       }
844     }
845     //
846     // 2. Process face nF
847     if (!aMFMV.Contains(nF)) {
848       continue;
849     }
850     //
851     const TColStd_IndexedMapOfInteger& aMVC=aMFMV.FindFromKey(nF);
852     aNbVC=aMVC.Extent();
853     if (!aNbVC) {
854       continue;
855     }
856     //
857     // 2.1 Refine candidates
858     TopTools_IndexedDataMapOfShapeListOfShape aMVE;
859     TopTools_ListOfShape aLV;
860     //
861     bHasImage=myImages.HasImage(aF);
862     if (bHasImage) {
863       const TopTools_ListOfShape& aLFx=myImages.Image(aF);
864       aIt.Initialize(aLFx);
865       for (; aIt.More(); aIt.Next()) {
866         const TopoDS_Shape& aFx=aIt.Value();
867         TopExp::MapShapesAndAncestors(aFx, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
868       }
869     }
870     else {
871       Standard_Boolean bFaceToProcess;
872       //
873       TopExp::MapShapesAndAncestors(aF, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
874       bFaceToProcess=Standard_False;
875       for (j=1; j<=aNbVC; ++j) {
876         nV=aMVC(j);
877         const TopoDS_Shape& aV=aDS.Shape(nV);
878         if (!aMVE.Contains(aV)) {
879           bFaceToProcess=!bFaceToProcess;
880           break;
881         }
882       }
883       if (!bFaceToProcess) {
884         continue;
885       }
886     }// else
887     //
888     for (j=1; j<=aNbVC; ++j) {
889       nV=aMVC(j);
890       const TopoDS_Shape& aV=aDS.Shape(nV);
891       if (aMVE.Contains(aV)) {
892         const TopTools_ListOfShape& aLE=aMVE.FindFromKey(aV);
893         aNbE=aLE.Extent();
894         if (aNbE) {
895           continue;
896         }
897       }
898       aLV.Append(aV);
899     }
900     //
901     aNbV=aLV.Extent();
902     if (aNbV) {
903       //  3. Try to put vertices into the face(s)
904       aItV.Initialize(aLV);
905       for (; aItV.More(); aItV.Next()) {
906         TopoDS_Vertex aV=TopoDS::Vertex(aItV.Value());
907         aV.Orientation(TopAbs_INTERNAL);
908         //
909         bHasImage=myImages.HasImage(aF);
910         if (bHasImage) {
911           const TopTools_ListOfShape& aLFx=myImages.Image(aF);
912           aIt.Initialize(aLFx);
913           for (; aIt.More(); aIt.Next()) {
914             TopoDS_Face aFx=TopoDS::Face(aIt.Value());
915             // update classifier
916             IntTools_FClass2d& aClsf=aCtx.FClass2d(aFx);
917             aClsf.Init(aFx, aTol);
918             //
919             iFlag=aCtx.ComputeVS (aV, aFx, aU1, aU2);
920             if (!iFlag) {
921               aBB.Add(aFx, aV);
922               break;
923             }
924           }
925         }
926         else {
927           const TopoDS_Face& aFx=TopoDS::Face(aF);
928           // update classifier
929           IntTools_FClass2d& aClsf=aCtx.FClass2d(aFx);
930           aClsf.Init(aFx, aTol);
931           //
932           iFlag=aCtx.ComputeVS (aV, aFx, aU1, aU2);
933           if (!iFlag) {
934             TopoDS_Face aFz;
935             //
936             GEOMAlgo_Tools3D::CopyFace(aFx, aFz);
937             aBB.Add(aFz, aV);
938             myImages.Bind(aF, aFz);
939           }
940         }
941       }// for (; aItV.More(); aItV.Next()) {
942     }// if (aNbV) {
943   }// for (nF=1; nF<=aNb; ++nF) {
944 }
945
946 //=======================================================================
947 // function: UpdateCandidates
948 // purpose:
949 //=======================================================================
950 void UpdateCandidates(const Standard_Integer theNF,
951                       const Standard_Integer theNV,
952                        NMTTools_IndexedDataMapOfIndexedMapOfInteger& theMFMV)
953 {
954   if (theMFMV.Contains(theNF)) {
955     TColStd_IndexedMapOfInteger& aMV=theMFMV.ChangeFromKey(theNF);
956     aMV.Add(theNV);
957   }
958   else {
959     TColStd_IndexedMapOfInteger aMV;
960     aMV.Add(theNV);
961     theMFMV.Add(theNF, aMV);
962   }
963 }
964
965 /*
966     {
967       TopoDS_Compound aCx;
968       BRep_Builder aBBx;
969       TopTools_ListIteratorOfListOfShape aItx;
970       //
971       aBBx.MakeCompound(aCx);
972       aBBx.Add(aCx, aFF);
973       aItx.Initialize(aSE);
974       for (; aItx.More(); aItx.Next()) {
975         TopoDS_Shape& aEx=aItx.Value();
976         aBBx.Add(aCx, aEx);
977       }
978       int a=0;
979     }
980     */