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