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
Circle.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
26#include "lsst/sphgeom/Circle.h"
27
28#include <ostream>
29#include <stdexcept>
30
31#include "lsst/sphgeom/Box.h"
32#include "lsst/sphgeom/Box3d.h"
35#include "lsst/sphgeom/codec.h"
36
37
38namespace lsst {
39namespace sphgeom {
40
42 if (a.asRadians() < 0.0) {
43 return -1.0;
44 }
45 if (a.asRadians() >= PI) {
46 return 4.0;
47 }
48 double s = sin(0.5 * a);
49 return 4.0 * s * s;
50}
51
52Angle Circle::openingAngleFor(double squaredChordLength) {
53 // Note: the maximum error in the opening angle (and circle bounding box
54 // width) computations is ~ 2 * MAX_ASIN_ERROR.
55 if (squaredChordLength < 0.0) {
56 return Angle(-1.0);
57 }
58 if (squaredChordLength >= 4.0) {
59 return Angle(PI);
60 }
61 return Angle(2.0 * std::asin(0.5 * std::sqrt(squaredChordLength)));
62}
63
64bool Circle::contains(Circle const & x) const {
65 if (isFull() || x.isEmpty()) {
66 return true;
67 }
68 if (isEmpty() || x.isFull()) {
69 return false;
70 }
71 if (*this == x) {
72 return true;
73 }
74 NormalizedAngle cc(_center, x._center);
75 return _openingAngle >
76 cc + x._openingAngle + 4.0 * Angle(MAX_ASIN_ERROR);
77}
78
79bool Circle::isDisjointFrom(Circle const & x) const {
80 if (isEmpty() || x.isEmpty()) {
81 return true;
82 }
83 if (isFull() || x.isFull()) {
84 return false;
85 }
86 NormalizedAngle cc(_center, x._center);
87 return cc > _openingAngle + x._openingAngle +
88 4.0 * Angle(MAX_ASIN_ERROR);
89}
90
92 *this = contains(x) ? Circle(x) : empty();
93 return *this;
94}
95
97 if (isEmpty() || x.isFull()) {
98 return *this;
99 }
100 if (isFull() || x.isEmpty()) {
101 *this = x;
102 return *this;
103 }
104 Angle a = _openingAngle;
105 Angle b = x._openingAngle;
106 NormalizedAngle cc(_center, x._center);
107 if (cc > a + b + 4.0 * Angle(MAX_ASIN_ERROR)) {
108 // This circle is disjoint from x.
109 *this = empty();
110 return *this;
111 }
112 // The circles (nearly) intersect, or one contains the other.
113 // For now, take the easy route and just use the smaller of
114 // the two circles as a bound on their intersection.
115 //
116 // TODO(smm): Compute the minimal bounding circle.
117 if (b < a) {
118 *this = x;
119 }
120 return *this;
121}
122
124 // For any circle c and unit vector x, c.expandTo(x).contains(x)
125 // should return true.
126 if (isEmpty()) {
127 *this = Circle(x);
128 } else if (!contains(x)) {
129 // Compute the normal vector for the plane defined by _center and x.
131 // The minimal bounding circle (MBC) includes unit vectors on the plane
132 // with normal n that span from _center.rotatedAround(n, -_openingAngle)
133 // to x. The MBC center is the midpoint of this interval.
134 NormalizedAngle cx(_center, x);
135 Angle o = 0.5 * (cx + _openingAngle);
136 Angle r = 0.5 * (cx - _openingAngle);
137 // Rotate _center by angle r around n to obtain the MBC center. This is
138 // done using Rodriques' formula, simplified by taking advantage of the
139 // orthogonality of _center and n.
140 _center = UnitVector3d(_center * cos(r) + n.cross(_center) * sin(r));
141 _squaredChordLength = squaredChordLengthFor(o + Angle(MAX_ASIN_ERROR));
142 _openingAngle = o + Angle(MAX_ASIN_ERROR);
143 }
144 return *this;
145}
146
148 if (isEmpty() || x.isFull()) {
149 *this = x;
150 return *this;
151 }
152 if (x.isEmpty() || isFull()) {
153 return *this;
154 }
155 NormalizedAngle cc(_center, x._center);
156 if (cc + x._openingAngle + 4.0 * Angle(MAX_ASIN_ERROR) <= _openingAngle) {
157 // This circle contains x.
158 return *this;
159 }
160 if (cc + _openingAngle + 4.0 * Angle(MAX_ASIN_ERROR) <= x._openingAngle) {
161 // x contains this circle.
162 *this = x;
163 return *this;
164 }
165 // The circles intersect or are disjoint.
166 Angle o = 0.5 * (cc + _openingAngle + x._openingAngle);
167 if (o + 2.0 * Angle(MAX_ASIN_ERROR) >= Angle(PI)) {
168 *this = full();
169 return *this;
170 }
171 // Compute the normal vector for the plane defined by the circle centers.
172 UnitVector3d n = UnitVector3d::orthogonalTo(_center, x._center);
173 // The minimal bounding circle (MBC) includes unit vectors on the plane
174 // with normal n that span from _center.rotatedAround(n, -_openingAngle)
175 // to x._center.rotatedAround(n, x._openingAngle). The MBC center is the
176 // midpoint of this interval.
177 Angle r = o - _openingAngle;
178 // Rotate _center by angle r around n to obtain the MBC center. This is
179 // done using Rodriques' formula, simplified by taking advantage of the
180 // orthogonality of _center and n.
181 _center = UnitVector3d(_center * cos(r) + n.cross(_center) * sin(r));
182 _squaredChordLength = squaredChordLengthFor(o + Angle(MAX_ASIN_ERROR));
183 _openingAngle = o + Angle(MAX_ASIN_ERROR);
184 return *this;
185}
186
188 if (!isEmpty() && !isFull() &&
189 (r.asRadians() > 0.0 || r.asRadians() < 0.0)) {
190 Angle o = _openingAngle + r;
191 _squaredChordLength = squaredChordLengthFor(o);
192 _openingAngle = o;
193 }
194 return *this;
195}
196
198 if (isEmpty()) {
199 // The complement of an empty circle is a full circle.
200 _squaredChordLength = 4.0;
201 _openingAngle = Angle(PI);
202 } else if (isFull()) {
203 // The complement of a full circle is an empty circle.
204 _squaredChordLength = -1.0;
205 _openingAngle = Angle(-1.0);
206 } else {
207 _center = -_center;
208 _squaredChordLength = 4.0 - _squaredChordLength;
209 _openingAngle = Angle(PI) - _openingAngle;
210 }
211 return *this;
212}
213
215 LonLat c(_center);
216 Angle h = _openingAngle + 2.0 * Angle(MAX_ASIN_ERROR);
219 return Box(c, w, h);
220}
221
223 static double const MAX_BOUNDARY_ERROR = 6.2e-16; // > 5.5ε, where ε = 2^-53
224 if (isEmpty()) {
225 return Box3d();
226 }
227 if (isFull()) {
229 }
230 // Given circle center c and standard basis vector eᵢ, to check whether
231 // ±eᵢ is inside the circle we need to check that (c ∓ eᵢ)·(c ∓ eᵢ) ≤ s.
232 // Since c·c = 1, eᵢ·eᵢ = 1 (c and eᵢ are unit vectors) this is the
233 // same as checking that 2 ∓ 2c·eᵢ ≤ s, or 2 ∓ 2cᵢ ≤ s, where cᵢ is
234 // the i-th component of c.
235 //
236 // Besides any standard basis vectors inside the circle, the bounding box
237 // must also include the circle boundary. To find the extent of this
238 // boundary along a particular axis, note that we can write the i-th
239 // component of the circle center vector as the sine of a latitude angle
240 // (treating the i-th standard basis vector as "north"). So given a circle
241 // opening angle θ, the desired extent is
242 //
243 // [min(sin(asin(cᵢ) ± θ)), max(sin(asin(cᵢ) ± θ))]
244 //
245 // which can be simplified using the usual trigonometric identities to
246 // arrive at the code below.
247 Interval1d e[3];
248 double s = sin(_openingAngle);
249 double c = cos(_openingAngle);
250 for (int i = 0; i < 3; ++i) {
251 double ci = _center(i);
252 double di = std::sqrt(1.0 - ci * ci);
253 double bmin = 1.0, bmax = -1.0;
254 if (2.0 - 2.0 * ci <= _squaredChordLength) {
255 bmax = 1.0;
256 }
257 if (2.0 + 2.0 * ci <= _squaredChordLength) {
258 bmin = -1.0;
259 }
260 double b0 = ci * c + di * s;
261 bmax = std::max(bmax, b0 + MAX_BOUNDARY_ERROR);
262 bmin = std::min(bmin, b0 - MAX_BOUNDARY_ERROR);
263 double b1 = ci * c - di * s;
264 bmax = std::max(bmax, b1 + MAX_BOUNDARY_ERROR);
265 bmin = std::min(bmin, b1 - MAX_BOUNDARY_ERROR);
266 e[i] = Interval1d(std::max(-1.0, bmin), std::min(1.0, bmax));
267 }
268 return Box3d(e[0], e[1], e[2]);
269}
270
272 if (contains(v)) {
273 return CONTAINS;
274 } else if (isEmpty()) {
275 return DISJOINT | WITHIN;
276 }
277 return DISJOINT;
278}
279
281 // Box-Circle relations are implemented by Box.
282 return invert(b.relate(*this));
283}
284
286 if (isEmpty()) {
287 if (c.isEmpty()) {
288 return CONTAINS | DISJOINT | WITHIN;
289 }
290 return DISJOINT | WITHIN;
291 } else if (c.isEmpty()) {
292 return CONTAINS | DISJOINT;
293 }
294 // Neither circle is empty.
295 if (isFull()) {
296 if (c.isFull()) {
297 return CONTAINS | WITHIN;
298 }
299 return CONTAINS;
300 } else if (c.isFull()) {
301 return WITHIN;
302 }
303 // Special case equality, which can be missed by logic below due to
304 // round-off error.
305 if (*this == c) {
306 return CONTAINS | WITHIN;
307 }
308 // Neither circle is full.
309 NormalizedAngle cc(_center, c._center);
310 if (cc > _openingAngle + c._openingAngle + 4.0 * Angle(MAX_ASIN_ERROR)) {
311 return DISJOINT;
312 }
313 if (cc + c._openingAngle + 4.0 * Angle(MAX_ASIN_ERROR) <= _openingAngle) {
314 return CONTAINS;
315 } else if (cc + _openingAngle + 4.0 * Angle(MAX_ASIN_ERROR) <=
316 c._openingAngle) {
317 return WITHIN;
318 }
319 return INTERSECTS;
320}
321
323 // ConvexPolygon-Circle relations are implemented by ConvexPolygon.
324 return invert(p.relate(*this));
325}
326
328 // Ellipse-Circle relations are implemented by Ellipse.
329 return invert(e.relate(*this));
330}
331
334 uint8_t tc = TYPE_CODE;
335 buffer.reserve(ENCODED_SIZE);
336 buffer.push_back(tc);
337 encodeDouble(_center.x(), buffer);
338 encodeDouble(_center.y(), buffer);
339 encodeDouble(_center.z(), buffer);
340 encodeDouble(_squaredChordLength, buffer);
341 encodeDouble(_openingAngle.asRadians(), buffer);
342 return buffer;
343}
344
345std::unique_ptr<Circle> Circle::decode(uint8_t const * buffer, size_t n) {
346 if (buffer == nullptr || n != ENCODED_SIZE || *buffer != TYPE_CODE) {
347 throw std::runtime_error("Byte-string is not an encoded Circle");
348 }
350 ++buffer;
351 double x = decodeDouble(buffer); buffer += 8;
352 double y = decodeDouble(buffer); buffer += 8;
353 double z = decodeDouble(buffer); buffer += 8;
354 double squaredChordLength = decodeDouble(buffer); buffer += 8;
355 double openingAngle = decodeDouble(buffer); buffer += 8;
356 circle->_center = UnitVector3d::fromNormalized(x, y, z);
357 circle->_squaredChordLength = squaredChordLength;
358 circle->_openingAngle = Angle(openingAngle);
359 return circle;
360}
361
363 char tail[32];
364 std::snprintf(tail, sizeof(tail), ", %.17g]}", c.getSquaredChordLength());
365 return os << "{\"Circle\": [" << c.getCenter() << tail;
366}
367
368}} // namespace lsst::sphgeom
This file declares a class for representing axis-aligned bounding boxes in ℝ³.
double x
This file declares a class for representing circular 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
std::ostream * os
Definition: Schema.cc:557
int y
Definition: SpanSet.cc:48
table::Key< int > b
table::Key< int > a
T asin(T... args)
Angle represents an angle in radians.
Definition: Angle.h:43
double asRadians() const
asRadians returns the value of this angle in units of radians.
Definition: Angle.h:85
Box3d represents a box in ℝ³.
Definition: Box3d.h:42
static Box3d aroundUnitSphere()
aroundUnitSphere returns a minimal Box3d containing the unit sphere.
Definition: Box3d.h:56
Box represents a rectangle in spherical coordinate space that contains its boundary.
Definition: Box.h:54
static NormalizedAngle halfWidthForCircle(Angle r, Angle lat)
halfWidthForCircle computes the half-width of bounding boxes for circles with radius r and centers at...
Definition: Box.cc:43
Circle is a circular region on the unit sphere that contains its boundary.
Definition: Circle.h:46
bool contains(Circle const &x) const
contains returns true if the intersection of this circle and x is equal to x.
Definition: Circle.cc:64
static Angle openingAngleFor(double squaredChordLength)
openingAngleFor computes and returns the angular separation between points in S² that are separated b...
Definition: Circle.cc:52
bool isEmpty() const
Definition: Circle.h:109
Box getBoundingBox() const override
getBoundingBox returns a bounding-box for this region.
Definition: Circle.cc:214
static Circle empty()
Definition: Circle.h:50
Circle & clipTo(UnitVector3d const &x)
Definition: Circle.cc:91
Circle & dilateBy(Angle r)
If r is positive, dilateBy increases the opening angle of this circle to include all points within an...
Definition: Circle.cc:187
static Circle full()
Definition: Circle.h:52
static std::unique_ptr< Circle > decode(std::vector< uint8_t > const &s)
Definition: Circle.h:251
bool isFull() const
Definition: Circle.h:114
std::vector< uint8_t > encode() const override
encode serializes this region into an opaque byte string.
Definition: Circle.cc:332
bool isDisjointFrom(UnitVector3d const &x) const
Definition: Circle.h:137
Relationship relate(UnitVector3d const &v) const
Definition: Circle.cc:271
double getSquaredChordLength() const
getSquaredChordLength returns the squared length of chords between the circle center and points on th...
Definition: Circle.h:123
UnitVector3d const & getCenter() const
getCenter returns the center of this circle as a unit vector.
Definition: Circle.h:118
Box3d getBoundingBox3d() const override
getBoundingBox3d returns a 3-dimensional bounding-box for this region.
Definition: Circle.cc:222
Circle & complement()
complement sets this circle to the closure of its complement.
Definition: Circle.cc:197
Circle & expandTo(UnitVector3d const &x)
Definition: Circle.cc:123
Circle()
This constructor creates an empty circle.
Definition: Circle.h:65
static double squaredChordLengthFor(Angle openingAngle)
squaredChordLengthFor computes and returns the squared chord length between points in S² that are sep...
Definition: Circle.cc:41
static constexpr uint8_t TYPE_CODE
Definition: Circle.h:48
ConvexPolygon is a closed convex polygon on the unit sphere.
Definition: ConvexPolygon.h:57
Relationship relate(Region const &r) const override
Ellipse is an elliptical region on the sphere.
Definition: Ellipse.h:170
Relationship relate(Region const &r) const override
Definition: Ellipse.h:289
Interval1d represents closed intervals of ℝ.
Definition: Interval1d.h:40
LonLat represents a spherical coordinate (longitude/latitude angle) pair.
Definition: LonLat.h:48
Angle getLat() const
Definition: LonLat.h:90
NormalizedAngle is an angle that lies in the range [0, 2π), with one exception - a NormalizedAngle ca...
UnitVector3d is a unit vector in ℝ³ with components stored in double precision.
Definition: UnitVector3d.h:55
static UnitVector3d fromNormalized(Vector3d const &v)
fromNormalized returns the unit vector equal to v, which is assumed to be normalized.
Definition: UnitVector3d.h:82
static UnitVector3d orthogonalTo(Vector3d const &v)
orthogonalTo returns an arbitrary unit vector that is orthogonal to v.
Definition: UnitVector3d.cc:34
Vector3d cross(Vector3d const &v) const
cross returns the cross product of this unit vector and v.
Definition: UnitVector3d.h:155
This file contains simple helper functions for encoding and decoding primitive types to/from byte str...
T snprintf(T... args)
T max(T... args)
T min(T... args)
constexpr double MAX_ASIN_ERROR
Definition: constants.h:45
void encodeDouble(double item, std::vector< uint8_t > &buffer)
encodeDouble appends an IEEE double in little-endian byte order to the end of buffer.
Definition: codec.h:49
std::ostream & operator<<(std::ostream &, Angle const &)
Definition: Angle.cc:34
double decodeDouble(uint8_t const *buffer)
decodeDouble extracts an IEEE double from the 8 byte little-endian byte sequence in buffer.
Definition: codec.h:69
constexpr double PI
Definition: constants.h:36
Relationship invert(Relationship r)
Given the relationship between two sets A and B (i.e.
Definition: Relationship.h:55
T push_back(T... args)
T reserve(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.
T sqrt(T... args)
double w
Definition: CoaddPsf.cc:69