Salome HOME
91ccb24e383a16461a689f3f026882f1c0b325b9
[modules/geom.git] / src / BlockFix / BlockFix_CheckTool.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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 <Geom_Curve.hxx>
32
33 #include <gp_Pnt.hxx>
34 #include <gp_Vec.hxx>
35
36 #include <TopExp.hxx>
37 #include <TopExp_Explorer.hxx>
38
39 #include <TopoDS.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Solid.hxx>
43 #include <TopoDS_Vertex.hxx>
44
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TopTools_MapOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49
50 //=======================================================================
51 //function : BlockFix_CheckTool()
52 //purpose  : Constructor
53 //=======================================================================
54 BlockFix_CheckTool::BlockFix_CheckTool( )
55 {
56   myHasCheck = Standard_False;
57   myAngTolerance = -1.;
58   myPossibleBlocks.Clear();
59 }
60
61 //=======================================================================
62 //function : SetShape
63 //purpose  :
64 //=======================================================================
65 void BlockFix_CheckTool::SetShape(const TopoDS_Shape& aShape)
66 {
67   myHasCheck = Standard_False;
68   myShape = aShape;
69   myPossibleBlocks.Clear();
70 }
71
72 //=======================================================================
73 //function : SetAngTolerance
74 //purpose  :
75 //=======================================================================
76 void BlockFix_CheckTool::SetAngTolerance(const Standard_Real theTolerance)
77 {
78   myHasCheck     = Standard_False;
79   myAngTolerance = theTolerance;
80   myPossibleBlocks.Clear();
81 }
82
83 //=======================================================================
84 //function : Perform
85 //purpose  :
86 //=======================================================================
87 void BlockFix_CheckTool::Perform()
88 {
89   myNbSolids=0;
90   myNbBlocks=0;
91   myNbDegen=0;
92   myNbUF=0;
93   myNbUE=0;
94   myNbUFUE=0;
95
96   TopExp_Explorer exps (myShape, TopAbs_SOLID);
97   TopTools_MapOfShape mapS;
98   for (; exps.More(); exps.Next()) {
99     TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
100     if (!mapS.Add(aSolid)) continue;
101     myNbSolids++;
102     Standard_Boolean IsBlock=Standard_True;
103     Standard_Boolean MayBeUF=Standard_False;
104     Standard_Boolean MayBeUE=Standard_False;
105     Standard_Integer nf=0;
106     TopExp_Explorer expf (aSolid, TopAbs_FACE);
107     TopTools_MapOfShape mapF;
108     for (; expf.More(); expf.Next()) {
109       if (mapF.Add(expf.Current()))
110         nf++;
111     }
112
113     if (nf < 6) {
114       IsBlock = Standard_False;
115     }
116     else if (nf > 6) {
117       IsBlock = Standard_False;
118       // check faces unification
119       TopTools_SequenceOfShape faces;
120       mapF.Clear();
121       for (expf.Init(aSolid, TopAbs_FACE); expf.More(); expf.Next()) {
122         if (mapF.Add(expf.Current())) {
123           TopoDS_Face aFace = TopoDS::Face(expf.Current());
124           faces.Append(aFace);
125         }
126       }
127       Standard_Boolean HasFacesForUnification = Standard_False;
128       for (Standard_Integer i=1; i<faces.Length() && !HasFacesForUnification; i++) {
129         TopoDS_Face F1 = TopoDS::Face(faces.Value(i));
130         TopTools_MapOfShape Edges;
131         for (TopExp_Explorer expe(F1,TopAbs_EDGE); expe.More(); expe.Next())
132           Edges.Add(expe.Current().Oriented(TopAbs_FORWARD));
133         TopLoc_Location L1;
134         Handle(Geom_Surface) S1 = BRep_Tool::Surface(F1,L1);
135         for(Standard_Integer j=i+1; j<=faces.Length() && !HasFacesForUnification; j++) {
136           TopoDS_Face F2 = TopoDS::Face(faces.Value(j));
137           TopLoc_Location L2;
138           Handle(Geom_Surface) S2 = BRep_Tool::Surface(F2,L2);
139           if( S1==S2 && L1==L2 ) {
140             // faces have equal based surface
141             // now check common edge
142             for(TopExp_Explorer expe2(F2,TopAbs_EDGE); expe2.More(); expe2.Next()) {
143               if(Edges.Contains(expe2.Current().Oriented(TopAbs_FORWARD))) {
144                 HasFacesForUnification = Standard_True;
145                 break;
146               }
147             }
148           }
149         }
150       }
151       if (HasFacesForUnification) {
152         MayBeUF=Standard_True;
153       }
154     }
155
156     Standard_Integer nbe=0;
157     TopTools_MapOfShape DegenEdges;
158     TopExp_Explorer expe (aSolid, TopAbs_EDGE);
159     TopTools_MapOfShape mapE;
160     for (; expe.More(); expe.Next()) {
161       TopoDS_Edge E = TopoDS::Edge(expe.Current());
162       if (!mapE.Add(E)) continue;
163       if (BRep_Tool::Degenerated(E)) {
164         DegenEdges.Add(E);
165       }
166       else {
167         nbe++;
168       }
169     }
170     if (nbe == 12 && DegenEdges.Extent() > 0) {
171       IsBlock = Standard_False;
172       myNbDegen++;
173       myPossibleBlocks.Append(aSolid);
174       continue;
175     }
176     if (nbe < 12)
177       IsBlock = Standard_False;
178     if (nbe > 12) {
179       // check edges unification
180       // creating map of edge faces
181       TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
182       TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
183
184       mapF.Clear();
185       for (expf.Init(aSolid, TopAbs_FACE); expf.More(); expf.Next()) {
186         TopoDS_Face aFace = TopoDS::Face(expf.Current());
187         if (!mapF.Add(aFace)) continue;
188         TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
189
190         TopTools_MapOfShape mapEe;
191         for (expe.Init(aFace, TopAbs_EDGE); expe.More(); expe.Next()) {
192           TopoDS_Edge edge = TopoDS::Edge(expe.Current());
193           if (!mapEe.Add(edge)) continue;
194           if (!aMapEdgeFaces.Contains(edge)) continue;
195           const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
196           TopTools_ListIteratorOfListOfShape anIter (aList);
197           for (; anIter.More(); anIter.Next()) {
198             TopoDS_Face face = TopoDS::Face(anIter.Value());
199             if (face.IsSame(aFace)) continue;
200             if (aMapFacesEdges.Contains(face)) {
201               aMapFacesEdges.ChangeFromKey(face).Append(edge);
202             }
203             else {
204               TopTools_ListOfShape ListEdges;
205               ListEdges.Append(edge);
206               aMapFacesEdges.Add(face,ListEdges);
207             }
208           }
209         }
210         Standard_Integer i = 1;
211         for (; i <= aMapFacesEdges.Extent(); i++) {
212           const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
213           if (ListEdges.Extent() > 1) {
214             if (myAngTolerance < 0.) {
215               break;
216             }
217
218             // Check if edges have C1 continuity.
219             if (!isC1(ListEdges)) {
220               break;
221             }
222           }
223         }
224         if (i <= aMapFacesEdges.Extent()) {
225           IsBlock = Standard_False;
226           MayBeUE = Standard_True;
227           break;
228         }
229       }
230     }
231
232     if (IsBlock)
233       myNbBlocks++;
234     else {
235       if (MayBeUF) {
236         myPossibleBlocks.Append(aSolid);
237         if (MayBeUE)
238           myNbUFUE++;
239         else
240           myNbUF++;
241       }
242       else if (MayBeUE) {
243         myNbUE++;
244         myPossibleBlocks.Append(aSolid);
245       }
246     }
247   }
248
249   myHasCheck = Standard_True;
250 }
251
252 //=======================================================================
253 //function : NbPossibleBlocks
254 //purpose  :
255 //=======================================================================
256 Standard_Integer BlockFix_CheckTool::NbPossibleBlocks() const
257 {
258   return myPossibleBlocks.Length();
259 }
260
261 //=======================================================================
262 //function : PossibleBlock
263 //purpose  :
264 //=======================================================================
265 TopoDS_Shape BlockFix_CheckTool::PossibleBlock(const Standard_Integer num) const
266 {
267   TopoDS_Shape res;
268   if( num>0 && num<=myPossibleBlocks.Length() )
269     res = myPossibleBlocks.Value(num);
270   return res;
271 }
272
273 //=======================================================================
274 //function : DumpCheckResult
275 //purpose  :
276 //=======================================================================
277 void BlockFix_CheckTool::DumpCheckResult(Standard_OStream& S) const
278 {
279   if(!myHasCheck)
280     S<<"Check not performed!"<<std::endl;
281   else {
282     S<<"dump results of check:"<<std::endl;
283     S<<"  total number of solids = "<<myNbSolids<<std::endl;
284     S<<"  including: number of good blocks = "<<myNbBlocks<<std::endl;
285     S<<"             number of possible blocks = "<<NbPossibleBlocks()<<std::endl;
286     S<<"             including: need remove degenerative = "<<myNbDegen<<std::endl;
287     S<<"                        need unionfaces = "<<myNbUF<<std::endl;
288     S<<"                        need unionedges = "<<myNbUE<<std::endl;
289     S<<"                        need both unionfaces and unionedges = "<<myNbUFUE<<std::endl;
290     Standard_Integer nbtmp = myNbSolids - myNbBlocks - NbPossibleBlocks();
291     S<<"             number of impossible blocks = "<<nbtmp<<std::endl;
292   }
293 }
294
295 //=======================================================================
296 //function : isC1
297 //purpose  :
298 //=======================================================================
299 Standard_Boolean BlockFix_CheckTool::isC1
300           (const TopTools_ListOfShape &theEdges) const
301 {
302   // Fill the map vertex - list of ancestor edges
303   TopTools_IndexedDataMapOfShapeListOfShape aMapVE;
304   TopTools_ListIteratorOfListOfShape        anIter(theEdges);
305   TopTools_MapOfShape                       aMapFence;
306   Standard_Integer                          i;
307   Standard_Integer                          aNbVtx;
308
309   for (; anIter.More(); anIter.Next()) {
310     TopTools_IndexedMapOfShape  aMapVtx;
311     const TopoDS_Shape         &anEdge = anIter.Value();
312
313     if (aMapFence.Add(anEdge)) {
314       TopExp::MapShapes(anEdge, TopAbs_VERTEX, aMapVtx);
315       aNbVtx = aMapVtx.Extent();
316
317       for (i = 1; i <= aNbVtx; ++i) {
318         const TopoDS_Shape &aVtx = aMapVtx.FindKey(i);
319
320         if (!aMapVE.Contains(aVtx)) {
321           aMapVE.Add(aVtx, TopTools_ListOfShape());
322         }
323
324         aMapVE.ChangeFromKey(aVtx).Append(anEdge);
325       }
326     }
327   }
328
329   // Check C1 continuity.
330   Standard_Integer aNbEnds = 0;
331
332   for (i = 1, aNbVtx = aMapVE.Extent(); i <= aNbVtx; ++i) {
333     const TopTools_ListOfShape &anEdges  = aMapVE.FindFromIndex(i);
334     Standard_Integer            aNbEdges = anEdges.Extent();
335
336     if (aNbEdges == 1) {
337       ++aNbEnds;
338     } else if (aNbEdges == 2) {
339       TopoDS_Vertex      aCommonVtx = TopoDS::Vertex(aMapVE.FindKey(i));
340       TopoDS_Edge        anEdge1    = TopoDS::Edge(anEdges.First());
341       TopoDS_Edge        anEdge2    = TopoDS::Edge(anEdges.Last());
342       Standard_Real      aParam1    = BRep_Tool::Parameter(aCommonVtx, anEdge1);
343       Standard_Real      aParam2    = BRep_Tool::Parameter(aCommonVtx, anEdge2);
344       Standard_Real      aPar[2];
345       Handle(Geom_Curve) aCurve1    =
346         BRep_Tool::Curve(anEdge1, aPar[0], aPar[1]);
347       Handle(Geom_Curve) aCurve2    =
348         BRep_Tool::Curve(anEdge2, aPar[0], aPar[1]);
349       gp_Pnt             aPnt;
350       gp_Vec             aVec1;
351       gp_Vec             aVec2;
352
353       aCurve1->D1(aParam1, aPnt, aVec1);
354       aCurve2->D1(aParam2, aPnt, aVec2);
355
356       if (anEdge1.Orientation() != anEdge2.Orientation()) {
357         // Orientations are different. One vector should be reversed.
358         aVec1.Reverse();
359       }
360
361       const Standard_Real anAngle = aVec1.Angle(aVec2);
362
363       if (anAngle > myAngTolerance) {
364         // There is no C1 continuity.
365         break;
366       }
367     } else {
368       // Non-manifold case.
369       break;
370     }
371   }
372
373   return (i > aNbVtx && aNbEnds == 2);
374 }