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