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