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));
28 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
30 : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
34 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
35 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd)
36 : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(), theEnd->x(), theEnd->y()))
40 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::location()
42 gp_Pnt2d aLoc = impl<gp_Lin2d>().Location();
43 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLoc.X(), aLoc.Y()));
46 std::shared_ptr<GeomAPI_Dir2d> GeomAPI_Lin2d::direction()
48 const gp_Dir2d& aDir = impl<gp_Lin2d>().Direction();
49 return std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir.X(), aDir.Y()));
52 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
54 return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
57 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
58 const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
60 IntAna2d_AnaIntersection anInter(*MY_LIN2D, theLine->impl<gp_Lin2d>());
61 if (!anInter.IsDone() || anInter.IsEmpty())
62 return std::shared_ptr<GeomAPI_Pnt2d>();
63 const gp_Pnt2d& aResult = anInter.Point(1).Value();
64 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
67 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
68 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
70 const gp_XY& aDir = MY_LIN2D->Direction().XY();
71 const gp_XY& aLoc = MY_LIN2D->Location().XY();
72 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
73 double aParam = aDir.Dot(aPnt - aLoc);
75 gp_XY aResult = aLoc + aDir * aParam;
76 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
79 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
81 const gp_XY& aDir = MY_LIN2D->Direction().XY();
82 const gp_XY& aLoc = MY_LIN2D->Location().XY();
83 const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
85 return aDir.Crossed(aPnt - aLoc) > 0;
89 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
91 gp_Vec2d aVec = MY_LIN2D->Direction();
92 aVec = aVec.GetNormal();
96 gp_Lin2d aLin = MY_LIN2D->Translated(aVec);
97 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLin.Location().X(), aLin.Location().Y()));