lsst.sphgeom g99f1e191f7+663e2227e0
Loading...
Searching...
No Matches
CompoundRegion.h
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
30#ifndef LSST_SPHGEOM_COMPOUND_REGION_H_
31#define LSST_SPHGEOM_COMPOUND_REGION_H_
32
36
37#include <iosfwd>
38#include <iterator>
39#include <vector>
40#include <cstdint>
41
42#include "Region.h"
43#include "UnitVector3d.h"
44
45namespace lsst {
46namespace sphgeom {
47
48class Box;
49class Circle;
50class ConvexPolygon;
51class Ellipse;
52
55class CompoundRegion : public Region {
56public:
58
59 explicit CompoundRegion(std::vector<std::unique_ptr<Region>> operands) noexcept;
61
63 CompoundRegion(CompoundRegion &&) noexcept = default;
64
65 // Disable assignment (including for subclasses) because it makes it hard
66 // to guarantee memory safety for operand accessors in Python.
67 CompoundRegion &operator=(CompoundRegion const &) = delete;
68 CompoundRegion &operator=(CompoundRegion &&) = delete;
69
70 // Return number of operands
71 size_t nOperands() const { return _operands.size(); }
72
73 // Return references to the operands.
74 Region const & getOperand(std::size_t n) const {
75 return *_operands[n];
76 }
77
78 // Region interface.
79 // Redeclared as pure virtual so that the overloads below do not hide
80 // it, and so that subclasses are still required to implement it.
81 Relationship relate(Region const &r) const override = 0;
82 Relationship relate(Box const &b) const override;
83 Relationship relate(Circle const &c) const override;
84 Relationship relate(ConvexPolygon const &p) const override;
85 Relationship relate(Ellipse const &e) const override;
86
90 static std::unique_ptr<CompoundRegion> decode(std::vector<std::uint8_t> const &s) {
91 return decode(s.data(), s.size());
92 }
93 static std::unique_ptr<CompoundRegion> decode(std::uint8_t const *buffer, size_t n);
95
96protected:
97
98 // Implementation helper for encode().
99 std::vector<std::uint8_t> _encode(std::uint8_t tc) const;
100
101 // Implementation helper for decode().
102 static std::vector<std::unique_ptr<Region>> _decode(
103 std::uint8_t tc, std::uint8_t const *buffer, std::size_t nBytes);
104
105 // Provide read access to all operands for quick iteration.
106 std::vector<std::unique_ptr<Region>> const& operands() const { return _operands; }
107
108 // Flatten vector of regions in-place.
109 template <typename Compound>
110 void flatten_operands();
111
112private:
113 std::vector<std::unique_ptr<Region>> _operands;
114};
115
121public:
122 static constexpr std::uint8_t TYPE_CODE = 'u';
123
125 explicit UnionRegion(std::vector<std::unique_ptr<Region>> operands);
126
127 // Region interface.
128 std::unique_ptr<Region> clone() const override { return std::make_unique<UnionRegion>(*this); }
129 bool isEmpty() const override;
130 Box getBoundingBox() const override;
131 Box3d getBoundingBox3d() const override;
132 Circle getBoundingCircle() const override;
133 using Region::contains;
134 bool contains(UnitVector3d const &v) const override;
135 Relationship relate(Region const &r) const override;
136 TriState overlaps(Region const& other) const override;
137 TriState overlaps(Box const &) const override;
138 TriState overlaps(Circle const &) const override;
139 TriState overlaps(ConvexPolygon const &) const override;
140 TriState overlaps(Ellipse const &) const override;
141 std::vector<std::uint8_t> encode() const override { return _encode(TYPE_CODE); }
142
146 static std::unique_ptr<UnionRegion> decode(std::vector<std::uint8_t> const &s) {
147 return decode(s.data(), s.size());
148 }
149 static std::unique_ptr<UnionRegion> decode(std::uint8_t const *buffer, size_t n) {
150 return std::make_unique<UnionRegion>(_decode(TYPE_CODE, buffer, n));
151 }
153
154private:
155 std::string toIvoaStcsBody(std::string const & frame = "") const override;
156};
157
163public:
164 static constexpr std::uint8_t TYPE_CODE = 'i';
165
167 explicit IntersectionRegion(std::vector<std::unique_ptr<Region>> operands);
168
169 // Region interface.
170 std::unique_ptr<Region> clone() const override { return std::make_unique<IntersectionRegion>(*this); }
171 bool isEmpty() const override;
172 Box getBoundingBox() const override;
173 Box3d getBoundingBox3d() const override;
174 Circle getBoundingCircle() const override;
175 using Region::contains;
176 bool contains(UnitVector3d const &v) const override;
177 Relationship relate(Region const &r) const override;
178 TriState overlaps(Region const& other) const override;
179 TriState overlaps(Box const &) const override;
180 TriState overlaps(Circle const &) const override;
181 TriState overlaps(ConvexPolygon const &) const override;
182 TriState overlaps(Ellipse const &) const override;
183 std::vector<std::uint8_t> encode() const override { return _encode(TYPE_CODE); }
184
188 static std::unique_ptr<IntersectionRegion> decode(std::vector<std::uint8_t> const &s) {
189 return decode(s.data(), s.size());
190 }
191 static std::unique_ptr<IntersectionRegion> decode(std::uint8_t const *buffer, size_t n) {
192 return std::make_unique<IntersectionRegion>(_decode(TYPE_CODE, buffer, n));
193 }
195
196private:
197 std::string toIvoaStcsBody(std::string const & frame = "") const override;
198};
199
200} // namespace sphgeom
201} // namespace lsst
202
203#endif // LSST_SPHGEOM_COMPOUND_REGION_H_
This file defines an interface for spherical regions.
This file declares a class for representing unit vectors in ℝ³.
Definition Box3d.h:49
Definition Box.h:62
Definition Circle.h:54
Relationship relate(Region const &r) const override=0
CompoundRegion(std::vector< std::unique_ptr< Region > > operands) noexcept
Construct by taking ownership of operands.
Definition CompoundRegion.cc:90
static std::unique_ptr< CompoundRegion > decode(std::vector< std::uint8_t > const &s)
Definition CompoundRegion.h:90
Definition ConvexPolygon.h:65
Definition Ellipse.h:177
Relationship relate(Region const &r) const override
Definition CompoundRegion.cc:334
IntersectionRegion(std::vector< std::unique_ptr< Region > > operands)
Construct by taking ownership of operands.
Definition CompoundRegion.cc:281
static std::unique_ptr< IntersectionRegion > decode(std::vector< std::uint8_t > const &s)
Definition CompoundRegion.h:188
std::unique_ptr< Region > clone() const override
clone returns a deep copy of this region.
Definition CompoundRegion.h:170
Circle getBoundingCircle() const override
getBoundingCircle returns a bounding-circle for this region.
Definition CompoundRegion.cc:321
bool isEmpty() const override
isEmpty returns true when a region does not contain any points.
Definition CompoundRegion.cc:287
Box3d getBoundingBox3d() const override
getBoundingBox3d returns a 3-dimensional bounding-box for this region.
Definition CompoundRegion.cc:317
bool contains(UnitVector3d const &v) const override
contains tests whether the given unit vector is inside this region.
Definition CompoundRegion.cc:325
Box getBoundingBox() const override
getBoundingBox returns a bounding-box for this region.
Definition CompoundRegion.cc:313
std::vector< std::uint8_t > encode() const override
Definition CompoundRegion.h:183
TriState overlaps(Region const &other) const override
Definition CompoundRegion.cc:363
Definition Region.h:105
virtual bool contains(UnitVector3d const &) const =0
contains tests whether the given unit vector is inside this region.
Definition TriState.h:46
Box getBoundingBox() const override
getBoundingBox returns a bounding-box for this region.
Definition CompoundRegion.cc:189
std::vector< std::uint8_t > encode() const override
Definition CompoundRegion.h:141
TriState overlaps(Region const &other) const override
Definition CompoundRegion.cc:241
bool isEmpty() const override
isEmpty returns true when a region does not contain any points.
Definition CompoundRegion.cc:179
Relationship relate(Region const &r) const override
Definition CompoundRegion.cc:210
bool contains(UnitVector3d const &v) const override
contains tests whether the given unit vector is inside this region.
Definition CompoundRegion.cc:201
UnionRegion(std::vector< std::unique_ptr< Region > > operands)
Construct by taking ownership of operands.
Definition CompoundRegion.cc:173
std::unique_ptr< Region > clone() const override
clone returns a deep copy of this region.
Definition CompoundRegion.h:128
Circle getBoundingCircle() const override
getBoundingCircle returns a bounding-circle for this region.
Definition CompoundRegion.cc:197
static std::unique_ptr< UnionRegion > decode(std::vector< std::uint8_t > const &s)
Definition CompoundRegion.h:146
Box3d getBoundingBox3d() const override
getBoundingBox3d returns a 3-dimensional bounding-box for this region.
Definition CompoundRegion.cc:193
Definition UnitVector3d.h:62
Definition Angle.h:45
std::bitset< 3 > Relationship
Relationship describes how two sets are related.
Definition Relationship.h:42