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>
9 #include <GeomAPI_Dir2d.h>
11 #include <gp_Dir2d.hxx>
12 #include <gp_Lin2d.hxx>
13 #include <gp_Pnt2d.hxx>
16 #include <IntAna2d_AnaIntersection.hxx>
18 #define MY_LIN2D implPtr<gp_Lin2d>()
20 static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX,
23 gp_XY aDir(theEndX - theStartX, theEndY - theStartY);
24 gp_Pnt2d aStart(theStartX, theStartY);
25 return new gp_Lin2d(aStart, gp_Dir2d(aDir));
29 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
31 : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
35 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
36 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd)
37 : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(), theEnd->x(), theEnd->y()))
41 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theOrigin,
42 const std::shared_ptr<GeomAPI_Dir2d>& theDirection)
43 : GeomAPI_Interface(newLine2d(theOrigin->x(), theOrigin->y(),
44 theOrigin->x() + theDirection->x(), theOrigin->y() + theDirection->y()))
49 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::location()
51 gp_Pnt2d aLoc = impl<gp_Lin2d>().Location();
52 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLoc.X(), aLoc.Y()));
55 std::shared_ptr<GeomAPI_Dir2d> GeomAPI_Lin2d::direction()
57 const gp_Dir2d& aDir = impl<gp_Lin2d>().Direction();
58 return std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir.X(), aDir.Y()));
61 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
63 return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
66 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
67 const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
69 IntAna2d_AnaIntersection anInter(*MY_LIN2D, theLine->impl<gp_Lin2d>());
70 if (!anInter.IsDone() || anInter.IsEmpty())
71 return std::shared_ptr<GeomAPI_Pnt2d>();
72 const gp_Pnt2d& aResult = anInter.Point(1).Value();
73 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
76 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
77 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
79 const gp_XY& aDir = MY_LIN2D->Direction().XY();
80 const gp_XY& aLoc = MY_LIN2D->Location().XY();
81 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
82 double aParam = aDir.Dot(aPnt - aLoc);
84 gp_XY aResult = aLoc + aDir * aParam;
85 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
88 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
90 const gp_XY& aDir = MY_LIN2D->Direction().XY();
91 const gp_XY& aLoc = MY_LIN2D->Location().XY();
92 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
94 return aDir.Crossed(aPnt - aLoc) > 0;
98 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
100 gp_Vec2d aVec = MY_LIN2D->Direction();
101 aVec = aVec.GetNormal();
104 aVec.Scale(theShift);
105 gp_Lin2d aLin = MY_LIN2D->Translated(aVec);
106 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLin.Location().X(), aLin.Location().Y()));