LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
LSST Data Management Base Package
Loading...
Searching...
No Matches
Region.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
33#include <stdexcept>
34#include <algorithm>
35
36#include "lsst/sphgeom/Region.h"
37
38#include "lsst/sphgeom/Box.h"
39#include "lsst/sphgeom/Circle.h"
44
45#include "base64.hpp"
46
47namespace lsst {
48namespace sphgeom {
49
50bool Region::contains(double x, double y, double z) const {
51 return contains(UnitVector3d(x, y, z));
52}
53
54bool Region::contains(double lon, double lat) const {
55 return contains(UnitVector3d(LonLat::fromRadians(lon, lat)));
56}
57
59Region::overlaps(Region const& other) const {
60 // Default implementation just uses `relate`, and it returns unknown state
61 // more frequently, subclasses will want to implement better tests.
62 auto r = this->relate(other);
63 if ((r & DISJOINT).any()) {
64 return TriState(false);
65 } else if ((r & (CONTAINS | WITHIN)).any()) {
66 return TriState(true);
67 } else {
68 return TriState();
69 }
70}
71
73 if (buffer == nullptr || n == 0) {
74 throw std::runtime_error("Byte-string is not an encoded Region");
75 }
76 std::uint8_t type = *buffer;
77 if (type == Box::TYPE_CODE) {
78 return Box::decode(buffer, n);
79 } else if (type == Circle::TYPE_CODE) {
80 return Circle::decode(buffer, n);
81 } else if (type == ConvexPolygon::TYPE_CODE) {
82 return ConvexPolygon::decode(buffer, n);
83 } else if (type == Ellipse::TYPE_CODE) {
84 return Ellipse::decode(buffer, n);
85 } else if (type == UnionRegion::TYPE_CODE) {
86 return UnionRegion::decode(buffer, n);
87 } else if (type == IntersectionRegion::TYPE_CODE) {
88 return IntersectionRegion::decode(buffer, n);
89 }
90 throw std::runtime_error("Byte-string is not an encoded Region");
91}
92
93std::unique_ptr<Region> Region::decodeBase64(std::string_view const & s) {
94 if (s.empty()) {
96 }
97 auto region_begin = s.begin();
98 auto region_end = std::find(s.begin(), s.end(), ':');
99 if (region_end != s.end()) {
101 while (region_end != s.end()) {
102 auto bytes = base64::decode_into<std::vector<std::uint8_t>>(region_begin, region_end);
103 union_args.push_back(decode(bytes));
104 region_begin = region_end;
105 ++region_begin;
106 region_end = std::find(region_begin, s.end(), ':');
107 }
108 auto bytes = base64::decode_into<std::vector<std::uint8_t>>(region_begin, region_end);
109 union_args.push_back(decode(bytes));
110 return std::unique_ptr<UnionRegion>(new UnionRegion(std::move(union_args)));
111 } else {
112 auto bytes = base64::decode_into<std::vector<std::uint8_t>>(region_begin, region_end);
113 return decode(bytes);
114 }
115}
116
117TriState Region::decodeOverlapsBase64(std::string_view const & s) {
118 TriState result(false);
119 if (s.empty()) {
120 // False makes the most sense as the limit of a logical OR of zero
121 // terms (e.g. `any([])` in Python).
122 return result;
123 }
124 auto begin = s.begin();
125 while (result != true) { // if result is known to be true, we're done.
126 auto mid = std::find(begin, s.end(), '&');
127 if (mid == s.end()) {
128 throw std::runtime_error("No '&' found in encoded overlap expression term.");
129 }
130 auto a = Region::decode(base64::decode_into<std::vector<std::uint8_t>>(begin, mid));
131 ++mid;
132 auto end = std::find(mid, s.end(), '|');
133 auto b = Region::decode(base64::decode_into<std::vector<std::uint8_t>>(mid, end));
134 result = result | a->overlaps(*b);
135 if (end == s.end()) {
136 break;
137 } else {
138 begin = end;
139 ++begin;
140 }
141 }
142 return result;
143}
144
147 if (auto union_region = dynamic_cast<UnionRegion const *>(&region)) {
148 for(unsigned i = 0; i < union_region->nOperands(); ++i) {
149 result.emplace_back(union_region->getOperand(i).clone());
150 }
151 } else if(auto intersection_region = dynamic_cast<IntersectionRegion const *>(&region)) {
152 for(unsigned i = 0; i < intersection_region->nOperands(); ++i) {
153 result.emplace_back(intersection_region->getOperand(i).clone());
154 }
155 } else {
156 result.emplace_back(region.clone());
157 }
158 return result;
159}
160
161
162}} // namespace lsst:sphgeom
py::object result
Definition _schema.cc:429
int end
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...
double z
Definition Match.cc:44
This file defines an interface for spherical regions.
int y
Definition SpanSet.cc:48
table::Key< int > b
This file declares a class for representing unit vectors in ℝ³.
table::Key< table::Array< std::uint8_t > > bytes
Definition python.h:135
static std::unique_ptr< Box > decode(std::vector< std::uint8_t > const &s)
Definition Box.h:356
static constexpr std::uint8_t TYPE_CODE
Definition Box.h:64
static constexpr std::uint8_t TYPE_CODE
Definition Circle.h:56
static std::unique_ptr< Circle > decode(std::vector< std::uint8_t > const &s)
Definition Circle.h:267
static constexpr std::uint8_t TYPE_CODE
static std::unique_ptr< ConvexPolygon > decode(std::vector< std::uint8_t > const &s)
static constexpr std::uint8_t TYPE_CODE
Definition Ellipse.h:179
static std::unique_ptr< Ellipse > decode(std::vector< std::uint8_t > const &s)
Definition Ellipse.h:318
IntersectionRegion is a lazy point-set inersection of its operands.
static std::unique_ptr< IntersectionRegion > decode(std::vector< std::uint8_t > const &s)
static constexpr std::uint8_t TYPE_CODE
static LonLat fromRadians(double lon, double lat)
Definition LonLat.h:62
Region is a minimal interface for 2-dimensional regions on the unit sphere.
Definition Region.h:89
static std::unique_ptr< Region > decodeBase64(std::string const &s)
Definition Region.h:176
virtual Relationship relate(Region const &) const =0
static std::vector< std::unique_ptr< Region > > getRegions(Region const &region)
getRegions returns a vector of Region.
Definition Region.cc:145
virtual bool contains(UnitVector3d const &) const =0
contains tests whether the given unit vector is inside this region.
virtual std::unique_ptr< Region > clone() const =0
clone returns a deep copy of this region.
static std::unique_ptr< Region > decode(std::vector< std::uint8_t > const &s)
Definition Region.h:162
virtual TriState overlaps(Region const &other) const =0
Definition Region.cc:59
static TriState decodeOverlapsBase64(std::string const &s)
Definition Region.h:190
TriState represents a boolean value with additional unknown state.
Definition TriState.h:46
UnionRegion is a lazy point-set union of its operands.
static constexpr std::uint8_t TYPE_CODE
static std::unique_ptr< UnionRegion > decode(std::vector< std::uint8_t > const &s)
UnitVector3d is a unit vector in ℝ³ with components stored in double precision.
T emplace_back(T... args)
T find(T... args)
T move(T... args)
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.