CARLA
Loading...
Searching...
No Matches
GeoProjectionsParams.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8#include <string>
9#include <limits>
10#include <unordered_map>
11#include <boost/optional.hpp>
12
13#include "carla/geom/Math.h"
14#include "carla/geom/Location.h"
15
16namespace carla {
17namespace geom {
18
19 // Must be all lower case to support both cases when parsing
20 static const std::unordered_map<std::string, std::pair<double,double>> custom_ellipsoids = {
21 {"wgs84", {6378137.0, 298.257223563}},
22 {"grs80", {6378137.0, 298.257222101}},
23 {"intl", {6378388.0, 297.0}},
24 {"bessel", {6377397.155, 299.1528128}},
25 {"clrk66", {6378206.4, 294.9786982138}},
26 {"airy", {6377563.396, 299.3249646}},
27 {"wgs72", {6378135.0, 298.26}},
28 {"wgs66", {6378145.0, 298.25}},
29 {"sphere", {6370997.0, std::numeric_limits<double>::infinity()}}
30 };
31
32 class Ellipsoid {
33 public:
34
35 Ellipsoid() = default;
36
37 Ellipsoid(double a, double f_inv):
38 a(a), f_inv(f_inv) {}
39
40 double a = 6378137.0;
41 double f_inv = std::numeric_limits<double>::infinity(); // Default to sphere
42
43 double f() const {
44 return 1.0 / f_inv;
45 }
46 double b() const {
47 return a * (1.0 - f());
48 }
49
50 double e2() const {
51 return f() * (2.0 - f());
52 }
53 double ep2() const{
54 return e2() / (1.0 - e2());
55 }
56
57 void fromb(double b) {
58 f_inv = 1.0 / (1.0 - b/a);
59 }
60 void fromf(double f) {
61 f_inv = 1.0 / f;
62 }
63
64 bool operator==(const Ellipsoid &rhs) const {
65 return (a == rhs.a) && (f_inv == rhs.f_inv);
66 }
67
68 bool operator!=(const Ellipsoid &rhs) const {
69 return (a != rhs.a) || (f_inv != rhs.f_inv);
70 }
71 };
72
74 public:
75
76 OffsetTransform() = default;
77
78 OffsetTransform(double offset_x, double offset_y, double offset_z, double offset_hdg):
79 offset_x(offset_x), offset_y(offset_y), offset_z(offset_z), offset_cos_h(std::cos(-offset_hdg)), offset_sin_h(std::sin(-offset_hdg)) {}
80
81 double offset_x = 0.0;
82 double offset_y = 0.0;
83 double offset_z = 0.0;
84 double offset_cos_h = 1.0;
85 double offset_sin_h = 0.0;
86
87 Location ApplyTransformation(const Location& location) const{
88
89 double tx = location.x + offset_x;
90 double ty = -location.y + offset_y;
91
92 double x_rot = (tx * offset_cos_h) - (ty * offset_sin_h);
93 double y_rot = (tx * offset_sin_h) + (ty * offset_cos_h);
94
95 return Location{static_cast<float>(x_rot), static_cast<float>(y_rot), static_cast<float>(location.z + offset_z)};
96 }
97
98 /// Inverse of @ref ApplyTransformation: recovers the pre-offset location
99 /// so that `ApplyTransformation(ApplyInverseTransformation(l)) == l`.
101
102 double tx = (location.x * offset_cos_h) + (location.y * offset_sin_h);
103 double ty = -(location.x * offset_sin_h) + (location.y * offset_cos_h);
104
105 double x = tx - offset_x;
106 double y = offset_y - ty;
107
108 return Location{static_cast<float>(x), static_cast<float>(y), static_cast<float>(location.z - offset_z)};
109 }
110
111 bool operator==(const OffsetTransform& rhs) const {
112 return (offset_x == rhs.offset_x) && (offset_y == rhs.offset_y) && (offset_z == rhs.offset_z) && (offset_cos_h == rhs.offset_cos_h) && (offset_sin_h == rhs.offset_sin_h);
113 }
114
115 bool operator!=(const OffsetTransform& rhs) const {
116 return !(*this == rhs);
117 }
118 };
119
121 public:
122
124
125 TransverseMercatorParams(double lat_0, double lon_0, double k, double x_0, double y_0, Ellipsoid ellps):
126 lat_0(lat_0), lon_0(lon_0), k(k), x_0(x_0), y_0(y_0), ellps(ellps) {}
127
128 double lat_0 = 0.0;
129 double lon_0 = 0.0;
130 double k = 1.0;
131 double x_0 = 0.0;
132 double y_0 = 0.0;
134
135 bool operator==(const TransverseMercatorParams &rhs) const {
136 return (lat_0 == rhs.lat_0) && (lon_0 == rhs.lon_0) && (k == rhs.k)
137 && (x_0 == rhs.x_0) && (y_0 == rhs.y_0) && (ellps == rhs.ellps);
138 }
139
140 bool operator!=(const TransverseMercatorParams &rhs) const {
141 return (lat_0 != rhs.lat_0) || (lon_0 != rhs.lon_0) || (k != rhs.k)
142 || (x_0 != rhs.x_0) || (y_0 != rhs.y_0) || (ellps != rhs.ellps);
143 }
144 };
145
147 public:
148
150
151 UniversalTransverseMercatorParams(int zone, bool north, Ellipsoid ellps, boost::optional<OffsetTransform> offset = boost::none):
153
154 int zone = 31;
155 bool north = true;
157 boost::optional<OffsetTransform> offset;
158
160 return (zone == rhs.zone) && (north == rhs.north) && (ellps == rhs.ellps) && (offset == rhs.offset);
161 }
162
164 return (zone != rhs.zone) || (north != rhs.north) || (ellps != rhs.ellps) || (offset != rhs.offset);
165 }
166 };
167
168
170 public:
171
172 WebMercatorParams() = default;
173
176
178
179 bool operator==(const WebMercatorParams &rhs) const {
180 return (ellps == rhs.ellps);
181 }
182
183 bool operator!=(const WebMercatorParams &rhs) const {
184 return (ellps != rhs.ellps);
185 }
186 };
187
189 public:
190
192
194 double lat_0, double lat_1, double lat_2, double lon_0, double x_0, double y_0, Ellipsoid ellps):
196
197 double lat_0 = 0.0;
198 double lat_1 = -5.0;
199 double lat_2 = 5.0;
200 double lon_0 = 0.0;
201 double x_0 = 0.0;
202 double y_0 = 0.0;
204
206 return (lat_0 == rhs.lat_0) && (lat_1 == rhs.lat_1) && (lat_2 == rhs.lat_2) && (lon_0 == rhs.lon_0)
207 && (x_0 == rhs.x_0) && (y_0 == rhs.y_0) && (ellps == rhs.ellps);
208 }
209
211 return (lat_0 != rhs.lat_0) || (lat_1 != rhs.lat_1) || (lat_2 != rhs.lat_2) || (lon_0 != rhs.lon_0)
212 || (x_0 != rhs.x_0) || (y_0 != rhs.y_0) || (ellps != rhs.ellps);
213 }
214 };
215
216} // namespace geom
217} // namespace carla
float y
float x
Ellipsoid(double a, double f_inv)
bool operator==(const Ellipsoid &rhs) const
bool operator!=(const Ellipsoid &rhs) const
bool operator==(const LambertConformalConicParams &rhs) const
LambertConformalConicParams(double lat_0, double lat_1, double lat_2, double lon_0, double x_0, double y_0, Ellipsoid ellps)
bool operator!=(const LambertConformalConicParams &rhs) const
bool operator==(const OffsetTransform &rhs) const
Location ApplyTransformation(const Location &location) const
bool operator!=(const OffsetTransform &rhs) const
Location ApplyInverseTransformation(const Location &location) const
Inverse of ApplyTransformation: recovers the pre-offset location so that ApplyTransformation(ApplyInv...
OffsetTransform(double offset_x, double offset_y, double offset_z, double offset_hdg)
bool operator!=(const TransverseMercatorParams &rhs) const
bool operator==(const TransverseMercatorParams &rhs) const
TransverseMercatorParams(double lat_0, double lon_0, double k, double x_0, double y_0, Ellipsoid ellps)
bool operator==(const UniversalTransverseMercatorParams &rhs) const
bool operator!=(const UniversalTransverseMercatorParams &rhs) const
UniversalTransverseMercatorParams(int zone, bool north, Ellipsoid ellps, boost::optional< OffsetTransform > offset=boost::none)
bool operator==(const WebMercatorParams &rhs) const
bool operator!=(const WebMercatorParams &rhs) const
static const std::unordered_map< std::string, std::pair< double, double > > custom_ellipsoids
This file contains definitions of common data structures used in traffic manager.