Salome HOME
0022227: [CEA 827] Building a surface from a cloud of points
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SmoothingSurfaceDriver.cxx
1 //  Copyright (C) 2007-2008  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 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_SmoothingSurfaceDriver.hxx>
25 #include <GEOMImpl_ISmoothingSurface.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <TFunction_Logbook.hxx>
30 #include <StdFail_NotDone.hxx>
31
32 //@@ include required header files here @@//
33 #include <TopoDS_Vertex.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <TopoDS.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 //function : GetID
78 //purpose  :
79 //=======================================================================
80 const Standard_GUID& GEOMImpl_SmoothingSurfaceDriver::GetID()
81 {
82   static Standard_GUID aGUID("1C3A0F30-729D-4E83-8232-78E74FC5637C");
83   return aGUID;
84 }
85
86 //=======================================================================
87 //function : GEOMImpl_SmoothingSurfaceDriver
88 //purpose  :
89 //=======================================================================
90 GEOMImpl_SmoothingSurfaceDriver::GEOMImpl_SmoothingSurfaceDriver()
91 {
92 }
93
94 //=======================================================================
95 //function : MakeSmoothingSurfaceUnClosed
96 //purpose  :
97 //=======================================================================
98 TopoDS_Shape GEOMImpl_SmoothingSurfaceDriver::MakeSmoothingSurfaceUnClosed(Handle_TColgp_HArray1OfPnt myListOfPoints) const
99 {
100   TopoDS_Face aInitShape;
101   
102   // Create an average Plane
103   //Handle(TColgp_HArray1OfPnt) HAP = new TColgp_HArray1OfPnt(1,myListOfPoints.Length())
104   GeomPlate_BuildAveragePlane gpbap(myListOfPoints,myListOfPoints->Length(),Precision::Confusion(),1,1);
105   Handle(Geom_Plane) plane(gpbap.Plane());
106   Standard_Real Umin, Umax, Vmin, Vmax;
107   gpbap.MinMaxBox(Umin,Umax,Vmin,Vmax);
108   // cout << "Vals : " << Umin << ", " << Umax << ", " << Vmin << ", " << Vmax << endl;
109   BRepBuilderAPI_MakeFace mf(plane,Umin,Umax,Vmin,Vmax,Precision::Confusion());
110   aInitShape =  mf.Face();
111   //return aInitShape;
112
113   GeomPlate_BuildPlateSurface aBuilder(3,10);
114   // ** Initialization of surface
115   Handle(BRepAdaptor_HSurface) HSI = new BRepAdaptor_HSurface();
116   HSI->ChangeSurface().Initialize(aInitShape);
117   aBuilder.LoadInitSurface( BRep_Tool::Surface(HSI->ChangeSurface().Face()));
118
119   Standard_Integer j, j1, j2;
120   // cout << "Init surface" << endl;
121   j1 = myListOfPoints->Lower();
122   j2 = myListOfPoints->Upper();
123   for (j=j1; j<=j2 ; j++)
124   {
125     gp_Pnt aPnt = myListOfPoints->Value(j); 
126     Handle(GeomPlate_PointConstraint) PCont = new GeomPlate_PointConstraint(aPnt,0);
127     aBuilder.Add(PCont);
128   }
129   // cout << "avant Perform surface" << endl;
130   aBuilder.Perform();
131   // cout << "Perform surface" << endl;
132
133   // A ce niveau : surface algo
134   Handle(GeomPlate_Surface) gpPlate = aBuilder.Surface();
135   
136   Standard_Integer nbcarreau=2;
137   Standard_Integer degmax=8;
138   Standard_Real seuil;
139   seuil = Max(0.0001,10*aBuilder.G0Error());
140   GeomPlate_MakeApprox Mapp(gpPlate,0.0001,nbcarreau,degmax,seuil);
141   // cout << "Approx surface" << endl;
142
143   Handle (Geom_Surface) Surf (Mapp.Surface());
144  
145   aBuilder.Surface()->Bounds( Umin, Umax, Vmin, Vmax);
146   
147   BRepBuilderAPI_MakeFace MF(Surf,Umin, Umax, Vmin, Vmax, Precision::Confusion());
148   TopoDS_Shape aShape = MF.Shape();
149   
150   return aShape;
151 }
152
153
154 //=======================================================================
155 //function : Execute
156 //purpose  :
157 //=======================================================================
158 Standard_Integer GEOMImpl_SmoothingSurfaceDriver::Execute(TFunction_Logbook& log) const
159 {
160   if (Label().IsNull()) return 0;
161   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
162   if (aFunction.IsNull()) return 0;
163
164   Standard_Integer aType = aFunction->GetType();
165
166   if (aType != SMOOTHINGSURFACE_LPOINTS) return 0;
167   // cout << "Youhou : " << aType << endl;
168
169   GEOMImpl_ISmoothingSurface aData (aFunction);
170   
171   Standard_Integer nbPoints = aData.GetLength();
172
173   Handle(TColgp_HArray1OfPnt) anArrayofPnt = new TColgp_HArray1OfPnt(1,nbPoints);
174   for (int ind=1;ind<=nbPoints;ind++)
175   {
176     Handle(GEOM_Function) aPoint = aData.GetPoint(ind);
177     TopoDS_Shape aShapePnt = aPoint->GetValue();
178     TopoDS_Vertex dsPoint;
179     dsPoint = TopoDS::Vertex( aShapePnt );
180     gp_Pnt aPnt = BRep_Tool::Pnt( dsPoint );
181     anArrayofPnt->SetValue(ind,aPnt);
182   }
183
184   TopoDS_Shape aShape;
185   aShape = GEOMImpl_SmoothingSurfaceDriver::MakeSmoothingSurfaceUnClosed(anArrayofPnt);
186
187   if (aShape.IsNull()) return 0;
188
189   aFunction->SetValue(aShape);
190
191   log.SetTouched(Label());
192
193   return 1;
194 }
195
196 //================================================================================
197 /*!
198  * \brief Returns a name of creation operation and names and values of creation parameters
199  */
200 //================================================================================
201
202 bool GEOMImpl_SmoothingSurfaceDriver::
203 GetCreationInformation(std::string&             theOperationName,
204                        std::vector<GEOM_Param>& theParams)
205 {
206   if (Label().IsNull()) return 0;
207   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
208
209   GEOMImpl_ISmoothingSurface aCI( function );
210   Standard_Integer aType = function->GetType();
211
212   theOperationName = "SMOOTHINGSURFACE";
213
214   switch ( aType ) {
215   case SMOOTHINGSURFACE_LPOINTS:
216     AddParam( theParams, "Points" );
217     if ( aCI.GetLength() > 1 )
218       theParams[0] << aCI.GetLength() << " points: ";
219     for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
220       theParams[0] << aCI.GetPoint( i ) << " ";
221     break;
222   default:
223     return false;
224   }
225   
226   return true;
227 }
228
229 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_SmoothingSurfaceDriver,GEOM_BaseDriver);
230 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_SmoothingSurfaceDriver,GEOM_BaseDriver);