Salome HOME
Issue #1860: fix end lines with spaces
[modules/shaper.git] / src / GeomAPI / GeomAPI_Pln.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Pln.cpp
4 // Created:     23 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <GeomAPI_Pln.h>
8 #include <GeomAPI_Ax3.h>
9 #include <GeomAPI_Pnt.h>
10 #include <GeomAPI_Dir.h>
11 #include <GeomAPI_Lin.h>
12 #include <GeomAPI_XYZ.h>
13
14 #include <gp_Pln.hxx>
15
16 #include <IntAna_QuadQuadGeo.hxx>
17
18 using namespace std;
19
20 GeomAPI_Pln::GeomAPI_Pln(const std::shared_ptr<GeomAPI_Ax3>& theAxis)
21 : GeomAPI_Interface(new gp_Ax3(theAxis->impl<gp_Ax3>()))
22 {
23 }
24
25 GeomAPI_Pln::GeomAPI_Pln(const std::shared_ptr<GeomAPI_Pnt>& thePoint,
26                          const std::shared_ptr<GeomAPI_Dir>& theNormal)
27     : GeomAPI_Interface(new gp_Pln(thePoint->impl<gp_Pnt>(), theNormal->impl<gp_Dir>()))
28 {
29 }
30
31 GeomAPI_Pln::GeomAPI_Pln(const double theA, const double theB, const double theC, const double theD)
32     : GeomAPI_Interface(new gp_Pln(theA, theB, theC, theD))
33 {
34 }
35
36 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Pln::location() const
37 {
38   gp_Pnt aLoc = impl<gp_Pln>().Location();
39   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
40 }
41
42 std::shared_ptr<GeomAPI_Dir> GeomAPI_Pln::direction() const
43 {
44   const gp_Dir& aDir = impl<gp_Pln>().Axis().Direction();
45   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
46 }
47
48 std::shared_ptr<GeomAPI_Dir> GeomAPI_Pln::xDirection() const
49 {
50   const gp_Dir& aDir = impl<gp_Pln>().XAxis().Direction();
51   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
52 }
53
54 void GeomAPI_Pln::coefficients(double& theA, double& theB, double& theC, double& theD)
55 {
56   impl<gp_Pln>().Coefficients(theA, theB, theC, theD);
57 }
58
59 bool GeomAPI_Pln::isCoincident(const std::shared_ptr<GeomAPI_Pln> thePlane,
60                                const double theTolerance)
61 {
62   if(!thePlane.get()) {
63     return false;
64   }
65
66   const gp_Pln& aMyPln = impl<gp_Pln>();
67   const gp_Pln& anOtherPln = thePlane->impl<gp_Pln>();
68   return (aMyPln.Contains(anOtherPln.Location(), theTolerance) &&
69     aMyPln.Axis().IsParallel(anOtherPln.Axis(), theTolerance));
70 }
71
72 bool GeomAPI_Pln::isParallel(const std::shared_ptr<GeomAPI_Lin> theLine)
73 {
74   std::shared_ptr<GeomAPI_XYZ> aLineDir = theLine->direction()->xyz();
75   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
76
77   std::shared_ptr<GeomAPI_XYZ> aNormal = direction()->xyz();
78   std::shared_ptr<GeomAPI_XYZ> aLocation = location()->xyz();
79
80   double aDot = aNormal->dot(aLineDir);
81   return Abs(aDot) < Precision::SquareConfusion();
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 void GeomAPI_Pln::translate(const std::shared_ptr<GeomAPI_Dir> theDir, double theDist)
124 {
125   gp_Vec aVec(theDir->impl<gp_Dir>());
126   aVec.Normalize();
127   aVec.Multiply(theDist);
128   implPtr<gp_Pln>()->Translate(aVec);
129 }
130
131 std::shared_ptr<GeomAPI_Lin>
132   GeomAPI_Pln::intersect(const std::shared_ptr<GeomAPI_Pln> thePlane) const
133 {
134   std::shared_ptr<GeomAPI_Lin> aRes;
135
136   if(!thePlane.get()) {
137     return aRes;
138   }
139
140   const gp_Pln& aMyPln = impl<gp_Pln>();
141   const gp_Pln& anOtherPln = thePlane->impl<gp_Pln>();
142
143   IntAna_QuadQuadGeo aQuad(aMyPln, anOtherPln, Precision::Confusion(), Precision::Confusion());
144
145   if(aQuad.IsDone() != Standard_True) {
146     return aRes;
147   }
148
149   if(aQuad.NbSolutions() != 1) {
150     return aRes;
151   }
152
153   gp_Lin aLin = aQuad.Line(1);
154   gp_Pnt aLoc = aLin.Location();
155   gp_Dir aDir = aLin.Direction();
156
157   std::shared_ptr<GeomAPI_Pnt> aGeomLoc(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
158   std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
159
160   aRes.reset(new GeomAPI_Lin(aGeomLoc, aGeomDir));
161
162   return aRes;
163 }