Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:03:43

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_Dir_HeaderFile
0016 #define _gp_Dir_HeaderFile
0017 
0018 #include <gp_XYZ.hxx>
0019 #include <Standard_ConstructionError.hxx>
0020 #include <Standard_DomainError.hxx>
0021 #include <Standard_OutOfRange.hxx>
0022 
0023 class gp_Vec;
0024 class gp_Ax1;
0025 class gp_Ax2;
0026 class gp_Trsf;
0027 
0028 //! Describes a unit vector in 3D space. This unit vector is also called "Direction".
0029 //! See Also
0030 //! gce_MakeDir which provides functions for more complex
0031 //! unit vector constructions
0032 //! Geom_Direction which provides additional functions for
0033 //! constructing unit vectors and works, in particular, with the
0034 //! parametric equations of unit vectors.
0035 class gp_Dir 
0036 {
0037 public:
0038 
0039   DEFINE_STANDARD_ALLOC
0040 
0041   //! Creates a direction corresponding to X axis.
0042   gp_Dir()
0043   : coord (1., 0., 0.)
0044   {}
0045 
0046   //! Normalizes the vector theV and creates a direction. Raises ConstructionError if theV.Magnitude() <= Resolution.
0047   gp_Dir (const gp_Vec& theV);
0048 
0049   //! Creates a direction from a triplet of coordinates. Raises ConstructionError if theCoord.Modulus() <= Resolution from gp.
0050   gp_Dir (const gp_XYZ& theCoord);
0051 
0052   //! Creates a direction with its 3 cartesian coordinates. Raises ConstructionError if Sqrt(theXv*theXv + theYv*theYv + theZv*theZv) <= Resolution
0053   //! Modification of the direction's coordinates
0054   //! If Sqrt (theXv*theXv + theYv*theYv + theZv*theZv) <= Resolution from gp where
0055   //! theXv, theYv ,theZv are the new coordinates it is not possible to
0056   //! construct the direction and the method raises the
0057   //! exception ConstructionError.
0058   gp_Dir (const Standard_Real theXv, const Standard_Real theYv, const Standard_Real theZv);
0059 
0060   //! For this unit vector,  assigns the value Xi to:
0061   //! -   the X coordinate if theIndex is 1, or
0062   //! -   the Y coordinate if theIndex is 2, or
0063   //! -   the Z coordinate if theIndex is 3,
0064   //! and then normalizes it.
0065   //! Warning
0066   //! Remember that all the coordinates of a unit vector are
0067   //! implicitly modified when any single one is changed directly.
0068   //! Exceptions
0069   //! Standard_OutOfRange if theIndex is not 1, 2, or 3.
0070   //! Standard_ConstructionError if either of the following
0071   //! is less than or equal to gp::Resolution():
0072   //! -   Sqrt(Xv*Xv + Yv*Yv + Zv*Zv), or
0073   //! -   the modulus of the number triple formed by the new
0074   //! value theXi and the two other coordinates of this vector
0075   //! that were not directly modified.
0076   void SetCoord (const Standard_Integer theIndex, const Standard_Real theXi);
0077 
0078   //! For this unit vector,  assigns the values theXv, theYv and theZv to its three coordinates.
0079   //! Remember that all the coordinates of a unit vector are
0080   //! implicitly modified when any single one is changed directly.
0081   void SetCoord (const Standard_Real theXv, const Standard_Real theYv, const Standard_Real theZv);
0082 
0083   //! Assigns the given value to the X coordinate of this   unit vector.
0084   void SetX (const Standard_Real theX);
0085 
0086   //! Assigns the given value to the Y coordinate of this   unit vector.
0087   void SetY (const Standard_Real theY);
0088 
0089   //! Assigns the given value to the Z  coordinate of this   unit vector.
0090   void SetZ (const Standard_Real theZ);
0091 
0092   //! Assigns the three coordinates of theCoord to this unit vector.
0093   void SetXYZ (const gp_XYZ& theCoord);
0094 
0095   //! Returns the coordinate of range theIndex :
0096   //! theIndex = 1 => X is returned
0097   //! Ithendex = 2 => Y is returned
0098   //! theIndex = 3 => Z is returned
0099   //! Exceptions
0100   //! Standard_OutOfRange if theIndex is not 1, 2, or 3.
0101   Standard_Real Coord (const Standard_Integer theIndex) const  { return coord.Coord (theIndex); }
0102 
0103   //! Returns for the  unit vector  its three coordinates theXv, theYv, and theZv.
0104   void Coord (Standard_Real& theXv, Standard_Real& theYv, Standard_Real& theZv) const  { coord.Coord (theXv, theYv, theZv); }
0105 
0106   //! Returns the X coordinate for a  unit vector.
0107   Standard_Real X() const { return coord.X(); }
0108 
0109   //! Returns the Y coordinate for a  unit vector.
0110   Standard_Real Y() const { return coord.Y(); }
0111 
0112   //! Returns the Z coordinate for a  unit vector.
0113   Standard_Real Z() const { return coord.Z(); }
0114 
0115   //! for this unit vector, returns  its three coordinates as a number triplea.
0116   const gp_XYZ& XYZ() const { return coord; }
0117 
0118   //! Returns True if the angle between the two directions is
0119   //! lower or equal to theAngularTolerance.
0120   Standard_Boolean IsEqual (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
0121   {
0122     return Angle (theOther) <= theAngularTolerance;
0123   }
0124 
0125   //! Returns True if  the angle between this unit vector and the unit vector theOther is equal to Pi/2 (normal).
0126   Standard_Boolean IsNormal (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
0127   {
0128     Standard_Real anAng = M_PI / 2.0 - Angle (theOther);
0129     if (anAng < 0)
0130     {
0131       anAng = -anAng;
0132     }
0133     return anAng <= theAngularTolerance;
0134   }
0135 
0136   //! Returns True if  the angle between this unit vector and the unit vector theOther is equal to  Pi (opposite).
0137   Standard_Boolean IsOpposite (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
0138   {
0139     return M_PI - Angle (theOther) <= theAngularTolerance;
0140   }
0141 
0142   //! Returns true if the angle between this unit vector and the
0143   //! unit vector theOther is equal to 0 or to Pi.
0144   //! Note: the tolerance criterion is given by theAngularTolerance.
0145   Standard_Boolean IsParallel (const gp_Dir& theOther, const Standard_Real theAngularTolerance) const
0146   {
0147     Standard_Real anAng = Angle (theOther);
0148     return anAng <= theAngularTolerance || M_PI - anAng <= theAngularTolerance;
0149   }
0150 
0151   //! Computes the angular value in radians between <me> and
0152   //! <theOther>. This value is always positive in 3D space.
0153   //! Returns the angle in the range [0, PI]
0154   Standard_EXPORT Standard_Real Angle (const gp_Dir& theOther) const;
0155 
0156   //! Computes the angular value between <me> and <theOther>.
0157   //! <theVRef> is the direction of reference normal to <me> and <theOther>
0158   //! and its orientation gives the positive sense of rotation.
0159   //! If the cross product <me> ^ <theOther> has the same orientation
0160   //! as <theVRef> the angular value is positive else negative.
0161   //! Returns the angular value in the range -PI and PI (in radians). Raises  DomainError if <me> and <theOther> are not parallel this exception is raised
0162   //! when <theVRef> is in the same plane as <me> and <theOther>
0163   //! The tolerance criterion is Resolution from package gp.
0164   Standard_EXPORT Standard_Real AngleWithRef (const gp_Dir& theOther, const gp_Dir& theVRef) const;
0165 
0166   //! Computes the cross product between two directions
0167   //! Raises the exception ConstructionError if the two directions
0168   //! are parallel because the computed vector cannot be normalized
0169   //! to create a direction.
0170   void Cross (const gp_Dir& theRight);
0171 
0172   void operator ^= (const gp_Dir& theRight) { Cross (theRight); }
0173 
0174   //! Computes the triple vector product.
0175   //! <me> ^ (V1 ^ V2)
0176   //! Raises the exception ConstructionError if V1 and V2 are parallel
0177   //! or <me> and (V1^V2) are parallel because the computed vector
0178   //! can't be normalized to create a direction.
0179   Standard_NODISCARD gp_Dir Crossed (const gp_Dir& theRight) const;
0180 
0181   Standard_NODISCARD gp_Dir operator ^ (const gp_Dir& theRight) const { return Crossed (theRight); }
0182 
0183   void CrossCross (const gp_Dir& theV1, const gp_Dir& theV2);
0184 
0185   //! Computes the double vector product this ^ (theV1 ^ theV2).
0186   //! -   CrossCrossed creates a new unit vector.
0187   //! Exceptions
0188   //! Standard_ConstructionError if:
0189   //! -   theV1 and theV2 are parallel, or
0190   //! -   this unit vector and (theV1 ^ theV2) are parallel.
0191   //! This is because, in these conditions, the computed vector
0192   //! is null and cannot be normalized.
0193   Standard_NODISCARD gp_Dir CrossCrossed (const gp_Dir& theV1, const gp_Dir& theV2) const;
0194 
0195   //! Computes the scalar product
0196   Standard_Real Dot (const gp_Dir& theOther) const { return coord.Dot (theOther.coord); }
0197 
0198   Standard_Real operator * (const gp_Dir& theOther) const { return Dot (theOther); }
0199 
0200   //! Computes the triple scalar product <me> * (theV1 ^ theV2).
0201   //! Warnings :
0202   //! The computed vector theV1' = theV1 ^ theV2 is not normalized
0203   //! to create a unitary vector. So this method never
0204   //! raises an exception even if theV1 and theV2 are parallel.
0205   Standard_Real DotCross (const gp_Dir& theV1, const gp_Dir& theV2) const
0206   {
0207     return coord.Dot (theV1.coord.Crossed (theV2.coord));
0208   }
0209 
0210   void Reverse() { coord.Reverse(); }
0211 
0212   //! Reverses the orientation of a direction
0213   //! geometric transformations
0214   //! Performs the symmetrical transformation of a direction
0215   //! with respect to the direction V which is the center of
0216   //! the  symmetry.]
0217   Standard_NODISCARD gp_Dir Reversed() const
0218   {
0219     gp_Dir aV = *this;
0220     aV.coord.Reverse();
0221     return aV;
0222   }
0223 
0224   Standard_NODISCARD gp_Dir operator -() const { return Reversed(); }
0225 
0226   Standard_EXPORT void Mirror (const gp_Dir& theV);
0227 
0228   //! Performs the symmetrical transformation of a direction
0229   //! with respect to the direction theV which is the center of
0230   //! the  symmetry.
0231   Standard_NODISCARD Standard_EXPORT gp_Dir Mirrored (const gp_Dir& theV) const;
0232 
0233   Standard_EXPORT void Mirror (const gp_Ax1& theA1);
0234 
0235   //! Performs the symmetrical transformation of a direction
0236   //! with respect to an axis placement which is the axis
0237   //! of the symmetry.
0238   Standard_NODISCARD Standard_EXPORT gp_Dir Mirrored (const gp_Ax1& theA1) const;
0239 
0240   Standard_EXPORT void Mirror (const gp_Ax2& theA2);
0241 
0242   //! Performs the symmetrical transformation of a direction
0243   //! with respect to a plane. The axis placement theA2 locates
0244   //! the plane of the symmetry : (Location, XDirection, YDirection).
0245   Standard_NODISCARD Standard_EXPORT gp_Dir Mirrored (const gp_Ax2& theA2) const;
0246 
0247   void Rotate(const gp_Ax1& theA1, const Standard_Real theAng);
0248 
0249   //! Rotates a direction. theA1 is the axis of the rotation.
0250   //! theAng is the angular value of the rotation in radians.
0251   Standard_NODISCARD gp_Dir Rotated (const gp_Ax1& theA1, const Standard_Real theAng) const
0252   {
0253     gp_Dir aV = *this;
0254     aV.Rotate (theA1, theAng);
0255     return aV;
0256   }
0257 
0258   Standard_EXPORT void Transform (const gp_Trsf& theT);
0259 
0260   //! Transforms a direction with a "Trsf" from gp.
0261   //! Warnings :
0262   //! If the scale factor of the "Trsf" theT is negative then the
0263   //! direction <me> is reversed.
0264   Standard_NODISCARD gp_Dir Transformed (const gp_Trsf& theT) const
0265   {
0266     gp_Dir aV = *this;
0267     aV.Transform (theT);
0268     return aV;
0269   }
0270 
0271   //! Dumps the content of me into the stream
0272   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0273 
0274   //! Inits the content of me from the stream
0275   Standard_EXPORT Standard_Boolean InitFromJson (const Standard_SStream& theSStream, Standard_Integer& theStreamPos);
0276 
0277 private:
0278 
0279   gp_XYZ coord;
0280 
0281 };
0282 
0283 #include <gp_Trsf.hxx>
0284 
0285 // =======================================================================
0286 // function : gp_Dir
0287 // purpose  :
0288 // =======================================================================
0289 inline gp_Dir::gp_Dir (const gp_Vec& theV)
0290 {
0291   const gp_XYZ& aXYZ = theV.XYZ();
0292   Standard_Real aX = aXYZ.X();
0293   Standard_Real aY = aXYZ.Y();
0294   Standard_Real aZ = aXYZ.Z();
0295   Standard_Real aD = sqrt (aX * aX + aY * aY + aZ * aZ);
0296   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir() - input vector has zero norm");
0297   coord.SetX (aX / aD);
0298   coord.SetY (aY / aD);
0299   coord.SetZ (aZ / aD);
0300 }
0301 
0302 // =======================================================================
0303 // function : gp_Dir
0304 // purpose  :
0305 // =======================================================================
0306 inline gp_Dir::gp_Dir (const gp_XYZ& theXYZ)
0307 {
0308   Standard_Real aX = theXYZ.X();
0309   Standard_Real aY = theXYZ.Y();
0310   Standard_Real aZ = theXYZ.Z();
0311   Standard_Real aD = sqrt (aX * aX + aY * aY + aZ * aZ);
0312   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir() - input vector has zero norm");
0313   coord.SetX (aX / aD);
0314   coord.SetY (aY / aD);
0315   coord.SetZ (aZ / aD);
0316 }
0317 
0318 // =======================================================================
0319 // function : gp_Dir
0320 // purpose  :
0321 // =======================================================================
0322 inline gp_Dir::gp_Dir (const Standard_Real theXv,
0323                        const Standard_Real theYv,
0324                        const Standard_Real theZv)
0325 {
0326   Standard_Real aD = sqrt (theXv * theXv + theYv * theYv + theZv * theZv);
0327   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir() - input vector has zero norm");
0328   coord.SetX (theXv / aD);
0329   coord.SetY (theYv / aD);
0330   coord.SetZ (theZv / aD);
0331 }
0332 
0333 // =======================================================================
0334 // function : SetCoord
0335 // purpose  :
0336 // =======================================================================
0337 inline void gp_Dir::SetCoord (const Standard_Integer theIndex,
0338                               const Standard_Real theXi)
0339 {
0340   Standard_Real aX = coord.X();
0341   Standard_Real aY = coord.Y();
0342   Standard_Real aZ = coord.Z();
0343   Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > 3, "gp_Dir::SetCoord() - index is out of range [1, 3]");
0344   if (theIndex == 1)
0345   {
0346     aX = theXi;
0347   }
0348   else if (theIndex == 2)
0349   {
0350     aY = theXi;
0351   }
0352   else
0353   {
0354     aZ = theXi;
0355   }
0356   Standard_Real aD = sqrt (aX * aX + aY * aY + aZ * aZ);
0357   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetCoord() - result vector has zero norm");
0358   coord.SetX (aX / aD);
0359   coord.SetY (aY / aD);
0360   coord.SetZ (aZ / aD);
0361 }
0362 
0363 // =======================================================================
0364 // function : SetCoord
0365 // purpose  :
0366 // =======================================================================
0367 inline void gp_Dir::SetCoord (const Standard_Real theXv,
0368                               const Standard_Real theYv,
0369                               const Standard_Real theZv) {
0370   Standard_Real aD = sqrt (theXv * theXv + theYv * theYv + theZv * theZv);
0371   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetCoord() - input vector has zero norm");
0372   coord.SetX (theXv / aD);
0373   coord.SetY (theYv / aD);
0374   coord.SetZ (theZv / aD);
0375 }
0376 
0377 // =======================================================================
0378 // function : SetX
0379 // purpose  :
0380 // =======================================================================
0381 inline void gp_Dir::SetX (const Standard_Real theX)
0382 {
0383   Standard_Real anY = coord.Y();
0384   Standard_Real aZ = coord.Z();
0385   Standard_Real aD = sqrt (theX * theX + anY * anY + aZ * aZ);
0386   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetX() - result vector has zero norm");
0387   coord.SetX (theX / aD);
0388   coord.SetY (anY / aD);
0389   coord.SetZ (aZ / aD);
0390 }
0391 
0392 // =======================================================================
0393 // function : SetY
0394 // purpose  :
0395 // =======================================================================
0396 inline void gp_Dir::SetY (const Standard_Real theY)
0397 {
0398   Standard_Real aZ = coord.Z();
0399   Standard_Real aX = coord.X();
0400   Standard_Real aD = sqrt (aX * aX + theY * theY + aZ * aZ);
0401   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetY() - result vector has zero norm");
0402   coord.SetX (aX / aD);
0403   coord.SetY (theY / aD);
0404   coord.SetZ (aZ / aD);
0405 }
0406 
0407 // =======================================================================
0408 // function : SetZ
0409 // purpose  :
0410 // =======================================================================
0411 inline void gp_Dir::SetZ (const Standard_Real theZ)
0412 {
0413   Standard_Real aX = coord.X();
0414   Standard_Real anY = coord.Y();
0415   Standard_Real aD = sqrt (aX * aX + anY * anY + theZ * theZ);
0416   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetZ() - result vector has zero norm");
0417   coord.SetX (aX / aD);
0418   coord.SetY (anY / aD);
0419   coord.SetZ (theZ / aD);
0420 }
0421 
0422 // =======================================================================
0423 // function : SetXYZ
0424 // purpose  :
0425 // =======================================================================
0426 inline void gp_Dir::SetXYZ (const gp_XYZ& theXYZ)
0427 {
0428   Standard_Real aX = theXYZ.X();
0429   Standard_Real anY = theXYZ.Y();
0430   Standard_Real aZ = theXYZ.Z();
0431   Standard_Real aD = sqrt(aX * aX + anY * anY + aZ * aZ);
0432   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::SetX() - input vector has zero norm");
0433   coord.SetX (aX / aD);
0434   coord.SetY (anY / aD);
0435   coord.SetZ (aZ / aD);
0436 }
0437 
0438 // =======================================================================
0439 // function : Cross
0440 // purpose  :
0441 // =======================================================================
0442 inline void gp_Dir::Cross(const gp_Dir& theRight)
0443 {
0444   coord.Cross (theRight.coord);
0445   Standard_Real aD = coord.Modulus();
0446   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::Cross() - result vector has zero norm");
0447   coord.Divide (aD);
0448 }
0449 
0450 // =======================================================================
0451 // function : Crossed
0452 // purpose  :
0453 // =======================================================================
0454 inline gp_Dir gp_Dir::Crossed (const gp_Dir& theRight) const
0455 {
0456   gp_Dir aV = *this;
0457   aV.coord.Cross (theRight.coord);
0458   Standard_Real aD = aV.coord.Modulus();
0459   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::Crossed() - result vector has zero norm");
0460   aV.coord.Divide (aD);
0461   return aV;
0462 }
0463 
0464 // =======================================================================
0465 // function : CrossCross
0466 // purpose  :
0467 // =======================================================================
0468 inline void gp_Dir::CrossCross (const gp_Dir& theV1, const gp_Dir& theV2)
0469 {
0470   coord.CrossCross (theV1.coord, theV2.coord);
0471   Standard_Real aD = coord.Modulus();
0472   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::CrossCross() - result vector has zero norm");
0473   coord.Divide (aD);
0474 }
0475 
0476 // =======================================================================
0477 // function : CrossCrossed
0478 // purpose  :
0479 // =======================================================================
0480 inline gp_Dir gp_Dir::CrossCrossed (const gp_Dir& theV1, const gp_Dir& theV2) const
0481 {
0482   gp_Dir aV = *this;
0483   (aV.coord).CrossCross (theV1.coord, theV2.coord);
0484   Standard_Real aD = aV.coord.Modulus();
0485   Standard_ConstructionError_Raise_if (aD <= gp::Resolution(), "gp_Dir::CrossCrossed() - result vector has zero norm");
0486   aV.coord.Divide (aD);
0487   return aV;
0488 }
0489 
0490 // =======================================================================
0491 // function : Rotate
0492 // purpose  :
0493 // =======================================================================
0494 inline void gp_Dir::Rotate(const gp_Ax1& theA1, const Standard_Real theAng)
0495 {
0496   gp_Trsf aT;
0497   aT.SetRotation (theA1, theAng);
0498   coord.Multiply (aT.HVectorialPart());
0499 }
0500 
0501 #endif // _gp_Dir_HeaderFile