Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[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_Circ2d.h>
26 #include <GeomAPI_Dir2d.h>
27 #include <GeomAPI_Lin2d.h>
28 #include <GeomAPI_Pnt2d.h>
29
30 #include <Extrema_ExtCC2d.hxx>
31 #include <Extrema_ExtElC2d.hxx>
32 #include <Geom2d_Ellipse.hxx>
33 #include <Geom2dAdaptor_Curve.hxx>
34 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
35 #include <GeomLib_Tool.hxx>
36 #include <gp_Ax22d.hxx>
37 #include <gp_Elips2d.hxx>
38 #include <IntAna2d_AnaIntersection.hxx>
39 #include <IntAna2d_Conic.hxx>
40 #include <Precision.hxx>
41
42 #define MY_ELLIPSE implPtr<gp_Elips2d>()
43
44 static gp_Elips2d* newEllipse(const gp_Pnt2d& theCenter,
45                               const gp_Dir2d& theXAxis,
46                               const double theMajorRadius,
47                               const double theMinorRadius)
48 {
49   if (theMajorRadius < theMinorRadius - Precision::Confusion()) {
50     return newEllipse(theCenter, gp_Dir2d(-theXAxis.Y(), theXAxis.X()),
51                       theMinorRadius, theMajorRadius);
52   }
53
54   gp_Ax22d anAxis(theCenter, theXAxis);
55   return new gp_Elips2d(anAxis, theMajorRadius, theMinorRadius);
56 }
57
58 static gp_Elips2d* newEllipse(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
59                               const std::shared_ptr<GeomAPI_Pnt2d>& theAxisPoint,
60                               const std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
61 {
62   const gp_Pnt2d& aCenter = theCenter->impl<gp_Pnt2d>();
63   const gp_Pnt2d& anAxisPnt = theAxisPoint->impl<gp_Pnt2d>();
64   const gp_Pnt2d& aPassedPnt = thePassingPoint->impl<gp_Pnt2d>();
65
66   gp_Dir2d aXAxis(anAxisPnt.XY() - aCenter.XY());
67   double aMajorRadius = anAxisPnt.Distance(aCenter);
68
69   gp_XY aPassedDir = aPassedPnt.XY() - aCenter.XY();
70
71   double X = aPassedDir.Dot(aXAxis.XY()) / aMajorRadius;
72   if (Abs(X) > 1.0 - Precision::Confusion())
73     return 0; // ellipse cannot be created for such parameters
74
75   double Y = aPassedDir.CrossMagnitude(aXAxis.XY());
76   double aMinorRadius = Y / Sqrt(1. - X * X);
77
78   return newEllipse(aCenter, aXAxis, aMajorRadius, aMinorRadius);
79 }
80
81
82 GeomAPI_Ellipse2d::GeomAPI_Ellipse2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
83                                      const std::shared_ptr<GeomAPI_Dir2d>& theXAxis,
84                                      const double theMajorRadius,
85                                      const double theMinorRadius)
86   : GeomAPI_Interface(newEllipse(theCenter->impl<gp_Pnt2d>(), theXAxis->impl<gp_Dir2d>(),
87                                  theMajorRadius, theMinorRadius))
88 {
89 }
90
91 GeomAPI_Ellipse2d::GeomAPI_Ellipse2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
92                                      const std::shared_ptr<GeomAPI_Pnt2d>& theAxisPoint,
93                                      const std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
94   : GeomAPI_Interface(newEllipse(theCenter, theAxisPoint, thePassingPoint))
95 {
96 }
97
98 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::center() const
99 {
100   const gp_Pnt2d& aCenter = MY_ELLIPSE->Location();
101   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
102 }
103
104 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::firstFocus() const
105 {
106   const gp_Pnt2d& aFirst = MY_ELLIPSE->Focus1();
107   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aFirst.X(), aFirst.Y()));
108 }
109
110 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::secondFocus() const
111 {
112   const gp_Pnt2d& aSecond = MY_ELLIPSE->Focus2();
113   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aSecond.X(), aSecond.Y()));
114 }
115
116 double GeomAPI_Ellipse2d::minorRadius() const
117 {
118   return MY_ELLIPSE->MinorRadius();
119 }
120
121 double GeomAPI_Ellipse2d::majorRadius() const
122 {
123   return MY_ELLIPSE->MajorRadius();
124 }
125
126 // theArrangePoint is used to select the nearest solution point if intersection is detected
127 template <typename EXTREMAPTR>
128 static double extrema(IntAna2d_AnaIntersection* theIntersectionAlgo,
129                       EXTREMAPTR theExtremaAlgo,
130                       GeomPnt2dPtr theArrangePoint,
131                       GeomPnt2dPtr& thePoint1,
132                       GeomPnt2dPtr& thePoint2)
133 {
134   double aDistance = Precision::Infinite();
135   if (theIntersectionAlgo->IsDone() && theIntersectionAlgo->NbPoints() > 0) {
136     gp_Pnt2d anArrangePoint(theArrangePoint->x(), theArrangePoint->y());
137     gp_Pnt2d anInterPnt = theIntersectionAlgo->Point(1).Value();
138     aDistance = anArrangePoint.SquareDistance(anInterPnt);
139     // get solution nearest to theArrangePoint
140     for (int i = 2; i <= theIntersectionAlgo->NbPoints(); ++i) {
141       const IntAna2d_IntPoint& aPnt = theIntersectionAlgo->Point(i);
142       double aSqDist = aPnt.Value().SquareDistance(anArrangePoint);
143       if (aSqDist < aDistance) {
144         aDistance = aSqDist;
145         anInterPnt = aPnt.Value();
146       }
147     }
148
149     aDistance = 0.0; // curves are intersected
150     thePoint1 = GeomPnt2dPtr(new GeomAPI_Pnt2d(anInterPnt.X(), anInterPnt.Y()));
151     thePoint2 = GeomPnt2dPtr(new GeomAPI_Pnt2d(anInterPnt.X(), anInterPnt.Y()));
152   }
153   else if (theExtremaAlgo->IsDone() && theExtremaAlgo->NbExt() > 0) {
154     Extrema_POnCurv2d aP1, aP2;
155     for (int i = 1; i <= theExtremaAlgo->NbExt(); ++i) {
156       double aSqDist = theExtremaAlgo->SquareDistance(i);
157       if (aSqDist < aDistance) {
158         aDistance = aSqDist;
159         theExtremaAlgo->Points(i, aP1, aP2);
160       }
161     }
162     aDistance = Sqrt(aDistance);
163     thePoint1 = GeomPnt2dPtr(new GeomAPI_Pnt2d(aP1.Value().X(), aP1.Value().Y()));
164     thePoint2 = GeomPnt2dPtr(new GeomAPI_Pnt2d(aP2.Value().X(), aP2.Value().Y()));
165   }
166   return aDistance;
167 }
168
169 double GeomAPI_Ellipse2d::distance(const std::shared_ptr<GeomAPI_Lin2d>& theLine,
170                                    std::shared_ptr<GeomAPI_Pnt2d>& thePointOnMe,
171                                    std::shared_ptr<GeomAPI_Pnt2d>& thePointOnLine)
172 {
173   IntAna2d_AnaIntersection anInter(theLine->impl<gp_Lin2d>(), IntAna2d_Conic(*MY_ELLIPSE));
174   Extrema_ExtElC2d anExtema(theLine->impl<gp_Lin2d>(), *MY_ELLIPSE);
175   return extrema(&anInter, &anExtema, theLine->location(), thePointOnLine, thePointOnMe);
176 }
177
178 double GeomAPI_Ellipse2d::distance(const std::shared_ptr<GeomAPI_Circ2d>& theCircle,
179                                    std::shared_ptr<GeomAPI_Pnt2d>& thePointOnMe,
180                                    std::shared_ptr<GeomAPI_Pnt2d>& thePointOnCircle)
181 {
182   IntAna2d_AnaIntersection anInter(theCircle->impl<gp_Circ2d>(), IntAna2d_Conic(*MY_ELLIPSE));
183   Extrema_ExtElC2d anExtema(theCircle->impl<gp_Circ2d>(), *MY_ELLIPSE);
184   GeomPnt2dPtr aCircleStart;
185   theCircle->D0(0.0, aCircleStart);
186   return extrema(&anInter, &anExtema, aCircleStart, thePointOnCircle, thePointOnMe);
187 }
188
189 double GeomAPI_Ellipse2d::distance(const std::shared_ptr<GeomAPI_Ellipse2d>& theEllipse,
190                                    std::shared_ptr<GeomAPI_Pnt2d>& thePointOnMe,
191                                    std::shared_ptr<GeomAPI_Pnt2d>& thePointOnEllipse)
192 {
193   Handle(Geom2d_Ellipse) anEllipse1 = new Geom2d_Ellipse(theEllipse->impl<gp_Elips2d>());
194   Handle(Geom2d_Ellipse) anEllipse2 = new Geom2d_Ellipse(*MY_ELLIPSE);
195
196   IntAna2d_AnaIntersection anInter(theEllipse->impl<gp_Elips2d>(), IntAna2d_Conic(*MY_ELLIPSE));
197   Extrema_ExtCC2d* anExtema =
198       new Extrema_ExtCC2d(Geom2dAdaptor_Curve(anEllipse1), Geom2dAdaptor_Curve(anEllipse2));
199   double aDistance = extrema(&anInter, anExtema, theEllipse->firstFocus(), thePointOnEllipse, thePointOnMe);
200   delete anExtema;
201   return aDistance;
202 }
203
204 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::project(
205     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
206 {
207   std::shared_ptr<GeomAPI_Pnt2d> aResult;
208   if (!MY_ELLIPSE)
209     return aResult;
210
211   Handle(Geom2d_Ellipse) aEllipse = new Geom2d_Ellipse(*MY_ELLIPSE);
212
213   const gp_Pnt2d& aPoint = thePoint->impl<gp_Pnt2d>();
214
215   Geom2dAPI_ProjectPointOnCurve aProj(aPoint, aEllipse);
216   Standard_Integer aNbPoint = aProj.NbPoints();
217   if (aNbPoint > 0) {
218     gp_Pnt2d aNearest = aProj.NearestPoint();
219     aResult.reset(new GeomAPI_Pnt2d(aNearest.X(), aNearest.Y()));
220   }
221   return aResult;
222 }
223
224 const bool GeomAPI_Ellipse2d::parameter(const std::shared_ptr<GeomAPI_Pnt2d> thePoint,
225                                         const double theTolerance,
226                                         double& theParameter) const
227 {
228   Handle(Geom2d_Ellipse) aCurve = new Geom2d_Ellipse(*MY_ELLIPSE);
229   return GeomLib_Tool::Parameter(aCurve, thePoint->impl<gp_Pnt2d>(),
230                                  theTolerance, theParameter) == Standard_True;
231 }