Add addition and subtraction operators to locations

This commit is contained in:
nsubiron 2018-07-29 18:04:57 +02:00
parent 4f5044a495
commit e34a5891b1
3 changed files with 96 additions and 48 deletions

View File

@ -0,0 +1,91 @@
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#pragma once
#include "carla/rpc/MsgPack.h"
#ifdef LIBCARLA_INCLUDED_FROM_UE4
# include "Math/Vector.h"
#endif // LIBCARLA_INCLUDED_FROM_UE4
namespace carla {
namespace rpc {
class Location {
public:
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
Location() = default;
Location(float ix, float iy, float iz)
: x(ix),
y(iy),
z(iz) {}
Location &operator+=(const Location &rhs) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
friend Location operator+(Location lhs, const Location &rhs) {
lhs += rhs;
return lhs;
}
Location &operator-=(const Location &rhs) {
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
friend Location operator-(Location lhs, const Location &rhs) {
lhs -= rhs;
return lhs;
}
#ifdef LIBCARLA_INCLUDED_FROM_UE4
Location(const FVector &vector) // from centimeters to meters.
: Location(1e-2f * vector.X, 1e-2f * vector.Y, 1e-2f * vector.Z) {}
operator FVector() const {
return FVector{1e2f * x, 1e2f * y, 1e2f * z}; // from meters to centimeters.
}
#endif // LIBCARLA_INCLUDED_FROM_UE4
// =========================================================================
/// @todo The following is copy-pasted from MSGPACK_DEFINE_ARRAY.
/// This is a workaround for an issue in msgpack library. The
/// MSGPACK_DEFINE_ARRAY macro is shadowing our `z` variable.
/// https://github.com/msgpack/msgpack-c/issues/709
// =========================================================================
template <typename Packer>
void msgpack_pack(Packer& pk) const
{
clmdep_msgpack::type::make_define_array(x, y, z).msgpack_pack(pk);
}
void msgpack_unpack(clmdep_msgpack::object const& o)
{
clmdep_msgpack::type::make_define_array(x, y, z).msgpack_unpack(o);
}
template <typename MSGPACK_OBJECT>
void msgpack_object(MSGPACK_OBJECT* o, clmdep_msgpack::zone& sneaky_variable_that_shadows_z) const
{
clmdep_msgpack::type::make_define_array(x, y, z).msgpack_object(o, sneaky_variable_that_shadows_z);
}
// =========================================================================
};
} // namespace rpc
} // namespace carla

View File

@ -6,6 +6,7 @@
#pragma once
#include "carla/rpc/Location.h"
#include "carla/rpc/MsgPack.h"
#ifdef LIBCARLA_INCLUDED_FROM_UE4
@ -15,54 +16,6 @@
namespace carla {
namespace rpc {
class Location {
public:
Location() = default;
Location(float ix, float iy, float iz)
: x(ix),
y(iy),
z(iz) {}
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
#ifdef LIBCARLA_INCLUDED_FROM_UE4
Location(const FVector &vector) // from centimeters to meters.
: Location(1e-2f * vector.X, 1e-2f * vector.Y, 1e-2f * vector.Z) {}
operator FVector() const {
return FVector{1e2f * x, 1e2f * y, 1e2f * z}; // from meters to centimeters.
}
#endif // LIBCARLA_INCLUDED_FROM_UE4
// =========================================================================
/// @todo The following is copy-pasted from MSGPACK_DEFINE_ARRAY.
/// This is a workaround for an issue in msgpack library. The
/// MSGPACK_DEFINE_ARRAY macro is shadowing our `z` variable.
/// https://github.com/msgpack/msgpack-c/issues/709
// =========================================================================
template <typename Packer>
void msgpack_pack(Packer& pk) const
{
clmdep_msgpack::type::make_define_array(x, y, z).msgpack_pack(pk);
}
void msgpack_unpack(clmdep_msgpack::object const& o)
{
clmdep_msgpack::type::make_define_array(x, y, z).msgpack_unpack(o);
}
template <typename MSGPACK_OBJECT>
void msgpack_object(MSGPACK_OBJECT* o, clmdep_msgpack::zone& sneaky_variable_that_shadows_z) const
{
clmdep_msgpack::type::make_define_array(x, y, z).msgpack_object(o, sneaky_variable_that_shadows_z);
}
// =========================================================================
};
class Rotation {
public:

View File

@ -44,6 +44,10 @@ void export_transform() {
.def_readwrite("x", &cc::Location::x)
.def_readwrite("y", &cc::Location::y)
.def_readwrite("z", &cc::Location::z)
.def(self += self)
.def(self + self)
.def(self -= self)
.def(self - self)
.def(self_ns::str(self_ns::self))
;