CARLA
geom/Vector2D.h
Go to the documentation of this file.
1 // Copyright (c) 2017 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 
14 namespace carla {
15 namespace geom {
16 
17  class Vector2D {
18  public:
19 
20  // =========================================================================
21  // -- Public data members --------------------------------------------------
22  // =========================================================================
23 
24  float x = 0.0f;
25 
26  float y = 0.0f;
27 
28  // =========================================================================
29  // -- Constructors ---------------------------------------------------------
30  // =========================================================================
31 
32  Vector2D() = default;
33 
34  Vector2D(float ix, float iy)
35  : x(ix),
36  y(iy) {}
37 
38  // =========================================================================
39  // -- Other methods --------------------------------------------------------
40  // =========================================================================
41 
42  float SquaredLength() const {
43  return x * x + y * y;
44  }
45 
46  float Length() const {
47  return std::sqrt(SquaredLength());
48  }
49 
51  const float len = Length();
52  DEVELOPMENT_ASSERT(len > 2.0f * std::numeric_limits<float>::epsilon());
53  const float k = 1.0f / len;
54  return Vector2D(x * k, y * k);
55  }
56 
57  // =========================================================================
58  // -- Arithmetic operators -------------------------------------------------
59  // =========================================================================
60 
61  Vector2D &operator+=(const Vector2D &rhs) {
62  x += rhs.x;
63  y += rhs.y;
64  return *this;
65  }
66 
67  friend Vector2D operator+(Vector2D lhs, const Vector2D &rhs) {
68  lhs += rhs;
69  return lhs;
70  }
71 
72  Vector2D &operator-=(const Vector2D &rhs) {
73  x -= rhs.x;
74  y -= rhs.y;
75  return *this;
76  }
77 
78  friend Vector2D operator-(Vector2D lhs, const Vector2D &rhs) {
79  lhs -= rhs;
80  return lhs;
81  }
82 
83  Vector2D &operator*=(float rhs) {
84  x *= rhs;
85  y *= rhs;
86  return *this;
87  }
88 
89  friend Vector2D operator*(Vector2D lhs, float rhs) {
90  lhs *= rhs;
91  return lhs;
92  }
93 
94  friend Vector2D operator*(float lhs, Vector2D rhs) {
95  rhs *= lhs;
96  return rhs;
97  }
98 
99  Vector2D &operator/=(float rhs) {
100  x /= rhs;
101  y /= rhs;
102  return *this;
103  }
104 
105  friend Vector2D operator/(Vector2D lhs, float rhs) {
106  lhs /= rhs;
107  return lhs;
108  }
109 
110  friend Vector2D operator/(float lhs, Vector2D rhs) {
111  rhs /= lhs;
112  return rhs;
113  }
114 
115  // =========================================================================
116  // -- Comparison operators -------------------------------------------------
117  // =========================================================================
118 
119  bool operator==(const Vector2D &rhs) const {
120  return (x == rhs.x) && (y == rhs.y);
121  }
122 
123  bool operator!=(const Vector2D &rhs) const {
124  return !(*this == rhs);
125  }
126 
127  // =========================================================================
128  // -- Conversions to UE4 types ---------------------------------------------
129  // =========================================================================
130 
131 #ifdef LIBCARLA_INCLUDED_FROM_UE4
132 
133  /// Return a Vector2D converted from centimeters to meters.
134  Vector2D ToMeters() const {
135  return *this * 1e-2f;
136  }
137 
138  /// Return a Vector2D converted from meters to centimeters.
139  Vector2D ToCentimeters() const {
140  return *this * 1e2f;
141  }
142 
143  FVector2D ToFVector2D() const {
144  return FVector2D{x, y};
145  }
146 
147 #endif // LIBCARLA_INCLUDED_FROM_UE4
148 
149  MSGPACK_DEFINE_ARRAY(x, y)
150  };
151 
152 } // namespace geom
153 } // namespace carla
friend Vector2D operator+(Vector2D lhs, const Vector2D &rhs)
Definition: geom/Vector2D.h:67
friend Vector2D operator*(Vector2D lhs, float rhs)
Definition: geom/Vector2D.h:89
Vector2D & operator/=(float rhs)
Definition: geom/Vector2D.h:99
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
friend Vector2D operator-(Vector2D lhs, const Vector2D &rhs)
Definition: geom/Vector2D.h:78
friend Vector2D operator/(float lhs, Vector2D rhs)
bool operator!=(const Vector2D &rhs) const
friend Vector2D operator*(float lhs, Vector2D rhs)
Definition: geom/Vector2D.h:94
#define DEVELOPMENT_ASSERT(pred)
Definition: Debug.h:82
Vector2D & operator+=(const Vector2D &rhs)
Definition: geom/Vector2D.h:61
Vector2D(float ix, float iy)
Definition: geom/Vector2D.h:34
float Length() const
Definition: geom/Vector2D.h:46
float SquaredLength() const
Definition: geom/Vector2D.h:42
Vector2D MakeUnitVector() const
Definition: geom/Vector2D.h:50
friend Vector2D operator/(Vector2D lhs, float rhs)
Vector2D & operator*=(float rhs)
Definition: geom/Vector2D.h:83
Vector2D & operator-=(const Vector2D &rhs)
Definition: geom/Vector2D.h:72
bool operator==(const Vector2D &rhs) const