LSST Applications g06d8191974+de063e15a7,g180d380827+d0b6459378,g2079a07aa2+86d27d4dc4,g2305ad1205+f1ae3263cc,g29320951ab+5752d78b6e,g2bbee38e9b+85cf0a37e7,g337abbeb29+85cf0a37e7,g33d1c0ed96+85cf0a37e7,g3a166c0a6a+85cf0a37e7,g3ddfee87b4+b5254b9343,g48712c4677+9ea88d309d,g487adcacf7+05f7dba17f,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+48904e3942,g64a986408d+de063e15a7,g858d7b2824+de063e15a7,g864b0138d7+33ab2bc355,g8a8a8dda67+585e252eca,g99cad8db69+4508353287,g9c22b2923f+53520f316c,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+ccb7f83a87,gc120e1dc64+6caf640b9b,gc28159a63d+85cf0a37e7,gc3e9b769f7+548c5e05a3,gcf0d15dbbd+b5254b9343,gdaeeff99f8+f9a426f77a,ge6526c86ff+515b6c9330,ge79ae78c31+85cf0a37e7,gee10cc3b42+585e252eca,gff1a9f87cc+de063e15a7,w.2024.17
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.