Back to home page

EIC code displayed by LXR

 
 

    


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

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_Elips2d_HeaderFile
0016 #define _gp_Elips2d_HeaderFile
0017 
0018 #include <gp.hxx>
0019 #include <gp_Ax22d.hxx>
0020 #include <gp_Ax2d.hxx>
0021 #include <gp_Pnt2d.hxx>
0022 #include <Standard_ConstructionError.hxx>
0023 
0024 //! Describes an ellipse in the plane (2D space).
0025 //! An ellipse is defined by its major and minor radii and
0026 //! positioned in the plane with a coordinate system (a
0027 //! gp_Ax22d object) as follows:
0028 //! -   the origin of the coordinate system is the center of the ellipse,
0029 //! -   its "X Direction" defines the major axis of the ellipse, and
0030 //! -   its "Y Direction" defines the minor axis of the ellipse.
0031 //! This coordinate system is the "local coordinate system"
0032 //! of the ellipse. Its orientation (direct or indirect) gives an
0033 //! implicit orientation to the ellipse. In this coordinate
0034 //! system, the equation of the ellipse is:
0035 //! @code
0036 //! X*X / (MajorRadius**2) + Y*Y / (MinorRadius**2) = 1.0
0037 //! @endcode
0038 //! See Also
0039 //! gce_MakeElips2d which provides functions for more
0040 //! complex ellipse constructions
0041 //! Geom2d_Ellipse which provides additional functions for
0042 //! constructing ellipses and works, in particular, with the
0043 //! parametric equations of ellipses
0044 class gp_Elips2d 
0045 {
0046 public:
0047 
0048   DEFINE_STANDARD_ALLOC
0049 
0050   //! Creates an indefinite ellipse.
0051   gp_Elips2d()
0052   : majorRadius (RealLast()),
0053     minorRadius (RealSmall())
0054   {}
0055 
0056   //! Creates an ellipse with the major axis, the major and the
0057   //! minor radius. The location of the theMajorAxis is the center
0058   //! of the  ellipse.
0059   //! The sense of parametrization is given by theIsSense.
0060   //! Warnings :
0061   //! It is possible to create an ellipse with
0062   //! theMajorRadius = theMinorRadius.
0063   //! Raises ConstructionError if theMajorRadius < theMinorRadius or theMinorRadius < 0.0
0064   gp_Elips2d (const gp_Ax2d& theMajorAxis, const Standard_Real theMajorRadius,
0065               const Standard_Real theMinorRadius, const Standard_Boolean theIsSense = Standard_True)
0066   : majorRadius (theMajorRadius),
0067     minorRadius (theMinorRadius)
0068   {
0069     pos = gp_Ax22d (theMajorAxis, theIsSense);
0070     Standard_ConstructionError_Raise_if (theMinorRadius < 0.0 || theMajorRadius < theMinorRadius,
0071       "gp_Elips2d() - invalid construction parameters");
0072   }
0073 
0074   //! Creates an ellipse with radii MajorRadius and
0075   //! MinorRadius, positioned in the plane by coordinate system theA where:
0076   //! -   the origin of theA is the center of the ellipse,
0077   //! -   the "X Direction" of theA defines the major axis of
0078   //! the ellipse, that is, the major radius MajorRadius
0079   //! is measured along this axis, and
0080   //! -   the "Y Direction" of theA defines the minor axis of
0081   //! the ellipse, that is, the minor radius theMinorRadius
0082   //! is measured along this axis, and
0083   //! -   the orientation (direct or indirect sense) of theA
0084   //! gives the orientation of the ellipse.
0085   //! Warnings :
0086   //! It is possible to create an ellipse with
0087   //! theMajorRadius = theMinorRadius.
0088   //! Raises ConstructionError if theMajorRadius < theMinorRadius or theMinorRadius < 0.0
0089   gp_Elips2d (const gp_Ax22d& theA, const Standard_Real theMajorRadius, const Standard_Real theMinorRadius)
0090   : pos (theA),
0091     majorRadius (theMajorRadius),
0092     minorRadius (theMinorRadius)
0093   {
0094     Standard_ConstructionError_Raise_if (theMinorRadius < 0.0 || theMajorRadius < theMinorRadius,
0095       "gp_Elips2d() - invalid construction parameters");
0096   }
0097 
0098   //! Modifies this ellipse, by redefining its local coordinate system so that
0099   //! -   its origin becomes theP.
0100   void SetLocation (const gp_Pnt2d& theP) { pos.SetLocation (theP); }
0101 
0102   //! Changes the value of the major radius.
0103   //! Raises ConstructionError if theMajorRadius < MinorRadius.
0104   void SetMajorRadius (const Standard_Real theMajorRadius)
0105   {
0106     Standard_ConstructionError_Raise_if (theMajorRadius < minorRadius,
0107       "gp_Elips2d::SetMajorRadius() - major radius should be greater or equal to minor radius");
0108     majorRadius = theMajorRadius;
0109   }
0110   
0111   //! Changes the value of the minor radius.
0112   //! Raises ConstructionError if MajorRadius < theMinorRadius or MinorRadius < 0.0
0113   void SetMinorRadius (const Standard_Real theMinorRadius)
0114   {
0115     Standard_ConstructionError_Raise_if (theMinorRadius < 0.0 || majorRadius < theMinorRadius,
0116       "gp_Elips2d::SetMinorRadius() - minor radius should be a positive number lesser or equal to major radius");
0117     minorRadius = theMinorRadius;
0118   }
0119 
0120   //! Modifies this ellipse, by redefining its local coordinate system so that
0121   //! it becomes theA.
0122   void SetAxis (const gp_Ax22d& theA) { pos.SetAxis (theA); }
0123 
0124   //! Modifies this ellipse, by redefining its local coordinate system so that
0125   //! its origin and its "X Direction"  become those
0126   //! of the axis theA. The "Y  Direction"  is then
0127   //! recomputed. The orientation of the local coordinate
0128   //! system is not modified.
0129   void SetXAxis (const gp_Ax2d& theA) { pos.SetXAxis (theA); }
0130 
0131   //! Modifies this ellipse, by redefining its local coordinate system so that
0132   //! its origin and its "Y Direction"  become those
0133   //! of the axis theA. The "X  Direction"  is then
0134   //! recomputed. The orientation of the local coordinate
0135   //! system is not modified.
0136   void SetYAxis (const gp_Ax2d& theA) { pos.SetYAxis (theA); }
0137 
0138   //! Computes the area of the ellipse.
0139   Standard_Real Area() const { return M_PI * majorRadius * minorRadius; }
0140 
0141   //! Returns the coefficients of the implicit equation of the ellipse.
0142   //! theA * (X**2) + theB * (Y**2) + 2*theC*(X*Y) + 2*theD*X + 2*theE*Y + theF = 0.
0143   Standard_EXPORT void Coefficients (Standard_Real& theA, Standard_Real& theB, Standard_Real& theC,
0144                                      Standard_Real& theD, Standard_Real& theE, Standard_Real& theF) const;
0145 
0146   //! This directrix is the line normal to the XAxis of the ellipse
0147   //! in the local plane (Z = 0) at a distance d = MajorRadius / e
0148   //! from the center of the ellipse, where e is the eccentricity of
0149   //! the ellipse.
0150   //! This line is parallel to the "YAxis". The intersection point
0151   //! between directrix1 and the "XAxis" is the location point of the
0152   //! directrix1. This point is on the positive side of the "XAxis".
0153   //!
0154   //! Raised if Eccentricity = 0.0. (The ellipse degenerates into a
0155   //! circle)
0156   gp_Ax2d Directrix1() const;
0157 
0158   //! This line is obtained by the symmetrical transformation
0159   //! of "Directrix1" with respect to the minor axis of the ellipse.
0160   //!
0161   //! Raised if Eccentricity = 0.0. (The ellipse degenerates into a
0162   //! circle).
0163   gp_Ax2d Directrix2() const;
0164 
0165   //! Returns the eccentricity of the ellipse  between 0.0 and 1.0
0166   //! If f is the distance between the center of the ellipse and
0167   //! the Focus1 then the eccentricity e = f / MajorRadius.
0168   //! Returns 0 if MajorRadius = 0.
0169   Standard_Real Eccentricity() const;
0170 
0171   //! Returns the distance between the center of the ellipse
0172   //! and focus1 or focus2.
0173   Standard_Real Focal() const
0174   {
0175     return 2.0 * sqrt (majorRadius * majorRadius - minorRadius * minorRadius);
0176   }
0177 
0178   //! Returns the first focus of the ellipse. This focus is on the
0179   //! positive side of the major axis of the ellipse.
0180   gp_Pnt2d Focus1() const;
0181 
0182   //! Returns the second focus of the ellipse. This focus is on the
0183   //! negative side of the major axis of the ellipse.
0184   gp_Pnt2d Focus2() const;
0185 
0186   //! Returns the center of the ellipse.
0187   const gp_Pnt2d& Location() const { return pos.Location(); }
0188 
0189   //! Returns the major radius of the Ellipse.
0190   Standard_Real MajorRadius() const { return majorRadius; }
0191 
0192   //! Returns the minor radius of the Ellipse.
0193   Standard_Real MinorRadius() const { return minorRadius; }
0194 
0195   //! Returns p = (1 - e * e) * MajorRadius where e is the eccentricity
0196   //! of the ellipse.
0197   //! Returns 0 if MajorRadius = 0
0198   Standard_Real Parameter() const;
0199 
0200   //! Returns the major axis of the ellipse.
0201   const gp_Ax22d& Axis() const { return pos; }
0202 
0203   //! Returns the major axis of the ellipse.
0204   gp_Ax2d XAxis() const { return pos.XAxis(); }
0205 
0206   //! Returns the minor axis of the ellipse.
0207   //! Reverses the direction of the circle.
0208   gp_Ax2d YAxis() const { return pos.YAxis(); }
0209 
0210   void Reverse()
0211   {
0212     gp_Dir2d aTemp = pos.YDirection();
0213     aTemp.Reverse();
0214     pos.SetAxis (gp_Ax22d (pos.Location(), pos.XDirection(), aTemp));
0215   }
0216 
0217   Standard_NODISCARD gp_Elips2d Reversed() const;
0218 
0219   //! Returns true if the local coordinate system is direct
0220   //! and false in the other case.
0221   Standard_Boolean IsDirect() const { return (pos.XDirection().Crossed (pos.YDirection())) >= 0.0; }
0222 
0223   Standard_EXPORT void Mirror (const gp_Pnt2d& theP);
0224 
0225   //! Performs the symmetrical transformation of a ellipse with respect
0226   //! to the point theP which is the center of the symmetry
0227   Standard_NODISCARD Standard_EXPORT gp_Elips2d Mirrored (const gp_Pnt2d& theP) const;
0228 
0229   Standard_EXPORT void Mirror (const gp_Ax2d& theA);
0230 
0231   //! Performs the symmetrical transformation of a ellipse with respect
0232   //! to an axis placement which is the axis of the symmetry.
0233   Standard_NODISCARD Standard_EXPORT gp_Elips2d Mirrored (const gp_Ax2d& theA) const;
0234 
0235   void Rotate (const gp_Pnt2d& theP, const Standard_Real theAng) { pos.Rotate (theP, theAng); }
0236 
0237   Standard_NODISCARD gp_Elips2d Rotated (const gp_Pnt2d& theP, const Standard_Real theAng) const
0238   {
0239     gp_Elips2d anE = *this;
0240     anE.pos.Rotate (theP, theAng);
0241     return anE;
0242   }
0243 
0244   void Scale (const gp_Pnt2d& theP, const Standard_Real theS);
0245 
0246   //! Scales a ellipse. theS is the scaling value.
0247   Standard_NODISCARD gp_Elips2d Scaled (const gp_Pnt2d& theP, const Standard_Real theS) const;
0248 
0249   void Transform (const gp_Trsf2d& theT);
0250 
0251   //! Transforms an ellipse with the transformation theT from class Trsf2d.
0252   Standard_NODISCARD gp_Elips2d Transformed (const gp_Trsf2d& theT) const;
0253 
0254   void Translate (const gp_Vec2d& theV) { pos.Translate (theV); }
0255 
0256   //! Translates a ellipse in the direction of the vector theV.
0257   //! The magnitude of the translation is the vector's magnitude.
0258   Standard_NODISCARD gp_Elips2d Translated (const gp_Vec2d& theV) const
0259   {
0260     gp_Elips2d anE = *this;
0261     anE.pos.Translate (theV);
0262     return anE;
0263   }
0264 
0265   void Translate (const gp_Pnt2d& theP1, const gp_Pnt2d& theP2) { pos.Translate (theP1, theP2); }
0266 
0267   //! Translates a ellipse from the point theP1 to the point theP2.
0268   Standard_NODISCARD gp_Elips2d Translated (const gp_Pnt2d& theP1, const gp_Pnt2d& theP2) const
0269   {
0270     gp_Elips2d anE = *this;
0271     anE.pos.Translate (theP1, theP2);
0272     return anE;
0273   }
0274 
0275 private:
0276 
0277   gp_Ax22d pos;
0278   Standard_Real majorRadius;
0279   Standard_Real minorRadius;
0280 
0281 };
0282 
0283 
0284 // =======================================================================
0285 // function : Directrix1
0286 // purpose  :
0287 // =======================================================================
0288 inline gp_Ax2d gp_Elips2d::Directrix1() const
0289 {
0290   Standard_Real anE = Eccentricity();
0291   Standard_ConstructionError_Raise_if (anE <= gp::Resolution(), "gp_Elips2d::Directrix1() - zero eccentricity");
0292   gp_XY anOrig = pos.XDirection().XY();
0293   anOrig.Multiply (majorRadius / anE);
0294   anOrig.Add (pos.Location().XY());
0295   return gp_Ax2d (gp_Pnt2d (anOrig), gp_Dir2d (pos.YDirection()));
0296 }
0297 
0298 // =======================================================================
0299 // function : Directrix2
0300 // purpose  :
0301 // =======================================================================
0302 inline gp_Ax2d gp_Elips2d::Directrix2() const
0303 {
0304   Standard_Real anE = Eccentricity();
0305   Standard_ConstructionError_Raise_if (anE <= gp::Resolution(), "gp_Elips2d::Directrix2() - zero eccentricity");
0306   gp_XY anOrig = pos.XDirection().XY();
0307   anOrig.Multiply (-majorRadius / anE);
0308   anOrig.Add (pos.Location().XY());
0309   return gp_Ax2d (gp_Pnt2d (anOrig), gp_Dir2d (pos.YDirection()));
0310 }
0311 
0312 // =======================================================================
0313 // function : Eccentricity
0314 // purpose  :
0315 // =======================================================================
0316 inline Standard_Real gp_Elips2d::Eccentricity() const
0317 {
0318   if (majorRadius == 0.0)
0319   {
0320     return 0.0;
0321   }
0322   else
0323   {
0324     return sqrt (majorRadius * majorRadius - minorRadius * minorRadius) / majorRadius;
0325   }
0326 }
0327 
0328 // =======================================================================
0329 // function : Focus1
0330 // purpose  :
0331 // =======================================================================
0332 inline gp_Pnt2d gp_Elips2d::Focus1() const
0333 {
0334   Standard_Real aC = sqrt (majorRadius * majorRadius - minorRadius * minorRadius);
0335   const gp_Pnt2d& aPP = pos.Location();
0336   const gp_Dir2d& aDD = pos.XDirection();
0337   return gp_Pnt2d (aPP.X() + aC * aDD.X(),
0338                    aPP.Y() + aC * aDD.Y());
0339 }
0340 
0341 // =======================================================================
0342 // function : Focus2
0343 // purpose  :
0344 // =======================================================================
0345 inline gp_Pnt2d gp_Elips2d::Focus2() const
0346 {
0347   Standard_Real aC = sqrt (majorRadius * majorRadius - minorRadius * minorRadius);
0348   const gp_Pnt2d& aPP = pos.Location();
0349   const gp_Dir2d& aDD = pos.XDirection();
0350   return gp_Pnt2d (aPP.X() - aC * aDD.X(),
0351                    aPP.Y() - aC * aDD.Y());
0352 }
0353 
0354 // =======================================================================
0355 // function : Scale
0356 // purpose  :
0357 // =======================================================================
0358 inline void gp_Elips2d::Scale (const gp_Pnt2d& theP, const Standard_Real theS)
0359 {
0360   majorRadius *= theS;
0361   if (majorRadius < 0)
0362   {
0363     majorRadius = -majorRadius;
0364   }
0365   minorRadius *= theS;
0366   if (minorRadius < 0)
0367   {
0368     minorRadius = -minorRadius;
0369   }
0370   pos.Scale (theP, theS);
0371 }
0372 
0373 // =======================================================================
0374 // function : Scaled
0375 // purpose  :
0376 // =======================================================================
0377 inline gp_Elips2d gp_Elips2d::Scaled (const gp_Pnt2d& theP, const Standard_Real theS) const
0378 {
0379   gp_Elips2d anE = *this;
0380   anE.majorRadius *= theS;
0381   if (anE.majorRadius < 0)
0382   {
0383     anE.majorRadius = -anE.majorRadius;
0384   }
0385   anE.minorRadius *= theS;
0386   if (anE.minorRadius < 0)
0387   {
0388     anE.minorRadius = -anE.minorRadius;
0389   }
0390   anE.pos.Scale (theP, theS);
0391   return anE; 
0392 }
0393 
0394 // =======================================================================
0395 // function : Parameter
0396 // purpose  :
0397 // =======================================================================
0398 inline Standard_Real gp_Elips2d::Parameter() const
0399 { 
0400   if (majorRadius == 0.0)
0401   {
0402     return 0.0;
0403   }
0404   else
0405   {
0406     return (minorRadius * minorRadius) / majorRadius;
0407   }
0408 }
0409 
0410 // =======================================================================
0411 // function : Reversed
0412 // purpose  :
0413 // =======================================================================
0414 inline gp_Elips2d gp_Elips2d::Reversed() const
0415 {
0416   gp_Elips2d anE = *this;
0417   gp_Dir2d aTemp = pos.YDirection ();
0418   aTemp.Reverse ();
0419   anE.pos.SetAxis (gp_Ax22d (pos.Location(),pos.XDirection(), aTemp));
0420   return anE;
0421 }
0422 
0423 // =======================================================================
0424 // function : Transform
0425 // purpose  :
0426 // =======================================================================
0427 inline void gp_Elips2d::Transform (const gp_Trsf2d& theT)
0428 {
0429   Standard_Real aTSca = theT.ScaleFactor();
0430   if (aTSca < 0.0)
0431   {
0432     aTSca = -aTSca;
0433   }
0434   majorRadius *=  aTSca;
0435   minorRadius *=  aTSca;
0436   pos.Transform (theT);
0437 }
0438 
0439 // =======================================================================
0440 // function : Transformed
0441 // purpose  :
0442 // =======================================================================
0443 inline gp_Elips2d gp_Elips2d::Transformed (const gp_Trsf2d& theT) const  
0444 {
0445   gp_Elips2d anE = *this;
0446   anE.majorRadius *= theT.ScaleFactor();
0447   if (anE.majorRadius < 0)
0448   {
0449     anE.majorRadius = -anE.majorRadius;
0450   }
0451   anE.minorRadius *= theT.ScaleFactor();
0452   if (anE.minorRadius < 0)
0453   {
0454     anE.minorRadius = -anE.minorRadius;
0455   }
0456   anE.pos.Transform (theT);
0457   return anE;
0458 }
0459 
0460 #endif // _gp_Elips2d_HeaderFile