Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
f7253761
Commit
f7253761
authored
Apr 26, 2018
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added the line class an some related utility functions.
parent
c56eaf0a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
375 additions
and
28 deletions
+375
-28
geometry.h
dlib/geometry.h
+1
-0
line.h
dlib/geometry/line.h
+180
-0
line_abstract.h
dlib/geometry/line_abstract.h
+194
-0
rectangle.h
dlib/geometry/rectangle.h
+0
-13
rectangle_abstract.h
dlib/geometry/rectangle_abstract.h
+0
-15
No files found.
dlib/geometry.h
View file @
f7253761
...
...
@@ -8,6 +8,7 @@
#include "geometry/vector.h"
#include "geometry/border_enumerator.h"
#include "geometry/point_transforms.h"
#include "geometry/line.h"
#endif // DLIB_GEOMETRy_HEADER
...
...
dlib/geometry/line.h
0 → 100644
View file @
f7253761
// 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_
dlib/geometry/line_abstract.h
0 → 100644
View file @
f7253761
// 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_
dlib/geometry/rectangle.h
View file @
f7253761
...
...
@@ -493,19 +493,6 @@ namespace dlib
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
(
...
...
dlib/geometry/rectangle_abstract.h
View file @
f7253761
...
...
@@ -748,21 +748,6 @@ namespace dlib
- 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
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment