1 // Copyright (C) 2014-2023 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <GeomAPI_Lin.h>
21 #include <GeomAPI_Pnt.h>
22 #include <GeomAPI_Dir.h>
26 #include <gp_Lin2d.hxx>
32 #include <IntAna2d_AnaIntersection.hxx>
33 #include <Precision.hxx>
34 #include <ProjLib.hxx>
36 #define MY_LIN implPtr<gp_Lin>()
38 static gp_Lin* newLine(const double theStartX, const double theStartY, const double theStartZ,
39 const double theEndX, const double theEndY, const double theEndZ)
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));
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))
52 GeomAPI_Lin::GeomAPI_Lin(const std::shared_ptr<GeomAPI_Pnt>& theStart,
53 const std::shared_ptr<GeomAPI_Pnt>& theEnd)
55 newLine(theStart->x(), theStart->y(), theStart->z(), theEnd->x(), theEnd->y(), theEnd->z()))
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()))
68 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::location()
70 gp_Pnt aLoc = impl<gp_Lin>().Location();
71 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
74 std::shared_ptr<GeomAPI_Dir> GeomAPI_Lin::direction()
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()));
80 double GeomAPI_Lin::distance(const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
82 return MY_LIN->Distance(thePoint->impl<gp_Pnt>());
85 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::intersect(
86 const std::shared_ptr<GeomAPI_Lin>& theLine) const
88 if (MY_LIN->SquareDistance(theLine->impl<gp_Lin>()) > Precision::Confusion())
89 return std::shared_ptr<GeomAPI_Pnt>();
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
96 gp_Lin2d aPrjLine1 = ProjLib::Project(aPlane, *MY_LIN);
97 gp_Lin2d aPrjLine2 = ProjLib::Project(aPlane, theLine->impl<gp_Lin>());
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);
105 return std::shared_ptr<GeomAPI_Pnt>(
106 new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
109 double GeomAPI_Lin::projParam(
110 const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
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);
118 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::project(
119 const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
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()));
128 bool GeomAPI_Lin::contains(const std::shared_ptr<GeomAPI_Pnt> thePoint,
129 const double theLinearTolerance) const
131 if(!thePoint.get()) {
135 return MY_LIN->Contains(thePoint->impl<gp_Pnt>(), theLinearTolerance) == Standard_True;
138 bool GeomAPI_Lin::isParallel(const std::shared_ptr<GeomAPI_Lin> theLin) const
140 return MY_LIN->Direction().IsParallel(theLin->impl<gp_Lin>().Direction(),
141 Precision::Confusion()) == Standard_True;
144 bool GeomAPI_Lin::isCoplanar(const std::shared_ptr<GeomAPI_Lin> theLin) const
146 if(MY_LIN->SquareDistance(theLin->impl<gp_Lin>()) > Precision::Confusion()) {