53constexpr uint8_t UNUSED = 255;
55alignas(64) uint8_t
const FACE_NUM[64] = {
56 4, 4, 4, 4, UNUSED, 3, UNUSED, UNUSED,
57 UNUSED, UNUSED, 0, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED,
58 UNUSED, UNUSED, UNUSED, 2, UNUSED, 3, UNUSED, 2,
59 UNUSED, UNUSED, 0, 2, UNUSED, UNUSED, UNUSED, 2,
60 5, UNUSED, UNUSED, UNUSED, 5, 3, UNUSED, UNUSED,
61 5, UNUSED, 0, UNUSED, 5, UNUSED, UNUSED, UNUSED,
62 UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, 3, UNUSED, UNUSED,
63 UNUSED, UNUSED, 0, UNUSED, 1, 1, 1, 1
66uint8_t
const FACE_COMP[6][4] = {
67 {0, 1, 2, UNUSED}, {1, 2, 0, UNUSED}, {2, 0, 1, UNUSED},
68 {0, 1, 2, UNUSED}, {1, 2, 0, UNUSED}, {2, 0, 1, UNUSED}
71double const FACE_CONST[6][4] = {
72 { 1.0, 1.0, -1.0, 0.0},
73 { 1.0, 1.0, 1.0, 0.0},
74 { 1.0, -1.0, 1.0, 0.0},
75 {-1.0, -1.0, 1.0, 0.0},
76 {-1.0, -1.0, -1.0, 0.0},
77 {-1.0, 1.0, -1.0, 0.0}
81constexpr double DILATION = 1.0e-15;
86uint64_t wrapIndex(
int level,
91 uint32_t
const stMax = (
static_cast<uint32_t
>(1) << level) - 1;
94 if (s ==
static_cast<uint32_t
>(-1)) {
95 face = (face + 4) % 6;
99 }
else if (s > stMax) {
100 face = (face + 1) % 6;
104 }
else if (t ==
static_cast<uint32_t
>(-1)) {
105 face = (face + 5) % 6;
109 }
else if (t > stMax) {
110 face = (face + 2) % 6;
117 return (
static_cast<uint64_t
>(face + 10) << (2 * level)) |
121int findNeighborhood(
int level, uint64_t i, uint64_t * dst) {
122 int const face =
static_cast<int>(i >> (2 * level)) - 10;
125 dst[0] = wrapIndex(level, face, s - 1, t - 1);
126 dst[1] = wrapIndex(level, face, s , t - 1);
127 dst[2] = wrapIndex(level, face, s + 1, t - 1);
128 dst[3] = wrapIndex(level, face, s - 1, t);
130 dst[5] = wrapIndex(level, face, s + 1, t);
131 dst[6] = wrapIndex(level, face, s - 1, t + 1);
132 dst[7] = wrapIndex(level, face, s , t + 1);
133 dst[8] = wrapIndex(level, face, s + 1, t + 1);
135 return static_cast<int>(
std::unique(dst, dst + 9) - dst);
138#if defined(NO_SIMD) || !defined(__x86_64__)
139 void makeQuad(uint64_t i,
int level, UnitVector3d * verts) {
140 int const face =
static_cast<int>(i >> (2 * level)) - 10;
141 double const faceScale = FACE_SCALE[level];
146 level,
static_cast<int32_t
>(s),
static_cast<int32_t
>(t));
147 double u1 = (u0 + faceScale) + DILATION;
148 double v1 = (v0 + faceScale) + DILATION;
151 std::tie(u0, v0) = atanApproxInverse(u0, v0);
152 std::tie(u1, v1) = atanApproxInverse(u1, v1);
153 verts[0] = faceToSphere(face, u0, v0, FACE_COMP, FACE_CONST);
154 verts[1] = faceToSphere(face, u1, v0, FACE_COMP, FACE_CONST);
155 verts[2] = faceToSphere(face, u1, v1, FACE_COMP, FACE_CONST);
156 verts[3] = faceToSphere(face, u0, v1, FACE_COMP, FACE_CONST);
161 if ((face & 1) == 0) {
166 void makeQuad(uint64_t i,
int level, UnitVector3d * verts) {
167 int const face =
static_cast<int>(i >> (2 * level)) - 10;
168 __m128d faceScale = _mm_set1_pd(FACE_SCALE[level]);
169 __m128d dilation = _mm_set1_pd(DILATION);
170 __m128d u0v0 = gridToFace(level, hilbertIndexInverseSimd(i, level));
171 __m128d u1v1 = _mm_add_pd(u0v0, faceScale);
172 u0v0 = atanApproxInverse(_mm_sub_pd(u0v0, dilation));
173 u1v1 = atanApproxInverse(_mm_add_pd(u1v1, dilation));
174 verts[0] = faceToSphere(face, u0v0, FACE_COMP, FACE_CONST);
175 verts[1] = faceToSphere(face, _mm_shuffle_pd(u1v1, u0v0, 2),
176 FACE_COMP, FACE_CONST);
177 verts[2] = faceToSphere(face, u1v1, FACE_COMP, FACE_CONST);
178 verts[3] = faceToSphere(face, _mm_shuffle_pd(u0v0, u1v1, 2),
179 FACE_COMP, FACE_CONST);
180 if ((face & 1) == 0) {
204template <
typename RegionType,
bool InteriorOnly>
205class Mq3cPixelFinder:
public detail::PixelFinder<
206 Mq3cPixelFinder<RegionType, InteriorOnly>, RegionType, InteriorOnly, 4>
209 using Base = detail::PixelFinder<
210 Mq3cPixelFinder<RegionType, InteriorOnly>, RegionType, InteriorOnly, 4>;
214 Mq3cPixelFinder(RangeSet & ranges,
215 RegionType
const & region,
218 Base(ranges, region, level, maxRanges)
222 UnitVector3d pixel[4];
224 for (uint64_t f = 10; f < 16; ++f) {
225 makeQuad(f, 0, pixel);
230 void subdivide(UnitVector3d
const *, uint64_t i,
int level) {
231 UnitVector3d pixel[4];
233 for (uint64_t c = i * 4; c != i * 4 + 4; ++c) {
234 makeQuad(c, level, pixel);
235 visit(pixel, c, level);
251 if ((j & 1) == 0 || (j == 1) || ((i >> (j - 3)) < 10)) {
263 makeQuad(i, l, verts);
264 return ConvexPolygon(verts[0], verts[1], verts[2], verts[3]);
273 int n = findNeighborhood(l, i, indexes);
278 static char const FACE_NORM[6][2] = {
279 {
'-',
'Z'}, {
'+',
'X'}, {
'+',
'Y'},
280 {
'+',
'Z'}, {
'-',
'X'}, {
'-',
'Y'},
288 char * p = s + (
sizeof(s) - 1);
289 for (; l > 0; --l, --p, i >>= 2) {
294 p[0] = FACE_NORM[i - 10][0];
295 p[1] = FACE_NORM[i - 10][1];
296 return std::string(p,
sizeof(s) -
static_cast<size_t>(p - s));
302 "Modified-Q3C subdivision level not in [0, 30]");
307 uint64_t f = i >> (2 * _level);
308 if (f < 10 || f > 15) {
312 makeQuad(i, _level, verts);
317#if defined(NO_SIMD) || !defined(__x86_64__)
319 int face = faceNumber(p, FACE_NUM);
321 double u = (p(FACE_COMP[face][0]) /
w) * FACE_CONST[face][0];
322 double v = (p(FACE_COMP[face][1]) /
w) * FACE_CONST[face][1];
325 uint64_t h =
hilbertIndex(
static_cast<uint32_t
>(std::get<0>(g)),
326 static_cast<uint32_t
>(std::get<1>(g)),
328 return (
static_cast<uint64_t
>(face + 10) << (2 * _level)) | h;
332 int face = faceNumber(p, FACE_NUM);
333 __m128d ww = _mm_set1_pd(p(FACE_COMP[face][2]));
334 __m128d uv = _mm_set_pd(p(FACE_COMP[face][1]), p(FACE_COMP[face][0]));
336 _mm_div_pd(uv, _mm_andnot_pd(_mm_set_pd(-0.0, -0.0), ww)),
337 _mm_set_pd(FACE_CONST[face][1], FACE_CONST[face][0])
339 __m128i st = faceToGrid(_level, atanApprox(uv));
341 return (
static_cast<uint64_t
>(face + 10) << (2 * _level)) | h;
346 return detail::findPixels<Mq3cPixelFinder, false>(r, maxRanges, _level);
350 return detail::findPixels<Mq3cPixelFinder, true>(r, maxRanges, _level);
This file declares a class for representing convex polygons with great circle edges on the unit spher...
This file declares a Pixelization subclass for the modified Q3C indexing scheme.
This file provides a base class for pixel finders.
This file contains functions used by Q3C pixelization implementations.
This file declares a class for representing unit vectors in ℝ³.
ConvexPolygon is a closed convex polygon on the unit sphere.
static int level(uint64_t i)
level returns the subdivision level of the given modified Q3C index.
static constexpr int MAX_LEVEL
The maximum supported cube-face grid resolution is 2^30 by 2^30.
static ConvexPolygon quad(uint64_t i)
quad returns the quadrilateral corresponding to the modified Q3C pixel with index i.
RangeSet _envelope(Region const &r, size_t maxRanges) const override
RangeSet _interior(Region const &r, size_t maxRanges) const override
static std::string asString(uint64_t i)
toString converts the given modified-Q3C index to a human readable string.
uint64_t index(UnitVector3d const &v) const override
index computes the index of the pixel for v.
Mq3cPixelization(int level)
This constructor creates a modified Q3C pixelization of the sphere with the given subdivision level.
static std::vector< uint64_t > neighborhood(uint64_t i)
neighborhood returns the indexes of all pixels that share a vertex with pixel i (including i itself).
std::unique_ptr< Region > pixel(uint64_t i) const override
pixel returns the spherical region corresponding to the pixel with index i.
A RangeSet is a set of unsigned 64 bit integers.
Region is a minimal interface for 2-dimensional regions on the unit sphere.
UnitVector3d is a unit vector in ℝ³ with components stored in double precision.
void visit(UnitVector3d const *pixel, uint64_t index, int level)
This file contains functions for space-filling curves.
std::tuple< uint32_t, uint32_t > hilbertIndexInverse(uint64_t h, int m)
hilbertIndexInverse returns the point (x, y) with Hilbert index h, where x and y are m bit integers.
uint64_t hilbertIndex(uint32_t x, uint32_t y, int m)
hilbertIndex returns the index of (x, y) in a 2-D Hilbert curve.