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