CARLA
Loading...
Searching...
No Matches
geom/Vector3D.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
9#include "carla/MsgPack.h"
10
11#include <cmath>
12#include <limits>
13
14namespace carla {
15namespace geom {
16
17 struct Vector3D {
18
19 static constexpr auto Dim = 3;
20
21 // =========================================================================
22 // -- Public data members --------------------------------------------------
23 // =========================================================================
24
25 float x = 0.0f;
26
27 float y = 0.0f;
28
29 float z = 0.0f;
30
31 // =========================================================================
32 // -- Constructors ---------------------------------------------------------
33 // =========================================================================
34
35 Vector3D() = default;
36
37 constexpr Vector3D(float ix, float iy, float iz)
38 : x(ix),
39 y(iy),
40 z(iz) {
41 }
42
43 // =========================================================================
44 // -- Other methods --------------------------------------------------------
45 // =========================================================================
46
47 constexpr float SquaredLength() const {
48 return x * x + y * y + z * z;
49 }
50
51 inline float Length() const {
52 return std::sqrt(SquaredLength());
53 }
54
55 constexpr float SquaredLength2D() const {
56 return x * x + y * y;
57 }
58
59 inline float Length2D() const {
60 return std::sqrt(SquaredLength2D());
61 }
62
63 inline Vector3D Abs() const {
64 return Vector3D(abs(x), abs(y), abs(z));
65 }
66
67 /// Return a unit-length copy of this vector. When the vector's length
68 /// is at or below `epsilon`, the input is returned unchanged (no
69 /// runtime exception, no NaN). Callers that need the strict
70 /// `1.0 / length` normalization should pass `epsilon = 0`.
72 const float epsilon = 2.0f * std::numeric_limits<float>::epsilon()) const {
73 const float length = Length();
74 if (length <= std::max(epsilon, 0.0f)) {
75 return *this;
76 }
77 const float k = 1.0f / length;
78 return Vector3D(x * k, y * k, z * k);
79 }
80
81 // =========================================================================
82 // -- Arithmetic operators -------------------------------------------------
83 // =========================================================================
84
85 constexpr Vector3D &operator+=(const Vector3D &rhs) {
86 x += rhs.x;
87 y += rhs.y;
88 z += rhs.z;
89 return *this;
90 }
91
92 constexpr friend Vector3D operator+(Vector3D lhs, const Vector3D &rhs) {
93 lhs += rhs;
94 return lhs;
95 }
96
97 constexpr Vector3D &operator-=(const Vector3D &rhs) {
98 x -= rhs.x;
99 y -= rhs.y;
100 z -= rhs.z;
101 return *this;
102 }
103
104 constexpr friend Vector3D operator-(Vector3D lhs, const Vector3D &rhs) {
105 lhs -= rhs;
106 return lhs;
107 }
108
109 constexpr Vector3D& operator-=(const float f) {
110 x -= f;
111 y -= f;
112 z -= f;
113 return *this;
114 }
115
116 constexpr Vector3D &operator*=(float rhs) {
117 x *= rhs;
118 y *= rhs;
119 z *= rhs;
120 return *this;
121 }
122
123 constexpr friend Vector3D operator*(Vector3D lhs, float rhs) {
124 lhs *= rhs;
125 return lhs;
126 }
127
128 constexpr friend Vector3D operator*(float lhs, Vector3D rhs) {
129 rhs *= lhs;
130 return rhs;
131 }
132
133 constexpr Vector3D &operator/=(float rhs) {
134 x /= rhs;
135 y /= rhs;
136 z /= rhs;
137 return *this;
138 }
139
140 constexpr friend Vector3D operator/(Vector3D lhs, float rhs) {
141 lhs /= rhs;
142 return lhs;
143 }
144
145 constexpr friend Vector3D operator/(float lhs, Vector3D rhs) {
146 rhs /= lhs;
147 return rhs;
148 }
149
150 // =========================================================================
151 // -- Comparison operators -------------------------------------------------
152 // =========================================================================
153
154 constexpr bool operator==(const Vector3D &rhs) const {
155 return (x == rhs.x) && (y == rhs.y) && (z == rhs.z);
156 }
157
158 constexpr bool operator!=(const Vector3D &rhs) const {
159 return !(*this == rhs);
160 }
161
162 // =========================================================================
163 // -- Conversions to UE4 types ---------------------------------------------
164 // =========================================================================
165
166#ifdef LIBCARLA_INCLUDED_FROM_UE4
167
168 /// These 2 methods are explicitly deleted to avoid creating them by other users,
169 /// unlike locations, some vectors have units and some don't, by removing
170 /// these methods we found several places were the conversion from cm to m was missing
171 Vector3D(const FVector &v) = delete;
172 Vector3D& operator=(const FVector &rhs) = delete;
173
174 /// Return a Vector3D converted from centimeters to meters.
175 inline Vector3D ToMeters() const {
176 return *this * 1e-2f;
177 }
178
179 /// Return a Vector3D converted from meters to centimeters.
180 inline Vector3D ToCentimeters() const {
181 return *this * 1e2f;
182 }
183
184 inline FVector ToFVector() const {
185 return FVector{x, y, z};
186 }
187
188#endif // LIBCARLA_INCLUDED_FROM_UE4
189
190 // =========================================================================
191 /// @todo The following is copy-pasted from MSGPACK_DEFINE_ARRAY.
192 /// This is a workaround for an issue in msgpack library. The
193 /// MSGPACK_DEFINE_ARRAY macro is shadowing our `z` variable.
194 /// https://github.com/msgpack/msgpack-c/issues/709
195 // =========================================================================
196 template <typename Packer>
197 void msgpack_pack(Packer& pk) const
198 {
199 clmdep_msgpack::type::make_define_array(x, y, z).msgpack_pack(pk);
200 }
201 void msgpack_unpack(clmdep_msgpack::object const& o)
202 {
203 clmdep_msgpack::type::make_define_array(x, y, z).msgpack_unpack(o);
204 }
205 template <typename MSGPACK_OBJECT>
206 void msgpack_object(MSGPACK_OBJECT* o, clmdep_msgpack::zone& sneaky_variable_that_shadows_z) const
207 {
208 clmdep_msgpack::type::make_define_array(x, y, z).msgpack_object(o, sneaky_variable_that_shadows_z);
209 }
210 // =========================================================================
211 };
212
213} // namespace geom
214} // namespace carla
This file contains definitions of common data structures used in traffic manager.
constexpr Vector3D & operator-=(const float f)
constexpr friend Vector3D operator/(Vector3D lhs, float rhs)
void msgpack_object(MSGPACK_OBJECT *o, clmdep_msgpack::zone &sneaky_variable_that_shadows_z) const
FVector ToFVector() const
constexpr Vector3D & operator+=(const Vector3D &rhs)
Vector3D(const FVector &v)=delete
These 2 methods are explicitly deleted to avoid creating them by other users, unlike locations,...
constexpr friend Vector3D operator*(float lhs, Vector3D rhs)
constexpr Vector3D & operator-=(const Vector3D &rhs)
static constexpr auto Dim
Vector3D ToCentimeters() const
Return a Vector3D converted from meters to centimeters.
void msgpack_pack(Packer &pk) const
constexpr friend Vector3D operator*(Vector3D lhs, float rhs)
constexpr friend Vector3D operator-(Vector3D lhs, const Vector3D &rhs)
constexpr float SquaredLength2D() const
Vector3D Abs() const
constexpr friend Vector3D operator+(Vector3D lhs, const Vector3D &rhs)
Vector3D ToMeters() const
Return a Vector3D converted from centimeters to meters.
constexpr float SquaredLength() const
Vector3D MakeUnitVector(const float epsilon=2.0f *std::numeric_limits< float >::epsilon()) const
Return a unit-length copy of this vector.
void msgpack_unpack(clmdep_msgpack::object const &o)
float Length2D() const
Vector3D & operator=(const FVector &rhs)=delete
constexpr Vector3D & operator/=(float rhs)
constexpr Vector3D(float ix, float iy, float iz)
constexpr friend Vector3D operator/(float lhs, Vector3D rhs)
constexpr bool operator!=(const Vector3D &rhs) const
constexpr bool operator==(const Vector3D &rhs) const
constexpr Vector3D & operator*=(float rhs)