LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
CompoundRegion.cc
Go to the documentation of this file.
1/*
2 * LSST Data Management System
3 * Copyright 2014-2015 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
25
27
28#include <iostream>
29#include <stdexcept>
30
31#include "lsst/sphgeom/Box.h"
32#include "lsst/sphgeom/Box3d.h"
33#include "lsst/sphgeom/Circle.h"
36#include "lsst/sphgeom/codec.h"
37
38namespace lsst {
39namespace sphgeom {
40
41namespace {
42
43// A version of decodeU64 from codec.h that checks for buffer overruns and
44// increments the buffer pointer it is given.
45std::uint64_t consumeDecodeU64(std::uint8_t const *&buffer, std::uint8_t const *end) {
46 if (buffer + 8 > end) {
47 throw std::runtime_error("Encoded CompoundRegion is truncated.");
48 }
50 buffer += 8;
51 return result;
53
54template <typename F>
55auto getUnionBounds(UnionRegion const &compound, F func) {
56 auto bounds = func(compound.getOperand(0));
57 bounds.expandTo(func(compound.getOperand(1)));
58 return bounds;
59}
60
61template <typename F>
62auto getIntersectionBounds(IntersectionRegion const &compound, F func) {
63 auto bounds = func(compound.getOperand(0));
64 bounds.clipTo(func(compound.getOperand(1)));
65 return bounds;
66}
67
68} // namespace
69
71 : _operands{first.clone(), second.clone()} {}
72
74 std::array<std::unique_ptr<Region>, 2> operands) noexcept
75 : _operands(std::move(operands)) {}
76
78 : _operands{other.getOperand(0).clone(), other.getOperand(1).clone()} {}
79
80Relationship CompoundRegion::relate(Box const &b) const { return relate(static_cast<Region const &>(b)); }
81Relationship CompoundRegion::relate(Circle const &c) const { return relate(static_cast<Region const &>(c)); }
82Relationship CompoundRegion::relate(ConvexPolygon const &p) const { return relate(static_cast<Region const &>(p)); }
83Relationship CompoundRegion::relate(Ellipse const &e) const { return relate(static_cast<Region const &>(e)); }
84
87 buffer.push_back(tc);
88 auto buffer1 = getOperand(0).encode();
89 encodeU64(buffer1.size(), buffer);
90 buffer.insert(buffer.end(), buffer1.begin(), buffer1.end());
91 auto buffer2 = getOperand(1).encode();
92 encodeU64(buffer2.size(), buffer);
93 buffer.insert(buffer.end(), buffer2.begin(), buffer2.end());
94 return buffer;
95}
96
98 std::uint8_t tc, std::uint8_t const *buffer, std::size_t nBytes) {
99 std::uint8_t const *end = buffer + nBytes;
100 if (nBytes == 0) {
101 throw std::runtime_error("Encoded CompoundRegion is truncated.");
102 }
103 if (buffer[0] != tc) {
104 throw std::runtime_error("Byte string is not an encoded CompoundRegion.");
105 }
106 ++buffer;
108 std::uint64_t nBytes1 = consumeDecodeU64(buffer, end);
109 result[0] = Region::decode(buffer, nBytes1);
110 buffer += nBytes1;
111 std::uint64_t nBytes2 = consumeDecodeU64(buffer, end);
112 result[1] = Region::decode(buffer, nBytes2);
113 buffer += nBytes2;
114 if (buffer != end) {
115 throw std::runtime_error("Encoded CompoundRegion is has unexpected additional bytes.");
116 }
117 return result;
118}
119
121 if (n == 0) {
122 throw std::runtime_error("Encoded CompoundRegion is truncated.");
123 }
124 switch (buffer[0]) {
126 return UnionRegion::decode(buffer, n);
128 return IntersectionRegion::decode(buffer, n);
129 default:
130 throw std::runtime_error("Byte string is not an encoded CompoundRegion.");
131 }
132}
133
135 return getUnionBounds(*this, [](Region const &r) { return r.getBoundingBox(); });
136}
137
139 return getUnionBounds(*this, [](Region const &r) { return r.getBoundingBox3d(); });
140}
141
143 return getUnionBounds(*this, [](Region const &r) { return r.getBoundingCircle(); });
144}
145
147 return getOperand(0).contains(v) || getOperand(1).contains(v);
148}
149
151 auto r1 = getOperand(0).relate(rhs);
152 auto r2 = getOperand(1).relate(rhs);
153 return
154 // Both operands must be disjoint with the given region for the union
155 // to be disjoint with it.
156 ((r1 & DISJOINT) & (r2 & DISJOINT))
157 // Both operands must be within the given region for the union to be
158 // within it.
159 | ((r1 & WITHIN) & (r2 & WITHIN))
160 // If either operand contains the given region, the union contains it.
161 | ((r1 & CONTAINS) | (r2 & CONTAINS));
162}
163
165 return getIntersectionBounds(*this, [](Region const &r) { return r.getBoundingBox(); });
166}
167
169 return getIntersectionBounds(*this, [](Region const &r) { return r.getBoundingBox3d(); });
170}
171
173 return getIntersectionBounds(*this, [](Region const &r) { return r.getBoundingCircle(); });
174}
175
177 return getOperand(0).contains(v) && getOperand(1).contains(v);
178}
179
181 auto r1 = getOperand(0).relate(rhs);
182 auto r2 = getOperand(1).relate(rhs);
183 return
184 // Both operands must contain the given region for the intersection to
185 // contain it.
186 ((r1 & CONTAINS) & (r2 & CONTAINS))
187 // If either operand is disjoint with the given region, the
188 // intersection is disjoint with it.
189 | ((r1 & DISJOINT) | (r2 & DISJOINT))
190 // If either operand is within the given region, the intersection is
191 // within it.
192 | ((r1 & WITHIN) | (r2 & WITHIN));
193}
194
195} // namespace sphgeom
196} // namespace lsst
py::object result
Definition: _schema.cc:429
int end
This file declares a class for representing axis-aligned bounding boxes in ℝ³.
This file declares a class for representing circular regions on the unit sphere.
This file declares classes for representing compound regions on the unit sphere.
This file declares a class for representing convex polygons with great circle edges on the unit spher...
table::Key< int > b
Box3d represents a box in ℝ³.
Definition: Box3d.h:42
Box represents a rectangle in spherical coordinate space that contains its boundary.
Definition: Box.h:54
Circle is a circular region on the unit sphere that contains its boundary.
Definition: Circle.h:46
CompoundRegion is an intermediate base class for spherical regions that are comprised of a point-set ...
virtual Relationship relate(Region const &r) const =0
Region const & getOperand(std::size_t n) const
std::vector< std::uint8_t > _encode(std::uint8_t tc) const
static std::unique_ptr< CompoundRegion > decode(std::vector< uint8_t > const &s)
CompoundRegion(Region const &first, Region const &second)
Construct by copying or taking ownership of operands.
static std::array< std::unique_ptr< Region >, 2 > _decode(std::uint8_t tc, std::uint8_t const *buffer, std::size_t nBytes)
ConvexPolygon is a closed convex polygon on the unit sphere.
Definition: ConvexPolygon.h:57
Ellipse is an elliptical region on the sphere.
Definition: Ellipse.h:170
IntersectionRegion is a lazy point-set inersection of its operands.
Relationship relate(Region const &r) const override
static std::unique_ptr< IntersectionRegion > decode(std::vector< uint8_t > const &s)
Circle getBoundingCircle() const override
getBoundingCircle returns a bounding-circle for this region.
Box3d getBoundingBox3d() const override
getBoundingBox3d returns a 3-dimensional bounding-box for this region.
bool contains(UnitVector3d const &v) const override
contains tests whether the given unit vector is inside this region.
static constexpr uint8_t TYPE_CODE
Box getBoundingBox() const override
getBoundingBox returns a bounding-box for this region.
Region is a minimal interface for 2-dimensional regions on the unit sphere.
Definition: Region.h:79
virtual Circle getBoundingCircle() const =0
getBoundingCircle returns a bounding-circle for this region.
virtual Relationship relate(Region const &) const =0
static std::unique_ptr< Region > decode(std::vector< uint8_t > const &s)
Definition: Region.h:137
virtual Box getBoundingBox() const =0
getBoundingBox returns a bounding-box for this region.
virtual bool contains(UnitVector3d const &) const =0
contains tests whether the given unit vector is inside this region.
virtual std::vector< uint8_t > encode() const =0
encode serializes this region into an opaque byte string.
virtual Box3d getBoundingBox3d() const =0
getBoundingBox3d returns a 3-dimensional bounding-box for this region.
UnionRegion is a lazy point-set union of its operands.
Box getBoundingBox() const override
getBoundingBox returns a bounding-box for this region.
static std::unique_ptr< UnionRegion > decode(std::vector< uint8_t > const &s)
static constexpr uint8_t TYPE_CODE
Relationship relate(Region const &r) const override
bool contains(UnitVector3d const &v) const override
contains tests whether the given unit vector is inside this region.
Circle getBoundingCircle() const override
getBoundingCircle returns a bounding-circle for this region.
Box3d getBoundingBox3d() const override
getBoundingBox3d returns a 3-dimensional bounding-box for this region.
UnitVector3d is a unit vector in ℝ³ with components stored in double precision.
Definition: UnitVector3d.h:55
This file contains simple helper functions for encoding and decoding primitive types to/from byte str...
T end(T... args)
T insert(T... args)
T move(T... args)
void encodeU64(std::uint64_t item, std::vector< uint8_t > &buffer)
encodeU64 appends an uint64 in little-endian byte order to the end of buffer.
Definition: codec.h:85
std::uint64_t decodeU64(uint8_t const *buffer)
decodeU64 extracts an uint64 from the 8 byte little-endian byte sequence in buffer.
Definition: codec.h:105
A base class for image defects.
T push_back(T... args)
This file declares a class for representing longitude/latitude angle boxes on the unit sphere.
This file declares a class for representing elliptical regions on the unit sphere.