Salome HOME
Update copyrights
[modules/shaper.git] / src / GeomAPI / GeomAPI_Lin2d.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAPI_Lin2d.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_Dir2d.h>
23
24 #include <gp_Dir2d.hxx>
25 #include <gp_Lin2d.hxx>
26 #include <gp_Pnt2d.hxx>
27 #include <gp_XY.hxx>
28
29 #include <IntAna2d_AnaIntersection.hxx>
30
31 #define MY_LIN2D implPtr<gp_Lin2d>()
32
33 static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX,
34                            const double theEndY)
35 {
36   gp_XY aDir(theEndX - theStartX, theEndY - theStartY);
37   gp_Pnt2d aStart(theStartX, theStartY);
38   return new gp_Lin2d(aStart, gp_Dir2d(aDir));
39 }
40
41
42 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
43                              const double theEndY)
44     : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
45 {
46 }
47
48 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
49                              const std::shared_ptr<GeomAPI_Pnt2d>& theEnd)
50     : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(), theEnd->x(), theEnd->y()))
51 {
52 }
53
54 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theOrigin,
55                              const std::shared_ptr<GeomAPI_Dir2d>& theDirection)
56     : GeomAPI_Interface(newLine2d(theOrigin->x(), theOrigin->y(),
57         theOrigin->x() + theDirection->x(), theOrigin->y() + theDirection->y()))
58 {
59 }
60
61
62 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::location()
63 {
64   gp_Pnt2d aLoc = impl<gp_Lin2d>().Location();
65   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLoc.X(), aLoc.Y()));
66 }
67
68 std::shared_ptr<GeomAPI_Dir2d> GeomAPI_Lin2d::direction()
69 {
70   const gp_Dir2d& aDir = impl<gp_Lin2d>().Direction();
71   return std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir.X(), aDir.Y()));
72 }
73
74 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
75 {
76   return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
77 }
78
79 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
80     const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
81 {
82   IntAna2d_AnaIntersection anInter(*MY_LIN2D, theLine->impl<gp_Lin2d>());
83   if (!anInter.IsDone() || anInter.IsEmpty())
84   return std::shared_ptr<GeomAPI_Pnt2d>();
85   const gp_Pnt2d& aResult = anInter.Point(1).Value();
86   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
87 }
88
89 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
90     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
91 {
92   const gp_XY& aDir = MY_LIN2D->Direction().XY();
93   const gp_XY& aLoc = MY_LIN2D->Location().XY();
94   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
95   double aParam = aDir.Dot(aPnt - aLoc);
96
97   gp_XY aResult = aLoc + aDir * aParam;
98   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
99 }
100
101 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
102 {
103   const gp_XY& aDir = MY_LIN2D->Direction().XY();
104   const gp_XY& aLoc = MY_LIN2D->Location().XY();
105   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
106
107   return aDir.Crossed(aPnt - aLoc) > 0;
108 }
109
110
111 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
112 {
113   gp_Vec2d aVec = MY_LIN2D->Direction();
114   aVec = aVec.GetNormal();
115   aVec.Normalize();
116   aVec.Reverse();
117   aVec.Scale(theShift);
118   gp_Lin2d aLin = MY_LIN2D->Translated(aVec);
119   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLin.Location().X(),
120                                                           aLin.Location().Y()));
121 }