lsst.afw  15.0-18-g5f205baaa
SpherePoint.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * See COPYRIGHT file at the top of the source tree.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #include <cmath>
26 #include <string>
27 #include <sstream>
28 
29 #include "Eigen/Geometry"
30 
32 #include "lsst/pex/exceptions.h"
33 
35 using namespace std;
36 
37 namespace lsst {
38 namespace afw {
39 namespace geom {
41 namespace {
55 Angle haversine(Angle const& deltaLon, Angle const& deltaLat, double cosLat1, double cosLat2) {
56  double const sinLat = sin(deltaLat / 2.0);
57  double const sinLon = sin(deltaLon / 2.0);
58  double const havDDelta = sinLat * sinLat;
59  double const havDAlpha = sinLon * sinLon;
60  double const havD = havDDelta + cosLat1 * cosLat2 * havDAlpha;
61  double const sinDHalf = sqrt(havD);
62  return (2.0 * asin(sinDHalf)) * radians;
63 }
64 } // end namespace
65 
67  : SpherePoint(longitude * units, latitude * units) {}
68 
70  : _longitude(longitude.wrap().asRadians()), _latitude(latitude.asRadians()) {
71  if (fabs(_latitude) > HALFPI) {
72  throw pexExcept::InvalidParameterError("Angle " + to_string(latitude.asDegrees()) +
73  " is not a valid latitude.");
74  }
75 }
76 
78  double norm = vector.asEigen().norm();
79  if (norm <= 0.0) {
80  stringstream buffer;
81  buffer << "Vector " << vector << " has zero norm and cannot be normalized.";
82  throw pexExcept::InvalidParameterError(buffer.str());
83  }
84  // To avoid unexpected behavior from mixing finite elements and infinite norm
85  if (!isfinite(norm)) {
86  norm = NAN;
87  }
88 
89  double const x = vector.getX() / norm;
90  double const y = vector.getY() / norm;
91  double const z = vector.getZ() / norm;
92 
93  _latitude = asin(z);
94  if (!atPole()) {
95  // Need to convert to Angle, Angle::wrap, and convert back to radians
96  // to handle _longitude = -1e-16 without code duplication
97  _longitude = (atan2(y, x) * radians).wrap().asRadians();
98  } else {
99  _longitude = 0;
100  }
101 }
102 
103 SpherePoint::SpherePoint() : _longitude(nan("")), _latitude(nan("")) {}
104 
105 SpherePoint::SpherePoint(SpherePoint const& other) noexcept = default;
106 
107 SpherePoint::SpherePoint(SpherePoint&& other) noexcept = default;
108 
109 SpherePoint& SpherePoint::operator=(SpherePoint const& other) noexcept = default;
110 
111 SpherePoint& SpherePoint::operator=(SpherePoint&& other) noexcept = default;
112 
113 SpherePoint::~SpherePoint() = default;
114 
115 Point3D SpherePoint::getVector() const noexcept {
116  return Point3D(cos(_longitude) * cos(_latitude), sin(_longitude) * cos(_latitude), sin(_latitude));
117 }
118 
120  return Point2D(getLongitude().asAngularUnits(unit), getLatitude().asAngularUnits(unit));
121 }
122 
123 Angle SpherePoint::operator[](size_t index) const {
124  switch (index) {
125  case 0:
126  return getLongitude();
127  case 1:
128  return getLatitude();
129  default:
130  throw pexExcept::OutOfRangeError("Index " + to_string(index) + " must be 0 or 1.");
131  }
132 }
133 
134 bool SpherePoint::isFinite() const noexcept { return isfinite(_longitude) && isfinite(_latitude); }
135 
136 bool SpherePoint::operator==(SpherePoint const& other) const noexcept {
137  // Deliberate override of Style Guide 5-12
138  // Approximate FP comparison would make object equality intransitive
139  return _longitude == other._longitude && _latitude == other._latitude;
140 }
141 
142 bool SpherePoint::operator!=(SpherePoint const& other) const noexcept { return !(*this == other); }
143 
145  Angle const deltaLon = other.getLongitude() - this->getLongitude();
146 
147  double const sinDelta1 = sin(getLatitude().asRadians());
148  double const sinDelta2 = sin(other.getLatitude().asRadians());
149  double const cosDelta1 = cos(getLatitude().asRadians());
150  double const cosDelta2 = cos(other.getLatitude().asRadians());
151 
152  // Adapted from http://www.movable-type.co.uk/scripts/latlong.html
153  double const y = sin(deltaLon) * cosDelta2;
154  double const x = cosDelta1 * sinDelta2 - sinDelta1 * cosDelta2 * cos(deltaLon);
155  return (90.0 * degrees - atan2(y, x) * radians).wrap();
156 }
157 
158 Angle SpherePoint::separation(SpherePoint const& other) const noexcept {
159  return haversine(getLongitude() - other.getLongitude(), getLatitude() - other.getLatitude(),
160  cos(getLatitude().asRadians()), cos(other.getLatitude().asRadians()));
161 }
162 
163 SpherePoint SpherePoint::rotated(SpherePoint const& axis, Angle const& amount) const noexcept {
164  auto const rotation = Eigen::AngleAxisd(amount.asRadians(), axis.getVector().asEigen()).matrix();
165  auto const x = getVector().asEigen();
166  auto const xprime = rotation * x;
167  return SpherePoint(Point3D(xprime));
168 }
169 
170 SpherePoint SpherePoint::offset(Angle const& bearing, Angle const& amount) const {
171  double const phi = bearing.asRadians();
172 
173  // let v = vector in the direction bearing points (tangent to surface of sphere)
174  // To do the rotation, use rotated() method.
175  // - must provide an axis of rotation: take the cross product r x v to get that axis (pole)
176 
177  Eigen::Vector3d r = getVector().asEigen();
178 
179  // Get the vector v:
180  // let u = unit vector lying on a parallel of declination
181  // let w = unit vector along line of longitude = r x u
182  // the vector v must satisfy the following:
183  // r . v = 0
184  // u . v = cos(phi)
185  // w . v = sin(phi)
186 
187  // v is a linear combination of u and w
188  // v = cos(phi)*u + sin(phi)*w
189  auto u = Eigen::Vector3d(-sin(_longitude), cos(_longitude), 0.0);
190  auto w = r.cross(u);
191  Eigen::Vector3d v = cos(phi) * u + sin(phi) * w;
192 
193  // take r x v to get the axis
194  SpherePoint axis = SpherePoint(Point3D(r.cross(v)));
195 
196  return rotated(axis, amount);
197 }
198 
200  geom::Angle const alpha1 = this->getLongitude();
201  geom::Angle const delta1 = this->getLatitude();
202  geom::Angle const alpha2 = other.getLongitude();
203  geom::Angle const delta2 = other.getLatitude();
204 
205  // Compute the projection of "other" on a tangent plane centered at this point
206  double const sinDelta1 = std::sin(delta1);
207  double const cosDelta1 = std::cos(delta1);
208  double const sinDelta2 = std::sin(delta2);
209  double const cosDelta2 = std::cos(delta2);
210  double const cosAlphaDiff = std::cos(alpha2 - alpha1);
211  double const sinAlphaDiff = std::sin(alpha2 - alpha1);
212 
213  double const div = cosDelta1 * cosAlphaDiff * cosDelta2 + sinDelta1 * sinDelta2;
214  double const xi = cosDelta1 * sinAlphaDiff / div;
215  double const eta = (cosDelta1 * cosAlphaDiff * sinDelta2 - sinDelta1 * cosDelta2) / div;
216 
217  return std::make_pair(xi * geom::radians, eta * geom::radians);
218 }
219 
221  if (coords.size() == 0) {
222  throw LSST_EXCEPT(pex::exceptions::LengthError, "No coordinates provided to average");
223  }
224  geom::Point3D sum(0, 0, 0);
225  geom::Point3D corr(0, 0, 0); // Kahan summation correction
226  for (auto const & sp : coords) {
227  geom::Point3D const point = sp.getVector();
228  // Kahan summation
229  geom::Extent3D const add = point - corr;
230  geom::Point3D const temp = sum + add;
231  corr = (temp - geom::Extent3D(sum)) - add;
232  sum = temp;
233  }
234  sum.scale(1.0 / coords.size());
235  return SpherePoint(sum);
236 }
237 
239  // Can't provide atomic guarantee anyway for I/O, so ok to be sloppy.
240  auto oldFlags = os.setf(ostream::fixed);
241  auto oldPrecision = os.precision(6);
242 
243  os << "(" << point.getLongitude().asDegrees() << ", ";
244  os.setf(ostream::showpos);
245  os << point.getLatitude().asDegrees() << ")";
246 
247  os.flags(oldFlags);
248  os.precision(oldPrecision);
249  return os;
250 }
251 }
252 }
253 } /* namespace lsst::afw::geom */
SpherePoint offset(Angle const &bearing, Angle const &amount) const
Return a point offset from this one along a great circle.
Definition: SpherePoint.cc:170
T setf(T... args)
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:59
bool operator!=(SpherePoint const &other) const noexcept
false if two points represent the same position.
Definition: SpherePoint.cc:142
Angle separation(SpherePoint const &other) const noexcept
Angular distance between two points.
Definition: SpherePoint.cc:158
lsst::afw::geom::Angle Angle
Definition: misc.h:33
void scale(double factor)
Definition: Point.h:124
std::ostream & operator<<(std::ostream &os, SpherePoint const &point)
Print the value of a point to a stream.
Definition: SpherePoint.cc:238
double constexpr HALFPI
Definition: Angle.h:23
AngleUnit constexpr radians
constant with units of radians
Definition: Angle.h:87
A coordinate class intended to represent offsets and dimensions.
Angle getLongitude() const noexcept
The longitude of this point.
Definition: SpherePoint.h:164
T to_string(T... args)
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:88
T norm(const T &x)
Definition: Integrate.h:194
STL namespace.
T precision(T... args)
Point2D getPosition(AngleUnit unit) const
Return longitude, latitude as a Point2D object.
Definition: SpherePoint.cc:119
Angle operator[](size_t index) const
Longitude and latitude by index.
Definition: SpherePoint.cc:123
ItemVariant const * other
Definition: Schema.cc:55
table::Key< geom::Angle > latitude
Definition: VisitInfo.cc:165
double z
Definition: Match.cc:44
bool isFinite() const noexcept
true if this point is a well-defined position.
Definition: SpherePoint.cc:134
def wrap(ctrl)
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:52
A coordinate class intended to represent absolute positions.
T atan2(T... args)
lsst::afw::geom::SpherePoint SpherePoint
Definition: misc.h:35
T sin(T... args)
T flags(T... args)
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
A class representing an angle.
Definition: Angle.h:102
A base class for image defects.
table::Key< geom::Angle > longitude
Definition: VisitInfo.cc:166
Point< double, 2 > Point2D
Definition: Point.h:304
T str(T... args)
T make_pair(T... args)
T isfinite(T... args)
T cos(T... args)
double x
T fabs(T... args)
SpherePoint()
Construct a SpherePoint with "nan" for longitude and latitude.
Definition: SpherePoint.cc:103
SpherePoint rotated(SpherePoint const &axis, Angle const &amount) const noexcept
Return a point rotated from this one around an axis.
Definition: SpherePoint.cc:163
Angle bearingTo(SpherePoint const &other) const
Orientation at this point of the great circle arc to another point.
Definition: SpherePoint.cc:144
T div(T... args)
T asin(T... args)
Angle getLatitude() const noexcept
The latitude of this point.
Definition: SpherePoint.h:176
T size(T... args)
Extent< double, 3 > Extent3D
Definition: Extent.h:384
#define LSST_EXCEPT(type,...)
STL class.
constexpr double asRadians() const noexcept
Return an Angle&#39;s value in radians.
Definition: Angle.h:141
bool operator==(SpherePoint const &other) const noexcept
true if two points have the same longitude and latitude.
Definition: SpherePoint.cc:136
T nan(T... args)
SpherePoint & operator=(SpherePoint const &other) noexcept
Overwrite this object with the value of another SpherePoint.
bool atPole() const noexcept
true if this point is either coordinate pole.
Definition: SpherePoint.h:221
T sqrt(T... args)
Point3D getVector() const noexcept
A unit vector representation of this point.
Definition: SpherePoint.cc:115
constexpr double asDegrees() const noexcept
Return an Angle&#39;s value in degrees.
Definition: Angle.h:144
std::pair< Angle, Angle > getTangentPlaneOffset(SpherePoint const &other) const
Get the offset from a tangent plane centered at this point to another point.
Definition: SpherePoint.cc:199
std::ostream * os
Definition: Schema.cc:736
Point< double, 3 > Point3D
Definition: Point.h:305
STL class.
int y
Definition: SpanSet.cc:44
SpherePoint averageSpherePoint(std::vector< SpherePoint > const &coords)
Return the average of a list of coordinates.
Definition: SpherePoint.cc:220