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