CARLA
Loading...
Searching...
No Matches
geom/Vector2D.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 Vector2D {
18
19 static constexpr auto Dim = 2;
20
21 // =========================================================================
22 // -- Public data members --------------------------------------------------
23 // =========================================================================
24
25 float x = 0.0f;
26
27 float y = 0.0f;
28
29 // =========================================================================
30 // -- Constructors ---------------------------------------------------------
31 // =========================================================================
32
33 Vector2D() = default;
34
35 Vector2D(float ix, float iy)
36 : x(ix),
37 y(iy) {}
38
39 // =========================================================================
40 // -- Other methods --------------------------------------------------------
41 // =========================================================================
42
43 float SquaredLength() const {
44 return x * x + y * y;
45 }
46
47 float Length() const {
48 return std::sqrt(SquaredLength());
49 }
50
51 /// Return a unit-length copy of this vector. When the vector's length
52 /// is at or below `epsilon`, the input is returned unchanged (no
53 /// runtime assert, no NaN).
55 const float epsilon = 2.0f * std::numeric_limits<float>::epsilon()) const {
56 const float len = Length();
57 if (len <= std::max(epsilon, 0.0f)) {
58 return *this;
59 }
60 const float k = 1.0f / len;
61 return Vector2D(x * k, y * k);
62 }
63
64 // =========================================================================
65 // -- Arithmetic operators -------------------------------------------------
66 // =========================================================================
67
69 x += rhs.x;
70 y += rhs.y;
71 return *this;
72 }
73
74 friend Vector2D operator+(Vector2D lhs, const Vector2D &rhs) {
75 lhs += rhs;
76 return lhs;
77 }
78
80 x -= rhs.x;
81 y -= rhs.y;
82 return *this;
83 }
84
85 friend Vector2D operator-(Vector2D lhs, const Vector2D &rhs) {
86 lhs -= rhs;
87 return lhs;
88 }
89
90 Vector2D &operator*=(float rhs) {
91 x *= rhs;
92 y *= rhs;
93 return *this;
94 }
95
96 friend Vector2D operator*(Vector2D lhs, float rhs) {
97 lhs *= rhs;
98 return lhs;
99 }
100
101 friend Vector2D operator*(float lhs, Vector2D rhs) {
102 rhs *= lhs;
103 return rhs;
104 }
105
106 Vector2D &operator/=(float rhs) {
107 x /= rhs;
108 y /= rhs;
109 return *this;
110 }
111
112 friend Vector2D operator/(Vector2D lhs, float rhs) {
113 lhs /= rhs;
114 return lhs;
115 }
116
117 friend Vector2D operator/(float lhs, Vector2D rhs) {
118 rhs /= lhs;
119 return rhs;
120 }
121
122 // =========================================================================
123 // -- Comparison operators -------------------------------------------------
124 // =========================================================================
125
126 bool operator==(const Vector2D &rhs) const {
127 return (x == rhs.x) && (y == rhs.y);
128 }
129
130 bool operator!=(const Vector2D &rhs) const {
131 return !(*this == rhs);
132 }
133
134 // =========================================================================
135 // -- Conversions to UE4 types ---------------------------------------------
136 // =========================================================================
137
138#ifdef LIBCARLA_INCLUDED_FROM_UE4
139
140 /// Return a Vector2D converted from centimeters to meters.
142 return *this * 1e-2f;
143 }
144
145 /// Return a Vector2D converted from meters to centimeters.
147 return *this * 1e2f;
148 }
149
150 FVector2D ToFVector2D() const {
151 return FVector2D{x, y};
152 }
153
154#endif // LIBCARLA_INCLUDED_FROM_UE4
155
156 MSGPACK_DEFINE_ARRAY(x, y)
157 };
158
159} // namespace geom
160} // namespace carla
This file contains definitions of common data structures used in traffic manager.
Vector2D ToMeters() const
Return a Vector2D converted from centimeters to meters.
friend Vector2D operator/(float lhs, Vector2D rhs)
friend Vector2D operator-(Vector2D lhs, const Vector2D &rhs)
bool operator==(const Vector2D &rhs) const
Vector2D & operator*=(float rhs)
static constexpr auto Dim
friend Vector2D operator+(Vector2D lhs, const Vector2D &rhs)
Vector2D(float ix, float iy)
friend Vector2D operator/(Vector2D lhs, float rhs)
Vector2D & operator+=(const Vector2D &rhs)
FVector2D ToFVector2D() const
float SquaredLength() const
bool operator!=(const Vector2D &rhs) const
friend Vector2D operator*(Vector2D lhs, float rhs)
Vector2D ToCentimeters() const
Return a Vector2D converted from meters to centimeters.
Vector2D & operator/=(float rhs)
Vector2D MakeUnitVector(const float epsilon=2.0f *std::numeric_limits< float >::epsilon()) const
Return a unit-length copy of this vector.
friend Vector2D operator*(float lhs, Vector2D rhs)
Vector2D & operator-=(const Vector2D &rhs)