1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <GeomAPI_Lin2d.h>
22 #include <GeomAPI_Pnt2d.h>
23 #include <GeomAPI_Dir2d.h>
25 #include <gp_Dir2d.hxx>
26 #include <gp_Lin2d.hxx>
27 #include <gp_Pnt2d.hxx>
30 #include <IntAna2d_AnaIntersection.hxx>
32 #define MY_LIN2D implPtr<gp_Lin2d>()
34 static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX,
37 gp_XY aDir(theEndX - theStartX, theEndY - theStartY);
38 gp_Pnt2d aStart(theStartX, theStartY);
39 return new gp_Lin2d(aStart, gp_Dir2d(aDir));
43 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
45 : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
49 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
50 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd)
51 : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(), theEnd->x(), theEnd->y()))
55 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theOrigin,
56 const std::shared_ptr<GeomAPI_Dir2d>& theDirection)
57 : GeomAPI_Interface(newLine2d(theOrigin->x(), theOrigin->y(),
58 theOrigin->x() + theDirection->x(), theOrigin->y() + theDirection->y()))
63 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::location()
65 gp_Pnt2d aLoc = impl<gp_Lin2d>().Location();
66 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLoc.X(), aLoc.Y()));
69 std::shared_ptr<GeomAPI_Dir2d> GeomAPI_Lin2d::direction()
71 const gp_Dir2d& aDir = impl<gp_Lin2d>().Direction();
72 return std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir.X(), aDir.Y()));
75 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
77 return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
80 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
81 const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
83 IntAna2d_AnaIntersection anInter(*MY_LIN2D, theLine->impl<gp_Lin2d>());
84 if (!anInter.IsDone() || anInter.IsEmpty())
85 return std::shared_ptr<GeomAPI_Pnt2d>();
86 const gp_Pnt2d& aResult = anInter.Point(1).Value();
87 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
90 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
91 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
93 const gp_XY& aDir = MY_LIN2D->Direction().XY();
94 const gp_XY& aLoc = MY_LIN2D->Location().XY();
95 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
96 double aParam = aDir.Dot(aPnt - aLoc);
98 gp_XY aResult = aLoc + aDir * aParam;
99 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
102 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
104 const gp_XY& aDir = MY_LIN2D->Direction().XY();
105 const gp_XY& aLoc = MY_LIN2D->Location().XY();
106 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
108 return aDir.Crossed(aPnt - aLoc) > 0;
112 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
114 gp_Vec2d aVec = MY_LIN2D->Direction();
115 aVec = aVec.GetNormal();
118 aVec.Scale(theShift);
119 gp_Lin2d aLin = MY_LIN2D->Translated(aVec);
120 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLin.Location().X(),
121 aLin.Location().Y()));