0024166: Unable to create file with "Save" menu of voxeldemo Qt sample
[occt.git] / src / Geom2dConvert / Geom2dConvert_BSplineCurveToBezierCurve.cxx
CommitLineData
b311480e 1// Created on: 1996-03-12
2// Created by: Bruno DUMORTIER
3// Copyright (c) 1995-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
5//
6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
7fd59977 21
22
23#include <Geom2dConvert_BSplineCurveToBezierCurve.ixx>
24
25#include <TColgp_Array1OfPnt2d.hxx>
26#include <TColStd_Array1OfReal.hxx>
27
28
29//=======================================================================
30//function : Geom2dConvert_BSplineCurveToBezierCurve
31//purpose :
32//=======================================================================
33
34Geom2dConvert_BSplineCurveToBezierCurve::Geom2dConvert_BSplineCurveToBezierCurve (const Handle(Geom2d_BSplineCurve)& BasisCurve)
35{
36 myCurve = Handle(Geom2d_BSplineCurve)::DownCast(BasisCurve->Copy());
37 // periodic curve can't be converted correctly by two main reasons:
38 // last pole (equal to first one) is missing;
39 // poles recomputation using default boor scheme is fails.
40 if(myCurve->IsPeriodic()) myCurve->SetNotPeriodic();
41 Standard_Real Uf = myCurve->FirstParameter();
42 Standard_Real Ul = myCurve->LastParameter();
43 myCurve->Segment(Uf,Ul);
44 myCurve->IncreaseMultiplicity(myCurve->FirstUKnotIndex(),
45 myCurve->LastUKnotIndex(),
46 myCurve->Degree());
47}
48
49
50//=======================================================================
51//function : Geom2dConvert_BSplineCurveToBezierCurve
52//purpose :
53//=======================================================================
54
55Geom2dConvert_BSplineCurveToBezierCurve::Geom2dConvert_BSplineCurveToBezierCurve
56(const Handle(Geom2d_BSplineCurve)& BasisCurve,
57 const Standard_Real U1,
58 const Standard_Real U2,
59 const Standard_Real ParametricTolerance)
60{
61 if (U2 - U1 < ParametricTolerance)
62 Standard_DomainError::Raise("GeomConvert_BSplineCurveToBezierSurface");
63
64 Standard_Real Uf = U1, Ul = U2;
65 Standard_Real PTol = ParametricTolerance/2 ;
66
67 Standard_Integer I1, I2;
68 myCurve = Handle(Geom2d_BSplineCurve)::DownCast(BasisCurve->Copy());
69 if(myCurve->IsPeriodic()) myCurve->SetNotPeriodic();
70
71 myCurve->LocateU(U1, PTol, I1, I2);
72 if (I1==I2) { // On est sur le noeud
73 if ( myCurve->Knot(I1) > U1) Uf = myCurve->Knot(I1);
74 }
75
76 myCurve->LocateU(U2, PTol, I1, I2);
77 if (I1==I2) { // On est sur le noeud
78 if ( myCurve->Knot(I1) < U2) Ul = myCurve->Knot(I1);
79 }
80
81
82 myCurve->Segment(Uf, Ul);
83 myCurve->IncreaseMultiplicity(myCurve->FirstUKnotIndex(),
84 myCurve->LastUKnotIndex(),
85 myCurve->Degree());
86}
87
88
89//=======================================================================
90//function : Arc
91//purpose :
92//=======================================================================
93
94Handle(Geom2d_BezierCurve) Geom2dConvert_BSplineCurveToBezierCurve::Arc
95(const Standard_Integer Index)
96{
97 if ( Index < 1 || Index > myCurve->NbKnots()-1) {
98 Standard_OutOfRange::Raise("Geom2dConvert_BSplineCurveToBezierCurve");
99 }
100 Standard_Integer Deg = myCurve->Degree();
101
102 TColgp_Array1OfPnt2d Poles(1,Deg+1);
103
104 Handle(Geom2d_BezierCurve) C;
105 if ( myCurve->IsRational()) {
106 TColStd_Array1OfReal Weights(1,Deg+1);
107 for ( Standard_Integer i = 1; i <= Deg+1; i++) {
108 Poles(i) = myCurve->Pole(i+Deg*(Index-1));
109 Weights(i) = myCurve->Weight(i+Deg*(Index-1));
110 }
111 C = new Geom2d_BezierCurve(Poles,Weights);
112 }
113 else {
114 for ( Standard_Integer i = 1; i <= Deg+1; i++) {
115 Poles(i) = myCurve->Pole(i+Deg*(Index-1));
116 }
117 C = new Geom2d_BezierCurve(Poles);
118 }
119 return C;
120}
121
122
123//=======================================================================
124//function : Arcs
125//purpose :
126//=======================================================================
127
128void Geom2dConvert_BSplineCurveToBezierCurve::Arcs
129(TColGeom2d_Array1OfBezierCurve& Curves)
130{
131 Standard_Integer n = NbArcs();
132 for ( Standard_Integer i = 1; i <= n; i++) {
133 Curves(i) = Arc(i);
134 }
135}
136
137//=======================================================================
138//function : Knots
139//purpose :
140//=======================================================================
141
142void Geom2dConvert_BSplineCurveToBezierCurve::Knots
143 (TColStd_Array1OfReal& TKnots) const
144{
145 Standard_Integer ii, kk;
146 for (ii = 1, kk = TKnots.Lower();
147 ii <=myCurve->NbKnots(); ii++, kk++)
148 TKnots(kk) = myCurve->Knot(ii);
149}
150
151//=======================================================================
152//function : NbArcs
153//purpose :
154//=======================================================================
155
156Standard_Integer Geom2dConvert_BSplineCurveToBezierCurve::NbArcs() const
157{
158 return (myCurve->NbKnots()-1);
159}