0022627: Change OCCT memory management defaults
[occt.git] / src / Geom2dConvert / Geom2dConvert_CompCurveToBSplineCurve.cxx
CommitLineData
7fd59977 1// File: Geom2dConvert_CompCurveToBSplineCurve.cxx
2// Created: Tue Apr 29 11:19:42 1997
3// Author: Stagiaire Francois DUMONT
4// <dum@brunox.paris1.matra-dtv.fr>
5
6
7#include <Geom2dConvert_CompCurveToBSplineCurve.ixx>
8
9#include <Geom2d_BSplineCurve.hxx>
10#include <Geom2dConvert.hxx>
11
12#include <TColStd_Array1OfReal.hxx>
13#include <TColStd_Array1OfInteger.hxx>
14
15#include <TColgp_Array1OfPnt2d.hxx>
16#include <gp_Vec2d.hxx>
17#include <gp_Pnt2d.hxx>
18#include <Precision.hxx>
19
20
21Geom2dConvert_CompCurveToBSplineCurve::
22Geom2dConvert_CompCurveToBSplineCurve(const Handle(Geom2d_BoundedCurve)& BasisCurve,
23 const Convert_ParameterisationType Parameterisation) :
24 myTol(Precision::Confusion()),
25 myType(Parameterisation)
26{
27 Handle(Geom2d_BSplineCurve) Bs =
28 Handle(Geom2d_BSplineCurve)::DownCast(BasisCurve);
29 if (!Bs.IsNull()) {
30 myCurve = Handle(Geom2d_BSplineCurve)::DownCast(BasisCurve->Copy());
31 }
32 else {
33 myCurve = Geom2dConvert::CurveToBSplineCurve (BasisCurve, myType);
34 }
35}
36
37//=======================================================================
38//function : Add
39//purpose :
40//=======================================================================
41
42Standard_Boolean Geom2dConvert_CompCurveToBSplineCurve::
43Add(const Handle(Geom2d_BoundedCurve)& NewCurve,
44 const Standard_Real Tolerance,
45 const Standard_Boolean After)
46{
47 myTol = Tolerance;
48// Convertion
49 Handle(Geom2d_BSplineCurve) Bs =
50 Handle(Geom2d_BSplineCurve)::DownCast(NewCurve);
51 if (!Bs.IsNull() ) {
52 Bs = Handle(Geom2d_BSplineCurve)::DownCast(NewCurve->Copy());
53 }
54 else {
55 Bs = Geom2dConvert::CurveToBSplineCurve (NewCurve, myType);
56 }
57
58 Standard_Integer LBs = Bs->NbPoles(), LCb = myCurve->NbPoles();
59
60 // myCurve est elle fermee ?
61 if (myCurve->Pole(LCb).Distance(myCurve->Pole(1))< myTol){
62 if(After){
63 // Ajout Apres ?
64 if (myCurve->Pole(LCb).Distance(Bs->Pole(LBs)) < myTol) {Bs->Reverse();}
65 if (myCurve->Pole(LCb).Distance(Bs->Pole(1)) < myTol) {
66 Add(myCurve, Bs, Standard_True);
67 return Standard_True;
68 }
69 }
70 else{
71 // Ajout avant ?
72 if (myCurve->Pole(1).Distance(Bs->Pole(1)) < myTol) {Bs->Reverse();}
73 if (myCurve->Pole(1).Distance(Bs->Pole(LBs)) < myTol) {
74 Add(Bs, myCurve, Standard_False);
75 return Standard_True;
76 }
77 }
78 }
79 // Ajout Apres ?
80 else {
81
82 Standard_Real d1 = myCurve->Pole(LCb).Distance(Bs->Pole(1));
83 Standard_Real d2 = myCurve->Pole(LCb).Distance(Bs->Pole(LBs));
84 if (( d1 < myTol) || ( d2 < myTol)) {
85 if (d2 < d1) {Bs->Reverse();}
86 Add(myCurve, Bs, Standard_True);
87 return Standard_True;
88 }
89 // Ajout avant ?
90 else {
91 d1 = myCurve->Pole(1).Distance(Bs->Pole(1));
92 d2 = myCurve->Pole(1).Distance(Bs->Pole(LBs));
93 if ( (d1 < myTol) || (d2 < myTol)) {
94 if (d1 < d2) {Bs->Reverse();}
95 Add(Bs, myCurve, Standard_False );
96 return Standard_True;
97 }
98 }
99 }
100 return Standard_False;
101}
102
103void Geom2dConvert_CompCurveToBSplineCurve::Add(
104 Handle(Geom2d_BSplineCurve)& FirstCurve,
105 Handle(Geom2d_BSplineCurve)& SecondCurve,
106 const Standard_Boolean After)
107{
108// Harmonisation des degres.
109 Standard_Integer Deg = Max(FirstCurve->Degree(), SecondCurve->Degree());
110 if (FirstCurve->Degree() < Deg) { FirstCurve->IncreaseDegree(Deg); }
111 if (SecondCurve->Degree() < Deg) { SecondCurve->IncreaseDegree(Deg); }
112
113// Declarationd
114 Standard_Real L1, L2, U_de_raccord;
115 Standard_Integer ii, jj;
116 Standard_Real Ratio=1, Ratio1, Ratio2, Delta1, Delta2;
117 Standard_Integer NbP1 = FirstCurve->NbPoles(), NbP2 = SecondCurve->NbPoles();
118 Standard_Integer NbK1 = FirstCurve->NbKnots(), NbK2 = SecondCurve->NbKnots();
119 TColStd_Array1OfReal Noeuds (1, NbK1+NbK2-1);
120 TColgp_Array1OfPnt2d Poles (1, NbP1+ NbP2-1);
121 TColStd_Array1OfReal Poids (1, NbP1+ NbP2-1);
122 TColStd_Array1OfInteger Mults (1, NbK1+NbK2-1);
123
124 // Ratio de reparametrisation (C1 si possible)
125 L1 = FirstCurve->DN(FirstCurve->LastParameter(), 1).Magnitude();
126 L2 = SecondCurve->DN(SecondCurve->FirstParameter(), 1). Magnitude();
127
128 if ( (L1 > Precision::Confusion()) && (L2 > Precision::Confusion()) ) {
129 Ratio = L1 / L2;
130 }
131 if ( (Ratio < Precision::Confusion()) || (Ratio > 1/Precision::Confusion()) ) {Ratio = 1;}
132
133 if (After) {
134// On ne bouge pas la premiere courbe
135 Ratio1 = 1;
136 Delta1 = 0;
137 Ratio2 = 1/Ratio;
138 Delta2 = Ratio2*SecondCurve->Knot(1) - FirstCurve->Knot(NbK1);
139 U_de_raccord = FirstCurve->LastParameter();
140 }
141 else {
142// On ne bouge pas la seconde courbe
143 Ratio1 = Ratio;
144 Delta1 = Ratio1*FirstCurve->Knot(NbK1) - SecondCurve->Knot(1);
145 Ratio2 = 1;
146 Delta2 = 0;
147 U_de_raccord = SecondCurve->FirstParameter();
148 }
149
150// Les Noeuds
151
152 for (ii=1; ii<NbK1; ii++) {
153 Noeuds(ii) = Ratio1*FirstCurve->Knot(ii) - Delta1;
154 Mults(ii) = FirstCurve->Multiplicity(ii);
155 }
156 Noeuds(NbK1) = U_de_raccord;
157 Mults(NbK1) = FirstCurve->Degree();
158 for (ii=2, jj=NbK1+1; ii<=NbK2; ii++, jj++) {
159 Noeuds(jj) = Ratio2*SecondCurve->Knot(ii) - Delta2;
160 Mults(jj) = SecondCurve->Multiplicity(ii);
161 }
162 Ratio = FirstCurve->Weight(NbP1) ;
163 Ratio /= SecondCurve->Weight(1) ;
164// Les Poles et Poids
165 for (ii=1; ii<NbP1; ii++) {
166 Poles(ii) = FirstCurve->Pole(ii);
167 Poids(ii) = FirstCurve->Weight(ii);
168 }
169 for (ii=1, jj=NbP1; ii<=NbP2; ii++, jj++) {
170 Poles(jj) = SecondCurve->Pole(ii);
171//
172// attentiion les poids ne se raccord pas forcement C0
173// d'ou Ratio
174//
175 Poids(jj) = Ratio * SecondCurve->Weight(ii);
176 }
177
178// Creation de la BSpline
179 myCurve = new (Geom2d_BSplineCurve) (Poles, Poids, Noeuds, Mults, Deg);
180
181// Reduction eventuelle de la multiplicite
182 Standard_Boolean Ok = Standard_True;
183 Standard_Integer M = Mults(NbK1);
184 while ( (M>0) && Ok) {
185 M--;
186 Ok = myCurve->RemoveKnot(NbK1, M, myTol);
187 }
188}
189
190
191Handle(Geom2d_BSplineCurve) Geom2dConvert_CompCurveToBSplineCurve::BSplineCurve() const
192{
193 return myCurve;
194}