lsst.sphgeom  22.0.1-5-g1e25446+dfc68d4d09
Ellipse.h
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 
23 #ifndef LSST_SPHGEOM_ELLIPSE_H_
24 #define LSST_SPHGEOM_ELLIPSE_H_
25 
29 
30 #include <iosfwd>
31 #include <limits>
32 
33 #include "Circle.h"
34 #include "Matrix3d.h"
35 #include "Region.h"
36 #include "UnitVector3d.h"
37 
38 
39 namespace lsst {
40 namespace sphgeom {
41 
170 class Ellipse : public Region {
171 public:
172  static constexpr uint8_t TYPE_CODE = 'e';
173 
174  static Ellipse empty() { return Ellipse(); }
175 
176  static Ellipse full() { return Ellipse().complement(); }
177 
180  _S(1.0),
181  _a(-2.0),
182  _b(-2.0),
183  _gamma(0.0),
184  _tana(std::numeric_limits<double>::infinity()),
185  _tanb(std::numeric_limits<double>::infinity())
186  {}
187 
189  explicit Ellipse(Circle const & c) {
190  *this = Ellipse(c.getCenter(), c.getCenter(), c.getOpeningAngle());
191  }
192 
195  explicit Ellipse(UnitVector3d const & v, Angle alpha = Angle(0.0)) {
196  *this = Ellipse(v, v, alpha);
197  }
198 
201  Ellipse(UnitVector3d const & f1, UnitVector3d const & f2, Angle alpha);
202 
208  Ellipse(UnitVector3d const & center,
209  Angle alpha,
210  Angle beta,
211  Angle orientation);
212 
213  bool operator==(Ellipse const & e) const {
214  return _S == e._S && _a == e._a && _b == e._b;
215  }
216 
217  bool operator!=(Ellipse const & e) const { return !(*this == e); }
218 
219  bool isEmpty() const { return Angle(0.5 * PI) + _a < _gamma; }
220 
221  bool isFull() const { return Angle(0.5 * PI) - _a <= _gamma; }
222 
223  bool isGreatCircle() const { return _a.asRadians() == 0.0; }
224 
225  bool isCircle() const { return _a == _b; }
226 
230  Matrix3d const & getTransformMatrix() const { return _S; }
231 
234  return UnitVector3d::fromNormalized(_S(2,0), _S(2,1), _S(2,2));
235  }
236 
238  UnitVector3d getF1() const {
239  UnitVector3d n = UnitVector3d::fromNormalized(_S(1,0), _S(1,1), _S(1,2));
240  return getCenter().rotatedAround(n, -_gamma);
241  }
242 
244  UnitVector3d getF2() const {
245  UnitVector3d n = UnitVector3d::fromNormalized(_S(1,0), _S(1,1), _S(1,2));
246  return getCenter().rotatedAround(n, _gamma);
247  }
248 
252  Angle getAlpha() const { return Angle(0.5 * PI) + _a; }
253 
257  Angle getBeta() const { return Angle(0.5 * PI) + _b; }
258 
261  Angle getGamma() const { return _gamma; }
262 
265  _S = Matrix3d(-_S(0,0), -_S(0,1), -_S(0,2),
266  _S(1,0), _S(1,1), _S(1,2),
267  -_S(2,0), -_S(2,1), -_S(2,2));
268  _a = -_a;
269  _b = -_b;
270  return *this;
271  }
272 
274  Ellipse complemented() const { return Ellipse(*this).complement(); }
275 
276  // Region interface
277  std::unique_ptr<Region> clone() const override {
278  return std::unique_ptr<Ellipse>(new Ellipse(*this));
279  }
280 
281  Box getBoundingBox() const override;
282  Box3d getBoundingBox3d() const override;
283  Circle getBoundingCircle() const override;
284 
285  bool contains(UnitVector3d const &v) const override;
286 
287  using Region::contains;
288 
289  Relationship relate(Region const & r) const override {
290  // Dispatch on the type of r.
291  return invert(r.relate(*this));
292  }
293 
294  Relationship relate(Box const &) const override;
295  Relationship relate(Circle const &) const override;
296  Relationship relate(ConvexPolygon const &) const override;
297  Relationship relate(Ellipse const &) const override;
298 
299  std::vector<uint8_t> encode() const override;
300 
303  static std::unique_ptr<Ellipse> decode(std::vector<uint8_t> const & s) {
304  return decode(s.data(), s.size());
305  }
306  static std::unique_ptr<Ellipse> decode(uint8_t const * buffer, size_t n);
308 
309 private:
310  static constexpr size_t ENCODED_SIZE = 113;
311 
312  Matrix3d _S;
313  Angle _a; // α - π/2
314  Angle _b; // β - π/2
315  Angle _gamma; // Half the angle between the ellipse foci
316  double _tana; // |tan a| = |cot α|
317  double _tanb; // |tan b| = |cot β|
318 };
319 
320 std::ostream & operator<<(std::ostream &, Ellipse const &);
321 
322 }} // namespace lsst::sphgeom
323 
324 #endif // LSST_SPHGEOM_ELLIPSE_H_
This file declares a class for representing circular regions on the unit sphere.
This file contains a class representing 3x3 real matrices.
This file defines an interface for spherical regions.
This file declares a class for representing unit vectors in ℝ³.
Definition: Angle.h:43
double asRadians() const
asRadians returns the value of this angle in units of radians.
Definition: Angle.h:85
Definition: Box3d.h:42
Definition: Box.h:54
Definition: Circle.h:46
UnitVector3d const & getCenter() const
Definition: Circle.h:118
Angle getOpeningAngle() const
Definition: Circle.h:128
Definition: ConvexPolygon.h:57
Definition: Ellipse.h:170
Matrix3d const & getTransformMatrix() const
Definition: Ellipse.h:230
Angle getAlpha() const
Definition: Ellipse.h:252
UnitVector3d getF2() const
getF2 returns the second focal point of the ellipse.
Definition: Ellipse.h:244
Angle getBeta() const
Definition: Ellipse.h:257
static std::unique_ptr< Ellipse > decode(std::vector< uint8_t > const &s)
Definition: Ellipse.h:303
Relationship relate(Region const &r) const override
Definition: Ellipse.h:289
Circle getBoundingCircle() const override
getBoundingCircle returns a bounding-circle for this region.
Definition: Ellipse.cc:241
Box getBoundingBox() const override
getBoundingBox returns a bounding-box for this region.
Definition: Ellipse.cc:190
Ellipse(Circle const &c)
This constructor creates an ellipse corresponding to the given circle.
Definition: Ellipse.h:189
std::vector< uint8_t > encode() const override
Definition: Ellipse.cc:339
Ellipse(UnitVector3d const &v, Angle alpha=Angle(0.0))
Definition: Ellipse.h:195
Ellipse()
This constructor creates an empty ellipse.
Definition: Ellipse.h:179
Ellipse & complement()
complement sets this ellipse to the closure of its complement.
Definition: Ellipse.h:264
UnitVector3d getF1() const
getF1 returns the first focal point of the ellipse.
Definition: Ellipse.h:238
virtual bool contains(UnitVector3d const &) const=0
contains tests whether the given unit vector is inside this region.
Angle getGamma() const
Definition: Ellipse.h:261
std::unique_ptr< Region > clone() const override
clone returns a deep copy of this region.
Definition: Ellipse.h:277
Ellipse complemented() const
complemented returns the closure of the complement of this ellipse.
Definition: Ellipse.h:274
UnitVector3d getCenter() const
getCenter returns the center of the ellipse as a unit vector.
Definition: Ellipse.h:233
Box3d getBoundingBox3d() const override
getBoundingBox3d returns a 3-dimensional bounding-box for this region.
Definition: Ellipse.cc:237
A 3x3 matrix with real entries stored in double precision.
Definition: Matrix3d.h:38
Definition: Region.h:79
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:55
static UnitVector3d fromNormalized(Vector3d const &v)
Definition: UnitVector3d.h:82
UnitVector3d rotatedAround(UnitVector3d const &k, Angle a) const
Definition: UnitVector3d.h:193
std::bitset< 3 > Relationship
Relationship describes how two sets are related.
Definition: Relationship.h:35
Relationship invert(Relationship r)
Definition: Relationship.h:55