Salome HOME
4985ae3f4d9ddb629a1eb16271965cda6f923aab
[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
8 #include <gp_Dir2d.hxx>
9 #include <gp_Circ2d.hxx>
10 #include <gp_Pnt2d.hxx>
11 #include <gp_Ax2d.hxx>
12 #include <Geom2d_Circle.hxx>
13 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
14 #include <Precision.hxx>
15
16 #include <IntAna2d_AnaIntersection.hxx>
17
18 #define MY_CIRC2D static_cast<gp_Circ2d*>(myImpl)
19
20 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY,
21                             const double thePointX,   const double thePointY)
22 {
23   gp_Pnt2d aCenter(theCenterX, theCenterY);
24   gp_Pnt2d aPoint(thePointX, thePointY);
25
26   double aRadius = aCenter.Distance(aPoint);
27
28   if (aCenter.IsEqual(aPoint, Precision::Confusion()))
29       return NULL;
30
31   gp_Dir2d aDir(theCenterX - thePointX, theCenterY - thePointY);
32   return new gp_Circ2d(gp_Ax2d(aCenter, aDir), aRadius);
33 }
34
35
36 GeomAPI_Circ2d::GeomAPI_Circ2d(const boost::shared_ptr<GeomAPI_Pnt2d>& theCenter,
37                                const boost::shared_ptr<GeomAPI_Pnt2d>& theCirclePoint)
38   : GeomAPI_Interface(newCirc2d(theCenter->x(), theCenter->y(),
39                                 theCirclePoint->x(),   theCirclePoint->y()))
40 {}
41
42 const boost::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::project(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
43 {
44   boost::shared_ptr<GeomAPI_Pnt2d> aResult;
45   if (!MY_CIRC2D)
46     return aResult;
47
48   Handle(Geom2d_Circle) aCircle = new Geom2d_Circle(MY_CIRC2D->Axis(), MY_CIRC2D->Radius());//(aCirc);
49
50   const gp_Pnt2d& aPoint = thePoint->impl<gp_Pnt2d>();
51
52   Geom2dAPI_ProjectPointOnCurve aProj(aPoint, aCircle);
53   Standard_Integer aNbPoint = aProj.NbPoints();
54   double aX, anY;
55   if (aNbPoint > 0) {
56     double aMinDistance = 0, aDistance;
57     for (Standard_Integer j = 1; j <= aNbPoint; j++) {
58       gp_Pnt2d aNewPoint = aProj.Point(j);
59       aDistance = aNewPoint.Distance(aPoint);
60       if (!aMinDistance || aDistance < aMinDistance) {
61         aX = aNewPoint.X();
62         anY = aNewPoint.Y();
63         aMinDistance = aDistance;
64         aResult = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY));
65       }
66     }
67   }
68   return aResult;
69 }
70