Salome HOME
Added option to create Construction Point by intersection of line and plane.
[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 using namespace std;
17
18 GeomAPI_Pln::GeomAPI_Pln(const std::shared_ptr<GeomAPI_Ax3>& theAxis)
19 : GeomAPI_Interface(new gp_Ax3(theAxis->impl<gp_Ax3>()))
20 {
21 }
22
23 GeomAPI_Pln::GeomAPI_Pln(const std::shared_ptr<GeomAPI_Pnt>& thePoint,
24                          const std::shared_ptr<GeomAPI_Dir>& theNormal)
25     : GeomAPI_Interface(new gp_Pln(thePoint->impl<gp_Pnt>(), theNormal->impl<gp_Dir>()))
26 {
27 }
28
29 GeomAPI_Pln::GeomAPI_Pln(const double theA, const double theB, const double theC, const double theD)
30     : GeomAPI_Interface(new gp_Pln(theA, theB, theC, theD))
31 {
32 }
33
34 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Pln::location() const
35 {
36   gp_Pnt aLoc = impl<gp_Pln>().Location();
37   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
38 }
39
40 std::shared_ptr<GeomAPI_Dir> GeomAPI_Pln::direction() const
41 {
42   const gp_Dir& aDir = impl<gp_Pln>().Axis().Direction();
43   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
44 }
45
46 std::shared_ptr<GeomAPI_Dir> GeomAPI_Pln::xDirection() const
47 {
48   const gp_Dir& aDir = impl<gp_Pln>().XAxis().Direction();
49   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
50 }
51
52 void GeomAPI_Pln::coefficients(double& theA, double& theB, double& theC, double& theD)
53 {
54   impl<gp_Pln>().Coefficients(theA, theB, theC, theD);
55 }
56
57 bool GeomAPI_Pln::isCoincident(const std::shared_ptr<GeomAPI_Pln> thePlane, const double theTolerance)
58 {
59   if(!thePlane.get()) {
60     return false;
61   }
62
63   const gp_Pln& aMyPln = impl<gp_Pln>();
64   const gp_Pln& anOtherPln = thePlane->impl<gp_Pln>();
65   return (aMyPln.Contains(anOtherPln.Location(), theTolerance) && aMyPln.Axis().IsParallel(anOtherPln.Axis(), theTolerance));
66 }
67
68 bool GeomAPI_Pln::isParallel(const std::shared_ptr<GeomAPI_Lin> theLine)
69 {
70   std::shared_ptr<GeomAPI_XYZ> aLineDir = theLine->direction()->xyz();
71   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
72
73   std::shared_ptr<GeomAPI_XYZ> aNormal = direction()->xyz();
74   std::shared_ptr<GeomAPI_XYZ> aLocation = location()->xyz();
75
76   double aDot = aNormal->dot(aLineDir);
77   return Abs(aDot) < Precision::SquareConfusion();
78 }
79
80 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Pln::intersect(const std::shared_ptr<GeomAPI_Lin>& theLine) const
81 {
82   std::shared_ptr<GeomAPI_XYZ> aLineDir = theLine->direction()->xyz();
83   std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
84
85   std::shared_ptr<GeomAPI_XYZ> aNormal = direction()->xyz();
86   std::shared_ptr<GeomAPI_XYZ> aLocation = location()->xyz();
87
88   double aDot = aNormal->dot(aLineDir);
89   if (Abs(aDot) < Precision::SquareConfusion())
90     return std::shared_ptr<GeomAPI_Pnt>();
91
92   double aParam = aNormal->dot(aLocation->decreased(aLineLoc)) / aDot;
93   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aLineLoc->added(aLineDir->multiplied(aParam))));
94 }
95
96 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Pln::project(const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
97 {
98   std::shared_ptr<GeomAPI_XYZ> aNormal = direction()->xyz();
99   std::shared_ptr<GeomAPI_XYZ> aLocation = location()->xyz();
100
101   std::shared_ptr<GeomAPI_XYZ> aVec = thePoint->xyz()->decreased(aLocation);
102   double aDot = aNormal->dot(aVec);
103   std::shared_ptr<GeomAPI_XYZ> aProjection = 
104       aLocation->added(aVec->decreased(aNormal->multiplied(aDot)));
105   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aProjection));
106 }