Skip to content

Commit 87b862c

Browse files
committed
Fix #388 by using Boost.Geometry
"crosses" fits the requirement as it means that the interior parts of the geometry share some common points, not exterior parts such as end-points, and it means "not within".
1 parent 8a60b5a commit 87b862c

File tree

1 file changed

+19
-70
lines changed

1 file changed

+19
-70
lines changed

include/boost/graph/is_straight_line_drawing.hpp

+19-70
Original file line numberDiff line numberDiff line change
@@ -16,75 +16,16 @@
1616
#include <boost/graph/properties.hpp>
1717
#include <boost/graph/planar_detail/bucket_sort.hpp>
1818

19+
#include <boost/geometry/algorithms/crosses.hpp>
20+
#include <boost/geometry/geometries/linestring.hpp>
21+
1922
#include <algorithm>
2023
#include <vector>
21-
#include <set>
2224
#include <map>
2325

2426
namespace boost
2527
{
2628

27-
// Return true exactly when the line segments s1 = ((x1,y1), (x2,y2)) and
28-
// s2 = ((a1,b1), (a2,b2)) intersect in a point other than the endpoints of
29-
// the line segments. The one exception to this rule is when s1 = s2, in
30-
// which case false is returned - this is to accomodate multiple edges
31-
// between the same pair of vertices, which shouldn't invalidate the straight
32-
// line embedding. A tolerance variable epsilon can also be used, which
33-
// defines how far away from the endpoints of s1 and s2 we want to consider
34-
// an intersection.
35-
36-
inline bool intersects(double x1, double y1, double x2, double y2, double a1,
37-
double b1, double a2, double b2, double epsilon = 0.000001)
38-
{
39-
40-
if (x1 - x2 == 0)
41-
{
42-
std::swap(x1, a1);
43-
std::swap(y1, b1);
44-
std::swap(x2, a2);
45-
std::swap(y2, b2);
46-
}
47-
48-
if (x1 - x2 == 0)
49-
{
50-
BOOST_USING_STD_MAX();
51-
BOOST_USING_STD_MIN();
52-
53-
// two vertical line segments
54-
double min_y = min BOOST_PREVENT_MACRO_SUBSTITUTION(y1, y2);
55-
double max_y = max BOOST_PREVENT_MACRO_SUBSTITUTION(y1, y2);
56-
double min_b = min BOOST_PREVENT_MACRO_SUBSTITUTION(b1, b2);
57-
double max_b = max BOOST_PREVENT_MACRO_SUBSTITUTION(b1, b2);
58-
if ((max_y > max_b && max_b > min_y)
59-
|| (max_b > max_y && max_y > min_b))
60-
return true;
61-
else
62-
return false;
63-
}
64-
65-
double x_diff = x1 - x2;
66-
double y_diff = y1 - y2;
67-
double a_diff = a2 - a1;
68-
double b_diff = b2 - b1;
69-
70-
double beta_denominator = b_diff - (y_diff / ((double)x_diff)) * a_diff;
71-
72-
if (beta_denominator == 0)
73-
{
74-
// parallel lines
75-
return false;
76-
}
77-
78-
double beta = (b2 - y2 - (y_diff / ((double)x_diff)) * (a2 - x2))
79-
/ beta_denominator;
80-
double alpha = (a2 - x2 - beta * (a_diff)) / x_diff;
81-
82-
double upper_bound = 1 - epsilon;
83-
double lower_bound = 0 + epsilon;
84-
85-
return (beta < upper_bound && beta > lower_bound && alpha < upper_bound
86-
&& alpha > lower_bound);
87-
}
8829

8930
template < typename Graph, typename GridPositionMap, typename VertexIndexMap >
9031
bool is_straight_line_drawing(
@@ -152,6 +93,10 @@ bool is_straight_line_drawing(
15293
}
15394
else
15495
{
96+
using geometry::crosses;
97+
using geometry::model::linestring;
98+
using geometry::model::d2::point_xy;
99+
155100
active_map_iterator_t before, after;
156101
if (a_itr == active_edges.begin())
157102
before = active_edges.end();
@@ -168,10 +113,12 @@ bool is_straight_line_drawing(
168113
vertex_t f_source(source(f, g));
169114
vertex_t f_target(target(f, g));
170115

171-
if (intersects(drawing[e_source].x, drawing[e_source].y,
172-
drawing[e_target].x, drawing[e_target].y,
173-
drawing[f_source].x, drawing[f_source].y,
174-
drawing[f_target].x, drawing[f_target].y))
116+
linestring<point_xy<double>> source{{drawing[e_source].x, drawing[e_source].y},
117+
{drawing[e_target].x, drawing[e_target].y}};
118+
linestring<point_xy<double>> target{{drawing[f_source].x, drawing[f_source].y},
119+
{drawing[f_target].x, drawing[f_target].y}};
120+
121+
if (crosses(source, target))
175122
return false;
176123
}
177124

@@ -184,10 +131,12 @@ bool is_straight_line_drawing(
184131
vertex_t f_source(source(f, g));
185132
vertex_t f_target(target(f, g));
186133

187-
if (intersects(drawing[e_source].x, drawing[e_source].y,
188-
drawing[e_target].x, drawing[e_target].y,
189-
drawing[f_source].x, drawing[f_source].y,
190-
drawing[f_target].x, drawing[f_target].y))
134+
linestring<point_xy<double>> source{{drawing[e_source].x, drawing[e_source].y},
135+
{drawing[e_target].x, drawing[e_target].y}};
136+
linestring<point_xy<double>> target{{drawing[f_source].x, drawing[f_source].y},
137+
{drawing[f_target].x, drawing[f_target].y}};
138+
139+
if (crosses(source, target))
191140
return false;
192141
}
193142

0 commit comments

Comments
 (0)