Salome HOME
Internal issue 0022865: Restructurization of Partition packages.
[modules/geom.git] / src / GEOMAlgo_NEW / GEOMAlgo_Gluer2_3.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 // File:        GEOMAlgo_Gluer2_3.cxx
23 // Created:     
24 // Author:      Peter KURNEV
25
26 #include <GEOMAlgo_Gluer2.hxx>
27
28 #include <TopAbs_ShapeEnum.hxx>
29
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Edge.hxx>
32
33 #include <BRep_Tool.hxx>
34
35 #include <TopExp.hxx>
36 #include <TopExp_Explorer.hxx>
37
38 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
39 #include <TopTools_DataMapOfShapeListOfShape.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_MapOfShape.hxx>
42 #include <TopTools_MapIteratorOfMapOfShape.hxx>
43 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46
47 #include <NMTTools_CoupleOfShape.hxx>
48 #include <NMTTools_ListOfCoupleOfShape.hxx>
49 #include <NMTTools_ListIteratorOfListOfCoupleOfShape.hxx>
50 #include <NMTTools_IndexedDataMapOfShapeIndexedMapOfShape.hxx>
51 #include <NMTTools_Tools.hxx>
52
53 #include <GEOMAlgo_GlueDetector.hxx>
54
55
56 static
57   void MapShapes1(const TopoDS_Shape& aS,
58                   const TopAbs_ShapeEnum aType,
59                   TopTools_IndexedMapOfShape& aM);
60
61 //=======================================================================
62 //function : Detect
63 //purpose  : 
64 //=======================================================================
65 void GEOMAlgo_Gluer2::Detect()
66 {
67   Standard_Boolean bCheckGeometry;
68   Standard_Integer iErr, aNbSD;
69   TopTools_ListIteratorOfListOfShape aItLS;
70   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
71   GEOMAlgo_GlueDetector aDetector;
72   //
73   myErrorStatus=0;
74   myWarningStatus=0;
75   //
76   myImagesDetected.Clear();
77   myOriginsDetected.Clear();
78   //
79   bCheckGeometry=Standard_True;
80   aDetector.SetArgument(myArgument);
81   aDetector.SetTolerance(myTolerance);
82   aDetector.SetCheckGeometry(bCheckGeometry);
83   //
84   aDetector.Perform();
85   iErr=aDetector.ErrorStatus();
86   if (iErr) {
87     myErrorStatus=11;// Detector failed
88     return;
89   }
90   //
91   const TopTools_DataMapOfShapeListOfShape& aImages=aDetector.Images();
92   aItDMSLS.Initialize(aImages);
93   for (; aItDMSLS.More(); aItDMSLS.Next()) {
94     const TopoDS_Shape& aSkey=aItDMSLS.Key();
95     const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
96     aNbSD=aLSD.Extent();
97     myImagesDetected.Bind(aSkey, aLSD);
98   }
99   //
100   aItDMSLS.Initialize(myImagesDetected);
101   for (; aItDMSLS.More(); aItDMSLS.Next()) {
102     const TopoDS_Shape& aSkey=aItDMSLS.Key();
103     const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
104     aItLS.Initialize(aLSD);
105     for (; aItLS.More(); aItLS.Next()) {
106       const TopoDS_Shape& aSx=aItLS.Value();
107       myOriginsDetected.Bind(aSx, aSkey);
108     }
109   }
110 }
111 //=======================================================================
112 //function : PerformShapesToWork
113 //purpose  : 
114 //=======================================================================
115 void GEOMAlgo_Gluer2::PerformShapesToWork()
116
117   Standard_Integer aNbSG, i, j, aNbC, aNb, aNbSD;
118   TopTools_ListIteratorOfListOfShape aItLS1, aItLS2;
119   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
120   NMTTools_CoupleOfShape aCS;
121   NMTTools_ListOfCoupleOfShape aLCS;
122   NMTTools_ListIteratorOfListOfCoupleOfShape aItCS; 
123   //
124   myErrorStatus=0;
125   myWarningStatus=0;
126   //
127   myImagesToWork.Clear();
128   myOriginsToWork.Clear();
129   //
130   aNbSD=myImagesDetected.Extent();
131   if (!aNbSD) {// no shapes to glue detected
132     myWarningStatus=1;
133     return;
134   }
135   //
136   aNbSG=myShapesToGlue.Extent();
137   if (!aNbSG) {
138     // glue all possible
139     myImagesToWork=myImagesDetected;
140     //
141     aItDMSLS.Initialize(myImagesToWork);
142     for (; aItDMSLS.More(); aItDMSLS.Next()) {
143       const TopoDS_Shape& aSkey=aItDMSLS.Key();
144       const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
145       aItLS1.Initialize(aLSD);
146       for (; aItLS1.More(); aItLS1.Next()) {
147         const TopoDS_Shape& aSx=aItLS1.Value();
148         myOriginsToWork.Bind(aSx, aSkey);
149       }
150     }
151     return;
152   }// if (!aNbSG) {
153   //
154   // 1. Make pairs
155   aItDMSLS.Initialize(myShapesToGlue);
156   for (; aItDMSLS.More(); aItDMSLS.Next()) {
157     //const TopoDS_Shape& aSkey=aItDMSLS.Key();
158     const TopTools_ListOfShape& aLSG=aItDMSLS.Value();
159     aItLS1.Initialize(aLSG);
160     for (i=0; aItLS1.More(); aItLS1.Next(), ++i) {
161       aItLS2.Initialize(aLSG);
162       for (j=0; aItLS2.More(); aItLS2.Next(), ++j) {
163         if (j>i) {
164           const TopoDS_Shape& aSG1=aItLS1.Value();
165           const TopoDS_Shape& aSG2=aItLS2.Value();
166           aCS.SetShape1(aSG1);
167           aCS.SetShape2(aSG2);
168           TreatPair(aCS, aLCS);
169         }
170       }
171     }
172   }
173   //
174   // 2. Find Chains
175   TopTools_ListOfShape aLSX;
176   NMTTools_IndexedDataMapOfShapeIndexedMapOfShape aMC;
177   //
178   NMTTools_Tools::FindChains(aLCS, aMC);
179   //
180   // 3. myImagesToWork, myOriginsToWork
181   aNbC=aMC.Extent();
182   for (i=1; i<=aNbC; ++i) {
183     const TopoDS_Shape& aSkey=aMC.FindKey(i);
184     const TopTools_IndexedMapOfShape& aM=aMC(i);
185     aLSX.Clear();
186     aNb=aM.Extent();
187     for (j=1; j<=aNb; ++j) {
188       const TopoDS_Shape& aS=aM(j);
189       aLSX.Append(aS);
190       myOriginsToWork.Bind(aS, aSkey);
191     }
192     myImagesToWork.Bind(aSkey, aLSX);
193   }
194 }
195 //=======================================================================
196 //function : TreatPair
197 //purpose  : 
198 //=======================================================================
199 void GEOMAlgo_Gluer2::TreatPair(const NMTTools_CoupleOfShape& aCS,
200                                 NMTTools_ListOfCoupleOfShape& aLCS)
201 {
202   if (myErrorStatus) {
203     return;
204   }
205   //
206   Standard_Integer i, aNbS1, aNbS2, aNbS;
207   TopAbs_ShapeEnum aType, aTypeS;
208   TopTools_ListIteratorOfListOfShape aItLS;
209   TopTools_IndexedMapOfShape aMS1, aMS2;
210   TopTools_DataMapOfShapeListOfShape aDMSLS;
211   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
212   NMTTools_CoupleOfShape aCSS;
213   //
214   // 1. Checking the pair on whether it can be glued at all
215   // 1.1
216   const TopoDS_Shape& aS1=aCS.Shape1();
217   if (!myOriginsDetected.IsBound(aS1)) {
218     myErrorStatus=30;
219     return;
220   }
221   const TopoDS_Shape& aSkey1=myOriginsDetected.Find(aS1);
222   // 1.2
223   const TopoDS_Shape& aS2=aCS.Shape2();
224   if (!myOriginsDetected.IsBound(aS2)) {
225     myErrorStatus=30;
226     return;
227   }
228   const TopoDS_Shape& aSkey2=myOriginsDetected.Find(aS2);
229   // 1.3
230   if (!aSkey1.IsSame(aSkey2)) {
231     myErrorStatus=33;
232     return;
233   }
234   //
235   // 2. Append the pair to the aLCS
236   aLCS.Append(aCS);
237   //
238   // 3. Treatment the sub-shapes of the pair
239   aType=aS1.ShapeType();
240   if (aType==TopAbs_VERTEX) {
241     return;
242   }
243   aTypeS=TopAbs_EDGE;
244   if (aType==aTypeS) {
245     aTypeS=TopAbs_VERTEX;
246   }
247   //
248   MapShapes1(aS1, aTypeS, aMS1);
249   MapShapes1(aS2, aTypeS, aMS2);
250   //
251   aNbS1=aMS1.Extent();
252   aNbS2=aMS2.Extent();
253   if (aNbS1!=aNbS2) {
254     myErrorStatus=31;
255     return;
256   }
257   //
258   // 1.
259   for (i=1; i<=aNbS1; ++i) {
260     const TopoDS_Shape& aSS1=aMS1(i);
261     if (aMS2.Contains(aSS1)) {
262       continue;
263     }
264     //
265     if (!myOriginsDetected.IsBound(aSS1)) {
266       myErrorStatus=30;
267       return;
268     }
269     //
270     const TopoDS_Shape& aSkey=myOriginsDetected.Find(aSS1);
271     if (aDMSLS.IsBound(aSkey)) {
272       TopTools_ListOfShape& aLS=aDMSLS.ChangeFind(aSkey);
273       aLS.Append(aSS1);
274     }
275     else {
276       TopTools_ListOfShape aLS;
277       //
278       aLS.Append(aSS1);
279       aDMSLS.Bind(aSkey, aLS);
280     }
281   }
282   //
283   // 2.
284   for (i=1; i<=aNbS2; ++i) {
285     const TopoDS_Shape& aSS2=aMS2(i);
286     if (aMS1.Contains(aSS2)) {
287       continue;
288     }
289     //
290     if (!myOriginsDetected.IsBound(aSS2)) {
291       myErrorStatus=30;
292       return;
293     }
294     //
295     const TopoDS_Shape& aSkey=myOriginsDetected.Find(aSS2);
296     if (aDMSLS.IsBound(aSkey)) {
297       TopTools_ListOfShape& aLS=aDMSLS.ChangeFind(aSkey);
298       aLS.Append(aSS2);
299     }
300     else {
301       TopTools_ListOfShape aLS;
302       //
303       aLS.Append(aSS2);
304       aDMSLS.Bind(aSkey, aLS);
305     }
306   }
307   //
308   // 3.
309   aItDMSLS.Initialize(aDMSLS);
310   for (; aItDMSLS.More(); aItDMSLS.Next()) {
311     //const TopoDS_Shape& aSkey=aItDMSLS.Key();
312     const TopTools_ListOfShape& aLS=aItDMSLS.Value();
313     aNbS=aLS.Extent();
314     if (aNbS!=2) {
315       myErrorStatus=32;
316       return;
317     }
318     //
319     const TopoDS_Shape& aSS1=aLS.First();
320     const TopoDS_Shape& aSS2=aLS.Last();
321     aCSS.SetShape1(aSS1);
322     aCSS.SetShape2(aSS2);
323     TreatPair(aCSS, aLCS);
324   }
325 }
326 //=======================================================================
327 //function : MapShapes1
328 //purpose  : 
329 //=======================================================================
330 void MapShapes1(const TopoDS_Shape& aS,
331                const TopAbs_ShapeEnum aType,
332                TopTools_IndexedMapOfShape& aM)
333 {
334   TopExp_Explorer aExp;
335   
336   aExp.Init (aS, aType);
337   for ( ;aExp.More(); aExp.Next()) {
338     const TopoDS_Shape aSx=aExp.Current();
339     if (aType==TopAbs_EDGE) {
340       const TopoDS_Edge& aEx=*((TopoDS_Edge*)&aSx);
341       if (BRep_Tool::Degenerated(aEx)) {
342         continue;
343       }
344     }
345     aM.Add(aSx);
346   }
347 }
348 /*
349 //=======================================================================
350 //function : MapShapes1
351 //purpose  : 
352 //=======================================================================
353 void MapShapes1(const TopoDS_Shape& aS,
354                const TopAbs_ShapeEnum aType,
355                TopTools_IndexedMapOfShape& aM)
356 {
357   TopExp_Explorer aExp (aS, aType);
358   while (aExp.More()) {
359     const TopoDS_Shape aSx=aExp.Current();
360     if (aType==TopAbs_EDGE) {
361       const TopoDS_Edge& aEx=*((TopoDS_Edge*)&aSx);
362       if (BRep_Tool::Degenerated(aEx)) {
363         aExp.Next();
364         continue;
365       }
366     }
367     aM.Add(aSx);
368     aExp.Next();
369   }
370 }
371 */