1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2015 Barend Gehrels, Amsterdam, the Netherlands.
6 // This file was modified by Oracle on 2017.
7 // Modifications copyright (c) 2017, Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
21 #include <boost/type_traits/is_same.hpp>
23 #if defined(TEST_WITH_SVG)
24 # include <boost/geometry/io/svg/svg_mapper.hpp>
27 #include <geometry_test_common.hpp>
28 #include <algorithms/check_validity.hpp>
30 #include <boost/geometry.hpp>
31 #include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
32 #include <boost/geometry/geometries/geometries.hpp>
34 //#include <boost/geometry/extensions/algorithms/inverse.hpp>
36 #if defined(TEST_WITH_SVG)
37 # include <boost/geometry/io/svg/svg_mapper.hpp>
40 #include "multi_overlay_cases.hpp"
43 #if defined(TEST_WITH_SVG)
44 template <typename Mapper>
47 map_visitor(Mapper& mapper)
53 void print(char const* header)
56 template <typename Turns>
57 void print(char const* header, Turns const& turns, int turn_index)
59 std::string style = "fill:rgb(0,0,0);font-family:Arial;font-size:6px";
60 stream(turns, turns[turn_index], turns[turn_index].operations[0], header, style);
63 template <typename Turns>
64 void print(char const* header, Turns const& turns, int turn_index, int op_index)
66 std::string style = "fill:rgb(0,0,0);font-family:Arial;font-size:6px";
67 stream(turns, turns[turn_index], turns[turn_index].operations[op_index], header, style);
70 template <typename Turns>
71 void visit_turns(int phase, Turns const& turns)
73 typedef typename boost::range_value<Turns>::type turn_type;
75 BOOST_FOREACH(turn_type const& turn, turns)
80 m_mapper.map(turn.point, "fill:rgb(255,128,0);"
81 "stroke:rgb(0,0,0);stroke-width:1", 3);
84 m_mapper.map(turn.point, "fill:rgb(92,255,0);" // Greenish
85 "stroke:rgb(0,0,0);stroke-width:1", 3);
88 m_mapper.map(turn.point, "fill:rgb(0,128,255);" // Blueish
89 "stroke:rgb(0,0,0);stroke-width:1", 3);
92 label_turn(index, turn);
99 template <typename Turns, typename Turn, typename Operation>
100 std::string stream_turn_index(Turns const& turns, Turn const& turn, Operation const& op)
102 std::ostringstream out;
104 if (turn.cluster_id >= 0)
106 out << "cl=" << turn.cluster_id << " ";
109 // Because turn index is unknown here, and still useful for debugging,
110 std::size_t index = 0;
111 for (typename Turns::const_iterator it = turns.begin();
112 it != turns.end(); ++it, ++index)
122 if (&op == &turn.operations[0]) { out << "[0]"; }
123 if (&op == &turn.operations[1]) { out << "[1]"; }
127 template <typename Clusters, typename Turns>
128 void visit_clusters(Clusters const& clusters, Turns const& turns)
130 typedef typename boost::range_value<Turns>::type turn_type;
132 BOOST_FOREACH(turn_type const& turn, turns)
134 if (turn.cluster_id >= 0)
136 std::cout << " TURN: " << index << " part of cluster " << turn.cluster_id << std::endl;
141 for (typename Clusters::const_iterator it = clusters.begin(); it != clusters.end(); ++it)
143 std::cout << " CLUSTER " << it->first << ": ";
144 for (typename std::set<bg::signed_size_type>::const_iterator sit
145 = it->second.turn_indices.begin();
146 sit != it->second.turn_indices.end(); ++sit)
148 std::cout << " " << *sit;
150 std::cout << std::endl;
153 std::cout << std::endl;
157 template <typename Turns, typename Turn, typename Operation>
158 void visit_traverse(Turns const& turns, Turn const& turn, Operation const& op, const std::string& header)
160 typedef typename boost::range_value<Turns>::type turn_type;
167 std::cout << "Visit turn " << stream_turn_index(turns, turn, op)
168 << " " << bg::operation_char(turn.operations[0].operation)
169 << bg::operation_char(turn.operations[1].operation)
170 << " (" << bg::operation_char(op.operation) << ")"
171 << " " << header << std::endl;
173 // Uncomment for more detailed debug info in SVG on traversal
175 = header == "Visit" ? "fill:rgb(80,80,80)" : "fill:rgb(0,0,0)";
177 style += ";font-family:Arial;font-size:6px";
179 stream(turns, turn, op, header.substr(0, 1), style);
182 template <typename Turns, typename Turn, typename Operation>
183 void visit_traverse_reject(Turns const& turns, Turn const& turn, Operation const& op,
184 bg::detail::overlay::traverse_error_type error)
190 std::cout << "Reject turn " << stream_turn_index(turns, turn, op)
191 << bg::operation_char(turn.operations[0].operation)
192 << bg::operation_char(turn.operations[1].operation)
193 << " (" << bg::operation_char(op.operation) << ")"
194 << " " << bg::detail::overlay::traverse_error_string(error) << std::endl;
197 std::string style = "fill:rgb(255,0,0);font-family:Arial;font-size:7px";
198 stream(turns, turn, op, bg::detail::overlay::traverse_error_string(error), style);
203 template <typename Turns, typename Turn, typename Operation>
204 void visit_traverse_select_turn_from_cluster(Turns const& turns, Turn const& turn, Operation const& op)
206 std::cout << "Visit turn from cluster " << stream_turn_index(turns, turn, op)
207 << " " << bg::operation_char(turn.operations[0].operation)
208 << bg::operation_char(turn.operations[1].operation)
209 << " (" << bg::operation_char(op.operation) << ")"
214 template <typename Turns, typename Turn, typename Operation>
215 void stream(Turns const& turns, Turn const& turn, Operation const& op, const std::string& header, const std::string& style)
217 std::ostringstream out;
218 out << m_traverse_seq++ << " " << header
219 << " " << stream_turn_index(turns, turn, op);
221 out << " " << bg::visited_char(op.visited);
223 add_text(turn, out.str(), style);
226 template <typename Turn>
227 bool label_operation(Turn const& turn, int index, std::ostream& os)
229 os << bg::operation_char(turn.operations[index].operation);
231 if (! turn.discarded)
233 if (turn.operations[index].enriched.next_ip_index != -1)
235 os << "->" << turn.operations[index].enriched.next_ip_index;
236 if (turn.operations[index].enriched.next_ip_index != -1)
243 os << "->" << turn.operations[index].enriched.travels_to_ip_index;
244 if (turn.operations[index].enriched.travels_to_ip_index != -1)
250 os << " {" << turn.operations[index].enriched.region_id
251 << (turn.operations[index].enriched.isolated ? " ISO" : "")
254 if (! turn.operations[index].enriched.startable)
263 template <typename Turn>
264 void label_turn(int index, Turn const& turn)
266 std::ostringstream out;
268 if (turn.cluster_id != -1)
270 out << " c=" << turn.cluster_id << " ";
272 bool lab1 = label_operation(turn, 0, out);
274 bool lab2 = label_operation(turn, 1, out);
279 if (turn.has_colocated_both)
283 bool const self_turn = bg::detail::overlay::is_self_turn<bg::overlay_union>(turn);
289 std::string font8 = "font-family:Arial;font-size:6px";
290 std::string font6 = "font-family:Arial;font-size:4px";
291 std::string style = "fill:rgb(0,0,255);" + font8;
296 style = "fill:rgb(128,28,128);" + font6;
300 style = "fill:rgb(255,0,255);" + font8;
303 else if (turn.discarded)
305 style = "fill:rgb(92,92,92);" + font6;
307 else if (turn.cluster_id != -1)
309 style = "fill:rgb(0,0,255);" + font8;
311 else if (! lab1 || ! lab2)
313 style = "fill:rgb(0,0,255);" + font6;
316 add_text(turn, out.str(), style);
319 template <typename Turn>
320 void add_text(Turn const& turn, std::string const& text, std::string const& style)
322 int const margin = 5;
323 int const lineheight = 6;
324 double const half = 0.5;
325 double const ten = 10;
327 // Map characteristics
328 // Create a rounded off point
329 std::pair<int, int> p
331 boost::numeric_cast<int>(half
332 + ten * bg::get<0>(turn.point)),
333 boost::numeric_cast<int>(half
334 + ten * bg::get<1>(turn.point))
336 m_mapper.text(turn.point, text, style, margin, m_offsets[p], lineheight);
337 m_offsets[p] += lineheight;
342 std::map<std::pair<int, int>, int> m_offsets;
349 template <typename Geometry, bg::overlay_type OverlayType>
350 void test_overlay(std::string const& caseid,
351 std::string const& wkt1, std::string const& wkt2,
352 double expected_area,
353 std::size_t expected_clip_count,
354 std::size_t expected_hole_count)
357 bg::read_wkt(wkt1, g1);
360 bg::read_wkt(wkt2, g2);
362 // Reverse if necessary
366 #if defined(TEST_WITH_SVG)
367 bool const ccw = bg::point_order<Geometry>::value == bg::counterclockwise;
368 bool const open = bg::closure<Geometry>::value == bg::open;
370 std::ostringstream filename;
371 filename << "overlay"
373 << "_" << string_from_type<typename bg::coordinate_type<Geometry>::type>::name()
374 << (ccw ? "_ccw" : "")
375 << (open ? "_open" : "")
376 #if defined(BOOST_GEOMETRY_USE_RESCALING)
381 std::ofstream svg(filename.str().c_str());
383 typedef bg::svg_mapper<typename bg::point_type<Geometry>::type> svg_mapper;
385 svg_mapper mapper(svg, 500, 500);
389 // Input shapes in green (src=0) / blue (src=1)
390 mapper.map(g1, "fill-opacity:0.5;fill:rgb(153,204,0);"
391 "stroke:rgb(153,204,0);stroke-width:3");
392 mapper.map(g2, "fill-opacity:0.3;fill:rgb(51,51,153);"
393 "stroke:rgb(51,51,153);stroke-width:3");
397 typedef typename boost::range_value<Geometry>::type geometry_out;
398 typedef bg::detail::overlay::overlay
401 bg::detail::overlay::do_reverse<bg::point_order<Geometry>::value>::value,
402 OverlayType == bg::overlay_difference
403 ? ! bg::detail::overlay::do_reverse<bg::point_order<Geometry>::value>::value
404 : bg::detail::overlay::do_reverse<bg::point_order<Geometry>::value>::value,
405 bg::detail::overlay::do_reverse<bg::point_order<Geometry>::value>::value,
410 typedef typename bg::strategy::intersection::services::default_strategy
412 typename bg::cs_tag<Geometry>::type
413 >::type strategy_type;
415 strategy_type strategy;
417 typedef typename bg::rescale_overlay_policy_type
421 >::type rescale_policy_type;
423 rescale_policy_type robust_policy
424 = bg::get_rescale_policy<rescale_policy_type>(g1, g2);
426 #if defined(TEST_WITH_SVG)
427 map_visitor<svg_mapper> visitor(mapper);
429 bg::detail::overlay::overlay_null_visitor visitor;
433 overlay::apply(g1, g2, robust_policy, std::back_inserter(result),
437 bool const valid = check_validity<Geometry>::apply(result, caseid, g1, g2, message);
438 BOOST_CHECK_MESSAGE(valid,
439 "overlay: " << caseid << " not valid: " << message
440 << " type: " << (type_for_assert_message<Geometry, Geometry>()));
442 BOOST_CHECK_CLOSE(bg::area(result), expected_area, 0.001);
443 BOOST_CHECK_MESSAGE((bg::num_interior_rings(result) == expected_hole_count),
445 << " hole count: detected: " << bg::num_interior_rings(result)
446 << " expected: " << expected_hole_count);
447 BOOST_CHECK_MESSAGE((result.size() == expected_clip_count),
449 << " clip count: detected: " << result.size()
450 << " expected: " << expected_clip_count);
452 #if defined(TEST_WITH_SVG)
453 mapper.map(result, "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);"
454 "stroke:rgb(255,0,255);stroke-width:8");
459 #define TEST_INTERSECTION(caseid, area, clips, holes) (test_overlay<multi_polygon, bg::overlay_intersection>) \
460 ( #caseid "_int", caseid[0], caseid[1], area, clips, holes)
461 #define TEST_UNION(caseid, area, clips, holes) (test_overlay<multi_polygon, bg::overlay_union>) \
462 ( #caseid "_union", caseid[0], caseid[1], area, clips, holes)
463 #define TEST_DIFFERENCE_A(caseid, area, clips, holes) (test_overlay<multi_polygon, bg::overlay_difference>) \
464 ( #caseid "_diff_a", caseid[0], caseid[1], area, clips, holes)
465 #define TEST_DIFFERENCE_B(caseid, area, clips, holes) (test_overlay<multi_polygon, bg::overlay_difference>) \
466 ( #caseid "_diff_b", caseid[1], caseid[0], area, clips, holes)
468 #define TEST_INTERSECTION_WITH(caseid, index1, index2, area, clips, holes) (test_overlay<multi_polygon, bg::overlay_intersection>) \
469 ( #caseid "_int_" #index1 "_" #index2, caseid[index1], caseid[index2], area, clips, holes)
470 #define TEST_UNION_WITH(caseid, index1, index2, area, clips, holes) (test_overlay<multi_polygon, bg::overlay_union>) \
471 ( #caseid "_union" #index1 "_" #index2, caseid[index1], caseid[index2], area, clips, holes)
473 template <typename T, bool Clockwise>
476 typedef bg::model::point<T, 2, bg::cs::cartesian> point_type;
477 typedef bg::model::polygon<point_type, Clockwise> polygon;
478 typedef bg::model::multi_polygon<polygon> multi_polygon;
480 TEST_UNION(case_multi_simplex, 14.58, 1, 0);
481 TEST_INTERSECTION(case_multi_simplex, 6.42, 2, 0);
483 TEST_DIFFERENCE_A(case_multi_simplex, 5.58, 5, 0);
484 TEST_DIFFERENCE_B(case_multi_simplex, 2.58, 4, 0);
486 // Contains 5 clusters, needing immediate selection of next turn
487 TEST_UNION_WITH(case_58_multi, 0, 3, 19.8333333, 2, 0);
489 // Contains many clusters, needing to exclude u/u turns
490 TEST_UNION(case_recursive_boxes_3, 56.5, 17, 6);
492 // Contains 4 clusters, one of which having 4 turns
493 TEST_UNION(case_recursive_boxes_7, 7.0, 2, 0);
495 // Contains 5 clusters, needing immediate selection of next turn
496 TEST_UNION(case_89_multi, 6.0, 1, 0);
498 // Needs ux/next_turn_index==-1 to be filtered out
499 TEST_INTERSECTION(case_77_multi, 9.0, 5, 0);
500 TEST_UNION(case_101_multi, 22.25, 1, 3);
501 TEST_INTERSECTION(case_101_multi, 4.75, 4, 0);
503 TEST_INTERSECTION(case_recursive_boxes_11, 1.0, 2, 0);
504 TEST_INTERSECTION(case_recursive_boxes_30, 6.0, 4, 0);
506 TEST_UNION(case_recursive_boxes_4, 96.75, 1, 2);
507 TEST_INTERSECTION_WITH(case_58_multi, 2, 6, 13.25, 1, 1);
508 TEST_INTERSECTION_WITH(case_72_multi, 1, 2, 6.15, 3, 1);
509 TEST_UNION(case_recursive_boxes_12, 6.0, 6, 0);
510 TEST_UNION(case_recursive_boxes_13, 10.25, 3, 0);
515 // << bg::inverse<multi_polygon>(case_65_multi[0], 1.0)
516 // << "\"" << std::endl;
519 int test_main(int, char* [])
521 test_all<double, true>();
522 // test_all<double, false>();