Salome HOME
Free memory on deletion of handle object
[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_Dir2d.hxx>
12 #include <gp_Circ2d.hxx>
13 #include <gp_Pnt2d.hxx>
14 #include <gp_Ax2d.hxx>
15 #include <Geom2d_Circle.hxx>
16 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
17 #include <Precision.hxx>
18
19 #include <IntAna2d_AnaIntersection.hxx>
20
21 #define MY_CIRC2D static_cast<gp_Circ2d*>(myImpl)
22
23 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY, const gp_Dir2d theDir,
24                             const double theRadius)
25 {
26   gp_Pnt2d aCenter(theCenterX, theCenterY);
27   return new gp_Circ2d(gp_Ax2d(aCenter, theDir), theRadius);
28 }
29
30 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY,
31                             const double thePointX, const double thePointY)
32 {
33   gp_Pnt2d aCenter(theCenterX, theCenterY);
34   gp_Pnt2d aPoint(thePointX, thePointY);
35
36   double aRadius = aCenter.Distance(aPoint);
37
38   if (aCenter.IsEqual(aPoint, Precision::Confusion()))
39     return NULL;
40
41   gp_Dir2d aDir(theCenterX - thePointX, theCenterY - thePointY);
42
43   return newCirc2d(theCenterX, theCenterY, aDir, aRadius);
44 }
45
46 GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
47                                const std::shared_ptr<GeomAPI_Pnt2d>& theCirclePoint)
48     : GeomAPI_Interface(
49         newCirc2d(theCenter->x(), theCenter->y(), theCirclePoint->x(), theCirclePoint->y()))
50 {
51 }
52
53 GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
54                                const std::shared_ptr<GeomAPI_Dir2d>& theDir, double theRadius)
55     : GeomAPI_Interface(
56         newCirc2d(theCenter->x(), theCenter->y(), theDir->impl<gp_Dir2d>(), theRadius))
57 {
58
59 }
60
61 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::project(
62     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
63 {
64   std::shared_ptr<GeomAPI_Pnt2d> aResult;
65   if (!MY_CIRC2D)
66     return aResult;
67
68   const gp_Pnt2d& aCenter = MY_CIRC2D->Location();
69   const gp_Pnt2d& aPoint = thePoint->impl<gp_Pnt2d>();
70
71   double aDist = aCenter.Distance(aPoint);
72   if (aDist < Precision::Confusion())
73     return aResult;
74
75   if (Abs(aDist - MY_CIRC2D->Radius()) < Precision::Confusion()) {
76     // Point on the circle
77     aResult = std::shared_ptr<GeomAPI_Pnt2d>(
78         new GeomAPI_Pnt2d(thePoint->x(), thePoint->y()));
79   } else {
80     gp_Dir2d aDir(aPoint.XY() - aCenter.XY());
81     gp_XY aNewPoint = aCenter.XY() + aDir.XY() * MY_CIRC2D->Radius();
82     aResult = std::shared_ptr<GeomAPI_Pnt2d>(
83         new GeomAPI_Pnt2d(aNewPoint.X(), aNewPoint.Y()));
84   }
85
86   return aResult;
87 }
88
89 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::center() const
90 {
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   return MY_CIRC2D->Radius();
98 }
99