LSST Applications 24.1.6,g063fba187b+e7121a6b04,g0f08755f38+4e0faf0f7f,g12f32b3c4e+7915c4de30,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g28da252d5a+94d9f37a33,g2bbee38e9b+ae03bbfc84,g2bc492864f+ae03bbfc84,g3156d2b45e+6e55a43351,g347aa1857d+ae03bbfc84,g35bb328faa+a8ce1bb630,g3a166c0a6a+ae03bbfc84,g3e281a1b8c+c5dd892a6c,g414038480c+6b9177ef31,g41af890bb2+9e154f3e8d,g6b1c1869cb+adc49b6f1a,g781aacb6e4+a8ce1bb630,g7af13505b9+3363a39af3,g7f202ee025+406ba613a5,g80478fca09+8fbba356e2,g82479be7b0+0d223595df,g858d7b2824+4e0faf0f7f,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,g9726552aa6+414189b318,ga5288a1d22+32d6120315,gacef1a1666+7f85da65db,gb58c049af0+d64f4d3760,gbcfae0f0a0+a8c62e8bb6,gc28159a63d+ae03bbfc84,gcf0d15dbbd+412a8a6f35,gda6a2b7d83+412a8a6f35,gdaeeff99f8+1711a396fd,ge79ae78c31+ae03bbfc84,gf0baf85859+c1f95f4921,gfa517265be+4e0faf0f7f,gfa999e8aa5+17cd334064,gfb92a5be7c+4e0faf0f7f
LSST Data Management Base Package
Loading...
Searching...
No Matches
LonLat.cc
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
32
33#include "lsst/sphgeom/LonLat.h"
34
35#include <cmath>
36#include <ostream>
37
39
40
41namespace lsst {
42namespace sphgeom {
43
45 double d2 = v(0) * v(0) + v(1) * v(1);
46 double lat = 0.0;
47 if (v(2) != 0.0) {
48 lat = std::atan2(v(2), sqrt(d2));
49 if (std::fabs(lat) > 0.5 * PI) {
50 lat = ::copysign(0.5 * PI, lat);
51 }
52 }
53 return Angle(lat);
54}
55
57 double d2 = v(0) * v(0) + v(1) * v(1);
58 double lon = 0.0;
59 if (d2 != 0.0) {
60 lon = std::atan2(v(1), v(0));
61 if (lon < 0.0) {
62 lon += 2*PI;
63 }
64 }
65 return NormalizedAngle(lon);
66}
67
68
69LonLat::LonLat(NormalizedAngle lon, Angle lat) : _lon(lon), _lat(lat) {
70 if (std::fabs(_lat.asRadians()) > 0.5 * PI) {
71 throw std::invalid_argument("invalid latitude angle");
72 }
73 _enforceInvariants();
74}
75
76LonLat::LonLat(Vector3d const & v) : _lon(longitudeOf(v)), _lat(latitudeOf(v)) {
77 _enforceInvariants();
78}
79
80void LonLat::_enforceInvariants() {
81 // Make sure that if one coordinate is NaN, the other is as well.
82 if (_lon.isNan()) {
83 _lat = Angle::nan();
84 } else if (_lat.isNan()) {
85 _lon = NormalizedAngle::nan();
86 }
87}
88
90 return os << '[' << p.getLon() << ", " << p.getLat() << ']';
91}
92
93}} // namespace lsst::sphgeom
This file contains a class representing spherical coordinates.
This file declares a class for representing vectors in ℝ³.
T atan2(T... args)
Angle represents an angle in radians.
Definition Angle.h:50
static Angle nan()
Definition Angle.h:52
bool isNan() const
isNan returns true if the angle value is NaN.
Definition Angle.h:98
double asRadians() const
asRadians returns the value of this angle in units of radians.
Definition Angle.h:92
LonLat represents a spherical coordinate (longitude/latitude angle) pair.
Definition LonLat.h:55
static Angle latitudeOf(Vector3d const &v)
latitudeOf returns the latitude of the point on the unit sphere corresponding to the direction of v.
Definition LonLat.cc:44
LonLat()
This constructor creates the point with longitude and latitude angle equal to zero.
Definition LonLat.h:77
Angle getLat() const
Definition LonLat.h:97
NormalizedAngle getLon() const
Definition LonLat.h:95
static NormalizedAngle longitudeOf(Vector3d const &v)
longitudeOf returns the longitude of the point on the unit sphere corresponding to the direction of v...
Definition LonLat.cc:56
NormalizedAngle is an angle that lies in the range [0, 2π), with one exception - a NormalizedAngle ca...
bool isNan() const
isNan returns true if the angle value is NaN.
static NormalizedAngle nan()
Vector3d is a vector in ℝ³ with components stored in double precision.
Definition Vector3d.h:51
T fabs(T... args)
std::ostream & operator<<(std::ostream &, Angle const &)
Definition Angle.cc:41
constexpr double PI
Definition constants.h:43