]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Ellipse2d.cpp
Salome HOME
Task 2.1. Creation of ellipses and arcs of ellipse.
[modules/shaper.git] / src / GeomAPI / GeomAPI_Ellipse2d.cpp
1 // Copyright (C) 2017-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Ellipse2d.cpp
4 // Created:     26 April 2017
5 // Author:      Artem ZHIDKOV
6
7 #include <GeomAPI_Ellipse2d.h>
8 #include <GeomAPI_Dir2d.h>
9 #include <GeomAPI_Pnt2d.h>
10
11 #include <gp_Ax22d.hxx>
12 #include <gp_Elips2d.hxx>
13 #include <Precision.hxx>
14
15 #define MY_ELLIPSE implPtr<gp_Elips2d>()
16
17 static gp_Elips2d* newEllipse(const gp_Pnt2d& theCenter,
18                               const gp_Dir2d& theXAxis,
19                               const double theMajorRadius,
20                               const double theMinorRadius)
21 {
22   if (theMajorRadius < theMinorRadius - Precision::Confusion()) {
23     return newEllipse(theCenter, gp_Dir2d(-theXAxis.Y(), theXAxis.X()),
24                       theMinorRadius, theMajorRadius);
25   }
26
27   gp_Ax22d anAxis(theCenter, theXAxis);
28   return new gp_Elips2d(anAxis, theMajorRadius, theMinorRadius);
29 }
30
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)
34 {
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>();
38
39   gp_Dir2d aXAxis(anAxisPnt.XY() - aCenter.XY());
40   double aMajorRadius = anAxisPnt.Distance(aCenter);
41
42   gp_XY aPassedDir = aPassedPnt.XY() - aCenter.XY();
43
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
47
48   double Y = aPassedDir.CrossMagnitude(aXAxis.XY());
49   double aMinorRadius = Y / Sqrt(1. - X * X);
50
51   return newEllipse(aCenter, aXAxis, aMajorRadius, aMinorRadius);
52 }
53
54
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))
61 {
62 }
63
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))
68 {
69 }
70
71 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::center() const
72 {
73   const gp_Pnt2d& aCenter = MY_ELLIPSE->Location();
74   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
75 }
76
77 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::firstFocus() const
78 {
79   const gp_Pnt2d& aFirst = MY_ELLIPSE->Focus1();
80   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aFirst.X(), aFirst.Y()));
81 }
82
83 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::secondFocus() const
84 {
85   const gp_Pnt2d& aSecond = MY_ELLIPSE->Focus2();
86   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aSecond.X(), aSecond.Y()));
87 }
88
89 double GeomAPI_Ellipse2d::minorRadius() const
90 {
91   return MY_ELLIPSE->MinorRadius();
92 }
93
94 double GeomAPI_Ellipse2d::majorRadius() const
95 {
96   return MY_ELLIPSE->MajorRadius();
97 }