Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAPI / GeomAPI_Pln.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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_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 std::shared_ptr<GeomAPI_Pnt>
84   GeomAPI_Pln::intersect(const std::shared_ptr<GeomAPI_Lin>& theLine) const
85 {
86   std::shared_ptr<GeomAPI_XYZ> aLineDir = theLine->direction()->xyz();
87   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
88
89   std::shared_ptr<GeomAPI_XYZ> aNormal = direction()->xyz();
90   std::shared_ptr<GeomAPI_XYZ> aLocation = location()->xyz();
91
92   double aDot = aNormal->dot(aLineDir);
93   if (Abs(aDot) < Precision::SquareConfusion())
94     return std::shared_ptr<GeomAPI_Pnt>();
95
96   double aParam = aNormal->dot(aLocation->decreased(aLineLoc)) / aDot;
97   return std::shared_ptr<GeomAPI_Pnt>(
98     new GeomAPI_Pnt(aLineLoc->added(aLineDir->multiplied(aParam))));
99 }
100
101 std::shared_ptr<GeomAPI_Pnt>
102   GeomAPI_Pln::project(const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
103 {
104   std::shared_ptr<GeomAPI_XYZ> aNormal = direction()->xyz();
105   std::shared_ptr<GeomAPI_XYZ> aLocation = location()->xyz();
106
107   std::shared_ptr<GeomAPI_XYZ> aVec = thePoint->xyz()->decreased(aLocation);
108   double aDot = aNormal->dot(aVec);
109   std::shared_ptr<GeomAPI_XYZ> aProjection =
110       aLocation->added(aVec->decreased(aNormal->multiplied(aDot)));
111   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aProjection));
112 }
113
114 double GeomAPI_Pln::distance(const std::shared_ptr<GeomAPI_Pln> thePlane) const
115 {
116   const gp_Pln& aMyPln = impl<gp_Pln>();
117   const gp_Pln& anOtherPln = thePlane->impl<gp_Pln>();
118
119   return aMyPln.Distance(anOtherPln);
120 }
121
122 double GeomAPI_Pln::distance(const std::shared_ptr<GeomAPI_Pnt> thePoint) const
123 {
124   const gp_Pln& aMyPln = impl<gp_Pln>();
125   const gp_Pnt& aPnt = thePoint->impl<gp_Pnt>();
126   return aMyPln.Distance(aPnt);
127 }
128
129 void GeomAPI_Pln::translate(const std::shared_ptr<GeomAPI_Dir> theDir, double theDist)
130 {
131   gp_Vec aVec(theDir->impl<gp_Dir>());
132   aVec.Normalize();
133   aVec.Multiply(theDist);
134   implPtr<gp_Pln>()->Translate(aVec);
135 }
136
137 std::shared_ptr<GeomAPI_Lin>
138   GeomAPI_Pln::intersect(const std::shared_ptr<GeomAPI_Pln> thePlane) const
139 {
140   std::shared_ptr<GeomAPI_Lin> aRes;
141
142   if(!thePlane.get()) {
143     return aRes;
144   }
145
146   const gp_Pln& aMyPln = impl<gp_Pln>();
147   const gp_Pln& anOtherPln = thePlane->impl<gp_Pln>();
148
149   IntAna_QuadQuadGeo aQuad(aMyPln, anOtherPln, Precision::Confusion(), Precision::Confusion());
150
151   if(aQuad.IsDone() != Standard_True) {
152     return aRes;
153   }
154
155   if(aQuad.NbSolutions() != 1) {
156     return aRes;
157   }
158
159   gp_Lin aLin = aQuad.Line(1);
160   gp_Pnt aLoc = aLin.Location();
161   gp_Dir aDir = aLin.Direction();
162
163   std::shared_ptr<GeomAPI_Pnt> aGeomLoc(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
164   std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
165
166   aRes.reset(new GeomAPI_Lin(aGeomLoc, aGeomDir));
167
168   return aRes;
169 }