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