KI18n

spatial_index.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "data/spatial_index_data.cpp"
8 #include "data/spatial_index_parameters_p.h"
9 #include "data/spatial_index_properties.cpp"
10 #include "spatial_index_p.h"
11 
12 #include <cmath>
13 #include <cstdint>
14 
15 constexpr const float XEnd = XStart + XRange;
16 constexpr const float YEnd = YStart + YRange;
17 
18 // verify the null entry is first in the property table
19 static_assert(spatial_index_properties[0].m_tz == Tz::Undefined);
20 static_assert(spatial_index_properties[0].m_subdiv == 0);
21 
22 // z index conversions
23 constexpr uint32_t latlonToZ(float lat, float lon)
24 {
25  const uint32_t x = ((lon - XStart) / XRange) * (1 << ZDepth);
26  const uint32_t y = ((lat - YStart) / YRange) * (1 << ZDepth);
27  uint32_t z = 0;
28  for (int i = ZDepth - 1; i >= 0; --i) {
29  z <<= 1;
30  z += (y & (1 << i)) ? 1 : 0;
31  z <<= 1;
32  z += (x & (1 << i)) ? 1 : 0;
33  }
34  return z;
35 }
36 
37 // "unit tests" for the z index conversion
38 static_assert(latlonToZ(YStart, XStart) == 0);
39 static_assert(latlonToZ(YEnd - 1.0f / (1 << ZDepth), XEnd - 1.0f / (1 << ZDepth)) == (1 << (2 * ZDepth)) - 1);
40 static_assert(latlonToZ(YStart + YRange / 2.0f, 0.0f) == ((1 << (2 * ZDepth - 1)) | (1 << (2 * ZDepth - 2))));
41 
42 SpatialIndexProperty SpatialIndex::lookup(float lat, float lon)
43 {
44  if (std::isnan(lat) || std::isnan(lon) || lon < XStart || lon >= XEnd || lat < YStart || lat >= YEnd) {
45  return spatial_index_properties[0];
46  }
47 
48  const auto z = latlonToZ(lat, lon);
49  const auto it = std::upper_bound(std::begin(spatial_index), std::end(spatial_index), z);
50  if (it == std::begin(spatial_index)) {
51  return spatial_index_properties[0];
52  }
53 
54  return spatial_index_properties[(*std::prev(it)).propertyIndex()];
55 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 16 2022 04:07:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.