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