0031381: Foundation Classes -wrong evaluations for rational BSpline curves using...
[occt.git] / src / BSplCLib / BSplCLib_CacheParams.hxx
1 // Copyright (c) 2018 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef _BSplCLib_CacheParams_Headerfile
15 #define _BSplCLib_CacheParams_Headerfile
16
17 #include <Standard_Real.hxx>
18 #include <TColStd_Array1OfReal.hxx>
19
20 #include <BSplCLib.hxx>
21
22 //! Simple structure containing parameters describing parameterization
23 //! of a B-spline curve or a surface in one direction (U or V),
24 //! and data of the current span for its caching
25 struct BSplCLib_CacheParams
26 {
27   const Standard_Integer Degree;      ///< degree of Bezier/B-spline
28   const Standard_Boolean IsPeriodic;  ///< true of the B-spline is periodic
29   const Standard_Real FirstParameter; ///< first valid parameter
30   const Standard_Real LastParameter;  ///< last valid parameter
31
32   const Standard_Integer SpanIndexMin; ///< minimal index of span
33   const Standard_Integer SpanIndexMax; ///< maximal index of span
34
35   Standard_Real    SpanStart;    ///< parameter for the frst point of the span
36   Standard_Real    SpanLength;   ///< length of the span
37   Standard_Integer SpanIndex;    ///< index of the span
38
39   //! Constructor, prepares data structures for caching.
40   //! \param theDegree     degree of the B-spline (or Bezier)
41   //! \param thePeriodic   identify whether the B-spline is periodic
42   //! \param theFlatKnots  knots of Bezier / B-spline parameterization
43   BSplCLib_CacheParams (Standard_Integer theDegree, Standard_Boolean thePeriodic,
44                         const TColStd_Array1OfReal& theFlatKnots)
45   : Degree(theDegree),
46     IsPeriodic(thePeriodic),
47     FirstParameter(theFlatKnots.Value(theFlatKnots.Lower() + theDegree)),
48     LastParameter(theFlatKnots.Value(theFlatKnots.Upper() - theDegree)),
49     SpanIndexMin(theFlatKnots.Lower() + theDegree),
50     SpanIndexMax(theFlatKnots.Upper() - theDegree - 1),
51     SpanStart(0.),
52     SpanLength(0.),
53     SpanIndex(0)
54   {}
55
56   //! Normalizes the parameter for periodic B-splines
57   //! \param theParameter the value to be normalized into the knots array
58   Standard_Real PeriodicNormalization (Standard_Real theParameter) const
59   {
60     if (IsPeriodic)
61     {
62       if (theParameter < FirstParameter)
63       {
64         Standard_Real aPeriod = LastParameter - FirstParameter;
65         Standard_Real aScale = IntegerPart ((FirstParameter - theParameter) / aPeriod);
66         return theParameter + aPeriod * (aScale + 1.0);
67       }
68       if (theParameter > LastParameter)
69       {
70         Standard_Real aPeriod = LastParameter - FirstParameter;
71         Standard_Real aScale = IntegerPart ((theParameter - LastParameter) / aPeriod);
72         return theParameter - aPeriod * (aScale + 1.0);
73       }
74     }
75     return theParameter;
76   }
77
78   //! Verifies validity of the cache using flat parameter of the point
79   //! \param theParameter parameter of the point placed in the span
80   Standard_Boolean IsCacheValid (Standard_Real theParameter) const
81   {
82     Standard_Real aNewParam = PeriodicNormalization  (theParameter);
83     Standard_Real aDelta = aNewParam - SpanStart;
84     return ((aDelta >= 0.0 || SpanIndex == SpanIndexMin) &&
85             (aDelta < SpanLength || SpanIndex == SpanIndexMax));
86   }
87
88   //! Computes span for the specified parameter
89   //! \param theParameter parameter of the point placed in the span
90   //! \param theFlatKnots  knots of Bezier / B-spline parameterization
91   void LocateParameter (Standard_Real& theParameter, const TColStd_Array1OfReal& theFlatKnots)
92   {
93     SpanIndex = 0;
94     BSplCLib::LocateParameter (Degree, theFlatKnots, BSplCLib::NoMults(), 
95                                theParameter, IsPeriodic, SpanIndex, theParameter);
96     SpanStart  = theFlatKnots.Value(SpanIndex);
97     SpanLength = theFlatKnots.Value(SpanIndex + 1) - SpanStart;
98   }
99
100 private:
101   // copying is prohibited
102   BSplCLib_CacheParams (const BSplCLib_CacheParams&);
103   void operator = (const BSplCLib_CacheParams&);
104 };
105
106 #endif