Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/gp_Dir2d.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) 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_Dir2d_HeaderFile
0016 #define _gp_Dir2d_HeaderFile
0017 
0018 #include <gp_XY.hxx>
0019 #include <Standard_ConstructionError.hxx>
0020 #include <Standard_DomainError.hxx>
0021 #include <Standard_OutOfRange.hxx>
0022 
0023 class gp_Vec2d;
0024 class gp_Ax2d;
0025 class gp_Trsf2d;
0026 
0027 //! Describes a unit vector in the plane (2D space). This unit
0028 //! vector is also called "Direction".
0029 //! See Also
0030 //! gce_MakeDir2d which provides functions for more
0031 //! complex unit vector constructions
0032 //! Geom2d_Direction which provides additional functions
0033 //! for constructing unit vectors and works, in particular, with
0034 //! the parametric equations of unit vectors
0035 class gp_Dir2d 
0036 {
0037 public:
0038 
0039   DEFINE_STANDARD_ALLOC
0040 
0041   //! Creates a direction corresponding to X axis.
0042   gp_Dir2d()
0043   : coord (1., 0.)
0044   {}
0045 
0046   //! Normalizes the vector theV and creates a Direction. Raises ConstructionError if theV.Magnitude() <= Resolution from gp.
0047   gp_Dir2d (const gp_Vec2d& theV);
0048 
0049   //! Creates a Direction from a doublet of coordinates. Raises ConstructionError if theCoord.Modulus() <= Resolution from gp.
0050   gp_Dir2d (const gp_XY& theCoord);
0051 
0052   //! Creates a Direction with its 2 cartesian coordinates. Raises ConstructionError if Sqrt(theXv*theXv + theYv*theYv) <= Resolution from gp.
0053   gp_Dir2d (const Standard_Real theXv, const Standard_Real theYv);
0054 
0055   //! For this unit vector, assigns:
0056   //! the value theXi to:
0057   //! -   the X coordinate if theIndex is 1, or
0058   //! -   the Y coordinate if theIndex is 2, and then normalizes it.
0059   //! Warning
0060   //! Remember that all the coordinates of a unit vector are
0061   //! implicitly modified when any single one is changed directly.
0062   //! Exceptions
0063   //! Standard_OutOfRange if theIndex is not 1 or 2.
0064   //! Standard_ConstructionError if either of the following
0065   //! is less than or equal to gp::Resolution():
0066   //! -   Sqrt(theXv*theXv + theYv*theYv), or
0067   //! -   the modulus of the number pair formed by the new
0068   //! value theXi and the other coordinate of this vector that
0069   //! was not directly modified.
0070   //! Raises OutOfRange if theIndex != {1, 2}.
0071   void SetCoord (const Standard_Integer theIndex, const Standard_Real theXi);
0072 
0073   //! For this unit vector, assigns:
0074   //! -   the values theXv and theYv to its two coordinates,
0075   //! Warning
0076   //! Remember that all the coordinates of a unit vector are
0077   //! implicitly modified when any single one is changed directly.
0078   //! Exceptions
0079   //! Standard_OutOfRange if theIndex is not 1 or 2.
0080   //! Standard_ConstructionError if either of the following
0081   //! is less than or equal to gp::Resolution():
0082   //! -   Sqrt(theXv*theXv + theYv*theYv), or
0083   //! -   the modulus of the number pair formed by the new
0084   //! value Xi and the other coordinate of this vector that
0085   //! was not directly modified.
0086   //! Raises OutOfRange if theIndex != {1, 2}.
0087   void SetCoord (const Standard_Real theXv, const Standard_Real theYv);
0088 
0089   //! Assigns the given value to the X coordinate of this unit   vector,
0090   //! and then normalizes it.
0091   //! Warning
0092   //! Remember that all the coordinates of a unit vector are
0093   //! implicitly modified when any single one is changed directly.
0094   //! Exceptions
0095   //! Standard_ConstructionError if either of the following
0096   //! is less than or equal to gp::Resolution():
0097   //! -   the modulus of Coord, or
0098   //! -   the modulus of the number pair formed from the new
0099   //! X or Y coordinate and the other coordinate of this
0100   //! vector that was not directly modified.
0101   void SetX (const Standard_Real theX);
0102 
0103   //! Assigns  the given value to the Y coordinate of this unit   vector,
0104   //! and then normalizes it.
0105   //! Warning
0106   //! Remember that all the coordinates of a unit vector are
0107   //! implicitly modified when any single one is changed directly.
0108   //! Exceptions
0109   //! Standard_ConstructionError if either of the following
0110   //! is less than or equal to gp::Resolution():
0111   //! -   the modulus of Coord, or
0112   //! -   the modulus of the number pair formed from the new
0113   //! X or Y coordinate and the other coordinate of this
0114   //! vector that was not directly modified.
0115   void SetY (const Standard_Real theY);
0116 
0117   //! Assigns:
0118   //! -   the two coordinates of theCoord to this unit vector,
0119   //! and then normalizes it.
0120   //! Warning
0121   //! Remember that all the coordinates of a unit vector are
0122   //! implicitly modified when any single one is changed directly.
0123   //! Exceptions
0124   //! Standard_ConstructionError if either of the following
0125   //! is less than or equal to gp::Resolution():
0126   //! -   the modulus of theCoord, or
0127   //! -   the modulus of the number pair formed from the new
0128   //! X or Y coordinate and the other coordinate of this
0129   //! vector that was not directly modified.
0130   void SetXY (const gp_XY& theCoord);
0131 
0132   //! For this unit vector returns the coordinate of range theIndex :
0133   //! theIndex = 1 => X is returned
0134   //! theIndex = 2 => Y is returned
0135   //! Raises OutOfRange if theIndex != {1, 2}.
0136   Standard_Real Coord (const Standard_Integer theIndex) const  { return coord.Coord (theIndex); }
0137 
0138   //! For this unit vector returns its two coordinates theXv and theYv.
0139   //! Raises OutOfRange if theIndex != {1, 2}.
0140   void Coord (Standard_Real& theXv, Standard_Real& theYv) const  { coord.Coord (theXv, theYv); }
0141 
0142   //! For this unit vector, returns its X coordinate.
0143   Standard_Real X() const { return coord.X(); }
0144 
0145   //! For this unit vector, returns its Y coordinate.
0146   Standard_Real Y() const { return coord.Y(); }
0147 
0148   //! For this unit vector, returns its two coordinates as a number pair.
0149   //! Comparison between Directions
0150   //! The precision value is an input data.
0151   const gp_XY& XY() const { return coord; }
0152 
0153   //! Returns True if the two vectors have the same direction
0154   //! i.e. the angle between this unit vector and the
0155   //! unit vector theOther is less than or equal to theAngularTolerance.
0156   Standard_Boolean IsEqual (const gp_Dir2d& theOther, const Standard_Real theAngularTolerance) const;
0157 
0158   //! Returns True if the angle between this unit vector and the
0159   //! unit vector theOther is equal to Pi/2 or -Pi/2 (normal)
0160   //! i.e. Abs(Abs(<me>.Angle(theOther)) - PI/2.) <= theAngularTolerance
0161   Standard_Boolean IsNormal (const gp_Dir2d& theOther, const Standard_Real theAngularTolerance) const;
0162 
0163   //! Returns True if the angle between this unit vector and the
0164   //! unit vector theOther is equal to Pi or -Pi (opposite).
0165   //! i.e.  PI - Abs(<me>.Angle(theOther)) <= theAngularTolerance
0166   Standard_Boolean IsOpposite (const gp_Dir2d& theOther, const Standard_Real theAngularTolerance) const;
0167 
0168   //! returns true if the angle between this unit vector and unit
0169   //! vector theOther is equal to 0, Pi or -Pi.
0170   //! i.e.  Abs(Angle(<me>, theOther)) <= theAngularTolerance or
0171   //! PI - Abs(Angle(<me>, theOther)) <= theAngularTolerance
0172   Standard_Boolean IsParallel (const gp_Dir2d& theOther, const Standard_Real theAngularTolerance) const;
0173 
0174   //! Computes the angular value in radians between <me> and
0175   //! <theOther>. Returns the angle in the range [-PI, PI].
0176   Standard_EXPORT Standard_Real Angle (const gp_Dir2d& theOther) const;
0177 
0178   //! Computes the cross product between two directions.
0179   Standard_NODISCARD Standard_Real Crossed (const gp_Dir2d& theRight) const { return coord.Crossed (theRight.coord); }
0180 
0181   Standard_NODISCARD Standard_Real operator ^ (const gp_Dir2d& theRight) const { return Crossed (theRight); }
0182 
0183   //! Computes the scalar product
0184   Standard_Real Dot (const gp_Dir2d& theOther) const { return coord.Dot (theOther.coord); }
0185 
0186   Standard_Real operator * (const gp_Dir2d& theOther) const { return Dot (theOther); }
0187 
0188   void Reverse() { coord.Reverse(); }
0189 
0190   //! Reverses the orientation of a direction
0191   Standard_NODISCARD gp_Dir2d Reversed() const
0192   {
0193     gp_Dir2d aV = *this;
0194     aV.coord.Reverse();
0195     return aV;
0196   }
0197 
0198   Standard_NODISCARD gp_Dir2d operator -() const { return Reversed(); }
0199 
0200   Standard_EXPORT void Mirror (const gp_Dir2d& theV);
0201 
0202   //! Performs the symmetrical transformation of a direction
0203   //! with respect to the direction theV which is the center of
0204   //! the  symmetry.
0205   Standard_NODISCARD Standard_EXPORT gp_Dir2d Mirrored (const gp_Dir2d& theV) const;
0206 
0207   Standard_EXPORT void Mirror (const gp_Ax2d& theA);
0208 
0209   //! Performs the symmetrical transformation of a direction
0210   //! with respect to an axis placement which is the axis
0211   //! of the symmetry.
0212   Standard_NODISCARD Standard_EXPORT gp_Dir2d Mirrored (const gp_Ax2d& theA) const;
0213 
0214   void Rotate (const Standard_Real Ang);
0215 
0216   //! Rotates a direction.  theAng is the angular value of
0217   //! the rotation in radians.
0218   Standard_NODISCARD gp_Dir2d Rotated (const Standard_Real theAng) const
0219   {
0220     gp_Dir2d aV = *this;
0221     aV.Rotate (theAng);
0222     return aV;
0223   }
0224 
0225   Standard_EXPORT void Transform (const gp_Trsf2d& theT);
0226 
0227   //! Transforms a direction with the "Trsf" theT.
0228   //! Warnings :
0229   //! If the scale factor of the "Trsf" theT is negative then the
0230   //! direction <me> is reversed.
0231   Standard_NODISCARD gp_Dir2d Transformed (const gp_Trsf2d& theT) const
0232   {
0233     gp_Dir2d aV = *this;
0234     aV.Transform (theT);
0235     return aV;
0236   }
0237 
0238   //! Dumps the content of me into the stream
0239   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0240 
0241 private:
0242 
0243   gp_XY coord;
0244 
0245 };
0246 
0247 #include <gp_Ax2d.hxx>
0248 #include <gp_Trsf2d.hxx>
0249 
0250 // =======================================================================
0251 // function : gp_Dir2d
0252 // purpose  :
0253 // =======================================================================
0254 inline gp_Dir2d::gp_Dir2d (const gp_Vec2d& theV)
0255 {
0256   const gp_XY& aXY = theV.XY();
0257   Standard_Real aX = aXY.X();
0258   Standard_Real anY = aXY.Y();
0259   Standard_Real aD = sqrt (aX * aX + anY * anY);
0260   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir2d() - input vector has zero norm");
0261   coord.SetX (aX / aD);
0262   coord.SetY (anY / aD);
0263 }
0264 
0265 // =======================================================================
0266 // function : gp_Dir2d
0267 // purpose  :
0268 // =======================================================================
0269 inline gp_Dir2d::gp_Dir2d (const gp_XY& theXY)
0270 {
0271   Standard_Real aX = theXY.X();
0272   Standard_Real anY = theXY.Y();
0273   Standard_Real aD = sqrt (aX * aX + anY * anY);
0274   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir2d() - input vector has zero norm");
0275   coord.SetX (aX / aD);
0276   coord.SetY (anY / aD);
0277 }
0278 
0279 // =======================================================================
0280 // function : gp_Dir2d
0281 // purpose  :
0282 // =======================================================================
0283 inline gp_Dir2d::gp_Dir2d (const Standard_Real theXv,
0284                            const Standard_Real theYv)
0285 {
0286   Standard_Real aD = sqrt (theXv * theXv + theYv * theYv);
0287   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir2d() - input vector has zero norm");
0288   coord.SetX (theXv / aD);
0289   coord.SetY (theYv / aD);
0290 }
0291 
0292 // =======================================================================
0293 // function : SetCoord
0294 // purpose  :
0295 // =======================================================================
0296 inline void gp_Dir2d::SetCoord (const Standard_Integer theIndex,
0297                                 const Standard_Real theXi)
0298 {
0299   Standard_Real aX = coord.X();
0300   Standard_Real anY = coord.Y();
0301   Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > 2, "gp_Dir2d::SetCoord() - index is out of range [1, 2]");
0302   if (theIndex == 1)
0303   {
0304     aX = theXi;
0305   }
0306   else
0307   {
0308     anY = theXi;
0309   }
0310   Standard_Real aD = sqrt (aX * aX + anY * anY);
0311   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir2d::SetCoord() - result vector has zero norm");
0312   coord.SetX (aX / aD);
0313   coord.SetY (anY / aD);
0314 }
0315 
0316 // =======================================================================
0317 // function : SetCoord
0318 // purpose  :
0319 // =======================================================================
0320 inline void gp_Dir2d::SetCoord (const Standard_Real theXv,
0321                                 const Standard_Real theYv)
0322 {
0323   Standard_Real aD = sqrt (theXv * theXv + theYv * theYv);
0324   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir2d::SetCoord() - result vector has zero norm");
0325   coord.SetX (theXv / aD);
0326   coord.SetY (theYv / aD);
0327 }
0328 
0329 // =======================================================================
0330 // function : SetX
0331 // purpose  :
0332 // =======================================================================
0333 inline void gp_Dir2d::SetX (const Standard_Real theX)
0334 {
0335   Standard_Real anY = coord.Y();
0336   Standard_Real aD = sqrt (theX * theX + anY * anY);
0337   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir2d::SetX() - result vector has zero norm");
0338   coord.SetX (theX / aD);
0339   coord.SetY (anY / aD);
0340 }
0341 
0342 // =======================================================================
0343 // function : SetY
0344 // purpose  :
0345 // =======================================================================
0346 inline void gp_Dir2d::SetY (const Standard_Real theY)
0347 {
0348   Standard_Real aX = coord.X();
0349   Standard_Real aD = sqrt (aX * aX + theY * theY);
0350   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir2d::SetY() - result vector has zero norm");
0351   coord.SetX (aX / aD);
0352   coord.SetY (theY / aD);
0353 }
0354 
0355 // =======================================================================
0356 // function : SetXY
0357 // purpose  :
0358 // =======================================================================
0359 inline void gp_Dir2d::SetXY (const gp_XY& theXY)
0360 {
0361   Standard_Real aX = theXY.X();
0362   Standard_Real anY = theXY.Y();
0363   Standard_Real aD = sqrt (aX * aX + anY * anY);
0364   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir2d::SetZ() - result vector has zero norm");
0365   coord.SetX (aX / aD);
0366   coord.SetY (anY / aD);
0367 }
0368 
0369 // =======================================================================
0370 // function : IsEqual
0371 // purpose  :
0372 // =======================================================================
0373 inline Standard_Boolean gp_Dir2d::IsEqual (const gp_Dir2d& theOther,
0374                                            const Standard_Real theAngularTolerance) const
0375 {
0376   Standard_Real anAng = Angle (theOther);
0377   if (anAng < 0)
0378   {
0379     anAng = -anAng;
0380   }
0381   return anAng <= theAngularTolerance;
0382 }
0383 
0384 // =======================================================================
0385 // function : IsNormal
0386 // purpose  :
0387 // =======================================================================
0388 inline Standard_Boolean gp_Dir2d::IsNormal (const gp_Dir2d& theOther,
0389                                             const Standard_Real theAngularTolerance) const
0390 {
0391   Standard_Real anAng = Angle (theOther);
0392   if (anAng < 0)
0393   {
0394     anAng = -anAng;
0395   }
0396   anAng = M_PI / 2.0 - anAng;
0397   if (anAng < 0)
0398   {
0399     anAng = -anAng;
0400   }
0401   return anAng <= theAngularTolerance;
0402 }
0403 
0404 // =======================================================================
0405 // function : IsOpposite
0406 // purpose  :
0407 // =======================================================================
0408 inline Standard_Boolean gp_Dir2d::IsOpposite (const gp_Dir2d& theOther,
0409                                               const Standard_Real theAngularTolerance) const
0410 { 
0411   Standard_Real anAng = Angle (theOther);
0412   if (anAng < 0)
0413   {
0414     anAng = -anAng;
0415   }
0416   return M_PI - anAng <= theAngularTolerance;
0417 }
0418 
0419 // =======================================================================
0420 // function : IsParallel
0421 // purpose  :
0422 // =======================================================================
0423 inline Standard_Boolean gp_Dir2d::IsParallel (const gp_Dir2d& theOther,
0424                                               const Standard_Real theAngularTolerance) const
0425 {
0426   Standard_Real anAng = Angle (theOther);
0427   if (anAng < 0)
0428   {
0429     anAng = -anAng;
0430   }
0431   return anAng <= theAngularTolerance || M_PI - anAng <= theAngularTolerance;
0432 }
0433 
0434 // =======================================================================
0435 // function : Rotate
0436 // purpose  :
0437 // =======================================================================
0438 inline void gp_Dir2d::Rotate (const Standard_Real theAng)
0439 {
0440   gp_Trsf2d aT;
0441   aT.SetRotation (gp_Pnt2d (0.0, 0.0), theAng);
0442   coord.Multiply (aT.HVectorialPart());
0443 }
0444 
0445 #endif // _gp_Dir2d_HeaderFile