]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Filling.cpp
Salome HOME
Fix for issue #2423: verify that maximal degree in Filling is enough to interpolate...
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Filling.cpp
1 // Copyright (C) 2017-20xx  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Filling.h"
22
23 #include <BRep_Tool.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <Geom_BSplineCurve.hxx>
26 #include <Geom_BSplineSurface.hxx>
27 #include <Geom_TrimmedCurve.hxx>
28 #include <GeomAPI_PointsToBSplineSurface.hxx>
29 #include <GeomFill_AppSurf.hxx>
30 #include <GeomFill_Line.hxx>
31 #include <GeomFill_SectionGenerator.hxx>
32 #include <Precision.hxx>
33 #include <ShapeFix_Face.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Face.hxx>
36
37 static void edgesToCurves(const std::list<GeomEdgePtr>& theEdges,
38                           std::list<Handle(Geom_Curve)>& theCurves)
39 {
40   for (std::list<GeomEdgePtr>::const_iterator anIt = theEdges.begin();
41        anIt != theEdges.end(); ++anIt) {
42     const TopoDS_Edge& anEdge = (*anIt)->impl<TopoDS_Edge>();
43     if (BRep_Tool::Degenerated(anEdge))
44       continue;
45
46     double aFirst, aLast;
47     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
48
49     aCurve = new Geom_TrimmedCurve(aCurve, aFirst, aLast);
50     if (anEdge.Orientation() == TopAbs_REVERSED)
51       aCurve->Reverse();
52
53     theCurves.push_back(aCurve);
54   }
55 }
56
57
58 GeomAlgoAPI_Filling::GeomAlgoAPI_Filling(const int theMinDegree,
59                                          const int theMaxDegree,
60                                          const int theNbIter,
61                                          const double theTol2D,
62                                          const double theTol3D)
63   : myMinDegree(theMinDegree),
64     myMaxDegree(theMaxDegree),
65     myNbIter(theNbIter),
66     myTol2D(theTol2D),
67     myTol3D(theTol3D)
68 {
69 }
70
71 void GeomAlgoAPI_Filling::add(const GeomEdgePtr theEdge)
72 {
73   myConstraints.push_back(theEdge);
74 }
75
76 void GeomAlgoAPI_Filling::build(bool isApproximate)
77 {
78   if (myConstraints.size() <= 1) // not enough edges
79     return;
80
81   if (isApproximate)
82     buildByControlPoints();
83   else
84     buildByEdges();
85 }
86
87 void GeomAlgoAPI_Filling::buildByEdges()
88 {
89   GeomFill_SectionGenerator aSection;
90
91   // obtain section curves
92   std::list<Handle(Geom_Curve)> aCurves;
93   edgesToCurves(myConstraints, aCurves);
94   for (std::list<Handle(Geom_Curve)>::iterator anIt = aCurves.begin();
95        anIt != aCurves.end(); ++anIt)
96     aSection.AddCurve(*anIt);
97
98   // a 'tolerance' is used to compare 2 knots
99   aSection.Perform(Precision::PConfusion());
100   int aNbCurves = (int)aCurves.size();
101   Handle(GeomFill_Line) aLine = new GeomFill_Line(aNbCurves);
102
103   // check myMaxDegree >= aCurves.size() - 1 to be able to interpolate a surface
104   if (myMaxDegree + 1 < aNbCurves)
105     return;
106
107   // perform filling by sections
108   GeomFill_AppSurf anAppSurf(myMinDegree, myMaxDegree, myTol3D, myTol2D, myNbIter);
109   anAppSurf.Perform(aLine, aSection);
110   if (!anAppSurf.IsDone())
111     return;
112
113   // build calculated surface
114   Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
115   anAppSurf.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
116   Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface(
117     anAppSurf.SurfPoles(), anAppSurf.SurfWeights(), anAppSurf.SurfUKnots(), anAppSurf.SurfVKnots(),
118     anAppSurf.SurfUMults(), anAppSurf.SurfVMults(), anAppSurf.UDegree(), anAppSurf.VDegree());
119
120   if (GBS.IsNull())
121     return;
122
123   // store result
124   TopoDS_Face aFace = BRepBuilderAPI_MakeFace(GBS, Precision::Confusion());
125   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
126   aShape->setImpl(new TopoDS_Shape(aFace));
127   setShape(aShape);
128   setDone(true);
129 }
130
131 static Handle(Geom_Curve) removeTrim(const Handle(Geom_Curve)& theCurve)
132 {
133   Handle(Geom_Curve) aCurve = theCurve;
134   Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
135   while (!aTC.IsNull()) {
136     aCurve = aTC->BasisCurve();
137     aTC = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
138   }
139   return aCurve;
140 }
141
142 void GeomAlgoAPI_Filling::buildByControlPoints()
143 {
144   // obtain section curves
145   std::list<Handle(Geom_Curve)> aCurves;
146   edgesToCurves(myConstraints, aCurves);
147
148   // compute maximal number of poles in B-spline curves
149   int aMaxPoles = 0;
150   std::list<Handle(Geom_Curve)>::iterator anIt = aCurves.begin();
151   for (; anIt != aCurves.end(); ++anIt) {
152     Handle(Geom_BSplineCurve) aBC = Handle(Geom_BSplineCurve)::DownCast(removeTrim(*anIt));
153     if (!aBC.IsNull())
154       aMaxPoles = Max(aMaxPoles, aBC->NbPoles());
155   }
156
157   // prepare array of points for creation bspline surface
158   // size of this array: by U parameter - number of curves,
159   // by V parameter - determ using MaxNbPoles but it's
160   // value must be between 21(min) and 101(max)
161   int aNbSections = (int) aCurves.size();
162   int aNbPntInSection = Max(21, 2 * aMaxPoles - 1);
163   TColgp_Array2OfPnt aPoints(1, aNbSections, 1, aNbPntInSection);
164   anIt = aCurves.begin();
165   for (int i = 1; anIt != aCurves.end(); ++i, ++anIt) {
166     Handle(Geom_Curve) aC = *anIt;
167     double fp = aC->FirstParameter();
168     double lp = aC->LastParameter();
169     double dp = (lp - fp) / (aNbPntInSection - 1);
170
171     gp_Pnt aPnt;
172     for (int j = 0; j < aNbPntInSection; j++) {
173       aC->D0(fp + dp * j, aPnt);
174       aPoints.SetValue(i, j+1, aPnt);
175     }
176   }
177
178   // convert a grid of points to B-spline surface
179   GeomAPI_PointsToBSplineSurface aPTB(aPoints, myMinDegree, myMaxDegree, GeomAbs_C2, myTol3D);
180   Handle(Geom_BSplineSurface) aBS = aPTB.Surface();
181   if (aBS.IsNull())
182     return;
183
184   // fix face orientation
185   TopoDS_Face aFace = BRepBuilderAPI_MakeFace(aBS, Precision::Confusion());
186   Handle(ShapeFix_Face) aFix = new ShapeFix_Face(aFace);
187   aFix->Perform();
188   aFix->FixOrientation();
189   aFace = aFix->Face();
190
191   // store result
192   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
193   aShape->setImpl(new TopoDS_Shape(aFace));
194   setShape(aShape);
195   setDone(true);
196 }