1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
//
// Test Suite for geos::algorithm::construct::LargestEmptyCircle
#include <tut/tut.hpp>
// geos
#include <geos/algorithm/construct/LargestEmptyCircle.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/PrecisionModel.h>
#include <geos/io/WKTReader.h>
#include <geos/constants.h>
// std
#include <string>
#include <memory>
using namespace geos::geom;
using geos::algorithm::construct::LargestEmptyCircle;
namespace tut {
//
// Test Group
//
struct test_data_LargestEmptyCircle {
PrecisionModel pm_;
GeometryFactory::Ptr factory_;
geos::io::WKTReader reader_;
test_data_LargestEmptyCircle():
pm_(PrecisionModel::FLOATING),
factory_(GeometryFactory::create(&pm_, 0)),
reader_(factory_.get())
{}
~test_data_LargestEmptyCircle()
{
}
void
ensure_equals_coordinate(const Coordinate &lhs,
const Coordinate &rhs, double tolerance)
{
ensure_equals("x coordinate does not match", lhs.x, rhs.x, tolerance);
ensure_equals("y coordinate does not match", lhs.y, rhs.y, tolerance);
}
/**
* A coarse distance check, mainly testing
* that there is not a huge number of iterations.
* (This will be revealed by CI taking a very long time!)
*/
void
checkCircle(std::string wktObstacles, double tolerance)
{
std::unique_ptr<Geometry> geom(reader_.read(wktObstacles));
LargestEmptyCircle lec(geom.get(), tolerance);
std::unique_ptr<Point> centerPoint = lec.getCenter();
double dist = geom->distance(centerPoint.get());
//std::cout << dist << std::endl;
std::unique_ptr<LineString> radiusLine = lec.getRadiusLine();
double actualRadius = radiusLine->getLength();
ensure(std::abs(actualRadius - dist) < 2 * tolerance);
}
void
checkCircle(const Geometry *obstacles, const Geometry *boundary, double build_tolerance, double x, double y, double expectedRadius)
{
double tolerance = 2*build_tolerance;
LargestEmptyCircle lec(obstacles, boundary, tolerance);
std::unique_ptr<Point> centerPoint = lec.getCenter();
Coordinate centerPt(*centerPoint->getCoordinate());
Coordinate expectedCenter(x, y);
ensure_equals_coordinate(centerPt, expectedCenter, tolerance);
std::unique_ptr<LineString> radiusLine = lec.getRadiusLine();
double actualRadius = radiusLine->getLength();
ensure_equals("radius", actualRadius, expectedRadius, tolerance);
const Coordinate& linePt0 = radiusLine->getCoordinateN(0);
const Coordinate& linePt1 = radiusLine->getCoordinateN(1);
// std::cout << std::endl;
// std::cout << writer_.write(geom) << std::endl;
// std::cout << writer_.write(radiusLine.get()) << std::endl;
ensure_equals_coordinate(centerPt, linePt0, tolerance);
Coordinate radiusPt(*lec.getRadiusPoint()->getCoordinate());
ensure_equals_coordinate(radiusPt, linePt1, tolerance);
}
void
checkCircleZeroRadius(const Geometry *geom, double tolerance)
{
LargestEmptyCircle lec(geom, tolerance);
std::unique_ptr<Point> centerPoint = lec.getCenter();
Coordinate centerPt(*centerPoint->getCoordinate());
std::unique_ptr<LineString> radiusLine = lec.getRadiusLine();
double actualRadius = radiusLine->getLength();
ensure_equals("radius", actualRadius, 0.0, tolerance);
const Coordinate& linePt0 = radiusLine->getCoordinateN(0);
const Coordinate& linePt1 = radiusLine->getCoordinateN(1);
ensure_equals_coordinate(centerPt, linePt0, tolerance);
Coordinate radiusPt(*lec.getRadiusPoint()->getCoordinate());
ensure_equals_coordinate(radiusPt, linePt1, tolerance);
}
void
checkCircleZeroRadius(std::string wkt, double tolerance)
{
std::unique_ptr<Geometry> geom(reader_.read(wkt));
checkCircleZeroRadius(geom.get(), tolerance);
}
void
checkCircle(std::string wkt, double tolerance, double x, double y, double expectedRadius)
{
std::unique_ptr<Geometry> geom(reader_.read(wkt));
checkCircle(geom.get(), nullptr, tolerance, x, y, expectedRadius);
}
void
checkCircle(std::string wktObstacles, std::string wktBoundary, double tolerance, double x, double y, double expectedRadius)
{
std::unique_ptr<Geometry> obstacles(reader_.read(wktObstacles));
std::unique_ptr<Geometry> boundary(reader_.read(wktBoundary));
checkCircle(obstacles.get(), boundary.get(), tolerance, x, y, expectedRadius);
}
};
typedef test_group<test_data_LargestEmptyCircle> group;
typedef group::object object;
group test_group_LargestEmptyCircle("geos::algorithm::construct::LargestEmptyCircle");
// testPointsSquare
template<>
template<>
void object::test<1>()
{
checkCircle("MULTIPOINT ((100 100), (100 200), (200 200), (200 100))",
0.01, 150, 150, 70.71 );
}
// testPointsTriangleOnHull
template<>
template<>
void object::test<2>()
{
checkCircle("MULTIPOINT ((100 100), (300 100), (150 50))",
0.01, 216.66, 99.99, 83.33 );
}
// testPointsTriangleInterior
template<>
template<>
void object::test<3>()
{
checkCircle("MULTIPOINT ((100 100), (300 100), (200 250))",
0.01, 200.00, 141.66, 108.33 );
}
// testLinesOpenDiamond
template<>
template<>
void object::test<4>()
{
checkCircle("MULTILINESTRING ((50 100, 150 50), (250 50, 350 100), (350 150, 250 200), (50 150, 150 200))",
0.01, 200, 125, 90.13 );
}
// testLinesCrossed
template<>
template<>
void object::test<5>()
{
checkCircle("MULTILINESTRING ((100 100, 300 300), (100 200, 300 0))",
0.01, 299.99, 150.00, 106.05 );
}
// testLinesZigzag
template<>
template<>
void object::test<6>()
{
checkCircle("MULTILINESTRING ((100 100, 200 150, 100 200, 250 250, 100 300, 300 350, 100 400), (70 380, 0 350, 50 300, 0 250, 50 200, 0 150, 50 120))",
0.01, 77.52, 249.99, 54.81 );
}
// testPointsLinesTriangle
template<>
template<>
void object::test<7>()
{
checkCircle("GEOMETRYCOLLECTION (LINESTRING (100 100, 300 100), POINT (250 200))",
0.01, 196.49, 164.31, 64.31 );
}
// testPointsLinesTriangle
template<>
template<>
void object::test<8>()
{
checkCircleZeroRadius("POINT (100 100)",
0.01 );
}
// testLineFlat
template<>
template<>
void object::test<9>()
{
checkCircleZeroRadius("LINESTRING (0 0, 50 50)",
0.01 );
}
// testThinExtent
template<>
template<>
void object::test<10>()
{
checkCircle("MULTIPOINT ((100 100), (300 100), (200 100.1))",
0.01 );
}
//------------ Polygon Obstacles -----------------
// testPolygonConcave
template<>
template<>
void object::test<11>()
{
checkCircle("POLYGON ((1 9, 9 6, 6 5, 5 3, 8 3, 9 4, 9 1, 1 1, 1 9))",
0.01, 7.495, 4.216, 1.21);
}
// testPolygonsBoxes
template<>
template<>
void object::test<12>()
{
checkCircle("MULTIPOLYGON (((1 6, 6 6, 6 1, 1 1, 1 6)), ((6 7, 4 7, 4 9, 6 9, 6 7)))",
0.01, 2.50, 7.50, 1.50);
}
// testPolygonLines
template<>
template<>
void object::test<13>()
{
checkCircle("GEOMETRYCOLLECTION (POLYGON ((1 6, 6 6, 6 1, 1 1, 1 6)), LINESTRING (6 7, 3 9), LINESTRING (1 7, 3 8))",
0.01, 3.74, 7.14, 1.14);
}
//---------- Obstacles and Boundary --------------
// testBoundaryEmpty
template<>
template<>
void object::test<14>()
{
checkCircle("MULTIPOINT ((2 2), (8 8), (7 5))",
"POLYGON EMPTY",
0.01, 4.127, 4.127, 3 );
}
// testBoundarySquare
template<>
template<>
void object::test<15>()
{
checkCircle("MULTIPOINT ((2 2), (6 4), (8 8))",
"POLYGON ((1 9, 9 9, 9 1, 1 1, 1 9))",
0.01, 1.00390625, 8.99609375, 7.065 );
}
//testBoundarySquareObstaclesOutside
template<>
template<>
void object::test<16>()
{
checkCircle("MULTIPOINT ((10 10), (10 0))",
"POLYGON ((1 9, 9 9, 9 1, 1 1, 1 9))",
0.01, 1.0044, 4.997, 10.29 );
}
// testBoundaryMultiSquares
template<>
template<>
void object::test<17>()
{
checkCircle("MULTIPOINT ((10 10), (10 0), (5 5))",
"MULTIPOLYGON (((1 9, 9 9, 9 1, 1 1, 1 9)), ((15 20, 20 20, 20 15, 15 15, 15 20)))",
0.01, 19.995, 19.997, 14.137 );
}
// testBoundaryAsObstacle
template<>
template<>
void object::test<18>()
{
checkCircle("GEOMETRYCOLLECTION (LINESTRING (1 9, 9 9, 9 1, 1 1, 1 9), POINT (4 3), POINT (7 6))",
"POLYGON ((1 9, 9 9, 9 1, 1 1, 1 9))",
0.01, 4, 6, 3 );
}
// testObstacleEmptyElement
template<>
template<>
void object::test<19>()
{
checkCircle("GEOMETRYCOLLECTION (LINESTRING EMPTY, POINT (4 3), POINT (7 6), POINT (4 6))",
0.01, 5.5, 4.5, 2.12 );
}
// https://trac.osgeo.org/postgis/ticket/5626
template<>
template<>
void object::test<20>()
{
checkCircle(
"POINT(-11 40)", // input
"POLYGON((-71.1 42.3,-71.1 42.4,-71.2 42.3,-71.1 42.3))", // boundary
0.1, // test tolerance
-71.15, 42.34, 60.19588 ); // expected x, y, radius
}
// https://trac.osgeo.org/postgis/ticket/5626
template<>
template<>
void object::test<21>()
{
checkCircle(
"POINT(-11.1111111 40)", // input
"POLYGON((-71.0821 42.3036,-71.0821 42.3936,-71.0901 42.3036,-71.0821 42.3036))", // boundary
1, // test tolerance
-71.15, 42.34, 60.19588 ); // expected x, y, radius
}
} // namespace tut
|