Salome HOME
Copyright update 2021
[modules/geom.git] / src / AdvancedEngine / AdvancedEngine_SmoothingSurfaceDriver.cxx
1 // Copyright (C) 2013-2021  CEA/DEN, EDF R&D, OPEN CASCADE
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 <Standard_Stream.hxx>
21
22 #include "AdvancedEngine_SmoothingSurfaceDriver.hxx"
23 #include "AdvancedEngine_ISmoothingSurface.hxx"
24 #include "AdvancedEngine_Types.hxx"
25
26 #include "GEOM_Function.hxx"
27
28 #include <StdFail_NotDone.hxx>
29
30 #include <TopoDS_Vertex.hxx>
31 #include <TopoDS_Face.hxx>
32 #include <TopoDS_Compound.hxx>
33 #include <TopoDS.hxx>
34 #include <TopTools_MapOfShape.hxx>
35 #include <TopTools_MapIteratorOfMapOfShape.hxx>
36 #include <TopExp_Explorer.hxx>
37
38 #include <TColgp_SequenceOfPnt.hxx>
39 #include <TColgp_Array2OfPnt.hxx>
40 #include <TColgp_SequenceOfXY.hxx>
41 #include <TColgp_SequenceOfXYZ.hxx>
42 #include <TColStd_Array1OfInteger.hxx>
43
44 #include <BRepAdaptor_HSurface.hxx>
45
46 #include <BRep_Builder.hxx>
47 #include <BRepGProp.hxx>
48 #include <BRep_Tool.hxx>
49 #include <BRepPrimAPI_MakeSphere.hxx>
50 #include <BRepPrimAPI_MakeBox.hxx>
51 #include <BRepBuilderAPI_MakeFace.hxx>
52 #include <BRepAlgoAPI_Cut.hxx>
53
54 #include <GeomPlate_Surface.hxx>
55 #include <GeomPlate_BuildPlateSurface.hxx>
56 #include <GeomPlate_PointConstraint.hxx>
57 #include <GeomPlate_MakeApprox.hxx>
58 #include <GeomPlate_PlateG0Criterion.hxx>
59 #include <GeomPlate_BuildAveragePlane.hxx>
60
61 #include <Geom_BSplineSurface.hxx>
62 #include <Geom_Surface.hxx>
63 #include <Geom_Plane.hxx>
64
65 #include <GProp_GProps.hxx>
66 #include <Bnd_Box.hxx>
67 #include <BRepBndLib.hxx>
68
69 #include <gp_Pnt.hxx>
70 #include <gp_Pln.hxx>
71 #include <gp_Ax3.hxx>
72 #include <gp_Dir.hxx>
73 #include <gp_Vec.hxx>
74
75 #include <GC_MakePlane.hxx>
76
77 //=======================================================================
78 //function : GetID
79 //purpose  :
80 //=======================================================================
81 const Standard_GUID& AdvancedEngine_SmoothingSurfaceDriver::GetID()
82 {
83   static Standard_GUID aGUID("1C3A0F30-729D-4E83-8232-78E74FC5637C");
84   return aGUID;
85 }
86
87 //=======================================================================
88 //function : AdvancedEngine_SmoothingSurfaceDriver
89 //purpose  :
90 //=======================================================================
91 AdvancedEngine_SmoothingSurfaceDriver::AdvancedEngine_SmoothingSurfaceDriver()
92 {
93 }
94
95 //=======================================================================
96 //function : MakeSmoothingSurfaceUnClosed
97 //purpose  :
98 //=======================================================================
99 TopoDS_Shape AdvancedEngine_SmoothingSurfaceDriver::MakeSmoothingSurfaceUnClosed
100                           (const Handle(TColgp_HArray1OfPnt) &theListOfPoints,
101                            const Standard_Integer            theNbMax,
102                            const Standard_Integer            theDegMax,
103                            const Standard_Real               theDMax) const
104 {
105   TopoDS_Face aInitShape;
106   
107   // Create an average Plane
108   //Handle(TColgp_HArray1OfPnt) HAP = new TColgp_HArray1OfPnt(1,theListOfPoints.Length())
109   GeomPlate_BuildAveragePlane gpbap(theListOfPoints,theListOfPoints->Length(),Precision::Confusion(),1,1);
110   Handle(Geom_Plane) plane(gpbap.Plane());
111   Standard_Real Umin, Umax, Vmin, Vmax;
112   gpbap.MinMaxBox(Umin,Umax,Vmin,Vmax);
113   // cout << "Vals : " << Umin << ", " << Umax << ", " << Vmin << ", " << Vmax << endl;
114   BRepBuilderAPI_MakeFace mf(plane,Umin,Umax,Vmin,Vmax,Precision::Confusion());
115   aInitShape =  mf.Face();
116   //return aInitShape;
117
118   GeomPlate_BuildPlateSurface aBuilder(3,10);
119   // ** Initialization of surface
120   Handle(BRepAdaptor_HSurface) HSI = new BRepAdaptor_HSurface();
121   HSI->ChangeSurface().Initialize(aInitShape);
122   aBuilder.LoadInitSurface( BRep_Tool::Surface(HSI->ChangeSurface().Face()));
123
124   Standard_Integer j, j1, j2;
125   // cout << "Init surface" << endl;
126   j1 = theListOfPoints->Lower();
127   j2 = theListOfPoints->Upper();
128   for (j=j1; j<=j2 ; j++)
129   {
130     gp_Pnt aPnt = theListOfPoints->Value(j); 
131     Handle(GeomPlate_PointConstraint) PCont = new GeomPlate_PointConstraint(aPnt,0);
132     aBuilder.Add(PCont);
133   }
134   // cout << "avant Perform surface" << endl;
135   aBuilder.Perform();
136   // cout << "Perform surface" << endl;
137
138   // A ce niveau : surface algo
139   Handle(GeomPlate_Surface) gpPlate = aBuilder.Surface();
140   
141   Standard_Integer nbcarreau = (theNbMax > 0 ? theNbMax : 2);
142   Standard_Integer degmax = (theDegMax > 0 ? theDegMax : 8);
143   Standard_Real seuil = theDMax;
144
145   if (seuil <= 0.) {
146     seuil = Max(0.0001,10*aBuilder.G0Error());
147   }
148
149   GeomPlate_MakeApprox Mapp(gpPlate,0.0001,nbcarreau,degmax,seuil);
150   // cout << "Approx surface" << endl;
151
152   Handle (Geom_Surface) Surf (Mapp.Surface());
153  
154   aBuilder.Surface()->Bounds( Umin, Umax, Vmin, Vmax);
155   
156   BRepBuilderAPI_MakeFace MF(Surf,Umin, Umax, Vmin, Vmax, Precision::Confusion());
157   TopoDS_Shape aShape = MF.Shape();
158   
159   return aShape;
160 }
161
162
163 //=======================================================================
164 //function : Execute
165 //purpose  :
166 //=======================================================================
167 Standard_Integer AdvancedEngine_SmoothingSurfaceDriver::Execute(Handle(TFunction_Logbook)& log) const
168 {
169   if (Label().IsNull()) return 0;
170   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
171   if (aFunction.IsNull()) return 0;
172
173   Standard_Integer aType = aFunction->GetType();
174
175   if (aType != SMOOTHINGSURFACE_LPOINTS) return 0;
176   // cout << "Youhou : " << aType << endl;
177
178   AdvancedEngine_ISmoothingSurface aData (aFunction);
179
180   // Fill the map of vertices.
181   Standard_Integer    aNbShapes = aData.GetLength();
182   TopTools_MapOfShape aMapPoints;
183   Standard_Integer    i;
184
185   for (i = 1; i <= aNbShapes; i++) {
186     Handle(GEOM_Function) aFShape = aData.GetPntOrComp(i);
187     TopoDS_Shape aShape = aFShape->GetValue();
188
189     if (aShape.ShapeType() == TopAbs_VERTEX) {
190       aMapPoints.Add(aShape);
191     } else {
192       TopExp_Explorer anExp(aShape, TopAbs_VERTEX);
193
194       for (; anExp.More(); anExp.Next()) {
195         aMapPoints.Add(anExp.Current());
196       }
197     }
198   }
199
200   // Add points to the array of points.
201   const Standard_Integer      aNbPoints    = aMapPoints.Extent();
202   Handle(TColgp_HArray1OfPnt) anArrayofPnt =
203       new TColgp_HArray1OfPnt(1, aNbPoints);
204   TopTools_MapIteratorOfMapOfShape anIter(aMapPoints);
205
206   for (i = 1; anIter.More(); anIter.Next(), i++) {
207     TopoDS_Vertex aPoint = TopoDS::Vertex(anIter.Key());
208     gp_Pnt aPnt = BRep_Tool::Pnt(aPoint);
209
210     anArrayofPnt->SetValue(i, aPnt);
211   }
212
213   const Standard_Integer aNbMax  = aData.GetNbMax();
214   const Standard_Integer aDegMax = aData.GetDegMax();
215   const Standard_Real    aDMax   = aData.GetDMax();
216
217   // Make smoothing surface.
218   TopoDS_Shape aShape = AdvancedEngine_SmoothingSurfaceDriver::
219     MakeSmoothingSurfaceUnClosed(anArrayofPnt, aNbMax, aDegMax, aDMax);
220
221   if (aShape.IsNull()) return 0;
222
223   aFunction->SetValue(aShape);
224
225   log->SetTouched(Label());
226
227   return 1;
228 }
229
230 //================================================================================
231 /*!
232  * \brief Returns a name of creation operation and names and values of creation parameters
233  */
234 //================================================================================
235
236 bool AdvancedEngine_SmoothingSurfaceDriver::
237 GetCreationInformation(std::string&             theOperationName,
238                        std::vector<GEOM_Param>& theParams)
239 {
240   if (Label().IsNull()) return 0;
241   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
242
243   AdvancedEngine_ISmoothingSurface aCI( function );
244   Standard_Integer aType = function->GetType();
245
246   theOperationName = "SMOOTHINGSURFACE";
247
248   switch ( aType ) {
249   case SMOOTHINGSURFACE_LPOINTS:
250     {
251       AddParam( theParams, "Points" );
252       if ( aCI.GetLength() > 1 )
253         theParams[0] << aCI.GetLength() << " points: ";
254       for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
255         theParams[0] << aCI.GetPntOrComp( i ) << " ";
256
257       AddParam(theParams, "Max nbr of Bezier pieces", aCI.GetNbMax());
258       AddParam(theParams, "Max BSpline surface degree", aCI.GetDegMax());
259       AddParam(theParams, "3D tolerance of initial approximation", aCI.GetDMax());
260     }
261     break;
262   default:
263     return false;
264   }
265   
266   return true;
267 }
268
269 IMPLEMENT_STANDARD_RTTIEXT (AdvancedEngine_SmoothingSurfaceDriver,GEOM_BaseDriver)