LSST Applications 24.1.6,g063fba187b+e7121a6b04,g0f08755f38+4e0faf0f7f,g12f32b3c4e+7915c4de30,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g28da252d5a+94d9f37a33,g2bbee38e9b+ae03bbfc84,g2bc492864f+ae03bbfc84,g3156d2b45e+6e55a43351,g347aa1857d+ae03bbfc84,g35bb328faa+a8ce1bb630,g3a166c0a6a+ae03bbfc84,g3e281a1b8c+c5dd892a6c,g414038480c+6b9177ef31,g41af890bb2+9e154f3e8d,g6b1c1869cb+adc49b6f1a,g781aacb6e4+a8ce1bb630,g7af13505b9+3363a39af3,g7f202ee025+406ba613a5,g80478fca09+8fbba356e2,g82479be7b0+0d223595df,g858d7b2824+4e0faf0f7f,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,g9726552aa6+414189b318,ga5288a1d22+32d6120315,gacef1a1666+7f85da65db,gb58c049af0+d64f4d3760,gbcfae0f0a0+a8c62e8bb6,gc28159a63d+ae03bbfc84,gcf0d15dbbd+412a8a6f35,gda6a2b7d83+412a8a6f35,gdaeeff99f8+1711a396fd,ge79ae78c31+ae03bbfc84,gf0baf85859+c1f95f4921,gfa517265be+4e0faf0f7f,gfa999e8aa5+17cd334064,gfb92a5be7c+4e0faf0f7f
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;
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:62
Circle is a circular region on the unit sphere that contains its boundary.
Definition Circle.h:54
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
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)
static std::unique_ptr< CompoundRegion > decode(std::vector< std::uint8_t > const &s)
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< std::uint8_t > const &s)
static constexpr std::uint8_t TYPE_CODE
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.
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:87
virtual Relationship relate(Region const &) const =0
virtual std::vector< std::uint8_t > encode() const =0
encode serializes this region into an opaque byte string.
virtual bool contains(UnitVector3d const &) const =0
contains tests whether the given unit vector is inside this region.
static std::unique_ptr< Region > decode(std::vector< std::uint8_t > const &s)
Definition Region.h:145
UnionRegion is a lazy point-set union of its operands.
Box getBoundingBox() const override
getBoundingBox returns a bounding-box for this region.
static constexpr std::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.
static std::unique_ptr< UnionRegion > decode(std::vector< std::uint8_t > const &s)
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)
std::uint64_t decodeU64(std::uint8_t const *buffer)
decodeU64 extracts an uint64 from the 8 byte little-endian byte sequence in buffer.
Definition codec.h:116
void encodeU64(std::uint64_t item, std::vector< std::uint8_t > &buffer)
encodeU64 appends an uint64 in little-endian byte order to the end of buffer.
Definition codec.h:96
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.