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