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