1 // Copyright (C) 2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 // File: GeomAPI_Ellipse2d.cpp
22 // Created: 26 April 2017
23 // Author: Artem ZHIDKOV
25 #include <GeomAPI_Ellipse2d.h>
26 #include <GeomAPI_Dir2d.h>
27 #include <GeomAPI_Pnt2d.h>
29 #include <gp_Ax22d.hxx>
30 #include <gp_Elips2d.hxx>
31 #include <Precision.hxx>
33 #define MY_ELLIPSE implPtr<gp_Elips2d>()
35 static gp_Elips2d* newEllipse(const gp_Pnt2d& theCenter,
36 const gp_Dir2d& theXAxis,
37 const double theMajorRadius,
38 const double theMinorRadius)
40 if (theMajorRadius < theMinorRadius - Precision::Confusion()) {
41 return newEllipse(theCenter, gp_Dir2d(-theXAxis.Y(), theXAxis.X()),
42 theMinorRadius, theMajorRadius);
45 gp_Ax22d anAxis(theCenter, theXAxis);
46 return new gp_Elips2d(anAxis, theMajorRadius, theMinorRadius);
49 static gp_Elips2d* newEllipse(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
50 const std::shared_ptr<GeomAPI_Pnt2d>& theAxisPoint,
51 const std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
53 const gp_Pnt2d& aCenter = theCenter->impl<gp_Pnt2d>();
54 const gp_Pnt2d& anAxisPnt = theAxisPoint->impl<gp_Pnt2d>();
55 const gp_Pnt2d& aPassedPnt = thePassingPoint->impl<gp_Pnt2d>();
57 gp_Dir2d aXAxis(anAxisPnt.XY() - aCenter.XY());
58 double aMajorRadius = anAxisPnt.Distance(aCenter);
60 gp_XY aPassedDir = aPassedPnt.XY() - aCenter.XY();
62 double X = aPassedDir.Dot(aXAxis.XY()) / aMajorRadius;
63 if (Abs(X) > 1.0 - Precision::Confusion())
64 return 0; // ellipse cannot be created for such parameters
66 double Y = aPassedDir.CrossMagnitude(aXAxis.XY());
67 double aMinorRadius = Y / Sqrt(1. - X * X);
69 return newEllipse(aCenter, aXAxis, aMajorRadius, aMinorRadius);
73 GeomAPI_Ellipse2d::GeomAPI_Ellipse2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
74 const std::shared_ptr<GeomAPI_Dir2d>& theXAxis,
75 const double theMajorRadius,
76 const double theMinorRadius)
77 : GeomAPI_Interface(newEllipse(theCenter->impl<gp_Pnt2d>(), theXAxis->impl<gp_Dir2d>(),
78 theMajorRadius, theMinorRadius))
82 GeomAPI_Ellipse2d::GeomAPI_Ellipse2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
83 const std::shared_ptr<GeomAPI_Pnt2d>& theAxisPoint,
84 const std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
85 : GeomAPI_Interface(newEllipse(theCenter, theAxisPoint, thePassingPoint))
89 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::center() const
91 const gp_Pnt2d& aCenter = MY_ELLIPSE->Location();
92 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
95 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::firstFocus() const
97 const gp_Pnt2d& aFirst = MY_ELLIPSE->Focus1();
98 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aFirst.X(), aFirst.Y()));
101 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::secondFocus() const
103 const gp_Pnt2d& aSecond = MY_ELLIPSE->Focus2();
104 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aSecond.X(), aSecond.Y()));
107 double GeomAPI_Ellipse2d::minorRadius() const
109 return MY_ELLIPSE->MinorRadius();
112 double GeomAPI_Ellipse2d::majorRadius() const
114 return MY_ELLIPSE->MajorRadius();