Libosmium  2.17.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 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <osmium/osm/location.hpp>
37 
38 #include <cassert>
39 #include <iosfwd>
40 
41 namespace 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  }
90 
100  Box& extend(const Location& location) noexcept {
101  if (location.valid()) {
102  if (m_bottom_left) {
103  if (location.x() < m_bottom_left.x()) {
104  m_bottom_left.set_x(location.x());
105  }
106  if (location.x() > m_top_right.x()) {
107  m_top_right.set_x(location.x());
108  }
109  if (location.y() < m_bottom_left.y()) {
110  m_bottom_left.set_y(location.y());
111  }
112  if (location.y() > m_top_right.y()) {
113  m_top_right.set_y(location.y());
114  }
115  } else {
116  m_bottom_left = location;
117  m_top_right = location;
118  }
119  }
120  return *this;
121  }
122 
130  Box& extend(const Box& box) noexcept {
131  extend(box.bottom_left());
132  extend(box.top_right());
133  return *this;
134  }
135 
139  explicit constexpr operator bool() const noexcept {
140  return bool(m_bottom_left) && bool(m_top_right);
141  }
142 
147  constexpr bool valid() const noexcept {
148  return bottom_left().valid() && top_right().valid();
149  }
150 
154  constexpr Location bottom_left() const noexcept {
155  return m_bottom_left;
156  }
157 
161  Location& bottom_left() noexcept {
162  return m_bottom_left;
163  }
164 
168  constexpr Location top_right() const noexcept {
169  return m_top_right;
170  }
171 
175  Location& top_right() noexcept {
176  return m_top_right;
177  }
178 
184  double left() const noexcept {
185  assert(valid());
186  return m_bottom_left.lon_without_check();
187  }
188 
194  double right() const noexcept {
195  assert(valid());
196  return m_top_right.lon_without_check();
197  }
198 
204  double top() const noexcept {
205  assert(valid());
206  return m_top_right.lat_without_check();
207  }
208 
214  double bottom() const noexcept {
215  assert(valid());
216  return m_bottom_left.lat_without_check();
217  }
218 
225  bool contains(const osmium::Location& location) const noexcept {
226  assert(bottom_left());
227  assert(top_right());
228  assert(location);
229  return location.x() >= bottom_left().x() && location.y() >= bottom_left().y() &&
230  location.x() <= top_right().x() && location.y() <= top_right().y();
231  }
232 
241  double size() const {
242  return (m_top_right.lon() - m_bottom_left.lon()) *
243  (m_top_right.lat() - m_bottom_left.lat());
244  }
245 
246  }; // class Box
247 
252  inline constexpr bool operator==(const Box& lhs, const Box& rhs) noexcept {
253  return lhs.bottom_left() == rhs.bottom_left() &&
254  lhs.top_right() == rhs.top_right();
255  }
256 
263  template <typename TChar, typename TTraits>
264  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const osmium::Box& box) {
265  if (box) {
266  out << '(';
267  box.bottom_left().as_string_without_check(std::ostream_iterator<char>(out));
268  out << ',';
269  box.top_right().as_string_without_check(std::ostream_iterator<char>(out));
270  out << ')';
271  } else {
272  out << "(undefined)";
273  }
274  return out;
275  }
276 
277 } // namespace osmium
278 
279 #endif // OSMIUM_OSM_BOX_HPP
Definition: location.hpp:271
constexpr int32_t x() const noexcept
Definition: location.hpp:373
constexpr bool valid() const noexcept
Definition: location.hpp:348
constexpr int32_t y() const noexcept
Definition: location.hpp:377
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:442
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const item_type item_type)
Definition: item_type.hpp:185