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