Commit f7253761 authored by Davis King's avatar Davis King

Added the line class an some related utility functions.

parent c56eaf0a
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "geometry/vector.h" #include "geometry/vector.h"
#include "geometry/border_enumerator.h" #include "geometry/border_enumerator.h"
#include "geometry/point_transforms.h" #include "geometry/point_transforms.h"
#include "geometry/line.h"
#endif // DLIB_GEOMETRy_HEADER #endif // DLIB_GEOMETRy_HEADER
......
// Copyright (C) 2018 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_LInE_H_
#define DLIB_LInE_H_
#include "line_abstract.h"
#include "vector.h"
#include <utility>
namespace dlib
{
// ----------------------------------------------------------------------------------------
class line
{
public:
line() = default;
line(const dpoint& a, const dpoint& b) : end1(a), end2(b)
{
normal_vector = (end1-end2).cross(dlib::vector<double,3>(0,0,1)).normalize();
}
template <typename T>
line(const std::pair<vector<T,2>,vector<T,2>>& l) : line(l.first, l.second) {}
const dpoint& p1() const { return end1; }
const dpoint& p2() const { return end2; }
const dpoint& normal() const { return normal_vector; }
private:
dpoint end1;
dpoint end2;
dpoint normal_vector;
};
// ----------------------------------------------------------------------------------------
template <typename U>
double signed_distance_to_line (
const line& l,
const vector<U,2>& p
)
{
return dot(p-l.p1(), l.normal());
}
template <typename T, typename U>
double signed_distance_to_line (
const std::pair<vector<T,2>,vector<T,2> >& l,
const vector<U,2>& p
)
{
return signed_distance_to_line(line(l),p);
}
template <typename T, typename U>
double distance_to_line (
const std::pair<vector<T,2>,vector<T,2> >& l,
const vector<U,2>& p
)
{
return std::abs(signed_distance_to_line(l,p));
}
template <typename U>
double distance_to_line (
const line& l,
const vector<U,2>& p
)
{
return std::abs(signed_distance_to_line(l,p));
}
// ----------------------------------------------------------------------------------------
inline line reverse(const line& l)
{
return line(l.p2(), l.p1());
}
// ----------------------------------------------------------------------------------------
template <typename T>
inline dpoint intersect(
const std::pair<vector<T,2>,vector<T,2>>& a,
const std::pair<vector<T,2>,vector<T,2>>& b
)
{
// convert to homogeneous coordinates
dlib::vector<double,3> a1 = a.first;
dlib::vector<double,3> a2 = a.second;
dlib::vector<double,3> b1 = b.first;
dlib::vector<double,3> b2 = b.second;
a1.z() = 1;
a2.z() = 1;
b1.z() = 1;
b2.z() = 1;
// find lines between pairs of points.
auto l1 = a1.cross(a2);
auto l2 = b1.cross(b2);
// find intersection of the lines.
auto p = l1.cross(l2);
if (p.z() != 0)
return p/p.z();
else
return dpoint(std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity());
}
// ----------------------------------------------------------------------------------------
inline dpoint intersect(
const line& a,
const line& b
)
{
return intersect(std::make_pair(a.p1(),a.p2()), std::make_pair(b.p1(), b.p2()));
}
// ----------------------------------------------------------------------------------------
template <typename T>
inline size_t count_points_on_side_of_line(
line l,
const dpoint& reference_point,
const std::vector<vector<T,2>>& pts,
const double& dist_thresh
)
{
if (signed_distance_to_line(l,reference_point) < 0)
l = reverse(l);
size_t cnt = 0;
for (auto& p : pts)
{
double dist = signed_distance_to_line(l,p);
if (0 <= dist && dist <= dist_thresh)
++cnt;
}
return cnt;
}
// ----------------------------------------------------------------------------------------
template <typename T>
inline double count_points_between_lines(
line l1,
line l2,
const dpoint& reference_point,
const std::vector<vector<T,2>>& pts
)
{
if (signed_distance_to_line(l1,reference_point) < 0)
l1 = reverse(l1);
if (signed_distance_to_line(l2,reference_point) < 0)
l2 = reverse(l2);
size_t cnt = 0;
for (auto& p : pts)
{
if (signed_distance_to_line(l1,p) > 0 && signed_distance_to_line(l2,p) > 0)
++cnt;
}
return cnt;
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_LInE_H_
// Copyright (C) 2018 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_LInE_ABSTRACT_H_
#ifdef DLIB_LInE_ABSTRACT_H_
namespace dlib
{
// ----------------------------------------------------------------------------------------
class line
{
/*!
WHAT THIS OBJECT REPRESENTS
This object represents a line in the 2D plane. The line is defined by two
points running through it, p1() and p2(). This object also includes a
unit normal vector that is perpendicular to the line.
!*/
public:
line(
);
/*!
ensures
- p1(), p2, and normal() are all the 0 vector.
!*/
line(
const dpoint& a,
const dpoint& b
);
/*!
ensures
- #p1() == a
- #p2() == b
- #normal() == A vector normal to the line passing through points a and b.
In particular, it is given by: (a-b).cross(dlib::vector<double,3>(0,0,1)).normalize()
!*/
template <typename T>
line(
const std::pair<vector<T,2>,vector<T,2>>& l
);
/*!
ensures
- #*this == line(l.first, l.second)
!*/
const dpoint& p1(
) const;
/*!
ensures
- returns the first endpoint of the line.
!*/
const dpoint& p2(
) const;
/*!
ensures
- returns the second endpoint of the line.
!*/
const dpoint& normal(
) const;
/*!
ensures
- returns a unit vector that is normal to the line passing through p1() and p2().
!*/
};
// ----------------------------------------------------------------------------------------
template <typename U>
double signed_distance_to_line (
const line& l,
const vector<U,2>& p
);
/*!
ensures
- returns how far p is from the line l. This is a signed distance. The sign
indicates which side of the line the point is on and the magnitude is the
distance. Moreover, the direction of positive sign is pointed to by the
vector l.normal().
- To be specific, this routine returns dot(p-l.p1(), l.normal())
!*/
template <typename T, typename U>
double signed_distance_to_line (
const std::pair<vector<T,2>,vector<T,2> >& l,
const vector<U,2>& p
);
/*!
ensures
- returns signed_distance_to_line(line(l),p);
!*/
template <typename T, typename U>
double distance_to_line (
const std::pair<vector<T,2>,vector<T,2> >& l,
const vector<U,2>& p
);
/*!
ensures
- returns abs(signed_distance_to_line(l,p))
!*/
template <typename U>
double distance_to_line (
const line& l,
const vector<U,2>& p
);
/*!
ensures
- returns abs(signed_distance_to_line(l,p))
!*/
// ----------------------------------------------------------------------------------------
line reverse(
const line& l
);
/*!
ensures
- returns line(l.p2(), l.p1())
(i.e. returns a line object that represents the same line as l but with the
endpoints, and therefore, the normal vector flipped. This means that the
signed distance of operator() is also flipped).
!*/
// ----------------------------------------------------------------------------------------
dpoint intersect(
const line& a,
const line& b
);
/*!
ensures
- returns the point of intersection between lines a and b. If no such point
exists then this function returns a point with Inf values in it.
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
dpoint intersect(
const std::pair<vector<T,2>,vector<T,2>>& a,
const std::pair<vector<T,2>,vector<T,2>>& b
);
/*!
ensures
- returns intersect(line(a), line(b))
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
size_t count_points_on_side_of_line(
const line& l,
const dpoint& reference_point,
const std::vector<vector<T,2>>& pts,
const double& dist_thresh
);
/*!
ensures
- Returns a count of how many points in pts are on the same side of l as
reference_point, but also no more than dist_thresh distance from the line.
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
double count_points_between_lines(
const line& l1,
const line& l2,
const dpoint& reference_point,
const std::vector<vector<T,2>>& pts
);
/*!
ensures
- Counts and returns the number of points in pts that are between lines l1 and
l2. Since a pair of lines will, in the general case, divide the plane into 4
regions, we identify the region of interest as the one that contains the
reference_point. Therefore, this function counts the number of points in pts
that appear in the same region as reference_point.
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_LInE_ABSTRACT_H_
...@@ -493,19 +493,6 @@ namespace dlib ...@@ -493,19 +493,6 @@ namespace dlib
return idx; return idx;
} }
// ----------------------------------------------------------------------------------------
template <typename T, typename U>
double distance_to_line (
const std::pair<vector<T,2>,vector<T,2> >& line,
const vector<U,2>& p
)
{
const vector<double,2> delta = p-line.second;
const double along_dist = (line.first-line.second).normalize().dot(delta);
return std::sqrt(std::max(0.0,delta.length_squared() - along_dist*along_dist));
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
inline void clip_line_to_rectangle ( inline void clip_line_to_rectangle (
......
...@@ -748,21 +748,6 @@ namespace dlib ...@@ -748,21 +748,6 @@ namespace dlib
- returns the Manhattan distance between the edge of rect and p. - returns the Manhattan distance between the edge of rect and p.
!*/ !*/
// ----------------------------------------------------------------------------------------
template <typename T, typename U>
double distance_to_line (
const std::pair<vector<T,2>,vector<T,2> >& line,
const vector<U,2>& p
);
/*!
ensures
- returns the euclidean distance between the given line and the point p. That
is, given a line that passes though the points line.first and line.second,
what is the distance between p and the nearest point on the line? This
function returns that distance.
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
void clip_line_to_rectangle ( void clip_line_to_rectangle (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment