Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:46:57

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 
0033   DEFINE_STANDARD_ALLOC
0034 
0035   //! Creates XY object with zero coordinates (0,0).
0036   gp_XY()
0037   : x (0.),
0038     y (0.)
0039   {}
0040 
0041   //! a number pair defined by the XY coordinates
0042   gp_XY (const Standard_Real theX, const Standard_Real theY)
0043   : x (theX),
0044     y (theY)
0045   {}
0046 
0047   //! modifies the coordinate of range theIndex
0048   //! theIndex = 1 => X is modified
0049   //! theIndex = 2 => Y is modified
0050   //! Raises OutOfRange if theIndex != {1, 2}.
0051   inline void SetCoord (const Standard_Integer theIndex, const Standard_Real theXi)
0052   {
0053     Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > 2, NULL);
0054     (&x)[theIndex - 1] = theXi;
0055   }
0056 
0057   //! For this number pair, assigns
0058   //! the values theX and theY to its coordinates
0059   inline void SetCoord (const Standard_Real theX, const Standard_Real theY)
0060   {
0061     x = theX;
0062     y = theY;
0063   }
0064 
0065   //! Assigns the given value to the X coordinate of this number pair.
0066   void SetX (const Standard_Real theX) { x = theX; }
0067 
0068   //! Assigns the given value to the Y  coordinate of this number pair.
0069   void SetY (const Standard_Real theY) { y = theY; }
0070 
0071   //! returns the coordinate of range theIndex :
0072   //! theIndex = 1 => X is returned
0073   //! theIndex = 2 => Y is returned
0074   //! Raises OutOfRange if theIndex != {1, 2}.
0075   inline Standard_Real Coord (const Standard_Integer theIndex) const
0076   {
0077     Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > 2, NULL);
0078     return (&x)[theIndex - 1];
0079   }
0080 
0081   inline Standard_Real& ChangeCoord (const Standard_Integer theIndex)
0082   {
0083     Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > 2, NULL);
0084     return (&x)[theIndex - 1];
0085   }
0086 
0087   //! For this number pair, returns its coordinates X and Y.
0088   inline void Coord (Standard_Real& theX, Standard_Real& theY) const
0089   {
0090     theX = x;
0091     theY = y;
0092   }
0093 
0094   //! Returns the X coordinate of this number pair.
0095   Standard_Real X() const { return x; }
0096 
0097   //! Returns the Y coordinate of this number pair.
0098   Standard_Real Y() const { return y; }
0099 
0100   //! Computes Sqrt (X*X + Y*Y) where X and Y are the two coordinates of this number pair.
0101   Standard_Real Modulus() const { return sqrt (x * x + y * y); }
0102 
0103   //! Computes X*X + Y*Y where X and Y are the two coordinates of this number pair.
0104   Standard_Real SquareModulus() const { return x * x + y * y; }
0105 
0106   //! Returns true if the coordinates of this number pair are
0107   //! equal to the respective coordinates of the number pair
0108   //! theOther, within the specified tolerance theTolerance. I.e.:
0109   //! abs(<me>.X() - theOther.X()) <= theTolerance and
0110   //! abs(<me>.Y() - theOther.Y()) <= theTolerance and
0111   //! computations
0112   Standard_EXPORT Standard_Boolean IsEqual (const gp_XY& theOther, const Standard_Real theTolerance) const;
0113 
0114   //! Computes the sum of this number pair and number pair theOther
0115   //! @code
0116   //! <me>.X() = <me>.X() + theOther.X()
0117   //! <me>.Y() = <me>.Y() + theOther.Y()
0118   //! @endcode
0119   inline void Add (const gp_XY& theOther)
0120   {
0121     x += theOther.x;
0122     y += theOther.y;
0123   }
0124 
0125   void operator+= (const gp_XY& theOther) { Add (theOther); }
0126 
0127   //! Computes the sum of this number pair and number pair theOther
0128   //! @code
0129   //! new.X() = <me>.X() + theOther.X()
0130   //! new.Y() = <me>.Y() + theOther.Y()
0131   //! @endcode
0132   Standard_NODISCARD gp_XY Added (const gp_XY& theOther) const
0133   {
0134     return gp_XY (x + theOther.X(), y + theOther.Y());
0135   }
0136 
0137   Standard_NODISCARD gp_XY operator+ (const gp_XY& theOther) const { return Added (theOther); }
0138 
0139   //! @code
0140   //! double D = <me>.X() * theOther.Y() - <me>.Y() * theOther.X()
0141   //! @endcode
0142   Standard_NODISCARD Standard_Real Crossed (const gp_XY& theOther) const { return x * theOther.y - y * theOther.x; }
0143 
0144   Standard_NODISCARD Standard_Real operator^ (const gp_XY& theOther) const { return Crossed (theOther); }
0145 
0146   //! computes the magnitude of the cross product between <me> and
0147   //! theRight. Returns || <me> ^ theRight ||
0148   inline Standard_Real CrossMagnitude (const gp_XY& theRight) const
0149   {
0150     Standard_Real aVal = x * theRight.y - y * theRight.x;
0151     return aVal < 0 ? -aVal : aVal;
0152   }
0153 
0154   //! computes the square magnitude of the cross product between <me> and
0155   //! theRight. Returns || <me> ^ theRight ||**2
0156   inline Standard_Real CrossSquareMagnitude (const gp_XY& theRight) const
0157   {
0158     Standard_Real aZresult = x * theRight.y - y * theRight.x;
0159     return aZresult * aZresult;
0160   }
0161 
0162   //! divides <me> by a real.
0163   void Divide (const Standard_Real theScalar)
0164   {
0165     x /= theScalar;
0166     y /= theScalar;
0167   }
0168 
0169   void operator /= (const Standard_Real theScalar) { Divide (theScalar); }
0170 
0171   //! Divides <me> by a real.
0172   Standard_NODISCARD gp_XY Divided (const Standard_Real theScalar) const
0173   {
0174     return gp_XY (x / theScalar, y / theScalar);
0175   }
0176 
0177   Standard_NODISCARD gp_XY operator/ (const Standard_Real theScalar) const { return Divided (theScalar); }
0178 
0179   //! Computes the scalar product between <me> and theOther
0180   Standard_Real Dot (const gp_XY& theOther) const { return x * theOther.x + y * theOther.y; }
0181 
0182   Standard_Real operator* (const gp_XY& theOther) const { return Dot (theOther); }
0183 
0184   //! @code
0185   //! <me>.X() = <me>.X() * theScalar;
0186   //! <me>.Y() = <me>.Y() * theScalar;
0187   //! @endcode
0188   void Multiply (const Standard_Real theScalar)
0189   {
0190     x *= theScalar;
0191     y *= theScalar;
0192   }
0193 
0194   void operator*= (const Standard_Real theScalar) { Multiply (theScalar); }
0195 
0196   //! @code
0197   //! <me>.X() = <me>.X() * theOther.X();
0198   //! <me>.Y() = <me>.Y() * theOther.Y();
0199   //! @endcode
0200   void Multiply (const gp_XY& theOther)
0201   {
0202     x *= theOther.x;
0203     y *= theOther.y;
0204   }
0205 
0206   void operator*= (const gp_XY& theOther) { Multiply (theOther); }
0207 
0208   //! <me> = theMatrix * <me>
0209   void Multiply (const gp_Mat2d& theMatrix);
0210 
0211   void operator*= (const gp_Mat2d& theMatrix) { Multiply (theMatrix); }
0212 
0213   //! @code
0214   //! New.X() = <me>.X() * theScalar;
0215   //! New.Y() = <me>.Y() * theScalar;
0216   //! @endcode
0217   Standard_NODISCARD gp_XY Multiplied (const Standard_Real theScalar) const { return gp_XY (x * theScalar, y * theScalar); }
0218 
0219   Standard_NODISCARD gp_XY operator*  (const Standard_Real theScalar) const { return Multiplied (theScalar); }
0220   //! @code
0221   //! new.X() = <me>.X() * theOther.X();
0222   //! new.Y() = <me>.Y() * theOther.Y();
0223   //! @endcode
0224   Standard_NODISCARD gp_XY Multiplied (const gp_XY& theOther) const { return gp_XY (x * theOther.X(), y * theOther.Y()); }
0225 
0226   //! New = theMatrix * <me>
0227   Standard_NODISCARD gp_XY Multiplied (const gp_Mat2d& theMatrix) const
0228   {
0229     return gp_XY (theMatrix.Value (1, 1) * x + theMatrix.Value (1, 2) * y,
0230                   theMatrix.Value (2, 1) * x + theMatrix.Value (2, 2) * y);
0231   }
0232 
0233   Standard_NODISCARD gp_XY operator*  (const gp_Mat2d& theMatrix) const { return Multiplied (theMatrix); }
0234   //! @code
0235   //! <me>.X() = <me>.X()/ <me>.Modulus()
0236   //! <me>.Y() = <me>.Y()/ <me>.Modulus()
0237   //! @endcode
0238   //! Raises ConstructionError if <me>.Modulus() <= Resolution from gp
0239   void Normalize();
0240 
0241   //! @code
0242   //! New.X() = <me>.X()/ <me>.Modulus()
0243   //! New.Y() = <me>.Y()/ <me>.Modulus()
0244   //! @endcode
0245   //! Raises ConstructionError if <me>.Modulus() <= Resolution from gp
0246   Standard_NODISCARD gp_XY Normalized() const
0247   {
0248     Standard_Real aD = Modulus();
0249     Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_XY::Normalized() - vector has zero norm");
0250     return gp_XY (x / aD, y / aD);
0251   }
0252 
0253   //! @code
0254   //! <me>.X() = -<me>.X()
0255   //! <me>.Y() = -<me>.Y()
0256   inline void Reverse()
0257   {
0258     x = -x;
0259     y = -y;
0260   }
0261 
0262   //! @code
0263   //! New.X() = -<me>.X()
0264   //! New.Y() = -<me>.Y()
0265   //! @endcode
0266   Standard_NODISCARD gp_XY Reversed() const
0267   {
0268     gp_XY aCoord2D = *this;
0269     aCoord2D.Reverse();
0270     return aCoord2D;
0271   }
0272 
0273   Standard_NODISCARD gp_XY operator-() const { return Reversed(); }
0274 
0275   //! Computes  the following linear combination and
0276   //! assigns the result to this number pair:
0277   //! @code
0278   //! theA1 * theXY1 + theA2 * theXY2
0279   //! @endcode
0280   inline void SetLinearForm (const Standard_Real theA1, const gp_XY& theXY1,
0281                              const Standard_Real theA2, const gp_XY& theXY2)
0282   {
0283     x = theA1 * theXY1.x + theA2 * theXY2.x;
0284     y = theA1 * theXY1.y + theA2 * theXY2.y;
0285   }
0286 
0287   //! --  Computes  the following linear combination and
0288   //! assigns the result to this number pair:
0289   //! @code
0290   //! theA1 * theXY1 + theA2 * theXY2 + theXY3
0291   //! @endcode
0292   inline void SetLinearForm (const Standard_Real theA1, const gp_XY& theXY1,
0293                              const Standard_Real theA2, const gp_XY& theXY2,
0294                              const gp_XY& theXY3)
0295   {
0296     x = theA1 * theXY1.x + theA2 * theXY2.x + theXY3.x;
0297     y = theA1 * theXY1.y + theA2 * theXY2.y + theXY3.y;
0298   }
0299 
0300   //! Computes  the following linear combination and
0301   //! assigns the result to this number pair:
0302   //! @code
0303   //! theA1 * theXY1 + theXY2
0304   //! @endcode
0305   inline void SetLinearForm (const Standard_Real theA1, const gp_XY& theXY1,
0306                              const gp_XY& theXY2)
0307   {
0308     x = theA1 * theXY1.x + theXY2.x;
0309     y = theA1 * theXY1.y + theXY2.y;
0310   }
0311 
0312   //! Computes  the following linear combination and
0313   //! assigns the result to this number pair:
0314   //! @code
0315   //! theXY1 + theXY2
0316   //! @endcode
0317   inline void SetLinearForm (const gp_XY& theXY1,
0318                              const gp_XY& theXY2)
0319   {
0320     x = theXY1.x + theXY2.x;
0321     y = theXY1.y + theXY2.y;
0322   }
0323 
0324   //! @code
0325   //! <me>.X() = <me>.X() - theOther.X()
0326   //! <me>.Y() = <me>.Y() - theOther.Y()
0327   //! @endcode
0328   inline void Subtract (const gp_XY& theOther)
0329   {
0330     x -= theOther.x;
0331     y -= theOther.y;
0332   }
0333 
0334   void operator-= (const gp_XY& theOther) { Subtract (theOther); }
0335 
0336   //! @code
0337   //! new.X() = <me>.X() - theOther.X()
0338   //! new.Y() = <me>.Y() - theOther.Y()
0339   //! @endcode
0340   Standard_NODISCARD gp_XY Subtracted (const gp_XY& theOther) const
0341   {
0342     gp_XY aCoord2D = *this;
0343     aCoord2D.Subtract (theOther);
0344     return aCoord2D;
0345   }
0346 
0347   Standard_NODISCARD gp_XY operator-  (const gp_XY& theOther) const { return Subtracted (theOther); }
0348 
0349 private:
0350 
0351   Standard_Real x;
0352   Standard_Real y;
0353 
0354 };
0355 
0356 //=======================================================================
0357 //function :  Multiply
0358 // purpose :
0359 //=======================================================================
0360 inline void gp_XY::Multiply (const gp_Mat2d& theMatrix)
0361 {
0362   Standard_Real aXresult = theMatrix.Value (1, 1) * x + theMatrix.Value (1, 2) * y;
0363   y = theMatrix.Value (2, 1) * x + theMatrix.Value (2, 2) * y;
0364   x = aXresult;
0365 }
0366 
0367 //=======================================================================
0368 //function :  Normalize
0369 // purpose :
0370 //=======================================================================
0371 inline void gp_XY::Normalize()
0372 {
0373   Standard_Real aD = Modulus();
0374   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_XY::Normalize() - vector has zero norm");
0375   x = x / aD;
0376   y = y / aD;
0377 }
0378 
0379 //=======================================================================
0380 //function :  operator*
0381 // purpose :
0382 //=======================================================================
0383 inline gp_XY operator* (const gp_Mat2d& theMatrix,
0384                         const gp_XY&    theCoord1)
0385 {
0386   return theCoord1.Multiplied (theMatrix);
0387 }
0388 
0389 //=======================================================================
0390 //function :  operator*
0391 // purpose :
0392 //=======================================================================
0393 inline gp_XY operator* (const Standard_Real theScalar,
0394                         const gp_XY&        theCoord1)
0395 {
0396   return theCoord1.Multiplied (theScalar);
0397 }
0398 
0399 #endif // _gp_XY_HeaderFile