0027234: Code duplication: Convert_CompBezierCurvesToBSplineCurve* in ShapeConstruct
[occt.git] / src / Convert / Convert_CompBezierCurves2dToBSplineCurve2d.cxx
1 // Created on: 1993-10-20
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1993-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 under
9 // the terms of the GNU Lesser General Public License 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
18 #include <BSplCLib.hxx>
19 #include <Convert_CompBezierCurves2dToBSplineCurve2d.hxx>
20 #include <gp.hxx>
21 #include <gp_Pnt2d.hxx>
22 #include <gp_Vec2d.hxx>
23 #include <PLib.hxx>
24 #include <Precision.hxx>
25 #include <Standard_ConstructionError.hxx>
26 #include <TColgp_HArray1OfPnt2d.hxx>
27
28 //=======================================================================
29 //function : Convert_CompBezierCurves2dToBSplineCurve2d
30 //purpose  : 
31 //=======================================================================
32 Convert_CompBezierCurves2dToBSplineCurve2d::
33 Convert_CompBezierCurves2dToBSplineCurve2d(
34                   const Standard_Real AngularTolerance) :
35                   myAngular(AngularTolerance),
36                   myDone(Standard_False)
37 {
38 }
39
40
41 //=======================================================================
42 //function : AddCurve
43 //purpose  : 
44 //=======================================================================
45
46 void  Convert_CompBezierCurves2dToBSplineCurve2d::AddCurve
47   (const TColgp_Array1OfPnt2d& Poles)
48 {
49   if ( !mySequence.IsEmpty()) {
50     gp_Pnt2d P1,P2;
51     P1 = mySequence.Last()->Value(mySequence.Last()->Upper());
52     P2 = Poles(Poles.Lower());
53
54 // User defined tolerance NYI
55 //    Standard_ConstructionError_Raise_if
56 //      ( !P1.IsEqual(P2,Precision::Confusion()),
57 //       "Convert_CompBezierCurves2dToBSplineCurve2d::Addcurve");
58
59   }
60   myDone = Standard_False;
61   Handle(TColgp_HArray1OfPnt2d) HPoles = 
62     new TColgp_HArray1OfPnt2d(Poles.Lower(),Poles.Upper());
63   HPoles->ChangeArray1() = Poles;
64   mySequence.Append(HPoles);
65 }
66
67
68 //=======================================================================
69 //function : Degree
70 //purpose  : 
71 //=======================================================================
72
73 Standard_Integer  Convert_CompBezierCurves2dToBSplineCurve2d::Degree()
74 const {
75   return myDegree;
76 }
77
78
79 //=======================================================================
80 //function : NbPoles
81 //purpose  : 
82 //=======================================================================
83
84 Standard_Integer  Convert_CompBezierCurves2dToBSplineCurve2d::NbPoles()
85 const {
86   return CurvePoles.Length();
87 }
88
89
90 //=======================================================================
91 //function : Poles
92 //purpose  : 
93 //=======================================================================
94
95 void  Convert_CompBezierCurves2dToBSplineCurve2d::Poles
96   (TColgp_Array1OfPnt2d& Poles) const
97 {
98   Standard_Integer i, Lower = Poles.Lower(), Upper = Poles.Upper();
99   Standard_Integer k = 1;
100   for (i = Lower; i <= Upper; i++) {
101     Poles(i) = CurvePoles(k++);
102   }
103 }
104
105
106 //=======================================================================
107 //function : NbKnots
108 //purpose  : 
109 //=======================================================================
110
111 Standard_Integer  Convert_CompBezierCurves2dToBSplineCurve2d::NbKnots()
112 const {
113   return CurveKnots.Length();
114 }
115
116
117 //=======================================================================
118 //function : KnotsAndMults
119 //purpose  : 
120 //=======================================================================
121
122 void  Convert_CompBezierCurves2dToBSplineCurve2d::KnotsAndMults
123   (TColStd_Array1OfReal&    Knots, 
124    TColStd_Array1OfInteger& Mults ) const
125 {
126   Standard_Integer i, LowerK = Knots.Lower(), UpperK = Knots.Upper();
127   Standard_Integer LowerM = Mults.Lower(), UpperM = Mults.Upper();
128   Standard_Integer k = 1;
129   for (i = LowerK; i <= UpperK; i++) {
130     Knots(i) = CurveKnots(k++);
131   }
132   k = 1;
133   for (i = LowerM; i <= UpperM; i++) {
134     Mults(i) = KnotsMultiplicities(k++);
135   }
136 }
137
138
139
140
141
142 //=======================================================================
143 //function : Perform
144 //purpose  : 
145 //=======================================================================
146
147 void Convert_CompBezierCurves2dToBSplineCurve2d::Perform() 
148 {
149   myDone = Standard_True;
150   CurvePoles.Clear();
151   CurveKnots.Clear();
152   KnotsMultiplicities.Clear();
153   Standard_Integer LowerI     = 1;
154   Standard_Integer UpperI     = mySequence.Length();
155   Standard_Integer NbrCurv    = UpperI-LowerI+1;
156 //  Standard_Integer NbKnotsSpl = NbrCurv + 1 ;
157   TColStd_Array1OfReal     CurveKnVals         (1,NbrCurv);
158
159   Standard_Integer i;
160   myDegree = 0;
161   for ( i = 1; i <= mySequence.Length(); i++) {
162     myDegree = Max( myDegree, (mySequence(i))->Length() -1);
163   }
164
165   Standard_Real Det=0;
166   gp_Pnt2d P1, P2, P3;
167   Standard_Integer Deg, Inc, MaxDegree = myDegree;
168   TColgp_Array1OfPnt2d Points(1, myDegree+1);
169
170   for (i = LowerI ; i <= UpperI ; i++) {
171     // 1- Rise Bezier curve to the maximum degree.
172     Deg = mySequence(i)->Length()-1;
173     Inc = myDegree - Deg;
174     if ( Inc > 0) {
175       BSplCLib::IncreaseDegree(myDegree, 
176                                mySequence(i)->Array1(), BSplCLib::NoWeights(),
177                                Points, BSplCLib::NoWeights());
178     }
179     else {
180       Points = mySequence(i)->Array1();
181     }
182
183     // 2- Process the node of junction between Bezier curves.
184     if (i == LowerI) {
185       // Processing of initial node of the BSpline.
186       for (Standard_Integer j = 1 ; j <= MaxDegree ; j++) {
187         CurvePoles.Append(Points(j));
188       }
189       CurveKnVals(1)         = 1.; // To begin the series.
190       KnotsMultiplicities.Append(MaxDegree+1);
191       Det = 1.;
192     }
193
194
195     if (i != LowerI) {
196       P2 = Points(1);
197       P3 = Points(2);
198       gp_Vec2d V1(P1, P2), V2(P2, P3);
199
200       // Processing of the tangency between the Bezier and the previous.
201       // This allows guaranteeing at least continuity C1 if the tangents are coherent.
202       // Test of angle at myAngular
203       Standard_Real D1 = V1.SquareMagnitude();
204       Standard_Real D2 = V2.SquareMagnitude();
205       if (MaxDegree > 1 && //rln 20.06.99 work-around
206           D1 > gp::Resolution() && D2 > gp::Resolution() && V1.IsParallel(V2, myAngular ))
207       {
208         Standard_Real Lambda = Sqrt(D2/D1);
209         KnotsMultiplicities.Append(MaxDegree - 1);
210         CurveKnVals(i) = CurveKnVals(i - 1) * Lambda;
211       }
212       else {
213         CurvePoles.Append(Points(1));
214         KnotsMultiplicities.Append(MaxDegree);
215         CurveKnVals(i) = 1.0;
216       }
217       Det += CurveKnVals(i);
218
219       // Store poles.
220       for (Standard_Integer j = 2 ; j <= MaxDegree ; j++) {
221         CurvePoles.Append(Points(j));
222       }
223
224     }
225
226
227     if (i == UpperI) {
228       // Process end node of the BSpline.
229       CurvePoles.Append(Points(MaxDegree+1));
230       KnotsMultiplicities.Append(MaxDegree+1);
231     }
232     P1 = Points(MaxDegree);
233   }
234
235   // Correct nodal values to make them variable within [0.,1.].
236   CurveKnots.Append(0.0);
237   for (i = 2 ; i <= NbrCurv ; i++) {
238     CurveKnots.Append(CurveKnots(i-1) + (CurveKnVals(i-1)/Det));
239   }
240   CurveKnots.Append(1.0);
241 }
242
243