File size: 7,685 Bytes
9375c9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
// Copyright (C) 2018 Davis E. King ([email protected])
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_LInE_ABSTRACT_H_
#ifdef DLIB_LInE_ABSTRACT_H_
#include "../vector_abstract.h"
#include <vector>
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().
Therefore, the normal vector is the vector (a-b) but unit normalized and rotated clockwise 90 degrees.
!*/
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.
!*/
// ----------------------------------------------------------------------------------------
double angle_between_lines (
const line& a,
const line& b
);
/*!
ensures
- returns the angle, in degrees, between the given lines. This is a number in
the range [0 90].
!*/
// ----------------------------------------------------------------------------------------
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_min = 0,
const double& dist_thresh_max = std::numeric_limits<double>::infinity()
);
/*!
ensures
- Returns a count of how many points in pts have a distance from the line l
that is in the range [dist_thresh_min, dist_thresh_max]. This distance is a
signed value that indicates how far a point is from the line. Moreover, if
the point is on the same side as reference_point then the distance is
positive, otherwise it is negative. So for example, If this range is [0,
infinity] then this function counts how many points are on the same side of l
as reference_point.
!*/
// ----------------------------------------------------------------------------------------
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.
!*/
// ----------------------------------------------------------------------------------------
struct no_convex_quadrilateral : dlib::error
{
/*!
WHAT THIS OBJECT REPRESENTS
This is the exception thrown by find_convex_quadrilateral() if the inputs
can't form a convex quadrilateral.
!*/
no_convex_quadrilateral(
) : dlib::error("Lines given to find_convex_quadrilateral() don't form any convex quadrilateral.")
{}
};
std::array<dpoint,4> find_convex_quadrilateral (
const std::array<line,4>& lines
);
/*!
ensures
- Is there a set of 4 points, made up of the intersections of the given lines,
that forms a convex quadrilateral? If yes then this routine returns those 4
points and if not throws no_convex_quadrilateral.
throws
- no_convex_quadrilateral
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_LInE_ABSTRACT_H_
|