Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[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
10 #include <gp_Dir2d.hxx>
11 #include <gp_Lin2d.hxx>
12 #include <gp_Pnt2d.hxx>
13 #include <gp_XY.hxx>
14
15 #include <IntAna2d_AnaIntersection.hxx>
16
17 #define MY_LIN2D static_cast<gp_Lin2d*>(myImpl)
18
19 static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX,
20                            const double theEndY)
21 {
22   gp_XY aDir(theEndX - theStartX, theEndY - theStartY);
23   gp_Pnt2d aStart(theStartX, theStartY);
24   return new gp_Lin2d(aStart, gp_Dir2d(aDir));
25 }
26
27 GeomAPI_Lin2d::GeomAPI_Lin2d(const double theStartX, const double theStartY, const double theEndX,
28                              const double theEndY)
29     : GeomAPI_Interface(newLine2d(theStartX, theStartY, theEndX, theEndY))
30 {
31 }
32
33 GeomAPI_Lin2d::GeomAPI_Lin2d(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
34                              const std::shared_ptr<GeomAPI_Pnt2d>& theEnd)
35     : GeomAPI_Interface(newLine2d(theStart->x(), theStart->y(), theEnd->x(), theEnd->y()))
36 {
37 }
38
39 double GeomAPI_Lin2d::distance(const std::shared_ptr<GeomAPI_Pnt2d>& theOther) const
40 {
41   return MY_LIN2D->Distance(theOther->impl<gp_Pnt2d>());
42 }
43
44 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::intersect(
45     const std::shared_ptr<GeomAPI_Lin2d>& theLine) const
46 {
47   IntAna2d_AnaIntersection anInter(*MY_LIN2D, theLine->impl<gp_Lin2d>());
48   if (!anInter.IsDone() || anInter.IsEmpty())
49   return std::shared_ptr<GeomAPI_Pnt2d>();
50   const gp_Pnt2d& aResult = anInter.Point(1).Value();
51   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
52 }
53
54 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::project(
55     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
56 {
57   const gp_XY& aDir = MY_LIN2D->Direction().XY();
58   const gp_XY& aLoc = MY_LIN2D->Location().XY();
59   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
60   double aParam = aDir.Dot(aPnt - aLoc);
61
62   gp_XY aResult = aLoc + aDir * aParam;
63   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aResult.X(), aResult.Y()));
64 }
65
66 bool GeomAPI_Lin2d::isRight(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
67 {
68   const gp_XY& aDir = MY_LIN2D->Direction().XY();
69   const gp_XY& aLoc = MY_LIN2D->Location().XY();
70   const gp_XY& aPnt = thePoint->impl<gp_Pnt2d>().XY();
71
72   return aDir.Crossed(aPnt - aLoc) > 0;
73 }
74
75
76 std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Lin2d::shiftedLocation(double theShift) const
77 {
78   gp_Vec2d aVec = MY_LIN2D->Direction();
79   aVec = aVec.GetNormal();
80   aVec.Normalize();
81   aVec.Reverse();
82   aVec.Scale(theShift);
83   gp_Lin2d aLin = MY_LIN2D->Translated(aVec);
84   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aLin.Location().X(), aLin.Location().Y()));
85 }