|
| 1 | +/********************************************************************** |
| 2 | + * |
| 3 | + * GEOS - Geometry Engine Open Source |
| 4 | + * http://geos.osgeo.org |
| 5 | + * |
| 6 | + * Copyright (C) 2025 ISciences LLC |
| 7 | + * |
| 8 | + * This is free software; you can redistribute and/or modify it under |
| 9 | + * the terms of the GNU Lesser General Public Licence as published |
| 10 | + * by the Free Software Foundation. |
| 11 | + * See the COPYING file for more information. |
| 12 | + * |
| 13 | + **********************************************************************/ |
| 14 | + |
| 15 | +#include <benchmark/benchmark.h> |
| 16 | +#include <BenchmarkUtils.h> |
| 17 | + |
| 18 | +#include <geos/geom/Envelope.h> |
| 19 | +#include <geos/geom/prep/PreparedGeometryFactory.h> |
| 20 | +#include <geos/operation/grid/Grid.h> |
| 21 | + |
| 22 | +#include <geos/operation/grid/GridIntersection.h> |
| 23 | +#include <geos/operation/intersection/Rectangle.h> |
| 24 | +#include <geos/operation/intersection/RectangleIntersection.h> |
| 25 | + |
| 26 | +using geos::geom::CoordinateXY; |
| 27 | +using geos::geom::Envelope; |
| 28 | +using geos::geom::Geometry; |
| 29 | +using Grid = geos::operation::grid::Grid<geos::operation::grid::bounded_extent>; |
| 30 | + |
| 31 | +template<bool AreaOnly> |
| 32 | +struct GridIntersection { |
| 33 | + static double Intersection(const Envelope& env, int nx, int ny, const Geometry& g) { |
| 34 | + Grid grid(env, env.getWidth() / nx, env.getHeight() / ny); |
| 35 | + if constexpr (AreaOnly) { |
| 36 | + auto result = geos::operation::grid::GridIntersection::grid_intersection(grid, g); |
| 37 | + float area = 0; |
| 38 | + for (std::size_t i = 0; i < result->rows(); i++) { |
| 39 | + for (std::size_t j = 0; j < result->cols(); j++) { |
| 40 | + area += (*result)(i, j); |
| 41 | + } |
| 42 | + } |
| 43 | + return static_cast<double>(area); |
| 44 | + } else { |
| 45 | + auto subdivided = geos::operation::grid::GridIntersection::subdivide_polygon(grid, g, true); |
| 46 | + return subdivided->getArea(); |
| 47 | + } |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +using GridIntersectionAreaOnly = GridIntersection<true>; |
| 52 | +using GridIntersectionFull = GridIntersection<false>; |
| 53 | + |
| 54 | +template<bool UseRectangleIntersection> |
| 55 | +struct SingleIntersection { |
| 56 | + |
| 57 | + static double Intersection(const Envelope& env, int nx, int ny, const Geometry& g) { |
| 58 | + double dx = env.getWidth() / nx; |
| 59 | + double dy = env.getHeight() / ny; |
| 60 | + |
| 61 | + double x0 = env.getMinX(); |
| 62 | + double y0 = env.getMinY(); |
| 63 | + |
| 64 | + const auto& gfact = *g.getFactory(); |
| 65 | + auto prepGeom = geos::geom::prep::PreparedGeometryFactory::prepare(&g); |
| 66 | + |
| 67 | + double area = 0; |
| 68 | + |
| 69 | + for (int i = 0; i < nx; i++) { |
| 70 | + for (int j = 0; j < ny; j++) { |
| 71 | + Envelope subEnv(x0 + i*dx, x0 + (i+1)*dx, y0 + j*dy, y0 + (j+1)*dy); |
| 72 | + auto cellGeom = gfact.toGeometry(&subEnv); |
| 73 | + if (!prepGeom->intersects(cellGeom.get())) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + std::unique_ptr<Geometry> isect; |
| 78 | + if constexpr (UseRectangleIntersection) { |
| 79 | + geos::operation::intersection::Rectangle rect(x0 + i*dx, y0 + j*dy, x0 + (i+1)*dx, y0 + (j+1)*dy); |
| 80 | + isect = geos::operation::intersection::RectangleIntersection::clip(g, rect); |
| 81 | + } else { |
| 82 | + isect = g.intersection(cellGeom.get()); |
| 83 | + } |
| 84 | + |
| 85 | + area += isect->getArea(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return area; |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +using PolygonIntersection = SingleIntersection<false>; |
| 94 | +using RectangleIntersection = SingleIntersection<true>; |
| 95 | + |
| 96 | +template<typename Impl> |
| 97 | +static void BM_GridIntersection(benchmark::State& state) |
| 98 | +{ |
| 99 | + auto nCells = state.range(0); |
| 100 | + |
| 101 | + auto nx = static_cast<int>(std::ceil(std::sqrt(nCells))); |
| 102 | + auto ny = static_cast<int>(std::ceil(std::sqrt(nCells))); |
| 103 | + |
| 104 | + CoordinateXY center; |
| 105 | + Envelope env(0, nx, 0, ny); |
| 106 | + env.centre(center); |
| 107 | + |
| 108 | + auto geom = geos::benchmark::createSineStar(center, env.getWidth() / 2, 500); |
| 109 | + |
| 110 | + for (auto _ : state) { |
| 111 | + Impl::Intersection(env, nx, ny, *geom); |
| 112 | + } |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +BENCHMARK_TEMPLATE(BM_GridIntersection, GridIntersectionAreaOnly)->Range(1000, 1000000); |
| 117 | +BENCHMARK_TEMPLATE(BM_GridIntersection, GridIntersectionFull)->Range(1000, 1000000); |
| 118 | +BENCHMARK_TEMPLATE(BM_GridIntersection, RectangleIntersection)->Range(1000, 1000000); |
| 119 | +BENCHMARK_TEMPLATE(BM_GridIntersection, PolygonIntersection)->Range(1000, 1000000); |
| 120 | + |
| 121 | +BENCHMARK_MAIN(); |
0 commit comments