Skip to content

Commit 75853d0

Browse files
committed
Move low-level geom functions into their own file and test them
1 parent fa21f55 commit 75853d0

File tree

6 files changed

+108
-16
lines changed

6 files changed

+108
-16
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(osm2pgsql_lib_SOURCES
55
dependency-manager.cpp
66
expire-tiles.cpp
77
gazetteer-style.cpp
8+
geom.cpp
89
geometry-processor.cpp
910
input.cpp
1011
logging.cpp

src/geom.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2020 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include "geom.hpp"
11+
12+
namespace geom {
13+
14+
double distance(osmium::geom::Coordinates p1,
15+
osmium::geom::Coordinates p2) noexcept
16+
{
17+
double const dx = p1.x - p2.x;
18+
double const dy = p1.y - p2.y;
19+
return std::sqrt(dx * dx + dy * dy);
20+
}
21+
22+
osmium::geom::Coordinates interpolate(osmium::geom::Coordinates p1,
23+
osmium::geom::Coordinates p2,
24+
double frac) noexcept
25+
{
26+
return osmium::geom::Coordinates{frac * (p1.x - p2.x) + p2.x,
27+
frac * (p1.y - p2.y) + p2.y};
28+
}
29+
30+
} // namespace geom
31+

src/geom.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef OSM2PGSQL_GEOM_HPP
2+
#define OSM2PGSQL_GEOM_HPP
3+
4+
/**
5+
* SPDX-License-Identifier: GPL-2.0-or-later
6+
*
7+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
8+
*
9+
* Copyright (C) 2006-2020 by the osm2pgsql developer community.
10+
* For a full list of authors see the git log.
11+
*/
12+
13+
/**
14+
* \file
15+
*
16+
* Low level geometry functions and types.
17+
*/
18+
19+
#include <osmium/geom/coordinates.hpp>
20+
21+
namespace geom {
22+
23+
double distance(osmium::geom::Coordinates p1,
24+
osmium::geom::Coordinates p2) noexcept;
25+
26+
osmium::geom::Coordinates interpolate(osmium::geom::Coordinates p1,
27+
osmium::geom::Coordinates p2,
28+
double frac) noexcept;
29+
30+
} // namespace geom
31+
32+
#endif // OSM2PGSQL_GEOM_HPP

src/osmium-builder.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,11 @@
1414

1515
#include <osmium/area/geom_assembler.hpp>
1616

17+
#include "geom.hpp"
1718
#include "osmium-builder.hpp"
1819

1920
namespace {
2021

21-
inline double distance(osmium::geom::Coordinates p1,
22-
osmium::geom::Coordinates p2)
23-
{
24-
double const dx = p1.x - p2.x;
25-
double const dy = p1.y - p2.y;
26-
return std::sqrt(dx * dx + dy * dy);
27-
}
28-
29-
inline osmium::geom::Coordinates interpolate(osmium::geom::Coordinates p1,
30-
osmium::geom::Coordinates p2,
31-
double frac) noexcept
32-
{
33-
return osmium::geom::Coordinates{frac * (p1.x - p2.x) + p2.x,
34-
frac * (p1.y - p2.y) + p2.y};
35-
}
36-
3722
template <typename ITERATOR>
3823
void add_nodes_to_builder(osmium::builder::WayNodeListBuilder &builder,
3924
ITERATOR const &begin, ITERATOR const &end,

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ set_test(test-db-copy-thread)
4343
set_test(test-db-copy-mgr)
4444
set_test(test-domain-matcher LABELS NoDB)
4545
set_test(test-expire-tiles LABELS NoDB)
46+
set_test(test-geom LABELS NoDB)
4647
set_test(test-middle)
4748
set_test(test-options-database LABELS NoDB)
4849
set_test(test-options-parse LABELS NoDB)

tests/test-geom.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2020 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include <catch.hpp>
11+
12+
#include "geom.hpp"
13+
14+
TEST_CASE("geom::distance", "[NoDB]")
15+
{
16+
osmium::geom::Coordinates const p1{10, 10};
17+
osmium::geom::Coordinates const p2{20, 10};
18+
osmium::geom::Coordinates const p3{13, 14};
19+
20+
REQUIRE(geom::distance(p1, p1) == Approx(0.0));
21+
REQUIRE(geom::distance(p1, p2) == Approx(10.0));
22+
REQUIRE(geom::distance(p1, p3) == Approx(5.0));
23+
}
24+
25+
TEST_CASE("geom::interpolate", "[NoDB]")
26+
{
27+
osmium::geom::Coordinates const p1{10, 10};
28+
osmium::geom::Coordinates const p2{20, 10};
29+
30+
auto const i1 = geom::interpolate(p1, p1, 0.5);
31+
REQUIRE(i1.x == 10);
32+
REQUIRE(i1.y == 10);
33+
34+
auto const i2 = geom::interpolate(p1, p2, 0.5);
35+
REQUIRE(i2.x == 15);
36+
REQUIRE(i2.y == 10);
37+
38+
auto const i3 = geom::interpolate(p2, p1, 0.5);
39+
REQUIRE(i3.x == 15);
40+
REQUIRE(i3.y == 10);
41+
}
42+

0 commit comments

Comments
 (0)