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