Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / GeomAPI / GeomAPI_Lin.cpp
1 // File:        GeomAPI_Lin.cpp
2 // Created:     29 May 2014
3 // Author:      Artem ZHIDKOV
4
5 #include <GeomAPI_Lin.h>
6 #include <GeomAPI_Pnt.h>
7
8 #include <gp_Dir.hxx>
9 #include <gp_Lin.hxx>
10 #include <gp_Lin2d.hxx>
11 #include <gp_Pln.hxx>
12 #include <gp_Pnt.hxx>
13 #include <gp_XYZ.hxx>
14
15 #include <ElSLib.hxx>
16 #include <IntAna2d_AnaIntersection.hxx>
17 #include <Precision.hxx>
18 #include <ProjLib.hxx>
19
20 #define MY_LIN static_cast<gp_Lin*>(myImpl)
21
22 static gp_Lin* newLine(const double theStartX, const double theStartY, const double theStartZ,
23                        const double theEndX, const double theEndY, const double theEndZ)
24 {
25   gp_XYZ aDir(theEndX - theStartX, theEndY - theStartY, theEndZ - theStartZ);
26   gp_Pnt aStart(theStartX, theStartY, theStartZ);
27   return new gp_Lin(aStart, gp_Dir(aDir));
28 }
29
30 GeomAPI_Lin::GeomAPI_Lin(const double theStartX, const double theStartY, const double theStartZ,
31                          const double theEndX, const double theEndY, const double theEndZ)
32     : GeomAPI_Interface(newLine(theStartX, theStartY, theStartZ, theEndX, theEndY, theEndZ))
33 {
34 }
35
36 GeomAPI_Lin::GeomAPI_Lin(const boost::shared_ptr<GeomAPI_Pnt>& theStart,
37                          const boost::shared_ptr<GeomAPI_Pnt>& theEnd)
38     : GeomAPI_Interface(
39         newLine(theStart->x(), theStart->y(), theStart->z(), theEnd->x(), theEnd->y(), theEnd->z()))
40 {
41 }
42
43 double GeomAPI_Lin::distance(const boost::shared_ptr<GeomAPI_Pnt>& thePoint) const
44 {
45   return MY_LIN->Distance(thePoint->impl<gp_Pnt>());
46 }
47
48 const boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::intersect(
49     const boost::shared_ptr<GeomAPI_Lin>& theLine) const
50 {
51   if (MY_LIN->SquareDistance(theLine->impl<gp_Lin>()) > Precision::Confusion())
52   return boost::shared_ptr<GeomAPI_Pnt>();
53
54   const gp_Dir& aDir1 = MY_LIN->Direction();
55   const gp_Dir& aDir2 = theLine->impl<gp_Lin>().Direction();
56   gp_Dir aCross = aDir1.Crossed(aDir2);
57   gp_Pln aPlane(MY_LIN->Location(), aCross);  // plane containing both lines
58
59   gp_Lin2d aPrjLine1 = ProjLib::Project(aPlane, *MY_LIN);
60   gp_Lin2d aPrjLine2 = ProjLib::Project(aPlane, theLine->impl<gp_Lin>());
61
62   IntAna2d_AnaIntersection anInter(aPrjLine1, aPrjLine1);
63   if (!anInter.IsDone() || anInter.IsEmpty())
64   return boost::shared_ptr<GeomAPI_Pnt>();
65   const gp_Pnt2d& anIntPnt2d = anInter.Point(0).Value();
66   gp_Pnt aResult = ElSLib::Value(anIntPnt2d.X(), anIntPnt2d.Y(), aPlane);
67
68   return boost::shared_ptr<GeomAPI_Pnt>(
69   new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
70 }
71
72 const boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::project(
73     const boost::shared_ptr<GeomAPI_Pnt>& thePoint) const
74 {
75   const gp_XYZ& aDir = MY_LIN->Direction().XYZ();
76   const gp_XYZ& aLoc = MY_LIN->Location().XYZ();
77   const gp_XYZ& aPnt = thePoint->impl<gp_Pnt>().XYZ();
78   double aParam = aDir.Dot(aPnt - aLoc);
79
80   gp_XYZ aResult = aPnt + aDir * aParam;
81   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
82 }
83