Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-13 09:16:33

0001 // Copyright (c) 1991-1999 Matra Datavision
0002 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _gp_XY_HeaderFile
0016 #define _gp_XY_HeaderFile
0017 
0018 #include <gp.hxx>
0019 #include <gp_Mat2d.hxx>
0020 #include <Standard_ConstructionError.hxx>
0021 #include <Standard_OutOfRange.hxx>
0022 
0023 //! This class describes a cartesian coordinate entity in 2D
0024 //! space {X,Y}. This class is non persistent. This entity used
0025 //! for algebraic calculation. An XY can be transformed with a
0026 //! Trsf2d or a GTrsf2d from package gp.
0027 //! It is used in vectorial computations or for holding this type
0028 //! of information in data structures.
0029 class gp_XY
0030 {
0031 public:
0032   DEFINE_STANDARD_ALLOC
0033 
0034   //! Creates XY object with zero coordinates (0,0).
0035   constexpr gp_XY() noexcept
0036       : x(0.),
0037         y(0.)
0038   {
0039   }
0040 
0041   //! a number pair defined by the XY coordinates
0042   constexpr gp_XY(const double theX, const double theY) noexcept
0043       : x(theX),
0044         y(theY)
0045   {
0046   }
0047 
0048   //! modifies the coordinate of range theIndex
0049   //! theIndex = 1 => X is modified
0050   //! theIndex = 2 => Y is modified
0051   //! Raises OutOfRange if theIndex != {1, 2}.
0052   constexpr void SetCoord(const int theIndex, const double theXi)
0053   {
0054     Standard_OutOfRange_Raise_if(theIndex < 1 || theIndex > 2, nullptr);
0055     if (theIndex == 1)
0056     {
0057       x = theXi;
0058     }
0059     else
0060     {
0061       y = theXi;
0062     }
0063   }
0064 
0065   //! For this number pair, assigns
0066   //! the values theX and theY to its coordinates
0067   constexpr void SetCoord(const double theX, const double theY) noexcept
0068   {
0069     x = theX;
0070     y = theY;
0071   }
0072 
0073   //! Assigns the given value to the X coordinate of this number pair.
0074   constexpr void SetX(const double theX) noexcept { x = theX; }
0075 
0076   //! Assigns the given value to the Y coordinate of this number pair.
0077   constexpr void SetY(const double theY) noexcept { y = theY; }
0078 
0079   //! returns the coordinate of range theIndex :
0080   //! theIndex = 1 => X is returned
0081   //! theIndex = 2 => Y is returned
0082   //! Raises OutOfRange if theIndex != {1, 2}.
0083   constexpr double Coord(const int theIndex) const
0084   {
0085     Standard_OutOfRange_Raise_if(theIndex < 1 || theIndex > 2, nullptr);
0086     if (theIndex == 1)
0087     {
0088       return x;
0089     }
0090     return y;
0091   }
0092 
0093   constexpr double& ChangeCoord(const int theIndex)
0094   {
0095     Standard_OutOfRange_Raise_if(theIndex < 1 || theIndex > 2, nullptr);
0096     if (theIndex == 1)
0097     {
0098       return x;
0099     }
0100     return y;
0101   }
0102 
0103   //! For this number pair, returns its coordinates X and Y.
0104   constexpr void Coord(double& theX, double& theY) const noexcept
0105   {
0106     theX = x;
0107     theY = y;
0108   }
0109 
0110   //! Returns the X coordinate of this number pair.
0111   constexpr double X() const noexcept { return x; }
0112 
0113   //! Returns the Y coordinate of this number pair.
0114   constexpr double Y() const noexcept { return y; }
0115 
0116   //! Computes std::sqrt(X*X + Y*Y) where X and Y are the two coordinates of this number pair.
0117   double Modulus() const { return sqrt(SquareModulus()); }
0118 
0119   //! Computes X*X + Y*Y where X and Y are the two coordinates of this number pair.
0120   constexpr double SquareModulus() const noexcept { return x * x + y * y; }
0121 
0122   //! Returns true if the coordinates of this number pair are
0123   //! equal to the respective coordinates of the number pair
0124   //! theOther, within the specified tolerance theTolerance.
0125   bool IsEqual(const gp_XY& theOther, const double theTolerance) const
0126   {
0127     return (std::abs(x - theOther.x) < theTolerance) && (std::abs(y - theOther.y) < theTolerance);
0128   }
0129 
0130   //! Computes the sum of this number pair and number pair theOther
0131   //! @code
0132   //! <me>.X() = <me>.X() + theOther.X()
0133   //! <me>.Y() = <me>.Y() + theOther.Y()
0134   //! @endcode
0135   constexpr void Add(const gp_XY& theOther) noexcept
0136   {
0137     x += theOther.x;
0138     y += theOther.y;
0139   }
0140 
0141   constexpr void operator+=(const gp_XY& theOther) noexcept { Add(theOther); }
0142 
0143   //! Computes the sum of this number pair and number pair theOther
0144   //! @code
0145   //! new.X() = <me>.X() + theOther.X()
0146   //! new.Y() = <me>.Y() + theOther.Y()
0147   //! @endcode
0148   [[nodiscard]] constexpr gp_XY Added(const gp_XY& theOther) const noexcept
0149   {
0150     return gp_XY(x + theOther.X(), y + theOther.Y());
0151   }
0152 
0153   [[nodiscard]] constexpr gp_XY operator+(const gp_XY& theOther) const noexcept
0154   {
0155     return Added(theOther);
0156   }
0157 
0158   //! @code
0159   //! double D = <me>.X() * theOther.Y() - <me>.Y() * theOther.X()
0160   //! @endcode
0161   [[nodiscard]] constexpr double Crossed(const gp_XY& theOther) const noexcept
0162   {
0163     return x * theOther.y - y * theOther.x;
0164   }
0165 
0166   [[nodiscard]] constexpr double operator^(const gp_XY& theOther) const noexcept
0167   {
0168     return Crossed(theOther);
0169   }
0170 
0171   //! computes the magnitude of the cross product between <me> and
0172   //! theRight. Returns || <me> ^ theRight ||
0173   double CrossMagnitude(const gp_XY& theRight) const
0174   {
0175     return std::abs(x * theRight.y - y * theRight.x);
0176   }
0177 
0178   //! computes the square magnitude of the cross product between <me> and
0179   //! theRight. Returns || <me> ^ theRight ||**2
0180   constexpr double CrossSquareMagnitude(const gp_XY& theRight) const noexcept
0181   {
0182     const double aZresult = x * theRight.y - y * theRight.x;
0183     return aZresult * aZresult;
0184   }
0185 
0186   //! divides <me> by a real.
0187   constexpr void Divide(const double theScalar)
0188   {
0189     x /= theScalar;
0190     y /= theScalar;
0191   }
0192 
0193   constexpr void operator/=(const double theScalar) { Divide(theScalar); }
0194 
0195   //! Divides <me> by a real.
0196   [[nodiscard]] constexpr gp_XY Divided(const double theScalar) const
0197   {
0198     return gp_XY(x / theScalar, y / theScalar);
0199   }
0200 
0201   [[nodiscard]] constexpr gp_XY operator/(const double theScalar) const
0202   {
0203     return Divided(theScalar);
0204   }
0205 
0206   //! Computes the scalar product between <me> and theOther
0207   constexpr double Dot(const gp_XY& theOther) const noexcept
0208   {
0209     return x * theOther.x + y * theOther.y;
0210   }
0211 
0212   constexpr double operator*(const gp_XY& theOther) const noexcept { return Dot(theOther); }
0213 
0214   //! @code
0215   //! <me>.X() = <me>.X() * theScalar;
0216   //! <me>.Y() = <me>.Y() * theScalar;
0217   //! @endcode
0218   constexpr void Multiply(const double theScalar) noexcept
0219   {
0220     x *= theScalar;
0221     y *= theScalar;
0222   }
0223 
0224   constexpr void operator*=(const double theScalar) noexcept { Multiply(theScalar); }
0225 
0226   //! @code
0227   //! <me>.X() = <me>.X() * theOther.X();
0228   //! <me>.Y() = <me>.Y() * theOther.Y();
0229   //! @endcode
0230   constexpr void Multiply(const gp_XY& theOther) noexcept
0231   {
0232     x *= theOther.x;
0233     y *= theOther.y;
0234   }
0235 
0236   constexpr void operator*=(const gp_XY& theOther) noexcept { Multiply(theOther); }
0237 
0238   //! <me> = theMatrix * <me>
0239   constexpr void Multiply(const gp_Mat2d& theMatrix) noexcept;
0240 
0241   constexpr void operator*=(const gp_Mat2d& theMatrix) noexcept { Multiply(theMatrix); }
0242 
0243   //! @code
0244   //! New.X() = <me>.X() * theScalar;
0245   //! New.Y() = <me>.Y() * theScalar;
0246   //! @endcode
0247   [[nodiscard]] constexpr gp_XY Multiplied(const double theScalar) const noexcept
0248   {
0249     return gp_XY(x * theScalar, y * theScalar);
0250   }
0251 
0252   [[nodiscard]] constexpr gp_XY operator*(const double theScalar) const noexcept
0253   {
0254     return Multiplied(theScalar);
0255   }
0256 
0257   //! @code
0258   //! new.X() = <me>.X() * theOther.X();
0259   //! new.Y() = <me>.Y() * theOther.Y();
0260   //! @endcode
0261   [[nodiscard]] constexpr gp_XY Multiplied(const gp_XY& theOther) const noexcept
0262   {
0263     return gp_XY(x * theOther.X(), y * theOther.Y());
0264   }
0265 
0266   //! New = theMatrix * <me>
0267   [[nodiscard]] constexpr gp_XY Multiplied(const gp_Mat2d& theMatrix) const noexcept
0268   {
0269     return gp_XY(theMatrix.myMat[0][0] * x + theMatrix.myMat[0][1] * y,
0270                  theMatrix.myMat[1][0] * x + theMatrix.myMat[1][1] * y);
0271   }
0272 
0273   [[nodiscard]] constexpr gp_XY operator*(const gp_Mat2d& theMatrix) const noexcept
0274   {
0275     return Multiplied(theMatrix);
0276   }
0277 
0278   //! @code
0279   //! <me>.X() = <me>.X()/ <me>.Modulus()
0280   //! <me>.Y() = <me>.Y()/ <me>.Modulus()
0281   //! @endcode
0282   //! Raises ConstructionError if <me>.Modulus() <= Resolution from gp
0283   void Normalize();
0284 
0285   //! @code
0286   //! New.X() = <me>.X()/ <me>.Modulus()
0287   //! New.Y() = <me>.Y()/ <me>.Modulus()
0288   //! @endcode
0289   //! Raises ConstructionError if <me>.Modulus() <= Resolution from gp
0290   [[nodiscard]] gp_XY Normalized() const
0291   {
0292     double aD = Modulus();
0293     Standard_ConstructionError_Raise_if(aD <= gp::Resolution(),
0294                                         "gp_XY::Normalized() - vector has zero norm");
0295     return gp_XY(x / aD, y / aD);
0296   }
0297 
0298   //! @code
0299   //! <me>.X() = -<me>.X()
0300   //! <me>.Y() = -<me>.Y()
0301   constexpr void Reverse() noexcept
0302   {
0303     x = -x;
0304     y = -y;
0305   }
0306 
0307   //! @code
0308   //! New.X() = -<me>.X()
0309   //! New.Y() = -<me>.Y()
0310   //! @endcode
0311   [[nodiscard]] constexpr gp_XY Reversed() const noexcept { return gp_XY(-x, -y); }
0312 
0313   [[nodiscard]] constexpr gp_XY operator-() const noexcept { return Reversed(); }
0314 
0315   //! Computes the following linear combination and
0316   //! assigns the result to this number pair:
0317   //! @code
0318   //! theA1 * theXY1 + theA2 * theXY2
0319   //! @endcode
0320   constexpr void SetLinearForm(const double theA1,
0321                                const gp_XY& theXY1,
0322                                const double theA2,
0323                                const gp_XY& theXY2) noexcept
0324   {
0325     x = theA1 * theXY1.x + theA2 * theXY2.x;
0326     y = theA1 * theXY1.y + theA2 * theXY2.y;
0327   }
0328 
0329   //! Computes the following linear combination and
0330   //! assigns the result to this number pair:
0331   //! @code
0332   //! theA1 * theXY1 + theA2 * theXY2 + theXY3
0333   //! @endcode
0334   constexpr void SetLinearForm(const double theA1,
0335                                const gp_XY& theXY1,
0336                                const double theA2,
0337                                const gp_XY& theXY2,
0338                                const gp_XY& theXY3) noexcept
0339   {
0340     x = theA1 * theXY1.x + theA2 * theXY2.x + theXY3.x;
0341     y = theA1 * theXY1.y + theA2 * theXY2.y + theXY3.y;
0342   }
0343 
0344   //! Computes the following linear combination and
0345   //! assigns the result to this number pair:
0346   //! @code
0347   //! theA1 * theXY1 + theXY2
0348   //! @endcode
0349   constexpr void SetLinearForm(const double theA1,
0350                                const gp_XY& theXY1,
0351                                const gp_XY& theXY2) noexcept
0352   {
0353     x = theA1 * theXY1.x + theXY2.x;
0354     y = theA1 * theXY1.y + theXY2.y;
0355   }
0356 
0357   //! Computes the following linear combination and
0358   //! assigns the result to this number pair:
0359   //! @code
0360   //! theXY1 + theXY2
0361   //! @endcode
0362   constexpr void SetLinearForm(const gp_XY& theXY1, const gp_XY& theXY2) noexcept
0363   {
0364     x = theXY1.x + theXY2.x;
0365     y = theXY1.y + theXY2.y;
0366   }
0367 
0368   //! @code
0369   //! <me>.X() = <me>.X() - theOther.X()
0370   //! <me>.Y() = <me>.Y() - theOther.Y()
0371   //! @endcode
0372   constexpr void Subtract(const gp_XY& theOther) noexcept
0373   {
0374     x -= theOther.x;
0375     y -= theOther.y;
0376   }
0377 
0378   constexpr void operator-=(const gp_XY& theOther) noexcept { Subtract(theOther); }
0379 
0380   //! @code
0381   //! new.X() = <me>.X() - theOther.X()
0382   //! new.Y() = <me>.Y() - theOther.Y()
0383   //! @endcode
0384   [[nodiscard]] constexpr gp_XY Subtracted(const gp_XY& theOther) const noexcept
0385   {
0386     return gp_XY(x - theOther.x, y - theOther.y);
0387   }
0388 
0389   [[nodiscard]] constexpr gp_XY operator-(const gp_XY& theOther) const noexcept
0390   {
0391     return Subtracted(theOther);
0392   }
0393 
0394 private:
0395   double x;
0396   double y;
0397 };
0398 
0399 //=================================================================================================
0400 
0401 inline constexpr void gp_XY::Multiply(const gp_Mat2d& theMatrix) noexcept
0402 {
0403   const double aXresult = theMatrix.myMat[0][0] * x + theMatrix.myMat[0][1] * y;
0404   y                     = theMatrix.myMat[1][0] * x + theMatrix.myMat[1][1] * y;
0405   x                     = aXresult;
0406 }
0407 
0408 //=================================================================================================
0409 
0410 inline void gp_XY::Normalize()
0411 {
0412   double aD = Modulus();
0413   Standard_ConstructionError_Raise_if(aD <= gp::Resolution(),
0414                                       "gp_XY::Normalize() - vector has zero norm");
0415   x = x / aD;
0416   y = y / aD;
0417 }
0418 
0419 //=======================================================================
0420 // function :  operator*
0421 // purpose :
0422 //=======================================================================
0423 inline constexpr gp_XY operator*(const gp_Mat2d& theMatrix, const gp_XY& theCoord1) noexcept
0424 {
0425   return theCoord1.Multiplied(theMatrix);
0426 }
0427 
0428 //=======================================================================
0429 // function :  operator*
0430 // purpose :
0431 //=======================================================================
0432 inline constexpr gp_XY operator*(const double theScalar, const gp_XY& theCoord1) noexcept
0433 {
0434   return theCoord1.Multiplied(theScalar);
0435 }
0436 
0437 #endif // _gp_XY_HeaderFile