LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
PixelFinder.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2016 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #ifndef LSST_SPHGEOM_PIXELFINDER_H_
24 #define LSST_SPHGEOM_PIXELFINDER_H_
25 
28 
29 #include "lsst/sphgeom/RangeSet.h"
30 
31 #include "ConvexPolygonImpl.h"
32 
33 
34 namespace lsst {
35 namespace sphgeom {
36 namespace detail {
37 
38 // `PixelFinder` is a CRTP base class that locates pixels intersecting a
39 // region. It assumes a hierarchical pixelization, and that pixels are
40 // convex spherical polygons with a fixed number of vertices.
41 //
42 // The algorithm used is top-down tree traversal, implemented via recursion
43 // for simplicity. Subclasses must provide a method named `subdivide` with
44 // the following signature:
45 //
46 // void subdivide(UnitVector3d const * pixel,
47 // uint64_t index,
48 // int level);
49 //
50 // that subdivides a pixel into its children and then invokes visit() on
51 // each child. Children should be visited in ascending index order to keep
52 // RangeSet inserts efficient. The subclass is also responsible for
53 // implementing a top-level method that invokes visit() on each root pixel,
54 // or on some set of candidate pixels.
55 //
56 // The `RegionType` parameter avoids the need for virtual function calls to
57 // determine the spatial relationship between pixels and the input region. The
58 // boolean template parameter `InteriorOnly` is a flag that indicates whether
59 // to locate all pixels that intersect the input region, or only those that
60 // are entirely inside it. Finally, the `NumVertices` template parameter is
61 // the number of vertices in the polygonal representation of a pixel.
62 template <
63  typename Derived,
64  typename RegionType,
65  bool InteriorOnly,
66  size_t NumVertices
67 >
68 class PixelFinder {
69 public:
71  RegionType const & region,
72  int level,
73  size_t maxRanges):
74  _ranges{&ranges},
75  _region{&region},
76  _level{level},
77  _desiredLevel{level},
78  _maxRanges{maxRanges == 0 ? maxRanges - 1 : maxRanges}
79  {}
80 
81  void visit(UnitVector3d const * pixel,
82  uint64_t index,
83  int level)
84  {
85  if (level > _level) {
86  // Nothing to do - the subdivision level has been reduced
87  // or a pixel that completely contains the search region
88  // has been found.
89  return;
90  }
91  // Determine the relationship between the pixel and the search region.
92  Relationship r = detail::relate(pixel, pixel + NumVertices, *_region);
93  if ((r & DISJOINT) != 0) {
94  // The pixel is disjoint from the search region.
95  return;
96  }
97  if ((r & WITHIN) != 0) {
98  // The tree traversal has reached a pixel that is entirely within
99  // the search region.
100  _insert(index, level);
101  return;
102  } else if (level == _level) {
103  // The tree traversal has reached a leaf.
104  if (!InteriorOnly) {
105  _insert(index, level);
106  }
107  return;
108  }
109  static_cast<Derived *>(this)->subdivide(pixel, index, level);
110  }
111 
112 private:
113  RangeSet * _ranges;
114  RegionType const * _region;
115  int _level;
116  int const _desiredLevel;
117  size_t const _maxRanges;
118 
119  void _insert(uint64_t index, int level) {
120  int shift = 2 * (_desiredLevel - level);
121  _ranges->insert(index << shift, (index + 1) << shift);
122  while (_ranges->size() > _maxRanges) {
123  // Reduce the subdivision level.
124  --_level;
125  shift += 2;
126  // When looking for intersecting pixels, ranges are simplified
127  // by expanding them outwards, causing nearly adjacent small ranges
128  // to merge.
129  //
130  // When looking for interior pixels, ranges are simplified by
131  // shrinking them inwards, causing small ranges to disappear.
132  if (InteriorOnly) {
133  _ranges->complement();
134  }
135  _ranges->simplify(shift);
136  if (InteriorOnly) {
137  _ranges->complement();
138  }
139  }
140  }
141 };
142 
143 
144 // `findPixels` implements pixel-finding for an arbitrary Region, given a
145 // PixelFinder subclass for a specific pixelization.
146 template <
147  template <typename, bool> class Finder,
148  bool InteriorOnly
149 >
150 RangeSet findPixels(Region const & r, size_t maxRanges, int level) {
151  RangeSet s;
152  Circle const * c = nullptr;
153  Ellipse const * e = nullptr;
154  Box const * b = nullptr;
155  if ((c = dynamic_cast<Circle const *>(&r))) {
156  Finder<Circle, InteriorOnly> find(s, *c, level, maxRanges);
157  find();
158  } else if ((e = dynamic_cast<Ellipse const *>(&r))) {
159  Finder<Circle, InteriorOnly> find(
160  s, e->getBoundingCircle(), level, maxRanges);
161  find();
162  } else if ((b = dynamic_cast<Box const *>(&r))) {
163  Finder<Box, InteriorOnly> find(s, *b, level, maxRanges);
164  find();
165  } else {
166  Finder<ConvexPolygon, InteriorOnly> find(
167  s, dynamic_cast<ConvexPolygon const &>(r), level, maxRanges);
168  find();
169  }
170  return s;
171 }
172 
173 }}} // namespace lsst::sphgeom::detail
174 
175 #endif // LSST_SPHGEOM_PIXELFINDER_H_
std::bitset
STL class.
lsst::sphgeom::Region
Region is a minimal interface for 2-dimensional regions on the unit sphere.
Definition: Region.h:79
lsst::sphgeom::RangeSet::insert
void insert(uint64_t u)
Definition: RangeSet.h:285
lsst::sphgeom::Box
Box represents a rectangle in spherical coordinate space that contains its boundary.
Definition: Box.h:54
lsst::sphgeom::RangeSet::complement
RangeSet & complement()
complement replaces this set S with U ∖ S, where U is the universe of range sets, [0,...
Definition: RangeSet.h:328
lsst::sphgeom::detail::PixelFinder::visit
void visit(UnitVector3d const *pixel, uint64_t index, int level)
Definition: PixelFinder.h:81
lsst::sphgeom::UnitVector3d
UnitVector3d is a unit vector in ℝ³ with components stored in double precision.
Definition: UnitVector3d.h:55
lsst::sphgeom::ConvexPolygon
ConvexPolygon is a closed convex polygon on the unit sphere.
Definition: ConvexPolygon.h:57
lsst::sphgeom::Ellipse
Ellipse is an elliptical region on the sphere.
Definition: Ellipse.h:169
ConvexPolygonImpl.h
This file contains the meat of the ConvexPolygon implementation.
lsst::sphgeom::detail::relate
Relationship relate(VertexIterator const begin, VertexIterator const end, Box const &b)
Definition: ConvexPolygonImpl.h:258
pixel
table::PointKey< int > pixel
Definition: DeltaFunctionKernel.cc:101
b
table::Key< int > b
Definition: TransmissionCurve.cc:467
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::sphgeom::detail::PixelFinder::PixelFinder
PixelFinder(RangeSet &ranges, RegionType const &region, int level, size_t maxRanges)
Definition: PixelFinder.h:70
lsst::sphgeom::RangeSet::size
size_t size() const
size returns the number of ranges in this set.
Definition: RangeSet.h:539
lsst::sphgeom::detail::PixelFinder
Definition: PixelFinder.h:68
RangeSet.h
This file provides a type for representing integer sets.
lsst::sphgeom::RangeSet
A RangeSet is a set of unsigned 64 bit integers.
Definition: RangeSet.h:99
lsst::sphgeom::Ellipse::getBoundingCircle
Circle getBoundingCircle() const override
getBoundingCircle returns a bounding-circle for this region.
Definition: Ellipse.cc:241
lsst::sphgeom::Circle
Circle is a circular region on the unit sphere that contains its boundary.
Definition: Circle.h:46
lsst::sphgeom::RangeSet::simplify
RangeSet & simplify(uint32_t n)
simplify simplifies this range set by "coarsening" its ranges.
Definition: RangeSet.cc:308
lsst::sphgeom::detail::findPixels
RangeSet findPixels(Region const &r, size_t maxRanges, int level)
Definition: PixelFinder.h:150