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