lsst.sphgeom gbd998247f1+585e252eca
Loading...
Searching...
No Matches
Circle.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_CIRCLE_H_
31#define LSST_SPHGEOM_CIRCLE_H_
32
36
37#include <iosfwd>
38
39#include "Region.h"
40#include "UnitVector3d.h"
41
42
43namespace lsst {
44namespace sphgeom {
45
53class Circle : public Region {
54public:
55 static constexpr uint8_t TYPE_CODE = 'c';
56
57 static Circle empty() { return Circle(); }
58
59 static Circle full() { return Circle(UnitVector3d::Z(), 4.0); }
60
64 static double squaredChordLengthFor(Angle openingAngle);
65
69 static Angle openingAngleFor(double squaredChordLength);
70
73 _center(UnitVector3d::Z()),
74 _squaredChordLength(-1.0),
75 _openingAngle(-1.0)
76 {}
77
83 explicit Circle(UnitVector3d const & c) :
84 _center(c),
85 _squaredChordLength(0.0),
86 _openingAngle(0.0)
87 {}
88
92 Circle(UnitVector3d const & c, Angle a) :
93 _center(c),
94 _squaredChordLength(squaredChordLengthFor(a)),
95 _openingAngle(a)
96 {}
97
101 Circle(UnitVector3d const & c, double cl2) :
102 _center(c),
103 _squaredChordLength(cl2),
104 _openingAngle(openingAngleFor(cl2))
105 {}
106
107 bool operator==(Circle const & c) const {
108 return (isEmpty() && c.isEmpty()) ||
109 (isFull() && c.isFull()) ||
110 (_center == c._center &&
111 _squaredChordLength == c._squaredChordLength &&
112 _openingAngle == c._openingAngle);
113 }
114 bool operator!=(Circle const & c) const { return !(*this == c); }
115
116 bool isEmpty() const {
117 // Return true when _squaredChordLength is negative or NaN.
118 return !(_squaredChordLength >= 0.0);
119 }
120
121 bool isFull() const { return _squaredChordLength >= 4.0; }
122
125 UnitVector3d const & getCenter() const { return _center; }
126
130 double getSquaredChordLength() const { return _squaredChordLength; }
131
135 Angle getOpeningAngle() const { return _openingAngle; }
136
139 bool contains(Circle const & x) const;
140
144 bool isDisjointFrom(UnitVector3d const & x) const { return !contains(x); }
145 bool isDisjointFrom(Circle const & x) const;
147
151 bool intersects(UnitVector3d const & x) const { return contains(x); }
152 bool intersects(Circle const & x) const { return !isDisjointFrom(x); }
154
158 bool isWithin(UnitVector3d const &) const { return isEmpty(); }
159 bool isWithin(Circle const & x) const { return x.contains(*this); }
161
165 Circle & clipTo(UnitVector3d const & x);
166 Circle & clipTo(Circle const & x);
168
172 Circle clippedTo(UnitVector3d const & x) const {
173 return Circle(*this).clipTo(x);
174 }
175
176 Circle clippedTo(Circle const & x) const {
177 return Circle(*this).clipTo(x);
178 }
180
183 Circle & expandTo(UnitVector3d const & x);
184 Circle & expandTo(Circle const & x);
186
190 Circle expandedTo(UnitVector3d const & x) const {
191 return Circle(*this).expandTo(x);
192 }
193
194 Circle expandedTo(Circle const & x) const {
195 return Circle(*this).expandTo(x);
196 }
198
206 Circle & dilateBy(Angle r);
207 Circle dilatedBy(Angle r) const { return Circle(*this).dilateBy(r); }
208 Circle & erodeBy(Angle r) { return dilateBy(-r); }
209 Circle erodedBy(Angle r) const { return dilatedBy(-r); }
210
212 double getArea() const {
213 return PI * std::max(0.0, std::min(_squaredChordLength, 4.0));
214 }
215
221 Circle & complement();
222
224 Circle complemented() const { return Circle(*this).complement(); }
225
226 Relationship relate(UnitVector3d const & v) const;
227
228 // Region interface
229 std::unique_ptr<Region> clone() const override {
230 return std::unique_ptr<Circle>(new Circle(*this));
231 }
232
233 Box getBoundingBox() const override;
234 Box3d getBoundingBox3d() const override;
235 Circle getBoundingCircle() const override { return *this; }
236
237 bool contains(UnitVector3d const & v) const override {
238 return isFull() ||
239 (v - _center).getSquaredNorm() <= _squaredChordLength;
240 }
241
242 using Region::contains;
243
244 Relationship relate(Region const & r) const override {
245 // Dispatch on the type of r.
246 return invert(r.relate(*this));
247 }
248
249 Relationship relate(Box const &) const override;
250 Relationship relate(Circle const &) const override;
251 Relationship relate(ConvexPolygon const &) const override;
252 Relationship relate(Ellipse const &) const override;
253
254 std::vector<uint8_t> encode() const override;
255
258 static std::unique_ptr<Circle> decode(std::vector<uint8_t> const & s) {
259 return decode(s.data(), s.size());
260 }
261 static std::unique_ptr<Circle> decode(uint8_t const * buffer, size_t n);
263
264private:
265 static constexpr size_t ENCODED_SIZE = 41;
266
267 UnitVector3d _center;
268 double _squaredChordLength;
269 Angle _openingAngle;
270};
271
272std::ostream & operator<<(std::ostream &, Circle const &);
273
274}} // namespace lsst::sphgeom
275
276#endif // LSST_SPHGEOM_CIRCLE_H_
This file defines an interface for spherical regions.
This file declares a class for representing unit vectors in ℝ³.
Definition Angle.h:50
Definition Box3d.h:49
Definition Box.h:61
Definition Circle.h:53
bool contains(Circle const &x) const
Definition Circle.cc:71
static Angle openingAngleFor(double squaredChordLength)
Definition Circle.cc:59
Box getBoundingBox() const override
getBoundingBox returns a bounding-box for this region.
Definition Circle.cc:221
Circle clippedTo(UnitVector3d const &x) const
Definition Circle.h:172
Circle & clipTo(UnitVector3d const &x)
Definition Circle.cc:98
std::unique_ptr< Region > clone() const override
clone returns a deep copy of this region.
Definition Circle.h:229
double getArea() const
getArea returns the area of this circle in steradians.
Definition Circle.h:212
Circle & dilateBy(Angle r)
Definition Circle.cc:194
bool isWithin(UnitVector3d const &) const
Definition Circle.h:158
static std::unique_ptr< Circle > decode(std::vector< uint8_t > const &s)
Definition Circle.h:258
Circle expandedTo(UnitVector3d const &x) const
Definition Circle.h:190
Circle(UnitVector3d const &c, Angle a)
Definition Circle.h:92
std::vector< uint8_t > encode() const override
Definition Circle.cc:339
bool isDisjointFrom(UnitVector3d const &x) const
Definition Circle.h:144
Circle(UnitVector3d const &c, double cl2)
Definition Circle.h:101
Circle(UnitVector3d const &c)
Definition Circle.h:83
double getSquaredChordLength() const
Definition Circle.h:130
bool contains(UnitVector3d const &v) const override
contains tests whether the given unit vector is inside this region.
Definition Circle.h:237
Angle getOpeningAngle() const
Definition Circle.h:135
UnitVector3d const & getCenter() const
Definition Circle.h:125
Box3d getBoundingBox3d() const override
getBoundingBox3d returns a 3-dimensional bounding-box for this region.
Definition Circle.cc:229
bool intersects(UnitVector3d const &x) const
Definition Circle.h:151
Circle & complement()
Definition Circle.cc:204
Circle & expandTo(UnitVector3d const &x)
Definition Circle.cc:130
Circle()
This constructor creates an empty circle.
Definition Circle.h:72
Circle getBoundingCircle() const override
getBoundingCircle returns a bounding-circle for this region.
Definition Circle.h:235
static double squaredChordLengthFor(Angle openingAngle)
Definition Circle.cc:48
Circle complemented() const
complemented returns the closure of the complement of this circle.
Definition Circle.h:224
Relationship relate(Region const &r) const override
Definition Circle.h:244
Definition ConvexPolygon.h:64
Definition Ellipse.h:177
Definition Region.h:86
virtual Relationship relate(Region const &) const =0
virtual bool contains(UnitVector3d const &) const =0
contains tests whether the given unit vector is inside this region.
Definition UnitVector3d.h:62
Relationship invert(Relationship r)
Definition Relationship.h:62
std::bitset< 3 > Relationship
Relationship describes how two sets are related.
Definition Relationship.h:42