]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Circ2d.cpp
Salome HOME
Changes in the presentations of features
[modules/shaper.git] / src / GeomAPI / GeomAPI_Circ2d.cpp
1 // File:        GeomAPI_Circ2d.cpp
2 // Created:     29 May 2014
3 // Author:      Artem ZHIDKOV
4
5 #include <GeomAPI_Circ2d.h>
6 #include <GeomAPI_Pnt2d.h>
7 #include <GeomAPI_Dir2d.h>
8
9 #include <gp_Dir2d.hxx>
10 #include <gp_Circ2d.hxx>
11 #include <gp_Pnt2d.hxx>
12 #include <gp_Ax2d.hxx>
13 #include <Geom2d_Circle.hxx>
14 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
15 #include <Precision.hxx>
16
17 #include <IntAna2d_AnaIntersection.hxx>
18
19 #define MY_CIRC2D static_cast<gp_Circ2d*>(myImpl)
20
21 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY,
22                             const gp_Dir2d theDir, const double theRadius)
23 {
24   gp_Pnt2d aCenter(theCenterX, theCenterY);
25   return new gp_Circ2d(gp_Ax2d(aCenter, theDir), theRadius);
26 }
27
28 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY,
29                             const double thePointX,   const double thePointY)
30 {
31   gp_Pnt2d aCenter(theCenterX, theCenterY);
32   gp_Pnt2d aPoint(thePointX, thePointY);
33
34   double aRadius = aCenter.Distance(aPoint);
35
36   if (aCenter.IsEqual(aPoint, Precision::Confusion()))
37       return NULL;
38
39   gp_Dir2d aDir(theCenterX - thePointX, theCenterY - thePointY);
40   
41   return newCirc2d(theCenterX, theCenterY, aDir, aRadius);
42 }
43
44 GeomAPI_Circ2d::GeomAPI_Circ2d(const boost::shared_ptr<GeomAPI_Pnt2d>& theCenter,
45                                const boost::shared_ptr<GeomAPI_Pnt2d>& theCirclePoint)
46   : GeomAPI_Interface(newCirc2d(theCenter->x(), theCenter->y(),
47                                 theCirclePoint->x(),   theCirclePoint->y()))
48 {}
49
50 GeomAPI_Circ2d::GeomAPI_Circ2d(const boost::shared_ptr<GeomAPI_Pnt2d>& theCenter,
51                                const boost::shared_ptr<GeomAPI_Dir2d>& theDir,
52                                double theRadius)
53  : GeomAPI_Interface(newCirc2d(theCenter->x(), theCenter->y(),
54                                theDir->impl<gp_Dir2d>(), theRadius))
55 {
56
57 }
58
59 const boost::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::project(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
60 {
61   boost::shared_ptr<GeomAPI_Pnt2d> aResult;
62   if (!MY_CIRC2D)
63     return aResult;
64
65   Handle(Geom2d_Circle) aCircle = new Geom2d_Circle(MY_CIRC2D->Axis(), MY_CIRC2D->Radius());//(aCirc);
66
67   const gp_Pnt2d& aPoint = thePoint->impl<gp_Pnt2d>();
68
69   Geom2dAPI_ProjectPointOnCurve aProj(aPoint, aCircle);
70   Standard_Integer aNbPoint = aProj.NbPoints();
71   double aX, anY;
72   if (aNbPoint > 0) {
73     double aMinDistance = 0, aDistance;
74     for (Standard_Integer j = 1; j <= aNbPoint; j++) {
75       gp_Pnt2d aNewPoint = aProj.Point(j);
76       aDistance = aNewPoint.Distance(aPoint);
77       if (!aMinDistance || aDistance < aMinDistance) {
78         aX = aNewPoint.X();
79         anY = aNewPoint.Y();
80         aMinDistance = aDistance;
81         aResult = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY));
82       }
83     }
84   }
85   return aResult;
86 }
87
88 const boost::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::center() const
89 {
90   const gp_Pnt2d& aCenter = MY_CIRC2D->Location();
91   return boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
92 }
93
94 double GeomAPI_Circ2d::radius() const
95 {
96   return MY_CIRC2D->Radius();
97 }
98