Salome HOME
Merge branch 'master' of newgeom:newgeom.git into BR_PYTHON_PLUGIN
[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, const gp_Dir& theDir, const double theRadius)
20 {
21   return new gp_Circ(gp_Ax2(theCenter, theDir), theRadius);
22 }
23
24 GeomAPI_Circ::GeomAPI_Circ(const std::shared_ptr<GeomAPI_Pnt>& theCenter,
25                            const std::shared_ptr<GeomAPI_Dir>& theDir, double theRadius)
26     : GeomAPI_Interface(newCirc(theCenter->impl<gp_Pnt>(), theDir->impl<gp_Dir>(), theRadius))
27 {
28 }
29
30 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Circ::project(
31     const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
32 {
33   std::shared_ptr<GeomAPI_Pnt> aResult;
34   if (!MY_CIRC)
35   return aResult;
36
37   Handle(Geom_Circle) aCircle = new Geom_Circle(*MY_CIRC);
38
39   const gp_Pnt& aPoint = thePoint->impl<gp_Pnt>();
40
41   GeomAPI_ProjectPointOnCurve aProj(aPoint, aCircle);
42   Standard_Integer aNbPoint = aProj.NbPoints();
43   if (aNbPoint > 0) {
44     double aMinDistance = 0, aDistance;
45     for (Standard_Integer j = 1; j <= aNbPoint; j++) {
46       gp_Pnt aNewPoint = aProj.Point(j);
47       aDistance = aNewPoint.Distance(aPoint);
48       if (!aMinDistance || aDistance < aMinDistance) {
49         aMinDistance = aDistance;
50         aResult = std::shared_ptr<GeomAPI_Pnt>(
51             new GeomAPI_Pnt(aNewPoint.X(), aNewPoint.Y(), aNewPoint.Z()));
52       }
53     }
54   }
55   return aResult;
56 }
57
58 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Circ::center() const
59 {
60   const gp_Pnt& aCenter = MY_CIRC->Location();
61   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
62 }
63
64 double GeomAPI_Circ::radius() const
65 {
66   return MY_CIRC->Radius();
67 }