]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Lin.cpp
Salome HOME
Update SketchPlugin_Projection feature (issue #1459)
[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 #include <GeomAPI_Dir.h>
10
11 #include <gp_Dir.hxx>
12 #include <gp_Lin.hxx>
13 #include <gp_Lin2d.hxx>
14 #include <gp_Pln.hxx>
15 #include <gp_Pnt.hxx>
16 #include <gp_XYZ.hxx>
17
18 #include <ElSLib.hxx>
19 #include <IntAna2d_AnaIntersection.hxx>
20 #include <Precision.hxx>
21 #include <ProjLib.hxx>
22
23 #define MY_LIN implPtr<gp_Lin>()
24
25 static gp_Lin* newLine(const double theStartX, const double theStartY, const double theStartZ,
26                        const double theEndX, const double theEndY, const double theEndZ)
27 {
28   gp_XYZ aDir(theEndX - theStartX, theEndY - theStartY, theEndZ - theStartZ);
29   gp_Pnt aStart(theStartX, theStartY, theStartZ);
30   return new gp_Lin(aStart, gp_Dir(aDir));
31 }
32
33 GeomAPI_Lin::GeomAPI_Lin(const double theStartX, const double theStartY, const double theStartZ,
34                          const double theEndX, const double theEndY, const double theEndZ)
35     : GeomAPI_Interface(newLine(theStartX, theStartY, theStartZ, theEndX, theEndY, theEndZ))
36 {
37 }
38
39 GeomAPI_Lin::GeomAPI_Lin(const std::shared_ptr<GeomAPI_Pnt>& theStart,
40                          const std::shared_ptr<GeomAPI_Pnt>& theEnd)
41     : GeomAPI_Interface(
42         newLine(theStart->x(), theStart->y(), theStart->z(), theEnd->x(), theEnd->y(), theEnd->z()))
43 {
44 }
45
46 GeomAPI_Lin::GeomAPI_Lin(const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
47                          const std::shared_ptr<GeomAPI_Dir>& theDirection)
48     : GeomAPI_Interface(newLine(theOrigin->x(), theOrigin->y(), theOrigin->z(),
49                                 theOrigin->x() + theDirection->x(),
50                                 theOrigin->y() + theDirection->y(),
51                                 theOrigin->z() + theDirection->z()))
52 {
53 }
54
55 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::location()
56 {
57   gp_Pnt aLoc = impl<gp_Lin>().Location();
58   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
59 }
60
61 std::shared_ptr<GeomAPI_Dir> GeomAPI_Lin::direction()
62 {
63   const gp_Dir& aDir = impl<gp_Lin>().Direction();
64   return std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
65 }
66
67 double GeomAPI_Lin::distance(const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
68 {
69   return MY_LIN->Distance(thePoint->impl<gp_Pnt>());
70 }
71
72 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::intersect(
73     const std::shared_ptr<GeomAPI_Lin>& theLine) const
74 {
75   if (MY_LIN->SquareDistance(theLine->impl<gp_Lin>()) > Precision::Confusion())
76   return std::shared_ptr<GeomAPI_Pnt>();
77
78   const gp_Dir& aDir1 = MY_LIN->Direction();
79   const gp_Dir& aDir2 = theLine->impl<gp_Lin>().Direction();
80   gp_Dir aCross = aDir1.Crossed(aDir2);
81   gp_Pln aPlane(MY_LIN->Location(), aCross);  // plane containing both lines
82
83   gp_Lin2d aPrjLine1 = ProjLib::Project(aPlane, *MY_LIN);
84   gp_Lin2d aPrjLine2 = ProjLib::Project(aPlane, theLine->impl<gp_Lin>());
85
86   IntAna2d_AnaIntersection anInter(aPrjLine1, aPrjLine2);
87   if (!anInter.IsDone() || anInter.IsEmpty())
88   return std::shared_ptr<GeomAPI_Pnt>();
89   const gp_Pnt2d& anIntPnt2d = anInter.Point(1).Value();
90   gp_Pnt aResult = ElSLib::Value(anIntPnt2d.X(), anIntPnt2d.Y(), aPlane);
91
92   return std::shared_ptr<GeomAPI_Pnt>(
93   new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
94 }
95
96 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::project(
97     const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
98 {
99   const gp_XYZ& aDir = MY_LIN->Direction().XYZ();
100   const gp_XYZ& aLoc = MY_LIN->Location().XYZ();
101   const gp_XYZ& aPnt = thePoint->impl<gp_Pnt>().XYZ();
102   double aParam = aDir.Dot(aPnt - aLoc);
103
104   gp_XYZ aResult = aPnt + aDir * aParam;
105   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
106 }
107