1 // Copyright (C) 2017-2019 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 email : webmaster.salome@opencascade.com
20 // File: GeomAPI_Ellipse2d.cpp
21 // Created: 26 April 2017
22 // Author: Artem ZHIDKOV
24 #include <GeomAPI_Ellipse2d.h>
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomAPI_Pnt2d.h>
28 #include <gp_Ax22d.hxx>
29 #include <gp_Elips2d.hxx>
30 #include <Precision.hxx>
32 #define MY_ELLIPSE implPtr<gp_Elips2d>()
34 static gp_Elips2d* newEllipse(const gp_Pnt2d& theCenter,
35 const gp_Dir2d& theXAxis,
36 const double theMajorRadius,
37 const double theMinorRadius)
39 if (theMajorRadius < theMinorRadius - Precision::Confusion()) {
40 return newEllipse(theCenter, gp_Dir2d(-theXAxis.Y(), theXAxis.X()),
41 theMinorRadius, theMajorRadius);
44 gp_Ax22d anAxis(theCenter, theXAxis);
45 return new gp_Elips2d(anAxis, theMajorRadius, theMinorRadius);
48 static gp_Elips2d* newEllipse(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
49 const std::shared_ptr<GeomAPI_Pnt2d>& theAxisPoint,
50 const std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
52 const gp_Pnt2d& aCenter = theCenter->impl<gp_Pnt2d>();
53 const gp_Pnt2d& anAxisPnt = theAxisPoint->impl<gp_Pnt2d>();
54 const gp_Pnt2d& aPassedPnt = thePassingPoint->impl<gp_Pnt2d>();
56 gp_Dir2d aXAxis(anAxisPnt.XY() - aCenter.XY());
57 double aMajorRadius = anAxisPnt.Distance(aCenter);
59 gp_XY aPassedDir = aPassedPnt.XY() - aCenter.XY();
61 double X = aPassedDir.Dot(aXAxis.XY()) / aMajorRadius;
62 if (Abs(X) > 1.0 - Precision::Confusion())
63 return 0; // ellipse cannot be created for such parameters
65 double Y = aPassedDir.CrossMagnitude(aXAxis.XY());
66 double aMinorRadius = Y / Sqrt(1. - X * X);
68 return newEllipse(aCenter, aXAxis, aMajorRadius, aMinorRadius);
72 GeomAPI_Ellipse2d::GeomAPI_Ellipse2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
73 const std::shared_ptr<GeomAPI_Dir2d>& theXAxis,
74 const double theMajorRadius,
75 const double theMinorRadius)
76 : GeomAPI_Interface(newEllipse(theCenter->impl<gp_Pnt2d>(), theXAxis->impl<gp_Dir2d>(),
77 theMajorRadius, theMinorRadius))
81 GeomAPI_Ellipse2d::GeomAPI_Ellipse2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
82 const std::shared_ptr<GeomAPI_Pnt2d>& theAxisPoint,
83 const std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
84 : GeomAPI_Interface(newEllipse(theCenter, theAxisPoint, thePassingPoint))
88 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::center() const
90 const gp_Pnt2d& aCenter = MY_ELLIPSE->Location();
91 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
94 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::firstFocus() const
96 const gp_Pnt2d& aFirst = MY_ELLIPSE->Focus1();
97 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aFirst.X(), aFirst.Y()));
100 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::secondFocus() const
102 const gp_Pnt2d& aSecond = MY_ELLIPSE->Focus2();
103 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aSecond.X(), aSecond.Y()));
106 double GeomAPI_Ellipse2d::minorRadius() const
108 return MY_ELLIPSE->MinorRadius();
111 double GeomAPI_Ellipse2d::majorRadius() const
113 return MY_ELLIPSE->MajorRadius();