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