Salome HOME
added new GlueFaces feature
[modules/shaper.git] / src / GeomAlgoImpl / GEOMAlgo_Gluer2_3.cxx
1 // Copyright (C) 2007-2022  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 void MapShapes1(const TopoDS_Shape& aS,
57                        const TopAbs_ShapeEnum aType,
58                        TopTools_IndexedMapOfShape& aM);
59
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   //
72   myErrorStatus=0;
73   myWarningStatus=0;
74   //
75   myImagesDetected.Clear();
76   myOriginsDetected.Clear();
77   //
78   bCheckGeometry=Standard_True;
79   //
80   //modified by NIZNHY-PKV Tue Mar 13 13:33:35 2012f
81   myDetector.Clear();
82   myDetector.SetContext(myContext);
83   //modified by NIZNHY-PKV Tue Mar 13 13:33:38 2012t
84   myDetector.SetArgument(myArgument);
85   myDetector.SetTolerance(myTolerance);
86   myDetector.SetCheckGeometry(bCheckGeometry);
87   //
88   myDetector.Perform();
89   iErr=myDetector.ErrorStatus();
90   if (iErr) {
91     // Detector is failed
92     myErrorStatus=11;
93     return;
94   }
95   //modified by NIZNHY-PKV Tue Mar 13 13:40:36 2012f
96   iErr=myDetector.WarningStatus();
97   if (iErr) {
98     // Sticked shapes are detected
99     myWarningStatus=2;
100   }
101   //modified by NIZNHY-PKV Tue Mar 13 13:40:39 2012t
102   //
103   const TopTools_DataMapOfShapeListOfShape& aImages=myDetector.Images();
104   aItDMSLS.Initialize(aImages);
105   for (; aItDMSLS.More(); aItDMSLS.Next()) {
106     const TopoDS_Shape& aSkey=aItDMSLS.Key();
107     const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
108     myImagesDetected.Bind(aSkey, aLSD);
109   }
110   //
111   aItDMSLS.Initialize(myImagesDetected);
112   for (; aItDMSLS.More(); aItDMSLS.Next()) {
113     const TopoDS_Shape& aSkey=aItDMSLS.Key();
114     const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
115     aItLS.Initialize(aLSD);
116     for (; aItLS.More(); aItLS.Next()) {
117       const TopoDS_Shape& aSx=aItLS.Value();
118       myOriginsDetected.Bind(aSx, aSkey);
119     }
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 //=======================================================================
209 //function : TreatPair
210 //purpose  :
211 //=======================================================================
212 void GEOMAlgo_Gluer2::TreatPair(const GEOMAlgo_CoupleOfShapes& aCS,
213                                 GEOMAlgo_ListOfCoupleOfShapes& aLCS)
214 {
215   if (myErrorStatus) {
216     return;
217   }
218   //
219   Standard_Integer i, aNbS1, aNbS2, aNbS;
220   TopAbs_ShapeEnum aType, aTypeS;
221   TopTools_ListIteratorOfListOfShape aItLS;
222   TopTools_IndexedMapOfShape aMS1, aMS2;
223   TopTools_DataMapOfShapeListOfShape aDMSLS;
224   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
225   GEOMAlgo_CoupleOfShapes aCSS;
226   //
227   // 1. Checking the pair on whether it can be glued at all
228   // 1.1
229   const TopoDS_Shape& aS1=aCS.Shape1();
230   if (!myOriginsDetected.IsBound(aS1)) {
231     myErrorStatus=30;
232     return;
233   }
234   const TopoDS_Shape& aSkey1=myOriginsDetected.Find(aS1);
235   // 1.2
236   const TopoDS_Shape& aS2=aCS.Shape2();
237   if (!myOriginsDetected.IsBound(aS2)) {
238     myErrorStatus=30;
239     return;
240   }
241   const TopoDS_Shape& aSkey2=myOriginsDetected.Find(aS2);
242   // 1.3
243   if (!aSkey1.IsSame(aSkey2)) {
244     myErrorStatus=33;
245     return;
246   }
247   //
248   // 2. Append the pair to the aLCS
249   aLCS.Append(aCS);
250   //
251   // 3. Treatment the sub-shapes of the pair
252   aType=aS1.ShapeType();
253   if (aType==TopAbs_VERTEX) {
254     return;
255   }
256   aTypeS=TopAbs_EDGE;
257   if (aType==aTypeS) {
258     aTypeS=TopAbs_VERTEX;
259   }
260   //
261   MapShapes1(aS1, aTypeS, aMS1);
262   MapShapes1(aS2, aTypeS, aMS2);
263   //
264   aNbS1=aMS1.Extent();
265   aNbS2=aMS2.Extent();
266   if (aNbS1!=aNbS2) {
267     myErrorStatus=31;
268     return;
269   }
270   //
271   // 1.
272   for (i=1; i<=aNbS1; ++i) {
273     const TopoDS_Shape& aSS1=aMS1(i);
274     if (aMS2.Contains(aSS1)) {
275       continue;
276     }
277     //
278     if (!myOriginsDetected.IsBound(aSS1)) {
279       myErrorStatus=30;
280       return;
281     }
282     //
283     const TopoDS_Shape& aSkey=myOriginsDetected.Find(aSS1);
284     if (aDMSLS.IsBound(aSkey)) {
285       TopTools_ListOfShape& aLS=aDMSLS.ChangeFind(aSkey);
286       aLS.Append(aSS1);
287     }
288     else {
289       TopTools_ListOfShape aLS;
290       //
291       aLS.Append(aSS1);
292       aDMSLS.Bind(aSkey, aLS);
293     }
294   }
295   //
296   // 2.
297   for (i=1; i<=aNbS2; ++i) {
298     const TopoDS_Shape& aSS2=aMS2(i);
299     if (aMS1.Contains(aSS2)) {
300       continue;
301     }
302     //
303     if (!myOriginsDetected.IsBound(aSS2)) {
304       myErrorStatus=30;
305       return;
306     }
307     //
308     const TopoDS_Shape& aSkey=myOriginsDetected.Find(aSS2);
309     if (aDMSLS.IsBound(aSkey)) {
310       TopTools_ListOfShape& aLS=aDMSLS.ChangeFind(aSkey);
311       aLS.Append(aSS2);
312     }
313     else {
314       TopTools_ListOfShape aLS;
315       //
316       aLS.Append(aSS2);
317       aDMSLS.Bind(aSkey, aLS);
318     }
319   }
320   //
321   // 3.
322   aItDMSLS.Initialize(aDMSLS);
323   for (; aItDMSLS.More(); aItDMSLS.Next()) {
324     //const TopoDS_Shape& aSkey=aItDMSLS.Key();
325     const TopTools_ListOfShape& aLS=aItDMSLS.Value();
326     aNbS=aLS.Extent();
327     if (aNbS!=2) {
328       myErrorStatus=32;
329       return;
330     }
331     //
332     const TopoDS_Shape& aSS1=aLS.First();
333     const TopoDS_Shape& aSS2=aLS.Last();
334     aCSS.SetShape1(aSS1);
335     aCSS.SetShape2(aSS2);
336     TreatPair(aCSS, aLCS);
337   }
338 }
339
340 //=======================================================================
341 //function : MapShapes1
342 //purpose  :
343 //=======================================================================
344 void MapShapes1(const TopoDS_Shape& aS,
345                 const TopAbs_ShapeEnum aType,
346                 TopTools_IndexedMapOfShape& aM)
347 {
348   TopExp_Explorer aExp;
349
350   aExp.Init (aS, aType);
351   for ( ;aExp.More(); aExp.Next()) {
352     const TopoDS_Shape aSx=aExp.Current();
353     if (aType==TopAbs_EDGE) {
354       const TopoDS_Edge& aEx=*((TopoDS_Edge*)&aSx);
355       if (BRep_Tool::Degenerated(aEx)) {
356         continue;
357       }
358     }
359     aM.Add(aSx);
360   }
361 }