CARLA
Loading...
Searching...
No Matches
TransformQuaternion.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 <array>
10#include <cmath>
11
12namespace carla {
13namespace ros2 {
14
15// Output of TransformFromCarlaRotation: a translation (tx, ty, tz) and a
16// (w, x, y, z) quaternion encoding a CARLA actor's world transform in the
17// ROS 2 right-handed-Z-up convention.
19 std::array<float, 3> translation;
20 std::array<float, 4> rotation; // (w, x, y, z)
21};
22
23// Converts CARLA (pitch, yaw, roll) Euler angles in **degrees** plus an
24// (tx, ty, tz) translation into a ROS-handed (translation, quaternion)
25// pair. This is the math previously inlined inside
26// CarlaTransformPublisher::ComputeTransform; extracting it lets a GTest pin
27// the deg-to-radian conversion + per-axis sign flip without pulling the
28// FastDDS publisher into the Build-Tests/ compile unit.
29//
30// Notes:
31// - degree-to-radian conversion uses M_PI / 180, restored by PR-1
32// (`d9cfc79fb`) after a prior spelling shipped `M_PI_2 / 180` and halved
33// every rotation;
34// - pitch and yaw axes carry a leading sign flip to land in the ROS
35// handedness; roll is left as-is;
36// - the translation Y axis is mirrored to match the same handedness
37// change.
38//
39// M_PI is used here, not std::numbers::pi_v, because this header is consumed
40// by the carla-ros2-native ExternalProject in Ros2Native/, which does not
41// configure CMAKE_CXX_STANDARD and therefore defaults to C++17 where
42// <numbers> is unavailable.
44 float tx, float ty, float tz,
45 float pitch_deg, float yaw_deg, float roll_deg) noexcept {
46 constexpr float deg2rad = static_cast<float>(M_PI) / 180.0f;
47 const float rx = -pitch_deg * deg2rad;
48 const float ry = -yaw_deg * deg2rad;
49 const float rz = roll_deg * deg2rad;
50
51 const float cr = std::cos(rz * 0.5f);
52 const float sr = std::sin(rz * 0.5f);
53 const float cp = std::cos(rx * 0.5f);
54 const float sp = std::sin(rx * 0.5f);
55 const float cy = std::cos(ry * 0.5f);
56 const float sy = std::sin(ry * 0.5f);
57
58 return {
59 {tx, -ty, tz},
60 {
61 cr * cp * cy + sr * sp * sy,
62 sr * cp * cy - cr * sp * sy,
63 cr * sp * cy + sr * cp * sy,
64 cr * cp * sy - sr * sp * cy,
65 },
66 };
67}
68
69} // namespace ros2
70} // namespace carla
TransformQuaternion TransformFromCarlaRotation(float tx, float ty, float tz, float pitch_deg, float yaw_deg, float roll_deg) noexcept
This file contains definitions of common data structures used in traffic manager.