LSST Applications 26.0.0,g0265f82a02+6660c170cc,g07994bdeae+30b05a742e,g0a0026dc87+17526d298f,g0a60f58ba1+17526d298f,g0e4bf8285c+96dd2c2ea9,g0ecae5effc+c266a536c8,g1e7d6db67d+6f7cb1f4bb,g26482f50c6+6346c0633c,g2bbee38e9b+6660c170cc,g2cc88a2952+0a4e78cd49,g3273194fdb+f6908454ef,g337abbeb29+6660c170cc,g337c41fc51+9a8f8f0815,g37c6e7c3d5+7bbafe9d37,g44018dc512+6660c170cc,g4a941329ef+4f7594a38e,g4c90b7bd52+5145c320d2,g58be5f913a+bea990ba40,g635b316a6c+8d6b3a3e56,g67924a670a+bfead8c487,g6ae5381d9b+81bc2a20b4,g93c4d6e787+26b17396bd,g98cecbdb62+ed2cb6d659,g98ffbb4407+81bc2a20b4,g9ddcbc5298+7f7571301f,ga1e77700b3+99e9273977,gae46bcf261+6660c170cc,gb2715bf1a1+17526d298f,gc86a011abf+17526d298f,gcf0d15dbbd+96dd2c2ea9,gdaeeff99f8+0d8dbea60f,gdb4ec4c597+6660c170cc,ge23793e450+96dd2c2ea9,gf041782ebf+171108ac67
LSST Data Management Base Package
Loading...
Searching...
No Matches
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
70CompoundRegion::CompoundRegion(Region const &first, Region const &second)
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.
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.
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:88
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:108
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.