Salome HOME
60cb7a4684332a7b69c257ce839d56a371893596
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Rotation.cpp
1 // Copyright (C) 2014-2023  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 <GeomAlgoAPI_Rotation.h>
21
22 #include <GeomAPI_Ax1.h>
23 #include <GeomAPI_Pnt.h>
24 #include <GeomAPI_XYZ.h>
25
26 #include <Precision.hxx>
27
28 // Verify points are applicable to build the rotation transformation
29 static bool checkPoints(GeomPointPtr theCenterPoint,
30                         GeomPointPtr theStartPoint,
31                         GeomPointPtr theEndPoint,
32                         std::string& theError);
33
34 //=================================================================================================
35 GeomAlgoAPI_Rotation::GeomAlgoAPI_Rotation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
36                                            std::shared_ptr<GeomAPI_Ax1>   theAxis,
37                                            double                         theAngle)
38 {
39   if (!theAxis) {
40     myError = "Rotation builder :: axis is not valid.";
41     return;
42   }
43
44   GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
45   aTrsf->setRotation(theAxis, theAngle);
46
47   build(theSourceShape, aTrsf);
48 }
49
50
51 //=================================================================================================
52 GeomAlgoAPI_Rotation::GeomAlgoAPI_Rotation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
53                                            std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
54                                            std::shared_ptr<GeomAPI_Pnt>   theStartPoint,
55                                            std::shared_ptr<GeomAPI_Pnt>   theEndPoint)
56 {
57   if (!checkPoints(theCenterPoint, theStartPoint, theEndPoint, myError))
58     return;
59
60   GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
61   aTrsf->setRotation(theCenterPoint, theStartPoint, theEndPoint);
62
63   build(theSourceShape, aTrsf);
64 }
65
66 //=================================================================================================
67 bool checkPoints(GeomPointPtr theCenterPoint,
68                  GeomPointPtr theStartPoint,
69                  GeomPointPtr theEndPoint,
70                  std::string& theError)
71 {
72   if (!theCenterPoint) {
73     theError = "Rotation builder :: center point is not valid.";
74     return false;
75   }
76   if (!theStartPoint) {
77     theError = "Rotation builder :: start point is not valid.";
78     return false;
79   }
80   if (!theEndPoint) {
81     theError = "Rotation builder :: end point is not valid.";
82     return false;
83   }
84   if (theCenterPoint->distance(theStartPoint) < Precision::Confusion()) {
85     theError = "Rotation builder :: center point and start point coincide.";
86     return false;
87   }
88   if (theCenterPoint->distance(theEndPoint) < Precision::Confusion()) {
89     theError = "Rotation builder :: center point and end point coincide.";
90     return false;
91   }
92   if (theStartPoint->distance(theEndPoint) < Precision::Confusion()) {
93     theError = "Rotation builder :: start point and end point coincide.";
94     return false;
95   }
96   std::shared_ptr<GeomAPI_XYZ> aCenterPointXYZ = theCenterPoint->xyz();
97   std::shared_ptr<GeomAPI_XYZ> aStartPointXYZ = theStartPoint->xyz();
98   std::shared_ptr<GeomAPI_XYZ> aEndPointXYZ = theEndPoint->xyz();
99   std::shared_ptr<GeomAPI_XYZ> vectCenterPointStartPoint =
100       aStartPointXYZ->decreased(aCenterPointXYZ);
101   std::shared_ptr<GeomAPI_XYZ> vectCenterPointEndPoint =
102       aEndPointXYZ->decreased(aCenterPointXYZ);
103   std::shared_ptr<GeomAPI_XYZ> crossProduct =
104       vectCenterPointStartPoint->cross(vectCenterPointEndPoint);
105
106   if (crossProduct->squareModulus() < Precision::SquareConfusion()) {
107     theError = "Rotation builder :: center point, start point and end point are on a line.";
108     return false;
109   }
110   return true;
111 }