1 // Copyright (C) 2017-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: GeomAPI_Ellipse2d.cpp
21 // Created: 26 April 2017
22 // Author: Artem ZHIDKOV
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>
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>
42 #define MY_ELLIPSE implPtr<gp_Elips2d>()
44 static gp_Elips2d* newEllipse(const gp_Pnt2d& theCenter,
45 const gp_Dir2d& theXAxis,
46 const double theMajorRadius,
47 const double theMinorRadius)
49 if (theMajorRadius < theMinorRadius - Precision::Confusion()) {
50 return newEllipse(theCenter, gp_Dir2d(-theXAxis.Y(), theXAxis.X()),
51 theMinorRadius, theMajorRadius);
54 gp_Ax22d anAxis(theCenter, theXAxis);
55 return new gp_Elips2d(anAxis, theMajorRadius, theMinorRadius);
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)
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>();
66 gp_Dir2d aXAxis(anAxisPnt.XY() - aCenter.XY());
67 double aMajorRadius = anAxisPnt.Distance(aCenter);
69 gp_XY aPassedDir = aPassedPnt.XY() - aCenter.XY();
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
75 double Y = aPassedDir.CrossMagnitude(aXAxis.XY());
76 double aMinorRadius = Y / Sqrt(1. - X * X);
78 return newEllipse(aCenter, aXAxis, aMajorRadius, aMinorRadius);
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))
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))
98 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::center() const
100 const gp_Pnt2d& aCenter = MY_ELLIPSE->Location();
101 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
104 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::firstFocus() const
106 const gp_Pnt2d& aFirst = MY_ELLIPSE->Focus1();
107 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aFirst.X(), aFirst.Y()));
110 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::secondFocus() const
112 const gp_Pnt2d& aSecond = MY_ELLIPSE->Focus2();
113 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aSecond.X(), aSecond.Y()));
116 double GeomAPI_Ellipse2d::minorRadius() const
118 return MY_ELLIPSE->MinorRadius();
121 double GeomAPI_Ellipse2d::majorRadius() const
123 return MY_ELLIPSE->MajorRadius();
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)
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) {
145 anInterPnt = aPnt.Value();
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()));
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) {
159 theExtremaAlgo->Points(i, aP1, aP2);
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()));
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)
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);
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)
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);
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)
193 Handle(Geom2d_Ellipse) anEllipse1 = new Geom2d_Ellipse(theEllipse->impl<gp_Elips2d>());
194 Handle(Geom2d_Ellipse) anEllipse2 = new Geom2d_Ellipse(*MY_ELLIPSE);
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(),
200 thePointOnEllipse, thePointOnMe);
205 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Ellipse2d::project(
206 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
208 std::shared_ptr<GeomAPI_Pnt2d> aResult;
212 Handle(Geom2d_Ellipse) aEllipse = new Geom2d_Ellipse(*MY_ELLIPSE);
214 const gp_Pnt2d& aPoint = thePoint->impl<gp_Pnt2d>();
216 Geom2dAPI_ProjectPointOnCurve aProj(aPoint, aEllipse);
217 Standard_Integer aNbPoint = aProj.NbPoints();
219 gp_Pnt2d aNearest = aProj.NearestPoint();
220 aResult.reset(new GeomAPI_Pnt2d(aNearest.X(), aNearest.Y()));
225 const bool GeomAPI_Ellipse2d::parameter(const std::shared_ptr<GeomAPI_Pnt2d> thePoint,
226 const double theTolerance,
227 double& theParameter) const
229 Handle(Geom2d_Ellipse) aCurve = new Geom2d_Ellipse(*MY_ELLIPSE);
230 return GeomLib_Tool::Parameter(aCurve, thePoint->impl<gp_Pnt2d>(),
231 theTolerance, theParameter) == Standard_True;