LSST Applications g070148d5b3+33e5256705,g0d53e28543+25c8b88941,g0da5cf3356+2dd1178308,g1081da9e2a+62d12e78cb,g17e5ecfddb+7e422d6136,g1c76d35bf8+ede3a706f7,g295839609d+225697d880,g2e2c1a68ba+cc1f6f037e,g2ffcdf413f+853cd4dcde,g38293774b4+62d12e78cb,g3b44f30a73+d953f1ac34,g48ccf36440+885b902d19,g4b2f1765b6+7dedbde6d2,g5320a0a9f6+0c5d6105b6,g56b687f8c9+ede3a706f7,g5c4744a4d9+ef6ac23297,g5ffd174ac0+0c5d6105b6,g6075d09f38+66af417445,g667d525e37+2ced63db88,g670421136f+2ced63db88,g71f27ac40c+2ced63db88,g774830318a+463cbe8d1f,g7876bc68e5+1d137996f1,g7985c39107+62d12e78cb,g7fdac2220c+0fd8241c05,g96f01af41f+368e6903a7,g9ca82378b8+2ced63db88,g9d27549199+ef6ac23297,gabe93b2c52+e3573e3735,gb065e2a02a+3dfbe639da,gbc3249ced9+0c5d6105b6,gbec6a3398f+0c5d6105b6,gc9534b9d65+35b9f25267,gd01420fc67+0c5d6105b6,geee7ff78d7+a14128c129,gf63283c776+ede3a706f7,gfed783d017+0c5d6105b6,w.2022.47
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.
Definition: ConvexPolygon.h:57
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.
Definition: UnitVector3d.h:55
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.