Salome HOME
Mantis issue 0021414: There is a difference between vectors and other edges in Geometry.
[modules/geom.git] / src / ShHealOper / ShHealOper_FillHoles.cxx
1 // Copyright (C) 2007-2011  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:      ShHealOper_FillHoles.cxx
23 // Created:   26.04.04 17:35:30
24 // Author:    Galina KULIKOVA
25
26 #include <Basics_OCCTVersion.hxx>
27
28 #include <ShapeFix_Shell.hxx>
29 #include <ShapeFix_Face.hxx>
30 #include <ShHealOper_FillHoles.hxx>
31 #include <ShapeAnalysis_FreeBounds.hxx>
32
33 #include <BRep_Tool.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35 #include <BRepAdaptor_HCurve.hxx>
36 #include <BRep_Builder.hxx>
37 #include <BRepFill_CurveConstraint.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39
40 #include <TopAbs_ShapeEnum.hxx>
41 #include <TopExp.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopoDS.hxx>
44 #include <TopoDS_Edge.hxx>
45 #include <TopoDS_Shell.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <TopoDS_Iterator.hxx>
48 #include <TopLoc_Location.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopTools_HSequenceOfShape.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52
53 #include <Geom_Curve.hxx>
54 #include <Geom_BSplineSurface.hxx>
55 #include <GeomPlate_Surface.hxx>
56 #include <GeomPlate_MakeApprox.hxx>
57 #include <GeomPlate_CurveConstraint.hxx>
58 #include <GeomPlate_PlateG0Criterion.hxx>
59 #include <GeomPlate_BuildPlateSurface.hxx>
60
61 #include <TColgp_SequenceOfXY.hxx>
62 #include <TColgp_SequenceOfXYZ.hxx>
63
64 #include <Precision.hxx>
65
66 //=======================================================================
67 //function : ShHealOper_FillHoles()
68 //purpose  : Constructor
69 //=======================================================================
70 ShHealOper_FillHoles::ShHealOper_FillHoles ()
71 {
72   InitParameters();
73 }
74
75 //=======================================================================
76 //function : ShHealOper_FillHoles
77 //purpose  :
78 //=======================================================================
79 ShHealOper_FillHoles::ShHealOper_FillHoles (const TopoDS_Shape& theShape)
80 {
81   Init(theShape);
82   InitParameters();
83 }
84
85 //=======================================================================
86 //function : Init
87 //purpose  :
88 //=======================================================================
89 void ShHealOper_FillHoles::Init(const TopoDS_Shape& theShape)
90 {
91   ShHealOper_Tool::Init(theShape);
92   TopExp::MapShapesAndAncestors( myInitShape, TopAbs_EDGE,TopAbs_SHELL   , myEdgeShells);
93   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_COMPOUND, myEdgeComps );
94
95   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_FACE, myEdgeFaces );
96 }
97
98 //=======================================================================
99 //function : InitParameters
100 //purpose  :
101 //=======================================================================
102 void ShHealOper_FillHoles::InitParameters(Standard_Integer theDegree,
103                                           Standard_Integer theNbPtsOnCur,
104                                           Standard_Integer theNbIter,
105                                           Standard_Real theTol3d,
106                                           Standard_Real theTol2d,
107                                           Standard_Real theTolAng,
108                                           Standard_Real theTolCrv,
109                                           Standard_Integer theMaxDeg,
110                                           Standard_Integer theMaxSeg)
111 {
112   myNbPtsOnCur = theNbPtsOnCur;
113   myNbIter = theNbIter;
114   myDegree =theDegree;
115   myTol2d = theTol2d;
116   myTol3d = theTol3d;
117   myTolAng = theTolAng;
118   myTolCrv = theTolCrv;
119   myMaxDeg = theMaxDeg;
120   myMaxSeg =theMaxSeg;
121 }
122 //=======================================================================
123 //function : Fill
124 //purpose  :
125 //=======================================================================
126 Standard_Boolean ShHealOper_FillHoles::Fill()
127 {
128   ShapeAnalysis_FreeBounds sab(myInitShape);
129   TopoDS_Compound aCompClosed = sab.GetClosedWires();
130   TopoDS_Compound aCompOpen = sab.GetOpenWires();
131   TopTools_SequenceOfShape aFillWires;
132   if(!aCompClosed.IsNull()) {
133     TopoDS_Iterator aIt(aCompClosed);
134
135     for( ; aIt.More(); aIt.Next())
136       aFillWires.Append(aIt.Value());
137   }
138   if(!aCompOpen.IsNull()) {
139     TopoDS_Iterator aIt(aCompOpen);
140     for(  ; aIt.More(); aIt.Next())
141       aFillWires.Append(aIt.Value());
142   }
143
144   TopExp_Explorer aExp(myInitShape,TopAbs_EDGE,TopAbs_FACE);
145
146   for( ; aExp.More(); aExp.Next())
147     aFillWires.Append(aExp.Current());
148
149   return Fill(aFillWires);
150 }
151
152 //=======================================================================
153 //function : Fill
154 //purpose  :
155 //=======================================================================
156 Standard_Boolean ShHealOper_FillHoles::Fill(const TopTools_SequenceOfShape& theFillShapes)
157 {
158   myDone = Standard_False;
159   myErrorStatus = ShHealOper_NotError;
160   if(myInitShape.IsNull()) {
161     myErrorStatus = ShHealOper_InvalidParameters;
162     return myDone;
163   }
164   if(!theFillShapes.Length()) {
165     return myDone;
166   }
167
168   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
169   if(!prepareWires(theFillShapes,aSeqWires)) {
170     myErrorStatus = ShHealOper_InvalidParameters;
171     return myDone;
172   }
173
174   myResultShape = myInitShape;
175   Standard_Integer i =1;
176   for( ; i <= aSeqWires->Length(); i++) {
177     TopoDS_Wire aWire = TopoDS::Wire(aSeqWires->Value(i));
178     Handle(TColGeom2d_HArray1OfCurve) aCurves2d;
179     Handle(TColStd_HArray1OfInteger) aOrders;
180     Handle(TColStd_HArray1OfInteger) aSenses;
181     Handle(Geom_Surface) aSurf = buildSurface(aWire,aCurves2d,aOrders,aSenses);
182     if(aSurf.IsNull())
183       myErrorStatus = ShHealOper_ErrorExecution;
184     else
185       myDone = (addFace(aSurf,aWire,aCurves2d,aOrders,aSenses) || myDone);
186   }
187   if(myDone)
188     myResultShape = myContext->Apply(myResultShape);
189   return myDone;
190 }
191
192 //=======================================================================
193 //function : isCircle
194 //purpose  :
195 //=======================================================================
196 static Standard_Boolean isCircle(const TopoDS_Edge theEdge)
197 {
198   Standard_Real aFirst, aLast;
199   Handle(Geom_Curve) aC3D = BRep_Tool::Curve(theEdge,aFirst, aLast );
200   if(aC3D.IsNull()) return Standard_False;
201   Standard_Boolean isCirc = (aC3D->Value(aFirst).Distance(aC3D->Value(aLast)) <
202      aC3D->Value(aFirst).Distance(aC3D->Value((aFirst +aLast)/2)));
203   return isCirc;
204 }
205 //=======================================================================
206 //function : prepareWires
207 //purpose  :
208 //=======================================================================
209
210 Standard_Boolean ShHealOper_FillHoles::prepareWires(const TopTools_SequenceOfShape& theFillShapes,
211                                                     Handle(TopTools_HSequenceOfShape)& theSeqWires)
212 {
213   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
214   Standard_Integer i =1;
215   for( ; i <= theFillShapes.Length(); i++) {
216     TopExp_Explorer aExp;
217     for (aExp.Init (theFillShapes.Value(i),TopAbs_WIRE); aExp.More(); aExp.Next()) {
218       TopoDS_Iterator aIt(aExp.Current());
219       Standard_Boolean isAdd = Standard_True;
220       for( ; aIt.More() && isAdd; aIt.Next()) {
221         if(myEdgeFaces.Contains(aIt.Value()))
222           isAdd =  (myEdgeFaces.FindFromKey(aIt.Value()).Extent() <2);
223       }
224       if(isAdd)
225         theSeqWires->Append(aExp.Current());
226     }
227     for (aExp.Init (theFillShapes.Value(i),TopAbs_EDGE, TopAbs_WIRE); aExp.More(); aExp.Next()) {
228       if (!BRep_Tool::Degenerated (TopoDS::Edge (aExp.Current())))
229         if(myEdgeFaces.Contains(aExp.Current()) && myEdgeFaces.FindFromKey(aExp.Current()).Extent() >1)
230           continue;
231         aSeqEdges->Append(aExp.Current());
232     }
233   }
234
235   if(aSeqEdges->Length())
236   {
237     Standard_Real aTol = 0.;
238     Standard_Boolean aShared = Standard_True;
239     Handle(TopTools_HSequenceOfShape) aTmpWires = new TopTools_HSequenceOfShape;
240     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, aTol, aShared, aTmpWires);
241     Handle(TopTools_HSequenceOfShape) anWiresClosed = new TopTools_HSequenceOfShape,
242     anWiresOpen   = new TopTools_HSequenceOfShape;
243     ShapeAnalysis_FreeBounds::SplitWires(aTmpWires, aTol, aShared, anWiresClosed, anWiresOpen);
244
245     for (i = 1; i <= anWiresClosed->Length(); i++)
246       theSeqWires->Append (anWiresClosed->Value (i));
247     for (i = 1; i <= anWiresOpen->Length(); i++)
248       theSeqWires->Append (anWiresOpen->Value (i));
249   }
250
251   for( i =1; i <= theSeqWires->Length(); i++) {
252     TopoDS_Wire aWire = TopoDS::Wire(theSeqWires->Value(i));
253
254     TopoDS_Iterator aIt(aWire);
255     Standard_Integer ne =0;
256     TopoDS_Edge ae;
257     for( ; aIt.More(); aIt.Next(), ne++)
258       ae = TopoDS::Edge(aIt.Value());
259     if((ne == 1) && ( !isCircle(ae))) {
260       theSeqWires->Remove(i--);
261       continue;
262     }
263   }
264   return (theSeqWires->Length());
265 }
266 //=======================================================================
267 //function : buildSurface
268 //purpose  :
269 //=======================================================================
270
271 Handle(Geom_Surface) ShHealOper_FillHoles::buildSurface(const TopoDS_Wire& theWire,
272                                                         Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
273                                                         Handle(TColStd_HArray1OfInteger)& theOrders,
274                                                         Handle(TColStd_HArray1OfInteger)& theSenses)
275 {
276   Handle(Geom_BSplineSurface) aSurf;
277   try {
278       GeomPlate_BuildPlateSurface aBuilder(myDegree, myNbPtsOnCur, myNbIter,
279                                                  myTol2d, myTol3d, myTolAng, myTolCrv);
280       TopoDS_Iterator aIter;
281       for(aIter.Initialize (theWire); aIter.More(); aIter.Next()) {
282
283         TopoDS_Edge ae = TopoDS::Edge(aIter.Value());
284         BRepAdaptor_Curve adC(ae);
285         Handle(BRepAdaptor_HCurve) aHAD= new BRepAdaptor_HCurve(adC);
286         Handle(BRepFill_CurveConstraint) aConst =
287             new BRepFill_CurveConstraint (aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
288         //Handle(GeomPlate_CurveConstraint) aConst =
289          // new GeomPlate_CurveConstraint(aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
290         aBuilder.Add (aConst);
291       }
292       aBuilder.Perform();
293       if(!aBuilder.IsDone())
294         return aSurf;
295       Handle(GeomPlate_Surface) aPlSurf = aBuilder.Surface();
296
297       //for filling holes without initial specified surface
298       //the initial surface should be build by GeomPlate itself
299       //following code was taken from BRepFill_Filling::Build
300
301       Standard_Real aDist = aBuilder.G0Error();
302       TColgp_SequenceOfXY S2d;
303       TColgp_SequenceOfXYZ S3d;
304       S2d.Clear();
305       S3d.Clear();
306       aBuilder.Disc2dContour(4,S2d);
307       aBuilder.Disc3dContour(4,0,S3d);
308       Standard_Real amaxTol = Max( myTol3d, 10* aDist);
309       GeomPlate_PlateG0Criterion Criterion( S2d, S3d, amaxTol );
310       GeomPlate_MakeApprox Approx( aPlSurf, Criterion, myTol3d, myMaxSeg, myMaxDeg );
311       aSurf = Approx.Surface();
312       if(aSurf.IsNull())
313         return aSurf;
314
315       theCurves2d = aBuilder.Curves2d();
316       theOrders    = aBuilder.Order();
317       theSenses    = aBuilder.Sense();
318     }
319
320   catch (Standard_Failure) {
321     aSurf.Nullify();
322     return aSurf;
323   }
324   return aSurf;
325 }
326
327 //=======================================================================
328 //function : addFace
329 //purpose  :
330 //=======================================================================
331
332 Standard_Boolean ShHealOper_FillHoles::addFace(const Handle(Geom_Surface)& theSurf,
333                                                const TopoDS_Wire& theWire,
334                                                const Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
335                                                const Handle(TColStd_HArray1OfInteger)& theOrders,
336                                                const Handle(TColStd_HArray1OfInteger)& theSenses)
337 {
338 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
339   BRepBuilderAPI_MakeFace aMakeFace (theSurf, Precision::Confusion());
340 #else
341   BRepBuilderAPI_MakeFace aMakeFace (theSurf);
342 #endif
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 }