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));
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))
36 GeomAPI_Lin::GeomAPI_Lin(const boost::shared_ptr<GeomAPI_Pnt>& theStart,
37 const boost::shared_ptr<GeomAPI_Pnt>& theEnd)
39 newLine(theStart->x(), theStart->y(), theStart->z(), theEnd->x(), theEnd->y(), theEnd->z()))
43 double GeomAPI_Lin::distance(const boost::shared_ptr<GeomAPI_Pnt>& thePoint) const
45 return MY_LIN->Distance(thePoint->impl<gp_Pnt>());
48 const boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::intersect(
49 const boost::shared_ptr<GeomAPI_Lin>& theLine) const
51 if (MY_LIN->SquareDistance(theLine->impl<gp_Lin>()) > Precision::Confusion())
52 return boost::shared_ptr<GeomAPI_Pnt>();
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
59 gp_Lin2d aPrjLine1 = ProjLib::Project(aPlane, *MY_LIN);
60 gp_Lin2d aPrjLine2 = ProjLib::Project(aPlane, theLine->impl<gp_Lin>());
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);
68 return boost::shared_ptr<GeomAPI_Pnt>(
69 new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));
72 const boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Lin::project(
73 const boost::shared_ptr<GeomAPI_Pnt>& thePoint) const
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);
80 gp_XYZ aResult = aPnt + aDir * aParam;
81 return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z()));