Loading [MathJax]/extensions/tex2jax.js
LSST Applications g04a91732dc+a3f7a6a005,g07dc498a13+5ab4d22ec3,g0fba68d861+870ee37b31,g1409bbee79+5ab4d22ec3,g1a7e361dbc+5ab4d22ec3,g1fd858c14a+11200c7927,g20f46db602+25d63fd678,g35bb328faa+fcb1d3bbc8,g4d2262a081+cc8af5cafb,g4d39ba7253+6b9d64fe03,g4e0f332c67+5d362be553,g53246c7159+fcb1d3bbc8,g60b5630c4e+6b9d64fe03,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g8048e755c2+a1301e4c20,g8852436030+a750987b4a,g89139ef638+5ab4d22ec3,g89e1512fd8+a86d53a4aa,g8d6b6b353c+6b9d64fe03,g9125e01d80+fcb1d3bbc8,g989de1cb63+5ab4d22ec3,g9f33ca652e+38ca901d1a,ga9baa6287d+6b9d64fe03,gaaedd4e678+5ab4d22ec3,gabe3b4be73+1e0a283bba,gb1101e3267+aa269f591c,gb58c049af0+f03b321e39,gb90eeb9370+af74afe682,gc741bbaa4f+7f5db660ea,gcf25f946ba+a750987b4a,gd315a588df+b78635c672,gd6cbbdb0b4+c8606af20c,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+5839af1903,ge278dab8ac+932305ba37,ge82c20c137+76d20ab76d,w.2025.11
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PixelFinder.h
Go to the documentation of this file.
1/*
2 * This file is part of sphgeom.
3 *
4 * Developed for the LSST Data Management System.
5 * This product includes software developed by the LSST Project
6 * (http://www.lsst.org).
7 * See the COPYRIGHT file at the top-level directory of this distribution
8 * for details of code ownership.
9 *
10 * This software is dual licensed under the GNU General Public License and also
11 * under a 3-clause BSD license. Recipients may choose which of these licenses
12 * to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
13 * respectively. If you choose the GPL option then the following text applies
14 * (but note that there is still no warranty even if you opt for BSD instead):
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29
30#ifndef LSST_SPHGEOM_PIXELFINDER_H_
31#define LSST_SPHGEOM_PIXELFINDER_H_
32
35
38#include "ConvexPolygonImpl.h"
39
40#include <typeinfo>
41
42namespace lsst {
43namespace sphgeom {
44namespace detail {
45
46// `PixelFinder` is a CRTP base class that locates pixels intersecting a
47// region. It assumes a hierarchical pixelization, and that pixels are
48// convex spherical polygons with a fixed number of vertices.
49//
50// The algorithm used is top-down tree traversal, implemented via recursion
51// for simplicity. Subclasses must provide a method named `subdivide` with
52// the following signature:
53//
54// void subdivide(UnitVector3d const * pixel,
55// uint64_t index,
56// int level);
57//
58// that subdivides a pixel into its children and then invokes visit() on
59// each child. Children should be visited in ascending index order to keep
60// RangeSet inserts efficient. The subclass is also responsible for
61// implementing a top-level method that invokes visit() on each root pixel,
62// or on some set of candidate pixels.
63//
64// The `RegionType` parameter avoids the need for virtual function calls to
65// determine the spatial relationship between pixels and the input region. The
66// boolean template parameter `InteriorOnly` is a flag that indicates whether
67// to locate all pixels that intersect the input region, or only those that
68// are entirely inside it. Finally, the `NumVertices` template parameter is
69// the number of vertices in the polygonal representation of a pixel.
70template <
71 typename Derived,
72 typename RegionType,
73 bool InteriorOnly,
74 size_t NumVertices
75>
77public:
79 RegionType const & region,
80 int level,
81 size_t maxRanges):
82 _ranges{&ranges},
83 _region{&region},
84 _level{level},
85 _desiredLevel{level},
86 _maxRanges{maxRanges == 0 ? maxRanges - 1 : maxRanges}
87 {}
88
89 void visit(UnitVector3d const * pixel,
90 uint64_t index,
91 int level)
92 {
93 if (level > _level) {
94 // Nothing to do - the subdivision level has been reduced
95 // or a pixel that completely contains the search region
96 // has been found.
97 return;
98 }
99 // Determine the relationship between the pixel and the search region.
100 Relationship r = detail::relate(pixel, pixel + NumVertices, *_region);
101 if ((r & DISJOINT) != 0) {
102 // The pixel is disjoint from the search region.
103 return;
104 }
105 if ((r & WITHIN) != 0) {
106 // The tree traversal has reached a pixel that is entirely within
107 // the search region.
108 _insert(index, level);
109 return;
110 } else if (level == _level) {
111 // The tree traversal has reached a leaf.
112 if (!InteriorOnly) {
113 _insert(index, level);
114 }
115 return;
116 }
117 static_cast<Derived *>(this)->subdivide(pixel, index, level);
118 }
119
120private:
121 RangeSet * _ranges;
122 RegionType const * _region;
123 int _level;
124 int const _desiredLevel;
125 size_t const _maxRanges;
126
127 void _insert(uint64_t index, int level) {
128 int shift = 2 * (_desiredLevel - level);
129 _ranges->insert(index << shift, (index + 1) << shift);
130 while (_ranges->size() > _maxRanges) {
131 // Reduce the subdivision level.
132 --_level;
133 shift += 2;
134 // When looking for intersecting pixels, ranges are simplified
135 // by expanding them outwards, causing nearly adjacent small ranges
136 // to merge.
137 //
138 // When looking for interior pixels, ranges are simplified by
139 // shrinking them inwards, causing small ranges to disappear.
140 if (InteriorOnly) {
141 _ranges->complement();
142 }
143 _ranges->simplify(shift);
144 if (InteriorOnly) {
145 _ranges->complement();
146 }
147 }
148 }
149};
150
151
152// `findPixels` implements pixel-finding for an arbitrary Region, given a
153// PixelFinder subclass for a specific pixelization.
154template <
155 template <typename, bool> class Finder,
156 bool InteriorOnly
157>
158RangeSet findPixels(Region const & r, size_t maxRanges, int level, RangeSet const & universe) {
159 RangeSet s;
160 if (auto circle = dynamic_cast<Circle const *>(&r)) {
161 Finder<Circle, InteriorOnly> find(s, *circle, level, maxRanges);
162 find();
163 } else if (auto ellipse = dynamic_cast<Ellipse const *>(&r)) {
164 Finder<Circle, InteriorOnly> find(
165 s, ellipse->getBoundingCircle(), level, maxRanges);
166 find();
167 } else if (auto box = dynamic_cast<Box const *>(&r)) {
168 Finder<Box, InteriorOnly> find(s, *box, level, maxRanges);
169 find();
170 } else if (auto polygon = dynamic_cast<ConvexPolygon const *>(&r)) {
171 Finder<ConvexPolygon, InteriorOnly> find(
172 s, *polygon, level, maxRanges);
173 find();
174 } else if (auto union_region = dynamic_cast<UnionRegion const *>(&r)) {
175 // This can generate more ranges than maxRanges.
176 for (std::size_t i = 0; i < union_region->nOperands(); ++ i) {
177 Region const &region = union_region->getOperand(i);
178 auto rs = findPixels<Finder, InteriorOnly>(region, maxRanges, level, universe);
179 s = s.join(rs);
180 }
181 } else if (auto intersection_region = dynamic_cast<IntersectionRegion const *>(&r)) {
182 // Empty IntersectionRegion normally means whole sky.
183 s = universe;
184 for (std::size_t i = 0; i < intersection_region->nOperands(); ++ i) {
185 Region const &region = intersection_region->getOperand(i);
186 auto rs = findPixels<Finder, InteriorOnly>(region, maxRanges, level, universe);
187 s = s.intersection(rs);
188 }
189 } else {
190 throw std::runtime_error(std::string("PixelFinder: Unsupported type ") + typeid(r).name());
191 }
192 return s;
193}
194
195}}} // namespace lsst::sphgeom::detail
196
197#endif // LSST_SPHGEOM_PIXELFINDER_H_
This file declares classes for representing compound regions on the unit sphere.
This file contains the meat of the ConvexPolygon implementation.
This file provides a type for representing integer sets.
Box represents a rectangle in spherical coordinate space that contains its boundary.
Definition Box.h:62
Circle is a circular region on the unit sphere that contains its boundary.
Definition Circle.h:54
ConvexPolygon is a closed convex polygon on the unit sphere.
Ellipse is an elliptical region on the sphere.
Definition Ellipse.h:177
IntersectionRegion is a lazy point-set inersection of its operands.
A RangeSet is a set of unsigned 64 bit integers.
Definition RangeSet.h:106
RangeSet & complement()
complement replaces this set S with U ∖ S, where U is the universe of range sets, [0,...
Definition RangeSet.h:335
size_t size() const
size returns the number of ranges in this set.
Definition RangeSet.h:546
RangeSet & simplify(std::uint32_t n)
simplify simplifies this range set by "coarsening" its ranges.
Definition RangeSet.cc:315
void insert(std::uint64_t u)
Definition RangeSet.h:292
Region is a minimal interface for 2-dimensional regions on the unit sphere.
Definition Region.h:89
UnionRegion is a lazy point-set union of its operands.
UnitVector3d is a unit vector in ℝ³ with components stored in double precision.
void visit(UnitVector3d const *pixel, uint64_t index, int level)
Definition PixelFinder.h:89
PixelFinder(RangeSet &ranges, RegionType const &region, int level, size_t maxRanges)
Definition PixelFinder.h:78
RangeSet findPixels(Region const &r, size_t maxRanges, int level, RangeSet const &universe)
Relationship relate(VertexIterator const begin, VertexIterator const end, Box const &b)
std::bitset< 3 > Relationship
Relationship describes how two sets are related.