LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
CompoundRegion.cc
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
32
34
35#include <iostream>
36#include <stdexcept>
37
38#include "lsst/sphgeom/Box.h"
39#include "lsst/sphgeom/Box3d.h"
40#include "lsst/sphgeom/Circle.h"
43#include "lsst/sphgeom/codec.h"
44
45namespace lsst {
46namespace sphgeom {
47
48namespace {
49
50// A version of decodeU64 from codec.h that checks for buffer overruns and
51// increments the buffer pointer it is given.
52std::uint64_t consumeDecodeU64(std::uint8_t const *&buffer, std::uint8_t const *end) {
53 if (buffer + 8 > end) {
54 throw std::runtime_error("Encoded CompoundRegion is truncated.");
55 }
57 buffer += 8;
58 return result;
60
61template <typename F>
62auto getUnionBounds(UnionRegion const &compound, F func) {
63 auto bounds = func(compound.getOperand(0));
64 bounds.expandTo(func(compound.getOperand(1)));
65 return bounds;
66}
67
68template <typename F>
69auto getIntersectionBounds(IntersectionRegion const &compound, F func) {
70 auto bounds = func(compound.getOperand(0));
71 bounds.clipTo(func(compound.getOperand(1)));
72 return bounds;
73}
74
75} // namespace
76
77CompoundRegion::CompoundRegion(Region const &first, Region const &second)
78 : _operands{first.clone(), second.clone()} {}
79
81 std::array<std::unique_ptr<Region>, 2> operands) noexcept
82 : _operands(std::move(operands)) {}
83
85 : _operands{other.getOperand(0).clone(), other.getOperand(1).clone()} {}
86
87Relationship CompoundRegion::relate(Box const &b) const { return relate(static_cast<Region const &>(b)); }
88Relationship CompoundRegion::relate(Circle const &c) const { return relate(static_cast<Region const &>(c)); }
89Relationship CompoundRegion::relate(ConvexPolygon const &p) const { return relate(static_cast<Region const &>(p)); }
90Relationship CompoundRegion::relate(Ellipse const &e) const { return relate(static_cast<Region const &>(e)); }
91
94 buffer.push_back(tc);
95 auto buffer1 = getOperand(0).encode();
96 encodeU64(buffer1.size(), buffer);
97 buffer.insert(buffer.end(), buffer1.begin(), buffer1.end());
98 auto buffer2 = getOperand(1).encode();
99 encodeU64(buffer2.size(), buffer);
100 buffer.insert(buffer.end(), buffer2.begin(), buffer2.end());
101 return buffer;
102}
103
105 std::uint8_t tc, std::uint8_t const *buffer, std::size_t nBytes) {
106 std::uint8_t const *end = buffer + nBytes;
107 if (nBytes == 0) {
108 throw std::runtime_error("Encoded CompoundRegion is truncated.");
109 }
110 if (buffer[0] != tc) {
111 throw std::runtime_error("Byte string is not an encoded CompoundRegion.");
112 }
113 ++buffer;
115 std::uint64_t nBytes1 = consumeDecodeU64(buffer, end);
116 result[0] = Region::decode(buffer, nBytes1);
117 buffer += nBytes1;
118 std::uint64_t nBytes2 = consumeDecodeU64(buffer, end);
119 result[1] = Region::decode(buffer, nBytes2);
120 buffer += nBytes2;
121 if (buffer != end) {
122 throw std::runtime_error("Encoded CompoundRegion is has unexpected additional bytes.");
123 }
124 return result;
125}
126
128 if (n == 0) {
129 throw std::runtime_error("Encoded CompoundRegion is truncated.");
130 }
131 switch (buffer[0]) {
133 return UnionRegion::decode(buffer, n);
135 return IntersectionRegion::decode(buffer, n);
136 default:
137 throw std::runtime_error("Byte string is not an encoded CompoundRegion.");
138 }
139}
140
142 return getUnionBounds(*this, [](Region const &r) { return r.getBoundingBox(); });
143}
144
146 return getUnionBounds(*this, [](Region const &r) { return r.getBoundingBox3d(); });
147}
148
150 return getUnionBounds(*this, [](Region const &r) { return r.getBoundingCircle(); });
151}
152
154 return getOperand(0).contains(v) || getOperand(1).contains(v);
155}
156
158 auto r1 = getOperand(0).relate(rhs);
159 auto r2 = getOperand(1).relate(rhs);
160 return
161 // Both operands must be disjoint with the given region for the union
162 // to be disjoint with it.
163 ((r1 & DISJOINT) & (r2 & DISJOINT))
164 // Both operands must be within the given region for the union to be
165 // within it.
166 | ((r1 & WITHIN) & (r2 & WITHIN))
167 // If either operand contains the given region, the union contains it.
168 | ((r1 & CONTAINS) | (r2 & CONTAINS));
169}
170
172 return getIntersectionBounds(*this, [](Region const &r) { return r.getBoundingBox(); });
173}
174
176 return getIntersectionBounds(*this, [](Region const &r) { return r.getBoundingBox3d(); });
177}
178
180 return getIntersectionBounds(*this, [](Region const &r) { return r.getBoundingCircle(); });
181}
182
184 return getOperand(0).contains(v) && getOperand(1).contains(v);
185}
186
188 auto r1 = getOperand(0).relate(rhs);
189 auto r2 = getOperand(1).relate(rhs);
190 return
191 // Both operands must contain the given region for the intersection to
192 // contain it.
193 ((r1 & CONTAINS) & (r2 & CONTAINS))
194 // If either operand is disjoint with the given region, the
195 // intersection is disjoint with it.
196 | ((r1 & DISJOINT) | (r2 & DISJOINT))
197 // If either operand is within the given region, the intersection is
198 // within it.
199 | ((r1 & WITHIN) | (r2 & WITHIN));
200}
201
202} // namespace sphgeom
203} // 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:49
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
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.
Ellipse is an elliptical region on the sphere.
Definition Ellipse.h:177
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:86
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:144
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.
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:95
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:115
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.