0026377: Passing Handle objects as arguments to functions as non-const reference...
[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
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
7fd59977 17
42cf5bc1 18#include <Geom2d_BezierCurve.hxx>
19#include <Geom2d_BSplineCurve.hxx>
20#include <Geom2dConvert_BSplineCurveToBezierCurve.hxx>
21#include <Standard_DimensionError.hxx>
22#include <Standard_DomainError.hxx>
23#include <Standard_OutOfRange.hxx>
7fd59977 24#include <TColgp_Array1OfPnt2d.hxx>
25#include <TColStd_Array1OfReal.hxx>
7fd59977 26
27//=======================================================================
28//function : Geom2dConvert_BSplineCurveToBezierCurve
29//purpose :
30//=======================================================================
7fd59977 31Geom2dConvert_BSplineCurveToBezierCurve::Geom2dConvert_BSplineCurveToBezierCurve (const Handle(Geom2d_BSplineCurve)& BasisCurve)
32{
33 myCurve = Handle(Geom2d_BSplineCurve)::DownCast(BasisCurve->Copy());
34 // periodic curve can't be converted correctly by two main reasons:
35 // last pole (equal to first one) is missing;
36 // poles recomputation using default boor scheme is fails.
37 if(myCurve->IsPeriodic()) myCurve->SetNotPeriodic();
38 Standard_Real Uf = myCurve->FirstParameter();
39 Standard_Real Ul = myCurve->LastParameter();
40 myCurve->Segment(Uf,Ul);
41 myCurve->IncreaseMultiplicity(myCurve->FirstUKnotIndex(),
42 myCurve->LastUKnotIndex(),
43 myCurve->Degree());
44}
45
46
47//=======================================================================
48//function : Geom2dConvert_BSplineCurveToBezierCurve
49//purpose :
50//=======================================================================
51
52Geom2dConvert_BSplineCurveToBezierCurve::Geom2dConvert_BSplineCurveToBezierCurve
53(const Handle(Geom2d_BSplineCurve)& BasisCurve,
54 const Standard_Real U1,
55 const Standard_Real U2,
56 const Standard_Real ParametricTolerance)
57{
58 if (U2 - U1 < ParametricTolerance)
59 Standard_DomainError::Raise("GeomConvert_BSplineCurveToBezierSurface");
60
61 Standard_Real Uf = U1, Ul = U2;
62 Standard_Real PTol = ParametricTolerance/2 ;
63
64 Standard_Integer I1, I2;
65 myCurve = Handle(Geom2d_BSplineCurve)::DownCast(BasisCurve->Copy());
66 if(myCurve->IsPeriodic()) myCurve->SetNotPeriodic();
67
68 myCurve->LocateU(U1, PTol, I1, I2);
69 if (I1==I2) { // On est sur le noeud
70 if ( myCurve->Knot(I1) > U1) Uf = myCurve->Knot(I1);
71 }
72
73 myCurve->LocateU(U2, PTol, I1, I2);
74 if (I1==I2) { // On est sur le noeud
75 if ( myCurve->Knot(I1) < U2) Ul = myCurve->Knot(I1);
76 }
77
78
79 myCurve->Segment(Uf, Ul);
80 myCurve->IncreaseMultiplicity(myCurve->FirstUKnotIndex(),
81 myCurve->LastUKnotIndex(),
82 myCurve->Degree());
83}
84
85
86//=======================================================================
87//function : Arc
88//purpose :
89//=======================================================================
90
91Handle(Geom2d_BezierCurve) Geom2dConvert_BSplineCurveToBezierCurve::Arc
92(const Standard_Integer Index)
93{
94 if ( Index < 1 || Index > myCurve->NbKnots()-1) {
95 Standard_OutOfRange::Raise("Geom2dConvert_BSplineCurveToBezierCurve");
96 }
97 Standard_Integer Deg = myCurve->Degree();
98
99 TColgp_Array1OfPnt2d Poles(1,Deg+1);
100
101 Handle(Geom2d_BezierCurve) C;
102 if ( myCurve->IsRational()) {
103 TColStd_Array1OfReal Weights(1,Deg+1);
104 for ( Standard_Integer i = 1; i <= Deg+1; i++) {
105 Poles(i) = myCurve->Pole(i+Deg*(Index-1));
106 Weights(i) = myCurve->Weight(i+Deg*(Index-1));
107 }
108 C = new Geom2d_BezierCurve(Poles,Weights);
109 }
110 else {
111 for ( Standard_Integer i = 1; i <= Deg+1; i++) {
112 Poles(i) = myCurve->Pole(i+Deg*(Index-1));
113 }
114 C = new Geom2d_BezierCurve(Poles);
115 }
116 return C;
117}
118
119
120//=======================================================================
121//function : Arcs
122//purpose :
123//=======================================================================
124
125void Geom2dConvert_BSplineCurveToBezierCurve::Arcs
126(TColGeom2d_Array1OfBezierCurve& Curves)
127{
128 Standard_Integer n = NbArcs();
129 for ( Standard_Integer i = 1; i <= n; i++) {
130 Curves(i) = Arc(i);
131 }
132}
133
134//=======================================================================
135//function : Knots
136//purpose :
137//=======================================================================
138
139void Geom2dConvert_BSplineCurveToBezierCurve::Knots
140 (TColStd_Array1OfReal& TKnots) const
141{
142 Standard_Integer ii, kk;
143 for (ii = 1, kk = TKnots.Lower();
144 ii <=myCurve->NbKnots(); ii++, kk++)
145 TKnots(kk) = myCurve->Knot(ii);
146}
147
148//=======================================================================
149//function : NbArcs
150//purpose :
151//=======================================================================
152
153Standard_Integer Geom2dConvert_BSplineCurveToBezierCurve::NbArcs() const
154{
155 return (myCurve->NbKnots()-1);
156}