0026377: Passing Handle objects as arguments to functions as non-const reference...
[occt.git] / src / Geom2dConvert / Geom2dConvert_CompCurveToBSplineCurve.cxx
CommitLineData
b311480e 1// Created on: 1997-04-29
2// Created by: Stagiaire Francois DUMONT
3// Copyright (c) 1997-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.
b311480e 16
7fd59977 17
42cf5bc1 18#include <Geom2d_BoundedCurve.hxx>
7fd59977 19#include <Geom2d_BSplineCurve.hxx>
20#include <Geom2dConvert.hxx>
42cf5bc1 21#include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
7fd59977 22#include <gp_Pnt2d.hxx>
42cf5bc1 23#include <gp_Vec2d.hxx>
7fd59977 24#include <Precision.hxx>
42cf5bc1 25#include <TColgp_Array1OfPnt2d.hxx>
26#include <TColStd_Array1OfInteger.hxx>
27#include <TColStd_Array1OfReal.hxx>
7fd59977 28
b514beda 29//=======================================================================
30//function : constructor
31//purpose :
32//=======================================================================
33Geom2dConvert_CompCurveToBSplineCurve::Geom2dConvert_CompCurveToBSplineCurve (const Convert_ParameterisationType theParameterisation)
34: myTol (Precision::Confusion()),
35 myType (theParameterisation)
36{
37 //
38}
7fd59977 39
b514beda 40//=======================================================================
41//function : constructor
42//purpose :
43//=======================================================================
7fd59977 44Geom2dConvert_CompCurveToBSplineCurve::
45Geom2dConvert_CompCurveToBSplineCurve(const Handle(Geom2d_BoundedCurve)& BasisCurve,
46 const Convert_ParameterisationType Parameterisation) :
47 myTol(Precision::Confusion()),
48 myType(Parameterisation)
49{
50 Handle(Geom2d_BSplineCurve) Bs =
51 Handle(Geom2d_BSplineCurve)::DownCast(BasisCurve);
52 if (!Bs.IsNull()) {
53 myCurve = Handle(Geom2d_BSplineCurve)::DownCast(BasisCurve->Copy());
54 }
55 else {
56 myCurve = Geom2dConvert::CurveToBSplineCurve (BasisCurve, myType);
57 }
58}
59
60//=======================================================================
61//function : Add
b514beda 62//purpose :
7fd59977 63//=======================================================================
64
65Standard_Boolean Geom2dConvert_CompCurveToBSplineCurve::
66Add(const Handle(Geom2d_BoundedCurve)& NewCurve,
67 const Standard_Real Tolerance,
68 const Standard_Boolean After)
69{
b514beda 70 // conversion
71 Handle(Geom2d_BSplineCurve) Bs = Handle(Geom2d_BSplineCurve)::DownCast (NewCurve);
72 if (!Bs.IsNull())
73 {
74 Bs = Handle(Geom2d_BSplineCurve)::DownCast (NewCurve->Copy());
7fd59977 75 }
b514beda 76 else
77 {
7fd59977 78 Bs = Geom2dConvert::CurveToBSplineCurve (NewCurve, myType);
79 }
b514beda 80 if (myCurve.IsNull())
81 {
82 myCurve = Bs;
83 return Standard_True;
84 }
85
86 myTol = Tolerance;
7fd59977 87
88 Standard_Integer LBs = Bs->NbPoles(), LCb = myCurve->NbPoles();
89
90 // myCurve est elle fermee ?
91 if (myCurve->Pole(LCb).Distance(myCurve->Pole(1))< myTol){
92 if(After){
93 // Ajout Apres ?
94 if (myCurve->Pole(LCb).Distance(Bs->Pole(LBs)) < myTol) {Bs->Reverse();}
95 if (myCurve->Pole(LCb).Distance(Bs->Pole(1)) < myTol) {
96 Add(myCurve, Bs, Standard_True);
97 return Standard_True;
98 }
99 }
100 else{
101 // Ajout avant ?
102 if (myCurve->Pole(1).Distance(Bs->Pole(1)) < myTol) {Bs->Reverse();}
103 if (myCurve->Pole(1).Distance(Bs->Pole(LBs)) < myTol) {
104 Add(Bs, myCurve, Standard_False);
105 return Standard_True;
106 }
107 }
108 }
109 // Ajout Apres ?
110 else {
111
112 Standard_Real d1 = myCurve->Pole(LCb).Distance(Bs->Pole(1));
113 Standard_Real d2 = myCurve->Pole(LCb).Distance(Bs->Pole(LBs));
114 if (( d1 < myTol) || ( d2 < myTol)) {
115 if (d2 < d1) {Bs->Reverse();}
116 Add(myCurve, Bs, Standard_True);
117 return Standard_True;
118 }
119 // Ajout avant ?
120 else {
121 d1 = myCurve->Pole(1).Distance(Bs->Pole(1));
122 d2 = myCurve->Pole(1).Distance(Bs->Pole(LBs));
123 if ( (d1 < myTol) || (d2 < myTol)) {
124 if (d1 < d2) {Bs->Reverse();}
125 Add(Bs, myCurve, Standard_False );
126 return Standard_True;
127 }
128 }
129 }
130 return Standard_False;
131}
132
b514beda 133//=======================================================================
134//function : Add
135//purpose :
136//=======================================================================
137
7fd59977 138void Geom2dConvert_CompCurveToBSplineCurve::Add(
139 Handle(Geom2d_BSplineCurve)& FirstCurve,
140 Handle(Geom2d_BSplineCurve)& SecondCurve,
141 const Standard_Boolean After)
142{
143// Harmonisation des degres.
144 Standard_Integer Deg = Max(FirstCurve->Degree(), SecondCurve->Degree());
145 if (FirstCurve->Degree() < Deg) { FirstCurve->IncreaseDegree(Deg); }
146 if (SecondCurve->Degree() < Deg) { SecondCurve->IncreaseDegree(Deg); }
147
148// Declarationd
149 Standard_Real L1, L2, U_de_raccord;
150 Standard_Integer ii, jj;
151 Standard_Real Ratio=1, Ratio1, Ratio2, Delta1, Delta2;
152 Standard_Integer NbP1 = FirstCurve->NbPoles(), NbP2 = SecondCurve->NbPoles();
153 Standard_Integer NbK1 = FirstCurve->NbKnots(), NbK2 = SecondCurve->NbKnots();
154 TColStd_Array1OfReal Noeuds (1, NbK1+NbK2-1);
155 TColgp_Array1OfPnt2d Poles (1, NbP1+ NbP2-1);
156 TColStd_Array1OfReal Poids (1, NbP1+ NbP2-1);
157 TColStd_Array1OfInteger Mults (1, NbK1+NbK2-1);
158
159 // Ratio de reparametrisation (C1 si possible)
160 L1 = FirstCurve->DN(FirstCurve->LastParameter(), 1).Magnitude();
161 L2 = SecondCurve->DN(SecondCurve->FirstParameter(), 1). Magnitude();
162
163 if ( (L1 > Precision::Confusion()) && (L2 > Precision::Confusion()) ) {
164 Ratio = L1 / L2;
165 }
166 if ( (Ratio < Precision::Confusion()) || (Ratio > 1/Precision::Confusion()) ) {Ratio = 1;}
167
168 if (After) {
169// On ne bouge pas la premiere courbe
170 Ratio1 = 1;
171 Delta1 = 0;
172 Ratio2 = 1/Ratio;
173 Delta2 = Ratio2*SecondCurve->Knot(1) - FirstCurve->Knot(NbK1);
174 U_de_raccord = FirstCurve->LastParameter();
175 }
176 else {
177// On ne bouge pas la seconde courbe
178 Ratio1 = Ratio;
179 Delta1 = Ratio1*FirstCurve->Knot(NbK1) - SecondCurve->Knot(1);
180 Ratio2 = 1;
181 Delta2 = 0;
182 U_de_raccord = SecondCurve->FirstParameter();
183 }
184
185// Les Noeuds
186
187 for (ii=1; ii<NbK1; ii++) {
188 Noeuds(ii) = Ratio1*FirstCurve->Knot(ii) - Delta1;
189 Mults(ii) = FirstCurve->Multiplicity(ii);
190 }
191 Noeuds(NbK1) = U_de_raccord;
192 Mults(NbK1) = FirstCurve->Degree();
193 for (ii=2, jj=NbK1+1; ii<=NbK2; ii++, jj++) {
194 Noeuds(jj) = Ratio2*SecondCurve->Knot(ii) - Delta2;
195 Mults(jj) = SecondCurve->Multiplicity(ii);
196 }
197 Ratio = FirstCurve->Weight(NbP1) ;
198 Ratio /= SecondCurve->Weight(1) ;
199// Les Poles et Poids
200 for (ii=1; ii<NbP1; ii++) {
201 Poles(ii) = FirstCurve->Pole(ii);
202 Poids(ii) = FirstCurve->Weight(ii);
203 }
204 for (ii=1, jj=NbP1; ii<=NbP2; ii++, jj++) {
205 Poles(jj) = SecondCurve->Pole(ii);
206//
207// attentiion les poids ne se raccord pas forcement C0
208// d'ou Ratio
209//
210 Poids(jj) = Ratio * SecondCurve->Weight(ii);
211 }
212
213// Creation de la BSpline
214 myCurve = new (Geom2d_BSplineCurve) (Poles, Poids, Noeuds, Mults, Deg);
215
216// Reduction eventuelle de la multiplicite
217 Standard_Boolean Ok = Standard_True;
218 Standard_Integer M = Mults(NbK1);
219 while ( (M>0) && Ok) {
220 M--;
221 Ok = myCurve->RemoveKnot(NbK1, M, myTol);
222 }
223}
224
b514beda 225//=======================================================================
226//function : BSplineCurve
227//purpose :
228//=======================================================================
7fd59977 229
230Handle(Geom2d_BSplineCurve) Geom2dConvert_CompCurveToBSplineCurve::BSplineCurve() const
231{
b514beda 232 return myCurve;
233}
234
235//=======================================================================
236//function : Clear
237//purpose :
238//=======================================================================
239
240void Geom2dConvert_CompCurveToBSplineCurve::Clear()
241{
242 myCurve.Nullify();
7fd59977 243}