1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
17 #include <Convert_EllipseToBSplineCurve.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>
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.
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){
59 Handle(TColStd_HArray1OfReal) CosNumeratorPtr,
67 if (Parameterisation != Convert_TgtThetaOver2 &&
68 Parameterisation != Convert_RationalC1) {
69 // If BuildCosAndSin cannot manage the periodicity
71 isperiodic = Standard_False;
72 Convert_ConicToBSplineCurve::
73 BuildCosAndSin(Parameterisation,
83 isperiodic = Standard_True;
84 Convert_ConicToBSplineCurve::
85 BuildCosAndSin(Parameterisation,
94 nbPoles = CosNumeratorPtr->Length();
95 nbKnots = knots->Length();
98 new TColgp_HArray1OfPnt2d(1,nbPoles) ;
101 gp_Dir2d Ox = E.XAxis().Direction();
102 gp_Dir2d Oy = E.YAxis().Direction();
104 Trsf.SetTransformation( E.XAxis(), gp::OX2d());
105 if ( Ox.X() * Oy.Y() - Ox.Y() * Oy.X() > 0.0e0) {
112 // Replace the bspline in the mark of the circle.
113 // and calculate the weight of the bspline.
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);
122 //=======================================================================
123 //function : Convert_EllipseToBSplineCurve
124 //purpose : this constructs a non periodic Ellipse
125 //=======================================================================
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)
135 Standard_Real Tol = Precision::PConfusion();
136 Standard_Real delta = ULast - UFirst;
138 Standard_DomainError_Raise_if( (delta > (2*M_PI+Tol)) || (delta <= 0.0e0),
139 "Convert_EllipseToBSplineCurve");
141 Standard_Real R, r, value;
142 Handle(TColStd_HArray1OfReal) CosNumeratorPtr, SinNumeratorPtr;
147 isperiodic = Standard_False;
148 Convert_ConicToBSplineCurve::BuildCosAndSin(Parameterisation,
158 nbPoles = CosNumeratorPtr->Length();
159 nbKnots = knots->Length();
161 poles = new TColgp_HArray1OfPnt2d(1,nbPoles) ;
163 gp_Dir2d Ox = E.XAxis().Direction();
164 gp_Dir2d Oy = E.YAxis().Direction();
166 Trsf.SetTransformation( E.XAxis(), gp::OX2d());
167 if ( Ox.X() * Oy.Y() - Ox.Y() * Oy.X() > 0.0e0) {
174 // Replace the bspline in the mark of the circle.
175 // and calculate the weight of the bspline.
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);