Salome HOME
Copyright update 2022
[modules/shaper.git] / src / GeomAPI / GeomAPI_Lin.cpp
1 // Copyright (C) 2014-2022  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
18 //
19
20 #include <GeomAPI_Lin.h>
21 #include <GeomAPI_Pnt.h>
22 #include <GeomAPI_Dir.h>
23
24 #include <gp_Dir.hxx>
25 #include <gp_Lin.hxx>
26 #include <gp_Lin2d.hxx>
27 #include <gp_Pln.hxx>
28 #include <gp_Pnt.hxx>
29 #include <gp_XYZ.hxx>
30
31 #include <ElSLib.hxx>
32 #include <IntAna2d_AnaIntersection.hxx>
33 #include <Precision.hxx>
34 #include <ProjLib.hxx>
35
36 #define MY_LIN implPtr<gp_Lin>()
37
38 static gp_Lin* newLine(const double theStartX, const double theStartY, const double theStartZ,
39                        const double theEndX, const double theEndY, const double theEndZ)
40 {
41   gp_XYZ aDir(theEndX - theStartX, theEndY - theStartY, theEndZ - theStartZ);
42   gp_Pnt aStart(theStartX, theStartY, theStartZ);
43   return new gp_Lin(aStart, gp_Dir(aDir));
44 }
45
46 GeomAPI_Lin::GeomAPI_Lin(const double theStartX, const double theStartY, const double theStartZ,
47                          const double theEndX, const double theEndY, const double theEndZ)
48     : GeomAPI_Interface(newLine(theStartX, theStartY, theStartZ, theEndX, theEndY, theEndZ))
49 {
50 }
51
52 GeomAPI_Lin::GeomAPI_Lin(const std::shared_ptr<GeomAPI_Pnt>& theStart,
53                          const std::shared_ptr<GeomAPI_Pnt>& theEnd)
54     : GeomAPI_Interface(
55         newLine(theStart->x(), theStart->y(), theStart->z(), theEnd->x(), theEnd->y(), theEnd->z()))
56 {
57 }
58
59 GeomAPI_Lin::GeomAPI_Lin(const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
60                          const std::shared_ptr<GeomAPI_Dir>& theDirection)
61     : GeomAPI_Interface(newLine(theOrigin->x(), theOrigin->y(), theOrigin->z(),
62                                 theOrigin->x() + theDirection->x(),
63                                 theOrigin->y() + theDirection->y(),
64                                 theOrigin->z() + theDirection->z()))
65 {
66 }
67
68 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::location()
69 {
70   gp_Pnt aLoc = impl<gp_Lin>().Location();
71   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
72 }
73
74 std::shared_ptr<GeomAPI_Dir> GeomAPI_Lin::direction()
75 {
76   const gp_Dir& aDir = impl<gp_Lin>().Direction();
77   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
78 }
79
80 double GeomAPI_Lin::distance(const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
81 {
82   return MY_LIN->Distance(thePoint->impl<gp_Pnt>());
83 }
84
85 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::intersect(
86     const std::shared_ptr<GeomAPI_Lin>& theLine) const
87 {
88   if (MY_LIN->SquareDistance(theLine->impl<gp_Lin>()) > Precision::Confusion())
89   return std::shared_ptr<GeomAPI_Pnt>();
90
91   const gp_Dir& aDir1 = MY_LIN->Direction();
92   const gp_Dir& aDir2 = theLine->impl<gp_Lin>().Direction();
93   gp_Dir aCross = aDir1.Crossed(aDir2);
94   gp_Pln aPlane(MY_LIN->Location(), aCross);  // plane containing both lines
95
96   gp_Lin2d aPrjLine1 = ProjLib::Project(aPlane, *MY_LIN);
97   gp_Lin2d aPrjLine2 = ProjLib::Project(aPlane, theLine->impl<gp_Lin>());
98
99   IntAna2d_AnaIntersection anInter(aPrjLine1, aPrjLine2);
100   if (!anInter.IsDone() || anInter.IsEmpty())
101   return std::shared_ptr<GeomAPI_Pnt>();
102   const gp_Pnt2d& anIntPnt2d = anInter.Point(1).Value();
103   gp_Pnt aResult = ElSLib::Value(anIntPnt2d.X(), anIntPnt2d.Y(), aPlane);
104
105   return std::shared_ptr<GeomAPI_Pnt>(
106   new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
107 }
108
109 double GeomAPI_Lin::projParam(
110       const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
111 {
112   const gp_XYZ& aDir = MY_LIN->Direction().XYZ();
113   const gp_XYZ& aLoc = MY_LIN->Location().XYZ();
114   const gp_XYZ& aPnt = thePoint->impl<gp_Pnt>().XYZ();
115   return aDir.Dot(aPnt - aLoc);
116 }
117
118 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::project(
119     const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
120 {
121   const gp_XYZ& aDir = MY_LIN->Direction().XYZ();
122   const gp_XYZ& aLoc = MY_LIN->Location().XYZ();
123   double aParam = projParam(thePoint);
124   gp_XYZ aResult = aLoc + aDir * aParam;
125   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
126 }
127
128 bool GeomAPI_Lin::contains(const std::shared_ptr<GeomAPI_Pnt> thePoint,
129                            const double theLinearTolerance) const
130 {
131   if(!thePoint.get()) {
132     return false;
133   }
134
135   return MY_LIN->Contains(thePoint->impl<gp_Pnt>(), theLinearTolerance) == Standard_True;
136 }
137
138 bool GeomAPI_Lin::isParallel(const std::shared_ptr<GeomAPI_Lin> theLin) const
139 {
140   return MY_LIN->Direction().IsParallel(theLin->impl<gp_Lin>().Direction(),
141                                         Precision::Confusion()) == Standard_True;
142 }
143
144 bool GeomAPI_Lin::isCoplanar(const std::shared_ptr<GeomAPI_Lin> theLin) const
145 {
146   if(MY_LIN->SquareDistance(theLin->impl<gp_Lin>()) > Precision::Confusion()) {
147     return false;
148   }
149
150   return true;
151 }