Salome HOME
Bug 0020052: EDF 867 GEOM: Non removable extra edges are created with fuse operation...
[modules/geom.git] / src / GEOMAlgo / BlockFix_CheckTool.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:      BlockFix_CheckTool.cxx
23 // Created:   17.12.04 11:15:25
24 // Author:    Sergey KUUL
25 //
26 #include <BlockFix_CheckTool.ixx>
27
28 #include <BRep_Tool.hxx>
29 #include <BlockFix_UnionEdges.hxx>
30 #include <BlockFix_UnionFaces.hxx>
31 #include <TopExp.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Face.hxx>
36 #include <TopoDS_Solid.hxx>
37 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
38 #include <TopTools_MapOfShape.hxx>
39 #include <TopTools_ListOfShape.hxx>
40 #include <TopTools_ListIteratorOfListOfShape.hxx>
41
42
43 //=======================================================================
44 //function : BlockFix_CheckTool()
45 //purpose  : Constructor
46 //=======================================================================
47
48 BlockFix_CheckTool::BlockFix_CheckTool( )
49 {
50   myHasCheck = Standard_False;
51   myPossibleBlocks.Clear();
52 }
53
54
55 //=======================================================================
56 //function : SetShape
57 //purpose  : 
58 //=======================================================================
59
60 void BlockFix_CheckTool::SetShape(const TopoDS_Shape& aShape)
61 {
62   myHasCheck = Standard_False;
63   myShape = aShape;
64   myPossibleBlocks.Clear();
65 }
66
67
68 //=======================================================================
69 //function : Perform
70 //purpose  : 
71 //=======================================================================
72
73 void BlockFix_CheckTool::Perform() 
74 {
75   myNbSolids=0;
76   myNbBlocks=0;
77   myNbDegen=0;
78   myNbUF=0;
79   myNbUE=0;
80   myNbUFUE=0;
81   TopExp_Explorer exps;
82   for(exps.Init(myShape, TopAbs_SOLID); exps.More(); exps.Next()) {
83     TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
84     myNbSolids++;
85     Standard_Boolean IsBlock=Standard_True;
86     Standard_Boolean MayBeUF=Standard_False;
87     Standard_Boolean MayBeUE=Standard_False;
88     Standard_Integer nf=0;
89     TopExp_Explorer expf;
90     for(expf.Init(aSolid, TopAbs_FACE); expf.More(); expf.Next()) nf++;
91
92     if(nf<6) {
93       IsBlock=Standard_False;
94     }
95     else if(nf>6) {
96       IsBlock=Standard_False;
97       // check faces unification
98       TopTools_SequenceOfShape faces;
99       for( expf.Init(aSolid, TopAbs_FACE); expf.More(); expf.Next()) {
100         TopoDS_Face aFace = TopoDS::Face(expf.Current());
101         faces.Append(aFace);
102       }
103       Standard_Boolean HasFacesForUnification = Standard_False;
104       for(Standard_Integer i=1; i<faces.Length() && !HasFacesForUnification; i++) {
105         TopoDS_Face F1 = TopoDS::Face(faces.Value(i));
106         TopTools_MapOfShape Edges;
107         for(TopExp_Explorer expe(F1,TopAbs_EDGE); expe.More(); expe.Next())
108           Edges.Add(expe.Current().Oriented(TopAbs_FORWARD));
109         TopLoc_Location L1;
110         Handle(Geom_Surface) S1 = BRep_Tool::Surface(F1,L1);
111         for(Standard_Integer j=i+1; j<=faces.Length() && !HasFacesForUnification; j++) {
112           TopoDS_Face F2 = TopoDS::Face(faces.Value(j));
113           TopLoc_Location L2;
114           Handle(Geom_Surface) S2 = BRep_Tool::Surface(F2,L2);
115           if( S1==S2 && L1==L2 ) {
116             // faces have equal based surface
117             // now check common edge
118             for(TopExp_Explorer expe2(F2,TopAbs_EDGE); expe2.More(); expe2.Next()) {
119               if(Edges.Contains(expe2.Current().Oriented(TopAbs_FORWARD))) {
120                 HasFacesForUnification = Standard_True;
121                 break;
122               }
123             }
124           }
125         }
126       }
127       if(HasFacesForUnification) {
128         MayBeUF=Standard_True;
129       }
130     }
131
132     Standard_Integer nbe=0;
133     TopTools_MapOfShape DegenEdges;
134     TopExp_Explorer expe;
135     for(expe.Init(aSolid, TopAbs_EDGE); expe.More(); expe.Next()) {
136       TopoDS_Edge E = TopoDS::Edge(expe.Current());
137       if(BRep_Tool::Degenerated(E)) {
138         if(!DegenEdges.Contains(E)) {
139           DegenEdges.Add(E);
140         }
141       }
142       else {
143         nbe++;
144       }
145     }
146     if( nbe==24 && DegenEdges.Extent()>0 ) {
147       IsBlock=Standard_False;
148       myNbDegen++;
149       myPossibleBlocks.Append(aSolid);
150       continue;
151     }
152     if(nbe<24)
153       IsBlock=Standard_False;
154     if(nbe>24) {
155       IsBlock=Standard_False;
156       // check edges unification
157       // creating map of edge faces
158       TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
159       TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
160       for(expf.Init(aSolid, TopAbs_FACE); expf.More(); expf.Next()) {
161         TopoDS_Face aFace = TopoDS::Face(expf.Current());
162         TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
163         for(expe.Init(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
164           TopoDS_Edge edge = TopoDS::Edge(expe.Current());
165           if(!aMapEdgeFaces.Contains(edge)) continue;
166           const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
167           TopTools_ListIteratorOfListOfShape anIter(aList);
168           for( ; anIter.More(); anIter.Next()) {
169             TopoDS_Face face = TopoDS::Face(anIter.Value());
170             if(face.IsSame(aFace)) continue;
171             if(aMapFacesEdges.Contains(face)) {
172               aMapFacesEdges.ChangeFromKey(face).Append(edge);
173             }
174             else {
175               TopTools_ListOfShape ListEdges;
176               ListEdges.Append(edge);
177               aMapFacesEdges.Add(face,ListEdges);
178             }
179           }
180         }
181         Standard_Integer i=1;
182         for(; i<=aMapFacesEdges.Extent(); i++) {
183           const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
184           if(ListEdges.Extent()>1) break;
185         }
186         if(i<=aMapFacesEdges.Extent()) {
187           MayBeUE=Standard_True;
188           break;
189         }
190       }
191     }
192
193     if(IsBlock) 
194       myNbBlocks++;
195     else {
196       if(MayBeUF) {
197         myPossibleBlocks.Append(aSolid);
198         if(MayBeUE)
199           myNbUFUE++;
200         else
201           myNbUF++;
202       }
203       else if(MayBeUE) {
204         myNbUE++;
205         myPossibleBlocks.Append(aSolid);
206       }
207     }
208
209   }
210
211   myHasCheck = Standard_True;
212 }
213
214
215 //=======================================================================
216 //function : NbPossibleBlocks
217 //purpose  : 
218 //=======================================================================
219
220 Standard_Integer BlockFix_CheckTool::NbPossibleBlocks() const
221 {
222   return myPossibleBlocks.Length();
223 }
224
225
226 //=======================================================================
227 //function : PossibleBlock
228 //purpose  : 
229 //=======================================================================
230
231 TopoDS_Shape BlockFix_CheckTool::PossibleBlock(const Standard_Integer num) const
232 {
233   TopoDS_Shape res;
234   if( num>0 && num<=myPossibleBlocks.Length() ) 
235     res = myPossibleBlocks.Value(num);
236   return res;
237 }
238
239
240 //=======================================================================
241 //function : DumpCheckResult
242 //purpose  : 
243 //=======================================================================
244
245 void BlockFix_CheckTool::DumpCheckResult(Standard_OStream& S) const
246 {
247   if(!myHasCheck)
248     S<<"Check not performed!"<<endl;
249   else {
250     S<<"dump results of check:"<<endl;
251     S<<"  total number of solids = "<<myNbSolids<<endl;
252     S<<"  including: number of good blocks = "<<myNbBlocks<<endl;
253     S<<"             number of possible blocks = "<<NbPossibleBlocks()<<endl;
254     S<<"             including: need remove degenerative = "<<myNbDegen<<endl;
255     S<<"                        need unionfaces = "<<myNbUF<<endl;
256     S<<"                        need unionedges = "<<myNbUE<<endl;
257     S<<"                        need both unionfaces and unionedges = "<<myNbUFUE<<endl;
258     Standard_Integer nbtmp = myNbSolids - myNbBlocks - NbPossibleBlocks();
259     S<<"             number of impossible blocks = "<<nbtmp<<endl;
260   }
261 }