Salome HOME
Added new geometrical objects
[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
31 GeomAPI_Lin::GeomAPI_Lin(const double theStartX, const double theStartY, const double theStartZ,
32                          const double theEndX,   const double theEndY,   const double theEndZ)
33   : GeomAPI_Interface(newLine(theStartX, theStartY, theStartZ, theEndX, theEndY, theEndZ))
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(newLine(theStart->x(), theStart->y(), theStart->z(), 
39                               theEnd->x(),   theEnd->y(),   theEnd->z()))
40 {}
41
42 double GeomAPI_Lin::distance(const boost::shared_ptr<GeomAPI_Pnt>& thePoint) const
43 {
44   return MY_LIN->Distance(thePoint->impl<gp_Pnt>());
45 }
46
47 const boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::intersect(
48                 const boost::shared_ptr<GeomAPI_Lin>& theLine) const
49 {
50   if (MY_LIN->SquareDistance(theLine->impl<gp_Lin>()) > Precision::Confusion())
51     return boost::shared_ptr<GeomAPI_Pnt>();
52
53   const gp_Dir& aDir1 = MY_LIN->Direction();
54   const gp_Dir& aDir2 = theLine->impl<gp_Lin>().Direction();
55   gp_Dir aCross = aDir1.Crossed(aDir2);
56   gp_Pln aPlane(MY_LIN->Location(), aCross); // plane containing both lines
57
58   gp_Lin2d aPrjLine1 = ProjLib::Project(aPlane, *MY_LIN);
59   gp_Lin2d aPrjLine2 = ProjLib::Project(aPlane, theLine->impl<gp_Lin>());
60
61   IntAna2d_AnaIntersection anInter(aPrjLine1, aPrjLine1);
62   if (!anInter.IsDone() || anInter.IsEmpty())
63     return boost::shared_ptr<GeomAPI_Pnt>();
64   const gp_Pnt2d& anIntPnt2d = anInter.Point(0).Value();
65   gp_Pnt aResult = ElSLib::Value(anIntPnt2d.X(), anIntPnt2d.Y(), aPlane);
66
67   return boost::shared_ptr<GeomAPI_Pnt>(
68     new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
69 }
70
71 const boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::project(const boost::shared_ptr<GeomAPI_Pnt>& thePoint) const
72 {
73   const gp_XYZ& aDir = MY_LIN->Direction().XYZ();
74   const gp_XYZ& aLoc = MY_LIN->Location().XYZ();
75   const gp_XYZ& aPnt = thePoint->impl<gp_Pnt>().XYZ();
76   double aParam = aDir.Dot(aPnt - aLoc);
77
78   gp_XYZ aResult = aPnt + aDir * aParam;
79   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
80 }
81