Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:19:57

0001 // Copyright (c) 2025 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 _ExtremaPC_BezierCurve_HeaderFile
0015 #define _ExtremaPC_BezierCurve_HeaderFile
0016 
0017 #include <ExtremaPC.hxx>
0018 #include <ExtremaPC_GridEvaluator.hxx>
0019 #include <GeomAdaptor_Curve.hxx>
0020 #include <Geom_BezierCurve.hxx>
0021 #include <gp_Pnt.hxx>
0022 #include <NCollection_Array1.hxx>
0023 #include <Standard_DefineAlloc.hxx>
0024 #include <Standard_Handle.hxx>
0025 
0026 //! @brief Point-BezierCurve extrema computation using grid-based approach.
0027 //!
0028 //! Computes the extrema between a 3D point and a Bezier curve using
0029 //! a grid-based approach with Newton refinement.
0030 //!
0031 //! The grid is cached for efficiency when performing multiple queries
0032 //! with the same parameter domain.
0033 //!
0034 //! The algorithm:
0035 //! 1. Build grid with (3 * (degree + 1)) samples using GeomGridEval
0036 //! 2. Linear scan of grid to find candidate intervals (sign changes in F(u))
0037 //! 3. Newton refinement on each candidate interval
0038 //!
0039 //! This approach is simpler and more stable than BVH-based methods,
0040 //! with comparable accuracy for typical Bezier curves.
0041 //!
0042 //! The domain is fixed at construction time and the grid is built eagerly
0043 //! for optimal performance with multiple queries.
0044 class ExtremaPC_BezierCurve
0045 {
0046 public:
0047   DEFINE_STANDARD_ALLOC
0048 
0049   //! Constructor with Bezier curve (uses full curve domain).
0050   //! Grid is built eagerly at construction time.
0051   //! @param[in] theCurve Bezier curve handle
0052   Standard_EXPORT explicit ExtremaPC_BezierCurve(const occ::handle<Geom_BezierCurve>& theCurve);
0053 
0054   //! Constructor with Bezier curve and parameter domain.
0055   //! Grid is built eagerly at construction time for the specified domain.
0056   //! @param[in] theCurve Bezier curve handle
0057   //! @param[in] theDomain parameter domain (fixed for all queries)
0058   Standard_EXPORT ExtremaPC_BezierCurve(const occ::handle<Geom_BezierCurve>& theCurve,
0059                                         const ExtremaPC::Domain1D&           theDomain);
0060 
0061   //! Copy constructor is deleted.
0062   ExtremaPC_BezierCurve(const ExtremaPC_BezierCurve&) = delete;
0063 
0064   //! Copy assignment operator is deleted.
0065   ExtremaPC_BezierCurve& operator=(const ExtremaPC_BezierCurve&) = delete;
0066 
0067   //! Move constructor.
0068   ExtremaPC_BezierCurve(ExtremaPC_BezierCurve&&) = default;
0069 
0070   //! Move assignment operator.
0071   ExtremaPC_BezierCurve& operator=(ExtremaPC_BezierCurve&&) = default;
0072 
0073   //! Evaluates point on curve at parameter.
0074   //! @param theU parameter
0075   //! @return point on curve
0076   Standard_EXPORT gp_Pnt Value(double theU) const;
0077 
0078   //! Returns true if domain is bounded (subset of curve domain).
0079   bool IsBounded() const { return true; } // Bezier curves are always bounded
0080 
0081   //! Returns the domain.
0082   const ExtremaPC::Domain1D& Domain() const { return myDomain; }
0083 
0084   //! Compute extrema between point P and the curve.
0085   //! Uses domain specified at construction time.
0086   //! @param theP query point
0087   //! @param theTol tolerance for root finding
0088   //! @param theMode search mode (MinMax, Min, or Max)
0089   //! @return const reference to result containing extrema
0090   [[nodiscard]] Standard_EXPORT const ExtremaPC::Result& Perform(
0091     const gp_Pnt&         theP,
0092     double                theTol,
0093     ExtremaPC::SearchMode theMode = ExtremaPC::SearchMode::MinMax) const;
0094 
0095   //! Compute extrema between point P and the curve including endpoints.
0096   //! Uses domain specified at construction time.
0097   //! @param theP query point
0098   //! @param theTol tolerance for root finding
0099   //! @param theMode search mode (MinMax, Min, or Max)
0100   //! @return const reference to result containing interior + endpoint extrema
0101   [[nodiscard]] Standard_EXPORT const ExtremaPC::Result& PerformWithEndpoints(
0102     const gp_Pnt&         theP,
0103     double                theTol,
0104     ExtremaPC::SearchMode theMode = ExtremaPC::SearchMode::MinMax) const;
0105 
0106   //! Returns the Bezier curve.
0107   const occ::handle<Geom_BezierCurve>& Curve() const { return myCurve; }
0108 
0109 private:
0110   //! Build grid for the curve.
0111   void buildGrid();
0112 
0113   occ::handle<Geom_BezierCurve> myCurve;     //!< Bezier curve
0114   GeomAdaptor_Curve             myAdaptor;   //!< Curve adaptor
0115   ExtremaPC::Domain1D           myDomain;    //!< Parameter domain (fixed)
0116   int                           myNbSamples; //!< Number of samples
0117 
0118   // Grid evaluator with cached state (grid, result, temporary vectors)
0119   mutable ExtremaPC_GridEvaluator myEvaluator;
0120 };
0121 
0122 #endif // _ExtremaPC_BezierCurve_HeaderFile