diff --git a/LibCarla/source/carla/geom/Location.h b/LibCarla/source/carla/geom/Location.h index 0ab681256..7ef93d411 100644 --- a/LibCarla/source/carla/geom/Location.h +++ b/LibCarla/source/carla/geom/Location.h @@ -6,7 +6,7 @@ #pragma once -#include "carla/MsgPack.h" +#include "carla/geom/Vector3D.h" #ifdef LIBCARLA_INCLUDED_FROM_UE4 # include "Math/Vector.h" @@ -15,24 +15,21 @@ namespace carla { namespace geom { - class Location { + class Location : private Vector3D { public: - float x = 0.0f; - float y = 0.0f; - float z = 0.0f; + using Vector3D::Vector3D; - Location() = default; + using Vector3D::x; + using Vector3D::y; + using Vector3D::z; - Location(float ix, float iy, float iz) - : x(ix), - y(iy), - z(iz) {} + using Vector3D::msgpack_pack; + using Vector3D::msgpack_unpack; + using Vector3D::msgpack_object; Location &operator+=(const Location &rhs) { - x += rhs.x; - y += rhs.y; - z += rhs.z; + static_cast(*this) += rhs; return *this; } @@ -42,9 +39,7 @@ namespace geom { } Location &operator-=(const Location &rhs) { - x -= rhs.x; - y -= rhs.y; - z -= rhs.z; + static_cast(*this) -= rhs; return *this; } @@ -63,28 +58,6 @@ namespace geom { } #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 - 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 - 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 geom diff --git a/LibCarla/source/carla/geom/Vector3D.h b/LibCarla/source/carla/geom/Vector3D.h new file mode 100644 index 000000000..8f8ddab11 --- /dev/null +++ b/LibCarla/source/carla/geom/Vector3D.h @@ -0,0 +1,76 @@ +// 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 . + +#pragma once + +#include "carla/MsgPack.h" + +namespace carla { +namespace geom { + + class Vector3D { + public: + + float x = 0.0f; + float y = 0.0f; + float z = 0.0f; + + Vector3D() = default; + + Vector3D(float ix, float iy, float iz) + : x(ix), + y(iy), + z(iz) {} + + Vector3D &operator+=(const Vector3D &rhs) { + x += rhs.x; + y += rhs.y; + z += rhs.z; + return *this; + } + + friend Vector3D operator+(Vector3D lhs, const Vector3D &rhs) { + lhs += rhs; + return lhs; + } + + Vector3D &operator-=(const Vector3D &rhs) { + x -= rhs.x; + y -= rhs.y; + z -= rhs.z; + return *this; + } + + friend Vector3D operator-(Vector3D lhs, const Vector3D &rhs) { + lhs -= rhs; + return lhs; + } + + // ========================================================================= + /// @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 + 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 + 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 geom +} // namespace carla diff --git a/PythonAPI/source/libcarla/Transform.cpp b/PythonAPI/source/libcarla/Transform.cpp index 8768abf86..ce4ef7914 100644 --- a/PythonAPI/source/libcarla/Transform.cpp +++ b/PythonAPI/source/libcarla/Transform.cpp @@ -11,6 +11,13 @@ namespace carla { namespace geom { + std::ostream &operator<<(std::ostream &out, const Vector3D &vector3D) { + out << "Vector3D(x=" << vector3D.x + << ", y=" << vector3D.y + << ", z=" << vector3D.z << ')'; + return out; + } + std::ostream &operator<<(std::ostream &out, const Location &location) { out << "Location(x=" << location.x << ", y=" << location.y @@ -37,11 +44,23 @@ void export_transform() { using namespace boost::python; namespace cg = carla::geom; + class_("Vector3D") + .def(init((arg("x")=0.0f, arg("y")=0.0f, arg("z")=0.0f))) + .def_readwrite("x", &cg::Vector3D::x) + .def_readwrite("y", &cg::Vector3D::y) + .def_readwrite("z", &cg::Vector3D::z) + .def(self += self) + .def(self + self) + .def(self -= self) + .def(self - self) + .def(self_ns::str(self_ns::self)) + ; + class_("Location") .def(init((arg("x")=0.0f, arg("y")=0.0f, arg("z")=0.0f))) - .def_readwrite("x", &cg::Location::x) - .def_readwrite("y", &cg::Location::y) - .def_readwrite("z", &cg::Location::z) + .add_property("x", +[](const cg::Location &self) { return self.x; }, +[](cg::Location &self, float x) { self.x = x; }) + .add_property("y", +[](const cg::Location &self) { return self.y; }, +[](cg::Location &self, float y) { self.y = y; }) + .add_property("z", +[](const cg::Location &self) { return self.z; }, +[](cg::Location &self, float z) { self.z = z; }) .def(self += self) .def(self + self) .def(self -= self)