Salome HOME
e72462f4e087f8fdf3ce518d332b458bc5befee7
[modules/shaper.git] / src / GeomAPI / GeomAPI_Pnt.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<GeomAPI_Pnt.h>
21 #include <GeomAPI_Ax1.h>
22 #include<GeomAPI_XYZ.h>
23 #include<GeomAPI_Pnt2d.h>
24 #include<GeomAPI_Dir.h>
25 #include<GeomAPI_Pln.h>
26
27 #include<gp_Pnt.hxx>
28 #include<gp_Pln.hxx>
29 #include<ProjLib.hxx>
30
31 #define MY_PNT implPtr<gp_Pnt>()
32
33 GeomAPI_Pnt::GeomAPI_Pnt(const double theX, const double theY, const double theZ)
34     : GeomAPI_Interface(new gp_Pnt(theX, theY, theZ))
35 {
36 }
37
38 GeomAPI_Pnt::GeomAPI_Pnt(const std::shared_ptr<GeomAPI_XYZ>& theCoords)
39     : GeomAPI_Interface(new gp_Pnt(theCoords->x(), theCoords->y(), theCoords->z()))
40 {
41 }
42
43 double GeomAPI_Pnt::x() const
44 {
45   return MY_PNT->X();
46 }
47
48 double GeomAPI_Pnt::y() const
49 {
50   return MY_PNT->Y();
51 }
52
53 double GeomAPI_Pnt::z() const
54 {
55   return MY_PNT->Z();
56 }
57
58 void GeomAPI_Pnt::setX(const double theX)
59 {
60   return MY_PNT->SetX(theX);
61 }
62
63 void GeomAPI_Pnt::setY(const double theY)
64 {
65   return MY_PNT->SetY(theY);
66 }
67
68 void GeomAPI_Pnt::setZ(const double theZ)
69 {
70   return MY_PNT->SetZ(theZ);
71 }
72
73 const std::shared_ptr<GeomAPI_XYZ> GeomAPI_Pnt::xyz()
74 {
75   return std::shared_ptr<GeomAPI_XYZ>(new GeomAPI_XYZ(MY_PNT->X(), MY_PNT->Y(), MY_PNT->Z()));
76 }
77
78 double GeomAPI_Pnt::distance(const std::shared_ptr<GeomAPI_Pnt>& theOther) const
79 {
80   return MY_PNT->Distance(theOther->impl<gp_Pnt>());
81 }
82
83 bool GeomAPI_Pnt::isEqual(const std::shared_ptr<GeomAPI_Pnt>& theOther) const
84 {
85   return distance(theOther) < Precision::Confusion();
86 }
87
88 bool GeomAPI_Pnt::isLess(const std::shared_ptr<GeomAPI_Pnt>& theOther,
89                          const double theTolerance) const
90 {
91   if (MY_PNT->X() + theTolerance < theOther->x())
92     return true;
93   else if (MY_PNT->X() < theOther->x() + theTolerance) {
94     if (MY_PNT->Y() + theTolerance < theOther->y())
95       return true;
96     else if (MY_PNT->Y() < theOther->y() + theTolerance &&
97              MY_PNT->Z() + theTolerance < theOther->z())
98       return true;
99   }
100   return false;
101 }
102
103 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Pnt::to2D(const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
104   const std::shared_ptr<GeomAPI_Dir>& theDirX, const std::shared_ptr<GeomAPI_Dir>& theDirY)
105 {
106   gp_Pnt anOriginPnt(theOrigin->x(), theOrigin->y(), theOrigin->z());
107   gp_Vec aVec(anOriginPnt, impl<gp_Pnt>());
108
109   double aX = aVec.X() * theDirX->x() + aVec.Y() * theDirX->y() + aVec.Z() * theDirX->z();
110   double aY = aVec.X() * theDirY->x() + aVec.Y() * theDirY->y() + aVec.Z() * theDirY->z();
111   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
112 }
113
114
115 void GeomAPI_Pnt::translate(const std::shared_ptr<GeomAPI_Dir>& theDir, double theDist)
116 {
117   gp_Vec aVec(theDir->impl<gp_Dir>());
118   aVec.Normalize();
119   aVec.Multiply(theDist);
120   MY_PNT->Translate(aVec);
121 }
122
123 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Pnt::to2D(const std::shared_ptr<GeomAPI_Pln>& thePln) const
124 {
125   double aA, aB, aC, aD;
126   thePln->coefficients(aA, aB, aC, aD);
127   gp_Pln aPln(aA, aB, aC, aD);
128
129   gp_Pnt2d aRes = ProjLib::Project(aPln, *MY_PNT);
130   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aRes.X(), aRes.Y()));
131 }
132
133 void GeomAPI_Pnt::rotate(const std::shared_ptr<GeomAPI_Ax1>& theAxis, const double theAngle)
134 {
135   MY_PNT->Rotate(theAxis->impl<gp_Ax1>(), theAngle / 180.0 * M_PI);
136 }