|
| 1 | +// Boost.Geometry (aka GGL, Generic Geometry Library) |
| 2 | +// Robustness Test |
| 3 | + |
| 4 | +// Copyright (c) 2025 Tinko Bartels, Shenzhen, China. |
| 5 | + |
| 6 | +// Use, modification and distribution is subject to the Boost Software License, |
| 7 | +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 8 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 9 | + |
| 10 | +#define BOOST_GEOMETRY_NO_BOOST_TEST |
| 11 | + |
| 12 | +#include <bitset> |
| 13 | +#include <chrono> |
| 14 | +#include <iostream> |
| 15 | +#include <random> |
| 16 | +#include <utility> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include <boost/program_options.hpp> |
| 20 | + |
| 21 | +#include <boost/geometry/algorithms/difference.hpp> |
| 22 | +#include <boost/geometry/algorithms/union.hpp> |
| 23 | +#include <boost/geometry/algorithms/sym_difference.hpp> |
| 24 | +#include <boost/geometry/algorithms/intersection.hpp> |
| 25 | +#include <boost/geometry/algorithms/is_valid.hpp> |
| 26 | +#include <boost/geometry/io/wkt/write.hpp> |
| 27 | +#include <boost/geometry/geometries/geometries.hpp> |
| 28 | + |
| 29 | +#define BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE |
| 30 | +#ifndef BOOST_GEOMETRY_DEFAULT_TEST_TYPE |
| 31 | +#define BOOST_GEOMETRY_DEFAULT_TEST_TYPE int |
| 32 | +#endif |
| 33 | +#include <geometry_test_common.hpp> |
| 34 | + |
| 35 | +constexpr int chunk_size = 64; |
| 36 | +using bits = std::vector<std::bitset<chunk_size>>; |
| 37 | +using generator_t = std::mt19937_64; |
| 38 | +static_assert(sizeof(generator_t::result_type) >= chunk_size / 8, "Generator output too narrow."); |
| 39 | + |
| 40 | +namespace bg = boost::geometry; |
| 41 | + |
| 42 | +using point = bg::model::d2::point_xy<BOOST_GEOMETRY_DEFAULT_TEST_TYPE>; |
| 43 | +using box = bg::model::box<point>; |
| 44 | +using mp_t = bg::model::multi_polygon<bg::model::polygon<point>>; |
| 45 | + |
| 46 | +struct grid_settings |
| 47 | +{ |
| 48 | + int width = 5; |
| 49 | + int height = 5; |
| 50 | + int count = 1; |
| 51 | + generator_t::result_type seed = generator_t::default_seed; |
| 52 | + bool verbose = false; |
| 53 | + std::vector<std::uint64_t> bits1{}; |
| 54 | + std::vector<std::uint64_t> bits2{}; |
| 55 | +}; |
| 56 | + |
| 57 | +constexpr int cell_dimension = 2; |
| 58 | + |
| 59 | +std::vector<box> grid_cells(grid_settings const& settings) |
| 60 | +{ |
| 61 | + std::vector<box> out; |
| 62 | + out.reserve(settings.height * settings.width); |
| 63 | + for (int y = 0; y < settings.height; ++y) |
| 64 | + { |
| 65 | + for (int x = 0; x < settings.width; ++x) |
| 66 | + { |
| 67 | + out.push_back(box{point(x * cell_dimension, y * cell_dimension), |
| 68 | + point((x + 1) * cell_dimension, (y + 1) * cell_dimension)}); |
| 69 | + } |
| 70 | + } |
| 71 | + return out; |
| 72 | +} |
| 73 | + |
| 74 | +std::vector<point> test_points(grid_settings const& settings) |
| 75 | +{ |
| 76 | + std::vector<point> out; |
| 77 | + out.reserve(settings.height * settings.width); |
| 78 | + for (int y = 0; y < settings.height; ++y) |
| 79 | + { |
| 80 | + for (int x = 0; x < settings.width; ++x) |
| 81 | + { |
| 82 | + out.push_back(point(x * cell_dimension + cell_dimension / 2, |
| 83 | + y * cell_dimension + cell_dimension / 2)); |
| 84 | + } |
| 85 | + } |
| 86 | + return out; |
| 87 | +} |
| 88 | + |
| 89 | +std::ostream& operator<<(std::ostream& os, std::pair<bits, grid_settings> const& b_gs) |
| 90 | +{ |
| 91 | + if (b_gs.second.verbose) |
| 92 | + { |
| 93 | + os << '\n'; |
| 94 | + for (int y = b_gs.second.height - 1; y >= 0; --y) |
| 95 | + { |
| 96 | + for (int x = 0; x < b_gs.second.width; ++x) |
| 97 | + { |
| 98 | + int index = y * b_gs.second.width + x; |
| 99 | + os << b_gs.first[index / chunk_size][index % chunk_size]; |
| 100 | + } |
| 101 | + os << '\n'; |
| 102 | + } |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + os << '{' << b_gs.first[0].to_ullong(); |
| 107 | + for (size_t i = 1; i < b_gs.first.size(); ++i) os << ' ' << b_gs.first[i].to_ullong(); |
| 108 | + os << '}'; |
| 109 | + } |
| 110 | + return os; |
| 111 | +} |
| 112 | + |
| 113 | +bits geometry_to_bits(mp_t const& geometry, std::vector<point> const& test_points) |
| 114 | +{ |
| 115 | + bits out((test_points.size() + chunk_size - 1) / chunk_size); |
| 116 | + for (size_t i = 0; i < test_points.size(); ++i) |
| 117 | + { |
| 118 | + out[i / chunk_size][i % chunk_size] = bg::within(test_points[i], geometry); |
| 119 | + } |
| 120 | + return out; |
| 121 | +} |
| 122 | + |
| 123 | +mp_t bits_to_geometry(bits const& b, std::vector<box> const& grid, std::vector<point> const& points, |
| 124 | + grid_settings const& settings, std::map<std::string, int>& failures) |
| 125 | +{ |
| 126 | + mp_t out; |
| 127 | + for (size_t i = 0; i < grid.size(); ++i) |
| 128 | + { |
| 129 | + if (b[i / chunk_size][i % chunk_size]) |
| 130 | + { |
| 131 | + mp_t temp; |
| 132 | + bg::union_(out, grid[i], temp); |
| 133 | + out = std::move(temp); |
| 134 | + } |
| 135 | + } |
| 136 | + // Convenience lambda to pair bits with settings to use width/height in operator<<(os, ...) |
| 137 | + const auto b_gs = [&settings](bits const& b) { return std::make_pair(b, settings); }; |
| 138 | + std::string reason{}; |
| 139 | + if (! bg::is_valid(out, reason)) |
| 140 | + { |
| 141 | + if (settings.verbose) |
| 142 | + { |
| 143 | + std::cout << bg::wkt(out) << "\ngenerated from" << b_gs(b) |
| 144 | + << "is invalid: " << reason << ".\n\n"; |
| 145 | + } |
| 146 | + else std::cout << b_gs(b) << " invalid (" << reason << ")\n"; |
| 147 | + ++failures["bits_to_geometry validity"]; |
| 148 | + } |
| 149 | + if (geometry_to_bits(out, points) != b) |
| 150 | + { |
| 151 | + if (settings.verbose) |
| 152 | + { |
| 153 | + std::cout << "Generating grid from pattern" << b_gs(b) |
| 154 | + << "results in mismatching geometry: " << bg::wkt(out) << ".\n\n"; |
| 155 | + } |
| 156 | + else std::cout << b_gs(b) << " mismatch.\n"; |
| 157 | + ++failures["bits_to_geometry mismatch"]; |
| 158 | + } |
| 159 | + return out; |
| 160 | +} |
| 161 | + |
| 162 | +bits gen_bits(generator_t& generator, int bits_size) |
| 163 | +{ |
| 164 | + bits b((bits_size + chunk_size - 1) / chunk_size); |
| 165 | + std::generate(b.begin(), b.end(), std::ref(generator)); |
| 166 | + if (bits_size % chunk_size != 0) |
| 167 | + { |
| 168 | + std::bitset<chunk_size> bm; |
| 169 | + bm.set(); |
| 170 | + bm >>= chunk_size - bits_size % chunk_size; |
| 171 | + b.back() &= bm; |
| 172 | + } |
| 173 | + return b; |
| 174 | +} |
| 175 | + |
| 176 | +bits to_bits(std::vector<std::uint64_t> const& in) |
| 177 | +{ |
| 178 | + bits out; |
| 179 | + out.reserve(in.size()); |
| 180 | + for (auto const& ullong : in) out.push_back(std::bitset<chunk_size>(ullong)); |
| 181 | + return out; |
| 182 | +} |
| 183 | + |
| 184 | +template <typename BitOp> |
| 185 | +bits apply_for_each(bits a, bits const& b, BitOp const& bit_op) |
| 186 | +{ |
| 187 | + for (size_t i = 0; i < a.size(); ++i) a[i] = bit_op(a[i], b[i]); |
| 188 | + return a; |
| 189 | +} |
| 190 | + |
| 191 | +template<typename BitOp, typename GeoOp> |
| 192 | +void test_op(bits const& bits1, bits const& bits2, mp_t const& geo1, mp_t const& geo2, |
| 193 | + std::string const& op_label, BitOp const& bit_op, GeoOp const& geo_op, |
| 194 | + std::vector<point> const& test_points, std::vector<box> const& grid, |
| 195 | + grid_settings const& settings, std::map<std::string, int>& failures) |
| 196 | +{ |
| 197 | + auto test_geo = geo_op(geo1, geo2); |
| 198 | + // Convenience lambda to pair bits with settings to use width/height in operator<<(os, ...) |
| 199 | + const auto b_gs = [&settings](bits const& b) { return std::make_pair(b, settings); }; |
| 200 | + std::string reason{}; |
| 201 | + if (! bg::is_valid(test_geo, reason)) |
| 202 | + { |
| 203 | + if (settings.verbose) |
| 204 | + { |
| 205 | + std::cout << op_label << "(\n\t" << bg::wkt(geo1) << ",\n\t " << bg::wkt(geo2) << "\n)," |
| 206 | + << "\ngenerated from" << b_gs(bits1) << "and" << b_gs(bits2) << "is invalid: " |
| 207 | + << reason << ".\n\n"; |
| 208 | + } |
| 209 | + else |
| 210 | + { |
| 211 | + std::cout << op_label << '(' << b_gs(bits1) << ", " << b_gs(bits2) << " invalid (" |
| 212 | + << reason << ").\n"; |
| 213 | + } |
| 214 | + ++failures[op_label + " validity"]; |
| 215 | + } |
| 216 | + const bits expected = apply_for_each(bits1, bits2, bit_op); |
| 217 | + const bits obtained = geometry_to_bits(test_geo, test_points); |
| 218 | + if (obtained != expected) |
| 219 | + { |
| 220 | + if (settings.verbose) |
| 221 | + { |
| 222 | + std::cout << op_label << "(\n\t" << bg::wkt(geo1) << ",\n\t" << bg::wkt(geo2) << "\n)," |
| 223 | + << "\ngenerated from" << b_gs(bits1) << "and" << b_gs(bits2) |
| 224 | + << "is incorrect.\nExpected: " |
| 225 | + << bg::wkt(bits_to_geometry(expected, grid, test_points, settings, failures)) |
| 226 | + << "\ncorresponding to" << b_gs(expected) << "Obtained: " |
| 227 | + << bg::wkt(test_geo) << "\ncorresponding to" << b_gs(obtained) << "\n"; |
| 228 | + } |
| 229 | + else std::cout << op_label << '(' << b_gs(bits1) << ", " << b_gs(bits2) << ") mismatch.\n"; |
| 230 | + ++failures[op_label + " mismatch"]; |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +void test_bits(bits const& bits1, bits const& bits2, |
| 235 | + std::vector<box> const& grid, std::vector<point> const& test_points, |
| 236 | + grid_settings const& settings, std::map<std::string, int>& failures) |
| 237 | +{ |
| 238 | + const auto geo1 = bits_to_geometry(bits1, grid, test_points, settings, failures); |
| 239 | + const auto geo2 = bits_to_geometry(bits2, grid, test_points, settings, failures); |
| 240 | + test_op(bits1, bits2, geo1, geo2, "union", std::bit_or<>{}, |
| 241 | + [](mp_t const& g1, mp_t const& g2) { mp_t g; bg::union_(g1, g2, g); return g; }, |
| 242 | + test_points, grid, settings, failures); |
| 243 | + test_op(bits1, bits2, geo1, geo2, "intersection", std::bit_and<>{}, |
| 244 | + [](mp_t const& g1, mp_t const& g2) { mp_t g; bg::intersection(g1, g2, g); return g; }, |
| 245 | + test_points, grid, settings, failures); |
| 246 | + test_op(bits1, bits2, geo1, geo2, "sym_difference", std::bit_xor<>{}, |
| 247 | + [](mp_t const& g1, mp_t const& g2) { mp_t g; bg::sym_difference(g1, g2, g); return g; }, |
| 248 | + test_points, grid, settings, failures); |
| 249 | + test_op(bits1, bits2, geo1, geo2, "difference g1 \\ g2", |
| 250 | + [](std::bitset<chunk_size> b1, std::bitset<chunk_size> b2) { return b1 & (~b2); }, |
| 251 | + [](mp_t const& g1, mp_t const& g2) { mp_t g; bg::difference(g1, g2, g); return g; }, |
| 252 | + test_points, grid, settings, failures); |
| 253 | + test_op(bits1, bits2, geo1, geo2, "difference g2 \\ g1", |
| 254 | + [](std::bitset<chunk_size> b1, std::bitset<chunk_size> b2) { return b2 & (~b1); }, |
| 255 | + [](mp_t const& g1, mp_t const& g2) { mp_t g; bg::difference(g2, g1, g); return g; }, |
| 256 | + test_points, grid, settings, failures); |
| 257 | +} |
| 258 | + |
| 259 | +bool test_all(grid_settings const& settings) |
| 260 | +{ |
| 261 | + generator_t genenerator(settings.seed); |
| 262 | + const auto grid = grid_cells(settings); |
| 263 | + const auto points = test_points(settings); |
| 264 | + std::map<std::string, int> failures; |
| 265 | + auto const t0 = std::chrono::high_resolution_clock::now(); |
| 266 | + for (int i = 0; i < settings.count || settings.count == -1; i++) |
| 267 | + { |
| 268 | + const bits bits1 = settings.bits1.size() == 0 ? |
| 269 | + gen_bits(genenerator, settings.width * settings.height) |
| 270 | + : to_bits(settings.bits1); |
| 271 | + const bits bits2 = settings.bits2.size() == 0 ? |
| 272 | + gen_bits(genenerator, settings.width * settings.height) |
| 273 | + : to_bits(settings.bits2); |
| 274 | + test_bits(bits1, bits2, grid, points, settings, failures); |
| 275 | + } |
| 276 | + auto const t = std::chrono::high_resolution_clock::now(); |
| 277 | + auto const elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t - t0).count(); |
| 278 | + int failure_count = std::accumulate(failures.begin(), failures.end(), 0, |
| 279 | + [](int acc, auto const& kv) { return acc + kv.second; }); |
| 280 | + std::cout << "\niterations: " << settings.count |
| 281 | + << " errors: " << failure_count |
| 282 | + << " time: " << elapsed_ms / 1000.0 << '\n'; |
| 283 | + if (failure_count != 0) |
| 284 | + { |
| 285 | + std::cout << "Failure counts by failure mode:\n"; |
| 286 | + for (auto const& fm : failures) std::cout << '\t' << fm.first << ": " << fm.second << '\n'; |
| 287 | + } |
| 288 | + return failure_count != 0; |
| 289 | +} |
| 290 | + |
| 291 | +bool validate_bits_input(std::vector<std::uint64_t> const& bits_in, size_t bits_size) |
| 292 | +{ |
| 293 | + if (bits_in.size() == 0) return true; |
| 294 | + if (bits_in.size() != (bits_size + chunk_size - 1) / chunk_size) return false; |
| 295 | + if (bits_size % chunk_size != 0) |
| 296 | + { |
| 297 | + std::bitset<chunk_size> bm; |
| 298 | + bm.set(); |
| 299 | + bm >>= chunk_size - bits_size % chunk_size; |
| 300 | + if (bits_in.back() & ~bm.to_ullong()) return false; |
| 301 | + } |
| 302 | + return true; |
| 303 | +} |
| 304 | + |
| 305 | +int main(int argc, char** argv) |
| 306 | +{ |
| 307 | + BoostGeometryWriteTestConfiguration(); |
| 308 | + try |
| 309 | + { |
| 310 | + namespace po = boost::program_options; |
| 311 | + po::options_description description("=== random_integer_grids ===\nAllowed options"); |
| 312 | + |
| 313 | + grid_settings settings; |
| 314 | + |
| 315 | + description.add_options() |
| 316 | + ("help", "Help message") |
| 317 | + ("seed", |
| 318 | + po::value<decltype(settings.seed)>(&settings.seed)->default_value(settings.seed), |
| 319 | + "Initialization seed for random generator") |
| 320 | + ("count", |
| 321 | + po::value<decltype(settings.count)>(&settings.count)->default_value(settings.count), |
| 322 | + "Number of tests (-1 for infinite loop)") |
| 323 | + ("width", |
| 324 | + po::value<decltype(settings.width)>(&settings.width)->default_value(settings.width), |
| 325 | + "Width of grid (>= 1)") |
| 326 | + ("height", |
| 327 | + po::value<decltype(settings.height)>(&settings.height)->default_value(settings.height), |
| 328 | + "Height of grid (>= 1)") |
| 329 | + ("verbose", |
| 330 | + po::bool_switch(&settings.verbose), |
| 331 | + "Print WKT and bit patterns for each failure.") |
| 332 | + ("bits1", |
| 333 | + po::value<decltype(settings.bits1)>(&settings.bits1)->multitoken(), |
| 334 | + "Fixed bit pattern for first operand as list of ullong.") |
| 335 | + ("bits2", |
| 336 | + po::value<decltype(settings.bits2)>(&settings.bits2)->multitoken(), |
| 337 | + "Fixed bit pattern for second operand as list of ullong.") |
| 338 | + ; |
| 339 | + |
| 340 | + po::variables_map varmap; |
| 341 | + po::store(po::parse_command_line(argc, argv, description), varmap); |
| 342 | + po::notify(varmap); |
| 343 | + |
| 344 | + if (! validate_bits_input(settings.bits1, settings.height * settings.width)) |
| 345 | + { |
| 346 | + std::cout << "bits1 was provided but does not match dimensions.\n"; |
| 347 | + return 1; |
| 348 | + } |
| 349 | + if (! validate_bits_input(settings.bits2, settings.height * settings.width)) |
| 350 | + { |
| 351 | + std::cout << "bits2 was provided but does not match dimensions.\n"; |
| 352 | + return 1; |
| 353 | + } |
| 354 | + if (settings.bits1.size() != 0 && settings.bits2.size() != 0 && settings.count != 1) |
| 355 | + { |
| 356 | + std::cout << "Both bit patterns fixed, count is changed to 1.\n"; |
| 357 | + settings.count = 1; |
| 358 | + } |
| 359 | + if (settings.height < 1 || settings.width < 1) |
| 360 | + { |
| 361 | + std::cout << "Invalid dimensions, height and width need to be positive.\n"; |
| 362 | + return 1; |
| 363 | + } |
| 364 | + if (varmap.count("help")) |
| 365 | + { |
| 366 | + std::cout << description << std::endl; |
| 367 | + return 1; |
| 368 | + } |
| 369 | + if (! test_all(settings)) return 1; |
| 370 | + } |
| 371 | + catch(std::exception const& e) |
| 372 | + { |
| 373 | + std::cout << "Exception " << e.what() << '\n'; |
| 374 | + } |
| 375 | + catch(...) |
| 376 | + { |
| 377 | + std::cout << "Other exception" << '\n'; |
| 378 | + } |
| 379 | + |
| 380 | + return 0; |
| 381 | +} |
0 commit comments