0022792: Globally defined symbol PI conflicts with VTK definition (Intel compiler)
[occt.git] / src / Convert / Convert_EllipseToBSplineCurve.cxx
1 //File Convert_EllipseToBSplineCurve.cxx
2 //JCV 16/10/91
3
4
5 #include <Convert_EllipseToBSplineCurve.ixx>
6
7 #include <TColgp_HArray1OfPnt2d.hxx>
8 #include <TColStd_HArray1OfReal.hxx>
9 #include <TColStd_HArray1OfInteger.hxx>
10 #include <TColStd_Array1OfReal.hxx>
11 #include <TColgp_Array1OfPnt2d.hxx>
12
13 #include <gp.hxx>
14 #include <gp_Ax2d.hxx>
15 #include <gp_Dir2d.hxx>
16 #include <gp_Trsf2d.hxx>
17
18 #include <Precision.hxx>
19
20 //Attention :
21 //To avoid use of persistent tables in the fields
22 //the tables are dimensioned to the maximum (TheNbKnots and TheNbPoles)
23 //that correspond to the full circle. For an arc of circle there is a
24 //need of less poles and nodes, that is why the fields
25 //nbKnots and nbPoles are present and updated in the 
26 //constructor of an arc of B-spline circle to take into account 
27 //the real number of poles and nodes.
28
29
30 // parameterization :
31 // Reference : Rational B-spline for Curve and Surface Representation
32 //             Wayne Tiller  CADG September 1983
33 //
34 // x(t) = (1 - t^2) / (1 + t^2)
35 // y(t) =  2 t / (1 + t^2)
36 //
37 // then t = Sqrt(2) u /  ((Sqrt(2) - 2) u + 2)
38 //
39 // => u = 2 t / (Sqrt(2) + (2 - Sqrt(2)) t)
40
41 //=======================================================================
42 //function : Convert_EllipseToBSplineCurve
43 //purpose  : this constructs a periodic Ellipse 
44 //=======================================================================
45
46 Convert_EllipseToBSplineCurve::Convert_EllipseToBSplineCurve 
47   (const gp_Elips2d& E, const Convert_ParameterisationType Parameterisation)
48 :Convert_ConicToBSplineCurve(0,0,0){
49
50   Standard_Integer ii ;
51
52   Standard_Real R,
53   r,
54   value ;
55   Handle(TColStd_HArray1OfReal) CosNumeratorPtr,
56   SinNumeratorPtr ;
57
58   
59   R = E.MajorRadius();
60   r = E.MinorRadius();
61
62
63   if (Parameterisation != Convert_TgtThetaOver2 &&
64     Parameterisation != Convert_RationalC1) {
65     // If BuildCosAndSin cannot 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   nbPoles = CosNumeratorPtr->Length();
91   nbKnots = knots->Length();
92
93   poles = 
94     new TColgp_HArray1OfPnt2d(1,nbPoles)   ;
95
96
97   gp_Dir2d Ox = E.XAxis().Direction();
98   gp_Dir2d Oy = E.YAxis().Direction();
99   gp_Trsf2d Trsf;
100   Trsf.SetTransformation( E.XAxis(), gp::OX2d());
101   if  ( Ox.X() * Oy.Y() - Ox.Y() * Oy.X() > 0.0e0) {
102     value = r ;
103   }
104   else {
105     value = -r ;
106    }
107   
108   // Replace the bspline in the mark of the circle.
109   // and calculate the weight of the bspline.
110
111   for (ii = 1; ii <= nbPoles ; ii++) {
112      poles->ChangeArray1()(ii).SetCoord(1, R * CosNumeratorPtr->Value(ii)) ;
113      poles->ChangeArray1()(ii).SetCoord(2, value * SinNumeratorPtr->Value(ii)) ;
114      poles->ChangeArray1()(ii).Transform( Trsf); 
115    }
116
117 }
118 //=======================================================================
119 //function : Convert_EllipseToBSplineCurve
120 //purpose  : this constructs a non periodic Ellipse
121 //=======================================================================
122
123 Convert_EllipseToBSplineCurve::Convert_EllipseToBSplineCurve 
124   (const gp_Elips2d& E, 
125    const Standard_Real  UFirst,
126    const Standard_Real  ULast,
127    const Convert_ParameterisationType Parameterisation)
128 :Convert_ConicToBSplineCurve(0,0,0)
129 {
130 #ifndef No_Exception
131   Standard_Real Tol = Precision::PConfusion();
132   Standard_Real delta = ULast - UFirst;
133 #endif
134   Standard_DomainError_Raise_if( (delta > (2*M_PI+Tol)) || (delta <= 0.0e0),
135                                 "Convert_EllipseToBSplineCurve");
136   Standard_Integer ii;
137   Standard_Real R, r, value;
138   Handle(TColStd_HArray1OfReal) CosNumeratorPtr, SinNumeratorPtr;
139   
140   R = E.MajorRadius();
141   r = E.MinorRadius();
142
143   isperiodic = Standard_False;
144   Convert_ConicToBSplineCurve::BuildCosAndSin(Parameterisation,
145                                               UFirst,
146                                               ULast,
147                                               CosNumeratorPtr,
148                                               SinNumeratorPtr,
149                                               weights,
150                                               degree,
151                                               knots,
152                                               mults) ;
153
154   nbPoles = CosNumeratorPtr->Length();
155   nbKnots = knots->Length();
156
157   poles = new TColgp_HArray1OfPnt2d(1,nbPoles)   ;
158
159   gp_Dir2d Ox = E.XAxis().Direction();
160   gp_Dir2d Oy = E.YAxis().Direction();
161   gp_Trsf2d Trsf;
162   Trsf.SetTransformation( E.XAxis(), gp::OX2d());
163   if  ( Ox.X() * Oy.Y() - Ox.Y() * Oy.X() > 0.0e0) {
164     value = r ;
165   }
166   else {
167     value = -r ;
168   }
169   
170   // Replace the bspline in the mark of the circle.
171   // and calculate the weight of the bspline.
172   
173   for (ii = 1; ii <= nbPoles ; ii++) {
174     poles->ChangeArray1()(ii).SetCoord(1, R * CosNumeratorPtr->Value(ii)) ;
175     poles->ChangeArray1()(ii).SetCoord(2, value * SinNumeratorPtr->Value(ii)) ;
176     poles->ChangeArray1()(ii).Transform( Trsf); 
177   }
178   
179 }
180