Libosmium  2.2.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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 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) {
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 static_cast<bool>(m_bottom_left);
151  }
152 
157  OSMIUM_CONSTEXPR bool valid() const noexcept {
158  return bottom_left().valid() && top_right().valid();
159  }
160 
165  return m_bottom_left;
166  }
167 
171  Location& bottom_left() noexcept {
172  return m_bottom_left;
173  }
174 
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 
208  double size() const {
209  return (m_top_right.lon() - m_bottom_left.lon()) *
210  (m_top_right.lat() - m_bottom_left.lat());
211  }
212 
213  }; // class Box
214 
219  inline OSMIUM_CONSTEXPR bool operator==(const Box& lhs, const Box& rhs) noexcept {
220  return lhs.bottom_left() == rhs.bottom_left() &&
221  lhs.top_right() == rhs.top_right();
222  }
223 
230  template <typename TChar, typename TTraits>
231  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const osmium::Box& box) {
232  if (box) {
233  out << '('
234  << box.bottom_left().lon()
235  << ','
236  << box.bottom_left().lat()
237  << ','
238  << box.top_right().lon()
239  << ','
240  << box.top_right().lat()
241  << ')';
242  } else {
243  out << "(undefined)";
244  }
245  return out;
246  }
247 
248 } // namespace osmium
249 
250 #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
Box & extend(const Location &location) noexcept
Definition: box.hpp:110
constexpr bool valid() const noexcept
Definition: location.hpp:156
OSMIUM_CONSTEXPR Location bottom_left() const noexcept
Definition: box.hpp:164
Box & operator=(const Box &)=default
OSMIUM_CONSTEXPR bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:219
double lat() const
Definition: location.hpp:205
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
OSMIUM_CONSTEXPR Location top_right() const noexcept
Definition: box.hpp:178
Box & extend(const Box &box) noexcept
Definition: box.hpp:140
osmium::Location m_top_right
Definition: box.hpp:53
OSMIUM_CONSTEXPR bool valid() const noexcept
Definition: box.hpp:157
constexpr int32_t y() const noexcept
Definition: location.hpp:167
Location & bottom_left() noexcept
Definition: box.hpp:171
~Box()=default
Location & set_y(const int32_t y) noexcept
Definition: location.hpp:176
Box(const osmium::Location &bottom_left, const osmium::Location &top_right)
Definition: box.hpp:86
Definition: location.hpp:79
Definition: box.hpp:50
Location & set_x(const int32_t x) noexcept
Definition: location.hpp:171
double lon() const
Definition: location.hpp:186
#define OSMIUM_CONSTEXPR
Definition: compatibility.hpp:43
Box(double minx, double miny, double maxx, double maxy)
Definition: box.hpp:71
constexpr int32_t x() const noexcept
Definition: location.hpp:163
double size() const
Definition: box.hpp:208
constexpr Box() noexcept
Definition: box.hpp:61