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>
24 #include <TColStd_Array1OfReal.hxx>
26 #define Us3 0.3333333333333333333333333333
28 void GCPnts_TangentialDeflection::EvaluateDu (
30 const Standard_Real U,
33 Standard_Boolean& NotDone) const {
37 Standard_Real Lt = T.Magnitude ();
38 Standard_Real LTol = Precision::Confusion ();
39 if (Lt > LTol && N.Magnitude () > LTol) {
40 Standard_Real Lc = N.CrossMagnitude (T);
41 Standard_Real Ln = Lc/Lt;
43 Du = sqrt (8.0 * Max(curvatureDeflection, myMinLen) / Ln);
44 NotDone = Standard_False;
50 //=======================================================================
51 //function : GCPnts_TangentialDeflection
53 //=======================================================================
55 GCPnts_TangentialDeflection::GCPnts_TangentialDeflection (
57 const Standard_Real AngularDeflection,
58 const Standard_Real CurvatureDeflection,
59 const Standard_Integer MinimumOfPoints,
60 const Standard_Real UTol,
61 const Standard_Real theMinLen)
64 Initialize (C,AngularDeflection,CurvatureDeflection,MinimumOfPoints,UTol,theMinLen);
68 //=======================================================================
69 //function : GCPnts_TangentialDeflection
71 //=======================================================================
73 GCPnts_TangentialDeflection::GCPnts_TangentialDeflection (
75 const Standard_Real FirstParameter,
76 const Standard_Real LastParameter,
77 const Standard_Real AngularDeflection,
78 const Standard_Real CurvatureDeflection,
79 const Standard_Integer MinimumOfPoints,
80 const Standard_Real UTol,
81 const Standard_Real theMinLen)
95 //=======================================================================
96 //function : Initialize
98 //=======================================================================
100 void GCPnts_TangentialDeflection::Initialize (
102 const Standard_Real AngularDeflection,
103 const Standard_Real CurvatureDeflection,
104 const Standard_Integer MinimumOfPoints,
105 const Standard_Real UTol,
106 const Standard_Real theMinLen)
119 //=======================================================================
120 //function : Initialize
122 //=======================================================================
124 void GCPnts_TangentialDeflection::Initialize (
126 const Standard_Real FirstParameter,
127 const Standard_Real LastParameter,
128 const Standard_Real AngularDeflection,
129 const Standard_Real CurvatureDeflection,
130 const Standard_Integer MinimumOfPoints,
131 const Standard_Real UTol,
132 const Standard_Real theMinLen)
136 Standard_ConstructionError_Raise_if (CurvatureDeflection <= Precision::Confusion () || AngularDeflection <= Precision::Angular (), "GCPnts_TangentialDeflection::Initialize - Zero Deflection")
140 if (FirstParameter < LastParameter) {
141 firstu = FirstParameter;
142 lastu = LastParameter;
145 lastu = FirstParameter;
146 firstu = LastParameter;
149 angularDeflection = AngularDeflection;
150 curvatureDeflection = CurvatureDeflection;
151 minNbPnts = Max (MinimumOfPoints, 2);
152 myMinLen = Max(theMinLen, Precision::Confusion());
154 switch (C.GetType()) {
164 case GeomAbs_BSplineCurve:
166 Handle_TheBSplineCurve BS = C.BSpline() ;
167 if (BS->NbPoles() == 2 ) PerformLinear (C);
168 else PerformCurve (C);
171 case GeomAbs_BezierCurve:
173 Handle_TheBezierCurve BZ = C.Bezier();
174 if (BZ->NbPoles() == 2) PerformLinear (C);
175 else PerformCurve (C);
178 default: PerformCurve (C);
184 //=======================================================================
185 //function : PerformLinear
187 //=======================================================================
189 void GCPnts_TangentialDeflection::PerformLinear (const TheCurve& C) {
193 parameters.Append (firstu);
196 Standard_Real Du = (lastu - firstu) / minNbPnts;
197 Standard_Real U = firstu + Du;
198 for (Standard_Integer i = 2; i <= minNbPnts; i++) {
200 parameters.Append (U);
206 parameters.Append (lastu);
210 //=======================================================================
211 //function : PerformCircular
213 //=======================================================================
215 void GCPnts_TangentialDeflection::PerformCircular (const TheCurve& C)
217 // akm 8/01/02 : check the radius before divide by it
218 Standard_Real dfR = C.Circle().Radius();
219 Standard_Real Du = GCPnts_TangentialDeflection::ArcAngularStep(
220 dfR, curvatureDeflection, angularDeflection, myMinLen);
222 const Standard_Real aDiff = lastu - firstu;
223 Standard_Integer NbPoints = (Standard_Integer)(aDiff / Du);
224 NbPoints = Max(NbPoints, minNbPnts - 1);
225 Du = aDiff / NbPoints;
228 Standard_Real U = firstu;
229 for (Standard_Integer i = 1; i <= NbPoints; i++)
232 parameters.Append (U);
237 parameters.Append (lastu);
243 //=======================================================================
244 //function : PerformCurve
245 //purpose : On respecte ll'angle et la fleche, on peut imposer un nombre
246 // minimum de points sur un element lineaire
247 //=======================================================================
249 void GCPnts_TangentialDeflection::PerformCurve (const TheCurve& C)
252 Standard_Integer i, j;
254 gp_Pnt MiddlePoint, CurrentPoint, LastPoint;
255 Standard_Real Du, Dusave, MiddleU, L1, L2;
257 Standard_Real U1 = firstu;
258 Standard_Real LTol = Precision::Confusion (); //protection longueur nulle
259 Standard_Real ATol = Precision::Angular (); //protection angle nul
261 D0 (C, lastu, LastPoint);
263 //Initialization du calcul
265 Standard_Boolean NotDone = Standard_True;
266 Dusave = (lastu - firstu)*Us3;
268 EvaluateDu (C, U1, CurrentPoint, Du, NotDone);
269 parameters.Append (U1);
270 points .Append (CurrentPoint);
274 //C'est soit une droite, soit une singularite :
275 V1 = (LastPoint.XYZ() - CurrentPoint.XYZ());
279 //Si c'est une droite on verifie en calculant minNbPoints :
280 Standard_Boolean IsLine = Standard_True;
281 Standard_Integer NbPoints = (minNbPnts > 3) ? minNbPnts : 3;
283 Standard_Integer NbInterv = const_cast<TheCurve*>(&C)->NbIntervals(GeomAbs_CN);
284 TColStd_Array1OfReal Intervs(1, NbInterv+1);
285 const_cast<TheCurve*>(&C)->Intervals(Intervs, GeomAbs_CN);
286 Standard_Real param = 0.;
287 for (i = 1; i <= NbInterv && IsLine; ++i)
289 // Avoid usage intervals out of [firstu, lastu].
290 if ((Intervs(i+1) < firstu) ||
291 (Intervs(i) > lastu))
295 // Fix border points in applicable intervals, to avoid be out of target interval.
296 if ((Intervs(i) < firstu) &&
297 (Intervs(i+1) > firstu))
301 if ((Intervs(i) < lastu) &&
302 (Intervs(i+1) > lastu))
304 Intervs(i + 1) = lastu;
307 Standard_Real delta = (Intervs(i+1) - Intervs(i))/NbPoints;
308 for (j = 1; j <= NbPoints && IsLine; ++j)
310 param = Intervs(i) + j*delta;
311 D0 (C, param, MiddlePoint);
312 V2 = (MiddlePoint.XYZ() - CurrentPoint.XYZ());
316 const Standard_Real aAngle = V2.CrossMagnitude(V1)/(L1*L2);
317 IsLine = (aAngle < ATol);
332 //c'etait une singularite on continue :
334 EvaluateDu (C, param, MiddlePoint, Du, NotDone);
339 Du = (lastu-firstu)/2.1;
340 MiddleU = firstu + Du;
341 D0 (C, MiddleU, MiddlePoint);
342 V1 = (MiddlePoint.XYZ() - CurrentPoint.XYZ());
346 // L1 < LTol C'est une courbe de longueur nulle, calcul termine :
347 // on renvoi un segment de 2 points (protection)
348 parameters.Append (lastu);
349 points .Append (LastPoint);
355 if (Du > Dusave) Du = Dusave;
361 parameters.Append (lastu);
362 points .Append (LastPoint);
367 //Traitement normal pour une courbe
368 Standard_Boolean MorePoints = Standard_True;
369 Standard_Real U2 = firstu;
370 Standard_Real AngleMax = angularDeflection * 0.5; //car on prend le point milieu
372 gp_Pnt aPrevPoint = points.Last();
378 if (U2 >= lastu) { //Bout de courbe
380 CurrentPoint = LastPoint;
384 else D0 (C, U2, CurrentPoint); //Point suivant
386 Standard_Real Coef, ACoef = 0., FCoef = 0.;
387 Standard_Boolean Correction, TooLarge, TooSmall;
388 TooLarge = Standard_False;
389 TooSmall = Standard_False;
390 Correction = Standard_True;
392 while (Correction) { //Ajustement Du
393 MiddleU = (U1+U2)*0.5; //Verif / au point milieu
394 D0 (C, MiddleU, MiddlePoint);
396 V1 = (CurrentPoint.XYZ() - aPrevPoint.XYZ()); //Critere de fleche
397 V2 = (MiddlePoint.XYZ() - aPrevPoint.XYZ());
400 FCoef = (L1 > myMinLen) ?
401 V1.CrossMagnitude(V2)/(L1*curvatureDeflection) : 0.0;
403 V1 = (CurrentPoint.XYZ() - MiddlePoint.XYZ()); //Critere d'angle
406 if (L1 > myMinLen && L2 > myMinLen)
408 Standard_Real angg = V1.CrossMagnitude(V2) / (L1 * L2);
409 ACoef = angg / AngleMax;
414 //On retient le plus penalisant
415 Coef = Max(ACoef, FCoef);
418 if (Abs (lastu-U2) < uTol) {
419 parameters.Append (lastu);
420 points .Append (LastPoint);
421 MorePoints = Standard_False;
422 Correction = Standard_False;
425 if (Coef >= 0.55 || TooLarge) {
426 parameters.Append (U2);
427 points .Append (CurrentPoint);
428 aPrevPoint = CurrentPoint;
429 Correction = Standard_False;
432 Correction = Standard_False;
433 aPrevPoint = CurrentPoint;
436 TooSmall = Standard_True;
437 //Standard_Real UUU2 = U2;
438 Du += Min((U2-U1)*(1.-Coef), Du*Us3);
443 D0 (C, U2, CurrentPoint);
452 CurrentPoint = MiddlePoint;
457 D0 (C, U2, CurrentPoint);
458 TooLarge = Standard_True;
469 //La fleche est critere de decoupage
470 EvaluateDu (C, U2, CurrentPoint, Du, NotDone);
472 Du += (Du-Dusave)*(Du/Dusave);
473 if (Du > 1.5 * Dusave) Du = 1.5 * Dusave;
474 if (Du < 0.75* Dusave) Du = 0.75 * Dusave;
478 //L'angle est le critere de decoupage
479 Du += (Du-Dusave)*(Du/Dusave);
480 if (Du > 1.5 * Dusave) Du = 1.5 * Dusave;
481 if (Du < 0.75* Dusave) Du = 0.75 * Dusave;
488 parameters.Append (lastu);
489 points .Append (LastPoint);
490 MorePoints = Standard_False;
492 else if (Du*Us3 > uTol) Du*=Us3;
498 //Recalage avant dernier point :
499 i = points.Length()-1;
500 // Real d = points (i).Distance (points (i+1));
501 // if (Abs(parameters (i) - parameters (i+1))<= 0.000001 || d < Precision::Confusion()) {
502 // cout<<"deux points confondus"<<endl;
503 // parameters.Remove (i+1);
504 // points.Remove (i+1);
509 MiddleU = parameters (i-1);
510 MiddleU = (lastu + MiddleU)*0.5;
511 D0 (C, MiddleU, MiddlePoint);
512 parameters.SetValue (i, MiddleU);
513 points .SetValue (i, MiddlePoint);
516 //-- On rajoute des points aux milieux des segments si le nombre
517 //-- mini de points n'est pas atteint
519 Standard_Integer Nbp = points.Length();
520 Standard_Integer MinNb= (9*minNbPnts)/10;
521 //if(MinNb<4) MinNb=4;
523 //-- if(Nbp < MinNb) { cout<<"\n*"; } else { cout<<"\n."; }
525 //-- cout<<" \nGCPnts TangentialDeflection : Ajout de Points ("<<Nbp<<" "<<minNbPnts<<" )"<<endl;
526 for(i=2; i<=Nbp; i++) {
527 MiddleU = (parameters.Value(i-1)+parameters.Value(i))*0.5;
528 D0 (C, MiddleU, MiddlePoint);
529 parameters.InsertBefore(i,MiddleU);
530 points.InsertBefore(i,MiddlePoint);