Salome HOME
Merge from V5_1_4_BR 07/05/2010
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FillingDriver.cxx
1 //  Copyright (C) 2007-2010  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
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_FillingDriver.hxx>
26 #include <GEOM_Function.hxx>
27 #include <GEOMImpl_IFilling.hxx>
28 #include <GEOMImpl_Types.hxx>
29
30 #include <BRep_Tool.hxx>
31 #include <BRepAlgo.hxx>
32 #include <BRepBuilderAPI_MakeFace.hxx>
33 #include <BRep_Builder.hxx>
34
35 #include <TopAbs.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Compound.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopExp_Explorer.hxx>
42
43 #include <Geom_Curve.hxx>
44 #include <Geom_Surface.hxx>
45 #include <Geom_TrimmedCurve.hxx>
46 #include <Geom_Line.hxx>
47 #include <Geom_Circle.hxx>
48 #include <Geom_Ellipse.hxx>
49 #include <Geom_BezierCurve.hxx>
50 #include <Geom_BSplineSurface.hxx>
51 #include <GeomFill_Line.hxx>
52 #include <GeomFill_AppSurf.hxx>
53 #include <GeomFill_SectionGenerator.hxx>
54
55 #include <Precision.hxx>
56 #include <Standard_ConstructionError.hxx>
57
58 #include <TColGeom_SequenceOfCurve.hxx>
59 #include <ShapeFix_Face.hxx>
60 #include <GeomAPI_PointsToBSplineSurface.hxx>
61 #include <Geom_BSplineCurve.hxx>
62 #include <GeomAPI_PointsToBSpline.hxx>
63
64 #include <TColgp_SequenceOfPnt.hxx>
65 #include <TColgp_Array1OfPnt.hxx>
66
67 //#include <BRepTools.hxx>
68
69
70 //=======================================================================
71 //function : GetID
72 //purpose  :
73 //=======================================================================
74 const Standard_GUID& GEOMImpl_FillingDriver::GetID()
75 {
76   static Standard_GUID aFillingDriver ("FF1BBB62-5D14-4df2-980B-3A668264EA16");
77   return aFillingDriver;
78 }
79
80
81 //=======================================================================
82 //function : GEOMImpl_FillingDriver
83 //purpose  :
84 //=======================================================================
85
86 GEOMImpl_FillingDriver::GEOMImpl_FillingDriver()
87 {
88 }
89
90 //=======================================================================
91 //function : Execute
92 //purpose  :
93 //=======================================================================
94 Standard_Integer GEOMImpl_FillingDriver::Execute(TFunction_Logbook& log) const
95 {
96   if (Label().IsNull()) return 0;
97   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
98   if (aFunction.IsNull()) return 0;
99
100   if (aFunction->GetType() != BASIC_FILLING) return 0;
101
102   GEOMImpl_IFilling IF (aFunction);
103   Handle(GEOM_Function) aShapeFunction = IF.GetShape();
104   if (aShapeFunction.IsNull()) return 0;
105   TopoDS_Shape aShape = aShapeFunction->GetValue();
106   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) return 0;
107
108   Standard_Integer mindeg = IF.GetMinDeg();
109   Standard_Integer maxdeg = IF.GetMaxDeg();
110   Standard_Real tol3d = IF.GetTol2D();
111   Standard_Real tol2d = IF.GetTol3D();
112   Standard_Integer nbiter = IF.GetNbIter();
113   Standard_Boolean isApprox = IF.GetApprox();
114   Standard_Integer aMethod = IF.GetMethod();
115
116   if (mindeg > maxdeg) {
117     Standard_RangeError::Raise("Minimal degree can not be more than maximal degree");
118   }
119
120   /* we verify the contents of the shape */
121   TopExp_Explorer Ex;
122   TopoDS_Shape Scurrent;
123   Standard_Real First, Last;
124   Handle(Geom_Curve) C;
125
126   TopoDS_Compound aComp;
127   BRep_Builder B;
128   B.MakeCompound(aComp);
129
130   TopoDS_Iterator It (aShape);
131   for (; It.More(); It.Next()) {
132     Scurrent = It.Value();
133     if (Scurrent.ShapeType() != TopAbs_EDGE) {
134       Handle(Geom_BSplineCurve) newC;
135       if (Scurrent.ShapeType() == TopAbs_WIRE) {
136         TColgp_SequenceOfPnt PntSeq;
137         // collect points
138         for (Ex.Init(Scurrent, TopAbs_EDGE); Ex.More(); Ex.Next()) {
139           TopoDS_Edge E = TopoDS::Edge(Ex.Current());
140           if (BRep_Tool::Degenerated(E)) continue;
141           C = BRep_Tool::Curve(E, First, Last);
142           if( E.Orientation() == TopAbs_REVERSED ) {
143             C->Reverse();
144           }
145           Handle(Geom_TrimmedCurve) tc = Handle(Geom_TrimmedCurve)::DownCast(C);
146           while( !tc.IsNull() ) {
147             C = tc->BasisCurve();
148             tc = Handle(Geom_TrimmedCurve)::DownCast(C);
149           }
150           int nbp = 10;
151           if( C->IsKind(STANDARD_TYPE(Geom_Line)) ) {
152             nbp = 4;
153           }
154           else if( C->IsKind(STANDARD_TYPE(Geom_Circle)) || 
155                    C->IsKind(STANDARD_TYPE(Geom_Ellipse)) ) {
156             nbp = (int)25*fabs(Last-First)/(2*PI);
157           }
158           else if( C->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) {
159             Handle(Geom_BezierCurve) C3d = Handle(Geom_BezierCurve)::DownCast(C);
160             nbp = C3d->NbPoles();
161           }
162           else if( C->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ) {
163             Handle(Geom_BSplineCurve) C3d = Handle(Geom_BSplineCurve)::DownCast(C);
164             nbp = C3d->NbPoles();
165           }
166           else {
167           }
168           if( nbp<4 ) nbp = 4;
169           double dp = (Last-First)/(nbp-1);
170           for(int i=1; i<nbp; i++) {
171             gp_Pnt P;
172             C->D0(First+dp*(i-1),P);
173             PntSeq.Append(P);
174           }
175         }
176         // add last point
177         gp_Pnt P;
178         C->D0(Last,P);
179         PntSeq.Append(P);
180         // create BSpline 
181         if(PntSeq.Length()>1) {
182           TColgp_Array1OfPnt Pnts(1,PntSeq.Length());
183           // check orientation of wire
184           if( Scurrent.Orientation() == TopAbs_REVERSED ) {
185             for(int i=1; i<=PntSeq.Length(); i++) {
186               Pnts.SetValue(PntSeq.Length()-i+1,PntSeq.Value(i));
187             }
188           }
189           else {
190             for(int i=1; i<=PntSeq.Length(); i++) {
191               Pnts.SetValue(i,PntSeq.Value(i));
192             }
193           }
194           GeomAPI_PointsToBSpline PTB(Pnts);
195           newC = Handle(Geom_BSplineCurve)::DownCast(PTB.Curve());
196           // set periodic flag if curve is closed
197           //if( newC->IsClosed() ) {
198           //  newC->SetPeriodic();
199           //}
200           // create edge
201           double fp = newC->FirstParameter();
202           double lp = newC->FirstParameter();
203           gp_Pnt PF,PL;
204           newC->D0(fp,PF);
205           newC->D0(lp,PL);
206           TopoDS_Vertex VF,VL;
207           B.MakeVertex(VF,PF,1.e-7);
208           B.MakeVertex(VL,PL,1.e-7);
209           TopoDS_Edge newE;
210           B.MakeEdge(newE,newC,1.e-7);
211           B.Add(newE,VF);
212           B.Add(newE,VL.Reversed());
213           Scurrent = newE;
214         }
215       }
216       if(newC.IsNull()) {
217         Standard_ConstructionError::Raise("The argument compound must contain only edges");
218       }
219     }
220     B.Add(aComp,Scurrent);
221   }
222   aShape = aComp;
223
224   if (!isApprox) {
225     // make filling as in old version of SALOME (before 4.1.1)
226     GeomFill_SectionGenerator Section;
227     Standard_Integer i = 0;
228     Handle(Geom_Curve) aLastC;
229     gp_Pnt PL1,PL2;
230     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
231       Scurrent = Ex.Current();
232       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
233       if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
234       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
235       //if (Scurrent.Orientation() == TopAbs_REVERSED)
236       //  // Mantis isuue 0020659: consider the orientation of the edges
237       //  C = new Geom_TrimmedCurve(C, Last, First);
238       //else
239       //  C = new Geom_TrimmedCurve(C, First, Last);
240       C = new Geom_TrimmedCurve(C, First, Last);
241       gp_Pnt P1,P2;
242       C->D0(First,P1);
243       C->D0(Last,P2);
244
245       if( aMethod==1 && Scurrent.Orientation() == TopAbs_REVERSED ) {
246         C->Reverse();
247       }
248       else if( aMethod==2 ) {
249         if( i==0 ) {
250           PL1 = P1;
251           PL2 = P2;
252         }
253         else {
254           double d1 = PL1.Distance(P1) + PL2.Distance(P2);
255           double d2 = PL1.Distance(P2) + PL2.Distance(P1);
256           if(d2<d1) {
257             C->Reverse();
258             PL1 = P2;
259             PL2 = P1;
260           }
261           else {
262             PL1 = P1;
263             PL2 = P2;
264           }
265         }
266       }
267
268       Section.AddCurve(C);
269       i++;
270     }
271
272     /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
273     Section.Perform(Precision::Confusion());
274     Handle(GeomFill_Line) Line = new GeomFill_Line(i);
275
276     GeomFill_AppSurf App (mindeg, maxdeg, tol3d, tol2d, nbiter); /* user parameters */
277     App.Perform(Line, Section);
278
279     if (!App.IsDone()) return 0;
280     Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
281     App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
282     Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface
283       (App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(),
284        App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
285
286     if (GBS.IsNull()) return 0;
287     aShape = BRepBuilderAPI_MakeFace(GBS);
288   }
289   else {
290     // implemented by skl 20.03.2008 for bug 16568
291     // make approximation - try to create bspline surface
292     // using GeomAPI_PointsToBSplineSurface
293     TColGeom_SequenceOfCurve aSeq;
294     int MaxNbPoles = 0;
295     // add curves from edges to sequence and find maximal
296     // number of poles if some of them are bsplines
297     for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
298       Scurrent = Ex.Current();
299       if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
300       if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
301       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
302       Handle(Geom_TrimmedCurve) TC = Handle(Geom_TrimmedCurve)::DownCast(C);
303       if(TC.IsNull()) {
304         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(C);
305         if(!BC.IsNull()) {
306           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
307         }
308       }
309       else {
310         Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(TC->BasisCurve());
311         if(BC.IsNull()) {
312           Handle(Geom_TrimmedCurve) TC1 = Handle(Geom_TrimmedCurve)::DownCast(TC->BasisCurve());
313           if(!TC1.IsNull()) {
314             BC = Handle(Geom_BSplineCurve)::DownCast(TC1->BasisCurve());
315           }
316         }
317         if(!BC.IsNull()) {
318           MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
319         }
320       }
321       aSeq.Append(C);
322     }
323     // prepare array of points for creation bspline surface
324     // size of this array: by U parameter - number of curves,
325     // by V parameter - determ using MaxNbPoles but it's
326     // value must be between 21(min) and 101(max)
327     int nbc = aSeq.Length();
328     int nbp = Max(21,2*MaxNbPoles-1);
329     if(nbp>101) nbp = 101;
330     TColgp_Array2OfPnt Points(1,nbc,1,nbp);
331     int ic = 1;
332     for(; ic<=nbc; ic++) {
333       Handle(Geom_Curve) C = aSeq.Value(ic);
334       double fp = C->FirstParameter();
335       double lp = C->LastParameter();
336       double dp = (lp-fp)/(nbp-1);
337       int j = 0;
338       gp_Pnt P;
339       for(; j<nbp; j++) {
340         C->D0(fp+dp*j,P);
341         Points.SetValue(ic,j+1,P);
342       }
343     }
344     GeomAPI_PointsToBSplineSurface PTB(Points,mindeg,maxdeg,GeomAbs_C2,tol3d);
345     Handle(Geom_BSplineSurface) BS = PTB.Surface();
346     BRepBuilderAPI_MakeFace BB(BS);
347     TopoDS_Face NewF = BB.Face();
348     Handle(ShapeFix_Face) sff = new ShapeFix_Face(NewF);
349     sff->Perform();
350     sff->FixOrientation();
351     aShape = sff->Face();
352   }
353
354   /* We test the validity of resulting shape */
355   if (!BRepAlgo::IsValid((aShape))) {
356     Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
357     return 0;
358   }
359
360   aFunction->SetValue(aShape);
361
362   log.SetTouched(Label());
363   return 1;
364 }
365
366
367 //=======================================================================
368 //function :  GEOMImpl_FillingDriver_Type_
369 //purpose  :
370 //=======================================================================
371 Standard_EXPORT Handle_Standard_Type& GEOMImpl_FillingDriver_Type_()
372 {
373
374   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
375   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
376   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
377   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
378   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
379   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
380
381
382   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
383   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_FillingDriver",
384                                                          sizeof(GEOMImpl_FillingDriver),
385                                                          1,
386                                                          (Standard_Address)_Ancestors,
387                                                          (Standard_Address)NULL);
388
389   return _aType;
390 }
391
392 //=======================================================================
393 //function : DownCast
394 //purpose  :
395 //=======================================================================
396
397 const Handle(GEOMImpl_FillingDriver) Handle(GEOMImpl_FillingDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
398 {
399   Handle(GEOMImpl_FillingDriver) _anOtherObject;
400
401   if (!AnObject.IsNull()) {
402      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_FillingDriver))) {
403        _anOtherObject = Handle(GEOMImpl_FillingDriver)((Handle(GEOMImpl_FillingDriver)&)AnObject);
404      }
405   }
406
407   return _anOtherObject;
408 }