1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D
3 // File: GeomAPI_Angle.cpp
4 // Created: 19 April 2016
5 // Author: Artem ZHIDKOV
7 #include <GeomAPI_Angle.h>
8 #include <GeomAPI_Dir.h>
9 #include <GeomAPI_Lin.h>
10 #include <GeomAPI_Pnt.h>
11 #include <GeomAPI_XYZ.h>
24 #define MY_ANGLE implPtr<ThreePoints>()
25 #define PI 3.1415926535897932
27 static ThreePoints* newAngle(const std::shared_ptr<GeomAPI_Pnt>& theCenter,
28 const std::shared_ptr<GeomAPI_Pnt>& theFirst,
29 const std::shared_ptr<GeomAPI_Pnt>& theSecond)
31 ThreePoints* aResult = new ThreePoints;
32 aResult->myCenter = gp_Pnt(theCenter->x(), theCenter->y(), theCenter->z());
33 aResult->myFirst = gp_Pnt(theFirst->x(), theFirst->y(), theFirst->z());
34 aResult->mySecond = gp_Pnt(theSecond->x(), theSecond->y(), theSecond->z());
35 aResult->myReversed[0] = aResult->myReversed[1] = false;
39 static ThreePoints* newAngle(const std::shared_ptr<GeomAPI_Pnt>& theStart1,
40 const std::shared_ptr<GeomAPI_Pnt>& theEnd1,
41 const std::shared_ptr<GeomAPI_Pnt>& theStart2,
42 const std::shared_ptr<GeomAPI_Pnt>& theEnd2)
44 std::shared_ptr<GeomAPI_Lin> aLine1(new GeomAPI_Lin(theStart1, theEnd1));
45 std::shared_ptr<GeomAPI_Lin> aLine2(new GeomAPI_Lin(theStart2, theEnd2));
46 std::shared_ptr<GeomAPI_Pnt> aCenter = aLine1->intersect(aLine2);
47 bool isParallel = !aCenter;
50 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
52 aPoint1 = aPoint2 = theEnd1;
54 aPoint1 = theStart1->distance(aCenter) < theEnd1->distance(aCenter) ? theEnd1 : theStart1;
55 aPoint2 = theStart2->distance(aCenter) < theEnd2->distance(aCenter) ? theEnd2 : theStart2;
57 ThreePoints* anAngle = newAngle(aCenter, aPoint1, aPoint2);
58 anAngle->myReversed[0] = aPoint1 == theStart1;
59 anAngle->myReversed[1] = !isParallel && aPoint2 == theStart2;
63 static ThreePoints* newAngle(const std::shared_ptr<GeomAPI_Lin>& theLine1, bool theReversed1,
64 const std::shared_ptr<GeomAPI_Lin>& theLine2, bool theReversed2)
66 std::shared_ptr<GeomAPI_Pnt> aCenter = theLine1->intersect(theLine2);
68 aCenter = theLine1->location();
69 double aCoeff = theReversed1 ? -1.0 : 1.0;
70 std::shared_ptr<GeomAPI_Pnt> aPoint1(new GeomAPI_Pnt(
71 aCenter->xyz()->added(theLine1->direction()->xyz()->multiplied(aCoeff))));
72 aCoeff = theReversed2 ? -1.0 : 1.0;
73 std::shared_ptr<GeomAPI_Pnt> aPoint2(new GeomAPI_Pnt(
74 aCenter->xyz()->added(theLine2->direction()->xyz()->multiplied(aCoeff))));
75 ThreePoints* anAngle = newAngle(aCenter, aPoint1, aPoint2);
76 anAngle->myReversed[0] = theReversed1;
77 anAngle->myReversed[1] = theReversed2;
83 GeomAPI_Angle::GeomAPI_Angle(const std::shared_ptr<GeomAPI_Pnt>& theStartLine1,
84 const std::shared_ptr<GeomAPI_Pnt>& theEndLine1,
85 const std::shared_ptr<GeomAPI_Pnt>& theStartLine2,
86 const std::shared_ptr<GeomAPI_Pnt>& theEndLine2)
87 : GeomAPI_Interface(newAngle(theStartLine1, theEndLine1, theStartLine2, theEndLine2))
91 GeomAPI_Angle::GeomAPI_Angle(const std::shared_ptr<GeomAPI_Lin>& theLine1, bool theReversed1,
92 const std::shared_ptr<GeomAPI_Lin>& theLine2, bool theReversed2)
93 : GeomAPI_Interface(newAngle(theLine1, theReversed1, theLine2, theReversed2))
97 GeomAPI_Angle::GeomAPI_Angle(const std::shared_ptr<GeomAPI_Pnt>& theCenter,
98 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
99 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
100 : GeomAPI_Interface(newAngle(theCenter, thePoint1, thePoint2))
104 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Angle::center()
106 gp_Pnt aPnt = MY_ANGLE->myCenter;
107 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
110 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Angle::firstPoint()
112 gp_Pnt aPnt = MY_ANGLE->myFirst;
113 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
116 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Angle::secondPoint()
118 gp_Pnt aPnt = MY_ANGLE->mySecond;
119 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
122 double GeomAPI_Angle::angleDegree()
124 return angleRadian() * 180.0 / PI;
127 double GeomAPI_Angle::angleRadian()
129 ThreePoints* anAngle = MY_ANGLE;
130 gp_Dir aDir1(anAngle->myFirst.XYZ() - anAngle->myCenter.XYZ());
131 gp_Dir aDir2(anAngle->mySecond.XYZ() - anAngle->myCenter.XYZ());
132 return aDir1.Angle(aDir2);
135 bool GeomAPI_Angle::isReversed(int theIndex)
137 return MY_ANGLE->myReversed[theIndex & 0x1];