olp-cpp-sdk  1.22.0
TileKey.h
1 /*
2  * Copyright (C) 2019-2023 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <bitset>
23 #include <cstdint>
24 #include <string>
25 
26 #include <boost/optional.hpp>
27 
28 #include <olp/core/CoreApi.h>
29 
30 namespace olp {
31 namespace geo {
32 
63 class CORE_API TileKey {
64  public:
65  enum { LevelCount = 32 };
66  enum { MaxLevel = LevelCount - 1 };
67 
74  enum class TileKeyQuadrant : uint8_t { SW, SE, NW, NE, Invalid };
75 
77  constexpr TileKey() : row_(0), column_(0), level_(LevelCount) {}
78 
80  constexpr TileKey(const TileKey&) = default;
81 
83  TileKey& operator=(const TileKey&) = default;
84 
86  constexpr bool IsValid() const {
87  return level_ < LevelCount && row_ < (1u << level_) &&
88  column_ < (1u << level_);
89  }
90 
96  constexpr bool operator==(const TileKey& other) const {
97  return level_ == other.level_ && row_ == other.row_ &&
98  column_ == other.column_;
99  }
100 
106  constexpr bool operator!=(const TileKey& other) const {
107  return !operator==(other);
108  }
109 
117  constexpr bool operator<(const TileKey& other) const {
118  return level_ != other.level_
119  ? level_ < other.level_
120  : row_ != other.row_ ? row_ < other.row_
121  : column_ < other.column_;
122  }
123 
136  std::string ToQuadKey() const;
137 
143  static TileKey FromQuadKey(const std::string& quad_key);
144 
152  std::string ToHereTile() const;
153 
159  static TileKey FromHereTile(const std::string& key);
160 
166  std::uint64_t ToQuadKey64() const;
167 
173  static TileKey FromQuadKey64(std::uint64_t quad_key);
174 
183  static TileKey FromRowColumnLevel(std::uint32_t row, std::uint32_t column,
184  std::uint32_t level);
185 
191  constexpr std::uint32_t Level() const { return level_; }
192 
200  constexpr std::uint32_t Row() const { return row_; }
201 
209  constexpr std::uint32_t RowCount() const { return 1 << level_; }
210 
219  constexpr std::uint32_t Column() const { return column_; }
220 
228  constexpr std::uint32_t ColumnCount() const { return 1 << level_; }
229 
237  TileKey Parent() const;
238 
247  bool IsChildOf(const TileKey& tile_key) const;
248 
257  bool IsParentOf(const TileKey& tile_key) const;
258 
271  TileKey ChangedLevelBy(int delta) const;
272 
286  TileKey ChangedLevelTo(std::uint32_t level) const;
287 
291  std::uint64_t GetSubkey64(int delta) const;
292 
294  TileKey AddedSubkey64(std::uint64_t sub_quad_key) const;
295 
306  TileKey AddedSubkey(const std::string& sub_quad_key) const;
307 
316  TileKey AddedSubHereTile(const std::string& sub_here_tile) const;
317 
323  constexpr bool HasNextRow() const { return row_ < (1u << level_) - 1; }
324 
331  TileKey NextRow() const;
332 
338  constexpr bool HasNextColumn() const { return column_ < (1u << level_) - 1; }
339 
347 
353  constexpr bool HasPreviousRow() const { return (row_ > 0); }
354 
362 
368  constexpr bool HasPreviousColumn() const { return (column_ > 0); }
369 
377 
385  TileKey GetChild(std::uint8_t index) const;
386 
394  TileKey GetChild(TileKeyQuadrant direction) const;
395 
402 
403  private:
404  std::uint32_t row_{0};
405  std::uint32_t column_{0};
406  std::uint32_t level_{LevelCount};
407 };
408 
415 struct CORE_API QuadKey64Helper {
419  explicit constexpr QuadKey64Helper(std::uint64_t key) : key(key) {}
420 
426  constexpr QuadKey64Helper Parent() const { return QuadKey64Helper{key >> 2}; }
427 
433  constexpr QuadKey64Helper Child() const { return QuadKey64Helper{key << 2}; }
434 
454  QuadKey64Helper GetSubkey(int delta) const;
455 
467 
477  static inline constexpr std::uint32_t RowsAtLevel(std::uint32_t level) {
478  return 1u << level;
479  }
480 
490  static inline constexpr std::uint32_t ChildrenAtLevel(std::uint32_t level) {
491  return 1u << (level << 1u);
492  }
493 
495  std::uint64_t key{0};
496 };
497 
498 using TileKeyLevels = std::bitset<TileKey::LevelCount>;
499 
507 CORE_API boost::optional<std::uint32_t> GetMinTileKeyLevel(
508  const TileKeyLevels& levels);
509 
517 CORE_API boost::optional<std::uint32_t> GetMaxTileKeyLevel(
518  const TileKeyLevels& levels);
519 
529 CORE_API boost::optional<std::uint32_t> GetNearestAvailableTileKeyLevel(
530  const TileKeyLevels& levels, const std::uint32_t reference_level);
531 
535 CORE_API std::ostream& operator<<(std::ostream& out,
536  const geo::TileKey& tile_key);
537 
538 } // namespace geo
539 } // namespace olp
540 
541 namespace std {
542 
544 template <>
545 struct hash<olp::geo::TileKey> {
551  std::size_t operator()(const olp::geo::TileKey& tile_key) const {
552  return std::hash<std::uint64_t>()(tile_key.ToQuadKey64());
553  }
554 };
555 
556 } // namespace std
Addresses a tile in a quadtree.
Definition: TileKey.h:63
TileKey NextColumn() const
Gets the key of the tile that has the same level and row numbers but belongs to the next column.
static TileKey FromRowColumnLevel(std::uint32_t row, std::uint32_t column, std::uint32_t level)
Creates a tile key.
static TileKey FromQuadKey(const std::string &quad_key)
Creates a tile key from a quad string.
constexpr bool HasPreviousColumn() const
Checks whether there is the previous column at this level.
Definition: TileKey.h:368
bool IsParentOf(const TileKey &tile_key) const
Checks whether the current tile is a parent of another tile.
std::string ToQuadKey() const
Creates a quad string from a tile key to later use it in REST API calls.
TileKey AddedSubHereTile(const std::string &sub_here_tile) const
Gets the absolute quadkey that is constructed from its HERE child tile key.
TileKey & operator=(const TileKey &)=default
The default copy operator.
TileKey Parent() const
Gets the key of the parent tile.
TileKey ChangedLevelTo(std::uint32_t level) const
Gets a new tile key at the requested level.
constexpr std::uint32_t Column() const
Gets the tile column.
Definition: TileKey.h:219
constexpr bool operator==(const TileKey &other) const
Checks whether the tile keys are equal.
Definition: TileKey.h:96
constexpr std::uint32_t RowCount() const
Gets the number of available rows at the tile level.
Definition: TileKey.h:209
constexpr bool operator!=(const TileKey &other) const
Checks whether the tile keys are not equal.
Definition: TileKey.h:106
constexpr bool HasNextColumn() const
Checks whether there is the next column at this level.
Definition: TileKey.h:338
constexpr bool HasNextRow() const
Checks whether there is the next row at this level.
Definition: TileKey.h:323
constexpr bool IsValid() const
Checks whether the tile key is valid.
Definition: TileKey.h:86
constexpr std::uint32_t Row() const
Gets the tile row.
Definition: TileKey.h:200
std::uint64_t GetSubkey64(int delta) const
Gets a subquadkey that is a relative of its parent.
bool IsChildOf(const TileKey &tile_key) const
Checks whether the current tile is a child of another tile.
TileKey GetChild(TileKeyQuadrant direction) const
Gets the child of the current tile using a direction.
constexpr std::uint32_t ColumnCount() const
Gets the number of available columns at the tile level.
Definition: TileKey.h:228
std::string ToHereTile() const
Creates a HERE tile code string from a tile key to later use it in REST API calls.
TileKey GetChild(std::uint8_t index) const
Gets the child of the current tile by its index.
constexpr bool operator<(const TileKey &other) const
Implements the following order on tile keys so they can be used in maps: first level,...
Definition: TileKey.h:117
static TileKey FromHereTile(const std::string &key)
Creates a tile key from a HERE tile code string.
TileKey ChangedLevelBy(int delta) const
Gets a new tile key at a level that differs from this tile level by delta.
static TileKey FromQuadKey64(std::uint64_t quad_key)
Creates a tile key from a 64-bit Morton code.
constexpr std::uint32_t Level() const
Gets the tile level.
Definition: TileKey.h:191
TileKey NextRow() const
Gets the key of the tile that has the same level and column numbers but belongs to the next row.
TileKey PreviousColumn() const
Gets the key of the tile that has the same level and row numbers but belongs to the previous column.
TileKey AddedSubkey(const std::string &sub_quad_key) const
Gets the absolute quadkey that is constructed from its subquadkey.
constexpr TileKey(const TileKey &)=default
The default copy constructor.
std::uint64_t ToQuadKey64() const
Creates a 64-bit Morton code from a tile key.
constexpr TileKey()
Creates an invalid tile key.
Definition: TileKey.h:77
TileKey PreviousRow() const
Gets the key of the tile that has the same level and column numbers but belongs to the previous row.
constexpr bool HasPreviousRow() const
Checks whether there is the previous row at this level.
Definition: TileKey.h:353
TileKey AddedSubkey64(std::uint64_t sub_quad_key) const
Gets the absolute quadkey that is constructed from its subquadkey.
TileKeyQuadrant RelationshipToParent() const
Computes the direction of the relationship to the parent.
TileKeyQuadrant
The main direction used to find a child node or the relationship to the parent.
Definition: TileKey.h:74
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24
A helper structure for basic operations on 64-bit Morton quadkeys.
Definition: TileKey.h:415
constexpr QuadKey64Helper Parent() const
Gets the quadkey of the parent.
Definition: TileKey.h:426
static constexpr std::uint32_t ChildrenAtLevel(std::uint32_t level)
Gets the number of children at a level.
Definition: TileKey.h:490
QuadKey64Helper AddedSubkey(QuadKey64Helper sub_key) const
Gets the absolute quadkey that is constructed from its subquadkey.
QuadKey64Helper GetSubkey(int delta) const
Gets a subquadkey that is a relative of its parent.
static constexpr std::uint32_t RowsAtLevel(std::uint32_t level)
Gets the number of rows at a given level.
Definition: TileKey.h:477
constexpr QuadKey64Helper Child() const
Gets the quadkey representing the first child of this quad.
Definition: TileKey.h:433
constexpr QuadKey64Helper(std::uint64_t key)
The default constructor.
Definition: TileKey.h:419
std::size_t operator()(const olp::geo::TileKey &tile_key) const
The hash function for tile keys.
Definition: TileKey.h:551