c1f34c4 Release v2.11.0 d3b72e0 Updated change log. 2982b8d Update embedded Protozero to version 1.5.1. cc1ab2a Add non-const WayNodeList::operator[]. 3da372e Add missing example to examples/README.md. 30604ba Add OSMIUM_USE_SLOW_MERCATOR_PROJECTION define. 47a92e0 Clearer CheckOrder handler doc. f11106d Formatting fixes. a870737 Use faster implementation of web mercator projection. 041bb42 Test cleanups. 8933bc5 Cleanup *Map::get() functions. 6b989ca Document that (Multipolygon)Collectors only work with unique Ids. 8fb5bd2 Updated included Protozero to version 1.5.0. 76e153d Removed Makefile. 35d7ec9 Update copyright date. a7f8126 Rename guard define to common scheme. a923c69 Cleanup I/O tests. d353993 Add Map::get_noexcept() method for all index maps. 94fa5ac Add const overload for mmap_vector_base::operator[]. 3cf9184 Add default constructed "invalid" Coordinates. 358f170 Add Tile constructor from web mercator coordinates. 006aa4c Add index::RelationsMap(Stash|Index) classes. 9cc842e Updated catch to v1.5.9. bd8c3b6 Use initializer_list trick instead of recursive template. 2c82a6f Merge pull request #183 from daniel-j-h/rvalue-apply 0bf5404 Implements rvalue handler support for apply, resolves #180. ccaab08 Merge pull request #182 from AMDmi3/freebsd-endianess bffe626 Handle endianess on FreeBSD properly 7250222 Code formatting and test cleanup. 6652436 Merge pull request #179 from oxidase/add_match_key_std_regex afadf5b Rename centroid variables and function in example. 8355284 Add envelope() functions to NodeRefList, Way, and Area. fc83d2e Remove unnecessary include. 9ddd00e Add match_key<std::regex> tag 9c54a53 Update README. Moved some infos to manual. 89a90a6 Update readme and developer docs. c3446ec Simplify subitem iteration code and made it more flexible. 542b07c Add some static_asserts. f0fd690 Memory reporting on M68k doesn't work properly. e8957c6 Compare doubles in test using Approx(). 58ae4a6 Add amenity_list example. 53783f8 Fix doxygen config for reproducible builds. de4e52d Release v2.10.3 0cc42a2 ObjectPointerCollection constructor can't be noexcept. 4472dfb Round out ObjectPointerCollection implementation and test it. 28cb35d Build with XCode 8 and GCC 6 on travis. 03e3e66 Upgrade to new protozero version 1.4.5. 2102c2f Add assertion in queue handling code. git-subtree-dir: third_party/libosmium git-subtree-split: c1f34c45507e233a2b9028663906679c610fe179
409 lines
15 KiB
C++
409 lines
15 KiB
C++
#ifndef OSMIUM_AREA_DETAIL_NODE_REF_SEGMENT_HPP
|
|
#define OSMIUM_AREA_DETAIL_NODE_REF_SEGMENT_HPP
|
|
|
|
/*
|
|
|
|
This file is part of Osmium (http://osmcode.org/libosmium).
|
|
|
|
Copyright 2013-2017 Jochen Topf <jochen@topf.org> and others (see README).
|
|
|
|
Boost Software License - Version 1.0 - August 17th, 2003
|
|
|
|
Permission is hereby granted, free of charge, to any person or organization
|
|
obtaining a copy of the software and accompanying documentation covered by
|
|
this license (the "Software") to use, reproduce, display, distribute,
|
|
execute, and transmit the Software, and to prepare derivative works of the
|
|
Software, and to permit third-parties to whom the Software is furnished to
|
|
do so, all subject to the following:
|
|
|
|
The copyright notices in the Software and this entire statement, including
|
|
the above license grant, this restriction and the following disclaimer,
|
|
must be included in all copies of the Software, in whole or in part, and
|
|
all derivative works of the Software, unless such copies or derivative
|
|
works are solely in the form of machine-executable object code generated by
|
|
a source language processor.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
|
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
|
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <iosfwd>
|
|
#include <utility>
|
|
|
|
#include <osmium/area/detail/vector.hpp>
|
|
#include <osmium/osm/location.hpp>
|
|
#include <osmium/osm/node_ref.hpp>
|
|
|
|
namespace osmium {
|
|
|
|
class Way;
|
|
|
|
namespace area {
|
|
|
|
/**
|
|
* @brief Namespace for Osmium internal use
|
|
*/
|
|
namespace detail {
|
|
|
|
class ProtoRing;
|
|
|
|
enum class role_type : uint8_t {
|
|
unknown = 0,
|
|
outer = 1,
|
|
inner = 2,
|
|
empty = 3
|
|
};
|
|
|
|
/**
|
|
* This helper class for the Assembler class models a segment,
|
|
* the connection between two nodes.
|
|
*
|
|
* Internally segments have their smaller coordinate at the
|
|
* beginning of the segment. Smaller, in this case, means smaller
|
|
* x coordinate, and, if they are the same, smaller y coordinate.
|
|
*/
|
|
class NodeRefSegment {
|
|
|
|
// First node in order described above.
|
|
osmium::NodeRef m_first;
|
|
|
|
// Second node in order described above.
|
|
osmium::NodeRef m_second;
|
|
|
|
// Way this segment was from.
|
|
const osmium::Way* m_way;
|
|
|
|
// The ring this segment is part of. Initially nullptr, this
|
|
// will be filled in once we know which ring the segment is in.
|
|
ProtoRing* m_ring;
|
|
|
|
// The role of this segment from the member role.
|
|
role_type m_role;
|
|
|
|
// Nodes have to be reversed to get the intended order.
|
|
bool m_reverse = false;
|
|
|
|
// We found the right direction for this segment in the ring.
|
|
// (This depends on whether it is an inner or outer ring.)
|
|
bool m_direction_done = false;
|
|
|
|
public:
|
|
|
|
NodeRefSegment() noexcept :
|
|
m_first(),
|
|
m_second(),
|
|
m_way(nullptr),
|
|
m_ring(nullptr),
|
|
m_role(role_type::unknown) {
|
|
}
|
|
|
|
NodeRefSegment(const osmium::NodeRef& nr1, const osmium::NodeRef& nr2, role_type role = role_type::unknown, const osmium::Way* way = nullptr) noexcept :
|
|
m_first(nr1),
|
|
m_second(nr2),
|
|
m_way(way),
|
|
m_ring(nullptr),
|
|
m_role(role) {
|
|
if (nr2.location() < nr1.location()) {
|
|
using std::swap;
|
|
swap(m_first, m_second);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The ring this segment is a part of. nullptr if we don't
|
|
* have the ring yet.
|
|
*/
|
|
ProtoRing* ring() const noexcept {
|
|
return m_ring;
|
|
}
|
|
|
|
/**
|
|
* Returns true if the segment has already been placed in a
|
|
* ring.
|
|
*/
|
|
bool is_done() const noexcept {
|
|
return m_ring != nullptr;
|
|
}
|
|
|
|
void set_ring(ProtoRing* ring) noexcept {
|
|
assert(ring);
|
|
m_ring = ring;
|
|
}
|
|
|
|
bool is_reverse() const noexcept {
|
|
return m_reverse;
|
|
}
|
|
|
|
void reverse() noexcept {
|
|
m_reverse = !m_reverse;
|
|
}
|
|
|
|
bool is_direction_done() const noexcept {
|
|
return m_direction_done;
|
|
}
|
|
|
|
void mark_direction_done() noexcept {
|
|
m_direction_done = true;
|
|
}
|
|
|
|
void mark_direction_not_done() noexcept {
|
|
m_direction_done = false;
|
|
}
|
|
|
|
/**
|
|
* Return first NodeRef of Segment according to sorting
|
|
* order (bottom left to top right).
|
|
*/
|
|
const osmium::NodeRef& first() const noexcept {
|
|
return m_first;
|
|
}
|
|
|
|
/**
|
|
* Return second NodeRef of Segment according to sorting
|
|
* order (bottom left to top right).
|
|
*/
|
|
const osmium::NodeRef& second() const noexcept {
|
|
return m_second;
|
|
}
|
|
|
|
/**
|
|
* Return real first NodeRef of Segment.
|
|
*/
|
|
const osmium::NodeRef& start() const noexcept {
|
|
return m_reverse ? m_second : m_first;
|
|
}
|
|
|
|
/**
|
|
* Return real second NodeRef of Segment.
|
|
*/
|
|
const osmium::NodeRef& stop() const noexcept {
|
|
return m_reverse ? m_first : m_second;
|
|
}
|
|
|
|
bool role_outer() const noexcept {
|
|
return m_role == role_type::outer;
|
|
}
|
|
|
|
bool role_inner() const noexcept {
|
|
return m_role == role_type::inner;
|
|
}
|
|
|
|
bool role_empty() const noexcept {
|
|
return m_role == role_type::empty;
|
|
}
|
|
|
|
const char* role_name() const noexcept {
|
|
static const char* names[] = { "unknown", "outer", "inner", "empty" };
|
|
return names[int(m_role)];
|
|
}
|
|
|
|
const osmium::Way* way() const noexcept {
|
|
return m_way;
|
|
}
|
|
|
|
/**
|
|
* The "determinant" of this segment. Used for calculating
|
|
* the winding order or a ring.
|
|
*/
|
|
int64_t det() const noexcept {
|
|
const vec a{start()};
|
|
const vec b{stop()};
|
|
return a * b;
|
|
}
|
|
|
|
}; // class NodeRefSegment
|
|
|
|
/// NodeRefSegments are equal if both their locations are equal
|
|
inline bool operator==(const NodeRefSegment& lhs, const NodeRefSegment& rhs) noexcept {
|
|
return lhs.first().location() == rhs.first().location() &&
|
|
lhs.second().location() == rhs.second().location();
|
|
}
|
|
|
|
inline bool operator!=(const NodeRefSegment& lhs, const NodeRefSegment& rhs) noexcept {
|
|
return ! (lhs == rhs);
|
|
}
|
|
|
|
/**
|
|
* A NodeRefSegment is "smaller" if the first point is to the
|
|
* left and down of the first point of the second segment.
|
|
* If both first points are the same, the segment with the higher
|
|
* slope comes first. If the slope is the same, the shorter
|
|
* segment comes first.
|
|
*/
|
|
inline bool operator<(const NodeRefSegment& lhs, const NodeRefSegment& rhs) noexcept {
|
|
if (lhs.first().location() == rhs.first().location()) {
|
|
const vec p0{lhs.first().location()};
|
|
const vec p1{lhs.second().location()};
|
|
const vec q0{rhs.first().location()};
|
|
const vec q1{rhs.second().location()};
|
|
const vec p = p1 - p0;
|
|
const vec q = q1 - q0;
|
|
|
|
if (p.x == 0 && q.x == 0) {
|
|
return p.y < q.y;
|
|
}
|
|
|
|
const auto a = p.y * q.x;
|
|
const auto b = q.y * p.x;
|
|
if (a == b) {
|
|
return p.x < q.x;
|
|
}
|
|
return a > b;
|
|
}
|
|
return lhs.first().location() < rhs.first().location();
|
|
}
|
|
|
|
inline bool operator>(const NodeRefSegment& lhs, const NodeRefSegment& rhs) noexcept {
|
|
return rhs < lhs;
|
|
}
|
|
|
|
inline bool operator<=(const NodeRefSegment& lhs, const NodeRefSegment& rhs) noexcept {
|
|
return ! (rhs < lhs);
|
|
}
|
|
|
|
inline bool operator>=(const NodeRefSegment& lhs, const NodeRefSegment& rhs) noexcept {
|
|
return ! (lhs < rhs);
|
|
}
|
|
|
|
template <typename TChar, typename TTraits>
|
|
inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const NodeRefSegment& segment) {
|
|
return out << segment.start() << "--" << segment.stop()
|
|
<< "[" << (segment.is_reverse() ? 'R' : '_')
|
|
<< (segment.is_done() ? 'd' : '_')
|
|
<< (segment.is_direction_done() ? 'D' : '_') << "]";
|
|
}
|
|
|
|
inline bool outside_x_range(const NodeRefSegment& s1, const NodeRefSegment& s2) noexcept {
|
|
if (s1.first().location().x() > s2.second().location().x()) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
inline bool y_range_overlap(const NodeRefSegment& s1, const NodeRefSegment& s2) noexcept {
|
|
const std::pair<int32_t, int32_t> m1 = std::minmax(s1.first().location().y(), s1.second().location().y());
|
|
const std::pair<int32_t, int32_t> m2 = std::minmax(s2.first().location().y(), s2.second().location().y());
|
|
if (m1.first > m2.second || m2.first > m1.second) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Calculate the intersection between two NodeRefSegments. The
|
|
* result is returned as a Location. Note that because the Location
|
|
* uses integers with limited precision internally, the result
|
|
* might be slightly different than the numerically correct
|
|
* location.
|
|
*
|
|
* This function uses integer arithmetic as much as possible and
|
|
* will not work if the segments are longer than about half the
|
|
* planet. This shouldn't happen with real data, so it isn't a big
|
|
* problem.
|
|
*
|
|
* If the segments touch in one or both of their endpoints, it
|
|
* doesn't count as an intersection.
|
|
*
|
|
* If the segments intersect not in a single point but in multiple
|
|
* points, ie if they are collinear and overlap, the smallest
|
|
* of the endpoints that is in the overlapping section is returned.
|
|
*
|
|
* @returns Undefined osmium::Location if there is no intersection
|
|
* or a defined Location if the segments intersect.
|
|
*/
|
|
inline osmium::Location calculate_intersection(const NodeRefSegment& s1, const NodeRefSegment& s2) noexcept {
|
|
// See http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
|
|
// for some hints about how the algorithm works.
|
|
const vec p0{s1.first()};
|
|
const vec p1{s1.second()};
|
|
const vec q0{s2.first()};
|
|
const vec q1{s2.second()};
|
|
|
|
if ((p0 == q0 && p1 == q1) ||
|
|
(p0 == q1 && p1 == q0)) {
|
|
// segments are the same
|
|
return osmium::Location();
|
|
}
|
|
|
|
const vec pd = p1 - p0;
|
|
const int64_t d = pd * (q1 - q0);
|
|
|
|
if (d != 0) {
|
|
// segments are not collinear
|
|
|
|
if (p0 == q0 || p0 == q1 || p1 == q0 || p1 == q1) {
|
|
// touching at an end point
|
|
return osmium::Location();
|
|
}
|
|
|
|
// intersection in a point
|
|
|
|
const int64_t na = (q1.x - q0.x) * (p0.y - q0.y) -
|
|
(q1.y - q0.y) * (p0.x - q0.x);
|
|
|
|
const int64_t nb = (p1.x - p0.x) * (p0.y - q0.y) -
|
|
(p1.y - p0.y) * (p0.x - q0.x);
|
|
|
|
if ((d > 0 && na >= 0 && na <= d && nb >= 0 && nb <= d) ||
|
|
(d < 0 && na <= 0 && na >= d && nb <= 0 && nb >= d)) {
|
|
const double ua = double(na) / d;
|
|
const vec i = p0 + ua * (p1 - p0);
|
|
return osmium::Location(int32_t(i.x), int32_t(i.y));
|
|
}
|
|
|
|
return osmium::Location();
|
|
}
|
|
|
|
// segments are collinear
|
|
|
|
if (pd * (q0 - p0) == 0) {
|
|
// segments are on the same line
|
|
|
|
struct seg_loc {
|
|
int segment;
|
|
osmium::Location location;
|
|
};
|
|
|
|
seg_loc sl[4];
|
|
sl[0] = {0, s1.first().location() };
|
|
sl[1] = {0, s1.second().location()};
|
|
sl[2] = {1, s2.first().location() };
|
|
sl[3] = {1, s2.second().location()};
|
|
|
|
std::sort(sl, sl+4, [](const seg_loc& lhs, const seg_loc& rhs) {
|
|
return lhs.location < rhs.location;
|
|
});
|
|
|
|
if (sl[1].location == sl[2].location) {
|
|
return osmium::Location();
|
|
}
|
|
|
|
if (sl[0].segment != sl[1].segment) {
|
|
if (sl[0].location == sl[1].location) {
|
|
return sl[2].location;
|
|
} else {
|
|
return sl[1].location;
|
|
}
|
|
}
|
|
}
|
|
|
|
return osmium::Location();
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace area
|
|
|
|
} // namespace osmium
|
|
|
|
#endif // OSMIUM_AREA_DETAIL_NODE_REF_SEGMENT_HPP
|