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