CARLA
Vector3DInt.h
Go to the documentation of this file.
1 // Copyright (c) 2021 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 Vector3DInt {
18  public:
19 
20  // =========================================================================
21  // -- Public data members --------------------------------------------------
22  // =========================================================================
23 
24  int32_t x = 0;
25 
26  int32_t y = 0;
27 
28  int32_t z = 0;
29 
30  // =========================================================================
31  // -- Constructors ---------------------------------------------------------
32  // =========================================================================
33 
34  Vector3DInt() = default;
35 
36  Vector3DInt(int32_t ix, int32_t iy, int32_t iz)
37  : x(ix),
38  y(iy),
39  z(iz) {}
40 
41  // =========================================================================
42  // -- Other methods --------------------------------------------------------
43  // =========================================================================
44 
45  int64_t SquaredLength() const {
46  return x * x + y * y + z * z;
47  }
48 
49  double Length() const {
50  return std::sqrt(SquaredLength());
51  }
52 
53  // =========================================================================
54  // -- Arithmetic operators -------------------------------------------------
55  // =========================================================================
56 
58  x += rhs.x;
59  y += rhs.y;
60  z += rhs.z;
61  return *this;
62  }
63 
64  friend Vector3DInt operator+(Vector3DInt lhs, const Vector3DInt &rhs) {
65  lhs += rhs;
66  return lhs;
67  }
68 
70  x -= rhs.x;
71  y -= rhs.y;
72  z -= rhs.z;
73  return *this;
74  }
75 
76  friend Vector3DInt operator-(Vector3DInt lhs, const Vector3DInt &rhs) {
77  lhs -= rhs;
78  return lhs;
79  }
80 
81  Vector3DInt &operator*=(int32_t rhs) {
82  x *= rhs;
83  y *= rhs;
84  z *= rhs;
85  return *this;
86  }
87 
88  friend Vector3DInt operator*(Vector3DInt lhs, int32_t rhs) {
89  lhs *= rhs;
90  return lhs;
91  }
92 
93  friend Vector3DInt operator*(int32_t lhs, Vector3DInt rhs) {
94  rhs *= lhs;
95  return rhs;
96  }
97 
98  Vector3DInt &operator/=(int32_t rhs) {
99  x /= rhs;
100  y /= rhs;
101  z /= rhs;
102  return *this;
103  }
104 
105  friend Vector3DInt operator/(Vector3DInt lhs, int32_t rhs) {
106  lhs /= rhs;
107  return lhs;
108  }
109 
110  friend Vector3DInt operator/(int32_t lhs, Vector3DInt rhs) {
111  rhs /= lhs;
112  return rhs;
113  }
114 
115  // =========================================================================
116  // -- Comparison operators -------------------------------------------------
117  // =========================================================================
118 
119  bool operator==(const Vector3DInt &rhs) const {
120  return (x == rhs.x) && (y == rhs.y) && (z == rhs.z);
121  }
122 
123  bool operator!=(const Vector3DInt &rhs) const {
124  return !(*this == rhs);
125  }
126 
127  // =========================================================================
128  // -- Conversions to UE4 types ---------------------------------------------
129  // =========================================================================
130 
131 #ifdef LIBCARLA_INCLUDED_FROM_UE4
132 
133  Vector3DInt(const FIntVector &v) = delete;
134  Vector3DInt& operator=(const FIntVector &rhs) = delete;
135 
136  /// Return a Vector3DInt converted from centimeters to meters.
137  Vector3DInt ToMeters() const {
138  return *this / 100;
139  }
140 
141  /// Return a Vector3DInt converted from meters to centimeters.
142  Vector3DInt ToCentimeters() const {
143  return *this * 100;
144  }
145 
146  FIntVector ToFIntVector() const {
147  return FIntVector{x, y, z};
148  }
149 
150 #endif // LIBCARLA_INCLUDED_FROM_UE4
151 
152  // =========================================================================
153  /// @todo The following is copy-pasted from MSGPACK_DEFINE_ARRAY.
154  /// This is a workaround for an issue in msgpack library. The
155  /// MSGPACK_DEFINE_ARRAY macro is shadowing our `z` variable.
156  /// https://github.com/msgpack/msgpack-c/issues/709
157  // =========================================================================
158  template <typename Packer>
159  void msgpack_pack(Packer& pk) const
160  {
161  clmdep_msgpack::type::make_define_array(x, y, z).msgpack_pack(pk);
162  }
163  void msgpack_unpack(clmdep_msgpack::object const& o)
164  {
165  clmdep_msgpack::type::make_define_array(x, y, z).msgpack_unpack(o);
166  }
167  template <typename MSGPACK_OBJECT>
168  void msgpack_object(MSGPACK_OBJECT* o, clmdep_msgpack::zone& sneaky_variable_that_shadows_z) const
169  {
170  clmdep_msgpack::type::make_define_array(x, y, z).msgpack_object(o, sneaky_variable_that_shadows_z);
171  }
172  // =========================================================================
173  };
174 
175 } // namespace geom
176 } // namespace carla
friend Vector3DInt operator+(Vector3DInt lhs, const Vector3DInt &rhs)
Definition: Vector3DInt.h:64
Vector3DInt & operator-=(const Vector3DInt &rhs)
Definition: Vector3DInt.h:69
Vector3DInt & operator+=(const Vector3DInt &rhs)
Definition: Vector3DInt.h:57
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
void msgpack_object(MSGPACK_OBJECT *o, clmdep_msgpack::zone &sneaky_variable_that_shadows_z) const
Definition: Vector3DInt.h:168
friend Vector3DInt operator/(int32_t lhs, Vector3DInt rhs)
Definition: Vector3DInt.h:110
void msgpack_unpack(clmdep_msgpack::object const &o)
Definition: Vector3DInt.h:163
double Length() const
Definition: Vector3DInt.h:49
friend Vector3DInt operator/(Vector3DInt lhs, int32_t rhs)
Definition: Vector3DInt.h:105
Vector3DInt & operator*=(int32_t rhs)
Definition: Vector3DInt.h:81
friend Vector3DInt operator-(Vector3DInt lhs, const Vector3DInt &rhs)
Definition: Vector3DInt.h:76
Vector3DInt & operator/=(int32_t rhs)
Definition: Vector3DInt.h:98
bool operator!=(const Vector3DInt &rhs) const
Definition: Vector3DInt.h:123
friend Vector3DInt operator*(int32_t lhs, Vector3DInt rhs)
Definition: Vector3DInt.h:93
int64_t SquaredLength() const
Definition: Vector3DInt.h:45
void msgpack_pack(Packer &pk) const
Definition: Vector3DInt.h:159
Vector3DInt(int32_t ix, int32_t iy, int32_t iz)
Definition: Vector3DInt.h:36
friend Vector3DInt operator*(Vector3DInt lhs, int32_t rhs)
Definition: Vector3DInt.h:88
bool operator==(const Vector3DInt &rhs) const
Definition: Vector3DInt.h:119