Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:21:30

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_Pnt_HeaderFile
0016 #define _gp_Pnt_HeaderFile
0017 
0018 #include <Standard.hxx>
0019 #include <Standard_DefineAlloc.hxx>
0020 #include <Standard_Handle.hxx>
0021 #include <gp_XYZ.hxx>
0022 #include <Standard_Real.hxx>
0023 #include <Standard_Boolean.hxx>
0024 
0025 class gp_Ax1;
0026 class gp_Ax2;
0027 class gp_Trsf;
0028 class gp_Vec;
0029 
0030 //! Defines a 3D cartesian point.
0031 class gp_Pnt 
0032 {
0033 public:
0034 
0035   DEFINE_STANDARD_ALLOC
0036 
0037   //! Creates a point with zero coordinates.
0038   gp_Pnt() {}
0039 
0040   //! Creates a point from a XYZ object.
0041   gp_Pnt (const gp_XYZ& theCoord)
0042   : coord (theCoord)
0043   {}
0044 
0045   //! Creates a  point with its 3 cartesian's coordinates : theXp, theYp, theZp.
0046   gp_Pnt (const Standard_Real theXp, const Standard_Real theYp, const Standard_Real theZp)
0047   : coord (theXp, theYp, theZp)
0048   {}
0049 
0050   //! Changes the coordinate of range theIndex :
0051   //! theIndex = 1 => X is modified
0052   //! theIndex = 2 => Y is modified
0053   //! theIndex = 3 => Z is modified
0054   //! Raised if theIndex != {1, 2, 3}.
0055   void SetCoord (const Standard_Integer theIndex, const Standard_Real theXi)
0056   {
0057     coord.SetCoord (theIndex, theXi);
0058   }
0059 
0060   //! For this point, assigns  the values theXp, theYp and theZp to its three coordinates.
0061   void SetCoord (const Standard_Real theXp, const Standard_Real theYp, const Standard_Real theZp)
0062   {
0063     coord.SetCoord (theXp, theYp, theZp);
0064   }
0065 
0066   //! Assigns the given value to the X coordinate of this point.
0067   void SetX (const Standard_Real theX) { coord.SetX (theX); }
0068 
0069   //! Assigns the given value to the Y coordinate of this point.
0070   void SetY (const Standard_Real theY) { coord.SetY (theY); }
0071 
0072   //! Assigns the given value to the Z coordinate of this point.
0073   void SetZ (const Standard_Real theZ) { coord.SetZ (theZ); }
0074 
0075   //! Assigns the three coordinates of theCoord to this point.
0076   void SetXYZ (const gp_XYZ& theCoord) { coord = theCoord; }
0077 
0078   //! Returns the coordinate of corresponding to the value of theIndex :
0079   //! theIndex = 1 => X is returned
0080   //! theIndex = 2 => Y is returned
0081   //! theIndex = 3 => Z is returned
0082   //! Raises OutOfRange if theIndex != {1, 2, 3}.
0083   //! Raised if theIndex != {1, 2, 3}.
0084   Standard_Real Coord (const Standard_Integer theIndex) const { return coord.Coord (theIndex); }
0085 
0086   //! For this point gives its three coordinates theXp, theYp and theZp.
0087   void Coord (Standard_Real& theXp, Standard_Real& theYp, Standard_Real& theZp) const
0088   {
0089     coord.Coord (theXp, theYp, theZp);
0090   }
0091 
0092   //! For this point, returns its X coordinate.
0093   Standard_Real X() const { return coord.X(); }
0094 
0095   //! For this point, returns its Y coordinate.
0096   Standard_Real Y() const { return coord.Y(); }
0097 
0098   //! For this point, returns its Z coordinate.
0099   Standard_Real Z() const { return coord.Z(); }
0100 
0101   //! For this point, returns its three coordinates as a XYZ object.
0102   const gp_XYZ& XYZ() const { return coord; }
0103 
0104   //! For this point, returns its three coordinates as a XYZ object.
0105   const gp_XYZ& Coord() const { return coord; }
0106 
0107   //! Returns the coordinates of this point.
0108   //! Note: This syntax allows direct modification of the returned value.
0109   gp_XYZ& ChangeCoord() { return coord; }
0110 
0111   //! Assigns the result of the following expression to this point
0112   //! (theAlpha*this + theBeta*theP) / (theAlpha + theBeta)
0113   void BaryCenter (const Standard_Real theAlpha, const gp_Pnt& theP, const Standard_Real theBeta)
0114   {
0115     coord.SetLinearForm (theAlpha, coord, theBeta, theP.coord);
0116     coord.Divide (theAlpha + theBeta);
0117   }
0118 
0119   //! Comparison
0120   //! Returns True if the distance between the two points is
0121   //! lower or equal to theLinearTolerance.
0122   Standard_Boolean IsEqual (const gp_Pnt& theOther, const Standard_Real theLinearTolerance) const
0123   {
0124     return Distance (theOther) <= theLinearTolerance;
0125   }
0126 
0127   //! Computes the distance between two points.
0128   Standard_Real Distance (const gp_Pnt& theOther) const;
0129 
0130   //! Computes the square distance between two points.
0131   Standard_Real SquareDistance (const gp_Pnt& theOther) const;
0132 
0133   //! Performs the symmetrical transformation of a point
0134   //! with respect to the point theP which is the center of
0135   //! the  symmetry.
0136   Standard_EXPORT void Mirror (const gp_Pnt& theP);
0137 
0138   //! Performs the symmetrical transformation of a point
0139   //! with respect to an axis placement which is the axis
0140   //! of the symmetry.
0141   Standard_NODISCARD Standard_EXPORT gp_Pnt Mirrored (const gp_Pnt& theP) const;
0142 
0143   Standard_EXPORT void Mirror (const gp_Ax1& theA1);
0144 
0145   //! Performs the symmetrical transformation of a point
0146   //! with respect to a plane. The axis placement theA2 locates
0147   //! the plane of the symmetry : (Location, XDirection, YDirection).
0148   Standard_NODISCARD Standard_EXPORT gp_Pnt Mirrored (const gp_Ax1& theA1) const;
0149 
0150   Standard_EXPORT void Mirror (const gp_Ax2& theA2);
0151 
0152   //! Rotates a point. theA1 is the axis of the rotation.
0153   //! theAng is the angular value of the rotation in radians.
0154   Standard_NODISCARD Standard_EXPORT gp_Pnt Mirrored (const gp_Ax2& theA2) const;
0155 
0156   void Rotate (const gp_Ax1& theA1, const Standard_Real theAng);
0157 
0158   Standard_NODISCARD gp_Pnt Rotated (const gp_Ax1& theA1, const Standard_Real theAng) const
0159   {
0160     gp_Pnt aP = *this;
0161     aP.Rotate (theA1, theAng);
0162     return aP;
0163   }
0164 
0165   //! Scales a point. theS is the scaling value.
0166   void Scale (const gp_Pnt& theP, const Standard_Real theS);
0167 
0168   Standard_NODISCARD gp_Pnt Scaled (const gp_Pnt& theP, const Standard_Real theS) const
0169   {
0170     gp_Pnt aPres = *this;
0171     aPres.Scale (theP, theS);
0172     return aPres;
0173   }
0174 
0175   //! Transforms a point with the transformation T.
0176   Standard_EXPORT void Transform (const gp_Trsf& theT);
0177 
0178   Standard_NODISCARD gp_Pnt Transformed (const gp_Trsf& theT) const
0179   {
0180     gp_Pnt aP = *this;
0181     aP.Transform (theT);
0182     return aP;
0183   }
0184 
0185   //! Translates a point in the direction of the vector theV.
0186   //! The magnitude of the translation is the vector's magnitude.
0187   void Translate (const gp_Vec& theV);
0188 
0189   Standard_NODISCARD gp_Pnt Translated (const gp_Vec& theV) const;
0190 
0191   //! Translates a point from the point theP1 to the point theP2.
0192   void Translate (const gp_Pnt& theP1, const gp_Pnt& theP2)
0193   {
0194     coord.Add (theP2.coord);
0195     coord.Subtract (theP1.coord);
0196   }
0197 
0198   Standard_NODISCARD gp_Pnt Translated (const gp_Pnt& theP1, const gp_Pnt& theP2) const
0199   {
0200     gp_Pnt aP = *this;
0201     aP.Translate (theP1, theP2);
0202     return aP;
0203   }
0204 
0205   //! Dumps the content of me into the stream
0206   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0207 
0208   //! Inits the content of me from the stream
0209   Standard_EXPORT Standard_Boolean InitFromJson (const Standard_SStream& theSStream, Standard_Integer& theStreamPos);
0210 
0211 private:
0212 
0213   gp_XYZ coord;
0214 
0215 };
0216 
0217 namespace std
0218 {
0219   template <>
0220   struct hash<gp_Pnt>
0221   {
0222     size_t operator()(const gp_Pnt& thePnt) const noexcept
0223     {
0224       union
0225       {
0226         Standard_Real    R[3];
0227         Standard_Integer I[6];
0228       } U;
0229 
0230       thePnt.Coord(U.R[0], U.R[1], U.R[2]);
0231 
0232       return std::hash<double>{}(U.I[0] / 23 + U.I[1] / 19 + U.I[2] / 17 + U.I[3] / 13 + U.I[4] / 11 + U.I[5] / 7);
0233     }
0234   };
0235 
0236   template<>
0237   struct equal_to<gp_Pnt>
0238   {
0239     bool operator()(const gp_Pnt& thePnt1,
0240                     const gp_Pnt& thePnt2) const noexcept
0241     {
0242       if (Abs(thePnt1.X() - thePnt2.X()) > Epsilon(thePnt2.X())) return false;
0243       if (Abs(thePnt1.Y() - thePnt2.Y()) > Epsilon(thePnt2.Y())) return false;
0244       if (Abs(thePnt1.Z() - thePnt2.Z()) > Epsilon(thePnt2.Z())) return false;
0245       return true;
0246     }
0247   };
0248 }
0249 
0250 #include <gp_Trsf.hxx>
0251 #include <gp_Vec.hxx>
0252 #include <gp_XYZ.hxx>
0253 
0254 //=======================================================================
0255 //function : Distance
0256 // purpose :
0257 //=======================================================================
0258 inline Standard_Real gp_Pnt::Distance (const gp_Pnt& theOther) const
0259 {
0260   Standard_Real aD=0,aDD;
0261   const gp_XYZ& aXYZ = theOther.coord;
0262   aDD = coord.X(); aDD -= aXYZ.X(); aDD *= aDD; aD += aDD;
0263   aDD = coord.Y(); aDD -= aXYZ.Y(); aDD *= aDD; aD += aDD;
0264   aDD = coord.Z(); aDD -= aXYZ.Z(); aDD *= aDD; aD += aDD;
0265   return sqrt (aD);
0266 }
0267 
0268 //=======================================================================
0269 //function : SquareDistance
0270 // purpose :
0271 //=======================================================================
0272 inline Standard_Real gp_Pnt::SquareDistance (const gp_Pnt& theOther) const
0273 {
0274   Standard_Real aD=0, aDD;
0275   const gp_XYZ& XYZ = theOther.coord;
0276   aDD = coord.X(); aDD -= XYZ.X(); aDD *= aDD; aD += aDD;
0277   aDD = coord.Y(); aDD -= XYZ.Y(); aDD *= aDD; aD += aDD;
0278   aDD = coord.Z(); aDD -= XYZ.Z(); aDD *= aDD; aD += aDD;
0279   return aD;
0280 }
0281 
0282 //=======================================================================
0283 //function : Rotate
0284 // purpose :
0285 //=======================================================================
0286 inline void gp_Pnt::Rotate (const gp_Ax1& theA1, const Standard_Real theAng)
0287 {
0288   gp_Trsf aT;
0289   aT.SetRotation (theA1, theAng);
0290   aT.Transforms (coord);
0291 }
0292 
0293 //=======================================================================
0294 //function : Scale
0295 // purpose :
0296 //=======================================================================
0297 inline void gp_Pnt::Scale (const gp_Pnt& theP, const Standard_Real theS)
0298 {
0299   gp_XYZ aXYZ = theP.coord;
0300   aXYZ.Multiply (1.0 - theS);
0301   coord.Multiply (theS);
0302   coord.Add (aXYZ);
0303 }
0304 
0305 //=======================================================================
0306 //function : Translate
0307 // purpose :
0308 //=======================================================================
0309 inline void gp_Pnt::Translate(const gp_Vec& theV)
0310 {
0311   coord.Add (theV.XYZ());
0312 }
0313 
0314 //=======================================================================
0315 //function : Translated
0316 // purpose :
0317 //=======================================================================
0318 inline gp_Pnt gp_Pnt::Translated (const gp_Vec& theV) const
0319 {
0320   gp_Pnt aP = *this;
0321   aP.coord.Add (theV.XYZ());
0322   return aP;
0323 }
0324 
0325 #endif // _gp_Pnt_HeaderFile