Salome HOME
New version of PartitionAlgo.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Builder_1.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:        GEOMAlgo_Builder_1.cxx
21 // Created:     
22 // Author:      Peter KURNEV 
23
24 #include <GEOMAlgo_Builder.hxx>
25 //
26 #include <TColStd_ListOfInteger.hxx>
27
28 #include <TopoDS.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Wire.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS_Shell.hxx>
33 #include <TopoDS_Iterator.hxx>
34 //
35 #include <TopTools_MapOfShape.hxx>
36 #include <TopTools_ListOfShape.hxx>
37 #include <TopTools_ListIteratorOfListOfShape.hxx>
38 #include <TopTools_MapIteratorOfMapOfShape.hxx>
39 //
40 #include <BRep_Tool.hxx>
41 #include <BRep_Builder.hxx>
42 //
43 #include <IntTools_Context.hxx>
44 #include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
45 #include <BOPTools_ListOfPaveBlock.hxx>
46 #include <BOPTools_PaveBlock.hxx>
47 //
48 #include <NMTDS_ShapesDataStructure.hxx>
49 //
50 #include <NMTTools_DSFiller.hxx>
51 #include <NMTTools_PaveFiller.hxx>
52 #include <NMTTools_CommonBlockPool.hxx>
53 #include <NMTTools_ListIteratorOfListOfCommonBlock.hxx>
54 #include <NMTTools_CommonBlock.hxx>
55 #include <NMTTools_CommonBlockPool.hxx>
56 #include <NMTTools_ListOfCommonBlock.hxx>
57 #include <NMTTools_CommonBlockAPI.hxx>
58 //
59 #include <GEOMAlgo_Tools3D.hxx>
60 #include <TColStd_ListIteratorOfListOfInteger.hxx>
61
62
63
64 static
65   void FillImagesCompounds(const TopTools_MapOfShape& ,
66                            BRepAlgo_Image& );
67
68 static
69   void FillImagesCompound(const TopoDS_Shape& ,
70                         BRepAlgo_Image& ,
71                         TopTools_MapOfShape& );
72
73 //=======================================================================
74 //function : FillImagesVertices
75 //purpose  : 
76 //=======================================================================
77   void GEOMAlgo_Builder::FillImagesVertices()
78 {
79   myErrorStatus=0;
80   //
81   const NMTDS_ShapesDataStructure& aDS=myDSFiller->DS();
82   NMTTools_PaveFiller* pPF=(NMTTools_PaveFiller*)&(myDSFiller->PaveFiller());
83   //
84   Standard_Integer i, aNb, iV;
85   //
86   aNb=aDS.NumberOfShapesOfTheObject();
87   for (i=1; i<=aNb; ++i) {
88     const TopoDS_Shape& aV=aDS.Shape(i);
89     if (aV.ShapeType()==TopAbs_VERTEX) {
90       iV=pPF->FindSDVertex(i);
91       if (iV) {
92         const TopoDS_Shape& aVSD=aDS.Shape(iV);
93         if (!myImages.HasImage(aV)) {
94           myImages.Bind(aV, aVSD);
95           //
96           mySameDomainShapes.Add(aV, aVSD);
97           //
98         }
99       }
100     }
101   }
102 }
103 //=======================================================================
104 // function: FillImagesEdges
105 // purpose: 
106 //=======================================================================
107   void GEOMAlgo_Builder::FillImagesEdges()
108 {
109   myErrorStatus=0;
110   //
111   const NMTDS_ShapesDataStructure& aDS=myDSFiller->DS();
112   NMTTools_PaveFiller* pPF=(NMTTools_PaveFiller*)&(myDSFiller->PaveFiller());
113   const BOPTools_SplitShapesPool& aSSP=pPF->SplitShapesPool();
114   NMTTools_CommonBlockPool& aCBP=pPF->ChangeCommonBlockPool();
115   IntTools_Context& aCtx=pPF->ChangeContext();
116   //
117   Standard_Boolean bToReverse;
118   Standard_Integer i, aNb, aNbSp, nSp, nSpR, nSpx;
119   TColStd_ListIteratorOfListOfInteger aItLB;
120   TColStd_ListOfInteger aLB;
121   TopoDS_Edge aEE, aESpR;
122   TopTools_MapOfShape aMFence;
123   TopTools_ListOfShape aLSp;
124   TopTools_ListIteratorOfListOfShape aIt1;
125   BOPTools_ListIteratorOfListOfPaveBlock aIt;
126   //
127   aNb=aDS.NumberOfShapesOfTheObject();
128   for (i=1; i<=aNb; ++i) {
129     const TopoDS_Shape& aE=aDS.Shape(i);
130     if (aE.ShapeType()!=TopAbs_EDGE) {
131       continue;
132     }
133     //
134     if (!aMFence.Add(aE)) {
135       continue;
136     }
137     //
138     const BOPTools_ListOfPaveBlock& aLPB=aSSP(aDS.RefEdge(i));
139     aNbSp=aLPB.Extent();
140     if (!aNbSp) {
141       continue;
142     }
143     //
144     aEE=TopoDS::Edge(aE);
145     aLSp.Clear();
146     //
147     if (aNbSp==1) {
148       const BOPTools_PaveBlock& aPB=aLPB.First();
149       nSp=aPB.Edge();
150       const TopoDS_Shape& aSp=aDS.Shape(nSp);
151       //
152       const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB);
153       nSpR=aPBR.Edge();
154       const TopoDS_Shape& aSpR=aDS.Shape(nSpR);
155       if (aSpR.IsSame(aSp) && aSpR.IsSame(aE)) {
156         continue;
157       }
158       //
159       aESpR=TopoDS::Edge(aSpR);
160       bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aESpR, aEE, aCtx);
161       if (bToReverse) {
162         aESpR.Reverse();
163       }
164       aLSp.Append(aESpR);
165       //
166       aItLB.Initialize(aLB);
167       for (; aItLB.More(); aItLB.Next()) {
168         nSpx=aItLB.Value();
169         const TopoDS_Shape& aSpx=aDS.Shape(nSpx);
170         mySameDomainShapes.Add(aSpx ,aSpR);
171       }
172       //
173       //
174     }// if (aNbSp==1) {
175     else {
176       aIt.Initialize(aLPB);
177       for (; aIt.More(); aIt.Next()) {
178         const BOPTools_PaveBlock& aPB=aIt.Value();
179         const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB);
180         nSpR=aPBR.Edge();
181         const TopoDS_Shape& aSpR=aDS.Shape(nSpR);
182         //
183         aESpR=TopoDS::Edge(aSpR);
184         bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aESpR, aEE, aCtx);
185         if (bToReverse) {
186           aESpR.Reverse();
187         }
188         aLSp.Append(aESpR);
189         //
190         aItLB.Initialize(aLB);
191         for (; aItLB.More(); aItLB.Next()) {
192           nSpx=aItLB.Value();
193           const TopoDS_Shape& aSpx=aDS.Shape(nSpx);
194           mySameDomainShapes.Add(aSpx ,aSpR);
195         }
196         //
197       }
198     }
199     //
200     myImages.Bind(aE, aLSp);
201     
202   }//for (i=1; i<=aNb; ++i)
203 }
204 //=======================================================================
205 // function: FillImagesContainers
206 // purpose: 
207 //=======================================================================
208   void GEOMAlgo_Builder::FillImagesContainers(const TopAbs_ShapeEnum theType)
209 {
210   myErrorStatus=0;
211   //
212   Standard_Boolean bInterferred, bToReverse;
213   Standard_Integer i, aNbS;
214   TopAbs_ShapeEnum aType;
215   BRep_Builder aBB;
216   TopoDS_Iterator aIt;
217   TopTools_ListIteratorOfListOfShape aItIm; 
218   TopTools_MapOfShape aMS;
219   TopTools_MapIteratorOfMapOfShape aItS;
220   //
221   const NMTDS_ShapesDataStructure& aDS=myDSFiller->DS();
222   NMTTools_PaveFiller* pPF=(NMTTools_PaveFiller*)&(myDSFiller->PaveFiller());
223   IntTools_Context& aCtx= pPF->ChangeContext();
224   //
225   aNbS=aDS.NumberOfShapesOfTheObject();
226   for (i=1; i<=aNbS; ++i) {
227     const TopoDS_Shape& aC=aDS.Shape(i);
228     aType=aC.ShapeType();
229     if (aType==theType) {
230       aMS.Add(aC);
231     }
232   }
233   //
234   if (theType==TopAbs_COMPOUND) {
235     FillImagesCompounds(aMS, myImages);
236     return;
237   }
238   //
239   aItS.Initialize(aMS);
240   for (; aItS.More(); aItS.Next()) {
241     const TopoDS_Shape& aC=aItS.Key();
242     // whether the shape has image
243     bInterferred=Standard_False;
244     aIt.Initialize(aC);
245     for (; aIt.More(); aIt.Next()) {
246       const TopoDS_Shape& aF=aIt.Value();
247       if (myImages.HasImage(aF)) {
248         bInterferred=!bInterferred;
249         break;
250       }
251     }
252     if (!bInterferred){
253       continue;
254     }
255     //
256     TopoDS_Shape aCIm;
257     GEOMAlgo_Tools3D::MakeContainer(theType, aCIm);
258     //
259     aIt.Initialize(aC);
260     for (; aIt.More(); aIt.Next()) {
261       const TopoDS_Shape& aF=aIt.Value();
262       if (myImages.HasImage(aF)) {
263         const TopTools_ListOfShape& aLFIm=myImages.Image(aF);
264         aItIm.Initialize(aLFIm);
265         for (; aItIm.More(); aItIm.Next()) {
266           TopoDS_Shape aFIm=aItIm.Value();
267           //
268           bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aFIm, aF, aCtx);
269           if (bToReverse) {
270             aFIm.Reverse();
271           }
272           aBB.Add(aCIm, aFIm);
273         }
274       }
275       else {
276         aBB.Add(aCIm, aF);
277       }
278     }
279     myImages.Bind(aC, aCIm); 
280   }// for (; aItS.More(); aItS.Next()) {
281 }
282 //=======================================================================
283 // function: FillImagesCompounds
284 // purpose: 
285 //=======================================================================
286 void FillImagesCompounds(const TopTools_MapOfShape& theMS,
287                          BRepAlgo_Image& theImages)
288 {
289   TopTools_MapOfShape aMFP;
290   TopTools_MapIteratorOfMapOfShape aItS;
291   //
292   aItS.Initialize(theMS);
293   for (; aItS.More(); aItS.Next()) {
294     const TopoDS_Shape& aC=aItS.Key();
295     FillImagesCompound(aC, theImages, aMFP);
296   }
297 }
298 //=======================================================================
299 //function : FillImagesCompound
300 //purpose  : 
301 //=======================================================================
302 void FillImagesCompound(const TopoDS_Shape& theS,
303                         BRepAlgo_Image& theImages,
304                         TopTools_MapOfShape& theMFP)
305
306   Standard_Boolean bInterferred;
307   TopAbs_ShapeEnum aTypeX;
308   TopAbs_Orientation aOrX;
309   TopoDS_Iterator aIt;
310   BRep_Builder aBB;
311   TopTools_ListIteratorOfListOfShape aItIm; 
312   //
313   if (!theMFP.Add(theS)) {
314     return;
315   }
316   //
317   bInterferred=Standard_False;
318   aIt.Initialize(theS);
319   for (; aIt.More(); aIt.Next()) {
320     const TopoDS_Shape& aSX=aIt.Value();
321     aTypeX=aSX.ShapeType();
322     if (aTypeX==TopAbs_COMPOUND) {
323       FillImagesCompound(aSX, theImages, theMFP);
324     }
325     if (theImages.HasImage(aSX)) {
326       bInterferred=Standard_True;
327     }
328   }
329   if (!bInterferred){
330     return;
331   }
332   //
333   TopoDS_Shape aCIm;
334   GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, aCIm);
335   //
336   aIt.Initialize(theS);
337   for (; aIt.More(); aIt.Next()) {
338     const TopoDS_Shape& aSX=aIt.Value();
339     aOrX=aSX.Orientation();
340     if (theImages.HasImage(aSX)) {
341       const TopTools_ListOfShape& aLFIm=theImages.Image(aSX);
342       aItIm.Initialize(aLFIm);
343       for (; aItIm.More(); aItIm.Next()) {
344         TopoDS_Shape aSXIm=aItIm.Value();
345         aSXIm.Orientation(aOrX);
346         aBB.Add(aCIm, aSXIm);
347       }
348     }
349     else {
350       aBB.Add(aCIm, aSX);
351     }
352   }
353   theImages.Bind(theS, aCIm); 
354 }