1 // Created on: 1996-11-08
2 // Created by: Jean Claude VAUTHIER
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <GCPnts_DeflectionType.hxx>
18 #include <Standard_ConstructionError.hxx>
19 #include <Precision.hxx>
25 #define Us3 0.3333333333333333333333333333
27 void GCPnts_TangentialDeflection::EvaluateDu (
29 const Standard_Real U,
32 Standard_Boolean& NotDone) const {
36 Standard_Real Lt = T.Magnitude ();
37 Standard_Real LTol = Precision::Confusion ();
38 if (Lt > LTol && N.Magnitude () > LTol) {
39 Standard_Real Lc = N.CrossMagnitude (T);
40 Standard_Real Ln = Lc/Lt;
42 Du = sqrt (8.0 * Max(curvatureDeflection, myMinLen) / Ln);
43 NotDone = Standard_False;
49 //=======================================================================
50 //function : GCPnts_TangentialDeflection
52 //=======================================================================
54 GCPnts_TangentialDeflection::GCPnts_TangentialDeflection (
56 const Standard_Real AngularDeflection,
57 const Standard_Real CurvatureDeflection,
58 const Standard_Integer MinimumOfPoints,
59 const Standard_Real UTol,
60 const Standard_Real theMinLen)
63 Initialize (C,AngularDeflection,CurvatureDeflection,MinimumOfPoints,UTol,theMinLen);
67 //=======================================================================
68 //function : GCPnts_TangentialDeflection
70 //=======================================================================
72 GCPnts_TangentialDeflection::GCPnts_TangentialDeflection (
74 const Standard_Real FirstParameter,
75 const Standard_Real LastParameter,
76 const Standard_Real AngularDeflection,
77 const Standard_Real CurvatureDeflection,
78 const Standard_Integer MinimumOfPoints,
79 const Standard_Real UTol,
80 const Standard_Real theMinLen)
94 //=======================================================================
95 //function : Initialize
97 //=======================================================================
99 void GCPnts_TangentialDeflection::Initialize (
101 const Standard_Real AngularDeflection,
102 const Standard_Real CurvatureDeflection,
103 const Standard_Integer MinimumOfPoints,
104 const Standard_Real UTol,
105 const Standard_Real theMinLen)
118 //=======================================================================
119 //function : Initialize
121 //=======================================================================
123 void GCPnts_TangentialDeflection::Initialize (
125 const Standard_Real FirstParameter,
126 const Standard_Real LastParameter,
127 const Standard_Real AngularDeflection,
128 const Standard_Real CurvatureDeflection,
129 const Standard_Integer MinimumOfPoints,
130 const Standard_Real UTol,
131 const Standard_Real theMinLen)
135 Standard_ConstructionError_Raise_if (CurvatureDeflection <= Precision::Confusion () || AngularDeflection <= Precision::Angular (), "GCPnts_TangentialDeflection::Initialize - Zero Deflection")
139 if (FirstParameter < LastParameter) {
140 firstu = FirstParameter;
141 lastu = LastParameter;
144 lastu = FirstParameter;
145 firstu = LastParameter;
148 angularDeflection = AngularDeflection;
149 curvatureDeflection = CurvatureDeflection;
150 minNbPnts = Max (MinimumOfPoints, 2);
151 myMinLen = Max(theMinLen, Precision::Confusion());
153 switch (C.GetType()) {
163 case GeomAbs_BSplineCurve:
165 Handle_TheBSplineCurve BS = C.BSpline() ;
166 if (BS->NbPoles() == 2 ) PerformLinear (C);
167 else PerformCurve (C);
170 case GeomAbs_BezierCurve:
172 Handle_TheBezierCurve BZ = C.Bezier();
173 if (BZ->NbPoles() == 2) PerformLinear (C);
174 else PerformCurve (C);
177 default: PerformCurve (C);
183 //=======================================================================
184 //function : PerformLinear
186 //=======================================================================
188 void GCPnts_TangentialDeflection::PerformLinear (const TheCurve& C) {
192 parameters.Append (firstu);
195 Standard_Real Du = (lastu - firstu) / minNbPnts;
196 Standard_Real U = firstu + Du;
197 for (Standard_Integer i = 2; i <= minNbPnts; i++) {
199 parameters.Append (U);
205 parameters.Append (lastu);
209 //=======================================================================
210 //function : PerformCircular
212 //=======================================================================
214 void GCPnts_TangentialDeflection::PerformCircular (const TheCurve& C)
216 // akm 8/01/02 : check the radius before divide by it
217 Standard_Real dfR = C.Circle().Radius();
218 Standard_Real Du = GCPnts_TangentialDeflection::ArcAngularStep(
219 dfR, curvatureDeflection, angularDeflection, myMinLen);
221 const Standard_Real aDiff = lastu - firstu;
222 Standard_Integer NbPoints = (Standard_Integer)(aDiff / Du);
223 NbPoints = Max(NbPoints, minNbPnts - 1);
224 Du = aDiff / NbPoints;
227 Standard_Real U = firstu;
228 for (Standard_Integer i = 1; i <= NbPoints; i++)
231 parameters.Append (U);
236 parameters.Append (lastu);
241 //=======================================================================
242 //function : PerformCurve
243 //purpose : On respecte ll'angle et la fleche, on peut imposer un nombre
244 // minimum de points sur un element lineaire
245 //=======================================================================
246 void GCPnts_TangentialDeflection::PerformCurve (const TheCurve& C)
249 Standard_Integer i, j;
251 gp_Pnt MiddlePoint, CurrentPoint, LastPoint;
252 Standard_Real Du, Dusave, MiddleU, L1, L2;
254 Standard_Real U1 = firstu;
255 Standard_Real LTol = Precision::Confusion (); //protection longueur nulle
256 Standard_Real ATol = Precision::Angular (); //protection angle nul
258 D0 (C, lastu, LastPoint);
260 //Initialization du calcul
262 Standard_Boolean NotDone = Standard_True;
263 Dusave = (lastu - firstu)*Us3;
265 EvaluateDu (C, U1, CurrentPoint, Du, NotDone);
266 parameters.Append (U1);
267 points .Append (CurrentPoint);
269 // Used to detect "isLine" current bspline and in Du computation in general handling.
270 Standard_Integer NbInterv = const_cast<TheCurve*>(&C)->NbIntervals(GeomAbs_CN);
271 TColStd_Array1OfReal Intervs(1, NbInterv+1);
272 const_cast<TheCurve*>(&C)->Intervals(Intervs, GeomAbs_CN);
275 //C'est soit une droite, soit une singularite :
276 V1 = (LastPoint.XYZ() - CurrentPoint.XYZ());
280 //Si c'est une droite on verifie en calculant minNbPoints :
281 Standard_Boolean IsLine = Standard_True;
282 Standard_Integer NbPoints = (minNbPnts > 3) ? minNbPnts : 3;
284 Standard_Real param = 0.;
285 for (i = 1; i <= NbInterv && IsLine; ++i)
287 // Avoid usage intervals out of [firstu, lastu].
288 if ((Intervs(i+1) < firstu) ||
289 (Intervs(i) > lastu))
293 // Fix border points in applicable intervals, to avoid be out of target interval.
294 if ((Intervs(i) < firstu) &&
295 (Intervs(i+1) > firstu))
299 if ((Intervs(i) < lastu) &&
300 (Intervs(i+1) > lastu))
302 Intervs(i + 1) = lastu;
305 Standard_Real delta = (Intervs(i+1) - Intervs(i))/NbPoints;
306 for (j = 1; j <= NbPoints && IsLine; ++j)
308 param = Intervs(i) + j*delta;
309 D0 (C, param, MiddlePoint);
310 V2 = (MiddlePoint.XYZ() - CurrentPoint.XYZ());
314 const Standard_Real aAngle = V2.CrossMagnitude(V1)/(L1*L2);
315 IsLine = (aAngle < ATol);
330 //c'etait une singularite on continue :
332 EvaluateDu (C, param, MiddlePoint, Du, NotDone);
337 Du = (lastu-firstu)/2.1;
338 MiddleU = firstu + Du;
339 D0 (C, MiddleU, MiddlePoint);
340 V1 = (MiddlePoint.XYZ() - CurrentPoint.XYZ());
344 // L1 < LTol C'est une courbe de longueur nulle, calcul termine :
345 // on renvoi un segment de 2 points (protection)
346 parameters.Append (lastu);
347 points .Append (LastPoint);
353 if (Du > Dusave) Du = Dusave;
359 parameters.Append (lastu);
360 points .Append (LastPoint);
365 //Traitement normal pour une courbe
366 Standard_Boolean MorePoints = Standard_True;
367 Standard_Real U2 = firstu;
368 Standard_Real AngleMax = angularDeflection * 0.5; //car on prend le point milieu
369 Standard_Integer aIdx[2] = {Intervs.Lower(), Intervs.Lower()}; // Indexes of intervals of U1 and U2, used to handle non-uniform case.
370 Standard_Boolean isNeedToCheck = Standard_False;
371 gp_Pnt aPrevPoint = points.Last();
374 aIdx[0] = getIntervalIdx(U1, Intervs, aIdx[0]);
377 if (U2 >= lastu) { //Bout de courbe
379 CurrentPoint = LastPoint;
383 else D0 (C, U2, CurrentPoint); //Point suivant
385 Standard_Real Coef = 0.0, ACoef = 0., FCoef = 0.;
386 Standard_Boolean Correction, TooLarge, TooSmall;
387 TooLarge = Standard_False;
388 Correction = Standard_True;
389 TooSmall = Standard_False;
391 while (Correction) { //Ajustement Du
394 aIdx[1] = getIntervalIdx(U2, Intervs, aIdx[0]);
395 if (aIdx[1] > aIdx[0]) // Jump to another polynom.
397 if (Du > (Intervs(aIdx[0] + 1) - Intervs(aIdx[0]) ) * Us3) // Set Du to the smallest value and check deflection on it.
399 Du = (Intervs(aIdx[0] + 1) - Intervs(aIdx[0]) ) * Us3;
403 D0 (C, U2, CurrentPoint);
407 MiddleU = (U1+U2)*0.5; //Verif / au point milieu
408 D0 (C, MiddleU, MiddlePoint);
410 V1 = (CurrentPoint.XYZ() - aPrevPoint.XYZ()); //Critere de fleche
411 V2 = (MiddlePoint.XYZ() - aPrevPoint.XYZ());
414 FCoef = (L1 > myMinLen) ?
415 V1.CrossMagnitude(V2)/(L1*curvatureDeflection) : 0.0;
417 V1 = (CurrentPoint.XYZ() - MiddlePoint.XYZ()); //Critere d'angle
420 if (L1 > myMinLen && L2 > myMinLen)
422 Standard_Real angg = V1.CrossMagnitude(V2) / (L1 * L2);
423 ACoef = angg / AngleMax;
428 //On retient le plus penalisant
429 Coef = Max(ACoef, FCoef);
431 if (isNeedToCheck && Coef < 0.55)
433 isNeedToCheck = Standard_False;
438 D0 (C, U2, CurrentPoint);
443 if (Abs (lastu-U2) < uTol) {
444 parameters.Append (lastu);
445 points .Append (LastPoint);
446 MorePoints = Standard_False;
447 Correction = Standard_False;
450 if (Coef >= 0.55 || TooLarge) {
451 parameters.Append (U2);
452 points .Append (CurrentPoint);
453 aPrevPoint = CurrentPoint;
454 Correction = Standard_False;
455 isNeedToCheck = Standard_True;
458 Correction = Standard_False;
459 aPrevPoint = CurrentPoint;
462 TooSmall = Standard_True;
463 //Standard_Real UUU2 = U2;
464 Du += Min((U2-U1)*(1.-Coef), Du*Us3);
469 D0 (C, U2, CurrentPoint);
478 CurrentPoint = MiddlePoint;
483 D0 (C, U2, CurrentPoint);
484 TooLarge = Standard_True;
495 //La fleche est critere de decoupage
496 EvaluateDu (C, U2, CurrentPoint, Du, NotDone);
498 Du += (Du-Dusave)*(Du/Dusave);
499 if (Du > 1.5 * Dusave) Du = 1.5 * Dusave;
500 if (Du < 0.75* Dusave) Du = 0.75 * Dusave;
504 //L'angle est le critere de decoupage
505 Du += (Du-Dusave)*(Du/Dusave);
506 if (Du > 1.5 * Dusave) Du = 1.5 * Dusave;
507 if (Du < 0.75* Dusave) Du = 0.75 * Dusave;
514 parameters.Append (lastu);
515 points .Append (LastPoint);
516 MorePoints = Standard_False;
518 else if (Du*Us3 > uTol) Du*=Us3;
524 //Recalage avant dernier point :
525 i = points.Length()-1;
526 // Real d = points (i).Distance (points (i+1));
527 // if (Abs(parameters (i) - parameters (i+1))<= 0.000001 || d < Precision::Confusion()) {
528 // cout<<"deux points confondus"<<endl;
529 // parameters.Remove (i+1);
530 // points.Remove (i+1);
534 MiddleU = parameters (i-1);
535 MiddleU = (lastu + MiddleU)*0.5;
536 D0 (C, MiddleU, MiddlePoint);
537 parameters.SetValue (i, MiddleU);
538 points .SetValue (i, MiddlePoint);
541 //-- On rajoute des points aux milieux des segments si le nombre
542 //-- mini de points n'est pas atteint
544 Standard_Integer Nbp = points.Length();
545 Standard_Integer MinNb= (9*minNbPnts)/10;
546 //if(MinNb<4) MinNb=4;
548 //-- if(Nbp < MinNb) { cout<<"\n*"; } else { cout<<"\n."; }
550 //-- cout<<" \nGCPnts TangentialDeflection : Ajout de Points ("<<Nbp<<" "<<minNbPnts<<" )"<<endl;
551 for(i=2; i<=Nbp; i++) {
552 MiddleU = (parameters.Value(i-1)+parameters.Value(i))*0.5;
553 D0 (C, MiddleU, MiddlePoint);
554 parameters.InsertBefore(i,MiddleU);
555 points.InsertBefore(i,MiddlePoint);