0026837: SameParameter algorithm throws an exception
[occt.git] / src / GeomLib / GeomLib.cxx
CommitLineData
b311480e 1// Created on: 1993-07-07
2// Created by: Jean Claude VAUTHIER
3// Copyright (c) 1993-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// Version:
b311480e 18//pmn 24/09/96 Ajout du prolongement de courbe.
7fd59977 19// jct 15/04/97 Ajout du prolongement de surface.
20// jct 24/04/97 simplification ou suppression de calculs
21// inutiles dans ExtendSurfByLength
22// correction de Tbord et Continuity=0 accepte
23// correction du calcul de lambda et appel a
24// TangExtendToConstraint avec lambmin au lieu de 1.
25// correction du passage Sr rat --> BSp nD
26// xab 26/06/97 treatement partiel anulation des derivees
27// partiels du denonimateur des Surfaces BSplines Rationnelles
28// dans le cas de valeurs proportionnelles des denominateurs
29// en umin umax et/ou vmin vmax.
30// pmn 4/07/97 Gestion de la continuite dans BuildCurve3d (PRO9097)
7fd59977 31// xab 10/07/97 on revient en arriere sur l'ajout du 26/06/97
32// pmn 26/09/97 Ajout des parametres d'approx dans BuildCurve3d
33// xab 29/09/97 on reintegre l'ajout du 26/06/97
34// pmn 31/10/97 Ajoute AdjustExtremity
35// jct 26/11/98 blindage dans ExtendSurf qd NTgte = 0 (CTS21288)
36// jct 19/01/99 traitement de la periodicite dans ExtendSurf
37// Design:
38// Warning: None
39// References: None
40// Language: C++2.0
41// Purpose:
7fd59977 42// Declarations:
43
7fd59977 44#include <Adaptor2d_HCurve2d.hxx>
42cf5bc1 45#include <Adaptor3d_Curve.hxx>
46#include <Adaptor3d_CurveOnSurface.hxx>
7fd59977 47#include <Adaptor3d_HCurve.hxx>
48#include <Adaptor3d_HSurface.hxx>
42cf5bc1 49#include <AdvApprox_ApproxAFunction.hxx>
50#include <AdvApprox_PrefAndRec.hxx>
51#include <BSplCLib.hxx>
52#include <BSplSLib.hxx>
53#include <CSLib.hxx>
54#include <CSLib_NormalStatus.hxx>
55#include <ElCLib.hxx>
7fd59977 56#include <Geom2d_BezierCurve.hxx>
42cf5bc1 57#include <Geom2d_BSplineCurve.hxx>
7fd59977 58#include <Geom2d_Circle.hxx>
42cf5bc1 59#include <Geom2d_Curve.hxx>
7fd59977 60#include <Geom2d_Ellipse.hxx>
7fd59977 61#include <Geom2d_Hyperbola.hxx>
42cf5bc1 62#include <Geom2d_Line.hxx>
7fd59977 63#include <Geom2d_OffsetCurve.hxx>
42cf5bc1 64#include <Geom2d_Parabola.hxx>
65#include <Geom2d_TrimmedCurve.hxx>
66#include <Geom2dAdaptor_Curve.hxx>
67#include <Geom2dAdaptor_GHCurve.hxx>
68#include <Geom2dAdaptor_HCurve.hxx>
69#include <Geom2dConvert.hxx>
70#include <Geom_BezierCurve.hxx>
7fd59977 71#include <Geom_BezierSurface.hxx>
42cf5bc1 72#include <Geom_BoundedCurve.hxx>
73#include <Geom_BoundedSurface.hxx>
74#include <Geom_BSplineCurve.hxx>
7fd59977 75#include <Geom_BSplineSurface.hxx>
42cf5bc1 76#include <Geom_Circle.hxx>
77#include <Geom_Curve.hxx>
78#include <Geom_Ellipse.hxx>
79#include <Geom_Hyperbola.hxx>
80#include <Geom_Line.hxx>
81#include <Geom_OffsetCurve.hxx>
82#include <Geom_Parabola.hxx>
83#include <Geom_Plane.hxx>
84#include <Geom_RectangularTrimmedSurface.hxx>
85#include <Geom_Surface.hxx>
86#include <Geom_TrimmedCurve.hxx>
87#include <GeomAdaptor_HSurface.hxx>
88#include <GeomAdaptor_Surface.hxx>
89#include <GeomConvert.hxx>
90#include <GeomConvert_ApproxSurface.hxx>
91#include <GeomConvert_CompCurveToBSplineCurve.hxx>
92#include <GeomLib.hxx>
93#include <GeomLib_DenominatorMultiplier.hxx>
94#include <GeomLib_DenominatorMultiplierPtr.hxx>
95#include <GeomLib_LogSample.hxx>
96#include <GeomLib_MakeCurvefromApprox.hxx>
97#include <GeomLib_PolyFunc.hxx>
98#include <gp_Ax2.hxx>
7fd59977 99#include <gp_Circ.hxx>
100#include <gp_Circ2d.hxx>
42cf5bc1 101#include <gp_Dir.hxx>
7fd59977 102#include <gp_Elips.hxx>
103#include <gp_Elips2d.hxx>
42cf5bc1 104#include <gp_GTrsf2d.hxx>
7fd59977 105#include <gp_Hypr.hxx>
106#include <gp_Hypr2d.hxx>
42cf5bc1 107#include <gp_Lin.hxx>
108#include <gp_Lin2d.hxx>
7fd59977 109#include <gp_Parab.hxx>
110#include <gp_Parab2d.hxx>
42cf5bc1 111#include <gp_Pnt.hxx>
112#include <gp_Pnt2d.hxx>
7fd59977 113#include <gp_Trsf2d.hxx>
42cf5bc1 114#include <gp_TrsfForm.hxx>
115#include <gp_Vec.hxx>
116#include <Hermit.hxx>
117#include <math.hxx>
118#include <math_FunctionAllRoots.hxx>
119#include <math_FunctionSample.hxx>
120#include <math_Jacobi.hxx>
121#include <math_Matrix.hxx>
122#include <math_Vector.hxx>
123#include <PLib.hxx>
124#include <Precision.hxx>
7fd59977 125#include <Standard_ConstructionError.hxx>
42cf5bc1 126#include <Standard_NotImplemented.hxx>
127#include <TColgp_Array1OfPnt.hxx>
128#include <TColgp_Array1OfPnt2d.hxx>
129#include <TColgp_Array1OfVec.hxx>
130#include <TColgp_Array1OfXYZ.hxx>
131#include <TColgp_Array2OfPnt.hxx>
132#include <TColgp_HArray2OfPnt.hxx>
133#include <TColStd_Array1OfInteger.hxx>
134#include <TColStd_Array1OfReal.hxx>
135#include <TColStd_Array2OfReal.hxx>
136#include <TColStd_HArray1OfReal.hxx>
137#include <TColStd_HArray2OfReal.hxx>
7fd59977 138
139//=======================================================================
140//function : ComputeLambda
141//purpose : Calcul le facteur lambda qui minimise la variation de vittesse
142// sur une interpolation d'hermite d'ordre (i,0)
143//=======================================================================
144static void ComputeLambda(const math_Matrix& Constraint,
145 const math_Matrix& Hermit,
146 const Standard_Real Length,
147 Standard_Real& Lambda )
148{
149 Standard_Integer size = Hermit.RowNumber();
150 Standard_Integer Continuity = size-2;
151 Standard_Integer ii, jj, ip, pp;
152
153 //Minimization
154 math_Matrix HDer(1, size-1, 1, size);
155 for (jj=1; jj<=size; jj++) {
156 for (ii=1; ii<size;ii++) {
157 HDer(ii, jj) = ii*Hermit(jj, ii+1);
158 }
159 }
160
161 math_Vector V(1, size);
162 math_Vector Vec1(1, Constraint.RowNumber());
163 math_Vector Vec2(1, Constraint.RowNumber());
164 math_Vector Vec3(1, Constraint.RowNumber());
165 math_Vector Vec4(1, Constraint.RowNumber());
166
167 Standard_Real * polynome = &HDer(1,1);
168 Standard_Real * valhder = &V(1);
169 Vec2 = Constraint.Col(2);
170 Vec2 /= Length;
171 Standard_Real t, squared1 = Vec2.Norm2(), GW;
172// math_Matrix Vec(1, Constraint.RowNumber(), 1, size-1);
173// gp_Vec Vfirst(p0.XYZ()), Vlast(Point.XYZ());
174// TColgp_Array1OfVec Der(2, 4);
175// Der(2) = d1; Der(3) = d2; Der(4) = d3;
176
177 Standard_Integer GOrdre = 4 + 4*Continuity,
178 DDim=Continuity*(Continuity+2);
179 math_Vector GaussP(1, GOrdre), GaussW(1, GOrdre),
180 pol2(1, 2*Continuity+1),
181 pol4(1, 4*Continuity+1);
182 math::GaussPoints(GOrdre, GaussP);
183 math::GaussWeights (GOrdre, GaussW);
184 pol4.Init(0.);
185
186 for (ip=1; ip<=GOrdre; ip++) {
187 t = (GaussP(ip)+1.)/2;
188 GW = GaussW(ip);
189 PLib::NoDerivativeEvalPolynomial(t , Continuity, Continuity+2, DDim,
190 polynome[0], valhder[0]);
191 V /= Length; //Normalisation
192
193 // i
194 // C'(t) = SUM Vi*Lambda
195 Vec1 = Constraint.Col(1);
196 Vec1 *= V(1);
197 Vec1 += V(size)*Constraint.Col(size);
198 Vec2 = Constraint.Col(2);
199 Vec2 *= V(2);
200 if (Continuity > 1) {
201 Vec3 = Constraint.Col(3);
202 Vec3 *= V(3);
203 if (Continuity > 2) {
204 Vec4 = Constraint.Col(4);
205 Vec4 *= V(4);
206 }
207 }
208
209
210 // 2 2
211 // C'(t) - C'(0)
212
213 pol2(1) = Vec1.Norm2();
214 pol2(2) = 2*(Vec1.Multiplied(Vec2));
215 pol2(3) = Vec2.Norm2() - squared1;
216 if (Continuity>1) {
217 pol2(3) += 2*(Vec1.Multiplied(Vec3));
218 pol2(4) = 2*(Vec2.Multiplied(Vec3));
219 pol2(5) = Vec3.Norm2();
220 if (Continuity>2) {
221 pol2(4)+= 2*(Vec1.Multiplied(Vec4));
222 pol2(5)+= 2*(Vec2.Multiplied(Vec4));
223 pol2(6) = 2*(Vec3.Multiplied(Vec4));
224 pol2(7) = Vec4.Norm2();
225 }
226 }
227
228 // 2 2 2
229 // Integrale de ( C'(t) - C'(0) )
230 for (ii=1; ii<=pol2.Length(); ii++) {
231 pp = ii;
232 for(jj=1; jj<ii; jj++, pp++) {
233 pol4(pp) += 2*GW*pol2(ii)*pol2(jj);
234 }
235 pol4(2*ii-1) += GW*Pow(pol2(ii), 2);
236 }
237 }
238
239 Standard_Real EMin, E;
240 PLib::NoDerivativeEvalPolynomial(Lambda , pol4.Length()-1, 1,
241 pol4.Length()-1,
242 pol4(1), EMin);
243
244 if (EMin > Precision::Confusion()) {
245 // Recheche des extrema de la fonction
246 GeomLib_PolyFunc FF(pol4);
247 GeomLib_LogSample S(Lambda/1000, 50*Lambda, 100);
248 math_FunctionAllRoots Solve(FF, S, Precision::Confusion(),
249 Precision::Confusion()*(Length+1),
250 1.e-15);
251 if (Solve.IsDone()) {
252 for (ii=1; ii<=Solve.NbPoints(); ii++) {
253 t = Solve.GetPoint(ii);
254 PLib::NoDerivativeEvalPolynomial(t , pol4.Length()-1, 1,
255 pol4.Length()-1,
256 pol4(1), E);
257 if (E < EMin) {
258 Lambda = t;
259 EMin = E;
260 }
261 }
262 }
263 }
264}
265
266#include <Extrema_LocateExtPC.hxx>
ec357c5c 267#include <Geom2d_Curve.hxx>
7fd59977 268//=======================================================================
269//function : RemovePointsFromArray
270//purpose :
271//=======================================================================
272
273void GeomLib::RemovePointsFromArray(const Standard_Integer NumPoints,
274 const TColStd_Array1OfReal& InParameters,
275 Handle(TColStd_HArray1OfReal)& OutParameters)
276{
277 Standard_Integer ii,
278 jj,
279 add_one_point,
280 loc_num_points,
281 num_points,
282 index ;
283 Standard_Real delta,
284 current_parameter ;
285
286 loc_num_points = Max(0,NumPoints-2) ;
287 delta = InParameters(InParameters.Upper()) - InParameters(InParameters.Lower()) ;
288 delta /= (Standard_Real) (loc_num_points + 1) ;
289 num_points = 1 ;
290 current_parameter = InParameters(InParameters.Lower()) + delta * 0.5e0 ;
291 ii = InParameters.Lower() + 1 ;
292 for (jj = 0 ; ii < InParameters.Upper() && jj < NumPoints ; jj++) {
293 add_one_point = 0 ;
294 while ( ii < InParameters.Upper() && InParameters(ii) < current_parameter) {
295 ii += 1 ;
296 add_one_point = 1 ;
297 }
298 num_points += add_one_point ;
299 current_parameter += delta ;
300 }
301 if (NumPoints <= 2) {
302 num_points = 2 ;
303 }
304 index = 2 ;
305 current_parameter = InParameters(InParameters.Lower()) + delta * 0.5e0 ;
306 OutParameters =
307 new TColStd_HArray1OfReal(1,num_points) ;
308 OutParameters->ChangeArray1()(1) = InParameters(InParameters.Lower()) ;
309 ii = InParameters.Lower() + 1 ;
310 for (jj = 0 ; ii < InParameters.Upper() && jj < NumPoints ; jj++) {
311 add_one_point = 0 ;
312 while (ii < InParameters.Upper() && InParameters(ii) < current_parameter) {
313 ii += 1 ;
314 add_one_point = 1 ;
315 }
316 if (add_one_point && index <= num_points) {
317 OutParameters->ChangeArray1()(index) = InParameters(ii-1) ;
318 index += 1 ;
319 }
320 current_parameter += delta ;
321 }
322 OutParameters->ChangeArray1()(num_points) = InParameters(InParameters.Upper()) ;
323}
324//=======================================================================
325//function : DensifyArray1OfReal
326//purpose :
327//=======================================================================
328
329void GeomLib::DensifyArray1OfReal(const Standard_Integer MinNumPoints,
330 const TColStd_Array1OfReal& InParameters,
331 Handle(TColStd_HArray1OfReal)& OutParameters)
332{
333 Standard_Integer ii,
334 in_order,
335 num_points,
336 num_parameters_to_add,
337 index ;
338 Standard_Real delta,
339 current_parameter ;
340
341 in_order = 1 ;
342 if (MinNumPoints > InParameters.Length()) {
343
344 //
345 // checks the paramaters are in increasing order
346 //
347 for (ii = InParameters.Lower() ; ii < InParameters.Upper() ; ii++) {
348 if (InParameters(ii) > InParameters(ii+1)) {
349 in_order = 0 ;
350 break ;
351 }
352 }
353 if (in_order) {
354 num_parameters_to_add = MinNumPoints - InParameters.Length() ;
355 delta = InParameters(InParameters.Upper()) - InParameters(InParameters.Lower()) ;
356 delta /= (Standard_Real) (num_parameters_to_add + 1) ;
357 num_points = MinNumPoints ;
358 OutParameters =
359 new TColStd_HArray1OfReal(1,num_points) ;
360 index = 1 ;
361 current_parameter = InParameters(InParameters.Lower()) ;
362 OutParameters->ChangeArray1()(index) = current_parameter ;
363 index += 1 ;
364 current_parameter += delta ;
365 for (ii = InParameters.Lower() + 1 ; index <= num_points && ii <= InParameters.Upper() ; ii++) {
366 while (current_parameter < InParameters(ii) && index <= num_points) {
367 OutParameters->ChangeArray1()(index) = current_parameter ;
368 index += 1 ;
369 current_parameter += delta ;
370 }
371 if (index <= num_points) {
372 OutParameters->ChangeArray1()(index) = InParameters(ii) ;
373 }
374 index += 1 ;
375 }
376 //
377 // beware of roundoff !
378 //
379 OutParameters->ChangeArray1()(num_points) = InParameters(InParameters.Upper()) ;
380 }
381 else {
382 index = 1 ;
383 num_points = InParameters.Length() ;
384 OutParameters =
385 new TColStd_HArray1OfReal(1,num_points) ;
386 for (ii = InParameters.Lower() ; ii <= InParameters.Upper() ; ii++) {
387 OutParameters->ChangeArray1()(index) = InParameters(ii) ;
388 index += 1 ;
389 }
390 }
391 }
392 else {
393 index = 1 ;
394 num_points = InParameters.Length() ;
395 OutParameters =
396 new TColStd_HArray1OfReal(1,num_points) ;
397 for (ii = InParameters.Lower() ; ii <= InParameters.Upper() ; ii++) {
398 OutParameters->ChangeArray1()(index) = InParameters(ii) ;
399 index += 1 ;
400 }
401 }
402}
403
404//=======================================================================
405//function : FuseIntervals
406//purpose :
407//=======================================================================
408void GeomLib::FuseIntervals(const TColStd_Array1OfReal& I1,
409 const TColStd_Array1OfReal& I2,
410 TColStd_SequenceOfReal& Seq,
411 const Standard_Real Epspar)
412{
413 Standard_Integer ind1=1, ind2=1;
414 Standard_Real v1, v2;
415// Initialisations : les IND1 et IND2 pointent sur le 1er element
416// de chacune des 2 tables a traiter.INDS pointe sur le dernier
417// element cree de TABSOR
418
419
420//--- On remplit TABSOR en parcourant TABLE1 et TABLE2 simultanement ---
421//------------------ en eliminant les occurrences multiples ------------
422
423 while ((ind1<=I1.Upper()) && (ind2<=I2.Upper())) {
424 v1 = I1(ind1);
425 v2 = I2(ind2);
426 if (Abs(v1-v2)<= Epspar) {
427// Ici les elements de I1 et I2 conviennent .
428 Seq.Append((v1+v2)/2);
429 ind1++;
430 ind2++;
431 }
432 else if (v1 < v2) {
433 // Ici l' element de I1 convient.
434 Seq.Append(v1);
435 ind1++;
436 }
437 else {
438// Ici l' element de TABLE2 convient.
439 Seq.Append(v2);
440 ind2++;
441 }
442 }
443
444 if (ind1>I1.Upper()) {
445//----- Ici I1 est epuise, on complete avec la fin de TABLE2 -------
446
447 for (; ind2<=I2.Upper(); ind2++) {
448 Seq.Append(I2(ind2));
449 }
450 }
451
452 if (ind2>I2.Upper()) {
453//----- Ici I2 est epuise, on complete avec la fin de I1 -------
454 for (; ind1<=I1.Upper(); ind1++) {
455 Seq.Append(I1(ind1));
456 }
457 }
458}
459
460
461//=======================================================================
462//function : EvalMaxParametricDistance
463//purpose :
464//=======================================================================
465
466void GeomLib::EvalMaxParametricDistance(const Adaptor3d_Curve& ACurve,
467 const Adaptor3d_Curve& AReferenceCurve,
468// const Standard_Real Tolerance,
469 const Standard_Real ,
470 const TColStd_Array1OfReal& Parameters,
471 Standard_Real& MaxDistance)
472{
473 Standard_Integer ii ;
474
475 Standard_Real max_squared = 0.0e0,
476// tolerance_squared,
477 local_distance_squared ;
478
479// tolerance_squared = Tolerance * Tolerance ;
480 gp_Pnt Point1 ;
481 gp_Pnt Point2 ;
482 for (ii = Parameters.Lower() ; ii <= Parameters.Upper() ; ii++) {
483 ACurve.D0(Parameters(ii),
484 Point1) ;
485 AReferenceCurve.D0(Parameters(ii),
486 Point2) ;
487 local_distance_squared =
488 Point1.SquareDistance (Point2) ;
489 max_squared = Max(max_squared,local_distance_squared) ;
490 }
491 if (max_squared > 0.0e0) {
492 MaxDistance = sqrt(max_squared) ;
493 }
494 else {
495 MaxDistance = 0.0e0 ;
496 }
497
498}
499//=======================================================================
500//function : EvalMaxDistanceAlongParameter
501//purpose :
502//=======================================================================
503
504void GeomLib::EvalMaxDistanceAlongParameter(const Adaptor3d_Curve& ACurve,
505 const Adaptor3d_Curve& AReferenceCurve,
506 const Standard_Real Tolerance,
507 const TColStd_Array1OfReal& Parameters,
508 Standard_Real& MaxDistance)
509{
510 Standard_Integer ii ;
511 Standard_Real max_squared = 0.0e0,
512 tolerance_squared = Tolerance * Tolerance,
513 other_parameter,
514 para_tolerance,
515 local_distance_squared ;
516 gp_Pnt Point1 ;
517 gp_Pnt Point2 ;
518
519
520
521 para_tolerance =
522 AReferenceCurve.Resolution(Tolerance) ;
523 other_parameter = Parameters(Parameters.Lower()) ;
524 ACurve.D0(other_parameter,
525 Point1) ;
526 Extrema_LocateExtPC a_projector(Point1,
527 AReferenceCurve,
528 other_parameter,
529 para_tolerance) ;
530 for (ii = Parameters.Lower() ; ii <= Parameters.Upper() ; ii++) {
531 ACurve.D0(Parameters(ii),
532 Point1) ;
533 AReferenceCurve.D0(Parameters(ii),
534 Point2) ;
535 local_distance_squared =
536 Point1.SquareDistance (Point2) ;
537
538 local_distance_squared =
539 Point1.SquareDistance (Point2) ;
540
541
542 if (local_distance_squared > tolerance_squared) {
543
544
545 a_projector.Perform(Point1,
546 other_parameter) ;
547 if (a_projector.IsDone()) {
548 other_parameter =
549 a_projector.Point().Parameter() ;
550 AReferenceCurve.D0(other_parameter,
551 Point2) ;
552 local_distance_squared =
553 Point1.SquareDistance (Point2) ;
554 }
555 else {
556 local_distance_squared = 0.0e0 ;
557 other_parameter = Parameters(ii) ;
558 }
559 }
560 else {
561 other_parameter = Parameters(ii) ;
562 }
563
564
565 max_squared = Max(max_squared,local_distance_squared) ;
566 }
567 if (max_squared > tolerance_squared) {
568 MaxDistance = sqrt(max_squared) ;
569 }
570 else {
571 MaxDistance = Tolerance ;
572 }
573}
574
575
576
577// Aliases:
578
579// Global data definitions:
580
581// Methods :
582
583
584//=======================================================================
585//function : To3d
586//purpose :
587//=======================================================================
588
589Handle(Geom_Curve) GeomLib::To3d (const gp_Ax2& Position,
590 const Handle(Geom2d_Curve)& Curve2d ) {
591 Handle(Geom_Curve) Curve3d;
592 Handle(Standard_Type) KindOfCurve = Curve2d->DynamicType();
593
594 if (KindOfCurve == STANDARD_TYPE (Geom2d_TrimmedCurve)) {
595 Handle(Geom2d_TrimmedCurve) Ct =
596 Handle(Geom2d_TrimmedCurve)::DownCast(Curve2d);
597 Standard_Real U1 = Ct->FirstParameter ();
598 Standard_Real U2 = Ct->LastParameter ();
599 Handle(Geom2d_Curve) CBasis2d = Ct->BasisCurve();
600 Handle(Geom_Curve) CC = GeomLib::To3d(Position, CBasis2d);
601 Curve3d = new Geom_TrimmedCurve (CC, U1, U2);
602 }
603 else if (KindOfCurve == STANDARD_TYPE (Geom2d_OffsetCurve)) {
604 Handle(Geom2d_OffsetCurve) Co =
605 Handle(Geom2d_OffsetCurve)::DownCast(Curve2d);
606 Standard_Real Offset = Co->Offset();
607 Handle(Geom2d_Curve) CBasis2d = Co->BasisCurve();
608 Handle(Geom_Curve) CC = GeomLib::To3d(Position, CBasis2d);
609 Curve3d = new Geom_OffsetCurve (CC, Offset, Position.Direction());
610 }
611 else if (KindOfCurve == STANDARD_TYPE (Geom2d_BezierCurve)) {
612 Handle(Geom2d_BezierCurve) CBez2d =
613 Handle(Geom2d_BezierCurve)::DownCast (Curve2d);
614 Standard_Integer Nbpoles = CBez2d->NbPoles ();
615 TColgp_Array1OfPnt2d Poles2d (1, Nbpoles);
616 CBez2d->Poles (Poles2d);
617 TColgp_Array1OfPnt Poles3d (1, Nbpoles);
618 for (Standard_Integer i = 1; i <= Nbpoles; i++) {
619 Poles3d (i) = ElCLib::To3d (Position, Poles2d (i));
620 }
621 Handle(Geom_BezierCurve) CBez3d;
622 if (CBez2d->IsRational()) {
623 TColStd_Array1OfReal TheWeights (1, Nbpoles);
624 CBez2d->Weights (TheWeights);
625 CBez3d = new Geom_BezierCurve (Poles3d, TheWeights);
626 }
627 else {
628 CBez3d = new Geom_BezierCurve (Poles3d);
629 }
630 Curve3d = CBez3d;
631 }
632 else if (KindOfCurve == STANDARD_TYPE (Geom2d_BSplineCurve)) {
633 Handle(Geom2d_BSplineCurve) CBSpl2d =
634 Handle(Geom2d_BSplineCurve)::DownCast (Curve2d);
635 Standard_Integer Nbpoles = CBSpl2d->NbPoles ();
636 Standard_Integer Nbknots = CBSpl2d->NbKnots ();
637 Standard_Integer TheDegree = CBSpl2d->Degree ();
638 Standard_Boolean IsPeriodic = CBSpl2d->IsPeriodic();
639 TColgp_Array1OfPnt2d Poles2d (1, Nbpoles);
640 CBSpl2d->Poles (Poles2d);
641 TColgp_Array1OfPnt Poles3d (1, Nbpoles);
642 for (Standard_Integer i = 1; i <= Nbpoles; i++) {
643 Poles3d (i) = ElCLib::To3d (Position, Poles2d (i));
644 }
645 TColStd_Array1OfReal TheKnots (1, Nbknots);
646 TColStd_Array1OfInteger TheMults (1, Nbknots);
647 CBSpl2d->Knots (TheKnots);
648 CBSpl2d->Multiplicities (TheMults);
649 Handle(Geom_BSplineCurve) CBSpl3d;
650 if (CBSpl2d->IsRational()) {
651 TColStd_Array1OfReal TheWeights (1, Nbpoles);
652 CBSpl2d->Weights (TheWeights);
653 CBSpl3d = new Geom_BSplineCurve (Poles3d, TheWeights, TheKnots, TheMults, TheDegree, IsPeriodic);
654 }
655 else {
656 CBSpl3d = new Geom_BSplineCurve (Poles3d, TheKnots, TheMults, TheDegree, IsPeriodic);
657 }
658 Curve3d = CBSpl3d;
659 }
660 else if (KindOfCurve == STANDARD_TYPE (Geom2d_Line)) {
661 Handle(Geom2d_Line) Line2d = Handle(Geom2d_Line)::DownCast (Curve2d);
662 gp_Lin2d L2d = Line2d->Lin2d();
663 gp_Lin L3d = ElCLib::To3d (Position, L2d);
664 Handle(Geom_Line) GeomL3d = new Geom_Line (L3d);
665 Curve3d = GeomL3d;
666 }
667 else if (KindOfCurve == STANDARD_TYPE (Geom2d_Circle)) {
668 Handle(Geom2d_Circle) Circle2d =
669 Handle(Geom2d_Circle)::DownCast (Curve2d);
670 gp_Circ2d C2d = Circle2d->Circ2d();
671 gp_Circ C3d = ElCLib::To3d (Position, C2d);
672 Handle(Geom_Circle) GeomC3d = new Geom_Circle (C3d);
673 Curve3d = GeomC3d;
674 }
675 else if (KindOfCurve == STANDARD_TYPE (Geom2d_Ellipse)) {
676 Handle(Geom2d_Ellipse) Ellipse2d =
677 Handle(Geom2d_Ellipse)::DownCast (Curve2d);
678 gp_Elips2d E2d = Ellipse2d->Elips2d ();
679 gp_Elips E3d = ElCLib::To3d (Position, E2d);
680 Handle(Geom_Ellipse) GeomE3d = new Geom_Ellipse (E3d);
681 Curve3d = GeomE3d;
682 }
683 else if (KindOfCurve == STANDARD_TYPE (Geom2d_Parabola)) {
684 Handle(Geom2d_Parabola) Parabola2d =
685 Handle(Geom2d_Parabola)::DownCast (Curve2d);
686 gp_Parab2d Prb2d = Parabola2d->Parab2d ();
687 gp_Parab Prb3d = ElCLib::To3d (Position, Prb2d);
688 Handle(Geom_Parabola) GeomPrb3d = new Geom_Parabola (Prb3d);
689 Curve3d = GeomPrb3d;
690 }
691 else if (KindOfCurve == STANDARD_TYPE (Geom2d_Hyperbola)) {
692 Handle(Geom2d_Hyperbola) Hyperbola2d =
693 Handle(Geom2d_Hyperbola)::DownCast (Curve2d);
694 gp_Hypr2d H2d = Hyperbola2d->Hypr2d ();
695 gp_Hypr H3d = ElCLib::To3d (Position, H2d);
696 Handle(Geom_Hyperbola) GeomH3d = new Geom_Hyperbola (H3d);
697 Curve3d = GeomH3d;
698 }
699 else {
700 Standard_NotImplemented::Raise();
701 }
702
703 return Curve3d;
704}
705
706
707
708//=======================================================================
709//function : GTransform
710//purpose :
711//=======================================================================
712
713Handle(Geom2d_Curve) GeomLib::GTransform(const Handle(Geom2d_Curve)& Curve,
714 const gp_GTrsf2d& GTrsf)
715{
716 gp_TrsfForm Form = GTrsf.Form();
717
718 if ( Form != gp_Other) {
719
720 // Alors, la GTrsf est en fait une Trsf.
721 // La geometrie des courbes sera alors inchangee.
722
723 Handle(Geom2d_Curve) C =
724 Handle(Geom2d_Curve)::DownCast(Curve->Transformed(GTrsf.Trsf2d()));
725 return C;
726 }
727 else {
728
729 // Alors, la GTrsf est une other Transformation.
730 // La geometrie des courbes est alors changee, et les conics devront
731 // etre converties en BSplines.
732
733 Handle(Standard_Type) TheType = Curve->DynamicType();
734
735 if ( TheType == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
736
737 // On va recurer sur la BasisCurve
738
739 Handle(Geom2d_TrimmedCurve) C =
740 Handle(Geom2d_TrimmedCurve)::DownCast(Curve->Copy());
741
742 Handle(Standard_Type) TheBasisType = (C->BasisCurve())->DynamicType();
743
744 if (TheBasisType == STANDARD_TYPE(Geom2d_BSplineCurve) ||
745 TheBasisType == STANDARD_TYPE(Geom2d_BezierCurve) ) {
746
747 // Dans ces cas le parametrage est conserve sur la courbe transformee
748 // on peut donc la trimmer avec les parametres de la courbe de base.
749
750 Standard_Real U1 = C->FirstParameter();
751 Standard_Real U2 = C->LastParameter();
752
753 Handle(Geom2d_TrimmedCurve) result =
754 new Geom2d_TrimmedCurve(GTransform(C->BasisCurve(), GTrsf), U1,U2);
755 return result;
756 }
757 else if ( TheBasisType == STANDARD_TYPE(Geom2d_Line)) {
758
759 // Dans ce cas, le parametrage n`est plus conserve.
760 // Il faut recalculer les parametres de Trimming sur la courbe
761 // resultante. ( Calcul par projection ( ElCLib) des points debut
762 // et fin transformes)
763
764 Handle(Geom2d_Line) L =
765 Handle(Geom2d_Line)::DownCast(GTransform(C->BasisCurve(), GTrsf));
766 gp_Lin2d Lin = L->Lin2d();
767
768 gp_Pnt2d P1 = C->StartPoint();
769 gp_Pnt2d P2 = C->EndPoint();
770 P1.SetXY(GTrsf.Transformed(P1.XY()));
771 P2.SetXY(GTrsf.Transformed(P2.XY()));
772 Standard_Real U1 = ElCLib::Parameter(Lin,P1);
773 Standard_Real U2 = ElCLib::Parameter(Lin,P2);
774
775 Handle(Geom2d_TrimmedCurve) result =
776 new Geom2d_TrimmedCurve(L,U1,U2);
777 return result;
778 }
779 else if (TheBasisType == STANDARD_TYPE(Geom2d_Circle) ||
780 TheBasisType == STANDARD_TYPE(Geom2d_Ellipse) ||
781 TheBasisType == STANDARD_TYPE(Geom2d_Parabola) ||
782 TheBasisType == STANDARD_TYPE(Geom2d_Hyperbola) ) {
783
784 // Dans ces cas, la geometrie de la courbe n`est pas conservee
785 // on la convertir en BSpline avant de lui appliquer la Trsf.
786
787 Handle(Geom2d_BSplineCurve) BS =
788 Geom2dConvert::CurveToBSplineCurve(C);
789 return GTransform(BS,GTrsf);
790 }
791 else {
792
793 // La transformee d`une OffsetCurve vaut ????? Sais pas faire !!
794
795 Handle(Geom2d_Curve) dummy;
796 return dummy;
797 }
798 }
799 else if ( TheType == STANDARD_TYPE(Geom2d_Line)) {
800
801 Handle(Geom2d_Line) L =
802 Handle(Geom2d_Line)::DownCast(Curve->Copy());
803 gp_Lin2d Lin = L->Lin2d();
804 gp_Pnt2d P = Lin.Location();
805 gp_Pnt2d PP = L->Value(10.); // pourquoi pas !!
806 P.SetXY(GTrsf.Transformed(P.XY()));
807 PP.SetXY(GTrsf.Transformed(PP.XY()));
808 L->SetLocation(P);
809 gp_Vec2d V(P,PP);
810 L->SetDirection(gp_Dir2d(V));
811 return L;
812 }
813 else if ( TheType == STANDARD_TYPE(Geom2d_BezierCurve)) {
814
815 // Les GTrsf etant des operation lineaires, la transformee d`une courbe
816 // a poles est la courbe dont les poles sont la transformee des poles
817 // de la courbe de base.
818
819 Handle(Geom2d_BezierCurve) C =
820 Handle(Geom2d_BezierCurve)::DownCast(Curve->Copy());
821 Standard_Integer NbPoles = C->NbPoles();
822 TColgp_Array1OfPnt2d Poles(1,NbPoles);
823 C->Poles(Poles);
824 for ( Standard_Integer i = 1; i <= NbPoles; i++) {
825 Poles(i).SetXY(GTrsf.Transformed(Poles(i).XY()));
826 C->SetPole(i,Poles(i));
827 }
828 return C;
829 }
830 else if ( TheType == STANDARD_TYPE(Geom2d_BSplineCurve)) {
831
832 // Voir commentaire pour les Bezier.
833
834 Handle(Geom2d_BSplineCurve) C =
835 Handle(Geom2d_BSplineCurve)::DownCast(Curve->Copy());
836 Standard_Integer NbPoles = C->NbPoles();
837 TColgp_Array1OfPnt2d Poles(1,NbPoles);
838 C->Poles(Poles);
839 for ( Standard_Integer i = 1; i <= NbPoles; i++) {
840 Poles(i).SetXY(GTrsf.Transformed(Poles(i).XY()));
841 C->SetPole(i,Poles(i));
842 }
843 return C;
844 }
845 else if ( TheType == STANDARD_TYPE(Geom2d_Circle) ||
846 TheType == STANDARD_TYPE(Geom2d_Ellipse) ) {
847
848 // Dans ces cas, la geometrie de la courbe n`est pas conservee
849 // on la convertir en BSpline avant de lui appliquer la Trsf.
850
851 Handle(Geom2d_BSplineCurve) C =
852 Geom2dConvert::CurveToBSplineCurve(Curve);
853 return GTransform(C, GTrsf);
854 }
855 else if ( TheType == STANDARD_TYPE(Geom2d_Parabola) ||
856 TheType == STANDARD_TYPE(Geom2d_Hyperbola) ||
857 TheType == STANDARD_TYPE(Geom2d_OffsetCurve) ) {
858
859 // On ne sait pas faire : return a null Handle;
860
861 Handle(Geom2d_Curve) dummy;
862 return dummy;
863 }
864 }
865
866 Handle(Geom2d_Curve) WNT__; // portage Windows.
867 return WNT__;
868}
869
870
871//=======================================================================
872//function : SameRange
873//purpose :
874//=======================================================================
875void GeomLib::SameRange(const Standard_Real Tolerance,
876 const Handle(Geom2d_Curve)& CurvePtr,
877 const Standard_Real FirstOnCurve,
878 const Standard_Real LastOnCurve,
879 const Standard_Real RequestedFirst,
880 const Standard_Real RequestedLast,
881 Handle(Geom2d_Curve)& NewCurvePtr)
882{
883 if(CurvePtr.IsNull()) Standard_Failure::Raise();
884 if (Abs(LastOnCurve - RequestedLast) <= Tolerance &&
54f91e03 885 Abs(FirstOnCurve - RequestedFirst) <= Tolerance)
886 {
887 NewCurvePtr = CurvePtr;
888 return;
7fd59977 889 }
890
891 // the parametrisation lentgh must at least be the same.
892 if (Abs(LastOnCurve - FirstOnCurve - RequestedLast + RequestedFirst)
54f91e03 893 <= Tolerance)
894 {
895 if (CurvePtr->IsKind(STANDARD_TYPE(Geom2d_Line)))
896 {
7fd59977 897 Handle(Geom2d_Line) Line =
54f91e03 898 Handle(Geom2d_Line)::DownCast(CurvePtr->Copy());
7fd59977 899 Standard_Real dU = FirstOnCurve - RequestedFirst;
900 gp_Dir2d D = Line->Direction() ;
901 Line->Translate(dU * gp_Vec2d(D));
902 NewCurvePtr = Line;
903 }
54f91e03 904 else if (CurvePtr->IsKind(STANDARD_TYPE(Geom2d_Circle)))
905 {
7fd59977 906 gp_Trsf2d Trsf;
907 NewCurvePtr = Handle(Geom2d_Curve)::DownCast(CurvePtr->Copy());
908 Handle(Geom2d_Circle) Circ =
54f91e03 909 Handle(Geom2d_Circle)::DownCast(NewCurvePtr);
7fd59977 910 gp_Pnt2d P = Circ->Location();
911 Standard_Real dU;
912 if (Circ->Circ2d().IsDirect()) {
54f91e03 913 dU = FirstOnCurve - RequestedFirst;
7fd59977 914 }
915 else {
54f91e03 916 dU = RequestedFirst - FirstOnCurve;
7fd59977 917 }
918 Trsf.SetRotation(P,dU);
919 NewCurvePtr->Transform(Trsf) ;
920 }
54f91e03 921 else if (CurvePtr->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)))
922 {
7fd59977 923 Handle(Geom2d_TrimmedCurve) TC =
54f91e03 924 Handle(Geom2d_TrimmedCurve)::DownCast(CurvePtr);
7fd59977 925 GeomLib::SameRange(Tolerance,
54f91e03 926 TC->BasisCurve(),
927 FirstOnCurve , LastOnCurve,
928 RequestedFirst, RequestedLast,
929 NewCurvePtr);
7fd59977 930 NewCurvePtr = new Geom2d_TrimmedCurve( NewCurvePtr, RequestedFirst, RequestedLast );
931 }
54f91e03 932 //
933 // attention a des problemes de limitation : utiliser le MEME test que dans
934 // Geom2d_TrimmedCurve::SetTrim car sinon comme on risque de relimite sur
935 // RequestedFirst et RequestedLast on aura un probleme
936 //
937 //
7fd59977 938 else if (Abs(LastOnCurve - FirstOnCurve) > Precision::PConfusion() ||
54f91e03 939 Abs(RequestedLast + RequestedFirst) > Precision::PConfusion())
940 {
941
7fd59977 942 Handle(Geom2d_TrimmedCurve) TC =
54f91e03 943 new Geom2d_TrimmedCurve(CurvePtr,FirstOnCurve,LastOnCurve);
944
7fd59977 945 Handle(Geom2d_BSplineCurve) BS =
54f91e03 946 Geom2dConvert::CurveToBSplineCurve(TC);
7fd59977 947 TColStd_Array1OfReal Knots(1,BS->NbKnots());
948 BS->Knots(Knots);
54f91e03 949
7fd59977 950 BSplCLib::Reparametrize(RequestedFirst,RequestedLast,Knots);
54f91e03 951
7fd59977 952 BS->SetKnots(Knots);
953 NewCurvePtr = BS;
954 }
7fd59977 955 }
54f91e03 956 else
957 { // On segmente le resultat
958 Standard_Real Udeb = Max(CurvePtr->FirstParameter(), FirstOnCurve);
959 Standard_Real Ufin = Min(CurvePtr->LastParameter(), LastOnCurve);
7fd59977 960 Handle(Geom2d_TrimmedCurve) TC =
54f91e03 961 new Geom2d_TrimmedCurve( CurvePtr, Udeb, Ufin );
905522ee 962 //
7fd59977 963 Handle(Geom2d_BSplineCurve) BS =
964 Geom2dConvert::CurveToBSplineCurve(TC);
7fd59977 965 TColStd_Array1OfReal Knots(1,BS->NbKnots());
966 BS->Knots(Knots);
54f91e03 967
7fd59977 968 BSplCLib::Reparametrize(RequestedFirst,RequestedLast,Knots);
54f91e03 969
7fd59977 970 BS->SetKnots(Knots);
971 NewCurvePtr = BS;
972 }
973}
974
975//=======================================================================
976//class : GeomLib_CurveOnSurfaceEvaluator
977//purpose: The evaluator for the Curve 3D building
978//=======================================================================
979
980class GeomLib_CurveOnSurfaceEvaluator : public AdvApprox_EvaluatorFunction
981{
982 public:
983 GeomLib_CurveOnSurfaceEvaluator (Adaptor3d_CurveOnSurface& theCurveOnSurface,
984 Standard_Real theFirst, Standard_Real theLast)
985 : CurveOnSurface(theCurveOnSurface), FirstParam(theFirst), LastParam(theLast) {}
986
987 virtual void Evaluate (Standard_Integer *Dimension,
988 Standard_Real StartEnd[2],
989 Standard_Real *Parameter,
990 Standard_Integer *DerivativeRequest,
991 Standard_Real *Result, // [Dimension]
992 Standard_Integer *ErrorCode);
993
994 private:
995 Adaptor3d_CurveOnSurface& CurveOnSurface;
996 Standard_Real FirstParam;
997 Standard_Real LastParam;
998
999 Handle(Adaptor3d_HCurve) TrimCurve;
1000};
1001
1002void GeomLib_CurveOnSurfaceEvaluator::Evaluate (Standard_Integer *,/*Dimension*/
1003 Standard_Real DebutFin[2],
1004 Standard_Real *Parameter,
1005 Standard_Integer *DerivativeRequest,
1006 Standard_Real *Result,// [Dimension]
1007 Standard_Integer *ReturnCode)
a7f510bf 1008{
1009 gp_Pnt Point;
7fd59977 1010
1011 //Gestion des positionnements gauche / droite
1012 if ((DebutFin[0] != FirstParam) || (DebutFin[1] != LastParam))
1013 {
1014 TrimCurve = CurveOnSurface.Trim(DebutFin[0], DebutFin[1], Precision::PConfusion());
1015 FirstParam = DebutFin[0];
1016 LastParam = DebutFin[1];
1017 }
1018
1019 //Positionemment
1020 if (*DerivativeRequest == 0)
1021 {
1022 TrimCurve->D0((*Parameter), Point) ;
1023
a7f510bf 1024 for (Standard_Integer ii = 0 ; ii < 3 ; ii++)
7fd59977 1025 Result[ii] = Point.Coord(ii + 1);
1026 }
1027 if (*DerivativeRequest == 1)
1028 {
1029 gp_Vec Vector;
1030 TrimCurve->D1((*Parameter), Point, Vector);
a7f510bf 1031 for (Standard_Integer ii = 0 ; ii < 3 ; ii++)
7fd59977 1032 Result[ii] = Vector.Coord(ii + 1) ;
1033 }
1034 if (*DerivativeRequest == 2)
1035 {
1036 gp_Vec Vector, VecBis;
1037 TrimCurve->D2((*Parameter), Point, VecBis, Vector);
a7f510bf 1038 for (Standard_Integer ii = 0 ; ii < 3 ; ii++)
7fd59977 1039 Result[ii] = Vector.Coord(ii + 1) ;
1040 }
1041 ReturnCode[0] = 0;
1042}
1043
1044//=======================================================================
1045//function : BuildCurve3d
1046//purpose :
1047//=======================================================================
1048
1049void GeomLib::BuildCurve3d(const Standard_Real Tolerance,
1050 Adaptor3d_CurveOnSurface& Curve,
1051 const Standard_Real FirstParameter,
1052 const Standard_Real LastParameter,
857ffd5e 1053 Handle(Geom_Curve)& NewCurvePtr,
7fd59977 1054 Standard_Real& MaxDeviation,
1055 Standard_Real& AverageDeviation,
1056 const GeomAbs_Shape Continuity,
1057 const Standard_Integer MaxDegree,
1058 const Standard_Integer MaxSegment)
1059
1060{
1061
1062
1063 Standard_Integer curve_not_computed = 1 ;
1064 MaxDeviation = 0.0e0 ;
1065 AverageDeviation = 0.0e0 ;
c5f3a425 1066 Handle(GeomAdaptor_HSurface) geom_adaptor_surface_ptr (Handle(GeomAdaptor_HSurface)::DownCast(Curve.GetSurface()) );
1067 Handle(Geom2dAdaptor_HCurve) geom_adaptor_curve_ptr (Handle(Geom2dAdaptor_HCurve)::DownCast(Curve.GetCurve()) );
7fd59977 1068
1069 if (! geom_adaptor_curve_ptr.IsNull() &&
1070 ! geom_adaptor_surface_ptr.IsNull()) {
1071 Handle(Geom_Plane) P ;
1072 const GeomAdaptor_Surface & geom_surface =
1073 * (GeomAdaptor_Surface *) &geom_adaptor_surface_ptr->Surface() ;
1074
1075 Handle(Geom_RectangularTrimmedSurface) RT =
1076 Handle(Geom_RectangularTrimmedSurface)::
1077 DownCast(geom_surface.Surface());
1078 if ( RT.IsNull()) {
1079 P = Handle(Geom_Plane)::DownCast(geom_surface.Surface());
1080 }
1081 else {
1082 P = Handle(Geom_Plane)::DownCast(RT->BasisSurface());
1083 }
1084
1085
1086 if (! P.IsNull()) {
1087 // compute the 3d curve
1088 gp_Ax2 axes = P->Position().Ax2();
1089 const Geom2dAdaptor_Curve & geom2d_curve =
1090 * (Geom2dAdaptor_Curve *) & geom_adaptor_curve_ptr->Curve2d() ;
1091 NewCurvePtr =
1092 GeomLib::To3d(axes,
1093 geom2d_curve.Curve());
1094 curve_not_computed = 0 ;
1095
1096 }
1097 }
1098 if (curve_not_computed) {
1099
1100 //
1101 // Entree
1102 //
1103 Handle(TColStd_HArray1OfReal) Tolerance1DPtr,Tolerance2DPtr;
1104 Handle(TColStd_HArray1OfReal) Tolerance3DPtr =
1105 new TColStd_HArray1OfReal(1,1) ;
1106 Tolerance3DPtr->SetValue(1,Tolerance);
1107
1108 // Recherche des discontinuitees
1109 Standard_Integer NbIntervalC2 = Curve.NbIntervals(GeomAbs_C2);
1110 TColStd_Array1OfReal Param_de_decoupeC2 (1, NbIntervalC2+1);
1111 Curve.Intervals(Param_de_decoupeC2, GeomAbs_C2);
1112
1113 Standard_Integer NbIntervalC3 = Curve.NbIntervals(GeomAbs_C3);
1114 TColStd_Array1OfReal Param_de_decoupeC3 (1, NbIntervalC3+1);
1115 Curve.Intervals(Param_de_decoupeC3, GeomAbs_C3);
1116
1117 // Note extension of the parameteric range
1118 // Pour forcer le Trim au premier appel de l'evaluateur
1119 GeomLib_CurveOnSurfaceEvaluator ev (Curve, FirstParameter - 1., LastParameter + 1.);
1120
1121 // Approximation avec decoupe preferentiel
1122 AdvApprox_PrefAndRec Preferentiel(Param_de_decoupeC2,
1123 Param_de_decoupeC3);
1124 AdvApprox_ApproxAFunction anApproximator(0,
1125 0,
1126 1,
1127 Tolerance1DPtr,
1128 Tolerance2DPtr,
1129 Tolerance3DPtr,
1130 FirstParameter,
1131 LastParameter,
1132 Continuity,
1133 MaxDegree,
1134 MaxSegment,
1135 ev,
1136// CurveOnSurfaceEvaluator,
1137 Preferentiel) ;
1138
1139 if (anApproximator.HasResult()) {
1140 GeomLib_MakeCurvefromApprox
1141 aCurveBuilder(anApproximator) ;
1142
1143 Handle(Geom_BSplineCurve) aCurvePtr =
1144 aCurveBuilder.Curve(1) ;
1145 // On rend les resultats de l'approx
1146 MaxDeviation = anApproximator.MaxError(3,1) ;
1147 AverageDeviation = anApproximator.AverageError(3,1) ;
1148 NewCurvePtr = aCurvePtr ;
1149 }
1150 }
1151 }
1152
1153//=======================================================================
1154//function : AdjustExtremity
1155//purpose :
1156//=======================================================================
1157
1158void GeomLib::AdjustExtremity(Handle(Geom_BoundedCurve)& Curve,
1159 const gp_Pnt& P1,
1160 const gp_Pnt& P2,
1161 const gp_Vec& T1,
1162 const gp_Vec& T2)
1163{
1164// il faut Convertir l'entree (en preservant si possible le parametrage)
1165 Handle(Geom_BSplineCurve) aIn, aDef;
1166 aIn = GeomConvert::CurveToBSplineCurve(Curve, Convert_QuasiAngular);
1167
1168 Standard_Integer ii, jj;
1169 gp_Pnt P;
1170 gp_Vec V, Vtan, DV;
1171 TColgp_Array1OfPnt PolesDef(1,4), Coeffs(1,4);
1172 TColStd_Array1OfReal FK(1, 8);
1173 TColStd_Array1OfReal Ti(1, 4);
1174 TColStd_Array1OfInteger Contact(1, 4);
1175
1176 Ti(1) = Ti(2) = aIn->FirstParameter();
1177 Ti(3) = Ti(4) = aIn->LastParameter();
1178 Contact(1) = Contact(3) = 0;
1179 Contact(2) = Contact(4) = 1;
1180 for (ii=1; ii<=4; ii++) {
1181 FK(ii) = aIn->FirstParameter();
1182 FK(ii) = aIn->LastParameter();
1183 }
1184
1185 // Calculs des contraintes de deformations
1186 aIn->D1(Ti(1), P, V);
1187 PolesDef(1).ChangeCoord() = P1.XYZ()-P.XYZ();
1188 Vtan = T1;
1189 Vtan.Normalize();
1190 DV = Vtan * (Vtan * V) - V;
1191 PolesDef(2).ChangeCoord() = (Ti(4)-Ti(1))*DV.XYZ();
1192
1193 aIn->D1(Ti(4), P, V);
1194 PolesDef(3).ChangeCoord() = P2.XYZ()-P.XYZ();
1195 Vtan = T2;
1196 Vtan.Normalize();
1197 DV = Vtan * (Vtan * V) - V;
1198 PolesDef(4).ChangeCoord() = (Ti(4)-Ti(1))* DV.XYZ();
1199
1200 // Interpolation des contraintes
1201 math_Matrix Mat(1, 4, 1, 4);
1202 if (!PLib::HermiteCoefficients(0., 1., 1, 1, Mat))
1203 Standard_ConstructionError::Raise();
1204
1205 for (jj=1; jj<=4; jj++) {
1206 gp_XYZ aux(0.,0.,0.);
1207 for (ii=1; ii<=4; ii++) {
1208 aux.SetLinearForm(Mat(ii,jj), PolesDef(ii).XYZ(), aux);
1209 }
1210 Coeffs(jj).SetXYZ(aux);
1211 }
1212
1213 PLib::CoefficientsPoles(Coeffs, PLib::NoWeights(),
1214 PolesDef, PLib::NoWeights());
1215
1216 // Ajout de la deformation
1217 TColStd_Array1OfReal K(1, 2);
1218 TColStd_Array1OfInteger M(1, 2);
1219 K(1) = Ti(1);
1220 K(2) = Ti(4);
1221 M.Init(4);
1222
1223 aDef = new (Geom_BSplineCurve) (PolesDef, K, M, 3);
1224 if (aIn->Degree() < 3) aIn->IncreaseDegree(3);
1225 else aDef->IncreaseDegree(aIn->Degree());
1226
1227 for (ii=2; ii<aIn->NbKnots(); ii++) {
1228 aDef->InsertKnot(aIn->Knot(ii), aIn->Multiplicity(ii));
1229 }
1230
1231 if (aDef->NbPoles() != aIn->NbPoles())
1232 Standard_ConstructionError::Raise("Inconsistent poles's number");
1233
1234 for (ii=1; ii<=aDef->NbPoles(); ii++) {
1235 P = aIn->Pole(ii);
1236 P.ChangeCoord() += aDef->Pole(ii).XYZ();
1237 aIn->SetPole(ii, P);
1238 }
1239 Curve = aIn;
1240}
1241//=======================================================================
1242//function : ExtendCurveToPoint
1243//purpose :
1244//=======================================================================
1245
1246void GeomLib::ExtendCurveToPoint(Handle(Geom_BoundedCurve)& Curve,
1247 const gp_Pnt& Point,
1248 const Standard_Integer Continuity,
1249 const Standard_Boolean After)
1250{
1251 if(Continuity < 1 || Continuity > 3) return;
1252 Standard_Integer size = Continuity + 2;
1253 Standard_Real Ubord, Tol=1.e-6;
1254 math_Matrix MatCoefs(1,size, 1,size);
1255 Standard_Real Lambda, L1;
1256 Standard_Integer ii, jj;
1257 gp_Vec d1, d2, d3;
1258 gp_Pnt p0;
1259// il faut Convertir l'entree (en preservant si possible le parametrage)
1260 GeomConvert_CompCurveToBSplineCurve Concat(Curve, Convert_QuasiAngular);
1261
1262// Les contraintes de constructions
1263 TColgp_Array1OfXYZ Cont(1,size);
1264 if (After) {
1265 Ubord = Curve->LastParameter();
1266
1267 }
1268 else {
1269 Ubord = Curve->FirstParameter();
1270 }
1271 PLib::HermiteCoefficients(0, 1, // Les Bornes
1272 Continuity, 0, // Les Ordres de contraintes
1273 MatCoefs);
1274
1275 Curve->D3(Ubord, p0, d1, d2, d3);
1276 if (!After) { // Inversion du parametrage
1277 d1 *= -1;
1278 d3 *= -1;
1279 }
1280
1281 L1 = p0.Distance(Point);
1282 if (L1 > Tol) {
1283 // Lambda est le ratio qu'il faut appliquer a la derive de la courbe
1284 // pour obtenir la derive du prolongement (fixe arbitrairement a la
1285 // longueur du segment bout de la courbe - point cible.
1286 // On essai d'avoir sur le prolongement la vitesse moyenne que l'on
1287 // a sur la courbe.
1288 gp_Vec daux;
1289 gp_Pnt pp;
1290 Standard_Real f= Curve->FirstParameter(), t, dt, norm;
1291 dt = (Curve->LastParameter()-f)/9;
1292 norm = d1.Magnitude();
1293 for (ii=1, t=f+dt; ii<=8; ii++, t+=dt) {
1294 Curve->D1(t, pp, daux);
1295 norm += daux.Magnitude();
1296 }
1297 norm /= 9;
1298 dt = d1.Magnitude() / norm;
1299 if ((dt<1.5) && (dt>0.75)) { // Le bord est dans la moyenne on le garde
1300 Lambda = ((Standard_Real)1) / Max (d1.Magnitude() / L1, Tol);
1301 }
1302 else {
1303 Lambda = ((Standard_Real)1) / Max (norm / L1, Tol);
1304 }
1305 }
1306 else {
1307 return; // Pas d'extension
1308 }
1309
1310 // Optimisation du Lambda
1311 math_Matrix Cons(1, 3, 1, size);
1312 Cons(1,1) = p0.X(); Cons(2,1) = p0.Y(); Cons(3,1) = p0.Z();
1313 Cons(1,2) = d1.X(); Cons(2,2) = d1.Y(); Cons(3,2) = d1.Z();
1314 Cons(1,size) = Point.X(); Cons(2,size) = Point.Y(); Cons(3,size) = Point.Z();
1315 if (Continuity >= 2) {
1316 Cons(1,3) = d2.X(); Cons(2,3) = d2.Y(); Cons(3,3) = d2.Z();
1317 }
1318 if (Continuity >= 3) {
1319 Cons(1,4) = d3.X(); Cons(2,4) = d3.Y(); Cons(3,4) = d3.Z();
1320 }
1321 ComputeLambda(Cons, MatCoefs, L1, Lambda);
1322
1323 // Construction dans la Base Polynomiale
1324 Cont(1) = p0.XYZ();
1325 Cont(2) = d1.XYZ() * Lambda;
1326 if(Continuity >= 2) Cont(3) = d2.XYZ() * Pow(Lambda,2);
1327 if(Continuity >= 3) Cont(4) = d3.XYZ() * Pow(Lambda,3);
1328 Cont(size) = Point.XYZ();
1329
1330
1331 TColgp_Array1OfPnt ExtrapPoles(1, size);
1332 TColgp_Array1OfPnt ExtraCoeffs(1, size);
1333
1334 gp_Pnt PNull(0.,0.,0.);
1335 ExtraCoeffs.Init(PNull);
1336 for (ii=1; ii<=size; ii++) {
1337 for (jj=1; jj<=size; jj++) {
1338 ExtraCoeffs(jj).ChangeCoord() += MatCoefs(ii,jj)*Cont(ii);
1339 }
1340 }
1341
1342 // Convertion Dans la Base de Bernstein
1343 PLib::CoefficientsPoles(ExtraCoeffs, PLib::NoWeights(),
1344 ExtrapPoles, PLib::NoWeights());
1345
1346 Handle(Geom_BezierCurve) Bezier = new (Geom_BezierCurve) (ExtrapPoles);
1347
1348 Standard_Real dist = ExtrapPoles(1).Distance(p0);
1349 Standard_Boolean Ok;
1350 Tol += dist;
1351
1352 // Concatenation
1353 Ok = Concat.Add(Bezier, Tol, After);
1354 if (!Ok) Standard_ConstructionError::Raise("ExtendCurveToPoint");
1355
1356 Curve = Concat.BSplineCurve();
1357}
1358
1359
1360//=======================================================================
1361//function : ExtendKPart
1362//purpose : Extension par longueur des surfaces cannonique
1363//=======================================================================
1364static Standard_Boolean
1365ExtendKPart(Handle(Geom_RectangularTrimmedSurface)& Surface,
1366 const Standard_Real Length,
1367 const Standard_Boolean InU,
1368 const Standard_Boolean After)
1369{
1370
1371 if (Surface.IsNull()) return Standard_False;
1372
1373 Standard_Boolean Ok=Standard_True;
1374 Standard_Real Uf, Ul, Vf, Vl;
1375 Handle(Geom_Surface) Support = Surface->BasisSurface();
1376 GeomAbs_SurfaceType Type;
1377
1378 Surface->Bounds(Uf, Ul, Vf, Vl);
1379 GeomAdaptor_Surface AS(Surface);
1380 Type = AS.GetType();
1381
1382 if (InU) {
1383 switch(Type) {
1384 case GeomAbs_Plane :
1385 {
1386 if (After) Ul+=Length;
1387 else Uf-=Length;
1388 Surface = new (Geom_RectangularTrimmedSurface)
1389 (Support, Uf, Ul, Vf, Vl);
1390 break;
1391 }
1392
1393 default:
1394 Ok = Standard_False;
1395 }
1396 }
1397 else {
1398 switch(Type) {
1399 case GeomAbs_Plane :
1400 case GeomAbs_Cylinder :
1401 case GeomAbs_SurfaceOfExtrusion :
1402 {
1403 if (After) Vl+=Length;
1404 else Vf-=Length;
1405 Surface = new (Geom_RectangularTrimmedSurface)
1406 (Support, Uf, Ul, Vf, Vl);
1407 break;
1408 }
1409 default:
1410 Ok = Standard_False;
1411 }
1412 }
1413
1414 return Ok;
1415}
1416
1417//=======================================================================
1418//function : ExtendSurfByLength
1419//purpose :
1420//=======================================================================
1421void GeomLib::ExtendSurfByLength(Handle(Geom_BoundedSurface)& Surface,
1422 const Standard_Real Length,
1423 const Standard_Integer Continuity,
1424 const Standard_Boolean InU,
1425 const Standard_Boolean After)
1426{
1427 if(Continuity < 0 || Continuity > 3) return;
1428 Standard_Integer Cont = Continuity;
1429
1430 // Kpart ?
1431 Handle(Geom_RectangularTrimmedSurface) TS =
1432 Handle(Geom_RectangularTrimmedSurface)::DownCast (Surface);
1433 if (ExtendKPart(TS,Length, InU, After) ) {
1434 Surface = TS;
1435 return;
1436 }
1437
1438// format BSplineSurface avec un degre suffisant pour la continuite voulue
1439 Handle(Geom_BSplineSurface) BS =
1440 Handle(Geom_BSplineSurface)::DownCast (Surface);
1441 if (BS.IsNull()) {
1442 //BS = GeomConvert::SurfaceToBSplineSurface(Surface);
1443 Standard_Real Tol = Precision::Confusion(); //1.e-4;
1444 GeomAbs_Shape UCont = GeomAbs_C1, VCont = GeomAbs_C1;
1445 Standard_Integer degU = 14, degV = 14;
1446 Standard_Integer nmax = 16;
543a9964 1447 Standard_Integer thePrec = 1;
1448 const Handle(Geom_Surface)& aSurf = Surface; // to resolve ambiguity
1449 GeomConvert_ApproxSurface theApprox(aSurf,Tol,UCont,VCont,degU,degV,nmax,thePrec);
7fd59977 1450 if (theApprox.HasResult())
1451 BS = theApprox.Surface();
1452 else
1453 BS = GeomConvert::SurfaceToBSplineSurface(Surface);
1454 }
1455 if (InU&&(BS->UDegree()<Continuity+1))
1456 BS->IncreaseDegree(Continuity+1,BS->VDegree());
1457 if (!InU&&(BS->VDegree()<Continuity+1))
1458 BS->IncreaseDegree(BS->UDegree(),Continuity+1);
1459
1460 // si BS etait periodique dans le sens de l'extension, elle ne le sera plus
1461 if ( (InU&&(BS->IsUPeriodic())) || (!InU&&(BS->IsVPeriodic())) ) {
1462 Standard_Real U0,U1,V0,V1;
1463 BS->Bounds(U0,U1,V0,V1);
1464 BS->Segment(U0,U1,V0,V1);
1465 }
1466
1467
47c580a7
A
1468// IFV Fix OCC bug 0022694 - wrong result extrapolating rational surfaces
1469// Standard_Boolean rational = ( InU && BS->IsURational() )
1470// || ( !InU && BS->IsVRational() ) ;
1471 Standard_Boolean rational = (BS->IsURational() || BS->IsVRational());
7fd59977 1472 Standard_Boolean NullWeight;
1473 Standard_Real EpsW = 10*Precision::PConfusion();
1474 Standard_Integer gap = 3;
1475 if ( rational ) gap++;
1476
1477
1478
1d47d8d0 1479 Standard_Integer Cdeg = 0, Cdim = 0, NbP = 0, Ksize = 0, Psize = 1;
7fd59977 1480 Standard_Integer ii, jj, ipole, Kount;
1481 Standard_Real Tbord, lambmin=Length;
1d47d8d0 1482 Standard_Real * Padr = NULL;
7fd59977 1483 Standard_Boolean Ok;
1484 Handle(TColStd_HArray1OfReal) FKnots, Point, lambda, Tgte, Poles;
1485
1486
1487
1488
1489 for (Kount=0, Ok=Standard_False; Kount<=2 && !Ok; Kount++) {
1490 // transformation de la surface en une BSpline non rationnelle a une variable
1491 // de degre UDegree ou VDegree et de dimension 3 ou 4 x NbVpoles ou NbUpoles
1492 // le nombre de poles egal a NbUpoles ou NbVpoles
1493 // ATTENTION : dans le cas rationnel, un point de coordonnees (x,y,z)
1494 // et de poids w devient un point de coordonnees (wx, wy, wz, w )
1495
1496
1497 if (InU) {
1498 Cdeg = BS->UDegree();
1499 NbP = BS->NbUPoles();
1500 Cdim = BS->NbVPoles() * gap;
1501 }
1502 else {
1503 Cdeg = BS->VDegree();
1504 NbP = BS->NbVPoles();
1505 Cdim = BS->NbUPoles() * gap;
1506 }
1507
1508 // les noeuds plats
1509 Ksize = NbP + Cdeg + 1;
1510 FKnots = new (TColStd_HArray1OfReal) (1,Ksize);
1511 if (InU)
1512 BS->UKnotSequence(FKnots->ChangeArray1());
1513 else
1514 BS->VKnotSequence(FKnots->ChangeArray1());
1515
1516 // le parametre du noeud de raccord
1517 if (After)
1518 Tbord = FKnots->Value(FKnots->Upper()-Cdeg);
1519 else
1520 Tbord = FKnots->Value(FKnots->Lower()+Cdeg);
1521
1522 // les poles
1523 Psize = Cdim * NbP;
1524 Poles = new (TColStd_HArray1OfReal) (1,Psize);
1525
1526 if (InU) {
1527 for (ii=1,ipole=1; ii<=NbP; ii++) {
1528 for (jj=1;jj<=BS->NbVPoles();jj++) {
1529 Poles->SetValue(ipole, BS->Pole(ii,jj).X());
1530 Poles->SetValue(ipole+1, BS->Pole(ii,jj).Y());
1531 Poles->SetValue(ipole+2, BS->Pole(ii,jj).Z());
1532 if (rational) Poles->SetValue(ipole+3, BS->Weight(ii,jj));
1533 ipole+=gap;
1534 }
1535 }
1536 }
1537 else {
1538 for (jj=1,ipole=1; jj<=NbP; jj++) {
1539 for (ii=1;ii<=BS->NbUPoles();ii++) {
1540 Poles->SetValue(ipole, BS->Pole(ii,jj).X());
1541 Poles->SetValue(ipole+1, BS->Pole(ii,jj).Y());
1542 Poles->SetValue(ipole+2, BS->Pole(ii,jj).Z());
1543 if (rational) Poles->SetValue(ipole+3, BS->Weight(ii,jj));
1544 ipole+=gap;
1545 }
1546 }
1547 }
1548 Padr = (Standard_Real *) &Poles->ChangeValue(1);
1549
1550 // calcul du point de raccord et de la tangente
1551 Point = new (TColStd_HArray1OfReal)(1,Cdim);
1552 Tgte = new (TColStd_HArray1OfReal)(1,Cdim);
1553 lambda = new (TColStd_HArray1OfReal)(1,Cdim);
1554
1555 Standard_Boolean periodic_flag = Standard_False ;
1556 Standard_Integer extrap_mode[2], derivative_request = Max(Continuity,1);
1557 extrap_mode[0] = extrap_mode[1] = Cdeg;
1558 TColStd_Array1OfReal Result(1, Cdim * (derivative_request+1)) ;
1559
1560 TColStd_Array1OfReal& tgte = Tgte->ChangeArray1();
1561 TColStd_Array1OfReal& point = Point->ChangeArray1();
1562 TColStd_Array1OfReal& lamb = lambda->ChangeArray1();
1563
1564 Standard_Real * Radr = (Standard_Real *) &Result(1) ;
1565
1566 BSplCLib::Eval(Tbord,periodic_flag,derivative_request,extrap_mode[0],
1567 Cdeg,FKnots->Array1(),Cdim,*Padr,*Radr);
1568 Ok = Standard_True;
1569 for (ii=1;ii<=Cdim;ii++) {
1570 point(ii) = Result(ii);
1571 tgte(ii) = Result(ii+Cdim);
1572 }
1573
1574 // calcul de la contrainte a atteindre
1575
1576 gp_Vec CurT, OldT;
1577
1578 Standard_Real NTgte, val, Tgtol = 1.e-12, OldN = 0.0;
1579 if (rational) {
1580 for (ii=gap;ii<=Cdim;ii+=gap) {
1581 tgte(ii) = 0.;
1582 }
1583 for (ii=gap;ii<=Cdim;ii+=gap) {
1584 CurT.SetCoord(tgte(ii-3),tgte(ii-2), tgte(ii-1));
1585 NTgte=CurT.Magnitude();
1586 if (NTgte>Tgtol) {
1587 val = Length/NTgte;
1588 // Attentions aux Cas ou le segment donne par les poles
1589 // est oppose au sens de la derive
1590 // Exemple: Certaine portions de tore.
1591 if ( (OldN > Tgtol) && (CurT.Angle(OldT) > 2)) {
1592 Ok = Standard_False;
1593 }
1594
1595 lamb(ii-1) = lamb(ii-2) = lamb(ii-3) = val;
1596 lamb(ii) = 0.;
1597 lambmin = Min(lambmin, val);
1598 }
1599 else {
1600 lamb(ii-1) = lamb(ii-2) = lamb(ii-3) = 0.;
1601 lamb(ii) = 0.;
1602 }
1603 OldT = CurT;
1604 OldN = NTgte;
1605 }
1606 }
1607 else {
1608 for (ii=gap;ii<=Cdim;ii+=gap) {
1609 CurT.SetCoord(tgte(ii-2),tgte(ii-1), tgte(ii));
1610 NTgte=CurT.Magnitude();
1611 if (NTgte>Tgtol) {
1612 val = Length/NTgte;
1613 // Attentions aux Cas ou le segment donne par les poles
1614 // est oppose au sens de la derive
1615 // Exemple: Certaine portion de tore.
1616 if ( (OldN > Tgtol) && (CurT.Angle(OldT) > 2)) {
1617 Ok = Standard_False;
1618 }
1619 lamb(ii) = lamb(ii-1) = lamb(ii-2) = val;
1620 lambmin = Min(lambmin, val);
1621 }
1622 else {
1623 lamb(ii) =lamb(ii-1) = lamb(ii-2) = 0.;
1624 }
1625 OldT = CurT;
1626 OldN = NTgte;
1627 }
1628 }
1629 if (!Ok && Kount<2) {
1630 // On augmente le degre de l'iso bord afin de rapprocher les poles de la surface
1631 // Et on ressaye
1632 if (InU) BS->IncreaseDegree(BS->UDegree(), BS->VDegree()+2);
1633 else BS->IncreaseDegree(BS->UDegree()+2, BS->VDegree());
1634 }
1635 }
1636
1637
1638 TColStd_Array1OfReal ConstraintPoint(1,Cdim);
1639 if (After) {
1640 for (ii=1;ii<=Cdim;ii++) {
1641 ConstraintPoint(ii) = Point->Value(ii) + lambda->Value(ii)*Tgte->Value(ii);
1642 }
1643 }
1644 else {
1645 for (ii=1;ii<=Cdim;ii++) {
1646 ConstraintPoint(ii) = Point->Value(ii) - lambda->Value(ii)*Tgte->Value(ii);
1647 }
1648 }
1649
1650// cas particulier du rationnel
1651 if (rational) {
1652 for (ipole=1;ipole<=Psize;ipole+=gap) {
1653 Poles->ChangeValue(ipole) *= Poles->Value(ipole+3);
1654 Poles->ChangeValue(ipole+1) *= Poles->Value(ipole+3);
1655 Poles->ChangeValue(ipole+2) *= Poles->Value(ipole+3);
1656 }
1657 for (ii=1;ii<=Cdim;ii+=gap) {
1658 ConstraintPoint(ii) *= ConstraintPoint(ii+3);
1659 ConstraintPoint(ii+1) *= ConstraintPoint(ii+3);
1660 ConstraintPoint(ii+2) *= ConstraintPoint(ii+3);
1661 }
1662 }
1663
1664// tableaux necessaires pour l'extension
1d47d8d0 1665 Standard_Integer Ksize2 = Ksize+Cdeg, NbPoles, NbKnots = 0;
7fd59977 1666 TColStd_Array1OfReal FK(1, Ksize2) ;
1667 Standard_Real * FKRadr = &FK(1);
1668
1669 Standard_Integer Psize2 = Psize+Cdeg*Cdim;
1670 TColStd_Array1OfReal PRes(1, Psize2) ;
1671 Standard_Real * PRadr = &PRes(1);
1672 Standard_Real ww;
1673 Standard_Boolean ExtOk = Standard_False;
1674 Handle(TColgp_HArray2OfPnt) NewPoles;
1675 Handle(TColStd_HArray2OfReal) NewWeights;
1676
1677
1678 for (Kount=1; Kount<=5 && !ExtOk; Kount++) {
1679 // extension
1680 BSplCLib::TangExtendToConstraint(FKnots->Array1(),
1681 lambmin,NbP,*Padr,
1682 Cdim,Cdeg,
1683 ConstraintPoint, Cont, After,
1684 NbPoles, NbKnots,*FKRadr, *PRadr);
1685
1686 // recopie des poles du resultat sous forme de points 3D et de poids
1687 Standard_Integer NU, NV, indice ;
1688 if (InU) {
1689 NU = NbPoles;
1690 NV = BS->NbVPoles();
1691 }
1692 else {
1693 NU = BS->NbUPoles();
1694 NV = NbPoles;
1695 }
1696
1697 NewPoles = new (TColgp_HArray2OfPnt)(1,NU,1,NV);
1698 TColgp_Array2OfPnt& NewP = NewPoles->ChangeArray2();
1699 NewWeights = new (TColStd_HArray2OfReal) (1,NU,1,NV);
1700 TColStd_Array2OfReal& NewW = NewWeights->ChangeArray2();
1701
1702 if (!rational) NewW.Init(1.);
1703 NullWeight= Standard_False;
1704
1705 if (InU) {
1706 for (ii=1; ii<=NU && !NullWeight; ii++) {
1707 for (jj=1; jj<=NV && !NullWeight; jj++) {
1708 indice = 1+(ii-1)*Cdim+(jj-1)*gap;
1709 NewP(ii,jj).SetCoord(1,PRes(indice));
1710 NewP(ii,jj).SetCoord(2,PRes(indice+1));
1711 NewP(ii,jj).SetCoord(3,PRes(indice+2));
1712 if (rational) {
1713 ww = PRes(indice+3);
94f71cad 1714 if (Abs(ww - 1.0) < EpsW)
1715 ww = 1.0;
7fd59977 1716 if (ww < EpsW) {
1717 NullWeight = Standard_True;
1718 }
1719 else {
1720 NewW(ii,jj) = ww;
1721 NewP(ii,jj).ChangeCoord() /= ww;
1722 }
1723 }
1724 }
1725 }
1726 }
1727 else {
1728 for (jj=1; jj<=NV && !NullWeight; jj++) {
1729 for (ii=1; ii<=NU && !NullWeight; ii++) {
1730 indice = 1+(ii-1)*gap+(jj-1)*Cdim;
1731 NewP(ii,jj).SetCoord(1,PRes(indice));
1732 NewP(ii,jj).SetCoord(2,PRes(indice+1));
1733 NewP(ii,jj).SetCoord(3,PRes(indice+2));
1734 if (rational) {
1735 ww = PRes(indice+3);
94f71cad 1736 if (Abs(ww - 1.0) < EpsW)
1737 ww = 1.0;
7fd59977 1738 if (ww < EpsW) {
1739 NullWeight = Standard_True;
1740 }
1741 else {
1742 NewW(ii,jj) = ww;
1743 NewP(ii,jj).ChangeCoord() /= ww;
1744 }
1745 }
1746 }
1747 }
1748 }
1749
1750 if (NullWeight) {
0797d9d3 1751#ifdef OCCT_DEBUG
7fd59977 1752 cout << "Echec de l'Extension rationnelle" << endl;
1753#endif
1754 lambmin /= 3.;
1755 NullWeight = Standard_False;
1756 }
1757 else {
1758 ExtOk = Standard_True;
1759 }
1760 }
1761
1762
1763// recopie des noeuds plats sous forme de noeuds avec leurs multiplicites
1764// calcul des degres du resultat
1765 Standard_Integer Usize = BS->NbUKnots(), Vsize = BS->NbVKnots(), UDeg, VDeg;
1766 if (InU)
1767 Usize++;
1768 else
1769 Vsize++;
1770 TColStd_Array1OfReal UKnots(1,Usize);
1771 TColStd_Array1OfReal VKnots(1,Vsize);
1772 TColStd_Array1OfInteger UMults(1,Usize);
1773 TColStd_Array1OfInteger VMults(1,Vsize);
1774 TColStd_Array1OfReal FKRes(1, NbKnots);
1775
1776 for (ii=1; ii<=NbKnots; ii++)
1777 FKRes(ii) = FK(ii);
1778
1779 if (InU) {
1780 BSplCLib::Knots(FKRes, UKnots, UMults);
1781 UDeg = Cdeg;
1782 UMults(Usize) = UDeg+1; // Petite verrue utile quand la continuite
1783 // n'est pas ok.
1784 BS->VKnots(VKnots);
1785 BS->VMultiplicities(VMults);
1786 VDeg = BS->VDegree();
1787 }
1788 else {
1789 BSplCLib::Knots(FKRes, VKnots, VMults);
1790 VDeg = Cdeg;
1791 VMults(Vsize) = VDeg+1;
1792 BS->UKnots(UKnots);
1793 BS->UMultiplicities(UMults);
1794 UDeg = BS->UDegree();
1795 }
1796
1797// construction de la surface BSpline resultat
1798 Handle(Geom_BSplineSurface) Res =
1799 new (Geom_BSplineSurface) (NewPoles->Array2(),
1800 NewWeights->Array2(),
1801 UKnots,VKnots,
1802 UMults,VMults,
1803 UDeg,VDeg,
1804 BS->IsUPeriodic(),
1805 BS->IsVPeriodic());
1806 Surface = Res;
1807}
1808
1809//=======================================================================
1810//function : Inertia
1811//purpose :
1812//=======================================================================
1813void GeomLib::Inertia(const TColgp_Array1OfPnt& Points,
1814 gp_Pnt& Bary,
1815 gp_Dir& XDir,
1816 gp_Dir& YDir,
1817 Standard_Real& Xgap,
1818 Standard_Real& Ygap,
1819 Standard_Real& Zgap)
1820{
1821 gp_XYZ GB(0., 0., 0.), Diff;
1822// gp_Vec A,B,C,D;
1823
1824 Standard_Integer i,nb=Points.Length();
1825 GB.SetCoord(0.,0.,0.);
1826 for (i=1; i<=nb; i++)
1827 GB += Points(i).XYZ();
1828
1829 GB /= nb;
1830
1831 math_Matrix M (1, 3, 1, 3);
1832 M.Init(0.);
1833 for (i=1; i<=nb; i++) {
1834 Diff.SetLinearForm(-1, Points(i).XYZ(), GB);
1835 M(1,1) += Diff.X() * Diff.X();
1836 M(2,2) += Diff.Y() * Diff.Y();
1837 M(3,3) += Diff.Z() * Diff.Z();
1838 M(1,2) += Diff.X() * Diff.Y();
1839 M(1,3) += Diff.X() * Diff.Z();
1840 M(2,3) += Diff.Y() * Diff.Z();
1841 }
1842
1843 M(2,1)=M(1,2) ;
1844 M(3,1)=M(1,3) ;
1845 M(3,2)=M(2,3) ;
1846
1847 M /= nb;
1848
1849 math_Jacobi J(M);
1850 if (!J.IsDone()) {
0797d9d3 1851#ifdef OCCT_DEBUG
7fd59977 1852 cout << "Erreur dans Jacobbi" << endl;
1853 M.Dump(cout);
1854#endif
1855 }
1856
1857 Standard_Real n1,n2,n3;
1858
1859 n1=J.Value(1);
1860 n2=J.Value(2);
1861 n3=J.Value(3);
1862
1863 Standard_Real r1 = Min(Min(n1,n2),n3), r2;
1864 Standard_Integer m1, m2, m3;
1865 if (r1==n1) {
1866 m1 = 1;
1867 r2 = Min(n2,n3);
1868 if (r2==n2) {
1869 m2 = 2;
1870 m3 = 3;
1871 }
1872 else {
1873 m2 = 3;
1874 m3 = 2;
1875 }
1876 }
1877 else {
1878 if (r1==n2) {
1879 m1 = 2 ;
1880 r2 = Min(n1,n3);
1881 if (r2==n1) {
1882 m2 = 1;
1883 m3 = 3;
1884 }
1885 else {
1886 m2 = 3;
1887 m3 = 1;
1888 }
1889 }
1890 else {
1891 m1 = 3 ;
1892 r2 = Min(n1,n2);
1893 if (r2==n1) {
1894 m2 = 1;
1895 m3 = 2;
1896 }
1897 else {
1898 m2 = 2;
1899 m3 = 1;
1900 }
1901 }
1902 }
1903
1904 math_Vector V2(1,3),V3(1,3);
1905 J.Vector(m2,V2);
1906 J.Vector(m3,V3);
1907
1908 Bary.SetXYZ(GB);
1909 XDir.SetCoord(V3(1),V3(2),V3(3));
1910 YDir.SetCoord(V2(1),V2(2),V2(3));
1911
1912 Zgap = sqrt(Abs(J.Value(m1)));
1913 Ygap = sqrt(Abs(J.Value(m2)));
1914 Xgap = sqrt(Abs(J.Value(m3)));
1915}
1916//=======================================================================
1917//function : AxeOfInertia
1918//purpose :
1919//=======================================================================
1920void GeomLib::AxeOfInertia(const TColgp_Array1OfPnt& Points,
1921 gp_Ax2& Axe,
1922 Standard_Boolean& IsSingular,
1923 const Standard_Real Tol)
1924{
1925 gp_Pnt Bary;
1926 gp_Dir OX,OY,OZ;
1927 Standard_Real gx, gy, gz;
1928
1929 GeomLib::Inertia(Points, Bary, OX, OY, gx, gy, gz);
1930
1931 if (gy*Points.Length()<=Tol) {
1932 gp_Ax2 axe (Bary, OX);
1933 OY = axe.XDirection();
1934 IsSingular = Standard_True;
1935 }
1936 else {
1937 IsSingular = Standard_False;
1938 }
1939
1940 OZ = OX^OY;
1941 gp_Ax2 TheAxe(Bary, OZ, OX);
1942 Axe = TheAxe;
1943}
1944
1945//=======================================================================
1946//function : CanBeTreated
1947//purpose : indicates if the surface can be treated(if the conditions are
1948// filled) and need to be treated(if the surface hasn't been yet
1949// treated or if the surface is rationnal and non periodic)
1950//=======================================================================
1951
1952static Standard_Boolean CanBeTreated(Handle(Geom_BSplineSurface)& BSurf)
1953
1954{Standard_Integer i;
1955 Standard_Real lambda; //proportionnality coefficient
1956 Standard_Boolean AlreadyTreated=Standard_True;
1957
1958 if (!BSurf->IsURational()||(BSurf->IsUPeriodic()))
1959 return Standard_False;
1960 else {
1961 lambda=(BSurf->Weight(1,1)/BSurf->Weight(BSurf->NbUPoles(),1));
1962 for (i=1;i<=BSurf->NbVPoles();i++) //test of the proportionnality of the denominator on the boundaries
1963 if ((BSurf->Weight(1,i)/(lambda*BSurf->Weight(BSurf->NbUPoles(),i))<(1-Precision::Confusion()))||
1964 (BSurf->Weight(1,i)/(lambda*BSurf->Weight(BSurf->NbUPoles(),i))>(1+Precision::Confusion())))
1965 return Standard_False;
1966 i=1;
1967 while ((AlreadyTreated) && (i<=BSurf->NbVPoles())){ //tests if the surface has already been treated
1968 if (((BSurf->Weight(1,i)/(BSurf->Weight(2,i)))<(1-Precision::Confusion()))||
1969 ((BSurf->Weight(1,i)/(BSurf->Weight(2,i)))>(1+Precision::Confusion()))||
1970 ((BSurf->Weight(BSurf->NbUPoles()-1,i)/(BSurf->Weight(BSurf->NbUPoles(),i)))<(1-Precision::Confusion()))||
1971 ((BSurf->Weight(BSurf->NbUPoles()-1,i)/(BSurf->Weight(BSurf->NbUPoles(),i)))>(1+Precision::Confusion())))
1972 AlreadyTreated=Standard_False;
1973 i++;
1974 }
1975 if (AlreadyTreated)
1976 return Standard_False;
1977 }
1978 return Standard_True;
1979}
1980
1981//=======================================================================
41194117
K
1982//class : law_evaluator
1983//purpose : usefull to estimate the value of a function of 2 variables
7fd59977 1984//=======================================================================
1985
41194117
K
1986class law_evaluator : public BSplSLib_EvaluatorFunction
1987{
7fd59977 1988
41194117 1989public:
7fd59977 1990
41194117
K
1991 law_evaluator (const GeomLib_DenominatorMultiplierPtr theDenominatorPtr)
1992 : myDenominator (theDenominatorPtr) {}
1993
1994 virtual void Evaluate (const Standard_Integer theDerivativeRequest,
1995 const Standard_Real theUParameter,
1996 const Standard_Real theVParameter,
1997 Standard_Real& theResult,
1998 Standard_Integer& theErrorCode) const
1999 {
2000 if ((myDenominator != NULL) && (theDerivativeRequest == 0))
2001 {
2002 theResult = myDenominator->Value (theUParameter, theVParameter);
2003 theErrorCode = 0;
2004 }
2005 else
2006 {
2007 theErrorCode = 1;
2008 }
7fd59977 2009 }
41194117
K
2010
2011private:
2012
2013 GeomLib_DenominatorMultiplierPtr myDenominator;
2014
2015};
2016
7fd59977 2017//=======================================================================
2018//function : CheckIfKnotExists
2019//purpose : true if the knot already exists in the knot sequence
2020//=======================================================================
2021
2022static Standard_Boolean CheckIfKnotExists(const TColStd_Array1OfReal& surface_knots,
2023 const Standard_Real knot)
2024
2025{Standard_Integer i;
2026 for (i=1;i<=surface_knots.Length();i++)
2027 if ((surface_knots(i)-Precision::Confusion()<=knot)&&(surface_knots(i)+Precision::Confusion()>=knot))
2028 return Standard_True;
2029 return Standard_False;
2030}
2031
2032//=======================================================================
2033//function : AddAKnot
2034//purpose : add a knot and its multiplicity to the knot sequence. This knot
2035// will be C2 and the degree is increased of deltasurface_degree
2036//=======================================================================
2037
2038static void AddAKnot(const TColStd_Array1OfReal& knots,
2039 const TColStd_Array1OfInteger& mults,
2040 const Standard_Real knotinserted,
2041 const Standard_Integer deltasurface_degree,
2042 const Standard_Integer finalsurfacedegree,
2043 Handle(TColStd_HArray1OfReal) & newknots,
2044 Handle(TColStd_HArray1OfInteger) & newmults)
2045
2046{Standard_Integer i;
2047
2048 newknots=new TColStd_HArray1OfReal(1,knots.Length()+1);
2049 newmults=new TColStd_HArray1OfInteger(1,knots.Length()+1);
2050 i=1;
2051 while (knots(i)<knotinserted){
2052 newknots->SetValue(i,knots(i));
2053 newmults->SetValue(i,mults(i)+deltasurface_degree);
2054 i++;
2055 }
2056 newknots->SetValue(i,knotinserted); //insertion of the new knot
2057 newmults->SetValue(i,finalsurfacedegree-2);
2058 i++;
2059 while (i<=newknots->Length()){
2060 newknots->SetValue(i,knots(i-1));
2061 newmults->SetValue(i,mults(i-1)+deltasurface_degree);
2062 i++;
2063 }
2064}
2065
2066//=======================================================================
2067//function : Sort
2068//purpose : give the new flat knots(u or v) of the surface
2069//=======================================================================
2070
2071static void BuildFlatKnot(const TColStd_Array1OfReal& surface_knots,
2072 const TColStd_Array1OfInteger& surface_mults,
2073 const Standard_Integer deltasurface_degree,
2074 const Standard_Integer finalsurface_degree,
2075 const Standard_Real knotmin,
2076 const Standard_Real knotmax,
2077 Handle(TColStd_HArray1OfReal)& ResultKnots,
2078 Handle(TColStd_HArray1OfInteger)& ResultMults)
2079
2080{
2081 Standard_Integer i;
2082
2083 if (CheckIfKnotExists(surface_knots,knotmin) &&
2084 CheckIfKnotExists(surface_knots,knotmax)){
2085 ResultKnots=new TColStd_HArray1OfReal(1,surface_knots.Length());
2086 ResultMults=new TColStd_HArray1OfInteger(1,surface_knots.Length());
2087 for (i=1;i<=surface_knots.Length();i++){
2088 ResultKnots->SetValue(i,surface_knots(i));
2089 ResultMults->SetValue(i,surface_mults(i)+deltasurface_degree);
2090 }
2091 }
2092 else{
2093 if ((CheckIfKnotExists(surface_knots,knotmin))&&(!CheckIfKnotExists(surface_knots,knotmax)))
2094 AddAKnot(surface_knots,surface_mults,knotmax,deltasurface_degree,finalsurface_degree,ResultKnots,ResultMults);
2095 else{
2096 if ((!CheckIfKnotExists(surface_knots,knotmin))&&(CheckIfKnotExists(surface_knots,knotmax)))
2097 AddAKnot(surface_knots,surface_mults,knotmin,deltasurface_degree,finalsurface_degree,ResultKnots,ResultMults);
2098 else{
2099 if ((!CheckIfKnotExists(surface_knots,knotmin))&&(!CheckIfKnotExists(surface_knots,knotmax))&&
2100 (knotmin==knotmax)){
2101 AddAKnot(surface_knots,surface_mults,knotmin,deltasurface_degree,finalsurface_degree,ResultKnots,ResultMults);
2102 }
2103 else{
2104 Handle(TColStd_HArray1OfReal) IntermedKnots;
2105 Handle(TColStd_HArray1OfInteger) IntermedMults;
2106 AddAKnot(surface_knots,surface_mults,knotmin,deltasurface_degree,finalsurface_degree,IntermedKnots,IntermedMults);
2107 AddAKnot(IntermedKnots->ChangeArray1(),IntermedMults->ChangeArray1(),knotmax,0,finalsurface_degree,ResultKnots,ResultMults);
2108 }
2109 }
2110 }
2111 }
2112}
2113
2114//=======================================================================
2115//function : FunctionMultiply
2116//purpose : multiply the surface BSurf by a(u,v) (law_evaluator) on its
2117// numerator and denominator
2118//=======================================================================
2119
2120static void FunctionMultiply(Handle(Geom_BSplineSurface)& BSurf,
2121 const Standard_Real knotmin,
2122 const Standard_Real knotmax)
2123
2124{TColStd_Array1OfReal surface_u_knots(1,BSurf->NbUKnots()) ;
2125 TColStd_Array1OfInteger surface_u_mults(1,BSurf->NbUKnots()) ;
2126 TColStd_Array1OfReal surface_v_knots(1,BSurf->NbVKnots()) ;
2127 TColStd_Array1OfInteger surface_v_mults(1,BSurf->NbVKnots()) ;
2128 TColgp_Array2OfPnt surface_poles(1,BSurf->NbUPoles(),
2129 1,BSurf->NbVPoles()) ;
2130 TColStd_Array2OfReal surface_weights(1,BSurf->NbUPoles(),
2131 1,BSurf->NbVPoles()) ;
2132 Standard_Integer i,j,k,status,new_num_u_poles,new_num_v_poles,length=0;
2133 Handle(TColStd_HArray1OfReal) newuknots,newvknots;
2134 Handle(TColStd_HArray1OfInteger) newumults,newvmults;
2135
2136 BSurf->UKnots(surface_u_knots) ;
2137 BSurf->UMultiplicities(surface_u_mults) ;
2138 BSurf->VKnots(surface_v_knots) ;
2139 BSurf->VMultiplicities(surface_v_mults) ;
2140 BSurf->Poles(surface_poles) ;
2141 BSurf->Weights(surface_weights) ;
2142
2143 TColStd_Array1OfReal Knots(1,2);
2144 TColStd_Array1OfInteger Mults(1,2);
2145 Handle(TColStd_HArray1OfReal) NewKnots;
2146 Handle(TColStd_HArray1OfInteger) NewMults;
2147
2148 Knots(1)=0;
2149 Knots(2)=1;
2150 Mults(1)=4;
2151 Mults(2)=4;
2152 BuildFlatKnot(Knots,Mults,0,3,knotmin,knotmax,NewKnots,NewMults);
2153
2154 for (i=1;i<=NewMults->Length();i++)
2155 length+=NewMults->Value(i);
2156 TColStd_Array1OfReal FlatKnots(1,length);
2157 BSplCLib::KnotSequence(NewKnots->ChangeArray1(),NewMults->ChangeArray1(),FlatKnots);
2158
41194117 2159 GeomLib_DenominatorMultiplier aDenominator (BSurf, FlatKnots);
7fd59977 2160
2161 BuildFlatKnot(surface_u_knots,
2162 surface_u_mults,
2163 3,
2164 BSurf->UDegree()+3,
2165 knotmin,
2166 knotmax,
2167 newuknots,
2168 newumults);
2169 BuildFlatKnot(surface_v_knots,
2170 surface_v_mults,
2171 BSurf->VDegree(),
2172 2*(BSurf->VDegree()),
2173 1.0,
2174 0.0,
2175 newvknots,
2176 newvmults);
2177 length=0;
2178 for (i=1;i<=newumults->Length();i++)
2179 length+=newumults->Value(i);
2180 new_num_u_poles=(length-BSurf->UDegree()-3-1);
2181 TColStd_Array1OfReal newuflatknots(1,length);
2182 length=0;
2183 for (i=1;i<=newvmults->Length();i++)
2184 length+=newvmults->Value(i);
2185 new_num_v_poles=(length-2*BSurf->VDegree()-1);
2186 TColStd_Array1OfReal newvflatknots(1,length);
2187
2188 TColgp_Array2OfPnt NewNumerator(1,new_num_u_poles,1,new_num_v_poles);
2189 TColStd_Array2OfReal NewDenominator(1,new_num_u_poles,1,new_num_v_poles);
2190
2191 BSplCLib::KnotSequence(newuknots->ChangeArray1(),newumults->ChangeArray1(),newuflatknots);
2192 BSplCLib::KnotSequence(newvknots->ChangeArray1(),newvmults->ChangeArray1(),newvflatknots);
2193//POP pour WNT
41194117 2194 law_evaluator ev (&aDenominator);
7fd59977 2195// BSplSLib::FunctionMultiply(law_evaluator, //multiplication
2196 BSplSLib::FunctionMultiply(ev, //multiplication
2197 BSurf->UDegree(),
2198 BSurf->VDegree(),
2199 surface_u_knots,
2200 surface_v_knots,
0e14656b 2201 &surface_u_mults,
2202 &surface_v_mults,
7fd59977 2203 surface_poles,
0e14656b 2204 &surface_weights,
7fd59977 2205 newuflatknots,
2206 newvflatknots,
2207 BSurf->UDegree()+3,
2208 2*(BSurf->VDegree()),
2209 NewNumerator,
2210 NewDenominator,
2211 status);
2212 if (status!=0)
2213 Standard_ConstructionError::Raise("GeomLib Multiplication Error") ;
2214 for (i = 1 ; i <= new_num_u_poles ; i++) {
2215 for (j = 1 ; j <= new_num_v_poles ; j++) {
2216 for (k = 1 ; k <= 3 ; k++) {
2217 NewNumerator(i,j).SetCoord(k,NewNumerator(i,j).Coord(k)/NewDenominator(i,j)) ;
2218 }
2219 }
2220 }
2221 BSurf= new Geom_BSplineSurface(NewNumerator,
2222 NewDenominator,
2223 newuknots->ChangeArray1(),
2224 newvknots->ChangeArray1(),
2225 newumults->ChangeArray1(),
2226 newvmults->ChangeArray1(),
2227 BSurf->UDegree()+3,
2228 2*(BSurf->VDegree()) );
2229}
2230
2231//=======================================================================
2232//function : CancelDenominatorDerivative1D
2233//purpose : cancel the denominator derivative in one direction
2234//=======================================================================
2235
2236static void CancelDenominatorDerivative1D(Handle(Geom_BSplineSurface) & BSurf)
2237
2238{Standard_Integer i,j;
2239 Standard_Real uknotmin=1.0,uknotmax=0.0,
2240 x,y,
2241 startu_value,
2242 endu_value;
2243 TColStd_Array1OfReal BSurf_u_knots(1,BSurf->NbUKnots()) ;
2244
2245 startu_value=BSurf->UKnot(1);
2246 endu_value=BSurf->UKnot(BSurf->NbUKnots());
2247 BSurf->UKnots(BSurf_u_knots) ;
2248 BSplCLib::Reparametrize(0.0,1.0,BSurf_u_knots);
2249 BSurf->SetUKnots(BSurf_u_knots); //reparametrisation of the surface
2250 Handle(Geom_BSplineCurve) BCurve;
2251 TColStd_Array1OfReal BCurveWeights(1,BSurf->NbUPoles());
2252 TColgp_Array1OfPnt BCurvePoles(1,BSurf->NbUPoles());
2253 TColStd_Array1OfReal BCurveKnots(1,BSurf->NbUKnots());
2254 TColStd_Array1OfInteger BCurveMults(1,BSurf->NbUKnots());
2255
2256 if (CanBeTreated(BSurf)){
2257 for (i=1;i<=BSurf->NbVPoles();i++){ //loop on each pole function
2258 x=1.0;y=0.0;
2259 for (j=1;j<=BSurf->NbUPoles();j++){
2260 BCurveWeights(j)=BSurf->Weight(j,i);
2261 BCurvePoles(j)=BSurf->Pole(j,i);
2262 }
2263 BSurf->UKnots(BCurveKnots);
2264 BSurf->UMultiplicities(BCurveMults);
2265 BCurve = new Geom_BSplineCurve(BCurvePoles, //building of a pole function
2266 BCurveWeights,
2267 BCurveKnots,
2268 BCurveMults,
2269 BSurf->UDegree());
2270 Hermit::Solutionbis(BCurve,x,y,Precision::Confusion(),Precision::Confusion());
2271 if (x<uknotmin)
2272 uknotmin=x; //uknotmin,uknotmax:extremal knots
2273 if ((x!=1.0)&&(x>uknotmax))
2274 uknotmax=x;
2275 if ((y!=0.0)&&(y<uknotmin))
2276 uknotmin=y;
2277 if (y>uknotmax)
2278 uknotmax=y;
2279 }
2280
2281 FunctionMultiply(BSurf,uknotmin,uknotmax); //multiplication
2282
2283 BSurf->UKnots(BSurf_u_knots) ;
2284 BSplCLib::Reparametrize(startu_value,endu_value,BSurf_u_knots);
2285 BSurf->SetUKnots(BSurf_u_knots);
2286 }
2287}
2288
2289//=======================================================================
2290//function : CancelDenominatorDerivative
2291//purpose :
2292//=======================================================================
2293
2294void GeomLib::CancelDenominatorDerivative(Handle(Geom_BSplineSurface) & BSurf,
2295 const Standard_Boolean udirection,
2296 const Standard_Boolean vdirection)
2297
2298{if (udirection && !vdirection)
2299 CancelDenominatorDerivative1D(BSurf);
2300 else{
2301 if (!udirection && vdirection) {
2302 BSurf->ExchangeUV();
2303 CancelDenominatorDerivative1D(BSurf);
2304 BSurf->ExchangeUV();
2305 }
2306 else{
2307 if (udirection && vdirection){ //optimize the treatment
2308 if (BSurf->UDegree()<=BSurf->VDegree()){
2309 CancelDenominatorDerivative1D(BSurf);
2310 BSurf->ExchangeUV();
2311 CancelDenominatorDerivative1D(BSurf);
2312 BSurf->ExchangeUV();
2313 }
2314 else{
2315 BSurf->ExchangeUV();
2316 CancelDenominatorDerivative1D(BSurf);
2317 BSurf->ExchangeUV();
2318 CancelDenominatorDerivative1D(BSurf);
2319 }
2320 }
2321 }
2322 }
2323}
2324
2325//=======================================================================
2326//function : NormEstim
2327//purpose :
2328//=======================================================================
2329
2330Standard_Integer GeomLib::NormEstim(const Handle(Geom_Surface)& S,
2331 const gp_Pnt2d& UV,
2332 const Standard_Real Tol, gp_Dir& N)
2333{
2334 gp_Vec DU, DV;
2335 gp_Pnt DummyPnt;
2336 Standard_Real aTol2 = Square(Tol);
2337
2338 S->D1(UV.X(), UV.Y(), DummyPnt, DU, DV);
2339
2340 Standard_Real MDU = DU.SquareMagnitude(), MDV = DV.SquareMagnitude();
2341
7fd59977 2342 if(MDU >= aTol2 && MDV >= aTol2) {
2343 gp_Vec Norm = DU^DV;
2344 Standard_Real Magn = Norm.SquareMagnitude();
2345 if(Magn < aTol2) return 3;
2346
2347 //Magn = sqrt(Magn);
2348 N.SetXYZ(Norm.XYZ());
2349
2350 return 0;
2351 }
7fd59977 2352 else {
2b21c641 2353 gp_Vec D2U, D2V, D2UV;
2354 Standard_Boolean isDone;
2355 CSLib_NormalStatus aStatus;
2356 gp_Dir aNormal;
2357
2358 S->D2(UV.X(), UV.Y(), DummyPnt, DU, DV, D2U, D2V, D2UV);
2359 CSLib::Normal(DU, DV, D2U, D2V, D2UV, Tol, isDone, aStatus, aNormal);
2360
2361 if (isDone) {
2362 Standard_Real Umin, Umax, Vmin, Vmax;
2363 Standard_Real step = 1.0e-5;
2364 Standard_Real eps = 1.0e-16;
23b894f7 2365 Standard_Real sign = -1.0;
2b21c641 2366
2367 S->Bounds(Umin, Umax, Vmin, Vmax);
23b894f7 2368
2369 // check for cone apex singularity point
2370 if ((UV.Y() > Vmin + step) && (UV.Y() < Vmax - step))
2371 {
2372 gp_Dir aNormal1, aNormal2;
2373 Standard_Real aConeSingularityAngleEps = 1.0e-4;
2374 S->D1(UV.X(), UV.Y() - sign * step, DummyPnt, DU, DV);
2375 if ((DU.XYZ().SquareModulus() > eps) && (DV.XYZ().SquareModulus() > eps)) {
2376 aNormal1 = DU^DV;
2377 S->D1(UV.X(), UV.Y() + sign * step, DummyPnt, DU, DV);
2378 if ((DU.XYZ().SquareModulus() > eps) && (DV.XYZ().SquareModulus() > eps)) {
2379 aNormal2 = DU^DV;
2380 if (aNormal1.IsOpposite(aNormal2, aConeSingularityAngleEps))
2381 return 2;
2382 }
2383 }
2384 }
2385
2b21c641 2386 // Along V
2387 if(MDU < aTol2 && MDV >= aTol2) {
23b894f7 2388 if ((Vmax - UV.Y()) > (UV.Y() - Vmin))
2389 sign = 1.0;
2b21c641 2390 S->D1(UV.X(), UV.Y() + sign * step, DummyPnt, DU, DV);
2391 gp_Vec Norm = DU^DV;
23b894f7 2392 if (Norm.SquareMagnitude() < eps) {
2393 Standard_Real sign1 = -1.0;
2394 if ((Umax - UV.X()) > (UV.X() - Umin))
2395 sign1 = 1.0;
2396 S->D1(UV.X() + sign1 * step, UV.Y() + sign * step, DummyPnt, DU, DV);
2397 Norm = DU^DV;
2398 }
2b21c641 2399 if ((Norm.SquareMagnitude() >= eps) && (Norm.Dot(aNormal) < 0.0))
23b894f7 2400 aNormal.Reverse();
2b21c641 2401 }
23b894f7 2402
2b21c641 2403 // Along U
2404 if(MDV < aTol2 && MDU >= aTol2) {
23b894f7 2405 if ((Umax - UV.X()) > (UV.X() - Umin))
2406 sign = 1.0;
2b21c641 2407 S->D1(UV.X() + sign * step, UV.Y(), DummyPnt, DU, DV);
2408 gp_Vec Norm = DU^DV;
23b894f7 2409 if (Norm.SquareMagnitude() < eps) {
2410 Standard_Real sign1 = -1.0;
2411 if ((Vmax - UV.Y()) > (UV.Y() - Vmin))
2412 sign1 = 1.0;
2413 S->D1(UV.X() + sign * step, UV.Y() + sign1 * step, DummyPnt, DU, DV);
2414 Norm = DU^DV;
2415 }
2b21c641 2416 if ((Norm.SquareMagnitude() >= eps) && (Norm.Dot(aNormal) < 0.0))
2417 aNormal.Reverse();
2418 }
7fd59977 2419
2b21c641 2420 // quasysingular
2421 if ((aStatus == CSLib_D1NuIsNull) || (aStatus == CSLib_D1NvIsNull) ||
2422 (aStatus == CSLib_D1NuIsParallelD1Nv)) {
2423 N.SetXYZ(aNormal.XYZ());
2424 return 1;
2425 }
2426 // conical
2427 if (aStatus == CSLib_InfinityOfSolutions)
2428 return 2;
7fd59977 2429 }
2b21c641 2430 // computation is impossible
7fd59977 2431 else {
2b21c641 2432 // conical
2433 if (aStatus == CSLib_D1NIsNull) {
2434 return 2;
2435 }
2436 return 3;
7fd59977 2437 }
7fd59977 2438 }
2b21c641 2439 return 3;
7fd59977 2440}
2441
2442