Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / GeomAPI / GeomAPI_Circ.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAPI_Circ.h>
21
22 #include <GeomAPI_Ax2.h>
23 #include <GeomAPI_Pnt.h>
24 #include <GeomAPI_Dir.h>
25
26 #include <gp_Dir.hxx>
27 #include <gp_Circ.hxx>
28 #include <gp_Pnt.hxx>
29 #include <gp_Ax2.hxx>
30
31 #include <Geom_Circle.hxx>
32 #include <GeomAPI_ProjectPointOnCurve.hxx>
33 #include <GeomLib_Tool.hxx>
34
35 #define MY_CIRC implPtr<gp_Circ>()
36
37 static gp_Circ* newCirc(const gp_Pnt& theCenter, const gp_Dir& theDir, const double theRadius)
38 {
39   return new gp_Circ(gp_Ax2(theCenter, theDir), theRadius);
40 }
41
42 //=================================================================================================
43 GeomAPI_Circ::GeomAPI_Circ(const std::shared_ptr<GeomAPI_Ax2> theAx2,
44                            const double theRadius)
45 : GeomAPI_Interface(new gp_Circ(theAx2->impl<gp_Ax2>(), theRadius))
46 {
47
48 }
49
50
51 //=================================================================================================
52 GeomAPI_Circ::GeomAPI_Circ(const std::shared_ptr<GeomAPI_Pnt>& theCenter,
53                            const std::shared_ptr<GeomAPI_Dir>& theDir, double theRadius)
54     : GeomAPI_Interface(newCirc(theCenter->impl<gp_Pnt>(), theDir->impl<gp_Dir>(), theRadius))
55 {
56 }
57
58 //=================================================================================================
59 GeomAPI_Circ::GeomAPI_Circ(const GeomCurvePtr& theCurve)
60 {
61   Handle(Geom_Curve) aCurve = theCurve->impl<Handle(Geom_Curve)>();
62   Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
63   if (aCirc.IsNull())
64     throw Standard_ConstructionError("GeomAPI_Circ: Curve is not a circle");
65   setImpl(new gp_Circ(aCirc->Circ()));
66 }
67
68 //=================================================================================================
69 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Circ::center() const
70 {
71   const gp_Pnt& aCenter = MY_CIRC->Location();
72   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
73 }
74
75 //=================================================================================================
76 double GeomAPI_Circ::radius() const
77 {
78   return MY_CIRC->Radius();
79 }
80
81 //=================================================================================================
82 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Circ::project(
83     const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
84 {
85   std::shared_ptr<GeomAPI_Pnt> aResult;
86   if (!MY_CIRC)
87   return aResult;
88
89   Handle(Geom_Circle) aCircle = new Geom_Circle(*MY_CIRC);
90
91   const gp_Pnt& aPoint = thePoint->impl<gp_Pnt>();
92
93   GeomAPI_ProjectPointOnCurve aProj(aPoint, aCircle);
94   Standard_Integer aNbPoint = aProj.NbPoints();
95   if (aNbPoint > 0) {
96     double aMinDistance = Precision::Infinite(), aDistance;
97     for (Standard_Integer j = 1; j <= aNbPoint; j++) {
98       gp_Pnt aNewPoint = aProj.Point(j);
99       aDistance = aNewPoint.Distance(aPoint);
100       if (aDistance < aMinDistance) {
101         aMinDistance = aDistance;
102         aResult = std::shared_ptr<GeomAPI_Pnt>(
103             new GeomAPI_Pnt(aNewPoint.X(), aNewPoint.Y(), aNewPoint.Z()));
104       }
105     }
106   }
107   return aResult;
108 }
109
110 //=================================================================================================
111 const bool GeomAPI_Circ::parameter(const std::shared_ptr<GeomAPI_Pnt> thePoint,
112                                    const double theTolerance,
113                                    double& theParameter) const
114 {
115   Handle(Geom_Circle) aCurve = new Geom_Circle(*MY_CIRC);
116   return GeomLib_Tool::Parameter(aCurve, thePoint->impl<gp_Pnt>(),
117                                  theTolerance, theParameter) == Standard_True;
118 }
119
120 //=================================================================================================
121 std::shared_ptr<GeomAPI_Dir> GeomAPI_Circ::normal() const
122 {
123   const gp_Ax1& anAxis = MY_CIRC->Axis();
124   const gp_Dir& aDir = anAxis.Direction();
125   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
126 }