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_Lin2d.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_Dir2d.h>
24 #include <gp_Dir2d.hxx>
25 #include <gp_Lin2d.hxx>
26 #include <gp_Pnt2d.hxx>
29 #include <IntAna2d_AnaIntersection.hxx>
31 #define MY_LIN2D implPtr<gp_Lin2d>()
33 static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX,
36 gp_XY aDir(theEndX - theStartX, theEndY - theStartY);
37 gp_Pnt2d aStart(theStartX, theStartY);
38 return new gp_Lin2d(aStart, gp_Dir2d(aDir));
42 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
44 : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
48 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
49 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd)
50 : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(), theEnd->x(), theEnd->y()))
54 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theOrigin,
55 const std::shared_ptr<GeomAPI_Dir2d>& theDirection)
56 : GeomAPI_Interface(newLine2d(theOrigin->x(), theOrigin->y(),
57 theOrigin->x() + theDirection->x(), theOrigin->y() + theDirection->y()))
62 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::location()
64 gp_Pnt2d aLoc = impl<gp_Lin2d>().Location();
65 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLoc.X(), aLoc.Y()));
68 std::shared_ptr<GeomAPI_Dir2d> GeomAPI_Lin2d::direction()
70 const gp_Dir2d& aDir = impl<gp_Lin2d>().Direction();
71 return std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir.X(), aDir.Y()));
74 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
76 return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
79 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
80 const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
82 IntAna2d_AnaIntersection anInter(*MY_LIN2D, theLine->impl<gp_Lin2d>());
83 if (!anInter.IsDone() || anInter.NbPoints() == 0)
84 return std::shared_ptr<GeomAPI_Pnt2d>();
85 const gp_Pnt2d& aResult = anInter.Point(1).Value();
86 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
89 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
90 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
92 const gp_XY& aDir = MY_LIN2D->Direction().XY();
93 const gp_XY& aLoc = MY_LIN2D->Location().XY();
94 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
95 double aParam = aDir.Dot(aPnt - aLoc);
97 gp_XY aResult = aLoc + aDir * aParam;
98 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
101 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
103 const gp_XY& aDir = MY_LIN2D->Direction().XY();
104 const gp_XY& aLoc = MY_LIN2D->Location().XY();
105 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
107 return aDir.Crossed(aPnt - aLoc) > 0;
111 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
113 gp_Vec2d aVec = MY_LIN2D->Direction();
114 aVec = aVec.GetNormal();
117 aVec.Scale(theShift);
118 gp_Lin2d aLin = MY_LIN2D->Translated(aVec);
119 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLin.Location().X(),
120 aLin.Location().Y()));