| // Copyright (c) 2015-2025 OPEN CASCADE SAS | |
| // | |
| // This file is part of Open CASCADE Technology software library. | |
| // | |
| // This library is free software; you can redistribute it and/or modify it under | |
| // the terms of the GNU Lesser General Public License version 2.1 as published | |
| // by the Free Software Foundation, with special exception defined in the file | |
| // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT | |
| // distribution for complete text of the license and disclaimer of any warranty. | |
| // | |
| // Alternatively, this file may be used under the terms of Open CASCADE | |
| // commercial license or contractual agreement. | |
| #ifndef _Geom2d_OffsetCurveUtils_HeaderFile | |
| #define _Geom2d_OffsetCurveUtils_HeaderFile | |
| #include <Geom2d_Curve.hxx> | |
| #include <gp.hxx> | |
| #include <gp_Dir2d.hxx> | |
| #include <gp_Pnt2d.hxx> | |
| #include <gp_Vec2d.hxx> | |
| #include <gp_XY.hxx> | |
| #include <cmath> | |
| //! Internal helper namespace for 2D offset curve calculations. | |
| //! Provides static inline functions to compute offset curve point and derivatives | |
| //! from basis curve derivatives. | |
| //! | |
| //! These functions are used by Geom2d_OffsetCurve and Geom2dAdaptor_Curve. | |
| //! | |
| //! Mathematical basis: | |
| //! P(u) = p(u) + Offset * N / ||N|| | |
| //! where N = (p'(u).Y, -p'(u).X) is the normal direction (rotated tangent by 90 degrees) | |
| namespace Geom2d_OffsetCurveUtils | |
| { | |
| //! Calculates D0 (point) for 2D offset curve. | |
| //! @param[in,out] theValue on input: basis curve point; on output: offset point | |
| //! @param[in] theD1 first derivative of basis curve at the point | |
| //! @param[in] theOffset offset distance value | |
| //! @return true if successful, false if tangent vector has zero magnitude | |
| inline bool CalculateD0(gp_Pnt2d& theValue, const gp_Vec2d& theD1, double theOffset) | |
| { | |
| if (theD1.SquareMagnitude() <= gp::Resolution()) | |
| { | |
| return false; | |
| } | |
| gp_Dir2d aNormal(theD1.Y(), -theD1.X()); | |
| theValue.ChangeCoord().Add(aNormal.XY() * theOffset); | |
| return true; | |
| } | |
| //! Calculates D0 and D1 for 2D offset curve. | |
| //! @param[in,out] theValue on input: basis curve point; on output: offset point | |
| //! @param[in,out] theD1 on input: first derivative of basis; on output: offset curve D1 | |
| //! @param[in] theD2 second derivative of basis curve | |
| //! @param[in] theOffset offset distance value | |
| //! @return true if successful, false if computation failed | |
| inline bool CalculateD1(gp_Pnt2d& theValue, | |
| gp_Vec2d& theD1, | |
| const gp_Vec2d& theD2, | |
| double theOffset) | |
| { | |
| // P(u) = p(u) + Offset * Ndir / R | |
| // with R = || p' ^ Z|| and Ndir = P' ^ Z | |
| // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R)) | |
| gp_XY Ndir(theD1.Y(), -theD1.X()); | |
| gp_XY DNdir(theD2.Y(), -theD2.X()); | |
| double R2 = Ndir.SquareModulus(); | |
| double R = std::sqrt(R2); | |
| double R3 = R * R2; | |
| double Dr = Ndir.Dot(DNdir); | |
| if (R3 <= gp::Resolution()) | |
| { | |
| if (R2 <= gp::Resolution()) | |
| { | |
| return false; | |
| } | |
| // We try another computation but the stability is not very good. | |
| DNdir.Multiply(R); | |
| DNdir.Subtract(Ndir.Multiplied(Dr / R)); | |
| DNdir.Multiply(theOffset / R2); | |
| } | |
| else | |
| { | |
| // Same computation as IICURV in EUCLID-IS because the stability is better | |
| DNdir.Multiply(theOffset / R); | |
| DNdir.Subtract(Ndir.Multiplied(theOffset * Dr / R3)); | |
| } | |
| Ndir.Multiply(theOffset / R); | |
| // P(u) | |
| theValue.ChangeCoord().Add(Ndir); | |
| // P'(u) | |
| theD1.Add(gp_Vec2d(DNdir)); | |
| return true; | |
| } | |
| //! Calculates D0, D1, D2 for 2D offset curve. | |
| //! @param[in,out] theValue on input: basis curve point; on output: offset point | |
| //! @param[in,out] theD1 on input: first derivative of basis; on output: offset curve D1 | |
| //! @param[in,out] theD2 on input: second derivative of basis; on output: offset curve D2 | |
| //! @param[in] theD3 third derivative of basis curve | |
| //! @param[in] theIsDirChange flag indicating direction change at singular point | |
| //! @param[in] theOffset offset distance value | |
| //! @return true if successful, false if computation failed | |
| inline bool CalculateD2(gp_Pnt2d& theValue, | |
| gp_Vec2d& theD1, | |
| gp_Vec2d& theD2, | |
| const gp_Vec2d& theD3, | |
| bool theIsDirChange, | |
| double theOffset) | |
| { | |
| // P(u) = p(u) + Offset * Ndir / R | |
| // with R = || p' ^ Z|| and Ndir = P' ^ Z | |
| // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R)) | |
| // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) + | |
| // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2))) | |
| gp_XY Ndir(theD1.Y(), -theD1.X()); | |
| gp_XY DNdir(theD2.Y(), -theD2.X()); | |
| gp_XY D2Ndir(theD3.Y(), -theD3.X()); | |
| double R2 = Ndir.SquareModulus(); | |
| double R = std::sqrt(R2); | |
| double R3 = R2 * R; | |
| double R4 = R2 * R2; | |
| double R5 = R3 * R2; | |
| double Dr = Ndir.Dot(DNdir); | |
| double D2r = Ndir.Dot(D2Ndir) + DNdir.Dot(DNdir); | |
| if (R5 <= gp::Resolution()) | |
| { | |
| if (R4 <= gp::Resolution()) | |
| { | |
| return false; | |
| } | |
| // We try another computation but the stability is not very good dixit ISG. | |
| // V2 = P" (U) : | |
| D2Ndir.Subtract(DNdir.Multiplied(2.0 * Dr / R2)); | |
| D2Ndir.Add(Ndir.Multiplied(((3.0 * Dr * Dr) / R4) - (D2r / R2))); | |
| D2Ndir.Multiply(theOffset / R); | |
| // V1 = P' (U) : | |
| DNdir.Multiply(R); | |
| DNdir.Subtract(Ndir.Multiplied(Dr / R)); | |
| DNdir.Multiply(theOffset / R2); | |
| } | |
| else | |
| { | |
| // Same computation as IICURV in EUCLID-IS because the stability is better. | |
| // V2 = P" (U) : | |
| D2Ndir.Multiply(theOffset / R); | |
| D2Ndir.Subtract(DNdir.Multiplied(2.0 * theOffset * Dr / R3)); | |
| D2Ndir.Add(Ndir.Multiplied(theOffset * (((3.0 * Dr * Dr) / R5) - (D2r / R3)))); | |
| // V1 = P' (U) | |
| DNdir.Multiply(theOffset / R); | |
| DNdir.Subtract(Ndir.Multiplied(theOffset * Dr / R3)); | |
| } | |
| Ndir.Multiply(theOffset / R); | |
| // P(u) | |
| theValue.ChangeCoord().Add(Ndir); | |
| // P'(u) : | |
| theD1.Add(gp_Vec2d(DNdir)); | |
| // P"(u) : | |
| if (theIsDirChange) | |
| { | |
| theD2.Reverse(); | |
| } | |
| theD2.Add(gp_Vec2d(D2Ndir)); | |
| return true; | |
| } | |
| //! Calculates D0, D1, D2, D3 for 2D offset curve. | |
| //! @param[in,out] theValue on input: basis curve point; on output: offset point | |
| //! @param[in,out] theD1 on input: first derivative of basis; on output: offset curve D1 | |
| //! @param[in,out] theD2 on input: second derivative of basis; on output: offset curve D2 | |
| //! @param[in,out] theD3 on input: third derivative of basis; on output: offset curve D3 | |
| //! @param[in] theD4 fourth derivative of basis curve | |
| //! @param[in] theIsDirChange flag indicating direction change at singular point | |
| //! @param[in] theOffset offset distance value | |
| //! @return true if successful, false if computation failed | |
| inline bool CalculateD3(gp_Pnt2d& theValue, | |
| gp_Vec2d& theD1, | |
| gp_Vec2d& theD2, | |
| gp_Vec2d& theD3, | |
| const gp_Vec2d& theD4, | |
| bool theIsDirChange, | |
| double theOffset) | |
| { | |
| // P(u) = p(u) + Offset * Ndir / R | |
| // with R = || p' ^ Z|| and Ndir = P' ^ Z | |
| // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R)) | |
| // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) + | |
| // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2))) | |
| // P"'(u) = p"'(u) + (Offset / R) * (D3Ndir - (3.0 * Dr/R**2 ) * D2Ndir - | |
| // (3.0 * D2r / R2) * DNdir) + (3.0 * Dr * Dr / R4) * DNdir - | |
| // (D3r/R2) * Ndir + (6.0 * Dr * Dr / R4) * Ndir + | |
| // (6.0 * Dr * D2r / R4) * Ndir - (15.0 * Dr* Dr* Dr /R6) * Ndir | |
| gp_XY Ndir(theD1.Y(), -theD1.X()); | |
| gp_XY DNdir(theD2.Y(), -theD2.X()); | |
| gp_XY D2Ndir(theD3.Y(), -theD3.X()); | |
| gp_XY D3Ndir(theD4.Y(), -theD4.X()); | |
| double R2 = Ndir.SquareModulus(); | |
| double R = std::sqrt(R2); | |
| double R3 = R2 * R; | |
| double R4 = R2 * R2; | |
| double R5 = R3 * R2; | |
| double R6 = R3 * R3; | |
| double R7 = R5 * R2; | |
| double Dr = Ndir.Dot(DNdir); | |
| double D2r = Ndir.Dot(D2Ndir) + DNdir.Dot(DNdir); | |
| double D3r = Ndir.Dot(D3Ndir) + 3.0 * DNdir.Dot(D2Ndir); | |
| if (R7 <= gp::Resolution()) | |
| { | |
| if (R6 <= gp::Resolution()) | |
| { | |
| return false; | |
| } | |
| // We try another computation but the stability is not very good dixit ISG. | |
| // V3 = P"' (U) : | |
| D3Ndir.Subtract(D2Ndir.Multiplied(3.0 * Dr / R2)); | |
| D3Ndir.Subtract(DNdir.Multiplied(3.0 * ((D2r / R2) + (Dr * Dr / R4)))); | |
| D3Ndir.Add( | |
| Ndir.Multiplied(6.0 * Dr * Dr / R4 + 6.0 * Dr * D2r / R4 - 15.0 * Dr * Dr * Dr / R6 - D3r)); | |
| D3Ndir.Multiply(theOffset / R); | |
| // V2 = P" (U) : | |
| R4 = R2 * R2; | |
| D2Ndir.Subtract(DNdir.Multiplied(2.0 * Dr / R2)); | |
| D2Ndir.Subtract(Ndir.Multiplied((3.0 * Dr * Dr / R4) - (D2r / R2))); | |
| D2Ndir.Multiply(theOffset / R); | |
| // V1 = P' (U) : | |
| DNdir.Multiply(R); | |
| DNdir.Subtract(Ndir.Multiplied(Dr / R)); | |
| DNdir.Multiply(theOffset / R2); | |
| } | |
| else | |
| { | |
| // Same computation as IICURV in EUCLID-IS because the stability is better. | |
| // V3 = P"' (U) : | |
| D3Ndir.Multiply(theOffset / R); | |
| D3Ndir.Subtract(D2Ndir.Multiplied(3.0 * theOffset * Dr / R3)); | |
| D3Ndir.Subtract(DNdir.Multiplied(((3.0 * theOffset) * ((D2r / R3) + (Dr * Dr) / R5)))); | |
| D3Ndir.Add(Ndir.Multiplied( | |
| (theOffset * (6.0 * Dr * Dr / R5 + 6.0 * Dr * D2r / R5 - 15.0 * Dr * Dr * Dr / R7 - D3r)))); | |
| // V2 = P" (U) : | |
| D2Ndir.Multiply(theOffset / R); | |
| D2Ndir.Subtract(DNdir.Multiplied(2.0 * theOffset * Dr / R3)); | |
| D2Ndir.Subtract(Ndir.Multiplied(theOffset * (((3.0 * Dr * Dr) / R5) - (D2r / R3)))); | |
| // V1 = P' (U) : | |
| DNdir.Multiply(theOffset / R); | |
| DNdir.Subtract(Ndir.Multiplied(theOffset * Dr / R3)); | |
| } | |
| Ndir.Multiply(theOffset / R); | |
| // P(u) | |
| theValue.ChangeCoord().Add(Ndir); | |
| // P'(u) : | |
| theD1.Add(gp_Vec2d(DNdir)); | |
| // P"(u) | |
| theD2.Add(gp_Vec2d(D2Ndir)); | |
| // P"'(u) | |
| if (theIsDirChange) | |
| { | |
| theD3.Reverse(); | |
| } | |
| theD3.Add(gp_Vec2d(D3Ndir)); | |
| return true; | |
| } | |
| //! Adjusts derivatives at singular points where the first derivative is nearly zero. | |
| //! Uses Taylor series approximation to find a valid tangent direction. | |
| //! Uses EvalDN/EvalD0 to avoid exceptions. | |
| //! @tparam CurveType type supporting EvalD0, EvalDN methods (Geom2d_Curve or adaptor) | |
| //! @param[in] theCurve basis curve for derivative evaluation | |
| //! @param[in] theMaxDerivative maximum derivative order to compute (3 or 4) | |
| //! @param[in] theU parameter value | |
| //! @param[in,out] theD1 first derivative (will be adjusted) | |
| //! @param[in,out] theD2 second derivative (will be adjusted) | |
| //! @param[in,out] theD3 third derivative (will be adjusted) | |
| //! @param[in,out] theD4 fourth derivative (will be adjusted if theMaxDerivative >= 4) | |
| //! @param[out] theIsDirectionChange true if direction change detected | |
| //! @return true if evaluation succeeded, false if any evaluation failed | |
| template <typename CurveType> | |
| bool AdjustDerivative(const CurveType& theCurve, | |
| int theMaxDerivative, | |
| double theU, | |
| gp_Vec2d& theD1, | |
| gp_Vec2d& theD2, | |
| gp_Vec2d& theD3, | |
| gp_Vec2d& theD4, | |
| bool& theIsDirectionChange) | |
| { | |
| static const double aTol = gp::Resolution(); | |
| static const double aMinStep = 1e-7; | |
| static const int aMaxDerivOrder = 3; | |
| theIsDirectionChange = false; | |
| const double anUinfium = theCurve.FirstParameter(); | |
| const double anUsupremum = theCurve.LastParameter(); | |
| static const double DivisionFactor = 1.e-3; | |
| double du; | |
| if ((anUsupremum >= RealLast()) || (anUinfium <= RealFirst())) | |
| { | |
| du = 0.0; | |
| } | |
| else | |
| { | |
| du = anUsupremum - anUinfium; | |
| } | |
| const double aDelta = std::max(du * DivisionFactor, aMinStep); | |
| // Derivative is approximated by Taylor-series | |
| int anIndex = 1; // Derivative order | |
| gp_Vec2d V; | |
| do | |
| { | |
| V = theCurve.EvalDN(theU, ++anIndex); | |
| } while ((V.SquareMagnitude() <= aTol) && anIndex < aMaxDerivOrder); | |
| double u; | |
| if (theU - anUinfium < aDelta) | |
| { | |
| u = theU + aDelta; | |
| } | |
| else | |
| { | |
| u = theU - aDelta; | |
| } | |
| gp_Pnt2d aP1; | |
| gp_Pnt2d aP2; | |
| aP1 = theCurve.EvalD0(std::min(theU, u)); | |
| aP2 = theCurve.EvalD0(std::max(theU, u)); | |
| gp_Vec2d V1(aP1, aP2); | |
| theIsDirectionChange = V.Dot(V1) < 0.0; | |
| const double aSign = theIsDirectionChange ? -1.0 : 1.0; | |
| theD1 = V * aSign; | |
| gp_Vec2d* aDeriv[3] = {&theD2, &theD3, &theD4}; | |
| for (int i = 1; i < theMaxDerivative; i++) | |
| { | |
| const gp_Vec2d aDN = theCurve.EvalDN(theU, anIndex + i); | |
| *(aDeriv[i - 1]) = aDN * aSign; | |
| } | |
| return true; | |
| } | |
| //! Template function for D0 evaluation of 2D offset curve. | |
| //! Gets D1 from basis curve and computes offset point. | |
| //! | |
| //! @tparam BasisCurveType type of basis curve (must have D1 method) | |
| //! @param[in] theU parameter value | |
| //! @param[in] theBasisCurve basis curve | |
| //! @param[in] theOffset offset distance | |
| //! @param[out] theValue computed offset point | |
| //! @return true if evaluation succeeded, false if normal is undefined | |
| template <typename BasisCurveType> | |
| bool EvaluateD0(double theU, | |
| const BasisCurveType& theBasisCurve, | |
| double theOffset, | |
| gp_Pnt2d& theValue) | |
| { | |
| const Geom2d_Curve::ResD1 aD1 = theBasisCurve->EvalD1(theU); | |
| theValue = aD1.Point; | |
| return CalculateD0(theValue, aD1.D1, theOffset); | |
| } | |
| //! Template function for D1 evaluation of 2D offset curve. | |
| //! Gets D2 from basis curve and computes offset point and first derivative. | |
| //! | |
| //! @tparam BasisCurveType type of basis curve (must have D2 method) | |
| //! @param[in] theU parameter value | |
| //! @param[in] theBasisCurve basis curve | |
| //! @param[in] theOffset offset distance | |
| //! @param[out] theValue computed offset point | |
| //! @param[out] theD1 computed first derivative | |
| //! @return true if evaluation succeeded, false if computation failed | |
| template <typename BasisCurveType> | |
| bool EvaluateD1(double theU, | |
| const BasisCurveType& theBasisCurve, | |
| double theOffset, | |
| gp_Pnt2d& theValue, | |
| gp_Vec2d& theD1) | |
| { | |
| const Geom2d_Curve::ResD2 aD2 = theBasisCurve->EvalD2(theU); | |
| theValue = aD2.Point; | |
| theD1 = aD2.D1; | |
| return CalculateD1(theValue, theD1, aD2.D2, theOffset); | |
| } | |
| //! Template function for D2 evaluation of 2D offset curve. | |
| //! Gets D3 from basis curve and computes offset point and derivatives. | |
| //! Handles singular points where first derivative is nearly zero. | |
| //! | |
| //! @tparam BasisCurveType type of basis curve (must have D3 and DN methods) | |
| //! @param[in] theU parameter value | |
| //! @param[in] theBasisCurve basis curve | |
| //! @param[in] theOffset offset distance | |
| //! @param[out] theValue computed offset point | |
| //! @param[out] theD1 computed first derivative | |
| //! @param[out] theD2 computed second derivative | |
| //! @return true if evaluation succeeded, false if computation failed | |
| template <typename BasisCurveType> | |
| bool EvaluateD2(double theU, | |
| const BasisCurveType& theBasisCurve, | |
| double theOffset, | |
| gp_Pnt2d& theValue, | |
| gp_Vec2d& theD1, | |
| gp_Vec2d& theD2) | |
| { | |
| const Geom2d_Curve::ResD3 aD3Result = theBasisCurve->EvalD3(theU); | |
| theValue = aD3Result.Point; | |
| theD1 = aD3Result.D1; | |
| theD2 = aD3Result.D2; | |
| gp_Vec2d aD3 = aD3Result.D3; | |
| bool isDirectionChange = false; | |
| if (theD1.SquareMagnitude() <= gp::Resolution()) | |
| { | |
| gp_Vec2d aDummyD4; | |
| if (!AdjustDerivative(*theBasisCurve, 3, theU, theD1, theD2, aD3, aDummyD4, isDirectionChange)) | |
| return false; | |
| } | |
| return CalculateD2(theValue, theD1, theD2, aD3, isDirectionChange, theOffset); | |
| } | |
| //! Template function for D3 evaluation of 2D offset curve. | |
| //! Gets D3 and D4 from basis curve and computes offset point and derivatives. | |
| //! Handles singular points where first derivative is nearly zero. | |
| //! | |
| //! @tparam BasisCurveType type of basis curve (must have D3 and DN methods) | |
| //! @param[in] theU parameter value | |
| //! @param[in] theBasisCurve basis curve | |
| //! @param[in] theOffset offset distance | |
| //! @param[out] theValue computed offset point | |
| //! @param[out] theD1 computed first derivative | |
| //! @param[out] theD2 computed second derivative | |
| //! @param[out] theD3 computed third derivative | |
| //! @return true if evaluation succeeded, false if computation failed | |
| template <typename BasisCurveType> | |
| bool EvaluateD3(double theU, | |
| const BasisCurveType& theBasisCurve, | |
| double theOffset, | |
| gp_Pnt2d& theValue, | |
| gp_Vec2d& theD1, | |
| gp_Vec2d& theD2, | |
| gp_Vec2d& theD3) | |
| { | |
| const Geom2d_Curve::ResD3 aD3Result = theBasisCurve->EvalD3(theU); | |
| theValue = aD3Result.Point; | |
| theD1 = aD3Result.D1; | |
| theD2 = aD3Result.D2; | |
| theD3 = aD3Result.D3; | |
| gp_Vec2d aD4 = theBasisCurve->EvalDN(theU, 4); | |
| bool isDirectionChange = false; | |
| if (theD1.SquareMagnitude() <= gp::Resolution()) | |
| { | |
| if (!AdjustDerivative(*theBasisCurve, 4, theU, theD1, theD2, theD3, aD4, isDirectionChange)) | |
| return false; | |
| } | |
| return CalculateD3(theValue, theD1, theD2, theD3, aD4, isDirectionChange, theOffset); | |
| } | |
| //! Template function for DN evaluation of 2D offset curve. | |
| //! Handles derivatives up to order 3 using D1/D2/D3 methods. | |
| //! For derivatives > 3, returns the basis curve derivative directly | |
| //! (offset contribution is negligible for high-order derivatives). | |
| //! | |
| //! @tparam BasisCurveType type of basis curve (must have D1, D2, D3, DN methods) | |
| //! @param[in] theU parameter value | |
| //! @param[in] theBasisCurve basis curve | |
| //! @param[in] theOffset offset distance | |
| //! @param[in] theN derivative order (must be >= 1) | |
| //! @param[out] theDN computed N-th derivative | |
| //! @return true if evaluation succeeded, false if computation failed | |
| template <typename BasisCurveType> | |
| bool EvaluateDN(double theU, | |
| const BasisCurveType& theBasisCurve, | |
| double theOffset, | |
| int theN, | |
| gp_Vec2d& theDN) | |
| { | |
| gp_Pnt2d aPnt; | |
| gp_Vec2d aDummy; | |
| switch (theN) | |
| { | |
| case 1: | |
| return EvaluateD1(theU, theBasisCurve, theOffset, aPnt, theDN); | |
| case 2: | |
| return EvaluateD2(theU, theBasisCurve, theOffset, aPnt, aDummy, theDN); | |
| case 3: | |
| return EvaluateD3(theU, theBasisCurve, theOffset, aPnt, aDummy, aDummy, theDN); | |
| default: { | |
| // For derivatives > 3, return basis curve derivative (no offset contribution) | |
| theDN = theBasisCurve->EvalDN(theU, theN); | |
| return true; | |
| } | |
| } | |
| } | |
| } // namespace Geom2d_OffsetCurveUtils | |
| #endif // _Geom2d_OffsetCurveUtils_HeaderFile |