1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: BlockFix_CheckTool.cxx
24 // Created: 17.12.04 11:15:25
25 // Author: Sergey KUUL
27 #include <BlockFix_CheckTool.hxx>
29 #include <BRep_Tool.hxx>
31 #include <Geom_Curve.hxx>
37 #include <TopExp_Explorer.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Solid.hxx>
43 #include <TopoDS_Vertex.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TopTools_MapOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 //=======================================================================
51 //function : BlockFix_CheckTool()
52 //purpose : Constructor
53 //=======================================================================
54 BlockFix_CheckTool::BlockFix_CheckTool( )
56 myHasCheck = Standard_False;
58 myPossibleBlocks.Clear();
61 //=======================================================================
64 //=======================================================================
65 void BlockFix_CheckTool::SetShape(const TopoDS_Shape& aShape)
67 myHasCheck = Standard_False;
69 myPossibleBlocks.Clear();
72 //=======================================================================
73 //function : SetAngTolerance
75 //=======================================================================
76 void BlockFix_CheckTool::SetAngTolerance(const Standard_Real theTolerance)
78 myHasCheck = Standard_False;
79 myAngTolerance = theTolerance;
80 myPossibleBlocks.Clear();
83 //=======================================================================
86 //=======================================================================
87 void BlockFix_CheckTool::Perform()
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;
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()))
114 IsBlock = Standard_False;
117 IsBlock = Standard_False;
118 // check faces unification
119 TopTools_SequenceOfShape faces;
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());
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));
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));
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;
151 if (HasFacesForUnification) {
152 MayBeUF=Standard_True;
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)) {
170 if (nbe == 12 && DegenEdges.Extent() > 0) {
171 IsBlock = Standard_False;
173 myPossibleBlocks.Append(aSolid);
177 IsBlock = Standard_False;
179 // check edges unification
180 // creating map of edge faces
181 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
182 TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
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;
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);
204 TopTools_ListOfShape ListEdges;
205 ListEdges.Append(edge);
206 aMapFacesEdges.Add(face,ListEdges);
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.) {
218 // Check if edges have C1 continuity.
219 if (!isC1(ListEdges)) {
224 if (i <= aMapFacesEdges.Extent()) {
225 IsBlock = Standard_False;
226 MayBeUE = Standard_True;
236 myPossibleBlocks.Append(aSolid);
244 myPossibleBlocks.Append(aSolid);
249 myHasCheck = Standard_True;
252 //=======================================================================
253 //function : NbPossibleBlocks
255 //=======================================================================
256 Standard_Integer BlockFix_CheckTool::NbPossibleBlocks() const
258 return myPossibleBlocks.Length();
261 //=======================================================================
262 //function : PossibleBlock
264 //=======================================================================
265 TopoDS_Shape BlockFix_CheckTool::PossibleBlock(const Standard_Integer num) const
268 if( num>0 && num<=myPossibleBlocks.Length() )
269 res = myPossibleBlocks.Value(num);
273 //=======================================================================
274 //function : DumpCheckResult
276 //=======================================================================
277 void BlockFix_CheckTool::DumpCheckResult(Standard_OStream& S) const
280 S<<"Check not performed!"<<std::endl;
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;
295 //=======================================================================
298 //=======================================================================
299 Standard_Boolean BlockFix_CheckTool::isC1
300 (const TopTools_ListOfShape &theEdges) const
302 // Fill the map vertex - list of ancestor edges
303 TopTools_IndexedDataMapOfShapeListOfShape aMapVE;
304 TopTools_ListIteratorOfListOfShape anIter(theEdges);
305 TopTools_MapOfShape aMapFence;
307 Standard_Integer aNbVtx;
309 for (; anIter.More(); anIter.Next()) {
310 TopTools_IndexedMapOfShape aMapVtx;
311 const TopoDS_Shape &anEdge = anIter.Value();
313 if (aMapFence.Add(anEdge)) {
314 TopExp::MapShapes(anEdge, TopAbs_VERTEX, aMapVtx);
315 aNbVtx = aMapVtx.Extent();
317 for (i = 1; i <= aNbVtx; ++i) {
318 const TopoDS_Shape &aVtx = aMapVtx.FindKey(i);
320 if (!aMapVE.Contains(aVtx)) {
321 aMapVE.Add(aVtx, TopTools_ListOfShape());
324 aMapVE.ChangeFromKey(aVtx).Append(anEdge);
329 // Check C1 continuity.
330 Standard_Integer aNbEnds = 0;
332 for (i = 1, aNbVtx = aMapVE.Extent(); i <= aNbVtx; ++i) {
333 const TopTools_ListOfShape &anEdges = aMapVE.FindFromIndex(i);
334 Standard_Integer aNbEdges = anEdges.Extent();
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]);
353 aCurve1->D1(aParam1, aPnt, aVec1);
354 aCurve2->D1(aParam2, aPnt, aVec2);
356 if (anEdge1.Orientation() != anEdge2.Orientation()) {
357 // Orientations are different. One vector should be reversed.
361 const Standard_Real anAngle = aVec1.Angle(aVec2);
363 if (anAngle > myAngTolerance) {
364 // There is no C1 continuity.
368 // Non-manifold case.
373 return (i > aNbVtx && aNbEnds == 2);