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