Debian Codegraph @

src/atlas/util/Point.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 /// @file Point.h
14 ///
15 /// This file contains classes and functions working on points.
16 /// The Point classes are inherited from eckit::geometry::Point2
17 /// or eckit::geometry::Point3.
18
19 #include <array>
20 #include <initializer_list>
21
22 #include "eckit/geometry/Point2.h"
23 #include "eckit/geometry/Point3.h"
24
25 #include "atlas/util/Earth.h"
26
27 namespace atlas {
28
29 using Point2 = eckit::geometry::Point2;
30 using Point3 = eckit::geometry::Point3;
31
32 inline bool operator==(const Point2& p1, const Point2& p2) {
33 return eckit::geometry::points_equal(p1, p2);
34 }
35 inline bool operator!=(const Point2& p1, const Point2& p2) {
36 return !eckit::geometry::points_equal(p1, p2);
37 }
38
39 inline bool operator==(const Point3& p1, const Point3& p2) {
40 return eckit::geometry::points_equal(p1, p2);
41 }
42 inline bool operator!=(const Point3& p1, const Point3& p2) {
43 return !eckit::geometry::points_equal(p1, p2);
44 }
45
46
47 /// @brief Point in arbitrary XY-coordinate system
48 class PointXY : public eckit::geometry::Point2 {
49 using array_t = std::array<double, 2>;
50
51 public:
52 using Point2::Point2;
53
54 PointXY(): Point2() {}
55
56 // Allow initialization through PointXY xy = {0,0};
57 PointXY(std::initializer_list<double> list): PointXY(list.begin()) {}
58 PointXY(const array_t& arr): PointXY(arr.data()) {}
59
60 double x() const { return x_[0]; }
61 double y() const { return x_[1]; }
62 double& x() { return x_[0]; }
63 double& y() { return x_[1]; }
64
65 using Point2::assign;
66
67 void assign(double x, double y) {
68 x_[0] = x;
69 x_[1] = y;
70 }
71 };
72
73 /// @brief Point in arbitrary XYZ-coordinate system
74 class PointXYZ : public eckit::geometry::Point3 {
75 using array_t = std::array<double, 3>;
76
77 PointXYZ(double, double) { /* No automatic converion allowed, otherwise
78 inherited from Point3 */
79 }
80
81 public:
82 using Point3::Point3;
83 using Point3::x;
84
85 PointXYZ(): Point3() {}
86
87 // Allow initialization through PointXYZ xyz = {0,0,0};
88 PointXYZ(std::initializer_list<double> list): PointXYZ(list.begin()) {}
89
90 PointXYZ(const array_t& arr): PointXYZ(arr.data()) {}
91
92 double x() const { return x_[0]; }
93 double y() const { return x_[1]; }
94 double z() const { return x_[2]; }
95 double& x() { return x_[0]; }
96 double& y() { return x_[1]; }
97 double& z() { return x_[2]; }
98
99 using Point3::assign;
100
101 void assign(double x, double y, double z) {
102 x_[0] = x;
103 x_[1] = y;
104 x_[2] = z;
105 }
106
107 PointXYZ& operator*=(double m) {
108 x_[0] *= m;
109 x_[1] *= m;
110 x_[2] *= m;
111 return *this;
112 }
113
114 PointXYZ& operator/=(double m) {
115 x_[0] /= m;
116 x_[1] /= m;
117 x_[2] /= m;
118 return *this;
119 }
120 };
121
122 /// @brief Point in longitude-latitude coordinate system.
123 /// This class does *not* normalise the longitude by default,
124 /// but contains a normalise function.
125 class PointLonLat : public eckit::geometry::Point2 {
126 using array_t = std::array<double, 2>;
127
128 public:
129 using Point2::Point2;
130
131 PointLonLat(): Point2() {}
132
133 // Allow initialization through PointXY lonlat = {0,0};
134 PointLonLat(std::initializer_list<double> list): PointLonLat(list.begin()) {}
135
136 PointLonLat(const array_t& arr): PointLonLat(arr.data()) {}
137
138 double lon() const { return x_[0]; }
139 double lat() const { return x_[1]; }
140 double& lon() { return x_[0]; }
141 double& lat() { return x_[1]; }
142
143 using Point2::assign;
144
145 void assign(double lon, double lat) {
146 x_[0] = lon;
147 x_[1] = lat;
148 }
149
150 PointLonLat& operator*=(double a) {
151 x_[0] *= a;
152 x_[1] *= a;
153 return *this;
154 }
155
156 void normalise();
157
158 void normalise(double west);
159
160 void normalise(double west, double east);
161 };
162
163 } // namespace atlas
164
165 #ifndef DOXYGEN_SHOULD_SKIP_THIS
166 namespace eckit {
167 // When including "eckit/types/Types.h" we get an overload for
168 // std::ostream& operator<<( std::ostream&, std::vector<T> )
169 // The default VectorPrintSelector is however the [VectorPrintContracted],
170 // which does not compile when [T={PointXY,PointLonLat,PointXYZ}]
171 // Following changes the default for these types to [VectorPrintSimple]
172 class VectorPrintSimple;
173 template <typename T>
174 struct VectorPrintSelector;
175 template <>
176 struct VectorPrintSelector<atlas::Point2> {
177 typedef VectorPrintSimple selector;
178 };
179 template <>
180 struct VectorPrintSelector<atlas::PointXY> {
181 typedef VectorPrintSimple selector;
182 };
183 template <>
184 struct VectorPrintSelector<atlas::PointLonLat> {
185 typedef VectorPrintSimple selector;
186 };
187 template <>
188 struct VectorPrintSelector<atlas::Point3> {
189 typedef VectorPrintSimple selector;
190 };
191 template <>
192 struct VectorPrintSelector<atlas::PointXYZ> {
193 typedef VectorPrintSimple selector;
194 };
195 } // namespace eckit
196 #endif