Salome HOME
Issue #1860: fix end lines with spaces
[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
29 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
30                              const double theEndY)
31     : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
32 {
33 }
34
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()))
38 {
39 }
40
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()))
45 {
46 }
47
48
49 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::location()
50 {
51   gp_Pnt2d aLoc = impl<gp_Lin2d>().Location();
52   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLoc.X(), aLoc.Y()));
53 }
54
55 std::shared_ptr<GeomAPI_Dir2d> GeomAPI_Lin2d::direction()
56 {
57   const gp_Dir2d& aDir = impl<gp_Lin2d>().Direction();
58   return std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir.X(), aDir.Y()));
59 }
60
61 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
62 {
63   return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
64 }
65
66 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
67     const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
68 {
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()));
74 }
75
76 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
77     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
78 {
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);
83
84   gp_XY aResult = aLoc + aDir * aParam;
85   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
86 }
87
88 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
89 {
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();
93
94   return aDir.Crossed(aPnt - aLoc) > 0;
95 }
96
97
98 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
99 {
100   gp_Vec2d aVec = MY_LIN2D->Direction();
101   aVec = aVec.GetNormal();
102   aVec.Normalize();
103   aVec.Reverse();
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(),
107                                                           aLin.Location().Y()));
108 }