Salome HOME
7cb9f20ba2fdb02df4fb556aa8af1b27e040e562
[modules/shaper.git] / src / GeomAPI / GeomAPI_Circ2d.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Circ2d.cpp
4 // Created:     29 May 2014
5 // Author:      Artem ZHIDKOV
6
7 #include <GeomAPI_Circ2d.h>
8 #include <GeomAPI_Pnt2d.h>
9 #include <GeomAPI_Dir2d.h>
10
11 #include <gp_Circ2d.hxx>
12 #include <gp_Pnt2d.hxx>
13 #include <GeomLib_Tool.hxx>
14 #include <Geom2d_Circle.hxx>
15 #include <Precision.hxx>
16
17 #define MY_CIRC2D implPtr<gp_Circ2d>()
18
19
20 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY, const gp_Dir2d theDir,
21                             const double theRadius)
22 {
23   gp_Pnt2d aCenter(theCenterX, theCenterY);
24   return new gp_Circ2d(gp_Ax2d(aCenter, theDir), theRadius);
25 }
26
27 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY,
28                             const double thePointX, const double thePointY)
29 {
30   gp_Pnt2d aCenter(theCenterX, theCenterY);
31   gp_Pnt2d aPoint(thePointX, thePointY);
32
33   double aRadius = aCenter.Distance(aPoint);
34
35   if (aCenter.IsEqual(aPoint, Precision::Confusion()))
36     return NULL;
37
38   gp_Dir2d aDir(thePointX - theCenterX, thePointY - theCenterY);
39
40   return newCirc2d(theCenterX, theCenterY, aDir, aRadius);
41 }
42
43
44
45 GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
46                                const std::shared_ptr<GeomAPI_Pnt2d>& theCirclePoint)
47     : GeomAPI_Interface(
48         newCirc2d(theCenter->x(), theCenter->y(), theCirclePoint->x(), theCirclePoint->y()))
49 {
50 }
51
52 GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
53                                const std::shared_ptr<GeomAPI_Dir2d>& theDir, double theRadius)
54     : GeomAPI_Interface(
55         newCirc2d(theCenter->x(), theCenter->y(), theDir->impl<gp_Dir2d>(), theRadius))
56 {
57 }
58
59 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::project(
60     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
61 {
62   std::shared_ptr<GeomAPI_Pnt2d> aResult;
63   if (!MY_CIRC2D)
64     return aResult;
65
66   const gp_Pnt2d& aCenter = MY_CIRC2D->Location();
67   const gp_Pnt2d& aPoint = thePoint->impl<gp_Pnt2d>();
68
69   double aDist = aCenter.Distance(aPoint);
70   if (aDist < Precision::Confusion())
71     return aResult;
72
73   if (Abs(aDist - MY_CIRC2D->Radius()) < Precision::Confusion()) {
74     // Point on the circle
75     aResult = std::shared_ptr<GeomAPI_Pnt2d>(
76         new GeomAPI_Pnt2d(thePoint->x(), thePoint->y()));
77   } else {
78     gp_Dir2d aDir(aPoint.XY() - aCenter.XY());
79     gp_XY aNewPoint = aCenter.XY() + aDir.XY() * MY_CIRC2D->Radius();
80     aResult = std::shared_ptr<GeomAPI_Pnt2d>(
81         new GeomAPI_Pnt2d(aNewPoint.X(), aNewPoint.Y()));
82   }
83
84   return aResult;
85 }
86
87 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::center() const
88 {
89   if (!MY_CIRC2D)
90     return std::shared_ptr<GeomAPI_Pnt2d>();
91   const gp_Pnt2d& aCenter = MY_CIRC2D->Location();
92   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
93 }
94
95 double GeomAPI_Circ2d::radius() const
96 {
97   if (!MY_CIRC2D)
98     return 0.0;
99   return MY_CIRC2D->Radius();
100 }
101
102 //=================================================================================================
103 const bool GeomAPI_Circ2d::parameter(const std::shared_ptr<GeomAPI_Pnt2d> thePoint,
104                                    const double theTolerance,
105                                    double& theParameter) const
106 {
107   Handle(Geom2d_Circle) aCurve = new Geom2d_Circle(*MY_CIRC2D);
108   return GeomLib_Tool::Parameter(aCurve, thePoint->impl<gp_Pnt2d>(),
109                                  theTolerance, theParameter) == Standard_True;
110 }
111
112 //=================================================================================================
113 void GeomAPI_Circ2d::D0(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
114 {
115   Handle(Geom2d_Circle) aCurve = new Geom2d_Circle(*MY_CIRC2D);
116   gp_Pnt2d aPnt;
117   aCurve->D0(theU, aPnt);
118   thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
119 }
120