olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
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 <olp/core/CoreApi.h>
27#include <olp/core/porting/optional.h>
28
29namespace olp {
30namespace geo {
31
63class 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
137 std::string ToQuadKey() const;
138
144 static TileKey FromQuadKey(const std::string& quad_key);
145
155 std::string ToHereTile() const;
156
162 static TileKey FromHereTile(const std::string& key);
163
170 std::uint64_t ToQuadKey64() const;
171
177 static TileKey FromQuadKey64(std::uint64_t quad_key);
178
188 static TileKey FromRowColumnLevel(std::uint32_t row, std::uint32_t column,
189 std::uint32_t level);
190
196 constexpr std::uint32_t Level() const { return level_; }
197
205 constexpr std::uint32_t Row() const { return row_; }
206
214 constexpr std::uint32_t RowCount() const { return 1 << level_; }
215
224 constexpr std::uint32_t Column() const { return column_; }
225
233 constexpr std::uint32_t ColumnCount() const { return 1 << level_; }
234
243
252 bool IsChildOf(const TileKey& tile_key) const;
253
262 bool IsParentOf(const TileKey& tile_key) const;
263
276 TileKey ChangedLevelBy(int delta) const;
277
291 TileKey ChangedLevelTo(std::uint32_t level) const;
292
296 std::uint64_t GetSubkey64(int delta) const;
297
299 TileKey AddedSubkey64(std::uint64_t sub_quad_key) const;
300
311 TileKey AddedSubkey(const std::string& sub_quad_key) const;
312
321 TileKey AddedSubHereTile(const std::string& sub_here_tile) const;
322
328 constexpr bool HasNextRow() const { return row_ < (1u << level_) - 1; }
329
337
343 constexpr bool HasNextColumn() const { return column_ < (1u << level_) - 1; }
344
352
358 constexpr bool HasPreviousRow() const { return (row_ > 0); }
359
367
373 constexpr bool HasPreviousColumn() const { return (column_ > 0); }
374
382
390 TileKey GetChild(std::uint8_t index) const;
391
400
407
408 private:
409 std::uint32_t row_{0};
410 std::uint32_t column_{0};
411 std::uint32_t level_{LevelCount};
412};
413
420struct CORE_API QuadKey64Helper {
424 explicit constexpr QuadKey64Helper(std::uint64_t key) : key(key) {}
425
431 constexpr QuadKey64Helper Parent() const { return QuadKey64Helper{key >> 2}; }
432
438 constexpr QuadKey64Helper Child() const { return QuadKey64Helper{key << 2}; }
439
459 QuadKey64Helper GetSubkey(int delta) const;
460
472
482 static inline constexpr std::uint32_t RowsAtLevel(std::uint32_t level) {
483 return 1u << level;
484 }
485
495 static inline constexpr std::uint32_t ChildrenAtLevel(std::uint32_t level) {
496 return 1u << (level << 1u);
497 }
498
500 std::uint64_t key{0};
501};
502
503using TileKeyLevels = std::bitset<TileKey::LevelCount>;
504
512CORE_API porting::optional<std::uint32_t> GetMinTileKeyLevel(
513 const TileKeyLevels& levels);
514
522CORE_API porting::optional<std::uint32_t> GetMaxTileKeyLevel(
523 const TileKeyLevels& levels);
524
534CORE_API porting::optional<std::uint32_t> GetNearestAvailableTileKeyLevel(
535 const TileKeyLevels& levels, const std::uint32_t reference_level);
536
540CORE_API std::ostream& operator<<(std::ostream& out,
541 const geo::TileKey& tile_key);
542
543} // namespace geo
544} // namespace olp
545
546namespace std {
547
549template <>
550struct hash<olp::geo::TileKey> {
556 std::size_t operator()(const olp::geo::TileKey& tile_key) const {
557 return std::hash<std::uint64_t>()(tile_key.ToQuadKey64());
558 }
559};
560
561} // 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:373
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 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:224
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:214
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:343
constexpr bool HasNextRow() const
Checks whether there is the next row at this level.
Definition TileKey.h:328
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:205
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:233
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:196
TileKey NextRow() const
Gets the key of the tile that has the same level and column numbers but belongs to the next row.
TileKey & operator=(const TileKey &)=default
The default copy operator.
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:358
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:420
constexpr QuadKey64Helper Parent() const
Gets the quadkey of the parent.
Definition TileKey.h:431
static constexpr std::uint32_t ChildrenAtLevel(std::uint32_t level)
Gets the number of children at a level.
Definition TileKey.h:495
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:482
constexpr QuadKey64Helper Child() const
Gets the quadkey representing the first child of this quad.
Definition TileKey.h:438
constexpr QuadKey64Helper(std::uint64_t key)
The default constructor.
Definition TileKey.h:424
std::size_t operator()(const olp::geo::TileKey &tile_key) const
The hash function for tile keys.
Definition TileKey.h:556