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