]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Angle2d.cpp
Salome HOME
Update SketchPlugin_Projection feature (issue #1459)
[modules/shaper.git] / src / GeomAPI / GeomAPI_Angle2d.cpp
1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Angle2d.cpp
4 // Created:     19 April 2016
5 // Author:      Artem ZHIDKOV
6
7 #include <GeomAPI_Angle2d.h>
8 #include <GeomAPI_Dir2d.h>
9 #include <GeomAPI_Lin2d.h>
10 #include <GeomAPI_Pnt2d.h>
11 #include <GeomAPI_XY.h>
12
13 #include <gp_Dir2d.hxx>
14 #include <gp_Pnt2d.hxx>
15 #include <gp_XY.hxx>
16
17 struct ThreePoints2d {
18   gp_Pnt2d myCenter;
19   gp_Pnt2d myFirst;
20   gp_Pnt2d mySecond;
21   bool myReversed[2];
22 };
23
24 #define MY_ANGLE implPtr<ThreePoints2d>()
25 #define PI 3.1415926535897932
26
27 static ThreePoints2d* newAngle(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
28                                const std::shared_ptr<GeomAPI_Pnt2d>& theFirst,
29                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecond)
30 {
31   ThreePoints2d* aResult = new ThreePoints2d;
32   aResult->myCenter = gp_Pnt2d(theCenter->x(), theCenter->y());
33   aResult->myFirst  = gp_Pnt2d(theFirst->x(), theFirst->y());
34   aResult->mySecond = gp_Pnt2d(theSecond->x(), theSecond->y());
35   aResult->myReversed[0] = aResult->myReversed[1] = false;
36   return aResult;
37 }
38
39 static ThreePoints2d* newAngle(const std::shared_ptr<GeomAPI_Pnt2d>& theStart1,
40                                const std::shared_ptr<GeomAPI_Pnt2d>& theEnd1,
41                                const std::shared_ptr<GeomAPI_Pnt2d>& theStart2,
42                                const std::shared_ptr<GeomAPI_Pnt2d>& theEnd2)
43 {
44   std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(theStart1, theEnd1));
45   std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(theStart2, theEnd2));
46   std::shared_ptr<GeomAPI_Pnt2d> aCenter = aLine1->intersect(aLine2);
47   bool isParallel = !aCenter;
48   if (isParallel)
49     aCenter = theStart1;
50   std::shared_ptr<GeomAPI_Pnt2d> aPoint1, aPoint2;
51   if (isParallel)
52     aPoint1 = aPoint2 = theEnd1;
53   else {
54     aPoint1 = theStart1->distance(aCenter) < theEnd1->distance(aCenter) ? theEnd1 : theStart1;
55     aPoint2 = theStart2->distance(aCenter) < theEnd2->distance(aCenter) ? theEnd2 : theStart2;
56   }
57   ThreePoints2d* anAngle = newAngle(aCenter, aPoint1, aPoint2);
58   anAngle->myReversed[0] = aPoint1 == theStart1;
59   anAngle->myReversed[1] = !isParallel && aPoint2 == theStart2;
60   return anAngle;
61 }
62
63 static ThreePoints2d* newAngle(const std::shared_ptr<GeomAPI_Lin2d>& theLine1, bool theReversed1,
64                                const std::shared_ptr<GeomAPI_Lin2d>& theLine2, bool theReversed2)
65 {
66   std::shared_ptr<GeomAPI_Pnt2d> aCenter = theLine1->intersect(theLine2);
67   if (!aCenter)
68     aCenter = theLine1->location();
69   double aCoeff = theReversed1 ? -1.0 : 1.0;
70   std::shared_ptr<GeomAPI_Pnt2d> aPoint1(new GeomAPI_Pnt2d(
71       aCenter->xy()->added(theLine1->direction()->xy()->multiplied(aCoeff))));
72   aCoeff = theReversed2 ? -1.0 : 1.0;
73   std::shared_ptr<GeomAPI_Pnt2d> aPoint2(new GeomAPI_Pnt2d(
74       aCenter->xy()->added(theLine2->direction()->xy()->multiplied(aCoeff))));
75   ThreePoints2d* anAngle = newAngle(aCenter, aPoint1, aPoint2);
76   anAngle->myReversed[0] = theReversed1;
77   anAngle->myReversed[1] = theReversed2;
78   return anAngle;
79 }
80
81
82
83 GeomAPI_Angle2d::GeomAPI_Angle2d(const std::shared_ptr<GeomAPI_Pnt2d>& theStartLine1,
84                                  const std::shared_ptr<GeomAPI_Pnt2d>& theEndLine1,
85                                  const std::shared_ptr<GeomAPI_Pnt2d>& theStartLine2,
86                                  const std::shared_ptr<GeomAPI_Pnt2d>& theEndLine2)
87     : GeomAPI_Interface(newAngle(theStartLine1, theEndLine1, theStartLine2, theEndLine2))
88 {
89 }
90
91 GeomAPI_Angle2d::GeomAPI_Angle2d(const std::shared_ptr<GeomAPI_Lin2d>& theLine1, bool theReversed1,
92                                  const std::shared_ptr<GeomAPI_Lin2d>& theLine2, bool theReversed2)
93     : GeomAPI_Interface(newAngle(theLine1, theReversed1, theLine2, theReversed2))
94 {
95 }
96
97 GeomAPI_Angle2d::GeomAPI_Angle2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
98                                  const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
99                                  const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2)
100     : GeomAPI_Interface(newAngle(theCenter, thePoint1, thePoint2))
101 {
102 }
103
104 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Angle2d::center()
105 {
106   gp_Pnt2d aPnt = MY_ANGLE->myCenter;
107   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
108 }
109
110 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Angle2d::firstPoint()
111 {
112   gp_Pnt2d aPnt = MY_ANGLE->myFirst;
113   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
114 }
115
116 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Angle2d::secondPoint()
117 {
118   gp_Pnt2d aPnt = MY_ANGLE->mySecond;
119   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
120 }
121
122 double GeomAPI_Angle2d::angleDegree()
123 {
124   return angleRadian() * 180.0 / PI;
125 }
126
127 double GeomAPI_Angle2d::angleRadian()
128 {
129   ThreePoints2d* anAngle = MY_ANGLE;
130   gp_Dir2d aDir1(anAngle->myFirst.XY() - anAngle->myCenter.XY());
131   gp_Dir2d aDir2(anAngle->mySecond.XY() - anAngle->myCenter.XY());
132   double aRes = aDir1.Angle(aDir2);
133   if (aRes < 0.0) aRes += 2 * PI;
134   return aRes;
135 }
136
137 bool GeomAPI_Angle2d::isReversed(int theIndex)
138 {
139   return MY_ANGLE->myReversed[theIndex & 0x1];
140 }