1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAPI_Lin2d.cpp
4 // Created: 29 May 2014
5 // Author: Artem ZHIDKOV
7 #include <GeomAPI_Lin2d.h>
8 #include <GeomAPI_Pnt2d.h>
10 #include <gp_Dir2d.hxx>
11 #include <gp_Lin2d.hxx>
12 #include <gp_Pnt2d.hxx>
15 #include <IntAna2d_AnaIntersection.hxx>
17 #define MY_LIN2D static_cast<gp_Lin2d*>(myImpl)
19 static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX,
22 gp_XY aDir(theEndX - theStartX, theEndY - theStartY);
23 gp_Pnt2d aStart(theStartX, theStartY);
24 return new gp_Lin2d(aStart, gp_Dir2d(aDir));
27 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
29 : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
33 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
34 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd)
35 : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(), theEnd->x(), theEnd->y()))
39 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
41 return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
44 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
45 const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
47 IntAna2d_AnaIntersection anInter(*MY_LIN2D, theLine->impl<gp_Lin2d>());
48 if (!anInter.IsDone() || anInter.IsEmpty())
49 return std::shared_ptr<GeomAPI_Pnt2d>();
50 const gp_Pnt2d& aResult = anInter.Point(1).Value();
51 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
54 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
55 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
57 const gp_XY& aDir = MY_LIN2D->Direction().XY();
58 const gp_XY& aLoc = MY_LIN2D->Location().XY();
59 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
60 double aParam = aDir.Dot(aPnt - aLoc);
62 gp_XY aResult = aLoc + aDir * aParam;
63 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
66 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
68 const gp_XY& aDir = MY_LIN2D->Direction().XY();
69 const gp_XY& aLoc = MY_LIN2D->Location().XY();
70 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
72 return aDir.Crossed(aPnt - aLoc) > 0;
76 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
78 gp_Vec2d aVec = MY_LIN2D->Direction();
79 aVec = aVec.GetNormal();
83 gp_Lin2d aLin = MY_LIN2D->Translated(aVec);
84 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLin.Location().X(), aLin.Location().Y()));