Loading [MathJax]/extensions/tex2jax.js
LSST Applications 28.0.0,g1653933729+a8ce1bb630,g1a997c3884+a8ce1bb630,g28da252d5a+5bd70b7e6d,g2bbee38e9b+638fca75ac,g2bc492864f+638fca75ac,g3156d2b45e+07302053f8,g347aa1857d+638fca75ac,g35bb328faa+a8ce1bb630,g3a166c0a6a+638fca75ac,g3e281a1b8c+7bbb0b2507,g4005a62e65+17cd334064,g414038480c+5b5cd4fff3,g41af890bb2+4ffae9de63,g4e1a3235cc+0f1912dca3,g6249c6f860+3c3976f90c,g80478fca09+46aba80bd6,g82479be7b0+77990446f6,g858d7b2824+78ba4d1ce1,g89c8672015+f667a5183b,g9125e01d80+a8ce1bb630,ga5288a1d22+2a6264e9ca,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc22bb204ba+78ba4d1ce1,gc28159a63d+638fca75ac,gcf0d15dbbd+32ddb6096f,gd6b7c0dfd1+3e339405e9,gda3e153d99+78ba4d1ce1,gda6a2b7d83+32ddb6096f,gdaeeff99f8+1711a396fd,gdd5a9049c5+b18c39e5e3,ge2409df99d+a5e4577cdc,ge33fd446bb+78ba4d1ce1,ge79ae78c31+638fca75ac,gf0baf85859+64e8883e75,gf5289d68f6+e1b046a8d7,gfa443fc69c+91d9ed1ecf,gfda6b12a05+8419469a56
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 Circle getBoundingCircle() const =0
getBoundingCircle returns a bounding-circle for this region.
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 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.
static std::unique_ptr< Region > decode(std::vector< std::uint8_t > const &s)
Definition Region.h:145
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 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.