0024428: Implementation of LGPL license
[occt.git] / src / BRepFill / BRepFill_ApproxSeewing.cxx
1 // Created on: 1995-09-22
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and / or modify it
9 // under the terms of the GNU Lesser General Public version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <BRepFill_ApproxSeewing.ixx>
18
19 #include <AppParCurves_MultiCurve.hxx>
20 #include <AppDef_Compute.hxx>
21 #include <AppDef_MultiLine.hxx>
22 #include <AppDef_MultiPointConstraint.hxx>
23 #include <BSplCLib.hxx>
24 #include <PLib.hxx>
25 #include <Geom_BSplineCurve.hxx>
26 #include <Geom2d_BSplineCurve.hxx>
27 #include <TColgp_Array1OfPnt.hxx>
28 #include <TColgp_Array1OfPnt2d.hxx>
29 #include <TColStd_Array1OfReal.hxx>
30 #include <TColStd_Array1OfInteger.hxx>
31
32
33 //=======================================================================
34 //function : BRepFill_ApproxSeewing
35 //purpose  : 
36 //=======================================================================
37
38 BRepFill_ApproxSeewing::BRepFill_ApproxSeewing() 
39 :myIsDone(Standard_False)
40 {
41 }
42
43
44 //=======================================================================
45 //function : BRepFill_ApproxSeewing
46 //purpose  : 
47 //=======================================================================
48
49 BRepFill_ApproxSeewing::BRepFill_ApproxSeewing(const BRepFill_MultiLine& ML) 
50 :myIsDone(Standard_False)
51 {
52   Perform(ML);
53 }
54
55
56 //=======================================================================
57 //function : Perform
58 //purpose  : 
59 //=======================================================================
60
61 void BRepFill_ApproxSeewing::Perform(const BRepFill_MultiLine& ML)
62 {
63   myML = ML;
64
65   // evaluate the approximative length of the 3dCurve
66   Standard_Integer i;
67   Standard_Real Length = 0.;
68   Standard_Real U1 = myML.FirstParameter();
69   Standard_Real U2 = myML.LastParameter();
70   Standard_Integer NbPoints = 50;
71   Standard_Real Dist, dU = (U2 - U1) / ( 2*NbPoints - 1);
72
73   TColgp_Array1OfPnt2d LP(1,2*NbPoints); // tableau Longueur <-> Param 
74   gp_Pnt P1, P2;
75   P1 = myML.Value(U1);
76
77   for ( i = 0; i < 2*NbPoints ; i++) {
78     P2      = myML.Value(U1 + i*dU);
79     Dist    = P1.Distance(P2);
80     Length += Dist;
81     LP(i+1) = gp_Pnt2d( Length, U1 + (i*dU));
82     P1      = P2;
83   }
84
85   // On cherche a mettre NbPoints dans la curve.
86   // on met les points environ a Length/NbPoints.
87   
88   AppDef_MultiLine            MLS ( NbPoints);
89   AppDef_MultiPointConstraint MP  ( 1, 2);
90   gp_Pnt                      P3d;
91   gp_Pnt2d                    PF1,PF2;
92
93   ML.Value3dOnF1OnF2(U1,P3d,PF1,PF2);
94   MP.SetPoint  (1, P3d);
95   MP.SetPoint2d(2, PF1);
96   MP.SetPoint2d(3, PF2);
97   MLS.SetValue (1, MP);
98   
99   Standard_Real DCorde = Length / ( NbPoints - 1); 
100   Standard_Real Corde  = DCorde;
101   Standard_Integer Index = 1;
102   Standard_Real U, Alpha;
103   for ( i = 2; i < NbPoints; i++) {
104     while ( LP(Index).X() < Corde) Index ++;
105     Alpha = (Corde - LP(Index-1).X()) / (LP(Index).X() - LP(Index-1).X());
106     U = LP(Index-1).Y() + Alpha * ( LP(Index).Y() - LP(Index-1).Y());
107     AppDef_MultiPointConstraint MPC( 1, 2);
108     ML.Value3dOnF1OnF2(U,P3d,PF1,PF2);
109     MPC.SetPoint  (1, P3d);
110     MPC.SetPoint2d(2, PF1);
111     MPC.SetPoint2d(3, PF2);
112     MLS.SetValue  (i, MPC);
113     Corde = i*DCorde;
114   }
115   AppDef_MultiPointConstraint MPE( 1, 2);
116   ML.Value3dOnF1OnF2(U2,P3d,PF1,PF2);
117   MPE.SetPoint  (1, P3d);
118   MPE.SetPoint2d(2, PF1);
119   MPE.SetPoint2d(3, PF2);
120   MLS.SetValue (NbPoints, MPE);
121   
122   AppDef_Compute Fit(MLS);
123
124   Standard_Integer NbCurves = Fit.NbMultiCurves();
125 //  Standard_Integer MaxDeg = 0;
126   
127   if ( NbCurves == 0) {
128 #ifdef DEBUG_EVOLVED
129     cout << " TrimSurfaceTool : Approx echoue, on met les polygones" << endl;
130 #endif
131
132     TColStd_Array1OfReal    Knots(1,NbPoints);
133     TColStd_Array1OfInteger Mults(1,NbPoints);
134     Mults.Init(1);
135     Mults(1) = Mults(NbPoints) =  2;
136     TColgp_Array1OfPnt   P (1,NbPoints);
137     TColgp_Array1OfPnt2d P1(1,NbPoints);
138     TColgp_Array1OfPnt2d P2(1,NbPoints);
139     
140     Standard_Real Uf = ML.FirstParameter();
141     Standard_Real Ul = ML.LastParameter();
142     Standard_Real dU = (Ul-Uf)/(NbPoints-1);
143     AppDef_MultiPointConstraint MPC;
144     for ( i = 1; i<= NbPoints-1; i++) {
145       MPC = MLS.Value(i);
146       U = Uf + (i-1) * dU;
147       P (i) = MPC.Point(1);
148       P1(i) = MPC.Point2d(2);
149       P2(i) = MPC.Point2d(3);
150       Knots(i) = U;
151     }
152     // eval the last point on Ul
153     MPC = MLS.Value(NbPoints);
154     P (NbPoints)    = MPC.Point(1);
155     P1(NbPoints)    = MPC.Point2d(2);
156     P2(NbPoints)    = MPC.Point2d(3);
157     Knots(NbPoints) = Ul;
158     
159     myCurve   = new Geom_BSplineCurve  ( P , Knots, Mults, 1);
160     myPCurve1 = new Geom2d_BSplineCurve( P1, Knots, Mults, 1);
161     myPCurve2 = new Geom2d_BSplineCurve( P2, Knots, Mults, 1);
162     
163     myIsDone  = Standard_True;
164     
165     return;
166   }
167   
168   // Les approx sont a priori OK.
169   
170   const AppParCurves_MultiBSpCurve& MBSp = 
171     Fit.SplineValue();
172   Standard_Integer NbPoles = MBSp.NbPoles();
173   TColgp_Array1OfPnt   Poles   (1 , NbPoles);
174   TColgp_Array1OfPnt2d Poles2d1(1 , NbPoles);
175   TColgp_Array1OfPnt2d Poles2d2(1 , NbPoles);
176   
177   MBSp.Curve(1, Poles);
178   MBSp.Curve(2, Poles2d1);
179   MBSp.Curve(3, Poles2d2);
180   
181   const TColStd_Array1OfReal&    Knots  = MBSp.Knots();
182   const TColStd_Array1OfInteger& Mults  = MBSp.Multiplicities();
183   Standard_Integer               Degree = MBSp.Degree();
184
185   myCurve   = new Geom_BSplineCurve  (Poles,   Knots,Mults,Degree);
186   myPCurve1 = new Geom2d_BSplineCurve(Poles2d1,Knots,Mults,Degree);
187   myPCurve2 = new Geom2d_BSplineCurve(Poles2d2,Knots,Mults,Degree);
188   
189   myIsDone = Standard_True;
190 }
191
192
193 //=======================================================================
194 //function : IsDone
195 //purpose  : 
196 //=======================================================================
197
198 Standard_Boolean BRepFill_ApproxSeewing::IsDone() const 
199 {
200   return myIsDone;
201 }
202
203
204 //=======================================================================
205 //function : Handle_Geom_Curve&
206 //purpose  : 
207 //=======================================================================
208
209 const Handle(Geom_Curve)& BRepFill_ApproxSeewing::Curve() const 
210 {
211   StdFail_NotDone_Raise_if( !myIsDone,
212                             "BRepFill_ApproxSeewing::Curve");
213   return myCurve;
214 }
215
216
217 //=======================================================================
218 //function : Handle_Geom2d_Curve&
219 //purpose  : 
220 //=======================================================================
221
222 const Handle(Geom2d_Curve)& BRepFill_ApproxSeewing::CurveOnF1() const 
223 {
224   StdFail_NotDone_Raise_if( !myIsDone,
225                             "BRepFill_ApproxSeewing::CurveOnF1");
226   return myPCurve1;
227 }
228
229
230 //=======================================================================
231 //function : Handle_Geom2d_Curve&
232 //purpose  : 
233 //=======================================================================
234
235 const Handle(Geom2d_Curve)& BRepFill_ApproxSeewing::CurveOnF2() const 
236 {
237   StdFail_NotDone_Raise_if( !myIsDone,
238                             "BRepFill_ApproxSeewing::CurveOnF2");
239   return myPCurve2;
240 }
241
242