Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
box.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_OSM_BOX_HPP
2#define OSMIUM_OSM_BOX_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
37
38#include <cassert>
39#include <iosfwd>
40
41namespace osmium {
42
49 class Box {
50
51 osmium::Location m_bottom_left{};
52 osmium::Location m_top_right{};
53
54 public:
55
60 constexpr Box() noexcept = default;
61
67 Box(double minx, double miny, double maxx, double maxy) :
68 m_bottom_left(minx, miny),
69 m_top_right(maxx, maxy) {
70 assert(minx <= maxx && miny <= maxy);
71 }
72
82 Box(const osmium::Location& bottom_left, const osmium::Location& top_right) :
83 m_bottom_left(bottom_left),
84 m_top_right(top_right) {
85 assert(
86 (!!bottom_left && !!top_right) ||
87 (bottom_left.x() <= top_right.x() && bottom_left.y() <= top_right.y()));
88 }
89
99 Box& extend(const Location& location) noexcept {
100 if (location.valid()) {
101 if (m_bottom_left) {
102 if (location.x() < m_bottom_left.x()) {
103 m_bottom_left.set_x(location.x());
104 }
105 if (location.x() > m_top_right.x()) {
106 m_top_right.set_x(location.x());
107 }
108 if (location.y() < m_bottom_left.y()) {
109 m_bottom_left.set_y(location.y());
110 }
111 if (location.y() > m_top_right.y()) {
112 m_top_right.set_y(location.y());
113 }
114 } else {
115 m_bottom_left = location;
116 m_top_right = location;
117 }
118 }
119 return *this;
120 }
121
129 Box& extend(const Box& box) noexcept {
130 extend(box.bottom_left());
131 extend(box.top_right());
132 return *this;
133 }
134
138 explicit constexpr operator bool() const noexcept {
139 return static_cast<bool>(m_bottom_left) && static_cast<bool>(m_top_right);
140 }
141
146 constexpr bool valid() const noexcept {
147 return bottom_left().valid() && top_right().valid();
148 }
149
153 constexpr Location bottom_left() const noexcept {
154 return m_bottom_left;
155 }
156
160 Location& bottom_left() noexcept {
161 return m_bottom_left;
162 }
163
167 constexpr Location top_right() const noexcept {
168 return m_top_right;
169 }
170
174 Location& top_right() noexcept {
175 return m_top_right;
176 }
177
183 double left() const noexcept {
184 assert(valid());
185 return m_bottom_left.lon_without_check();
186 }
187
193 double right() const noexcept {
194 assert(valid());
195 return m_top_right.lon_without_check();
196 }
197
203 double top() const noexcept {
204 assert(valid());
205 return m_top_right.lat_without_check();
206 }
207
213 double bottom() const noexcept {
214 assert(valid());
215 return m_bottom_left.lat_without_check();
216 }
217
224 bool contains(const osmium::Location& location) const noexcept {
225 assert(bottom_left());
226 assert(top_right());
227 assert(location);
228 return location.x() >= bottom_left().x() && location.y() >= bottom_left().y() &&
229 location.x() <= top_right().x() && location.y() <= top_right().y();
230 }
231
240 double size() const {
241 return (m_top_right.lon() - m_bottom_left.lon()) *
242 (m_top_right.lat() - m_bottom_left.lat());
243 }
244
245 }; // class Box
246
251 inline constexpr bool operator==(const Box& lhs, const Box& rhs) noexcept {
252 return lhs.bottom_left() == rhs.bottom_left() &&
253 lhs.top_right() == rhs.top_right();
254 }
255
262 template <typename TChar, typename TTraits>
263 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const osmium::Box& box) {
264 if (box) {
265 out << '(';
266 box.bottom_left().as_string_without_check(std::ostream_iterator<char>(out));
267 out << ',';
268 box.top_right().as_string_without_check(std::ostream_iterator<char>(out));
269 out << ')';
270 } else {
271 out << "(undefined)";
272 }
273 return out;
274 }
275
276} // namespace osmium
277
278#endif // OSMIUM_OSM_BOX_HPP
Definition: location.hpp:271
constexpr int32_t x() const noexcept
Definition: location.hpp:377
constexpr bool valid() const noexcept
Definition: location.hpp:352
constexpr int32_t y() const noexcept
Definition: location.hpp:381
bool contains(const osmium::Box &lhs, const osmium::Box &rhs) noexcept
Definition: relations.hpp:46
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
bool operator==(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:440
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const item_type item_type)
Definition: item_type.hpp:187