CARLA
geom/Transform.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/Location.h"
11 #include "carla/geom/Math.h"
12 #include "carla/geom/Rotation.h"
13 
14 #ifdef LIBCARLA_INCLUDED_FROM_UE4
16 #include "Math/Transform.h"
18 #endif // LIBCARLA_INCLUDED_FROM_UE4
19 
20 namespace carla {
21 namespace geom {
22 
23  class Transform {
24  public:
25 
26  // =========================================================================
27  // -- Public data members --------------------------------------------------
28  // =========================================================================
29 
31 
33 
34  MSGPACK_DEFINE_ARRAY(location, rotation);
35 
36  // =========================================================================
37  // -- Constructors ---------------------------------------------------------
38  // =========================================================================
39 
40  Transform() = default;
41 
42  Transform(const Location &in_location)
43  : location(in_location),
44  rotation() {}
45 
46  Transform(const Location &in_location, const Rotation &in_rotation)
47  : location(in_location),
48  rotation(in_rotation) {}
49 
50  // =========================================================================
51  // -- Other methods --------------------------------------------------------
52  // =========================================================================
53 
55  return rotation.GetForwardVector();
56  }
57 
59  return rotation.GetRightVector();
60  }
61 
63  return rotation.GetUpVector();
64  }
65 
66  /// Applies this transformation to @a in_point (first translation then rotation).
67  void TransformPoint(Vector3D &in_point) const {
68  auto out_point = in_point;
69  rotation.RotateVector(out_point); // First rotate
70  out_point += location; // Then translate
71  in_point = out_point;
72  }
73 
74  /// Applies this transformation to @a in_vector (rotation only).
75  void TransformVector(Vector3D &in_vector) const {
76  auto out_vector = in_vector;
77  rotation.RotateVector(out_vector); // First rotate
78  in_vector = out_vector;
79  }
80 
81  /// Applies the inverse of this transformation to @a in_point
82  void InverseTransformPoint(Vector3D &in_point) const {
83  auto out_point = in_point;
84  out_point -= location; // First translate inverse
85  rotation.InverseRotateVector(out_point); // Then rotate inverse
86  in_point = out_point;
87  }
88 
89  /// Computes the 4-matrix form of the transformation
90  std::array<float, 16> GetMatrix() const {
91  const float yaw = rotation.yaw;
92  const float cy = std::cos(Math::ToRadians(yaw));
93  const float sy = std::sin(Math::ToRadians(yaw));
94 
95  const float roll = rotation.roll;
96  const float cr = std::cos(Math::ToRadians(roll));
97  const float sr = std::sin(Math::ToRadians(roll));
98 
99  const float pitch = rotation.pitch;
100  const float cp = std::cos(Math::ToRadians(pitch));
101  const float sp = std::sin(Math::ToRadians(pitch));
102 
103  std::array<float, 16> transform = {
104  cp * cy, cy * sp * sr - sy * cr, -cy * sp * cr - sy * sr, location.x,
105  cp * sy, sy * sp * sr + cy * cr, -sy * sp * cr + cy * sr, location.y,
106  sp, -cp * sr, cp * cr, location.z,
107  0.0, 0.0, 0.0, 1.0};
108 
109  return transform;
110  }
111 
112  /// Computes the 4-matrix form of the inverse transformation
113  std::array<float, 16> GetInverseMatrix() const {
114  const float yaw = rotation.yaw;
115  const float cy = std::cos(Math::ToRadians(yaw));
116  const float sy = std::sin(Math::ToRadians(yaw));
117 
118  const float roll = rotation.roll;
119  const float cr = std::cos(Math::ToRadians(roll));
120  const float sr = std::sin(Math::ToRadians(roll));
121 
122  const float pitch = rotation.pitch;
123  const float cp = std::cos(Math::ToRadians(pitch));
124  const float sp = std::sin(Math::ToRadians(pitch));
125 
126  Vector3D a = {0.0f, 0.0f, 0.0f};
128 
129  std::array<float, 16> transform = {
130  cp * cy, cp * sy, sp, a.x,
131  cy * sp * sr - sy * cr, sy * sp * sr + cy * cr, -cp * sr, a.y,
132  -cy * sp * cr - sy * sr, -sy * sp * cr + cy * sr, cp * cr, a.z,
133  0.0f, 0.0f, 0.0f, 1.0};
134 
135  return transform;
136  }
137 
138  // =========================================================================
139  // -- Comparison operators -------------------------------------------------
140  // =========================================================================
141 
142  bool operator==(const Transform &rhs) const {
143  return (location == rhs.location) && (rotation == rhs.rotation);
144  }
145 
146  bool operator!=(const Transform &rhs) const {
147  return !(*this == rhs);
148  }
149 
150  // =========================================================================
151  // -- Conversions to UE4 types ---------------------------------------------
152  // =========================================================================
153 
154 #ifdef LIBCARLA_INCLUDED_FROM_UE4
155 
156  Transform(const FTransform &transform)
157  : Transform(Location(transform.GetLocation()), Rotation(transform.Rotator())) {}
158 
159  operator FTransform() const {
160  const FVector scale{1.0f, 1.0f, 1.0f};
161  return FTransform{FRotator(rotation), FVector(location), scale};
162  }
163 
164 #endif // LIBCARLA_INCLUDED_FROM_UE4
165  };
166 
167 } // namespace geom
168 } // namespace carla
Vector3D GetRightVector() const
Definition: Rotation.h:56
void InverseRotateVector(Vector3D &in_point) const
Definition: Rotation.h:98
std::array< float, 16 > GetInverseMatrix() const
Computes the 4-matrix form of the inverse transformation.
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
Transform(const Location &in_location)
Vector3D GetForwardVector() const
geom::Location Location
Definition: rpc/Location.h:14
bool operator==(const Transform &rhs) const
void TransformVector(Vector3D &in_vector) const
Applies this transformation to in_vector (rotation only).
Transform(const Location &in_location, const Rotation &in_rotation)
void TransformPoint(Vector3D &in_point) const
Applies this transformation to in_point (first translation then rotation).
std::array< float, 16 > GetMatrix() const
Computes the 4-matrix form of the transformation.
static constexpr T ToRadians(T deg)
Definition: Math.h:43
geom::Rotation Rotation
Definition: rpc/Transform.h:14
Vector3D GetUpVector() const
Vector3D GetForwardVector() const
Definition: Rotation.h:52
Vector3D GetRightVector() const
Vector3D GetUpVector() const
Definition: Rotation.h:60
bool operator!=(const Transform &rhs) const
void InverseTransformPoint(Vector3D &in_point) const
Applies the inverse of this transformation to in_point.
void RotateVector(Vector3D &in_point) const
Definition: Rotation.h:64
MSGPACK_DEFINE_ARRAY(location, rotation)