Salome HOME
Windows compatibility
[modules/geom.git] / src / ShHealOper / ShHealOper_FillHoles.cxx
1 // Copyright (C) 2007-2016  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:      ShHealOper_FillHoles.cxx
24 // Created:   26.04.04 17:35:30
25 // Author:    Galina KULIKOVA
26
27 #include <Basics_OCCTVersion.hxx>
28
29 #include <ShapeFix_Shell.hxx>
30 #include <ShapeFix_Face.hxx>
31 #include <ShHealOper_FillHoles.hxx>
32 #include <ShapeAnalysis_FreeBounds.hxx>
33
34 #include <BRep_Tool.hxx>
35 #include <BRepAdaptor_Curve.hxx>
36 #include <BRepAdaptor_HCurve.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRepFill_CurveConstraint.hxx>
39 #include <BRepBuilderAPI_MakeFace.hxx>
40
41 #include <TopAbs_ShapeEnum.hxx>
42 #include <TopExp.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopoDS.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Shell.hxx>
47 #include <TopoDS_Compound.hxx>
48 #include <TopoDS_Iterator.hxx>
49 #include <TopLoc_Location.hxx>
50 #include <TopTools_ListOfShape.hxx>
51 #include <TopTools_HSequenceOfShape.hxx>
52 #include <TopTools_IndexedMapOfShape.hxx>
53
54 #include <Geom_Curve.hxx>
55 #include <Geom_BSplineSurface.hxx>
56 #include <GeomPlate_Surface.hxx>
57 #include <GeomPlate_MakeApprox.hxx>
58 #include <GeomPlate_CurveConstraint.hxx>
59 #include <GeomPlate_PlateG0Criterion.hxx>
60 #include <GeomPlate_BuildPlateSurface.hxx>
61
62 #include <TColgp_SequenceOfXY.hxx>
63 #include <TColgp_SequenceOfXYZ.hxx>
64
65 #include <Precision.hxx>
66
67 //=======================================================================
68 //function : ShHealOper_FillHoles()
69 //purpose  : Constructor
70 //=======================================================================
71 ShHealOper_FillHoles::ShHealOper_FillHoles ()
72 {
73   InitParameters();
74 }
75
76 //=======================================================================
77 //function : ShHealOper_FillHoles
78 //purpose  :
79 //=======================================================================
80 ShHealOper_FillHoles::ShHealOper_FillHoles (const TopoDS_Shape& theShape)
81 {
82   Init(theShape);
83   InitParameters();
84 }
85
86 //=======================================================================
87 //function : Init
88 //purpose  :
89 //=======================================================================
90 void ShHealOper_FillHoles::Init(const TopoDS_Shape& theShape)
91 {
92   ShHealOper_Tool::Init(theShape);
93   TopExp::MapShapesAndAncestors( myInitShape, TopAbs_EDGE,TopAbs_SHELL   , myEdgeShells);
94   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_COMPOUND, myEdgeComps );
95
96   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_FACE, myEdgeFaces );
97 }
98
99 //=======================================================================
100 //function : InitParameters
101 //purpose  :
102 //=======================================================================
103 void ShHealOper_FillHoles::InitParameters(Standard_Integer theDegree,
104                                           Standard_Integer theNbPtsOnCur,
105                                           Standard_Integer theNbIter,
106                                           Standard_Real theTol3d,
107                                           Standard_Real theTol2d,
108                                           Standard_Real theTolAng,
109                                           Standard_Real theTolCrv,
110                                           Standard_Integer theMaxDeg,
111                                           Standard_Integer theMaxSeg)
112 {
113   myNbPtsOnCur = theNbPtsOnCur;
114   myNbIter = theNbIter;
115   myDegree =theDegree;
116   myTol2d = theTol2d;
117   myTol3d = theTol3d;
118   myTolAng = theTolAng;
119   myTolCrv = theTolCrv;
120   myMaxDeg = theMaxDeg;
121   myMaxSeg =theMaxSeg;
122 }
123 //=======================================================================
124 //function : Fill
125 //purpose  :
126 //=======================================================================
127 Standard_Boolean ShHealOper_FillHoles::Fill()
128 {
129   ShapeAnalysis_FreeBounds sab(myInitShape);
130   TopoDS_Compound aCompClosed = sab.GetClosedWires();
131   TopoDS_Compound aCompOpen = sab.GetOpenWires();
132   TopTools_SequenceOfShape aFillWires;
133   if(!aCompClosed.IsNull()) {
134     TopoDS_Iterator aIt(aCompClosed);
135
136     for( ; aIt.More(); aIt.Next())
137       aFillWires.Append(aIt.Value());
138   }
139   if(!aCompOpen.IsNull()) {
140     TopoDS_Iterator aIt(aCompOpen);
141     for(  ; aIt.More(); aIt.Next())
142       aFillWires.Append(aIt.Value());
143   }
144
145   TopExp_Explorer aExp(myInitShape,TopAbs_EDGE,TopAbs_FACE);
146
147   for( ; aExp.More(); aExp.Next())
148     aFillWires.Append(aExp.Current());
149
150   return Fill(aFillWires);
151 }
152
153 //=======================================================================
154 //function : Fill
155 //purpose  :
156 //=======================================================================
157 Standard_Boolean ShHealOper_FillHoles::Fill(const TopTools_SequenceOfShape& theFillShapes)
158 {
159   myDone = Standard_False;
160   myErrorStatus = ShHealOper_NotError;
161   if(myInitShape.IsNull()) {
162     myErrorStatus = ShHealOper_InvalidParameters;
163     return myDone;
164   }
165   if(!theFillShapes.Length()) {
166     return myDone;
167   }
168
169   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
170   if(!prepareWires(theFillShapes,aSeqWires)) {
171     myErrorStatus = ShHealOper_InvalidParameters;
172     return myDone;
173   }
174
175   myResultShape = myInitShape;
176   Standard_Integer i =1;
177   for( ; i <= aSeqWires->Length(); i++) {
178     TopoDS_Wire aWire = TopoDS::Wire(aSeqWires->Value(i));
179     Handle(TColGeom2d_HArray1OfCurve) aCurves2d;
180     Handle(TColStd_HArray1OfInteger) aOrders;
181     Handle(TColStd_HArray1OfInteger) aSenses;
182     Handle(Geom_Surface) aSurf = buildSurface(aWire,aCurves2d,aOrders,aSenses);
183     if(aSurf.IsNull())
184       myErrorStatus = ShHealOper_ErrorExecution;
185     else
186       myDone = (addFace(aSurf,aWire,aCurves2d,aOrders,aSenses) || myDone);
187   }
188   if(myDone)
189   {
190     myResultShape = myContext->Apply(myResultShape);
191     myStatistics.AddModif( "Face created to fill hole" , aSeqWires->Length() );
192   }
193   return myDone;
194 }
195
196 //=======================================================================
197 //function : isCircle
198 //purpose  :
199 //=======================================================================
200 static Standard_Boolean isCircle(const TopoDS_Edge theEdge)
201 {
202   Standard_Real aFirst, aLast;
203   Handle(Geom_Curve) aC3D = BRep_Tool::Curve(theEdge,aFirst, aLast );
204   if(aC3D.IsNull()) return Standard_False;
205   Standard_Boolean isCirc = (aC3D->Value(aFirst).Distance(aC3D->Value(aLast)) <
206      aC3D->Value(aFirst).Distance(aC3D->Value((aFirst +aLast)/2)));
207   return isCirc;
208 }
209 //=======================================================================
210 //function : prepareWires
211 //purpose  :
212 //=======================================================================
213
214 Standard_Boolean ShHealOper_FillHoles::prepareWires(const TopTools_SequenceOfShape& theFillShapes,
215                                                     Handle(TopTools_HSequenceOfShape)& theSeqWires)
216 {
217   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
218   Standard_Integer i =1;
219   for( ; i <= theFillShapes.Length(); i++) {
220     TopExp_Explorer aExp;
221     for (aExp.Init (theFillShapes.Value(i),TopAbs_WIRE); aExp.More(); aExp.Next()) {
222       TopoDS_Iterator aIt(aExp.Current());
223       Standard_Boolean isAdd = Standard_True;
224       for( ; aIt.More() && isAdd; aIt.Next()) {
225         if(myEdgeFaces.Contains(aIt.Value()))
226           isAdd =  (myEdgeFaces.FindFromKey(aIt.Value()).Extent() <2);
227       }
228       if(isAdd)
229         theSeqWires->Append(aExp.Current());
230     }
231     for (aExp.Init (theFillShapes.Value(i),TopAbs_EDGE, TopAbs_WIRE); aExp.More(); aExp.Next()) {
232       if (!BRep_Tool::Degenerated (TopoDS::Edge (aExp.Current())))
233         if(myEdgeFaces.Contains(aExp.Current()) && myEdgeFaces.FindFromKey(aExp.Current()).Extent() >1)
234           continue;
235         aSeqEdges->Append(aExp.Current());
236     }
237   }
238
239   if(aSeqEdges->Length())
240   {
241     Standard_Real aTol = 0.;
242     Standard_Boolean aShared = Standard_True;
243     Handle(TopTools_HSequenceOfShape) aTmpWires = new TopTools_HSequenceOfShape;
244     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, aTol, aShared, aTmpWires);
245     Handle(TopTools_HSequenceOfShape) anWiresClosed = new TopTools_HSequenceOfShape,
246     anWiresOpen   = new TopTools_HSequenceOfShape;
247     ShapeAnalysis_FreeBounds::SplitWires(aTmpWires, aTol, aShared, anWiresClosed, anWiresOpen);
248
249     for (i = 1; i <= anWiresClosed->Length(); i++)
250       theSeqWires->Append (anWiresClosed->Value (i));
251     for (i = 1; i <= anWiresOpen->Length(); i++)
252       theSeqWires->Append (anWiresOpen->Value (i));
253   }
254
255   for( i =1; i <= theSeqWires->Length(); i++) {
256     TopoDS_Wire aWire = TopoDS::Wire(theSeqWires->Value(i));
257
258     TopoDS_Iterator aIt(aWire);
259     Standard_Integer ne =0;
260     TopoDS_Edge ae;
261     for( ; aIt.More(); aIt.Next(), ne++)
262       ae = TopoDS::Edge(aIt.Value());
263     if((ne == 1) && ( !isCircle(ae))) {
264       theSeqWires->Remove(i--);
265       continue;
266     }
267   }
268   return (theSeqWires->Length());
269 }
270 //=======================================================================
271 //function : buildSurface
272 //purpose  :
273 //=======================================================================
274
275 Handle(Geom_Surface) ShHealOper_FillHoles::buildSurface(const TopoDS_Wire& theWire,
276                                                         Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
277                                                         Handle(TColStd_HArray1OfInteger)& theOrders,
278                                                         Handle(TColStd_HArray1OfInteger)& theSenses)
279 {
280   Handle(Geom_BSplineSurface) aSurf;
281   try {
282       GeomPlate_BuildPlateSurface aBuilder(myDegree, myNbPtsOnCur, myNbIter,
283                                                  myTol2d, myTol3d, myTolAng, myTolCrv);
284       TopoDS_Iterator aIter;
285       for(aIter.Initialize (theWire); aIter.More(); aIter.Next()) {
286
287         TopoDS_Edge ae = TopoDS::Edge(aIter.Value());
288         BRepAdaptor_Curve adC(ae);
289         Handle(BRepAdaptor_HCurve) aHAD= new BRepAdaptor_HCurve(adC);
290         Handle(BRepFill_CurveConstraint) aConst =
291             new BRepFill_CurveConstraint (Handle(Adaptor3d_HCurve)::DownCast(aHAD), (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
292         //Handle(GeomPlate_CurveConstraint) aConst =
293          // new GeomPlate_CurveConstraint(aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
294         aBuilder.Add (Handle(GeomPlate_CurveConstraint)::DownCast(aConst));
295       }
296       aBuilder.Perform();
297       if(!aBuilder.IsDone())
298         return aSurf;
299       Handle(GeomPlate_Surface) aPlSurf = aBuilder.Surface();
300
301       //for filling holes without initial specified surface
302       //the initial surface should be build by GeomPlate itself
303       //following code was taken from BRepFill_Filling::Build
304
305       Standard_Real aDist = aBuilder.G0Error();
306       TColgp_SequenceOfXY S2d;
307       TColgp_SequenceOfXYZ S3d;
308       S2d.Clear();
309       S3d.Clear();
310       aBuilder.Disc2dContour(4,S2d);
311       aBuilder.Disc3dContour(4,0,S3d);
312       Standard_Real amaxTol = Max( myTol3d, 10* aDist);
313       GeomPlate_PlateG0Criterion Criterion( S2d, S3d, amaxTol );
314       GeomPlate_MakeApprox Approx( aPlSurf, Criterion, myTol3d, myMaxSeg, myMaxDeg );
315       aSurf = Approx.Surface();
316       if(aSurf.IsNull())
317         return aSurf;
318
319       theCurves2d = aBuilder.Curves2d();
320       theOrders    = aBuilder.Order();
321       theSenses    = aBuilder.Sense();
322     }
323
324   catch (Standard_Failure) {
325     aSurf.Nullify();
326     return aSurf;
327   }
328   return aSurf;
329 }
330
331 //=======================================================================
332 //function : addFace
333 //purpose  :
334 //=======================================================================
335
336 Standard_Boolean ShHealOper_FillHoles::addFace(const Handle(Geom_Surface)& theSurf,
337                                                const TopoDS_Wire& theWire,
338                                                const Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
339                                                const Handle(TColStd_HArray1OfInteger)& theOrders,
340                                                const Handle(TColStd_HArray1OfInteger)& theSenses)
341 {
342   BRepBuilderAPI_MakeFace aMakeFace (theSurf, Precision::Confusion());
343   TopoDS_Face aFace = aMakeFace.Face();
344   aFace.EmptyCopy();
345
346   TopoDS_Wire aWire;
347   BRep_Builder aB;
348   aB.MakeWire(aWire);
349
350   TopTools_IndexedMapOfShape aMapParent;
351   Standard_Integer aInd = 1;
352   Standard_Boolean hasShell = Standard_False;
353   TopoDS_Iterator aIter(theWire);
354   for ( ; aIter.More(); aIter.Next(), aInd++) {
355     TopoDS_Edge anEdge = TopoDS::Edge (aIter.Value());
356     Standard_Real aF, aL;
357     BRep_Tool::Range (anEdge, aF, aL);
358     TopLoc_Location aLoc;
359     aB.UpdateEdge (anEdge, theCurves2d->Value (aInd),aFace, 0.);
360
361     aB.Range (anEdge, aFace, aF, aL);
362
363     // Set orientation of the edge: orientation should be changed
364     // if its orientation does not make sence with curve orientation
365     // recommended by GeomPlate
366     if ((anEdge.Orientation() == TopAbs_FORWARD) ==
367         (theSenses->Value (theOrders->Value (aInd)) == 1)) {
368       anEdge.Reverse();
369     }
370     aB.SameParameter(anEdge,Standard_False);
371     aB.Add (aWire, anEdge);
372     TopoDS_Shape aParent;
373     if(!myEdgeFaces.Contains(anEdge))
374       continue;
375
376     if(myEdgeFaces.FindFromKey(anEdge).Extent() >1)
377       continue;
378
379     if(myEdgeShells.Contains(anEdge)) {
380       if(myEdgeShells.FindFromKey(anEdge).Extent()) {
381        aParent = myEdgeShells.FindFromKey(anEdge).First();
382        hasShell = Standard_True;
383        aMapParent.Add(aParent);
384      }
385     }
386     else if(myEdgeComps.Contains(anEdge)) {
387       if(myEdgeComps.FindFromKey(anEdge).Extent()) {
388         aParent = myEdgeComps.FindFromKey(anEdge).First();
389         aMapParent.Add(aParent);
390       }
391     }
392
393   }
394   aB.Add(aFace,aWire);
395   Handle(ShapeFix_Face) aSff = new ShapeFix_Face(aFace);
396   aSff->SetContext(myContext);
397   aSff->SetPrecision(myTol3d);
398   aSff->Perform();
399   if(aSff->Status(ShapeExtend_FAIL)) {
400     myErrorStatus = ShHealOper_ErrorExecution;
401     return Standard_False;
402   }
403   //theFace = aSff->Face();
404   TopoDS_Shape aResShape = aSff->Result();
405   getResShape(aResShape,aMapParent,hasShell);
406   return Standard_True;
407 }
408
409 //=======================================================================
410 //function : getResShape
411 //purpose  :
412 //=======================================================================
413
414 void ShHealOper_FillHoles::getResShape(const TopoDS_Shape& theAddShape,
415                                        const TopTools_IndexedMapOfShape& aMapParent,
416                                        const Standard_Boolean theHasShell)
417 {
418   BRep_Builder aB;
419
420   if(!aMapParent.Extent()) {
421     TopoDS_Compound aComp;
422     aB.MakeCompound(aComp);
423     TopoDS_Shape aresShape = myContext->Apply(myResultShape);
424     aB.Add(aComp,aresShape);
425     aB.Add(aComp,theAddShape);
426     myResultShape = aComp;
427     return ;
428   }
429   Standard_Boolean anhasShell = theHasShell;
430   TopoDS_Shell aTmpShell;
431   aB.MakeShell(aTmpShell);
432   TopTools_SequenceOfShape aseqShells;
433   if(anhasShell) {
434     aB.Add(aTmpShell,theAddShape);
435     Standard_Integer i =1;
436     for( ; i <= aMapParent.Extent(); i++) {
437       TopoDS_Shape aParShape = myContext->Apply(aMapParent.FindKey(i));
438       if(aParShape.ShapeType() == TopAbs_SHELL) {
439         TopExp_Explorer aexp(aParShape,TopAbs_FACE);
440         for( ; aexp.More(); aexp.Next())
441           aB.Add(aTmpShell,aexp.Current());
442         aseqShells.Append(aParShape);
443       }
444     }
445     anhasShell = aseqShells.Length();
446   }
447   if(anhasShell) {
448     Handle(ShapeFix_Shell) asfs = new ShapeFix_Shell;
449     asfs->FixFaceOrientation(aTmpShell);
450     TopoDS_Shape anshape = asfs->Shape();
451     myContext->Replace(aseqShells.Value(1),anshape);
452     Standard_Integer i =2;
453     for( ; i<= aseqShells.Length(); i++)
454       myContext->Remove(aseqShells.Value(i));
455   }
456   else {
457     TopoDS_Compound aComp;
458     aB.MakeCompound(aComp);
459     TopoDS_Shape oldshape = myContext->Apply(aMapParent.FindKey(1));
460     TopoDS_Iterator aIt(oldshape);
461     for( ; aIt.More(); aIt.Next())
462       aB.Add(aComp,aIt.Value());
463     aB.Add(aComp,theAddShape);
464     myContext->Replace( oldshape,aComp);
465   }
466 }