Salome HOME
Sources formated according to the codeing standards
[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, const double theEndX,
18                            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 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
26                              const double theEndY)
27     : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
28 {
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(), theEnd->x(), theEnd->y()))
34 {
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(1).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(
53     const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
54 {
55   const gp_XY& aDir = MY_LIN2D->Direction().XY();
56   const gp_XY& aLoc = MY_LIN2D->Location().XY();
57   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
58   double aParam = aDir.Dot(aPnt - aLoc);
59
60   gp_XY aResult = aLoc + aDir * aParam;
61   return boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
62 }
63
64 bool GeomAPI_Lin2d::isRight(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
65 {
66   const gp_XY& aDir = MY_LIN2D->Direction().XY();
67   const gp_XY& aLoc = MY_LIN2D->Location().XY();
68   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
69
70   return aDir.Crossed(aPnt - aLoc) > 0;
71 }