Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-02 08:22:37

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_Ax2_HeaderFile
0016 #define _gp_Ax2_HeaderFile
0017 
0018 #include <gp_Ax1.hxx>
0019 #include <gp_Dir.hxx>
0020 #include <Precision.hxx>
0021 
0022 class gp_Trsf;
0023 class gp_Vec;
0024 
0025 //! Describes a right-handed coordinate system in 3D space.
0026 //! A coordinate system is defined by:
0027 //! -   its origin (also referred to as its "Location point"), and
0028 //! -   three orthogonal unit vectors, termed respectively the
0029 //! "X Direction", the "Y Direction" and the "Direction" (also
0030 //! referred to as the "main Direction").
0031 //! The "Direction" of the coordinate system is called its
0032 //! "main Direction" because whenever this unit vector is
0033 //! modified, the "X Direction" and the "Y Direction" are
0034 //! recomputed. However, when we modify either the "X
0035 //! Direction" or the "Y Direction", "Direction" is not modified.
0036 //! The "main Direction" is also the "Z Direction".
0037 //! Since an Ax2 coordinate system is right-handed, its
0038 //! "main Direction" is always equal to the cross product of
0039 //! its "X Direction" and "Y Direction". (To define a
0040 //! left-handed coordinate system, use gp_Ax3.)
0041 //! A coordinate system is used:
0042 //! -   to describe geometric entities, in particular to position
0043 //! them. The local coordinate system of a geometric
0044 //! entity serves the same purpose as the STEP function
0045 //! "axis placement two axes", or
0046 //! -   to define geometric transformations.
0047 //! Note: we refer to the "X Axis", "Y Axis" and "Z Axis",
0048 //! respectively, as to axes having:
0049 //! - the origin of the coordinate system as their origin, and
0050 //! -   the unit vectors "X Direction", "Y Direction" and "main
0051 //! Direction", respectively, as their unit vectors.
0052 //! The "Z Axis" is also the "main Axis".
0053 class gp_Ax2
0054 {
0055 public:
0056   DEFINE_STANDARD_ALLOC
0057 
0058   //! Creates an object corresponding to the reference
0059   //! coordinate system (OXYZ).
0060   gp_Ax2()
0061       : vydir(0., 1., 0.)
0062   // vxdir(1.,0.,0.) use default ctor of gp_Dir, as it creates the same dir(1,0,0)
0063   {
0064   }
0065 
0066   //! Creates an axis placement with an origin P such that:
0067   //! -   N is the Direction, and
0068   //! -   the "X Direction" is normal to N, in the plane
0069   //! defined by the vectors (N, Vx): "X
0070   //! Direction" = (N ^ Vx) ^ N,
0071   //! Exception: raises ConstructionError if N and Vx are parallel (same or opposite orientation).
0072   gp_Ax2(const gp_Pnt& P, const gp_Dir& N, const gp_Dir& Vx)
0073       : axis(P, N),
0074         vydir(N),
0075         vxdir(N)
0076   {
0077     vxdir.CrossCross(Vx, N);
0078     vydir.Cross(vxdir);
0079   }
0080 
0081   //! Creates -   a coordinate system with an origin P, where V
0082   //! gives the "main Direction" (here, "X Direction" and "Y
0083   //! Direction" are defined automatically).
0084   Standard_EXPORT gp_Ax2(const gp_Pnt& P, const gp_Dir& V);
0085 
0086   //! Assigns the origin and "main Direction" of the axis A1 to
0087   //! this coordinate system, then recomputes its "X Direction" and "Y Direction".
0088   //! Note: The new "X Direction" is computed as follows:
0089   //! new "X Direction" = V1 ^(previous "X Direction" ^ V)
0090   //! where V is the "Direction" of A1.
0091   //! Exceptions
0092   //! Standard_ConstructionError if A1 is parallel to the "X
0093   //! Direction" of this coordinate system.
0094   void SetAxis(const gp_Ax1& A1);
0095 
0096   //! Changes the "main Direction" of this coordinate system,
0097   //! then recomputes its "X Direction" and "Y Direction".
0098   //! Note: the new "X Direction" is computed as follows:
0099   //! new "X Direction" = V ^ (previous "X Direction" ^ V)
0100   //! Exceptions
0101   //! Standard_ConstructionError if V is parallel to the "X
0102   //! Direction" of this coordinate system.
0103   void SetDirection(const gp_Dir& V);
0104 
0105   //! Changes the "Location" point (origin) of <me>.
0106   void SetLocation(const gp_Pnt& theP) { axis.SetLocation(theP); }
0107 
0108   //! Changes the "Xdirection" of <me>. The main direction
0109   //! "Direction" is not modified, the "Ydirection" is modified.
0110   //! If <Vx> is not normal to the main direction then <XDirection>
0111   //! is computed as follows XDirection = Direction ^ (Vx ^ Direction).
0112   //! Exceptions
0113   //! Standard_ConstructionError if Vx or Vy is parallel to
0114   //! the "main Direction" of this coordinate system.
0115   void SetXDirection(const gp_Dir& theVx)
0116   {
0117     vxdir = axis.Direction().CrossCrossed(theVx, axis.Direction());
0118     vydir = axis.Direction().Crossed(vxdir);
0119   }
0120 
0121   //! Changes the "Ydirection" of <me>. The main direction is not
0122   //! modified but the "Xdirection" is changed.
0123   //! If <Vy> is not normal to the main direction then "YDirection"
0124   //! is computed as  follows
0125   //! YDirection = Direction ^ (<Vy> ^ Direction).
0126   //! Exceptions
0127   //! Standard_ConstructionError if Vx or Vy is parallel to
0128   //! the "main Direction" of this coordinate system.
0129   void SetYDirection(const gp_Dir& theVy)
0130   {
0131     vxdir = theVy.Crossed(axis.Direction());
0132     vydir = (axis.Direction()).Crossed(vxdir);
0133   }
0134 
0135   //! Computes the angular value, in radians, between the main direction of
0136   //! <me> and the main direction of <theOther>. Returns the angle
0137   //! between 0 and PI in radians.
0138   Standard_Real Angle(const gp_Ax2& theOther) const { return axis.Angle(theOther.axis); }
0139 
0140   //! Returns the main axis of <me>. It is the "Location" point
0141   //! and the main "Direction".
0142   const gp_Ax1& Axis() const { return axis; }
0143 
0144   //! Returns the main direction of <me>.
0145   const gp_Dir& Direction() const { return axis.Direction(); }
0146 
0147   //! Returns the "Location" point (origin) of <me>.
0148   const gp_Pnt& Location() const { return axis.Location(); }
0149 
0150   //! Returns the "XDirection" of <me>.
0151   const gp_Dir& XDirection() const { return vxdir; }
0152 
0153   //! Returns the "YDirection" of <me>.
0154   const gp_Dir& YDirection() const { return vydir; }
0155 
0156   Standard_Boolean IsCoplanar(const gp_Ax2&       Other,
0157                               const Standard_Real LinearTolerance,
0158                               const Standard_Real AngularTolerance) const;
0159 
0160   //! Returns True if
0161   //! . the distance between <me> and the "Location" point of A1
0162   //! is lower of equal to LinearTolerance and
0163   //! . the main direction of <me> and the direction of A1 are normal.
0164   //! Note: the tolerance criterion for angular equality is given by AngularTolerance.
0165   Standard_Boolean IsCoplanar(const gp_Ax1&       A1,
0166                               const Standard_Real LinearTolerance,
0167                               const Standard_Real AngularTolerance) const;
0168 
0169   //! Performs a symmetrical transformation of this coordinate
0170   //! system with respect to:
0171   //! -   the point P, and assigns the result to this coordinate system.
0172   //! Warning
0173   //! This transformation is always performed on the origin.
0174   //! In case of a reflection with respect to a point:
0175   //! - the main direction of the coordinate system is not changed, and
0176   //! - the "X Direction" and the "Y Direction" are simply reversed
0177   //! In case of a reflection with respect to an axis or a plane:
0178   //! -   the transformation is applied to the "X Direction"
0179   //! and the "Y Direction", then
0180   //! -   the "main Direction" is recomputed as the cross
0181   //! product "X Direction" ^ "Y   Direction".
0182   //! This maintains the right-handed property of the
0183   //! coordinate system.
0184   Standard_EXPORT void Mirror(const gp_Pnt& P);
0185 
0186   //! Performs a symmetrical transformation of this coordinate
0187   //! system with respect to:
0188   //! -   the point P, and creates a new one.
0189   //! Warning
0190   //! This transformation is always performed on the origin.
0191   //! In case of a reflection with respect to a point:
0192   //! - the main direction of the coordinate system is not changed, and
0193   //! - the "X Direction" and the "Y Direction" are simply reversed
0194   //! In case of a reflection with respect to an axis or a plane:
0195   //! -   the transformation is applied to the "X Direction"
0196   //! and the "Y Direction", then
0197   //! -   the "main Direction" is recomputed as the cross
0198   //! product "X Direction" ^ "Y   Direction".
0199   //! This maintains the right-handed property of the
0200   //! coordinate system.
0201   Standard_NODISCARD Standard_EXPORT gp_Ax2 Mirrored(const gp_Pnt& P) const;
0202 
0203   //! Performs a symmetrical transformation of this coordinate
0204   //! system with respect to:
0205   //! -   the axis A1, and assigns the result to this coordinate systeme.
0206   //! Warning
0207   //! This transformation is always performed on the origin.
0208   //! In case of a reflection with respect to a point:
0209   //! - the main direction of the coordinate system is not changed, and
0210   //! - the "X Direction" and the "Y Direction" are simply reversed
0211   //! In case of a reflection with respect to an axis or a plane:
0212   //! -   the transformation is applied to the "X Direction"
0213   //! and the "Y Direction", then
0214   //! -   the "main Direction" is recomputed as the cross
0215   //! product "X Direction" ^ "Y   Direction".
0216   //! This maintains the right-handed property of the
0217   //! coordinate system.
0218   Standard_EXPORT void Mirror(const gp_Ax1& A1);
0219 
0220   //! Performs a symmetrical transformation of this coordinate
0221   //! system with respect to:
0222   //! -   the axis A1, and  creates a new one.
0223   //! Warning
0224   //! This transformation is always performed on the origin.
0225   //! In case of a reflection with respect to a point:
0226   //! - the main direction of the coordinate system is not changed, and
0227   //! - the "X Direction" and the "Y Direction" are simply reversed
0228   //! In case of a reflection with respect to an axis or a plane:
0229   //! -   the transformation is applied to the "X Direction"
0230   //! and the "Y Direction", then
0231   //! -   the "main Direction" is recomputed as the cross
0232   //! product "X Direction" ^ "Y   Direction".
0233   //! This maintains the right-handed property of the
0234   //! coordinate system.
0235   Standard_NODISCARD Standard_EXPORT gp_Ax2 Mirrored(const gp_Ax1& A1) const;
0236 
0237   //! Performs a symmetrical transformation of this coordinate
0238   //! system with respect to:
0239   //! -   the plane defined by the origin, "X Direction" and "Y
0240   //! Direction" of coordinate system A2 and  assigns the result to this coordinate systeme.
0241   //! Warning
0242   //! This transformation is always performed on the origin.
0243   //! In case of a reflection with respect to a point:
0244   //! - the main direction of the coordinate system is not changed, and
0245   //! - the "X Direction" and the "Y Direction" are simply reversed
0246   //! In case of a reflection with respect to an axis or a plane:
0247   //! -   the transformation is applied to the "X Direction"
0248   //! and the "Y Direction", then
0249   //! -   the "main Direction" is recomputed as the cross
0250   //! product "X Direction" ^ "Y   Direction".
0251   //! This maintains the right-handed property of the
0252   //! coordinate system.
0253   Standard_EXPORT void Mirror(const gp_Ax2& A2);
0254 
0255   //! Performs a symmetrical transformation of this coordinate
0256   //! system with respect to:
0257   //! -   the plane defined by the origin, "X Direction" and "Y
0258   //! Direction" of coordinate system A2 and creates a new one.
0259   //! Warning
0260   //! This transformation is always performed on the origin.
0261   //! In case of a reflection with respect to a point:
0262   //! - the main direction of the coordinate system is not changed, and
0263   //! - the "X Direction" and the "Y Direction" are simply reversed
0264   //! In case of a reflection with respect to an axis or a plane:
0265   //! -   the transformation is applied to the "X Direction"
0266   //! and the "Y Direction", then
0267   //! -   the "main Direction" is recomputed as the cross
0268   //! product "X Direction" ^ "Y   Direction".
0269   //! This maintains the right-handed property of the
0270   //! coordinate system.
0271   Standard_NODISCARD Standard_EXPORT gp_Ax2 Mirrored(const gp_Ax2& A2) const;
0272 
0273   void Rotate(const gp_Ax1& theA1, const Standard_Real theAng)
0274   {
0275     gp_Pnt aTemp = axis.Location();
0276     aTemp.Rotate(theA1, theAng);
0277     axis.SetLocation(aTemp);
0278     vxdir.Rotate(theA1, theAng);
0279     vydir.Rotate(theA1, theAng);
0280     axis.SetDirection(vxdir.Crossed(vydir));
0281   }
0282 
0283   //! Rotates an axis placement. <theA1> is the axis of the rotation.
0284   //! theAng is the angular value of the rotation in radians.
0285   Standard_NODISCARD gp_Ax2 Rotated(const gp_Ax1& theA1, const Standard_Real theAng) const
0286   {
0287     gp_Ax2 aTemp = *this;
0288     aTemp.Rotate(theA1, theAng);
0289     return aTemp;
0290   }
0291 
0292   void Scale(const gp_Pnt& theP, const Standard_Real theS)
0293   {
0294     gp_Pnt aTemp = axis.Location();
0295     aTemp.Scale(theP, theS);
0296     axis.SetLocation(aTemp);
0297     if (theS < 0.0)
0298     {
0299       vxdir.Reverse();
0300       vydir.Reverse();
0301     }
0302   }
0303 
0304   //! Applies a scaling transformation on the axis placement.
0305   //! The "Location" point of the axisplacement is modified.
0306   //! Warnings :
0307   //! If the scale <S> is negative :
0308   //! . the main direction of the axis placement is not changed.
0309   //! . The "XDirection" and the "YDirection" are reversed.
0310   //! So the axis placement stay right handed.
0311   Standard_NODISCARD gp_Ax2 Scaled(const gp_Pnt& theP, const Standard_Real theS) const
0312   {
0313     gp_Ax2 aTemp = *this;
0314     aTemp.Scale(theP, theS);
0315     return aTemp;
0316   }
0317 
0318   void Transform(const gp_Trsf& theT)
0319   {
0320     gp_Pnt aTemp = axis.Location();
0321     aTemp.Transform(theT);
0322     axis.SetLocation(aTemp);
0323     vxdir.Transform(theT);
0324     vydir.Transform(theT);
0325     axis.SetDirection(vxdir.Crossed(vydir));
0326   }
0327 
0328   //! Transforms an axis placement with a Trsf.
0329   //! The "Location" point, the "XDirection" and the "YDirection" are transformed with theT.
0330   //! The resulting main "Direction" of <me> is the cross product between
0331   //! the "XDirection" and the "YDirection" after transformation.
0332   Standard_NODISCARD gp_Ax2 Transformed(const gp_Trsf& theT) const
0333   {
0334     gp_Ax2 aTemp = *this;
0335     aTemp.Transform(theT);
0336     return aTemp;
0337   }
0338 
0339   void Translate(const gp_Vec& theV) { axis.Translate(theV); }
0340 
0341   //! Translates an axis plaxement in the direction of the vector <theV>.
0342   //! The magnitude of the translation is the vector's magnitude.
0343   Standard_NODISCARD gp_Ax2 Translated(const gp_Vec& theV) const
0344   {
0345     gp_Ax2 aTemp = *this;
0346     aTemp.Translate(theV);
0347     return aTemp;
0348   }
0349 
0350   void Translate(const gp_Pnt& theP1, const gp_Pnt& theP2) { axis.Translate(theP1, theP2); }
0351 
0352   //! Translates an axis placement from the point <theP1> to the point <theP2>.
0353   Standard_NODISCARD gp_Ax2 Translated(const gp_Pnt& theP1, const gp_Pnt& theP2) const
0354   {
0355     gp_Ax2 aTemp = *this;
0356     aTemp.Translate(theP1, theP2);
0357     return aTemp;
0358   }
0359 
0360   //! Dumps the content of me into the stream
0361   Standard_EXPORT void DumpJson(Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0362 
0363   //! Inits the content of me from the stream
0364   Standard_EXPORT Standard_Boolean InitFromJson(const Standard_SStream& theSStream,
0365                                                 Standard_Integer&       theStreamPos);
0366 
0367 private:
0368   gp_Ax1 axis;
0369   gp_Dir vydir;
0370   gp_Dir vxdir;
0371 };
0372 
0373 // =======================================================================
0374 // function : SetAxis
0375 // purpose  :
0376 // =======================================================================
0377 inline void gp_Ax2::SetAxis(const gp_Ax1& theA1)
0378 {
0379   Standard_Real a = theA1.Direction() * vxdir;
0380   if (Abs(Abs(a) - 1.) <= Precision::Angular())
0381   {
0382     if (a > 0.)
0383     {
0384       vxdir = vydir;
0385       vydir = axis.Direction();
0386       axis  = theA1;
0387     }
0388     else
0389     {
0390       vxdir = axis.Direction();
0391       axis  = theA1;
0392     }
0393   }
0394   else
0395   {
0396     axis  = theA1;
0397     vxdir = axis.Direction().CrossCrossed(vxdir, axis.Direction());
0398     vydir = axis.Direction().Crossed(vxdir);
0399   }
0400 }
0401 
0402 // =======================================================================
0403 // function : SetDirection
0404 // purpose  :
0405 // =======================================================================
0406 inline void gp_Ax2::SetDirection(const gp_Dir& theV)
0407 {
0408   Standard_Real a = theV * vxdir;
0409   if (Abs(Abs(a) - 1.) <= Precision::Angular())
0410   {
0411     if (a > 0.)
0412     {
0413       vxdir = vydir;
0414       vydir = axis.Direction();
0415       axis.SetDirection(theV);
0416     }
0417     else
0418     {
0419       vxdir = axis.Direction();
0420       axis.SetDirection(theV);
0421     }
0422   }
0423   else
0424   {
0425     axis.SetDirection(theV);
0426     vxdir = theV.CrossCrossed(vxdir, theV);
0427     vydir = theV.Crossed(vxdir);
0428   }
0429 }
0430 
0431 // =======================================================================
0432 // function : IsCoplanar
0433 // purpose  :
0434 // =======================================================================
0435 inline Standard_Boolean gp_Ax2::IsCoplanar(const gp_Ax2&       theOther,
0436                                            const Standard_Real theLinearTolerance,
0437                                            const Standard_Real theAngularTolerance) const
0438 {
0439   const gp_Dir& DD = axis.Direction();
0440   const gp_Pnt& PP = axis.Location();
0441   const gp_Pnt& OP = theOther.axis.Location();
0442   Standard_Real D1 =
0443     (DD.X() * (OP.X() - PP.X()) + DD.Y() * (OP.Y() - PP.Y()) + DD.Z() * (OP.Z() - PP.Z()));
0444   if (D1 < 0)
0445   {
0446     D1 = -D1;
0447   }
0448   return D1 <= theLinearTolerance && axis.IsParallel(theOther.axis, theAngularTolerance);
0449 }
0450 
0451 // =======================================================================
0452 // function : IsCoplanar
0453 // purpose  :
0454 // =======================================================================
0455 inline Standard_Boolean gp_Ax2::IsCoplanar(const gp_Ax1&       theA,
0456                                            const Standard_Real theLinearTolerance,
0457                                            const Standard_Real theAngularTolerance) const
0458 {
0459   const gp_Dir& DD = axis.Direction();
0460   const gp_Pnt& PP = axis.Location();
0461   const gp_Pnt& AP = theA.Location();
0462   Standard_Real D1 =
0463     (DD.X() * (AP.X() - PP.X()) + DD.Y() * (AP.Y() - PP.Y()) + DD.Z() * (AP.Z() - PP.Z()));
0464   if (D1 < 0)
0465   {
0466     D1 = -D1;
0467   }
0468   return D1 <= theLinearTolerance && axis.IsNormal(theA, theAngularTolerance);
0469 }
0470 
0471 #endif // _gp_Ax2_HeaderFile