Skip to content
Snippets Groups Projects
Commit e1cac8e3 authored by Tom Fischer's avatar Tom Fischer
Browse files

[MaL] Reimpl. calcProjPntToLineAndDists using Eigen.

parent f850166b
No related branches found
No related tags found
No related merge requests found
...@@ -7,32 +7,39 @@ ...@@ -7,32 +7,39 @@
* http://www.opengeosys.org/project/license * http://www.opengeosys.org/project/license
*/ */
#include "MathTools.h"
#include <Eigen/Dense>
#include <cmath> #include <cmath>
#include "MathTools.h" #include "Point3d.h"
namespace MathLib namespace MathLib
{ {
double calcProjPntToLineAndDists(const double p[3], const double a[3], double calcProjPntToLineAndDists(Point3d const& pp, Point3d const& pa,
const double b[3], double &lambda, double &d0) Point3d const& pb, double& lambda, double& d0)
{ {
// g (lambda) = a + lambda v, v = b-a auto const a =
double v[3] = {b[0] - a[0], b[1] - a[1], b[2] - a[2]}; Eigen::Map<Eigen::Vector3d>(const_cast<double*>(pa.getCoords()));
// orthogonal projection: (g(lambda)-p) * v = 0 => in order to compute lambda we define a help vector u auto const b =
double u[3] = {p[0] - a[0], p[1] - a[1], p[2] - a[2]}; Eigen::Map<Eigen::Vector3d>(const_cast<double*>(pb.getCoords()));
lambda = scalarProduct<double,3> (u, v) / scalarProduct<double,3> (v, v); auto const p =
Eigen::Map<Eigen::Vector3d>(const_cast<double*>(pp.getCoords()));
// g(lambda) = a + lambda v, v = b-a
Eigen::Vector3d const v = b - a;
// orthogonal projection: (p - g(lambda))^T * v = 0
// <=> (a-p - lambda (b-a))^T * (b-a) = 0
// <=> (a-p)^T * (b-a) = lambda (b-a)^T ) (b-a)
lambda = (((p - a).transpose() * v) / v.squaredNorm())(0, 0);
// compute projected point // compute projected point
double proj_pnt[3]; Eigen::Vector3d const proj_pnt = a + lambda * v;
for (std::size_t k(0); k < 3; k++)
{
proj_pnt[k] = a[k] + lambda * v[k];
}
d0 = std::sqrt (sqrDist (proj_pnt, a)); d0 = (proj_pnt - a).norm();
return std::sqrt (sqrDist (p, proj_pnt)); return (p - proj_pnt).norm();
} }
double getAngle (const double p0[3], const double p1[3], const double p2[3]) double getAngle (const double p0[3], const double p1[3], const double p2[3])
......
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
namespace MathLib namespace MathLib
{ {
template <typename T, std::size_t DIM> class TemplatePoint;
using Point3d = MathLib::TemplatePoint<double, 3>;
/** /**
* standard inner product in R^N * standard inner product in R^N
* \param v0 array of type T representing the vector * \param v0 array of type T representing the vector
...@@ -65,16 +68,18 @@ inline T scalarProduct(T const* const v0, T const* const v1, int const n) ...@@ -65,16 +68,18 @@ inline T scalarProduct(T const* const v0, T const* const v1, int const n)
* \f$g(\lambda) = a + \lambda (b - a)\f$, * \f$g(\lambda) = a + \lambda (b - a)\f$,
* the distance between p and the projected point * the distance between p and the projected point
* and the distances between the projected point and the end * and the distances between the projected point and the end
* points a, b of the line * points pa, pb of the line
* \param p the (mesh) point * \param pp the (mesh) point
* \param a first point of line * \param pa first point of line
* \param b second point of line * \param pb second point of line
* \param lambda the projected point described by the line equation above * \param lambda the projected point described by the line equation above
* \param d0 distance to the line point a * \param d0 distance to the line point a
* \returns the distance between p and the orthogonal projection of p * \returns the distance between pp and the orthogonal projection of pp
*/ */
double calcProjPntToLineAndDists(const double p[3], const double a[3], double calcProjPntToLineAndDists(MathLib::Point3d const& pp,
const double b[3], double &lambda, double &d0); MathLib::Point3d const& pa,
MathLib::Point3d const& pb, double& lambda,
double& d0);
/** squared dist between double arrays p0 and p1 (size of arrays is 3) */ /** squared dist between double arrays p0 and p1 (size of arrays is 3) */
inline inline
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment