Salome HOME
Merge branch 'master' of newgeom:newgeom
[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,
18                            const double theEndX,   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
26 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY,
27                              const double theEndX,   const double theEndY)
28   : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
29 {}
30
31 GeomAPI_Lin2d::GeomAPI_Lin2d(const boost::shared_ptr<GeomAPI_Pnt2d>& theStart,
32                          const boost::shared_ptr<GeomAPI_Pnt2d>& theEnd)
33   : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(),
34                                 theEnd->x(),   theEnd->y()))
35 {}
36
37 double GeomAPI_Lin2d::distance(const boost::shared_ptr<GeomAPI_Pnt2d>& theOther) const
38 {
39   return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
40 }
41
42 const boost::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
43                 const boost::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 boost::shared_ptr<GeomAPI_Pnt2d>();
48   const gp_Pnt2d& aResult = anInter.Point(0).Value();
49   return boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
50 }
51
52 const boost::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
53 {
54   const gp_XY& aDir = MY_LIN2D->Direction().XY();
55   const gp_XY& aLoc = MY_LIN2D->Location().XY();
56   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
57   double aParam = aDir.Dot(aPnt - aLoc);
58
59   gp_XY aResult = aPnt + aDir * aParam;
60   return boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
61 }
62