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