Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomAPI / GeomAPI_Circ.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GeomAPI_Circ.h>
22
23 #include <GeomAPI_Ax2.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_Dir.h>
26
27 #include <gp_Dir.hxx>
28 #include <gp_Circ.hxx>
29 #include <gp_Pnt.hxx>
30 #include <gp_Ax2.hxx>
31
32 #include <Geom_Circle.hxx>
33 #include <GeomAPI_ProjectPointOnCurve.hxx>
34 #include <GeomLib_Tool.hxx>
35
36 #define MY_CIRC implPtr<gp_Circ>()
37
38 static gp_Circ* newCirc(const gp_Pnt& theCenter, const gp_Dir& theDir, const double theRadius)
39 {
40   return new gp_Circ(gp_Ax2(theCenter, theDir), theRadius);
41 }
42
43 //=================================================================================================
44 GeomAPI_Circ::GeomAPI_Circ(const std::shared_ptr<GeomAPI_Ax2> theAx2,
45                            const double theRadius)
46 : GeomAPI_Interface(new gp_Circ(theAx2->impl<gp_Ax2>(), theRadius))
47 {
48
49 }
50
51
52 //=================================================================================================
53 GeomAPI_Circ::GeomAPI_Circ(const std::shared_ptr<GeomAPI_Pnt>& theCenter,
54                            const std::shared_ptr<GeomAPI_Dir>& theDir, double theRadius)
55     : GeomAPI_Interface(newCirc(theCenter->impl<gp_Pnt>(), theDir->impl<gp_Dir>(), theRadius))
56 {
57 }
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 //=================================================================================================
67 double GeomAPI_Circ::radius() const
68 {
69   return MY_CIRC->Radius();
70 }
71
72 //=================================================================================================
73 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Circ::project(
74     const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
75 {
76   std::shared_ptr<GeomAPI_Pnt> aResult;
77   if (!MY_CIRC)
78   return aResult;
79
80   Handle(Geom_Circle) aCircle = new Geom_Circle(*MY_CIRC);
81
82   const gp_Pnt& aPoint = thePoint->impl<gp_Pnt>();
83
84   GeomAPI_ProjectPointOnCurve aProj(aPoint, aCircle);
85   Standard_Integer aNbPoint = aProj.NbPoints();
86   if (aNbPoint > 0) {
87     double aMinDistance = Precision::Infinite(), aDistance;
88     for (Standard_Integer j = 1; j <= aNbPoint; j++) {
89       gp_Pnt aNewPoint = aProj.Point(j);
90       aDistance = aNewPoint.Distance(aPoint);
91       if (aDistance < aMinDistance) {
92         aMinDistance = aDistance;
93         aResult = std::shared_ptr<GeomAPI_Pnt>(
94             new GeomAPI_Pnt(aNewPoint.X(), aNewPoint.Y(), aNewPoint.Z()));
95       }
96     }
97   }
98   return aResult;
99 }
100
101 //=================================================================================================
102 const bool GeomAPI_Circ::parameter(const std::shared_ptr<GeomAPI_Pnt> thePoint,
103                                    const double theTolerance,
104                                    double& theParameter) const
105 {
106   Handle(Geom_Circle) aCurve = new Geom_Circle(*MY_CIRC);
107   return GeomLib_Tool::Parameter(aCurve, thePoint->impl<gp_Pnt>(),
108                                  theTolerance, theParameter) == Standard_True;
109 }
110
111 //=================================================================================================
112 std::shared_ptr<GeomAPI_Dir> GeomAPI_Circ::normal() const
113 {
114   const gp_Ax1& anAxis = MY_CIRC->Axis();
115   const gp_Dir& aDir = anAxis.Direction();
116   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
117 }