Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[modules/shaper.git] / src / GeomAPI / GeomAPI_Pln.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GeomAPI_Pln.h>
22 #include <GeomAPI_Ax3.h>
23 #include <GeomAPI_Pnt.h>
24 #include <GeomAPI_Dir.h>
25 #include <GeomAPI_Lin.h>
26 #include <GeomAPI_XYZ.h>
27
28 #include <gp_Pln.hxx>
29
30 #include <IntAna_QuadQuadGeo.hxx>
31
32 GeomAPI_Pln::GeomAPI_Pln(const std::shared_ptr<GeomAPI_Ax3>& theAxis)
33 : GeomAPI_Interface(new gp_Ax3(theAxis->impl<gp_Ax3>()))
34 {
35 }
36
37 GeomAPI_Pln::GeomAPI_Pln(const std::shared_ptr<GeomAPI_Pnt>& thePoint,
38                          const std::shared_ptr<GeomAPI_Dir>& theNormal)
39     : GeomAPI_Interface(new gp_Pln(thePoint->impl<gp_Pnt>(), theNormal->impl<gp_Dir>()))
40 {
41 }
42
43 GeomAPI_Pln::GeomAPI_Pln(const double theA, const double theB, const double theC, const double theD)
44     : GeomAPI_Interface(new gp_Pln(theA, theB, theC, theD))
45 {
46 }
47
48 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Pln::location() const
49 {
50   gp_Pnt aLoc = impl<gp_Pln>().Location();
51   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
52 }
53
54 std::shared_ptr<GeomAPI_Dir> GeomAPI_Pln::direction() const
55 {
56   const gp_Dir& aDir = impl<gp_Pln>().Axis().Direction();
57   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
58 }
59
60 std::shared_ptr<GeomAPI_Dir> GeomAPI_Pln::xDirection() const
61 {
62   const gp_Dir& aDir = impl<gp_Pln>().XAxis().Direction();
63   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
64 }
65
66 void GeomAPI_Pln::coefficients(double& theA, double& theB, double& theC, double& theD)
67 {
68   impl<gp_Pln>().Coefficients(theA, theB, theC, theD);
69 }
70
71 bool GeomAPI_Pln::isCoincident(const std::shared_ptr<GeomAPI_Pln> thePlane,
72                                const double theTolerance)
73 {
74   if(!thePlane.get()) {
75     return false;
76   }
77
78   const gp_Pln& aMyPln = impl<gp_Pln>();
79   const gp_Pln& anOtherPln = thePlane->impl<gp_Pln>();
80   return (aMyPln.Contains(anOtherPln.Location(), theTolerance) &&
81     aMyPln.Axis().IsParallel(anOtherPln.Axis(), theTolerance));
82 }
83
84 std::shared_ptr<GeomAPI_Pnt>
85   GeomAPI_Pln::intersect(const std::shared_ptr<GeomAPI_Lin>& theLine) const
86 {
87   std::shared_ptr<GeomAPI_XYZ> aLineDir = theLine->direction()->xyz();
88   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
89
90   std::shared_ptr<GeomAPI_XYZ> aNormal = direction()->xyz();
91   std::shared_ptr<GeomAPI_XYZ> aLocation = location()->xyz();
92
93   double aDot = aNormal->dot(aLineDir);
94   if (Abs(aDot) < Precision::SquareConfusion())
95     return std::shared_ptr<GeomAPI_Pnt>();
96
97   double aParam = aNormal->dot(aLocation->decreased(aLineLoc)) / aDot;
98   return std::shared_ptr<GeomAPI_Pnt>(
99     new GeomAPI_Pnt(aLineLoc->added(aLineDir->multiplied(aParam))));
100 }
101
102 std::shared_ptr<GeomAPI_Pnt>
103   GeomAPI_Pln::project(const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
104 {
105   std::shared_ptr<GeomAPI_XYZ> aNormal = direction()->xyz();
106   std::shared_ptr<GeomAPI_XYZ> aLocation = location()->xyz();
107
108   std::shared_ptr<GeomAPI_XYZ> aVec = thePoint->xyz()->decreased(aLocation);
109   double aDot = aNormal->dot(aVec);
110   std::shared_ptr<GeomAPI_XYZ> aProjection =
111       aLocation->added(aVec->decreased(aNormal->multiplied(aDot)));
112   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aProjection));
113 }
114
115 double GeomAPI_Pln::distance(const std::shared_ptr<GeomAPI_Pln> thePlane) const
116 {
117   const gp_Pln& aMyPln = impl<gp_Pln>();
118   const gp_Pln& anOtherPln = thePlane->impl<gp_Pln>();
119
120   return aMyPln.Distance(anOtherPln);
121 }
122
123 double GeomAPI_Pln::distance(const std::shared_ptr<GeomAPI_Pnt> thePoint) const
124 {
125   const gp_Pln& aMyPln = impl<gp_Pln>();
126   const gp_Pnt& aPnt = thePoint->impl<gp_Pnt>();
127   return aMyPln.Distance(aPnt);
128 }
129
130 void GeomAPI_Pln::translate(const std::shared_ptr<GeomAPI_Dir> theDir, double theDist)
131 {
132   gp_Vec aVec(theDir->impl<gp_Dir>());
133   aVec.Normalize();
134   aVec.Multiply(theDist);
135   implPtr<gp_Pln>()->Translate(aVec);
136 }
137
138 std::shared_ptr<GeomAPI_Lin>
139   GeomAPI_Pln::intersect(const std::shared_ptr<GeomAPI_Pln> thePlane) const
140 {
141   std::shared_ptr<GeomAPI_Lin> aRes;
142
143   if(!thePlane.get()) {
144     return aRes;
145   }
146
147   const gp_Pln& aMyPln = impl<gp_Pln>();
148   const gp_Pln& anOtherPln = thePlane->impl<gp_Pln>();
149
150   IntAna_QuadQuadGeo aQuad(aMyPln, anOtherPln, Precision::Confusion(), Precision::Confusion());
151
152   if(aQuad.IsDone() != Standard_True) {
153     return aRes;
154   }
155
156   if(aQuad.NbSolutions() != 1) {
157     return aRes;
158   }
159
160   gp_Lin aLin = aQuad.Line(1);
161   gp_Pnt aLoc = aLin.Location();
162   gp_Dir aDir = aLin.Direction();
163
164   std::shared_ptr<GeomAPI_Pnt> aGeomLoc(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
165   std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
166
167   aRes.reset(new GeomAPI_Lin(aGeomLoc, aGeomDir));
168
169   return aRes;
170 }