Skip to content

Commit 590d54f

Browse files
Extend ROS2 support Step 1: Update geometry data types (#9425)
* Extend ROS2 support Step 1: Update geometry data types To extend and simplify the native ROS2 support, some additional geometry data types are introducued. Furthermore, some - Introduced geom::AngularVelocity, geom::Acceleration, geom::Quaternion and geom::Velocity to ease the Unreal -> Carla -> ROS2 data conversion - Added some Math functions for Quaternions - Renamed MakeSafeUnitVector->MakeUnitVector (no 'unsafe' variant allowed anymore) - Renamed RotateVector -> RotatedVector to reflect that a rotated vector is returned and the original vector is NOT rotated - Added Math::GetVectorAngle(), Math::GetVectorAngleAbs() and supporting functions. Use within MotionPlanStage to get correct angle delta. BUGFIX: When introducing the rotations via Quaternions it became obvious that the pitch and roll roations have been incorrect up to now. Therefore, the RightHandedVector3D class was introduced to support the conversion Unreal -> CARLA - Fixed geom::Rotation::RotateVector() rotation directions of pitch and roll! - Added RightHandedVector3D.h (internal class deployed for implicit correct rotation of CARLA left handed vectors) - Allow to comment TransformationMatrix access out by ifdef to prevent from erroneous misuse, since the TransformationMatrix does NOT provide a right-handed rotation as the user might expect. (Per default access is still enabled) * use geom types on Actor functions
1 parent cca73be commit 590d54f

28 files changed

+1405
-222
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ build/
3838
*CodeLitePreProcessor.txt
3939
.aria2c.input
4040
.codelite
41+
.debug
4142
.gdb_history
4243
.gtest
4344
.idea

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Improved the turning behavior of vehicles controlled by the TrafficManager, making them smoother.
55
* Added a hybrid solid-state LiDAR with adjustable parameters (blueprint attributes)
66
* Fix OpenDrive Builder lane width
7+
* Introduced geom::AngularVelocity, geom::Velocity, geom::Acceleration, geom::Quaternion types
8+
* Fixed geom::Rotation::RotateVector() rotation directions of pitch and roll
79

810
## CARLA 0.9.16
911

LibCarla/source/carla/client/Actor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ namespace client {
2020
return GetEpisode().Lock()->GetActorTransform(*this);
2121
}
2222

23-
geom::Vector3D Actor::GetVelocity() const {
23+
geom::Velocity Actor::GetVelocity() const {
2424
return GetEpisode().Lock()->GetActorVelocity(*this);
2525
}
2626

27-
geom::Vector3D Actor::GetAngularVelocity() const {
27+
geom::AngularVelocity Actor::GetAngularVelocity() const {
2828
return GetEpisode().Lock()->GetActorAngularVelocity(*this);
2929
}
3030

31-
geom::Vector3D Actor::GetAcceleration() const {
31+
geom::Acceleration Actor::GetAcceleration() const {
3232
return GetEpisode().Lock()->GetActorAcceleration(*this);
3333
}
3434

LibCarla/source/carla/client/Actor.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include "carla/Debug.h"
1010
#include "carla/Memory.h"
1111
#include "carla/client/detail/ActorState.h"
12+
#include "carla/geom/Acceleration.h"
13+
#include "carla/geom/AngularVelocity.h"
14+
#include "carla/geom/Velocity.h"
1215
#include "carla/profiler/LifetimeProfiled.h"
1316

1417
namespace carla {
@@ -45,19 +48,19 @@ namespace client {
4548
///
4649
/// @note This function does not call the simulator, it returns the
4750
/// velocity received in the last tick.
48-
geom::Vector3D GetVelocity() const;
51+
geom::Velocity GetVelocity() const;
4952

5053
/// Return the current 3D angular velocity of the actor.
5154
///
5255
/// @note This function does not call the simulator, it returns the
5356
/// angular velocity received in the last tick.
54-
geom::Vector3D GetAngularVelocity() const;
57+
geom::AngularVelocity GetAngularVelocity() const;
5558

5659
/// Return the current 3D acceleration of the actor.
5760
///
5861
/// @note This function does not call the simulator, it returns the
5962
/// acceleration calculated after the actor's velocity.
60-
geom::Vector3D GetAcceleration() const;
63+
geom::Acceleration GetAcceleration() const;
6164

6265
geom::BoundingBox GetBoundingBox() const;
6366

LibCarla/source/carla/client/detail/Client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ namespace detail {
381381
if (attachment_type == rpc::AttachmentType::SpringArm ||
382382
attachment_type == rpc::AttachmentType::SpringArmGhost)
383383
{
384-
const auto a = transform.location.MakeSafeUnitVector(std::numeric_limits<float>::epsilon());
384+
const auto a = transform.location.MakeUnitVector(std::numeric_limits<float>::epsilon());
385385
const auto z = geom::Vector3D(0.0f, 0.f, 1.0f);
386386
constexpr float OneEps = 1.0f - std::numeric_limits<float>::epsilon();
387387
if (geom::Math::Dot(a, z) > OneEps) {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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/geom/Vector3D.h"
10+
#include "carla/geom/Vector3DInt.h"
11+
#include "carla/geom/Math.h"
12+
13+
#ifdef LIBCARLA_INCLUDED_FROM_UE4
14+
#include <compiler/enable-ue4-macros.h>
15+
#include "Math/Vector.h"
16+
#include <compiler/disable-ue4-macros.h>
17+
#endif // LIBCARLA_INCLUDED_FROM_UE4
18+
19+
namespace carla {
20+
namespace geom {
21+
22+
/**
23+
* Stores the acceleration vector of an entity in m/s²
24+
*/
25+
class Acceleration : public Vector3D {
26+
public:
27+
28+
// =========================================================================
29+
// -- Constructors ---------------------------------------------------------
30+
// =========================================================================
31+
32+
Acceleration() = default;
33+
34+
using Vector3D::Vector3D;
35+
36+
Acceleration(const Vector3D &rhs) : Vector3D(rhs) {}
37+
38+
Acceleration(const Vector3DInt &rhs) :
39+
Vector3D(static_cast<float>(rhs.x),
40+
static_cast<float>(rhs.y),
41+
static_cast<float>(rhs.z)) {}
42+
43+
// =========================================================================
44+
// -- Arithmetic operators -------------------------------------------------
45+
// =========================================================================
46+
47+
Acceleration &operator+=(const Acceleration &rhs) {
48+
static_cast<Vector3D &>(*this) += static_cast<const Vector3D &>(rhs);
49+
return *this;
50+
}
51+
52+
friend Acceleration operator+(Acceleration lhs, const Acceleration &rhs) {
53+
lhs += rhs;
54+
return lhs;
55+
}
56+
57+
Acceleration &operator-=(const Acceleration &rhs) {
58+
static_cast<Vector3D &>(*this) -= static_cast<const Vector3D &>(rhs);
59+
return *this;
60+
}
61+
62+
friend Acceleration operator-(Acceleration lhs, const Acceleration &rhs) {
63+
lhs -= rhs;
64+
return lhs;
65+
}
66+
67+
// =========================================================================
68+
// -- Comparison operators -------------------------------------------------
69+
// =========================================================================
70+
71+
bool operator==(const Acceleration &rhs) const {
72+
return static_cast<const Vector3D &>(*this) == static_cast<const Vector3D &>(rhs);
73+
}
74+
75+
bool operator!=(const Acceleration &rhs) const {
76+
return !(*this == rhs);
77+
}
78+
79+
// =========================================================================
80+
// -- Conversions to UE4 types ---------------------------------------------
81+
// =========================================================================
82+
83+
#ifdef LIBCARLA_INCLUDED_FROM_UE4
84+
85+
// from cm/s² to m/s².
86+
Acceleration(const FVector &vector)
87+
: Acceleration(1e-2f * vector.X, 1e-2f * vector.Y, 1e-2f * vector.Z) {}
88+
89+
// from m/s² to cm/s²
90+
operator FVector() const {
91+
return FVector{1e2f * x, 1e2f * y, 1e2f * z};
92+
}
93+
94+
#endif // LIBCARLA_INCLUDED_FROM_UE4
95+
};
96+
97+
} // namespace geom
98+
} // namespace carla
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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/geom/Vector3D.h"
10+
#include "carla/geom/Vector3DInt.h"
11+
#include "carla/geom/Math.h"
12+
13+
#ifdef LIBCARLA_INCLUDED_FROM_UE4
14+
#include <compiler/enable-ue4-macros.h>
15+
#include "Math/Vector.h"
16+
#include <compiler/disable-ue4-macros.h>
17+
#endif // LIBCARLA_INCLUDED_FROM_UE4
18+
19+
namespace carla {
20+
namespace geom {
21+
22+
/**
23+
* Stores the angular velocity vector in deg/s
24+
*/
25+
class AngularVelocity : public Vector3D {
26+
public:
27+
28+
// =========================================================================
29+
// -- Constructors ---------------------------------------------------------
30+
// =========================================================================
31+
32+
AngularVelocity() = default;
33+
34+
using Vector3D::Vector3D;
35+
36+
AngularVelocity(const Vector3D &rhs) : Vector3D(rhs) {}
37+
38+
AngularVelocity(const Vector3DInt &rhs) :
39+
Vector3D(static_cast<float>(rhs.x),
40+
static_cast<float>(rhs.y),
41+
static_cast<float>(rhs.z)) {}
42+
43+
// =========================================================================
44+
// -- Arithmetic operators -------------------------------------------------
45+
// =========================================================================
46+
47+
AngularVelocity &operator+=(const AngularVelocity &rhs) {
48+
static_cast<Vector3D &>(*this) += static_cast<const Vector3D &>(rhs);
49+
return *this;
50+
}
51+
52+
friend AngularVelocity operator+(AngularVelocity lhs, const AngularVelocity &rhs) {
53+
lhs += rhs;
54+
return lhs;
55+
}
56+
57+
AngularVelocity &operator-=(const AngularVelocity &rhs) {
58+
static_cast<Vector3D &>(*this) -= static_cast<const Vector3D &>(rhs);
59+
return *this;
60+
}
61+
62+
friend AngularVelocity operator-(AngularVelocity lhs, const AngularVelocity &rhs) {
63+
lhs -= rhs;
64+
return lhs;
65+
}
66+
67+
// =========================================================================
68+
// -- Comparison operators -------------------------------------------------
69+
// =========================================================================
70+
71+
bool operator==(const AngularVelocity &rhs) const {
72+
return static_cast<const Vector3D &>(*this) == static_cast<const Vector3D &>(rhs);
73+
}
74+
75+
bool operator!=(const AngularVelocity &rhs) const {
76+
return !(*this == rhs);
77+
}
78+
79+
// =========================================================================
80+
// -- Conversions to UE4 types ---------------------------------------------
81+
// =========================================================================
82+
83+
#ifdef LIBCARLA_INCLUDED_FROM_UE4
84+
85+
// in future potentially transform deg/s -> rad/s?
86+
AngularVelocity(const FVector &vector)
87+
: Vector3D(vector.X, vector.Y, vector.Z) {}
88+
89+
// in future potentially transform rad/s -> deg/s?
90+
operator FVector() const {
91+
return FVector{x, y, z};
92+
}
93+
94+
#endif // LIBCARLA_INCLUDED_FROM_UE4
95+
};
96+
97+
} // namespace geom
98+
} // namespace carla

LibCarla/source/carla/geom/BoundingBox.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ namespace geom {
8383
std::array<Location, 8> GetLocalVertices() const {
8484

8585
return {{
86-
location + Location(rotation.RotateVector({-extent.x,-extent.y,-extent.z})),
87-
location + Location(rotation.RotateVector({-extent.x,-extent.y, extent.z})),
88-
location + Location(rotation.RotateVector({-extent.x, extent.y,-extent.z})),
89-
location + Location(rotation.RotateVector({-extent.x, extent.y, extent.z})),
90-
location + Location(rotation.RotateVector({ extent.x,-extent.y,-extent.z})),
91-
location + Location(rotation.RotateVector({ extent.x,-extent.y, extent.z})),
92-
location + Location(rotation.RotateVector({ extent.x, extent.y,-extent.z})),
93-
location + Location(rotation.RotateVector({ extent.x, extent.y, extent.z}))
86+
location + Location(rotation.RotatedVector(Location(-extent.x,-extent.y,-extent.z))),
87+
location + Location(rotation.RotatedVector(Location(-extent.x,-extent.y, extent.z))),
88+
location + Location(rotation.RotatedVector(Location(-extent.x, extent.y,-extent.z))),
89+
location + Location(rotation.RotatedVector(Location(-extent.x, extent.y, extent.z))),
90+
location + Location(rotation.RotatedVector(Location( extent.x,-extent.y,-extent.z))),
91+
location + Location(rotation.RotatedVector(Location( extent.x,-extent.y, extent.z))),
92+
location + Location(rotation.RotatedVector(Location( extent.x, extent.y,-extent.z))),
93+
location + Location(rotation.RotatedVector(Location( extent.x, extent.y, extent.z)))
9494
}};
9595
}
9696

LibCarla/source/carla/geom/Math.cpp

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,38 @@
77
#include "carla/geom/Math.h"
88

99
#include "carla/geom/Rotation.h"
10+
#include "carla/geom/Quaternion.h"
1011

1112
namespace carla {
1213
namespace geom {
1314

14-
double Math::GetVectorAngle(const Vector3D &a, const Vector3D &b) {
15-
return std::acos(Dot(a, b) / (a.Length() * b.Length()));
15+
float Math::SineVectorAngleFromUnitVectors(Vector3D const &a_unit, Vector3D const &b_unit)
16+
{
17+
auto const cross = Math::Cross(a_unit, b_unit);
18+
Vector3D const to_up(0.f, 0.f, 1.f);
19+
auto const sine_angle_abs = Math::Length(cross);
20+
if ( std::signbit(Math::Dot(cross, to_up) ) )
21+
{
22+
return -sine_angle_abs;
23+
}
24+
else
25+
{
26+
return sine_angle_abs;
27+
}
28+
}
29+
30+
float Math::GetVectorAngleAbs(const Vector3D &a, const Vector3D &b) {
31+
float cosine_vector_angle = Math::CosineVectorAngle(a, b);
32+
return std::acos(cosine_vector_angle);
33+
}
34+
35+
float Math::GetVectorAngle(const Vector3D &a, const Vector3D &b) {
36+
auto const a_unit = a.MakeUnitVector();
37+
auto const b_unit = b.MakeUnitVector();
38+
auto const cosine_vector_angle = Math::CosineVectorAngleFromUnitVectors(a_unit, b_unit);
39+
auto const sine_vector_angle = Math::SineVectorAngleFromUnitVectors(a_unit, b_unit);
40+
auto const angle = std::atan2(sine_vector_angle, cosine_vector_angle);
41+
return angle;
1642
}
1743

1844
std::pair<float, float> Math::DistanceSegmentToPoint(
@@ -114,39 +140,17 @@ namespace geom {
114140
return Vector3D(p.x * c - p.y * s, p.x * s + p.y * c, 0.0f);
115141
}
116142

117-
Vector3D Math::GetForwardVector(const Rotation &rotation) {
118-
const float cp = std::cos(ToRadians(rotation.pitch));
119-
const float sp = std::sin(ToRadians(rotation.pitch));
120-
const float cy = std::cos(ToRadians(rotation.yaw));
121-
const float sy = std::sin(ToRadians(rotation.yaw));
122-
return {cy * cp, sy * cp, sp};
123-
}
143+
Vector3D GetForwardVector(const Rotation &rotation) { return rotation.GetForwardVector(); }
124144

125-
Vector3D Math::GetRightVector(const Rotation &rotation) {
126-
const float cy = std::cos(ToRadians(rotation.yaw));
127-
const float sy = std::sin(ToRadians(rotation.yaw));
128-
const float cr = std::cos(ToRadians(rotation.roll));
129-
const float sr = std::sin(ToRadians(rotation.roll));
130-
const float cp = std::cos(ToRadians(rotation.pitch));
131-
const float sp = std::sin(ToRadians(rotation.pitch));
132-
return {
133-
cy * sp * sr - sy * cr,
134-
sy * sp * sr + cy * cr,
135-
-cp * sr};
136-
}
145+
Vector3D GetRightVector(const Rotation &rotation) { return rotation.GetRightVector(); }
137146

138-
Vector3D Math::GetUpVector(const Rotation &rotation) {
139-
const float cy = std::cos(ToRadians(rotation.yaw));
140-
const float sy = std::sin(ToRadians(rotation.yaw));
141-
const float cr = std::cos(ToRadians(rotation.roll));
142-
const float sr = std::sin(ToRadians(rotation.roll));
143-
const float cp = std::cos(ToRadians(rotation.pitch));
144-
const float sp = std::sin(ToRadians(rotation.pitch));
145-
return {
146-
-cy * sp * cr - sy * sr,
147-
-sy * sp * cr + cy * sr,
148-
cp * cr};
149-
}
147+
Vector3D GetUpVector(const Rotation &rotation) { return rotation.GetUpVector(); }
148+
149+
Vector3D GetForwardVector(const Quaternion &quaternion) { return quaternion.GetForwardVector(); }
150+
151+
Vector3D GetRightVector(const Quaternion &quaternion) { return quaternion.GetRightVector(); }
152+
153+
Vector3D GetUpVector(const Quaternion &quaternion) { return quaternion.GetUpVector(); }
150154

151155
std::vector<int> Math::GenerateRange(int a, int b) {
152156
std::vector<int> result;

0 commit comments

Comments
 (0)