LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
LSST Data Management Base Package
Loading...
Searching...
No Matches
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_
table::Key< std::string > name
Definition Amplifier.cc:116
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)