1 // File: GeomAPI_Lin.cpp
2 // Created: 29 May 2014
3 // Author: Artem ZHIDKOV
5 #include <GeomAPI_Lin.h>
6 #include <GeomAPI_Pnt.h>
10 #include <gp_Lin2d.hxx>
16 #include <IntAna2d_AnaIntersection.hxx>
17 #include <Precision.hxx>
18 #include <ProjLib.hxx>
20 #define MY_LIN static_cast<gp_Lin*>(myImpl)
22 static gp_Lin* newLine(const double theStartX, const double theStartY, const double theStartZ,
23 const double theEndX, const double theEndY, const double theEndZ)
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));
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))
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()))
42 double GeomAPI_Lin::distance(const boost::shared_ptr<GeomAPI_Pnt>& thePoint) const
44 return MY_LIN->Distance(thePoint->impl<gp_Pnt>());
47 const boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::intersect(
48 const boost::shared_ptr<GeomAPI_Lin>& theLine) const
50 if (MY_LIN->SquareDistance(theLine->impl<gp_Lin>()) > Precision::Confusion())
51 return boost::shared_ptr<GeomAPI_Pnt>();
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
58 gp_Lin2d aPrjLine1 = ProjLib::Project(aPlane, *MY_LIN);
59 gp_Lin2d aPrjLine2 = ProjLib::Project(aPlane, theLine->impl<gp_Lin>());
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);
67 return boost::shared_ptr<GeomAPI_Pnt>(
68 new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
71 const boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::project(const boost::shared_ptr<GeomAPI_Pnt>& thePoint) const
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);
78 gp_XYZ aResult = aPnt + aDir * aParam;
79 return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));