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