Salome HOME
Improve angle measurement
[modules/shaper.git] / src / GeomAPI / GeomAPI_Angle.cpp
1 // Copyright (C) 2018-20xx  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_Angle.h>
22
23 #include <GeomAPI_Edge.h>
24 #include <GeomAPI_Pnt.h>
25
26 #include <BRep_Tool.hxx>
27 #include <ElCLib.hxx>
28 #include <Geom_Curve.hxx>
29 #include <GeomAPI_ProjectPointOnCurve.hxx>
30 #include <Precision.hxx>
31 #include <TopoDS_Edge.hxx>
32
33 /// \struct AngleDirections
34 /// \brief Used to store info about angle.
35 struct AngleDirections {
36   gp_Vec myDir1;
37   gp_Vec myDir2;
38 };
39
40 #define MY_ANGLE implPtr<AngleDirections>()
41 #define PI 3.1415926535897932
42
43
44 GeomAPI_Angle::GeomAPI_Angle(const std::shared_ptr<GeomAPI_Edge>& theEdge1,
45                              const std::shared_ptr<GeomAPI_Edge>& theEdge2,
46                              const std::shared_ptr<GeomAPI_Pnt>&  thePoint)
47 {
48   gp_Pnt aPoint = thePoint->impl<gp_Pnt>();
49   const TopoDS_Edge& anEdge1 = theEdge1->impl<TopoDS_Edge>();
50   const TopoDS_Edge& anEdge2 = theEdge2->impl<TopoDS_Edge>();
51
52   double aF1, aL1;
53   Handle(Geom_Curve) aCurve1 = BRep_Tool::Curve(anEdge1, aF1, aL1);
54   double aF2, aL2;
55   Handle(Geom_Curve) aCurve2 = BRep_Tool::Curve(anEdge2, aF2, aL2);
56
57   AngleDirections* anAngle = new AngleDirections;
58   gp_Pnt aP;
59
60   GeomAPI_ProjectPointOnCurve aProj1(aPoint, aCurve1);
61   if (aProj1.NbPoints() > 0)
62     aCurve1->D1(aProj1.LowerDistanceParameter(), aP, anAngle->myDir1);
63
64   GeomAPI_ProjectPointOnCurve aProj2(aPoint, aCurve2);
65   if (aProj2.NbPoints() > 0)
66     aCurve2->D1(aProj2.LowerDistanceParameter(), aP, anAngle->myDir2);
67
68   setImpl(anAngle);
69 }
70
71 double GeomAPI_Angle::angleDegree()
72 {
73   return angleRadian() * 180.0 / PI;
74 }
75
76 double GeomAPI_Angle::angleRadian()
77 {
78   AngleDirections* anAngle = MY_ANGLE;
79   if (anAngle->myDir1.SquareMagnitude() < Precision::SquareConfusion() ||
80       anAngle->myDir2.SquareMagnitude() < Precision::SquareConfusion())
81     return 0.0;
82
83   gp_Dir aDir1(anAngle->myDir1);
84   gp_Dir aDir2(anAngle->myDir2);
85   double aRes = aDir1.Angle(aDir2);
86   aRes = ElCLib::InPeriod(aRes, 0.0, 2.0 * PI);
87   if (Abs(aRes) < 1.e-12)
88     aRes = 0.0;
89   return aRes;
90 }