1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 //JCV 16/10/91
16
17 #include <Convert_CircleToBSplineCurve.hxx>
18 #include <gp.hxx>
19 #include <gp_Ax2d.hxx>
20 #include <gp_Circ2d.hxx>
21 #include <gp_Dir2d.hxx>
22 #include <gp_Trsf2d.hxx>
23 #include <Precision.hxx>
24 #include <Standard_DomainError.hxx>
25 #include <TColgp_Array1OfPnt2d.hxx>
26 #include <TColgp_HArray1OfPnt2d.hxx>
27 #include <TColStd_Array1OfReal.hxx>
28 #include <TColStd_HArray1OfInteger.hxx>
29 #include <TColStd_HArray1OfReal.hxx>
30
31 //Attention :
32 //To avoid use of persistent tables in the fields
33 //the tables are dimensioned to the maximum (TheNbKnots and TheNbPoles)
34 //that correspond to the full circle. For an arc of circle there is a
35 //need of less poles and nodes, that is why the fields
36 //nbKnots and nbPoles are present and updated in the
37 //constructor of an arc of B-spline circle to take into account
38 //the real number of poles and nodes.
39 // parametrization :
40 // Reference : Rational B-spline for Curve and Surface Representation
41 // Wayne Tiller CADG September 1983
42 // x(t) = (1 - t^2) / (1 + t^2)
43 // y(t) = 2 t / (1 + t^2)
44 // then t = Sqrt(2) u / ((Sqrt(2) - 2) u + 2)
45 // => u = 2 t / (Sqrt(2) + (2 - Sqrt(2)) t)
46 //=======================================================================
47 //function : Convert_CircleToBSplineCurve
48 //purpose : this constructs a periodic circle
49 //=======================================================================
Convert_CircleToBSplineCurve(const gp_Circ2d & C,const Convert_ParameterisationType Parameterisation)50 Convert_CircleToBSplineCurve::Convert_CircleToBSplineCurve
51 (const gp_Circ2d& C, const Convert_ParameterisationType Parameterisation)
52 :Convert_ConicToBSplineCurve(0,0,0){
53
54 Standard_Integer ii ;
55
56 Standard_Real R,
57 value ;
58 Handle(TColStd_HArray1OfReal) CosNumeratorPtr,
59 SinNumeratorPtr ;
60
61
62 R = C.Radius() ;
63 if (Parameterisation != Convert_TgtThetaOver2 &&
64 Parameterisation != Convert_RationalC1) {
65 // In case if BuildCosAndSin does not know how to manage the periodicity
66 // => trim on 0,2*PI
67 isperiodic = Standard_False;
68 Convert_ConicToBSplineCurve::
69 BuildCosAndSin(Parameterisation,
70 0, 2*M_PI,
71 CosNumeratorPtr,
72 SinNumeratorPtr,
73 weights,
74 degree,
75 knots,
76 mults);
77 }
78 else {
79 isperiodic = Standard_True;
80 Convert_ConicToBSplineCurve::
81 BuildCosAndSin(Parameterisation,
82 CosNumeratorPtr,
83 SinNumeratorPtr,
84 weights,
85 degree,
86 knots,
87 mults);
88 }
89
90
91 nbPoles = CosNumeratorPtr->Length();
92 nbKnots = knots->Length();
93
94 poles =
95 new TColgp_HArray1OfPnt2d(1,nbPoles);
96
97
98 gp_Dir2d Ox = C.XAxis().Direction();
99 gp_Dir2d Oy = C.YAxis().Direction();
100 gp_Trsf2d Trsf;
101 Trsf.SetTransformation( C.XAxis(), gp::OX2d());
102 if ( Ox.X() * Oy.Y() - Ox.Y() * Oy.X() > 0.0e0) {
103 value = R ;
104 }
105 else {
106 value = -R ;
107 }
108
109 // Replace the bspline in the reference of the circle.
110 // and calculate the weight of the bspline.
111
112 for (ii = 1; ii <= nbPoles ; ii++) {
113 poles->ChangeArray1()(ii).SetCoord(1, R * CosNumeratorPtr->Value(ii)) ;
114 poles->ChangeArray1()(ii).SetCoord(2, value * SinNumeratorPtr->Value(ii)) ;
115 poles->ChangeArray1()(ii).Transform( Trsf);
116 }
117
118 }
119 //=======================================================================
120 //function : Convert_CircleToBSplineCurve
121 //purpose : this constructs a non periodic circle
122 //=======================================================================
123
Convert_CircleToBSplineCurve(const gp_Circ2d & C,const Standard_Real UFirst,const Standard_Real ULast,const Convert_ParameterisationType Parameterisation)124 Convert_CircleToBSplineCurve::Convert_CircleToBSplineCurve
125 (const gp_Circ2d& C,
126 const Standard_Real UFirst,
127 const Standard_Real ULast,
128 const Convert_ParameterisationType Parameterisation)
129 :Convert_ConicToBSplineCurve(0,0,0)
130 {
131 Standard_Real delta = ULast - UFirst ;
132 Standard_Real Eps = Precision::PConfusion();
133
134 if ( (delta > (2*M_PI + Eps)) || (delta <= 0.0e0) ) {
135 throw Standard_DomainError( "Convert_CircleToBSplineCurve");
136 }
137
138 Standard_Integer ii;
139 Standard_Real R, value ;
140 Handle(TColStd_HArray1OfReal) CosNumeratorPtr,SinNumeratorPtr ;
141
142
143 R = C.Radius() ;
144 isperiodic = Standard_False;
145 Convert_ConicToBSplineCurve::BuildCosAndSin(Parameterisation,
146 UFirst,
147 ULast,
148 CosNumeratorPtr,
149 SinNumeratorPtr,
150 weights,
151 degree,
152 knots,
153 mults) ;
154
155
156
157 nbPoles = CosNumeratorPtr->Length();
158 nbKnots = knots->Length();
159
160 poles =
161 new TColgp_HArray1OfPnt2d(1,nbPoles) ;
162
163 gp_Dir2d Ox = C.XAxis().Direction();
164 gp_Dir2d Oy = C.YAxis().Direction();
165 gp_Trsf2d Trsf;
166 Trsf.SetTransformation( C.XAxis(), gp::OX2d());
167 if ( Ox.X() * Oy.Y() - Ox.Y() * Oy.X() > 0.0e0) {
168 value = R ;
169 }
170 else {
171 value = -R ;
172 }
173
174 // Replace the bspline in the reference of the circle.
175 // and calculate the weight of the bspline.
176
177 for (ii = 1; ii <= nbPoles ; ii++) {
178 poles->ChangeArray1()(ii).SetCoord(1, R * CosNumeratorPtr->Value(ii)) ;
179 poles->ChangeArray1()(ii).SetCoord(2, value * SinNumeratorPtr->Value(ii)) ;
180 poles->ChangeArray1()(ii).Transform( Trsf);
181 }
182
183 }
184
185