LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
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) {
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 Region const &region1 = union_region->getOperand(0);
176 Region const &region2 = union_region->getOperand(1);
177 auto rs1 = findPixels<Finder, InteriorOnly>(region1, maxRanges, level);
178 auto rs2 = findPixels<Finder, InteriorOnly>(region2, maxRanges, level);
179 s = rs1.join(rs2);
180 } else if (auto intersection_region = dynamic_cast<IntersectionRegion const *>(&r)) {
181 Region const &region1 = intersection_region->getOperand(0);
182 Region const &region2 = intersection_region->getOperand(1);
183 auto rs1 = findPixels<Finder, InteriorOnly>(region1, maxRanges, level);
184 auto rs2 = findPixels<Finder, InteriorOnly>(region2, maxRanges, level);
185 s = rs1.intersection(rs2);
186 } else {
187 throw std::runtime_error(std::string("PixelFinder: Unsupported type ") + typeid(r).name());
188 }
189 return s;
190}
191
192}}} // namespace lsst::sphgeom::detail
193
194#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:87
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)
Relationship relate(VertexIterator const begin, VertexIterator const end, Box const &b)