Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / GeomAPI / GeomAPI_Circ2d.cpp
1 // Copyright (C) 2014-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 #include <GeomAPI_Circ2d.h>
22 #include <GeomAPI_Pnt2d.h>
23 #include <GeomAPI_Dir2d.h>
24
25 #include <gp_Circ2d.hxx>
26 #include <gp_Pnt2d.hxx>
27 #include <GeomLib_Tool.hxx>
28 #include <Geom2d_Circle.hxx>
29 #include <Precision.hxx>
30
31 #define MY_CIRC2D implPtr<gp_Circ2d>()
32
33
34 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY, const gp_Dir2d theDir,
35                             const double theRadius)
36 {
37   gp_Pnt2d aCenter(theCenterX, theCenterY);
38   return new gp_Circ2d(gp_Ax2d(aCenter, theDir), theRadius);
39 }
40
41 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY,
42                             const double thePointX, const double thePointY)
43 {
44   gp_Pnt2d aCenter(theCenterX, theCenterY);
45   gp_Pnt2d aPoint(thePointX, thePointY);
46
47   double aRadius = aCenter.Distance(aPoint);
48
49   if (aCenter.IsEqual(aPoint, Precision::Confusion()))
50     return NULL;
51
52   gp_Dir2d aDir(thePointX - theCenterX, thePointY - theCenterY);
53
54   return newCirc2d(theCenterX, theCenterY, aDir, aRadius);
55 }
56
57
58
59 GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
60                                const std::shared_ptr<GeomAPI_Pnt2d>& theCirclePoint)
61     : GeomAPI_Interface(
62         newCirc2d(theCenter->x(), theCenter->y(), theCirclePoint->x(), theCirclePoint->y()))
63 {
64 }
65
66 GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
67                                const std::shared_ptr<GeomAPI_Dir2d>& theDir, double theRadius)
68     : GeomAPI_Interface(
69         newCirc2d(theCenter->x(), theCenter->y(), theDir->impl<gp_Dir2d>(), theRadius))
70 {
71 }
72
73 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::project(
74     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
75 {
76   std::shared_ptr<GeomAPI_Pnt2d> aResult;
77   if (!MY_CIRC2D)
78     return aResult;
79
80   const gp_Pnt2d& aCenter = MY_CIRC2D->Location();
81   const gp_Pnt2d& aPoint = thePoint->impl<gp_Pnt2d>();
82
83   double aDist = aCenter.Distance(aPoint);
84   if (aDist < Precision::Confusion())
85     return aResult;
86
87   if (Abs(aDist - MY_CIRC2D->Radius()) < Precision::Confusion()) {
88     // Point on the circle
89     aResult = std::shared_ptr<GeomAPI_Pnt2d>(
90         new GeomAPI_Pnt2d(thePoint->x(), thePoint->y()));
91   } else {
92     gp_Dir2d aDir(aPoint.XY() - aCenter.XY());
93     gp_XY aNewPoint = aCenter.XY() + aDir.XY() * MY_CIRC2D->Radius();
94     aResult = std::shared_ptr<GeomAPI_Pnt2d>(
95         new GeomAPI_Pnt2d(aNewPoint.X(), aNewPoint.Y()));
96   }
97
98   return aResult;
99 }
100
101 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::center() const
102 {
103   if (!MY_CIRC2D)
104     return std::shared_ptr<GeomAPI_Pnt2d>();
105   const gp_Pnt2d& aCenter = MY_CIRC2D->Location();
106   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
107 }
108
109 double GeomAPI_Circ2d::radius() const
110 {
111   if (!MY_CIRC2D)
112     return 0.0;
113   return MY_CIRC2D->Radius();
114 }
115
116 //=================================================================================================
117 const bool GeomAPI_Circ2d::parameter(const std::shared_ptr<GeomAPI_Pnt2d> thePoint,
118                                    const double theTolerance,
119                                    double& theParameter) const
120 {
121   Handle(Geom2d_Circle) aCurve = new Geom2d_Circle(*MY_CIRC2D);
122   return GeomLib_Tool::Parameter(aCurve, thePoint->impl<gp_Pnt2d>(),
123                                  theTolerance, theParameter) == Standard_True;
124 }
125
126 //=================================================================================================
127 void GeomAPI_Circ2d::D0(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
128 {
129   Handle(Geom2d_Circle) aCurve = new Geom2d_Circle(*MY_CIRC2D);
130   gp_Pnt2d aPnt;
131   aCurve->D0(theU, aPnt);
132   thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
133 }
134