Salome HOME
6b53b8fdc8abbee47052c4ad5d4b75e32e18e020
[modules/shaper.git] / src / GeomAPI / GeomAPI_Lin2d.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Lin2d.cpp
4 // Created:     29 May 2014
5 // Author:      Artem ZHIDKOV
6
7 #include <GeomAPI_Lin2d.h>
8 #include <GeomAPI_Pnt2d.h>
9 #include <GeomAPI_Dir2d.h>
10
11 #include <gp_Dir2d.hxx>
12 #include <gp_Lin2d.hxx>
13 #include <gp_Pnt2d.hxx>
14 #include <gp_XY.hxx>
15
16 #include <IntAna2d_AnaIntersection.hxx>
17
18 #define MY_LIN2D implPtr<gp_Lin2d>()
19
20 static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX,
21                            const double theEndY)
22 {
23   gp_XY aDir(theEndX - theStartX, theEndY - theStartY);
24   gp_Pnt2d aStart(theStartX, theStartY);
25   return new gp_Lin2d(aStart, gp_Dir2d(aDir));
26 }
27
28 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
29                              const double theEndY)
30     : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
31 {
32 }
33
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()))
37 {
38 }
39
40 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::location()
41 {
42   gp_Pnt2d aLoc = impl<gp_Lin2d>().Location();
43   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLoc.X(), aLoc.Y()));
44 }
45
46 std::shared_ptr<GeomAPI_Dir2d> GeomAPI_Lin2d::direction()
47 {
48   const gp_Dir2d& aDir = impl<gp_Lin2d>().Direction();
49   return std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir.X(), aDir.Y()));
50 }
51
52 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
53 {
54   return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
55 }
56
57 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
58     const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
59 {
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()));
65 }
66
67 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
68     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
69 {
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);
74
75   gp_XY aResult = aLoc + aDir * aParam;
76   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
77 }
78
79 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
80 {
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();
84
85   return aDir.Crossed(aPnt - aLoc) > 0;
86 }
87
88
89 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
90 {
91   gp_Vec2d aVec = MY_LIN2D->Direction();
92   aVec = aVec.GetNormal();
93   aVec.Normalize();
94   aVec.Reverse();
95   aVec.Scale(theShift);
96   gp_Lin2d aLin = MY_LIN2D->Translated(aVec);
97   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLin.Location().X(), aLin.Location().Y()));
98 }