Libosmium  2.12.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-2017 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 
39 #include <osmium/osm/location.hpp>
40 
41 namespace osmium {
42 
49  class Box {
50 
53 
54  public:
55 
60  constexpr Box() noexcept :
61  m_bottom_left(),
62  m_top_right() {
63  }
64 
70  Box(double minx, double miny, double maxx, double maxy) :
71  m_bottom_left(minx, miny),
72  m_top_right(maxx, maxy) {
73  assert(minx <= maxx && miny <= maxy);
74  }
75 
86  m_bottom_left(bottom_left),
87  m_top_right(top_right) {
88  assert(
89  (!!bottom_left && !!top_right) ||
90  (bottom_left.x() <= top_right.x() && bottom_left.y() <= top_right.y())
91  );
92  }
93 
94  Box(const Box&) = default;
95  Box(Box&&) = default;
96  Box& operator=(const Box&) = default;
97  Box& operator=(Box&&) = default;
98  ~Box() = default;
99 
109  Box& extend(const Location& location) noexcept {
110  if (location.valid()) {
111  if (m_bottom_left) {
112  if (location.x() < m_bottom_left.x()) {
113  m_bottom_left.set_x(location.x());
114  }
115  if (location.x() > m_top_right.x()) {
116  m_top_right.set_x(location.x());
117  }
118  if (location.y() < m_bottom_left.y()) {
119  m_bottom_left.set_y(location.y());
120  }
121  if (location.y() > m_top_right.y()) {
122  m_top_right.set_y(location.y());
123  }
124  } else {
125  m_bottom_left = location;
126  m_top_right = location;
127  }
128  }
129  return *this;
130  }
131 
139  Box& extend(const Box& box) noexcept {
140  extend(box.bottom_left());
141  extend(box.top_right());
142  return *this;
143  }
144 
148  explicit constexpr operator bool() const noexcept {
149  return bool(m_bottom_left) && bool(m_top_right);
150  }
151 
156  constexpr bool valid() const noexcept {
157  return bottom_left().valid() && top_right().valid();
158  }
159 
163  constexpr Location bottom_left() const noexcept {
164  return m_bottom_left;
165  }
166 
170  Location& bottom_left() noexcept {
171  return m_bottom_left;
172  }
173 
177  constexpr Location top_right() const noexcept {
178  return m_top_right;
179  }
180 
184  Location& top_right() noexcept {
185  return m_top_right;
186  }
187 
194  bool contains(const osmium::Location& location) const noexcept {
195  assert(bottom_left());
196  assert(top_right());
197  assert(location);
198  return location.x() >= bottom_left().x() && location.y() >= bottom_left().y() &&
199  location.x() <= top_right().x() && location.y() <= top_right().y();
200  }
201 
210  double size() const {
211  return (m_top_right.lon() - m_bottom_left.lon()) *
212  (m_top_right.lat() - m_bottom_left.lat());
213  }
214 
215  }; // class Box
216 
221  inline constexpr bool operator==(const Box& lhs, const Box& rhs) noexcept {
222  return lhs.bottom_left() == rhs.bottom_left() &&
223  lhs.top_right() == rhs.top_right();
224  }
225 
232  template <typename TChar, typename TTraits>
233  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const osmium::Box& box) {
234  if (box) {
235  out << '('
236  << box.bottom_left().lon()
237  << ','
238  << box.bottom_left().lat()
239  << ','
240  << box.top_right().lon()
241  << ','
242  << box.top_right().lat()
243  << ')';
244  } else {
245  out << "(undefined)";
246  }
247  return out;
248  }
249 
250 } // namespace osmium
251 
252 #endif // OSMIUM_OSM_BOX_HPP
osmium::Location m_bottom_left
Definition: box.hpp:51
double lon() const
Definition: location.hpp:371
Location & top_right() noexcept
Definition: box.hpp:184
bool contains(const osmium::Location &location) const noexcept
Definition: box.hpp:194
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:221
constexpr Location top_right() const noexcept
Definition: box.hpp:177
Box & extend(const Location &location) noexcept
Definition: box.hpp:109
double lat() const
Definition: location.hpp:390
constexpr bool valid() const noexcept
Definition: location.hpp:341
Box & operator=(const Box &)=default
Namespace for everything in the Osmium library.
Definition: assembler.hpp:63
Box & extend(const Box &box) noexcept
Definition: box.hpp:139
osmium::Location m_top_right
Definition: box.hpp:52
constexpr bool valid() const noexcept
Definition: box.hpp:156
constexpr int32_t y() const noexcept
Definition: location.hpp:352
Location & bottom_left() noexcept
Definition: box.hpp:170
~Box()=default
Location & set_y(const int32_t y) noexcept
Definition: location.hpp:361
Box(const osmium::Location &bottom_left, const osmium::Location &top_right)
Definition: box.hpp:85
Definition: location.hpp:266
Definition: box.hpp:49
Location & set_x(const int32_t x) noexcept
Definition: location.hpp:356
constexpr Location bottom_left() const noexcept
Definition: box.hpp:163
Box(double minx, double miny, double maxx, double maxy)
Definition: box.hpp:70
constexpr int32_t x() const noexcept
Definition: location.hpp:348
double size() const
Definition: box.hpp:210
constexpr Box() noexcept
Definition: box.hpp:60