Libosmium  2.7.2
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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 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 <cassert>
37 #include <iosfwd>
38 
40 #include <osmium/osm/location.hpp>
41 
42 namespace osmium {
43 
50  class Box {
51 
54 
55  public:
56 
61  constexpr Box() noexcept :
62  m_bottom_left(),
63  m_top_right() {
64  }
65 
71  Box(double minx, double miny, double maxx, double maxy) :
72  m_bottom_left(minx, miny),
73  m_top_right(maxx, maxy) {
74  assert(minx <= maxx && miny <= maxy);
75  }
76 
87  m_bottom_left(bottom_left),
88  m_top_right(top_right) {
89  assert(
90  (!!bottom_left && !!top_right) ||
91  (bottom_left.x() <= top_right.x() && bottom_left.y() <= top_right.y())
92  );
93  }
94 
95  Box(const Box&) = default;
96  Box(Box&&) = default;
97  Box& operator=(const Box&) = default;
98  Box& operator=(Box&&) = default;
99  ~Box() = default;
100 
110  Box& extend(const Location& location) noexcept {
111  if (location.valid()) {
112  if (m_bottom_left) {
113  if (location.x() < m_bottom_left.x()) {
114  m_bottom_left.set_x(location.x());
115  }
116  if (location.x() > m_top_right.x()) {
117  m_top_right.set_x(location.x());
118  }
119  if (location.y() < m_bottom_left.y()) {
120  m_bottom_left.set_y(location.y());
121  }
122  if (location.y() > m_top_right.y()) {
123  m_top_right.set_y(location.y());
124  }
125  } else {
126  m_bottom_left = location;
127  m_top_right = location;
128  }
129  }
130  return *this;
131  }
132 
140  Box& extend(const Box& box) noexcept {
141  extend(box.bottom_left());
142  extend(box.top_right());
143  return *this;
144  }
145 
149  explicit constexpr operator bool() const noexcept {
150  return bool(m_bottom_left) && bool(m_top_right);
151  }
152 
157  constexpr bool valid() const noexcept {
158  return bottom_left().valid() && top_right().valid();
159  }
160 
164  constexpr Location bottom_left() const noexcept {
165  return m_bottom_left;
166  }
167 
171  Location& bottom_left() noexcept {
172  return m_bottom_left;
173  }
174 
178  constexpr Location top_right() const noexcept {
179  return m_top_right;
180  }
181 
185  Location& top_right() noexcept {
186  return m_top_right;
187  }
188 
195  bool contains(const osmium::Location& location) const noexcept {
196  assert(bottom_left());
197  assert(top_right());
198  assert(location);
199  return location.x() >= bottom_left().x() && location.y() >= bottom_left().y() &&
200  location.x() <= top_right().x() && location.y() <= top_right().y();
201  }
202 
211  double size() const {
212  return (m_top_right.lon() - m_bottom_left.lon()) *
213  (m_top_right.lat() - m_bottom_left.lat());
214  }
215 
216  }; // class Box
217 
222  inline constexpr bool operator==(const Box& lhs, const Box& rhs) noexcept {
223  return lhs.bottom_left() == rhs.bottom_left() &&
224  lhs.top_right() == rhs.top_right();
225  }
226 
233  template <typename TChar, typename TTraits>
234  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const osmium::Box& box) {
235  if (box) {
236  out << '('
237  << box.bottom_left().lon()
238  << ','
239  << box.bottom_left().lat()
240  << ','
241  << box.top_right().lon()
242  << ','
243  << box.top_right().lat()
244  << ')';
245  } else {
246  out << "(undefined)";
247  }
248  return out;
249  }
250 
251 } // namespace osmium
252 
253 #endif // OSMIUM_OSM_BOX_HPP
osmium::Location m_bottom_left
Definition: box.hpp:52
Location & top_right() noexcept
Definition: box.hpp:185
bool contains(const osmium::Location &location) const noexcept
Definition: box.hpp:195
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:222
constexpr Location top_right() const noexcept
Definition: box.hpp:178
Box & extend(const Location &location) noexcept
Definition: box.hpp:110
constexpr bool valid() const noexcept
Definition: location.hpp:291
Box & operator=(const Box &)=default
double lat() const
Definition: location.hpp:340
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
Box & extend(const Box &box) noexcept
Definition: box.hpp:140
osmium::Location m_top_right
Definition: box.hpp:53
constexpr bool valid() const noexcept
Definition: box.hpp:157
constexpr int32_t y() const noexcept
Definition: location.hpp:302
Location & bottom_left() noexcept
Definition: box.hpp:171
~Box()=default
Location & set_y(const int32_t y) noexcept
Definition: location.hpp:311
Box(const osmium::Location &bottom_left, const osmium::Location &top_right)
Definition: box.hpp:86
Definition: location.hpp:216
Definition: box.hpp:50
Location & set_x(const int32_t x) noexcept
Definition: location.hpp:306
double lon() const
Definition: location.hpp:321
constexpr Location bottom_left() const noexcept
Definition: box.hpp:164
Box(double minx, double miny, double maxx, double maxy)
Definition: box.hpp:71
constexpr int32_t x() const noexcept
Definition: location.hpp:298
double size() const
Definition: box.hpp:211
constexpr Box() noexcept
Definition: box.hpp:61