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