Salome HOME
a59c5b4ea0fa801faa8ba7eb54ff97e3fb6afecf
[modules/shaper.git] / src / GeomAPI / GeomAPI_Lin2d.cpp
1 // File:        GeomAPI_Lin2d.cpp
2 // Created:     29 May 2014
3 // Author:      Artem ZHIDKOV
4
5 #include <GeomAPI_Lin2d.h>
6 #include <GeomAPI_Pnt2d.h>
7
8 #include <gp_Dir2d.hxx>
9 #include <gp_Lin2d.hxx>
10 #include <gp_Pnt2d.hxx>
11 #include <gp_XY.hxx>
12
13 #include <IntAna2d_AnaIntersection.hxx>
14
15 #define MY_LIN2D static_cast<gp_Lin2d*>(myImpl)
16
17 static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX,
18                            const double theEndY)
19 {
20   gp_XY aDir(theEndX - theStartX, theEndY - theStartY);
21   gp_Pnt2d aStart(theStartX, theStartY);
22   return new gp_Lin2d(aStart, gp_Dir2d(aDir));
23 }
24
25 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
26                              const double theEndY)
27     : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
28 {
29 }
30
31 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
32                              const std::shared_ptr<GeomAPI_Pnt2d>& theEnd)
33     : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(), theEnd->x(), theEnd->y()))
34 {
35 }
36
37 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
38 {
39   return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
40 }
41
42 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
43     const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
44 {
45   IntAna2d_AnaIntersection anInter(*MY_LIN2D, theLine->impl<gp_Lin2d>());
46   if (!anInter.IsDone() || anInter.IsEmpty())
47   return std::shared_ptr<GeomAPI_Pnt2d>();
48   const gp_Pnt2d& aResult = anInter.Point(1).Value();
49   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
50 }
51
52 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
53     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
54 {
55   const gp_XY& aDir = MY_LIN2D->Direction().XY();
56   const gp_XY& aLoc = MY_LIN2D->Location().XY();
57   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
58   double aParam = aDir.Dot(aPnt - aLoc);
59
60   gp_XY aResult = aLoc + aDir * aParam;
61   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
62 }
63
64 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
65 {
66   const gp_XY& aDir = MY_LIN2D->Direction().XY();
67   const gp_XY& aLoc = MY_LIN2D->Location().XY();
68   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
69
70   return aDir.Crossed(aPnt - aLoc) > 0;
71 }
72
73
74 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
75 {
76   gp_Vec2d aVec = MY_LIN2D->Direction();
77   aVec = aVec.GetNormal();
78   aVec.Normalize();
79   aVec.Reverse();
80   aVec.Scale(theShift);
81   gp_Lin2d aLin = MY_LIN2D->Translated(aVec);
82   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLin.Location().X(), aLin.Location().Y()));
83 }