File indexing completed on 2024-12-08 10:16:04

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ztile.h"
0008 #include "datatypes.h"
0009 
0010 using namespace OSM;
0011 
0012 OSM::BoundingBox ZTile::boundingBox() const
0013 {
0014     const OSM::Coordinate min(z << (2 * depth));
0015     const OSM::Coordinate max(min.latitude + size(), min.longitude + size());
0016     return OSM::BoundingBox(min, max);
0017 }
0018 
0019 bool ZTile::intersects(OSM::BoundingBox bbox) const
0020 {
0021     return OSM::intersects(boundingBox(), bbox);
0022 }
0023 
0024 bool ZTile::intersects(ZTile other) const
0025 {
0026     const auto commonDepth = std::max(depth, other.depth);
0027     const auto z1 = z >> (2 * (commonDepth - depth));
0028     const auto z2 = other.z >> (2 * (commonDepth - other.depth));
0029     return z1 == z2;
0030 }
0031 
0032 ZTile ZTile::parent() const
0033 {
0034     return ZTile{z >> 2, (uint8_t)(depth + 1)};
0035 }
0036 
0037 std::array<ZTile, 4> ZTile::quadSplit() const
0038 {
0039     if (depth == 0) {
0040         return {};
0041     }
0042 
0043     const uint8_t subDepth = depth - 1;
0044     const uint64_t subZ = z << 2;
0045 
0046     return { ZTile{ subZ, subDepth }, ZTile{ subZ + 1, subDepth }, ZTile{ subZ + 2, subDepth }, ZTile { subZ + 3, subDepth } };
0047 }
0048 
0049 ZTile OSM::ztileFromBoundingBox(OSM::BoundingBox bbox)
0050 {
0051     uint64_t zMin = bbox.min.z();
0052     uint64_t zMax = bbox.max.z();
0053 
0054     ZTile tile;
0055 
0056     while (zMin != zMax) {
0057         zMin >>= 2;
0058         zMax >>= 2;
0059         ++tile.depth;
0060     }
0061     tile.z = zMin;
0062     return tile;
0063 }