0026937: Eliminate NO_CXX_EXCEPTION macro support
[occt.git] / src / Convert / Convert_EllipseToBSplineCurve.cxx
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_EllipseToBSplineCurve.hxx>
18 #include <gp.hxx>
19 #include <gp_Ax2d.hxx>
20 #include <gp_Dir2d.hxx>
21 #include <gp_Elips2d.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 // parameterization :
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_EllipseToBSplineCurve
48 //purpose  : this constructs a periodic Ellipse 
49 //=======================================================================
50 Convert_EllipseToBSplineCurve::Convert_EllipseToBSplineCurve 
51   (const gp_Elips2d& E, const Convert_ParameterisationType Parameterisation)
52 :Convert_ConicToBSplineCurve(0,0,0){
53
54   Standard_Integer ii ;
55
56   Standard_Real R,
57   r,
58   value ;
59   Handle(TColStd_HArray1OfReal) CosNumeratorPtr,
60   SinNumeratorPtr ;
61
62   
63   R = E.MajorRadius();
64   r = E.MinorRadius();
65
66
67   if (Parameterisation != Convert_TgtThetaOver2 &&
68     Parameterisation != Convert_RationalC1) {
69     // If BuildCosAndSin cannot manage the periodicity
70     // => trim on 0,2*PI
71     isperiodic = Standard_False;
72     Convert_ConicToBSplineCurve::
73       BuildCosAndSin(Parameterisation,
74                      0, 2*M_PI,
75                      CosNumeratorPtr,
76                      SinNumeratorPtr,
77                      weights,
78                      degree,
79                      knots,
80                      mults) ;
81       }   
82   else {
83     isperiodic = Standard_True;
84     Convert_ConicToBSplineCurve::
85       BuildCosAndSin(Parameterisation,
86                      CosNumeratorPtr,
87                      SinNumeratorPtr,
88                      weights,
89                      degree,
90                      knots,
91                      mults);
92   }
93
94   nbPoles = CosNumeratorPtr->Length();
95   nbKnots = knots->Length();
96
97   poles = 
98     new TColgp_HArray1OfPnt2d(1,nbPoles)   ;
99
100
101   gp_Dir2d Ox = E.XAxis().Direction();
102   gp_Dir2d Oy = E.YAxis().Direction();
103   gp_Trsf2d Trsf;
104   Trsf.SetTransformation( E.XAxis(), gp::OX2d());
105   if  ( Ox.X() * Oy.Y() - Ox.Y() * Oy.X() > 0.0e0) {
106     value = r ;
107   }
108   else {
109     value = -r ;
110    }
111   
112   // Replace the bspline in the mark of the circle.
113   // and calculate the weight of the bspline.
114
115   for (ii = 1; ii <= nbPoles ; ii++) {
116      poles->ChangeArray1()(ii).SetCoord(1, R * CosNumeratorPtr->Value(ii)) ;
117      poles->ChangeArray1()(ii).SetCoord(2, value * SinNumeratorPtr->Value(ii)) ;
118      poles->ChangeArray1()(ii).Transform( Trsf); 
119    }
120
121 }
122 //=======================================================================
123 //function : Convert_EllipseToBSplineCurve
124 //purpose  : this constructs a non periodic Ellipse
125 //=======================================================================
126
127 Convert_EllipseToBSplineCurve::Convert_EllipseToBSplineCurve 
128   (const gp_Elips2d& E, 
129    const Standard_Real  UFirst,
130    const Standard_Real  ULast,
131    const Convert_ParameterisationType Parameterisation)
132 :Convert_ConicToBSplineCurve(0,0,0)
133 {
134 #ifndef No_Exception
135   Standard_Real Tol = Precision::PConfusion();
136   Standard_Real delta = ULast - UFirst;
137 #endif
138   Standard_DomainError_Raise_if( (delta > (2*M_PI+Tol)) || (delta <= 0.0e0),
139                                 "Convert_EllipseToBSplineCurve");
140   Standard_Integer ii;
141   Standard_Real R, r, value;
142   Handle(TColStd_HArray1OfReal) CosNumeratorPtr, SinNumeratorPtr;
143   
144   R = E.MajorRadius();
145   r = E.MinorRadius();
146
147   isperiodic = Standard_False;
148   Convert_ConicToBSplineCurve::BuildCosAndSin(Parameterisation,
149                                               UFirst,
150                                               ULast,
151                                               CosNumeratorPtr,
152                                               SinNumeratorPtr,
153                                               weights,
154                                               degree,
155                                               knots,
156                                               mults) ;
157
158   nbPoles = CosNumeratorPtr->Length();
159   nbKnots = knots->Length();
160
161   poles = new TColgp_HArray1OfPnt2d(1,nbPoles)   ;
162
163   gp_Dir2d Ox = E.XAxis().Direction();
164   gp_Dir2d Oy = E.YAxis().Direction();
165   gp_Trsf2d Trsf;
166   Trsf.SetTransformation( E.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 mark 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