Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/BSplCLib_CacheParams.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (c) 2018 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _BSplCLib_CacheParams_Headerfile
0015 #define _BSplCLib_CacheParams_Headerfile
0016 
0017 #include <BSplCLib.hxx>
0018 
0019 #include <algorithm>
0020 #include <cmath>
0021 
0022 //! Simple structure containing parameters describing parameterization
0023 //! of a B-spline curve or a surface in one direction (U or V),
0024 //! and data of the current span for its caching
0025 struct BSplCLib_CacheParams
0026 {
0027   const int    Degree;         ///< degree of Bezier/B-spline
0028   const bool   IsPeriodic;     ///< true of the B-spline is periodic
0029   const double FirstParameter; ///< first valid parameter
0030   const double LastParameter;  ///< last valid parameter
0031 
0032   const int SpanIndexMin; ///< minimal index of span
0033   const int SpanIndexMax; ///< maximal index of span
0034 
0035   double SpanStart;  ///< parameter for the frst point of the span
0036   double SpanLength; ///< length of the span
0037   int    SpanIndex;  ///< index of the span
0038 
0039   //! Constructor, prepares data structures for caching.
0040   //! \param theDegree     degree of the B-spline (or Bezier)
0041   //! \param thePeriodic   identify whether the B-spline is periodic
0042   //! \param theFlatKnots  knots of Bezier / B-spline parameterization
0043   BSplCLib_CacheParams(int                               theDegree,
0044                        bool                              thePeriodic,
0045                        const NCollection_Array1<double>& theFlatKnots)
0046       : Degree(theDegree),
0047         IsPeriodic(thePeriodic),
0048         FirstParameter(theFlatKnots.Value(theFlatKnots.Lower() + theDegree)),
0049         LastParameter(theFlatKnots.Value(theFlatKnots.Upper() - theDegree)),
0050         SpanIndexMin(theFlatKnots.Lower() + theDegree),
0051         SpanIndexMax(theFlatKnots.Upper() - theDegree - 1),
0052         SpanStart(0.0),
0053         SpanLength(0.0),
0054         SpanIndex(0)
0055   {
0056   }
0057 
0058   //! Normalizes the parameter for periodic B-splines
0059   //! \param theParameter the value to be normalized into the knots array
0060   double PeriodicNormalization(double theParameter) const noexcept
0061   {
0062     if (IsPeriodic)
0063     {
0064       const double aPeriod = LastParameter - FirstParameter;
0065       if (theParameter < FirstParameter)
0066       {
0067         const double aScale = std::trunc((FirstParameter - theParameter) / aPeriod);
0068         return theParameter + aPeriod * (aScale + 1.0);
0069       }
0070       if (theParameter > LastParameter)
0071       {
0072         const double aScale = std::trunc((theParameter - LastParameter) / aPeriod);
0073         return theParameter - aPeriod * (aScale + 1.0);
0074       }
0075     }
0076     return theParameter;
0077   }
0078 
0079   //! Verifies validity of the cache using flat parameter of the point
0080   //! \param theParameter parameter of the point placed in the span
0081   bool IsCacheValid(double theParameter) const noexcept
0082   {
0083     const double aNewParam = PeriodicNormalization(theParameter);
0084     const double aDelta    = aNewParam - SpanStart;
0085     if ((aDelta < 0.0 && SpanIndex != SpanIndexMin)
0086         || (aDelta >= SpanLength && SpanIndex != SpanIndexMax))
0087     {
0088       return false;
0089     }
0090 
0091     if (SpanIndex == SpanIndexMax)
0092       return true;
0093 
0094     // from BSplCLib::LocateParameter() check hitting of the next knot
0095     // within double floating point precision
0096     const double anEps        = Epsilon((std::min)(std::fabs(LastParameter), std::fabs(aNewParam)));
0097     const double aDeltaToNext = std::fabs(aDelta - SpanLength);
0098     return aDeltaToNext > anEps; // next knot should be used instead
0099   }
0100 
0101   //! Computes span for the specified parameter
0102   //! \param theParameter parameter of the point placed in the span
0103   //! \param theFlatKnots  knots of Bezier / B-spline parameterization
0104   void LocateParameter(double& theParameter, const NCollection_Array1<double>& theFlatKnots)
0105   {
0106     SpanIndex = 0;
0107     BSplCLib::LocateParameter(Degree,
0108                               theFlatKnots,
0109                               BSplCLib::NoMults(),
0110                               theParameter,
0111                               IsPeriodic,
0112                               SpanIndex,
0113                               theParameter);
0114     SpanStart  = theFlatKnots.Value(SpanIndex);
0115     SpanLength = theFlatKnots.Value(SpanIndex + 1) - SpanStart;
0116   }
0117 
0118   // copying is prohibited
0119   BSplCLib_CacheParams(const BSplCLib_CacheParams&)            = delete;
0120   BSplCLib_CacheParams& operator=(const BSplCLib_CacheParams&) = delete;
0121 };
0122 
0123 #endif