Salome HOME
Task #3005 : To be able to create a group on a whole result
[modules/shaper.git] / src / GeomAPI / GeomAPI_Ellipse2d.cpp
1 // Copyright (C) 2017-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:        GeomAPI_Ellipse2d.cpp
21 // Created:     26 April 2017
22 // Author:      Artem ZHIDKOV
23
24 #include <GeomAPI_Ellipse2d.h>
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomAPI_Pnt2d.h>
27
28 #include <gp_Ax22d.hxx>
29 #include <gp_Elips2d.hxx>
30 #include <Precision.hxx>
31
32 #define MY_ELLIPSE implPtr<gp_Elips2d>()
33
34 static gp_Elips2d* newEllipse(const gp_Pnt2d& theCenter,
35                               const gp_Dir2d& theXAxis,
36                               const double theMajorRadius,
37                               const double theMinorRadius)
38 {
39   if (theMajorRadius < theMinorRadius - Precision::Confusion()) {
40     return newEllipse(theCenter, gp_Dir2d(-theXAxis.Y(), theXAxis.X()),
41                       theMinorRadius, theMajorRadius);
42   }
43
44   gp_Ax22d anAxis(theCenter, theXAxis);
45   return new gp_Elips2d(anAxis, theMajorRadius, theMinorRadius);
46 }
47
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)
51 {
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>();
55
56   gp_Dir2d aXAxis(anAxisPnt.XY() - aCenter.XY());
57   double aMajorRadius = anAxisPnt.Distance(aCenter);
58
59   gp_XY aPassedDir = aPassedPnt.XY() - aCenter.XY();
60
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
64
65   double Y = aPassedDir.CrossMagnitude(aXAxis.XY());
66   double aMinorRadius = Y / Sqrt(1. - X * X);
67
68   return newEllipse(aCenter, aXAxis, aMajorRadius, aMinorRadius);
69 }
70
71
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))
78 {
79 }
80
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))
85 {
86 }
87
88 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::center() const
89 {
90   const gp_Pnt2d& aCenter = MY_ELLIPSE->Location();
91   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
92 }
93
94 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::firstFocus() const
95 {
96   const gp_Pnt2d& aFirst = MY_ELLIPSE->Focus1();
97   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aFirst.X(), aFirst.Y()));
98 }
99
100 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::secondFocus() const
101 {
102   const gp_Pnt2d& aSecond = MY_ELLIPSE->Focus2();
103   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aSecond.X(), aSecond.Y()));
104 }
105
106 double GeomAPI_Ellipse2d::minorRadius() const
107 {
108   return MY_ELLIPSE->MinorRadius();
109 }
110
111 double GeomAPI_Ellipse2d::majorRadius() const
112 {
113   return MY_ELLIPSE->MajorRadius();
114 }