CARLA
Rotation.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 #include "carla/geom/Math.h"
11 #include "carla/geom/Vector3D.h"
12 
13 #ifdef LIBCARLA_INCLUDED_FROM_UE4
15 #include "Math/Rotator.h"
17 #endif // LIBCARLA_INCLUDED_FROM_UE4
18 
19 namespace carla {
20 namespace geom {
21 
22  class Rotation {
23  public:
24 
25  // =========================================================================
26  // -- Public data members --------------------------------------------------
27  // =========================================================================
28 
29  float pitch = 0.0f;
30 
31  float yaw = 0.0f;
32 
33  float roll = 0.0f;
34 
35  MSGPACK_DEFINE_ARRAY(pitch, yaw, roll);
36 
37  // =========================================================================
38  // -- Constructors ---------------------------------------------------------
39  // =========================================================================
40 
41  Rotation() = default;
42 
43  Rotation(float p, float y, float r)
44  : pitch(p),
45  yaw(y),
46  roll(r) {}
47 
48  // =========================================================================
49  // -- Other methods --------------------------------------------------------
50  // =========================================================================
51 
53  return Math::GetForwardVector(*this);
54  }
55 
57  return Math::GetRightVector(*this);
58  }
59 
61  return Math::GetUpVector(*this);
62  }
63 
64  void RotateVector(Vector3D &in_point) const {
65  // Rotates Rz(yaw) * Ry(pitch) * Rx(roll) = first x, then y, then z.
66  const float cy = std::cos(Math::ToRadians(yaw));
67  const float sy = std::sin(Math::ToRadians(yaw));
68  const float cr = std::cos(Math::ToRadians(roll));
69  const float sr = std::sin(Math::ToRadians(roll));
70  const float cp = std::cos(Math::ToRadians(pitch));
71  const float sp = std::sin(Math::ToRadians(pitch));
72 
73  Vector3D out_point;
74  out_point.x =
75  in_point.x * (cp * cy) +
76  in_point.y * (cy * sp * sr - sy * cr) +
77  in_point.z * (-cy * sp * cr - sy * sr);
78 
79  out_point.y =
80  in_point.x * (cp * sy) +
81  in_point.y * (sy * sp * sr + cy * cr) +
82  in_point.z * (-sy * sp * cr + cy * sr);
83 
84  out_point.z =
85  in_point.x * (sp) +
86  in_point.y * (-cp * sr) +
87  in_point.z * (cp * cr);
88 
89  in_point = out_point;
90  }
91 
92  Vector3D RotateVector(const Vector3D& in_point) const {
93  Vector3D out_point = in_point;
94  RotateVector(out_point);
95  return out_point;
96  }
97 
98  void InverseRotateVector(Vector3D &in_point) const {
99  // Applies the transposed of the matrix used in RotateVector function,
100  // which is the rotation inverse.
101  const float cy = std::cos(Math::ToRadians(yaw));
102  const float sy = std::sin(Math::ToRadians(yaw));
103  const float cr = std::cos(Math::ToRadians(roll));
104  const float sr = std::sin(Math::ToRadians(roll));
105  const float cp = std::cos(Math::ToRadians(pitch));
106  const float sp = std::sin(Math::ToRadians(pitch));
107 
108  Vector3D out_point;
109  out_point.x =
110  in_point.x * (cp * cy) +
111  in_point.y * (cp * sy) +
112  in_point.z * (sp);
113 
114  out_point.y =
115  in_point.x * (cy * sp * sr - sy * cr) +
116  in_point.y * (sy * sp * sr + cy * cr) +
117  in_point.z * (-cp * sr);
118 
119  out_point.z =
120  in_point.x * (-cy * sp * cr - sy * sr) +
121  in_point.y * (-sy * sp * cr + cy * sr) +
122  in_point.z * (cp * cr);
123 
124  in_point = out_point;
125  }
126 
127  // =========================================================================
128  // -- Comparison operators -------------------------------------------------
129  // =========================================================================
130 
131  bool operator==(const Rotation &rhs) const {
132  return (pitch == rhs.pitch) && (yaw == rhs.yaw) && (roll == rhs.roll);
133  }
134 
135  bool operator!=(const Rotation &rhs) const {
136  return !(*this == rhs);
137  }
138 
139  // =========================================================================
140  // -- Conversions to UE4 types ---------------------------------------------
141  // =========================================================================
142 
143 #ifdef LIBCARLA_INCLUDED_FROM_UE4
144 
145  Rotation(const FRotator &rotator)
146  : Rotation(rotator.Pitch, rotator.Yaw, rotator.Roll) {}
147 
148  operator FRotator() const {
149  return FRotator{pitch, yaw, roll};
150  }
151 
152 #endif // LIBCARLA_INCLUDED_FROM_UE4
153  };
154 
155 } // namespace geom
156 } // namespace carla
Vector3D GetRightVector() const
Definition: Rotation.h:56
void InverseRotateVector(Vector3D &in_point) const
Definition: Rotation.h:98
static Vector3D GetUpVector(const Rotation &rotation)
Compute the unit vector pointing towards the Y-axis of rotation.
Definition: Math.cpp:138
Vector3D RotateVector(const Vector3D &in_point) const
Definition: Rotation.h:92
static Vector3D GetForwardVector(const Rotation &rotation)
Compute the unit vector pointing towards the X-axis of rotation.
Definition: Math.cpp:117
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
static Vector3D GetRightVector(const Rotation &rotation)
Compute the unit vector pointing towards the Y-axis of rotation.
Definition: Math.cpp:125
bool operator==(const Rotation &rhs) const
Definition: Rotation.h:131
bool operator!=(const Rotation &rhs) const
Definition: Rotation.h:135
static constexpr T ToRadians(T deg)
Definition: Math.h:43
Vector3D GetForwardVector() const
Definition: Rotation.h:52
Rotation(float p, float y, float r)
Definition: Rotation.h:43
MSGPACK_DEFINE_ARRAY(pitch, yaw, roll)
Vector3D GetUpVector() const
Definition: Rotation.h:60
void RotateVector(Vector3D &in_point) const
Definition: Rotation.h:64