Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / GeomAPI / GeomAPI_Lin2d.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GeomAPI_Lin2d.h>
22 #include <GeomAPI_Pnt2d.h>
23 #include <GeomAPI_Dir2d.h>
24
25 #include <gp_Dir2d.hxx>
26 #include <gp_Lin2d.hxx>
27 #include <gp_Pnt2d.hxx>
28 #include <gp_XY.hxx>
29
30 #include <IntAna2d_AnaIntersection.hxx>
31
32 #define MY_LIN2D implPtr<gp_Lin2d>()
33
34 static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX,
35                            const double theEndY)
36 {
37   gp_XY aDir(theEndX - theStartX, theEndY - theStartY);
38   gp_Pnt2d aStart(theStartX, theStartY);
39   return new gp_Lin2d(aStart, gp_Dir2d(aDir));
40 }
41
42
43 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
44                              const double theEndY)
45     : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
46 {
47 }
48
49 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
50                              const std::shared_ptr<GeomAPI_Pnt2d>& theEnd)
51     : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(), theEnd->x(), theEnd->y()))
52 {
53 }
54
55 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theOrigin,
56                              const std::shared_ptr<GeomAPI_Dir2d>& theDirection)
57     : GeomAPI_Interface(newLine2d(theOrigin->x(), theOrigin->y(),
58         theOrigin->x() + theDirection->x(), theOrigin->y() + theDirection->y()))
59 {
60 }
61
62
63 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::location()
64 {
65   gp_Pnt2d aLoc = impl<gp_Lin2d>().Location();
66   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLoc.X(), aLoc.Y()));
67 }
68
69 std::shared_ptr<GeomAPI_Dir2d> GeomAPI_Lin2d::direction()
70 {
71   const gp_Dir2d& aDir = impl<gp_Lin2d>().Direction();
72   return std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir.X(), aDir.Y()));
73 }
74
75 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
76 {
77   return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
78 }
79
80 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
81     const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
82 {
83   IntAna2d_AnaIntersection anInter(*MY_LIN2D, theLine->impl<gp_Lin2d>());
84   if (!anInter.IsDone() || anInter.IsEmpty())
85   return std::shared_ptr<GeomAPI_Pnt2d>();
86   const gp_Pnt2d& aResult = anInter.Point(1).Value();
87   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
88 }
89
90 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
91     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
92 {
93   const gp_XY& aDir = MY_LIN2D->Direction().XY();
94   const gp_XY& aLoc = MY_LIN2D->Location().XY();
95   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
96   double aParam = aDir.Dot(aPnt - aLoc);
97
98   gp_XY aResult = aLoc + aDir * aParam;
99   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
100 }
101
102 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
103 {
104   const gp_XY& aDir = MY_LIN2D->Direction().XY();
105   const gp_XY& aLoc = MY_LIN2D->Location().XY();
106   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
107
108   return aDir.Crossed(aPnt - aLoc) > 0;
109 }
110
111
112 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
113 {
114   gp_Vec2d aVec = MY_LIN2D->Direction();
115   aVec = aVec.GetNormal();
116   aVec.Normalize();
117   aVec.Reverse();
118   aVec.Scale(theShift);
119   gp_Lin2d aLin = MY_LIN2D->Translated(aVec);
120   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLin.Location().X(),
121                                                           aLin.Location().Y()));
122 }