Salome HOME
Update copyrights
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Filling.cpp
1 // Copyright (C) 2017-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_Filling.h"
21
22 #include <BRep_Tool.hxx>
23 #include <BRepBuilderAPI_MakeFace.hxx>
24 #include <Geom_BSplineCurve.hxx>
25 #include <Geom_BSplineSurface.hxx>
26 #include <Geom_TrimmedCurve.hxx>
27 #include <GeomAPI_PointsToBSplineSurface.hxx>
28 #include <GeomFill_AppSurf.hxx>
29 #include <GeomFill_Line.hxx>
30 #include <GeomFill_SectionGenerator.hxx>
31 #include <Precision.hxx>
32 #include <ShapeFix_Face.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Face.hxx>
35
36 static void edgesToCurves(const std::list<GeomEdgePtr>& theEdges,
37                           std::list<Handle(Geom_Curve)>& theCurves)
38 {
39   for (std::list<GeomEdgePtr>::const_iterator anIt = theEdges.begin();
40        anIt != theEdges.end(); ++anIt) {
41     const TopoDS_Edge& anEdge = (*anIt)->impl<TopoDS_Edge>();
42     if (BRep_Tool::Degenerated(anEdge))
43       continue;
44
45     double aFirst, aLast;
46     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
47
48     aCurve = new Geom_TrimmedCurve(aCurve, aFirst, aLast);
49     if (anEdge.Orientation() == TopAbs_REVERSED)
50       aCurve->Reverse();
51
52     theCurves.push_back(aCurve);
53   }
54 }
55
56
57 GeomAlgoAPI_Filling::GeomAlgoAPI_Filling(const int theMinDegree,
58                                          const int theMaxDegree,
59                                          const int theNbIter,
60                                          const double theTol2D,
61                                          const double theTol3D)
62   : myMinDegree(theMinDegree),
63     myMaxDegree(theMaxDegree),
64     myNbIter(theNbIter),
65     myTol2D(theTol2D),
66     myTol3D(theTol3D)
67 {
68 }
69
70 void GeomAlgoAPI_Filling::add(const GeomEdgePtr theEdge)
71 {
72   myConstraints.push_back(theEdge);
73 }
74
75 void GeomAlgoAPI_Filling::build(bool isApproximate)
76 {
77   if (myConstraints.size() <= 1) // not enough edges
78     return;
79
80   if (isApproximate)
81     buildByControlPoints();
82   else
83     buildByEdges();
84 }
85
86 void GeomAlgoAPI_Filling::buildByEdges()
87 {
88   GeomFill_SectionGenerator aSection;
89
90   // obtain section curves
91   std::list<Handle(Geom_Curve)> aCurves;
92   edgesToCurves(myConstraints, aCurves);
93   for (std::list<Handle(Geom_Curve)>::iterator anIt = aCurves.begin();
94        anIt != aCurves.end(); ++anIt)
95     aSection.AddCurve(*anIt);
96
97   // a 'tolerance' is used to compare 2 knots
98   aSection.Perform(Precision::PConfusion());
99   int aNbCurves = (int)aCurves.size();
100   Handle(GeomFill_Line) aLine = new GeomFill_Line(aNbCurves);
101
102   // check myMaxDegree >= aCurves.size() - 1 to be able to interpolate a surface
103   if (myMaxDegree + 1 < aNbCurves) {
104     myError = "Unable to interpolate surface,"
105        " Max deg + 1 should be greater or equal than number of sections.";
106     return;
107   }
108
109   // perform filling by sections
110   GeomFill_AppSurf anAppSurf(myMinDegree, myMaxDegree, myTol3D, myTol2D, myNbIter);
111   anAppSurf.Perform(aLine, aSection);
112   if (!anAppSurf.IsDone()) {
113     myError = "Approximation algorithm failed.";
114     return;
115   }
116
117   // build calculated surface
118   Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
119   anAppSurf.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
120   Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface(
121     anAppSurf.SurfPoles(), anAppSurf.SurfWeights(), anAppSurf.SurfUKnots(), anAppSurf.SurfVKnots(),
122     anAppSurf.SurfUMults(), anAppSurf.SurfVMults(), anAppSurf.UDegree(), anAppSurf.VDegree());
123
124   if (GBS.IsNull())
125     return;
126
127   // store result
128   TopoDS_Face aFace = BRepBuilderAPI_MakeFace(GBS, Precision::Confusion());
129   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
130   aShape->setImpl(new TopoDS_Shape(aFace));
131   setShape(aShape);
132   setDone(true);
133 }
134
135 static Handle(Geom_Curve) removeTrim(const Handle(Geom_Curve)& theCurve)
136 {
137   Handle(Geom_Curve) aCurve = theCurve;
138   Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
139   while (!aTC.IsNull()) {
140     aCurve = aTC->BasisCurve();
141     aTC = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
142   }
143   return aCurve;
144 }
145
146 void GeomAlgoAPI_Filling::buildByControlPoints()
147 {
148   // obtain section curves
149   std::list<Handle(Geom_Curve)> aCurves;
150   edgesToCurves(myConstraints, aCurves);
151
152   // compute maximal number of poles in B-spline curves
153   int aMaxPoles = 0;
154   std::list<Handle(Geom_Curve)>::iterator anIt = aCurves.begin();
155   for (; anIt != aCurves.end(); ++anIt) {
156     Handle(Geom_BSplineCurve) aBC = Handle(Geom_BSplineCurve)::DownCast(removeTrim(*anIt));
157     if (!aBC.IsNull())
158       aMaxPoles = Max(aMaxPoles, aBC->NbPoles());
159   }
160
161   // prepare array of points for creation bspline surface
162   // size of this array: by U parameter - number of curves,
163   // by V parameter - determ using MaxNbPoles but it's
164   // value must be between 21(min) and 101(max)
165   int aNbSections = (int) aCurves.size();
166   int aNbPntInSection = Max(21, 2 * aMaxPoles - 1);
167   TColgp_Array2OfPnt aPoints(1, aNbSections, 1, aNbPntInSection);
168   anIt = aCurves.begin();
169   for (int i = 1; anIt != aCurves.end(); ++i, ++anIt) {
170     Handle(Geom_Curve) aC = *anIt;
171     double fp = aC->FirstParameter();
172     double lp = aC->LastParameter();
173     double dp = (lp - fp) / (aNbPntInSection - 1);
174
175     gp_Pnt aPnt;
176     for (int j = 0; j < aNbPntInSection; j++) {
177       aC->D0(fp + dp * j, aPnt);
178       aPoints.SetValue(i, j+1, aPnt);
179     }
180   }
181
182   // convert a grid of points to B-spline surface
183   GeomAPI_PointsToBSplineSurface aPTB(aPoints, myMinDegree, myMaxDegree, GeomAbs_C2, myTol3D);
184   Handle(Geom_BSplineSurface) aBS = aPTB.Surface();
185   if (aBS.IsNull())
186     return;
187
188   // fix face orientation
189   TopoDS_Face aFace = BRepBuilderAPI_MakeFace(aBS, Precision::Confusion());
190   Handle(ShapeFix_Face) aFix = new ShapeFix_Face(aFace);
191   aFix->Perform();
192   aFix->FixOrientation();
193   aFace = aFix->Face();
194
195   // store result
196   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
197   aShape->setImpl(new TopoDS_Shape(aFace));
198   setShape(aShape);
199   setDone(true);
200 }