Salome HOME
b56d73ced68f7755a22963050c4b7d241781017c
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_WESCorrector.cxx
1 //  Copyright (C) 2007-2008  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:        NMTAlgo_WESCorrector.cxx
23 // Created:     
24 // Author:      Peter KURNEV
25 //              <pkv@irinox>
26 //
27 #include <GEOMAlgo_WESCorrector.ixx>
28
29 #include <TopoDS.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Wire.hxx>
32 #include <TopoDS_Face.hxx>
33 #include <TopoDS_Edge.hxx>
34
35 #include <BRep_Builder.hxx>
36 #include <BRep_Tool.hxx>
37
38 #include <TopTools_IndexedMapOfOrientedShape.hxx>
39 #include <TopTools_ListIteratorOfListOfShape.hxx>
40
41 #include <BOP_ConnexityBlock.hxx>
42 #include <BOP_ListIteratorOfListOfConnexityBlock.hxx>
43
44 #include <BOPTColStd_ListOfListOfShape.hxx>
45 #include <BOPTColStd_ListIteratorOfListOfListOfShape.hxx>
46
47 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
48 #include <NMTTools_IndexedDataMapOfShapeIndexedMapOfShape.hxx>
49 #include <TopExp.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TopTools_MapOfShape.hxx>
52 #include <TopTools_MapIteratorOfMapOfShape.hxx>
53 #include <TopoDS_Iterator.hxx>
54 #include <GEOMAlgo_WireSplitter.hxx>
55
56
57 static
58   void MakeWire(const TopTools_ListOfShape& aLE, 
59                 TopoDS_Wire& newWire);
60
61 //=======================================================================
62 // function: 
63 // purpose: 
64 //=======================================================================
65   GEOMAlgo_WESCorrector::GEOMAlgo_WESCorrector()
66 :
67   GEOMAlgo_Algo()
68 {
69 }
70 //=======================================================================
71 // function: ~
72 // purpose: 
73 //=======================================================================
74   GEOMAlgo_WESCorrector::~GEOMAlgo_WESCorrector()
75 {
76 }
77 //=======================================================================
78 // function: SetWES
79 // purpose: 
80 //=======================================================================
81   void GEOMAlgo_WESCorrector::SetWES (const GEOMAlgo_WireEdgeSet& aWES)
82 {
83   GEOMAlgo_WireEdgeSet* pWES=(GEOMAlgo_WireEdgeSet*) &aWES;
84   myWES=pWES;
85 }
86 //=======================================================================
87 // function: WES
88 // purpose: 
89 //=======================================================================
90   GEOMAlgo_WireEdgeSet& GEOMAlgo_WESCorrector::WES () 
91 {
92   return *myWES;
93 }
94 //=======================================================================
95 // function: NewWES
96 // purpose: 
97 //=======================================================================
98   GEOMAlgo_WireEdgeSet& GEOMAlgo_WESCorrector::NewWES () 
99 {
100   return myNewWES;
101 }
102 //=======================================================================
103 // function: Perform
104 // purpose: 
105 //=======================================================================
106   void GEOMAlgo_WESCorrector::Perform()
107 {
108   myErrorStatus=0;
109   //
110   DoConnexityBlocks();
111   DoCorrections();
112 }
113 //=======================================================================
114 // function: DoConnexityBlocks
115 // purpose: 
116 //=======================================================================
117   void GEOMAlgo_WESCorrector::DoConnexityBlocks()
118 {
119   Standard_Boolean bRegular, bClosed;
120   Standard_Integer i, aNbV, j, aNbC, aNbVP, aNbVS;
121   TopTools_ListIteratorOfListOfShape aIt;
122   TopoDS_Iterator aItE;
123   TopoDS_Shape aER;
124   TopTools_IndexedMapOfShape aMER, aMEP, aMEC, aMVP;
125   TopTools_IndexedMapOfShape aMVS, aMVAdd;
126   TopTools_IndexedDataMapOfShapeListOfShape aMVE;
127   //
128   // 1. aMVE;
129   const TopTools_ListOfShape& aLSE=myWES->StartElements();
130   aIt.Initialize(aLSE);
131   for (; aIt.More(); aIt.Next()) {
132     const TopoDS_Shape& aE=aIt.Value();
133     if (!aMEP.Contains(aE)) {
134       aMEP.Add(aE);
135       TopExp::MapShapesAndAncestors(aE, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
136     }
137     else {
138       aMER.Add(aE);
139     }
140   }
141   //
142   // 2. 
143   aNbV=aMVE.Extent();
144   for (i=1; i<=aNbV; ++i) {
145     const TopoDS_Shape& aV=aMVE.FindKey(i);
146     //
147     aNbVS=aMVS.Extent();
148     if (aNbVS==aNbV) {
149       break;
150     }
151     //
152     if (aMVS.Contains(aV)) {
153       continue;
154     }
155     aMVS.Add(aV);    // aMVS - globally processed vertices
156     //
157     //------------------------------------- goal: aMEC
158     aMEC.Clear();    // aMEC - edges of CB
159     aMVP.Clear();    // aMVP - vertices to process right now 
160     aMVAdd.Clear();  // aMVAdd vertices to process on next step of while(1)
161     //
162     aMVP.Add(aV);
163     //
164     while(1) {
165       aNbVP=aMVP.Extent();
166       for (j=1; j<=aNbVP; ++j) {
167         const TopoDS_Shape& aVP=aMVP(j);
168         const TopTools_ListOfShape& aLE=aMVE.FindFromKey(aVP);
169         aIt.Initialize(aLE);
170         for (; aIt.More(); aIt.Next()) {
171           const TopoDS_Shape& aE=aIt.Value();
172           if (aMEC.Contains(aE)) {
173             continue;
174           }
175           aMEC.Add(aE);
176           //
177           aItE.Initialize(aE);
178           for (; aItE.More(); aItE.Next()) {
179             const TopoDS_Shape& aVE=aItE.Value();
180             if (!aMVS.Contains(aVE)) {
181               aMVS.Add(aVE);
182               aMVAdd.Add(aVE);
183             }
184           }
185         }
186       }//for (j=1; j<=aNbVP; ++j) 
187       //
188       aNbVP=aMVAdd.Extent();
189       if (!aNbVP) {
190         break; // from while(1)
191       }
192       //
193       aMVP.Clear();
194       for (j=1; j<=aNbVP; ++j) {
195         const TopoDS_Shape& aVE=aMVAdd(j);
196         aMVP.Add(aVE);
197       }
198       aMVAdd.Clear();
199     }// while(1) {
200     //-------------------------------------
201     BOP_ConnexityBlock aCB;
202     TopTools_ListOfShape aLEC;
203     TopTools_IndexedDataMapOfShapeListOfShape aMVER;
204     //
205     bRegular=Standard_True;
206    
207     aNbC=aMEC.Extent();
208     for (j=1; j<=aNbC; ++j) {
209       aER=aMEC(j);
210       //
211       if (aMER.Contains(aER)) {
212         aER.Orientation(TopAbs_FORWARD);
213         aLEC.Append(aER);
214         aER.Orientation(TopAbs_REVERSED);
215         aLEC.Append(aER);
216         //
217         //modified by NIZNHY-PKV Tue Nov 28 12:02:29 2006f
218         //bClosed=BRep_Tool::IsClosed(TopoDS::Edge(aER), myWES->Face());
219         //if (!bClosed) {
220           bRegular=Standard_False;
221         //}
222         //modified by NIZNHY-PKV Tue Nov 28 12:02:33 2006t
223       }
224       else {
225         aLEC.Append(aER);
226       }
227       //
228       if (bRegular) {
229         TopExp::MapShapesAndAncestors(aER, TopAbs_VERTEX, TopAbs_EDGE, aMVER);
230       }
231     }//for (j=1; j<=aNbC; ++j) {
232     //
233     if (bRegular) {
234       Standard_Integer k, aNbVR, aNbER;
235       //
236       aNbVR=aMVER.Extent();
237       for (k=1; k<=aNbVR; ++k) {
238         const TopTools_ListOfShape& aLER=aMVER(k);
239         aNbER=aLER.Extent();
240         if (aNbER==1) {
241           const TopoDS_Edge& aEx=TopoDS::Edge(aER);
242           bClosed=BRep_Tool::IsClosed(aEx, myWES->Face());
243           if (!bClosed) {
244             bRegular=!bRegular;
245             break;
246           }
247         }
248         if (aNbER>2) {
249           bRegular=!bRegular;
250           break;
251         }
252       }
253     }
254     //
255     aCB.SetShapes(aLEC);
256     aCB.SetRegularity(bRegular);
257     myConnexityBlocks.Append(aCB);
258     aMEC.Clear();
259   }//for (i=1; i<=aNbV; ++i) {
260 }
261
262 //=======================================================================
263 // function: DoCorrections
264 // purpose: 
265 //=======================================================================
266   void GEOMAlgo_WESCorrector::DoCorrections()
267 {
268   Standard_Boolean bIsRegular, bIsNothingToDo;
269   Standard_Integer iErr;
270   TopoDS_Wire aW;
271   BOP_ListIteratorOfListOfConnexityBlock aCBIt;
272   //
273   const TopoDS_Face& aF=myWES->Face();
274   //
275   myNewWES.SetFace(aF);
276   aCBIt.Initialize(myConnexityBlocks);
277   for (; aCBIt.More(); aCBIt.Next()) {
278     const BOP_ConnexityBlock& aCB=aCBIt.Value();
279     const TopTools_ListOfShape& aLE=aCB.Shapes();
280
281     bIsRegular=aCB.IsRegular();
282
283     if (bIsRegular) {
284       MakeWire(aLE, aW);
285       myNewWES.AddShape (aW);
286       continue;
287     }
288     //
289     GEOMAlgo_WireSplitter aWS;
290     //
291     aWS.SetFace(aF);
292     aWS.SetEdges(aLE);
293     //
294     aWS.Perform();
295     iErr=aWS.ErrorStatus();
296     if (iErr) {
297       continue;
298     }
299     bIsNothingToDo=aWS.IsNothingToDo();
300     if (bIsNothingToDo) {
301       MakeWire(aLE, aW);
302       myNewWES.AddShape (aW);
303       continue;
304     }
305     //
306     const BOPTColStd_ListOfListOfShape& aSSS=aWS.Shapes();
307     
308     BOPTColStd_ListIteratorOfListOfListOfShape aWireIt(aSSS);
309     for (; aWireIt.More(); aWireIt.Next()) {
310       const TopTools_ListOfShape& aLEx=aWireIt.Value();
311       //
312       MakeWire(aLEx, aW);
313       myNewWES.AddShape (aW);
314     }
315   }
316 }
317 //=======================================================================
318 // function: MakeWire
319 // purpose: 
320 //=======================================================================
321   void MakeWire(const TopTools_ListOfShape& aLE, 
322                 TopoDS_Wire& newWire)
323 {
324   BRep_Builder aBB;
325   aBB.MakeWire(newWire);
326
327   TopTools_ListIteratorOfListOfShape anIt(aLE);
328   for (; anIt.More(); anIt.Next()){
329     const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
330     aBB.Add(newWire, aE);
331   }
332 }