Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_WESCorrector.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:        NMTAlgo_WESCorrector.cxx
21 // Created:     
22 // Author:      Peter KURNEV
23 //              <pkv@irinox>
24
25
26 #include <GEOMAlgo_WESCorrector.ixx>
27
28 #include <TopoDS.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Wire.hxx>
31 #include <TopoDS_Face.hxx>
32 #include <TopoDS_Edge.hxx>
33
34 #include <BRep_Builder.hxx>
35 #include <BRep_Tool.hxx>
36
37 #include <TopTools_IndexedMapOfOrientedShape.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39
40 #include <BOP_ConnexityBlock.hxx>
41 #include <BOP_ListIteratorOfListOfConnexityBlock.hxx>
42
43 #include <BOPTColStd_ListOfListOfShape.hxx>
44 #include <BOPTColStd_ListIteratorOfListOfListOfShape.hxx>
45
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
47 #include <NMTTools_IndexedDataMapOfShapeIndexedMapOfShape.hxx>
48 #include <TopExp.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <TopTools_MapOfShape.hxx>
51 #include <TopTools_MapIteratorOfMapOfShape.hxx>
52 #include <TopoDS_Iterator.hxx>
53 #include <GEOMAlgo_WireSplitter.hxx>
54
55
56 static
57   void MakeWire(const TopTools_ListOfShape& aLE, 
58                 TopoDS_Wire& newWire);
59
60 //=======================================================================
61 // function: 
62 // purpose: 
63 //=======================================================================
64   GEOMAlgo_WESCorrector::GEOMAlgo_WESCorrector()
65 :
66   GEOMAlgo_Algo()
67 {
68 }
69 //=======================================================================
70 // function: ~
71 // purpose: 
72 //=======================================================================
73   GEOMAlgo_WESCorrector::~GEOMAlgo_WESCorrector()
74 {
75 }
76 //=======================================================================
77 // function: SetWES
78 // purpose: 
79 //=======================================================================
80   void GEOMAlgo_WESCorrector::SetWES (const GEOMAlgo_WireEdgeSet& aWES)
81 {
82   GEOMAlgo_WireEdgeSet* pWES=(GEOMAlgo_WireEdgeSet*) &aWES;
83   myWES=pWES;
84 }
85 //=======================================================================
86 // function: WES
87 // purpose: 
88 //=======================================================================
89   GEOMAlgo_WireEdgeSet& GEOMAlgo_WESCorrector::WES () 
90 {
91   return *myWES;
92 }
93 //=======================================================================
94 // function: NewWES
95 // purpose: 
96 //=======================================================================
97   GEOMAlgo_WireEdgeSet& GEOMAlgo_WESCorrector::NewWES () 
98 {
99   return myNewWES;
100 }
101 //=======================================================================
102 // function: Perform
103 // purpose: 
104 //=======================================================================
105   void GEOMAlgo_WESCorrector::Perform()
106 {
107   myErrorStatus=0;
108   //
109   DoConnexityBlocks();
110   DoCorrections();
111 }
112 //=======================================================================
113 // function: DoConnexityBlocks
114 // purpose: 
115 //=======================================================================
116   void GEOMAlgo_WESCorrector::DoConnexityBlocks()
117 {
118   Standard_Boolean bRegular, bClosed;
119   Standard_Integer i, aNbV, j, aNbC, aNbVP, aNbVS;
120   TopTools_ListIteratorOfListOfShape aIt;
121   TopoDS_Iterator aItE;
122   TopoDS_Shape aER;
123   TopTools_IndexedMapOfShape aMER, aMEP, aMEC, aMVP;
124   TopTools_IndexedMapOfShape aMVS, aMVAdd;
125   TopTools_IndexedDataMapOfShapeListOfShape aMVE;
126   //
127   // 1. aMVE;
128   const TopTools_ListOfShape& aLSE=myWES->StartElements();
129   aIt.Initialize(aLSE);
130   for (; aIt.More(); aIt.Next()) {
131     const TopoDS_Shape& aE=aIt.Value();
132     if (!aMEP.Contains(aE)) {
133       aMEP.Add(aE);
134       TopExp::MapShapesAndAncestors(aE, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
135     }
136     else {
137       aMER.Add(aE);
138     }
139   }
140   //
141   // 2. 
142   aNbV=aMVE.Extent();
143   for (i=1; i<=aNbV; ++i) {
144     const TopoDS_Shape& aV=aMVE.FindKey(i);
145     //
146     aNbVS=aMVS.Extent();
147     if (aNbVS==aNbV) {
148       break;
149     }
150     //
151     if (aMVS.Contains(aV)) {
152       continue;
153     }
154     aMVS.Add(aV);    // aMVS - globally processed vertices
155     //
156     //------------------------------------- goal: aMEC
157     aMEC.Clear();    // aMEC - edges of CB
158     aMVP.Clear();    // aMVP - vertices to process right now 
159     aMVAdd.Clear();  // aMVAdd vertices to process on next step of while(1)
160     //
161     aMVP.Add(aV);
162     //
163     while(1) {
164       aNbVP=aMVP.Extent();
165       for (j=1; j<=aNbVP; ++j) {
166         const TopoDS_Shape& aVP=aMVP(j);
167         const TopTools_ListOfShape& aLE=aMVE.FindFromKey(aVP);
168         aIt.Initialize(aLE);
169         for (; aIt.More(); aIt.Next()) {
170           const TopoDS_Shape& aE=aIt.Value();
171           if (aMEC.Contains(aE)) {
172             continue;
173           }
174           aMEC.Add(aE);
175           //
176           aItE.Initialize(aE);
177           for (; aItE.More(); aItE.Next()) {
178             const TopoDS_Shape& aVE=aItE.Value();
179             if (!aMVS.Contains(aVE)) {
180               aMVS.Add(aVE);
181               aMVAdd.Add(aVE);
182             }
183           }
184         }
185       }//for (j=1; j<=aNbVP; ++j) 
186       //
187       aNbVP=aMVAdd.Extent();
188       if (!aNbVP) {
189         break; // from while(1)
190       }
191       //
192       aMVP.Clear();
193       for (j=1; j<=aNbVP; ++j) {
194         const TopoDS_Shape& aVE=aMVAdd(j);
195         aMVP.Add(aVE);
196       }
197       aMVAdd.Clear();
198     }// while(1) {
199     //-------------------------------------
200     BOP_ConnexityBlock aCB;
201     TopTools_ListOfShape aLEC;
202     TopTools_IndexedDataMapOfShapeListOfShape aMVER;
203     //
204     bRegular=Standard_True;
205    
206     aNbC=aMEC.Extent();
207     for (j=1; j<=aNbC; ++j) {
208       aER=aMEC(j);
209       //
210       if (aMER.Contains(aER)) {
211         aER.Orientation(TopAbs_FORWARD);
212         aLEC.Append(aER);
213         aER.Orientation(TopAbs_REVERSED);
214         aLEC.Append(aER);
215         //
216         //modified by NIZNHY-PKV Tue Nov 28 12:02:29 2006f
217         //bClosed=BRep_Tool::IsClosed(TopoDS::Edge(aER), myWES->Face());
218         //if (!bClosed) {
219           bRegular=Standard_False;
220         //}
221         //modified by NIZNHY-PKV Tue Nov 28 12:02:33 2006t
222       }
223       else {
224         aLEC.Append(aER);
225       }
226       //
227       if (bRegular) {
228         TopExp::MapShapesAndAncestors(aER, TopAbs_VERTEX, TopAbs_EDGE, aMVER);
229       }
230     }//for (j=1; j<=aNbC; ++j) {
231     //
232     if (bRegular) {
233       Standard_Integer k, aNbVR, aNbER;
234       //
235       aNbVR=aMVER.Extent();
236       for (k=1; k<=aNbVR; ++k) {
237         const TopTools_ListOfShape& aLER=aMVER(k);
238         aNbER=aLER.Extent();
239         if (aNbER==1) {
240           const TopoDS_Edge& aEx=TopoDS::Edge(aER);
241           bClosed=BRep_Tool::IsClosed(aEx, myWES->Face());
242           if (!bClosed) {
243             bRegular=!bRegular;
244             break;
245           }
246         }
247         if (aNbER>2) {
248           bRegular=!bRegular;
249           break;
250         }
251       }
252     }
253     //
254     aCB.SetShapes(aLEC);
255     aCB.SetRegularity(bRegular);
256     myConnexityBlocks.Append(aCB);
257     aMEC.Clear();
258   }//for (i=1; i<=aNbV; ++i) {
259 }
260
261 //=======================================================================
262 // function: DoCorrections
263 // purpose: 
264 //=======================================================================
265   void GEOMAlgo_WESCorrector::DoCorrections()
266 {
267   Standard_Boolean bIsRegular, bIsNothingToDo;
268   Standard_Integer iErr;
269   TopoDS_Wire aW;
270   BOP_ListIteratorOfListOfConnexityBlock aCBIt;
271   //
272   const TopoDS_Face& aF=myWES->Face();
273   //
274   myNewWES.SetFace(aF);
275   aCBIt.Initialize(myConnexityBlocks);
276   for (; aCBIt.More(); aCBIt.Next()) {
277     const BOP_ConnexityBlock& aCB=aCBIt.Value();
278     const TopTools_ListOfShape& aLE=aCB.Shapes();
279
280     bIsRegular=aCB.IsRegular();
281
282     if (bIsRegular) {
283       MakeWire(aLE, aW);
284       myNewWES.AddShape (aW);
285       continue;
286     }
287     //
288     GEOMAlgo_WireSplitter aWS;
289     //
290     aWS.SetFace(aF);
291     aWS.SetEdges(aLE);
292     //
293     aWS.Perform();
294     iErr=aWS.ErrorStatus();
295     if (iErr) {
296       continue;
297     }
298     bIsNothingToDo=aWS.IsNothingToDo();
299     if (bIsNothingToDo) {
300       MakeWire(aLE, aW);
301       myNewWES.AddShape (aW);
302       continue;
303     }
304     //
305     const BOPTColStd_ListOfListOfShape& aSSS=aWS.Shapes();
306     
307     BOPTColStd_ListIteratorOfListOfListOfShape aWireIt(aSSS);
308     for (; aWireIt.More(); aWireIt.Next()) {
309       const TopTools_ListOfShape& aLEx=aWireIt.Value();
310       //
311       MakeWire(aLEx, aW);
312       myNewWES.AddShape (aW);
313     }
314   }
315 }
316 //=======================================================================
317 // function: MakeWire
318 // purpose: 
319 //=======================================================================
320   void MakeWire(const TopTools_ListOfShape& aLE, 
321                 TopoDS_Wire& newWire)
322 {
323   BRep_Builder aBB;
324   aBB.MakeWire(newWire);
325
326   TopTools_ListIteratorOfListOfShape anIt(aLE);
327   for (; anIt.More(); anIt.Next()){
328     const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
329     aBB.Add(newWire, aE);
330   }
331 }