LSST Applications g180d380827+6621f76652,g2079a07aa2+86d27d4dc4,g2305ad1205+f5a9e323a1,g2bbee38e9b+c6a8a0fb72,g337abbeb29+c6a8a0fb72,g33d1c0ed96+c6a8a0fb72,g3a166c0a6a+c6a8a0fb72,g3ddfee87b4+9a10e1fe7b,g48712c4677+c9a099281a,g487adcacf7+f2e03ea30b,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+aead732c78,g64a986408d+eddffb812c,g858d7b2824+eddffb812c,g864b0138d7+aa38e45daa,g974c55ee3d+f37bf00e57,g99cad8db69+119519a52d,g9c22b2923f+e2510deafe,g9ddcbc5298+9a081db1e4,ga1e77700b3+03d07e1c1f,gb0e22166c9+60f28cb32d,gb23b769143+eddffb812c,gba4ed39666+c2a2e4ac27,gbb8dafda3b+27317ec8e9,gbd998247f1+585e252eca,gc120e1dc64+5817c176a8,gc28159a63d+c6a8a0fb72,gc3e9b769f7+6707aea8b4,gcf0d15dbbd+9a10e1fe7b,gdaeeff99f8+f9a426f77a,ge6526c86ff+6a2e01d432,ge79ae78c31+c6a8a0fb72,gee10cc3b42+585e252eca,gff1a9f87cc+eddffb812c,v27.0.0.rc1
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
37
38#include "ConvexPolygonImpl.h"
39
40
41namespace lsst {
42namespace sphgeom {
43namespace detail {
44
45// `PixelFinder` is a CRTP base class that locates pixels intersecting a
46// region. It assumes a hierarchical pixelization, and that pixels are
47// convex spherical polygons with a fixed number of vertices.
48//
49// The algorithm used is top-down tree traversal, implemented via recursion
50// for simplicity. Subclasses must provide a method named `subdivide` with
51// the following signature:
52//
53// void subdivide(UnitVector3d const * pixel,
54// uint64_t index,
55// int level);
56//
57// that subdivides a pixel into its children and then invokes visit() on
58// each child. Children should be visited in ascending index order to keep
59// RangeSet inserts efficient. The subclass is also responsible for
60// implementing a top-level method that invokes visit() on each root pixel,
61// or on some set of candidate pixels.
62//
63// The `RegionType` parameter avoids the need for virtual function calls to
64// determine the spatial relationship between pixels and the input region. The
65// boolean template parameter `InteriorOnly` is a flag that indicates whether
66// to locate all pixels that intersect the input region, or only those that
67// are entirely inside it. Finally, the `NumVertices` template parameter is
68// the number of vertices in the polygonal representation of a pixel.
69template <
70 typename Derived,
71 typename RegionType,
72 bool InteriorOnly,
73 size_t NumVertices
74>
76public:
78 RegionType const & region,
79 int level,
80 size_t maxRanges):
81 _ranges{&ranges},
82 _region{&region},
83 _level{level},
84 _desiredLevel{level},
85 _maxRanges{maxRanges == 0 ? maxRanges - 1 : maxRanges}
86 {}
87
88 void visit(UnitVector3d const * pixel,
89 uint64_t index,
90 int level)
91 {
92 if (level > _level) {
93 // Nothing to do - the subdivision level has been reduced
94 // or a pixel that completely contains the search region
95 // has been found.
96 return;
97 }
98 // Determine the relationship between the pixel and the search region.
99 Relationship r = detail::relate(pixel, pixel + NumVertices, *_region);
100 if ((r & DISJOINT) != 0) {
101 // The pixel is disjoint from the search region.
102 return;
103 }
104 if ((r & WITHIN) != 0) {
105 // The tree traversal has reached a pixel that is entirely within
106 // the search region.
107 _insert(index, level);
108 return;
109 } else if (level == _level) {
110 // The tree traversal has reached a leaf.
111 if (!InteriorOnly) {
112 _insert(index, level);
113 }
114 return;
115 }
116 static_cast<Derived *>(this)->subdivide(pixel, index, level);
117 }
118
119private:
120 RangeSet * _ranges;
121 RegionType const * _region;
122 int _level;
123 int const _desiredLevel;
124 size_t const _maxRanges;
125
126 void _insert(uint64_t index, int level) {
127 int shift = 2 * (_desiredLevel - level);
128 _ranges->insert(index << shift, (index + 1) << shift);
129 while (_ranges->size() > _maxRanges) {
130 // Reduce the subdivision level.
131 --_level;
132 shift += 2;
133 // When looking for intersecting pixels, ranges are simplified
134 // by expanding them outwards, causing nearly adjacent small ranges
135 // to merge.
136 //
137 // When looking for interior pixels, ranges are simplified by
138 // shrinking them inwards, causing small ranges to disappear.
139 if (InteriorOnly) {
140 _ranges->complement();
141 }
142 _ranges->simplify(shift);
143 if (InteriorOnly) {
144 _ranges->complement();
145 }
146 }
147 }
148};
149
150
151// `findPixels` implements pixel-finding for an arbitrary Region, given a
152// PixelFinder subclass for a specific pixelization.
153template <
154 template <typename, bool> class Finder,
155 bool InteriorOnly
156>
157RangeSet findPixels(Region const & r, size_t maxRanges, int level) {
158 RangeSet s;
159 Circle const * c = nullptr;
160 Ellipse const * e = nullptr;
161 Box const * b = nullptr;
162 if ((c = dynamic_cast<Circle const *>(&r))) {
163 Finder<Circle, InteriorOnly> find(s, *c, level, maxRanges);
164 find();
165 } else if ((e = dynamic_cast<Ellipse const *>(&r))) {
166 Finder<Circle, InteriorOnly> find(
167 s, e->getBoundingCircle(), level, maxRanges);
168 find();
169 } else if ((b = dynamic_cast<Box const *>(&r))) {
170 Finder<Box, InteriorOnly> find(s, *b, level, maxRanges);
171 find();
172 } else {
173 Finder<ConvexPolygon, InteriorOnly> find(
174 s, dynamic_cast<ConvexPolygon const &>(r), level, maxRanges);
175 find();
176 }
177 return s;
178}
179
180}}} // namespace lsst::sphgeom::detail
181
182#endif // LSST_SPHGEOM_PIXELFINDER_H_
This file contains the meat of the ConvexPolygon implementation.
This file provides a type for representing integer sets.
table::Key< int > b
Box represents a rectangle in spherical coordinate space that contains its boundary.
Definition Box.h:61
Circle is a circular region on the unit sphere that contains its boundary.
Definition Circle.h:53
ConvexPolygon is a closed convex polygon on the unit sphere.
Ellipse is an elliptical region on the sphere.
Definition Ellipse.h:177
Circle getBoundingCircle() const override
getBoundingCircle returns a bounding-circle for this region.
Definition Ellipse.cc:248
A RangeSet is a set of unsigned 64 bit integers.
Definition RangeSet.h:106
void insert(uint64_t u)
Definition RangeSet.h:292
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(uint32_t n)
simplify simplifies this range set by "coarsening" its ranges.
Definition RangeSet.cc:315
Region is a minimal interface for 2-dimensional regions on the unit sphere.
Definition Region.h:86
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:88
PixelFinder(RangeSet &ranges, RegionType const &region, int level, size_t maxRanges)
Definition PixelFinder.h:77
RangeSet findPixels(Region const &r, size_t maxRanges, int level)
Relationship relate(VertexIterator const begin, VertexIterator const end, Box const &b)