7fd59977 |
1 | // File: BRepBlend_AppFuncRoot.cxx |
2 | // Created: Tue May 12 16:57:47 1998 |
3 | // Author: Philippe NOUAILLE |
4 | // <pne@cleox.paris1.matra-dtv.fr> |
5 | |
6 | |
7 | #include <BRepBlend_AppFuncRoot.ixx> |
8 | #include <Blend_AppFunction.hxx> |
9 | |
10 | #include <Blend_Point.hxx> |
11 | #include <BRepBlend_Line.hxx> |
12 | |
13 | #include <math_FunctionSetRoot.hxx> |
14 | |
15 | #include <TColgp_HArray1OfPnt.hxx> |
16 | #include <TColgp_HArray1OfPnt2d.hxx> |
17 | #include <TColgp_HArray1OfVec.hxx> |
18 | #include <TColgp_HArray1OfVec2d.hxx> |
19 | |
20 | #include <TColStd_HArray1OfReal.hxx> |
21 | #include <TColStd_HArray1OfInteger.hxx> |
22 | |
23 | BRepBlend_AppFuncRoot::BRepBlend_AppFuncRoot(Handle(BRepBlend_Line)& Line, |
24 | Blend_AppFunction& Func, |
25 | const Standard_Real Tol3d, |
26 | const Standard_Real Tol2d) |
27 | :myLine(Line), |
28 | myFunc(&Func), |
29 | myTolerance(1,Func.NbVariables()), |
30 | X1(1,Func.NbVariables()), |
31 | X2(1,Func.NbVariables()), |
32 | XInit(1,Func.NbVariables()), |
33 | Sol(1,Func.NbVariables()) |
34 | { |
35 | Standard_Integer NbPoles, NbKnots, Degree, NbPoles2d; |
36 | Standard_Integer ii; |
37 | |
38 | // Tolerances |
39 | Func.GetTolerance(myTolerance, Tol3d); |
40 | Standard_Integer dim = Func.NbVariables(); |
41 | for (ii=1; ii<= dim; ii++) { |
42 | if (myTolerance(ii)>Tol2d) { myTolerance(ii) = Tol2d;} |
43 | } |
44 | |
81bba717 |
45 | // Tables |
7fd59977 |
46 | Func.GetShape( NbPoles, NbKnots, Degree, NbPoles2d); |
47 | |
81bba717 |
48 | // Calculation of BaryCentre (rationnal case). |
7fd59977 |
49 | if (Func.IsRational()) { |
50 | Standard_Real Xmax =-1.e100, Xmin = 1.e100, |
51 | Ymax =-1.e100, Ymin = 1.e100, |
52 | Zmax =-1.e100, Zmin = 1.e100; |
53 | Blend_Point P; |
54 | for (ii=1; ii<=myLine->NbPoints(); ii++) { |
55 | P = myLine->Point(ii); |
56 | Xmax = Max ( Max(P.PointOnS1().X(), P.PointOnS2().X()), Xmax); |
57 | Xmin = Min ( Min(P.PointOnS1().X(), P.PointOnS2().X()), Xmin); |
58 | Ymax = Max ( Max(P.PointOnS1().Y(), P.PointOnS2().Y()), Ymax); |
59 | Ymin = Min ( Min(P.PointOnS1().Y(), P.PointOnS2().Y()), Ymin); |
60 | Zmax = Max ( Max(P.PointOnS1().Z(), P.PointOnS2().Z()), Zmax); |
61 | Zmin = Min ( Min(P.PointOnS1().Z(), P.PointOnS2().Z()), Zmin); |
62 | |
63 | myBary.SetCoord((Xmax+Xmin)/2, (Ymax+Ymin)/2, (Zmax+Zmin)/2); |
64 | } |
65 | } |
66 | else {myBary.SetCoord(0,0,0);} |
67 | } |
68 | |
69 | //================================================================================ |
70 | // Function: D0 |
81bba717 |
71 | // Purpose : Calculation of section for v = Param, if calculation fails |
72 | // Standard_False is raised. |
7fd59977 |
73 | //================================================================================ |
74 | Standard_Boolean BRepBlend_AppFuncRoot::D0(const Standard_Real Param, |
75 | const Standard_Real First, |
76 | const Standard_Real Last,TColgp_Array1OfPnt& Poles, |
77 | TColgp_Array1OfPnt2d& Poles2d,TColStd_Array1OfReal& Weigths) |
78 | { |
79 | Standard_Boolean Ok=Standard_True; |
80 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
81 | Ok = SearchPoint( *Func, Param, myPnt); |
82 | |
83 | if (Ok) (*Func).Section(myPnt, |
84 | Poles, |
85 | Poles2d, |
86 | Weigths); |
87 | return Ok; |
88 | } |
89 | |
90 | //================================================================================ |
91 | // Function: D1 |
81bba717 |
92 | // Purpose : Calculation of the partial derivative of the section corresponding to v |
93 | // for v = Param, if the calculation fails Standard_False is raised. |
7fd59977 |
94 | //================================================================================ |
95 | Standard_Boolean BRepBlend_AppFuncRoot::D1(const Standard_Real Param, |
96 | const Standard_Real First, |
97 | const Standard_Real Last, |
98 | TColgp_Array1OfPnt& Poles, |
99 | TColgp_Array1OfVec& DPoles, |
100 | TColgp_Array1OfPnt2d& Poles2d, |
101 | TColgp_Array1OfVec2d& DPoles2d, |
102 | TColStd_Array1OfReal& Weigths, |
103 | TColStd_Array1OfReal& DWeigths) |
104 | { |
105 | Standard_Boolean Ok=Standard_True; |
106 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
107 | |
108 | Ok = SearchPoint( *Func, Param, myPnt); |
109 | |
110 | if (Ok) { |
111 | Ok = (*Func).Section(myPnt, |
112 | Poles, DPoles, |
113 | Poles2d, DPoles2d, |
114 | Weigths, DWeigths); |
115 | } |
116 | |
117 | return Ok; |
118 | } |
119 | |
120 | //=========================================================================== |
121 | // Function: D2 |
81bba717 |
122 | // Purpose : Calculation of the derivative and second partial of the |
123 | // section corresponding to v. |
124 | // For v = Param, if the calculation fails Standard_False is raised. |
7fd59977 |
125 | //=========================================================================== |
126 | Standard_Boolean BRepBlend_AppFuncRoot::D2(const Standard_Real Param, |
127 | const Standard_Real First, |
128 | const Standard_Real Last, |
129 | TColgp_Array1OfPnt& Poles, |
130 | TColgp_Array1OfVec& DPoles, |
131 | TColgp_Array1OfVec& D2Poles, |
132 | TColgp_Array1OfPnt2d& Poles2d, |
133 | TColgp_Array1OfVec2d& DPoles2d, |
134 | TColgp_Array1OfVec2d& D2Poles2d, |
135 | TColStd_Array1OfReal& Weigths, |
136 | TColStd_Array1OfReal& DWeigths, |
137 | TColStd_Array1OfReal& D2Weigths) |
138 | { |
139 | Standard_Boolean Ok=Standard_True; |
140 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
141 | |
142 | Ok = SearchPoint( *Func, Param, myPnt); |
143 | if (Ok) { |
144 | Ok = (*Func).Section(myPnt, |
145 | Poles, DPoles, D2Poles, |
146 | Poles2d, DPoles2d, D2Poles2d, |
147 | Weigths, DWeigths, D2Weigths); |
148 | } |
149 | return Ok; |
150 | } |
151 | |
152 | Standard_Integer BRepBlend_AppFuncRoot::Nb2dCurves() const |
153 | { |
154 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
155 | Standard_Integer i,j,k,nbpol2d; |
156 | (*Func).GetShape(i,j,k,nbpol2d); |
157 | return nbpol2d; |
158 | } |
159 | |
160 | void BRepBlend_AppFuncRoot::SectionShape(Standard_Integer& NbPoles, |
161 | Standard_Integer& NbKnots, |
162 | Standard_Integer& Degree) const |
163 | { |
164 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
165 | Standard_Integer ii; |
166 | (*Func).GetShape( NbPoles, NbKnots, Degree, ii); |
167 | } |
168 | |
169 | void BRepBlend_AppFuncRoot::Knots(TColStd_Array1OfReal& TKnots) const |
170 | { |
171 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
172 | Func->Knots(TKnots); |
173 | } |
174 | |
175 | void BRepBlend_AppFuncRoot::Mults(TColStd_Array1OfInteger& TMults) const |
176 | { |
177 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
178 | Func->Mults(TMults); |
179 | } |
180 | |
181 | Standard_Boolean BRepBlend_AppFuncRoot::IsRational() const |
182 | { |
183 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
184 | return (*Func).IsRational(); |
185 | } |
186 | |
187 | Standard_Integer BRepBlend_AppFuncRoot::NbIntervals(const GeomAbs_Shape S) const |
188 | { |
189 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
190 | return Func->NbIntervals(S); |
191 | } |
192 | |
193 | void BRepBlend_AppFuncRoot::Intervals(TColStd_Array1OfReal& T,const GeomAbs_Shape S) const |
194 | { |
195 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
196 | Func->Intervals(T, S); |
197 | } |
198 | |
199 | void BRepBlend_AppFuncRoot::SetInterval(const Standard_Real First,const Standard_Real Last) |
200 | { |
201 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
202 | Func->Set(First, Last); |
203 | } |
204 | |
205 | void BRepBlend_AppFuncRoot::Resolution(const Standard_Integer Index, |
206 | const Standard_Real Tol, |
207 | Standard_Real& TolU, |
208 | Standard_Real& TolV) const |
209 | { |
210 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
211 | Func->Resolution(Index,Tol,TolU,TolV); |
212 | } |
213 | |
214 | void BRepBlend_AppFuncRoot::GetTolerance(const Standard_Real BoundTol, |
215 | const Standard_Real SurfTol, |
216 | const Standard_Real AngleTol, |
217 | TColStd_Array1OfReal& Tol3d) const |
218 | { |
219 | Standard_Integer ii; |
220 | math_Vector V3d(1, Tol3d.Length()), V1d(1, Tol3d.Length()); |
221 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
222 | |
223 | Func->GetTolerance(BoundTol, SurfTol, AngleTol, V3d, V1d); |
224 | for (ii=1; ii<=Tol3d.Length(); ii++) Tol3d(ii) = V3d(ii); |
225 | } |
226 | |
227 | void BRepBlend_AppFuncRoot::SetTolerance(const Standard_Real Tol3d, |
228 | const Standard_Real Tol2d) |
229 | { |
230 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
231 | Standard_Integer ii, dim = Func->NbVariables(); |
232 | Func->GetTolerance(myTolerance, Tol3d); |
233 | for (ii=1; ii<=dim; ii++) { |
234 | if (myTolerance(ii)>Tol2d) { myTolerance(ii) = Tol2d;} |
235 | } |
236 | } |
237 | |
238 | gp_Pnt BRepBlend_AppFuncRoot::BarycentreOfSurf() const |
239 | { |
240 | return myBary; |
241 | } |
242 | |
243 | Standard_Real BRepBlend_AppFuncRoot::MaximalSection() const |
244 | { |
245 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
246 | return Func->GetSectionSize(); |
247 | } |
248 | |
249 | void BRepBlend_AppFuncRoot::GetMinimalWeight(TColStd_Array1OfReal& Weigths) const |
250 | { |
251 | Blend_AppFunction* Func = (Blend_AppFunction*)myFunc; |
252 | Func->GetMinimalWeight(Weigths); |
253 | } |
254 | |
255 | |
256 | //================================================================================ |
257 | // |
258 | // Function : SearchPoint |
259 | // |
81bba717 |
260 | // Purpose : Find point solution with parameter Param (on 2 Surfaces) |
7fd59977 |
261 | // |
81bba717 |
262 | // Algorithm : |
263 | // 1) Approximative solution is found from already calculated Points |
264 | // 2) Convergence is done by a method of type Newton |
7fd59977 |
265 | // |
81bba717 |
266 | // Possible causes of fails : |
267 | // - Singularity on surfaces. |
268 | // - no information oin the "line" resulting from processing. |
7fd59977 |
269 | // |
270 | //================================================================================ |
271 | |
272 | Standard_Boolean BRepBlend_AppFuncRoot::SearchPoint(Blend_AppFunction& Func, |
273 | const Standard_Real Param, |
274 | Blend_Point& Pnt) |
275 | { |
276 | Standard_Boolean Trouve; |
277 | Standard_Integer dim = Func.NbVariables(); |
81bba717 |
278 | // (1) Find a point of init |
7fd59977 |
279 | Standard_Integer I1=1, I2=myLine->NbPoints(), Index; |
280 | Standard_Real t1, t2; |
281 | |
81bba717 |
282 | // (1.a) It is checked if it is inside |
7fd59977 |
283 | if (Param < myLine->Point(I1).Parameter()) {return Standard_False;} |
284 | if (Param > myLine->Point(I2).Parameter()) {return Standard_False;} |
285 | |
81bba717 |
286 | // (1.b) Find the interval |
7fd59977 |
287 | Trouve = SearchLocation(Param, I1, I2, Index); |
288 | |
81bba717 |
289 | // (1.c) If the point is already calculated it is returned |
7fd59977 |
290 | if (Trouve) { |
291 | Pnt = myLine->Point(Index); |
292 | Vec(XInit,Pnt); |
293 | } |
294 | else { |
81bba717 |
295 | // (1.d) Intialisation by linear interpolation |
7fd59977 |
296 | Pnt = myLine->Point(Index); |
297 | Vec(X1,Pnt); |
298 | t1 = Pnt.Parameter(); |
299 | |
300 | Pnt = myLine->Point(Index+1); |
301 | Vec(X2,Pnt); |
302 | t2 = Pnt.Parameter(); |
303 | |
304 | Standard_Real Parammt1 = (Param-t1) / (t2-t1); |
305 | Standard_Real t2mParam = (t2-Param) / (t2-t1); |
306 | for(Standard_Integer i = 1; i <= dim; i++){ |
307 | XInit(i) = X2(i) * Parammt1 + X1(i) * t2mParam; |
308 | } |
309 | } |
310 | |
81bba717 |
311 | // (2) Calculation of the solution ------------------------ |
7fd59977 |
312 | Func.Set(Param); |
313 | Func.GetBounds(X1, X2); |
314 | math_FunctionSetRoot rsnld(Func, myTolerance, 30); |
315 | |
316 | rsnld.Perform(Func, XInit, X1, X2); |
317 | |
318 | if (!rsnld.IsDone()) { |
319 | # ifdef DEB |
320 | cout << "AppFunc : RNLD Not done en t = " << Param << endl; |
321 | # endif |
322 | return Standard_False; |
323 | } |
324 | rsnld.Root(Sol); |
325 | |
81bba717 |
326 | // (3) Storage of the point |
7fd59977 |
327 | Point(Func,Param,Sol,Pnt); |
328 | |
81bba717 |
329 | // (4) Insertion of the point if the calculation seems long. |
7fd59977 |
330 | if ((!Trouve)&&(rsnld.NbIterations()>3)) { |
331 | #ifdef DEB |
81bba717 |
332 | cout << "Evaluation in t = " << Param << "given" << endl; |
7fd59977 |
333 | rsnld.Dump(cout); |
334 | #endif |
335 | myLine->InsertBefore(Index+1, Pnt); |
336 | } |
337 | return Standard_True; |
338 | } |
339 | |
340 | |
341 | //============================================================================= |
342 | // |
343 | // Function : SearchLocation |
344 | // |
81bba717 |
345 | // Purpose : Binary search of the line of the parametric interval containing |
346 | // Param in the list of calculated points (myline) |
347 | // if the point of parameter Param is already stored in the list |
348 | // True is raised and ParamIndex corresponds to line of Point. |
349 | // Complexity of this algorithm is log(n)/log(2) |
7fd59977 |
350 | //================================================================================ |
351 | Standard_Boolean BRepBlend_AppFuncRoot::SearchLocation(const Standard_Real Param, |
352 | const Standard_Integer FirstIndex, |
353 | const Standard_Integer LastIndex, |
354 | Standard_Integer& ParamIndex) const |
355 | { |
356 | Standard_Integer Ideb = FirstIndex, Ifin = LastIndex, Idemi; |
357 | Standard_Real Valeur; |
358 | |
359 | Valeur = myLine->Point(Ideb).Parameter(); |
360 | if (Param == Valeur) { |
361 | ParamIndex = Ideb; |
362 | return Standard_True; |
363 | } |
364 | |
365 | Valeur = myLine->Point(Ifin).Parameter(); |
366 | if (Param == Valeur) { |
367 | ParamIndex = Ifin; |
368 | return Standard_True; |
369 | } |
370 | |
371 | while ( Ideb+1 != Ifin) { |
372 | Idemi = (Ideb+Ifin)/2; |
373 | Valeur = myLine->Point(Idemi).Parameter(); |
374 | if (Valeur < Param) {Ideb = Idemi;} |
375 | else { |
376 | if ( Valeur > Param) { Ifin = Idemi;} |
377 | else { ParamIndex = Idemi; |
378 | return Standard_True;} |
379 | } |
380 | } |
381 | |
382 | ParamIndex = Ideb; |
383 | return Standard_False; |
384 | } |
385 | |