LSSTApplications  12.1-5-gbdcc3ab+2,15.0+13,15.0+26,15.0-1-g19261fa+17,15.0-1-g60afb23+26,15.0-1-g615e0bb+18,15.0-1-g788a293+26,15.0-1-ga91101e+26,15.0-1-gae1598d+12,15.0-1-gd076f1f+24,15.0-1-gdf18595+5,15.0-1-gf4f1c34+12,15.0-11-g7db6e543+4,15.0-12-g3681e7a+4,15.0-15-gc15de322,15.0-16-g83b84f4,15.0-2-g100d730+19,15.0-2-g1f9c9cf+4,15.0-2-g8aea5f4+1,15.0-2-gf38729e+21,15.0-29-ga12a2b06e,15.0-3-g11fe1a0+14,15.0-3-g707930d+3,15.0-3-g9103c06+12,15.0-3-gd3cbb57+3,15.0-4-g2d82b59,15.0-4-g535e784+10,15.0-4-g92ca6c3+4,15.0-4-gf906033+2,15.0-5-g23e394c+14,15.0-5-g4be42a9,15.0-6-g69628aa,15.0-6-g86e3f3d+1,15.0-6-gfa9b38f+4,15.0-7-g949993c+3,15.0-8-g67a62d3+1,15.0-8-gcf05001+1,15.0-9-g1e7c341+1,w.2018.21
LSSTDataManagementBasePackage
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 
33 #include "lsst/pex/exceptions.h"
34 
36 using namespace std;
37 
38 namespace lsst {
39 namespace afw {
40 namespace geom {
42 namespace {
56 Angle haversine(Angle const& deltaLon, Angle const& deltaLat, double cosLat1, double cosLat2) {
57  double const sinLat = sin(deltaLat / 2.0);
58  double const sinLon = sin(deltaLon / 2.0);
59  double const havDDelta = sinLat * sinLat;
60  double const havDAlpha = sinLon * sinLon;
61  double const havD = havDDelta + cosLat1 * cosLat2 * havDAlpha;
62  double const sinDHalf = sqrt(havD);
63  return (2.0 * asin(sinDHalf)) * radians;
64 }
65 } // end namespace
66 
68  : SpherePoint(longitude * units, latitude * units) {}
69 
71  : _longitude(longitude.wrap().asRadians()), _latitude(latitude.asRadians()) {
72  if (fabs(_latitude) > HALFPI) {
73  throw pexExcept::InvalidParameterError("Angle " + to_string(latitude.asDegrees()) +
74  " is not a valid latitude.");
75  }
76 }
77 
79  // sphgeom Vector3d has its own normalization,
80  // but its behavior is not documented for non-finite values
81  double norm = vector.getNorm();
82  if (norm <= 0.0) {
83  stringstream buffer;
84  buffer << "Vector " << vector << " has zero norm and cannot be normalized.";
85  throw pexExcept::InvalidParameterError(buffer.str());
86  }
87  // To avoid unexpected behavior from mixing finite elements and infinite norm
88  if (!isfinite(norm)) {
89  norm = NAN;
90  }
91  auto unitVector =
92  sphgeom::UnitVector3d::fromNormalized(vector.x() / norm, vector.y() / norm, vector.z() / norm);
93  _set(unitVector);
94 }
95 
97  : SpherePoint(lonLat.getLon().asRadians(), lonLat.getLat().asRadians(), radians) {}
98 
99 void SpherePoint::_set(sphgeom::UnitVector3d const& unitVector) {
100  _latitude = asin(unitVector.z());
101  if (!atPole()) {
102  // Need to convert to Angle, Angle::wrap, and convert back to radians
103  // to handle _longitude = -1e-16 without code duplication
104  _longitude = (atan2(unitVector.y(), unitVector.x()) * radians).wrap().asRadians();
105  } else {
106  _longitude = 0;
107  }
108 }
109 
110 SpherePoint::SpherePoint() : _longitude(nan("")), _latitude(nan("")) {}
111 
112 SpherePoint::SpherePoint(SpherePoint const& other) noexcept = default;
113 
114 SpherePoint::SpherePoint(SpherePoint&& other) noexcept = default;
115 
116 SpherePoint& SpherePoint::operator=(SpherePoint const& other) noexcept = default;
117 
118 SpherePoint& SpherePoint::operator=(SpherePoint&& other) noexcept = default;
119 
120 SpherePoint::operator sphgeom::LonLat() const {
121  return sphgeom::LonLat::fromRadians(getLongitude().asRadians(), getLatitude().asRadians());
122 }
123 
124 SpherePoint::~SpherePoint() = default;
125 
127  return sphgeom::UnitVector3d::fromNormalized(cos(_longitude) * cos(_latitude),
128  sin(_longitude) * cos(_latitude), sin(_latitude));
129 }
130 
132  return Point2D(getLongitude().asAngularUnits(unit), getLatitude().asAngularUnits(unit));
133 }
134 
135 Angle SpherePoint::operator[](size_t index) const {
136  switch (index) {
137  case 0:
138  return getLongitude();
139  case 1:
140  return getLatitude();
141  default:
142  throw pexExcept::OutOfRangeError("Index " + to_string(index) + " must be 0 or 1.");
143  }
144 }
145 
146 bool SpherePoint::isFinite() const noexcept { return isfinite(_longitude) && isfinite(_latitude); }
147 
148 bool SpherePoint::operator==(SpherePoint const& other) const noexcept {
149  // Deliberate override of Style Guide 5-12
150  // Approximate FP comparison would make object equality intransitive
151  return _longitude == other._longitude && _latitude == other._latitude;
152 }
153 
154 bool SpherePoint::operator!=(SpherePoint const& other) const noexcept { return !(*this == other); }
155 
157  Angle const deltaLon = other.getLongitude() - this->getLongitude();
158 
159  double const sinDelta1 = sin(getLatitude().asRadians());
160  double const sinDelta2 = sin(other.getLatitude().asRadians());
161  double const cosDelta1 = cos(getLatitude().asRadians());
162  double const cosDelta2 = cos(other.getLatitude().asRadians());
163 
164  // Adapted from http://www.movable-type.co.uk/scripts/latlong.html
165  double const y = sin(deltaLon) * cosDelta2;
166  double const x = cosDelta1 * sinDelta2 - sinDelta1 * cosDelta2 * cos(deltaLon);
167  return (90.0 * degrees - atan2(y, x) * radians).wrap();
168 }
169 
170 Angle SpherePoint::separation(SpherePoint const& other) const noexcept {
171  return haversine(getLongitude() - other.getLongitude(), getLatitude() - other.getLatitude(),
172  cos(getLatitude().asRadians()), cos(other.getLatitude().asRadians()));
173 }
174 
175 SpherePoint SpherePoint::rotated(SpherePoint const& axis, Angle const& amount) const noexcept {
176  auto const rotation = Eigen::AngleAxisd(amount.asRadians(), asEigen(axis.getVector())).matrix();
177  auto const x = asEigen(getVector());
178  auto const xprime = rotation * x;
179  return SpherePoint(sphgeom::Vector3d(xprime[0], xprime[1], xprime[2]));
180 }
181 
182 SpherePoint SpherePoint::offset(Angle const& bearing, Angle const& amount) const {
183  double const phi = bearing.asRadians();
184 
185  // let v = vector in the direction bearing points (tangent to surface of sphere)
186  // To do the rotation, use rotated() method.
187  // - must provide an axis of rotation: take the cross product r x v to get that axis (pole)
188 
189  auto r = getVector();
190 
191  // Get the vector v:
192  // let u = unit vector lying on a parallel of declination
193  // let w = unit vector along line of longitude = r x u
194  // the vector v must satisfy the following:
195  // r . v = 0
196  // u . v = cos(phi)
197  // w . v = sin(phi)
198 
199  // v is a linear combination of u and w
200  // v = cos(phi)*u + sin(phi)*w
201  sphgeom::Vector3d const u(-sin(_longitude), cos(_longitude), 0.0);
202  auto w = r.cross(u);
203  auto v = cos(phi) * u + sin(phi) * w;
204 
205  // take r x v to get the axis
206  SpherePoint axis = SpherePoint(r.cross(v));
207 
208  return rotated(axis, amount);
209 }
210 
212  geom::Angle const alpha1 = this->getLongitude();
213  geom::Angle const delta1 = this->getLatitude();
214  geom::Angle const alpha2 = other.getLongitude();
215  geom::Angle const delta2 = other.getLatitude();
216 
217  // Compute the projection of "other" on a tangent plane centered at this point
218  double const sinDelta1 = std::sin(delta1);
219  double const cosDelta1 = std::cos(delta1);
220  double const sinDelta2 = std::sin(delta2);
221  double const cosDelta2 = std::cos(delta2);
222  double const cosAlphaDiff = std::cos(alpha2 - alpha1);
223  double const sinAlphaDiff = std::sin(alpha2 - alpha1);
224 
225  double const div = cosDelta1 * cosAlphaDiff * cosDelta2 + sinDelta1 * sinDelta2;
226  double const xi = cosDelta1 * sinAlphaDiff / div;
227  double const eta = (cosDelta1 * cosAlphaDiff * sinDelta2 - sinDelta1 * cosDelta2) / div;
228 
229  return std::make_pair(xi * geom::radians, eta * geom::radians);
230 }
231 
233  if (coords.size() == 0) {
234  throw LSST_EXCEPT(pex::exceptions::LengthError, "No coordinates provided to average");
235  }
236  sphgeom::Vector3d sum(0, 0, 0);
237  sphgeom::Vector3d corr(0, 0, 0); // Kahan summation correction
238  for (auto const& sp : coords) {
239  auto const point = sp.getVector();
240  // Kahan summation
241  auto const add = point - corr;
242  auto const temp = sum + add;
243  corr = (temp - sum) - add;
244  sum = temp;
245  }
246  sum /= static_cast<double>(coords.size());
247  return SpherePoint(sum);
248 }
249 
251  // Can't provide atomic guarantee anyway for I/O, so ok to be sloppy.
252  auto oldFlags = os.setf(ostream::fixed);
253  auto oldPrecision = os.precision(6);
254 
255  os << "(" << point.getLongitude().asDegrees() << ", ";
256  os.setf(ostream::showpos);
257  os << point.getLatitude().asDegrees() << ")";
258 
259  os.flags(oldFlags);
260  os.precision(oldPrecision);
261  return os;
262 }
263 } // namespace geom
264 } // namespace afw
265 } // namespace lsst
SpherePoint offset(Angle const &bearing, Angle const &amount) const
Return a point offset from this one along a great circle.
Definition: SpherePoint.cc:182
T setf(T... args)
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:61
bool operator!=(SpherePoint const &other) const noexcept
false if two points represent the same position.
Definition: SpherePoint.cc:154
Angle separation(SpherePoint const &other) const noexcept
Angular distance between two points.
Definition: SpherePoint.cc:170
lsst::afw::geom::Angle Angle
Definition: misc.h:33
table::Key< geom::Angle > longitude
Definition: VisitInfo.cc:166
std::ostream & operator<<(std::ostream &os, SpherePoint const &point)
Print the value of a point to a stream.
Definition: SpherePoint.cc:250
double constexpr HALFPI
Definition: Angle.h:23
AngleUnit constexpr radians
constant with units of radians
Definition: Angle.h:87
Angle getLongitude() const noexcept
The longitude of this point.
Definition: SpherePoint.h:180
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
T to_string(T... args)
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:88
int y
Definition: SpanSet.cc:44
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:131
Angle operator[](size_t index) const
Longitude and latitude by index.
Definition: SpherePoint.cc:135
Vector3d is a vector in ℝ³ with components stored in double precision.
Definition: Vector3d.h:44
double sin(Angle const &a)
Definition: Angle.h:102
bool isFinite() const noexcept
true if this point is a well-defined position.
Definition: SpherePoint.cc:146
def wrap(ctrl)
Definition: wrap.py:250
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:52
double y() const
Definition: Vector3d.h:68
double cos(Angle const &a)
Definition: Angle.h:103
T atan2(T... args)
lsst::afw::geom::SpherePoint SpherePoint
Definition: misc.h:35
T sin(T... args)
T flags(T... args)
A class representing an angle.
Definition: Angle.h:102
double x() const
Definition: Vector3d.h:66
A base class for image defects.
Definition: cameraGeom.dox:3
Eigen::Vector3d asEigen(sphgeom::Vector3d const &vector)
Definition: sphgeomUtils.h:38
Point< double, 2 > Point2D
Definition: Point.h:304
table::Key< geom::Angle > latitude
Definition: VisitInfo.cc:165
T str(T... args)
T make_pair(T... args)
T isfinite(T... args)
T cos(T... args)
T fabs(T... args)
double x
SpherePoint()
Construct a SpherePoint with "nan" for longitude and latitude.
Definition: SpherePoint.cc:110
SpherePoint rotated(SpherePoint const &axis, Angle const &amount) const noexcept
Return a point rotated from this one around an axis.
Definition: SpherePoint.cc:175
Angle bearingTo(SpherePoint const &other) const
Orientation at this point of the great circle arc to another point.
Definition: SpherePoint.cc:156
T div(T... args)
double w
Definition: CoaddPsf.cc:57
T asin(T... args)
Angle getLatitude() const noexcept
The latitude of this point.
Definition: SpherePoint.h:192
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:47
Reports attempts to access elements outside a valid range of indices.
Definition: Runtime.h:89
STL class.
static LonLat fromRadians(double lon, double lat)
Definition: LonLat.h:55
LonLat represents a spherical coordinate (longitude/latitude angle) pair.
Definition: LonLat.h:48
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:148
T nan(T... args)
SpherePoint & operator=(SpherePoint const &other) noexcept
Overwrite this object with the value of another SpherePoint.
double z() const
Definition: Vector3d.h:70
Reports invalid arguments.
Definition: Runtime.h:66
bool atPole() const noexcept
true if this point is either coordinate pole.
Definition: SpherePoint.h:237
ItemVariant const * other
Definition: Schema.cc:55
UnitVector3d is a unit vector in ℝ³ with components stored in double precision.
Definition: UnitVector3d.h:55
T sqrt(T... args)
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:211
STL class.
static UnitVector3d fromNormalized(Vector3d const &v)
fromNormalized returns the unit vector equal to v, which is assumed to be normalized.
Definition: UnitVector3d.h:82
std::ostream * os
Definition: Schema.cc:736
SpherePoint averageSpherePoint(std::vector< SpherePoint > const &coords)
Return the average of a list of coordinates.
Definition: SpherePoint.cc:232
sphgeom::UnitVector3d getVector() const noexcept
A unit vector representation of this point.
Definition: SpherePoint.cc:126
double getNorm() const
getNorm returns the L2 norm of this vector.
Definition: Vector3d.h:81