Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SketchSolver
[modules/shaper.git] / src / GeomAPI / GeomAPI_Circ.cpp
1 // File:        GeomAPI_Circ2cpp
2 // Created:     24 Jun 2014
3 // Author:      Artem ZHIDKOV
4
5 #include <GeomAPI_Circ.h>
6 #include <GeomAPI_Pnt.h>
7 #include <GeomAPI_Dir.h>
8
9 #include <gp_Dir.hxx>
10 #include <gp_Circ.hxx>
11 #include <gp_Pnt.hxx>
12 #include <gp_Ax2.hxx>
13
14 #include <Geom_Circle.hxx>
15 #include <GeomAPI_ProjectPointOnCurve.hxx>
16
17 #define MY_CIRC static_cast<gp_Circ*>(myImpl)
18
19 static gp_Circ* newCirc(const gp_Pnt& theCenter,
20                         const gp_Dir& theDir,
21                         const double  theRadius)
22 {
23   return new gp_Circ(gp_Ax2(theCenter, theDir), theRadius);
24 }
25
26 GeomAPI_Circ::GeomAPI_Circ(const boost::shared_ptr<GeomAPI_Pnt>& theCenter,
27                            const boost::shared_ptr<GeomAPI_Dir>& theDir,
28                            double                                theRadius)
29   : GeomAPI_Interface(newCirc(theCenter->impl<gp_Pnt>(),
30                               theDir->impl<gp_Dir>(), theRadius))
31 {
32 }
33
34 const boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Circ::project(const boost::shared_ptr<GeomAPI_Pnt>& thePoint) const
35 {
36   boost::shared_ptr<GeomAPI_Pnt> aResult;
37   if (!MY_CIRC)
38     return aResult;
39
40   Handle(Geom_Circle) aCircle = new Geom_Circle(*MY_CIRC);
41
42   const gp_Pnt& aPoint = thePoint->impl<gp_Pnt>();
43
44   GeomAPI_ProjectPointOnCurve aProj(aPoint, aCircle);
45   Standard_Integer aNbPoint = aProj.NbPoints();
46   if (aNbPoint > 0)
47   {
48     double aMinDistance = 0, aDistance;
49     for (Standard_Integer j = 1; j <= aNbPoint; j++)
50     {
51       gp_Pnt aNewPoint = aProj.Point(j);
52       aDistance = aNewPoint.Distance(aPoint);
53       if (!aMinDistance || aDistance < aMinDistance)
54       {
55         aMinDistance = aDistance;
56         aResult = boost::shared_ptr<GeomAPI_Pnt>(
57           new GeomAPI_Pnt(aNewPoint.X(), aNewPoint.Y(), aNewPoint.Z()));
58       }
59     }
60   }
61   return aResult;
62 }
63
64 const boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Circ::center() const
65 {
66   const gp_Pnt& aCenter = MY_CIRC->Location();
67   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
68 }
69
70 double GeomAPI_Circ::radius() const
71 {
72   return MY_CIRC->Radius();
73 }