1 // Copyright (C) 2017-20xx CEA/DEN, EDF R&D
3 // File: GeomAPI_Ellipse2d.cpp
4 // Created: 26 April 2017
5 // Author: Artem ZHIDKOV
7 #include <GeomAPI_Ellipse2d.h>
8 #include <GeomAPI_Dir2d.h>
9 #include <GeomAPI_Pnt2d.h>
11 #include <gp_Ax22d.hxx>
12 #include <gp_Elips2d.hxx>
13 #include <Precision.hxx>
15 #define MY_ELLIPSE implPtr<gp_Elips2d>()
17 static gp_Elips2d* newEllipse(const gp_Pnt2d& theCenter,
18 const gp_Dir2d& theXAxis,
19 const double theMajorRadius,
20 const double theMinorRadius)
22 if (theMajorRadius < theMinorRadius - Precision::Confusion()) {
23 return newEllipse(theCenter, gp_Dir2d(-theXAxis.Y(), theXAxis.X()),
24 theMinorRadius, theMajorRadius);
27 gp_Ax22d anAxis(theCenter, theXAxis);
28 return new gp_Elips2d(anAxis, theMajorRadius, theMinorRadius);
31 static gp_Elips2d* newEllipse(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
32 const std::shared_ptr<GeomAPI_Pnt2d>& theAxisPoint,
33 const std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
35 const gp_Pnt2d& aCenter = theCenter->impl<gp_Pnt2d>();
36 const gp_Pnt2d& anAxisPnt = theAxisPoint->impl<gp_Pnt2d>();
37 const gp_Pnt2d& aPassedPnt = thePassingPoint->impl<gp_Pnt2d>();
39 gp_Dir2d aXAxis(anAxisPnt.XY() - aCenter.XY());
40 double aMajorRadius = anAxisPnt.Distance(aCenter);
42 gp_XY aPassedDir = aPassedPnt.XY() - aCenter.XY();
44 double X = aPassedDir.Dot(aXAxis.XY()) / aMajorRadius;
45 if (Abs(X) > 1.0 - Precision::Confusion())
46 return 0; // ellipse cannot be created for such parameters
48 double Y = aPassedDir.CrossMagnitude(aXAxis.XY());
49 double aMinorRadius = Y / Sqrt(1. - X * X);
51 return newEllipse(aCenter, aXAxis, aMajorRadius, aMinorRadius);
55 GeomAPI_Ellipse2d::GeomAPI_Ellipse2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
56 const std::shared_ptr<GeomAPI_Dir2d>& theXAxis,
57 const double theMajorRadius,
58 const double theMinorRadius)
59 : GeomAPI_Interface(newEllipse(theCenter->impl<gp_Pnt2d>(), theXAxis->impl<gp_Dir2d>(),
60 theMajorRadius, theMinorRadius))
64 GeomAPI_Ellipse2d::GeomAPI_Ellipse2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
65 const std::shared_ptr<GeomAPI_Pnt2d>& theAxisPoint,
66 const std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
67 : GeomAPI_Interface(newEllipse(theCenter, theAxisPoint, thePassingPoint))
71 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::center() const
73 const gp_Pnt2d& aCenter = MY_ELLIPSE->Location();
74 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
77 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::firstFocus() const
79 const gp_Pnt2d& aFirst = MY_ELLIPSE->Focus1();
80 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aFirst.X(), aFirst.Y()));
83 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::secondFocus() const
85 const gp_Pnt2d& aSecond = MY_ELLIPSE->Focus2();
86 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aSecond.X(), aSecond.Y()));
89 double GeomAPI_Ellipse2d::minorRadius() const
91 return MY_ELLIPSE->MinorRadius();
94 double GeomAPI_Ellipse2d::majorRadius() const
96 return MY_ELLIPSE->MajorRadius();