diff --git a/src/viam/sdk/CMakeLists.txt b/src/viam/sdk/CMakeLists.txt index 626254823..fafeb316b 100644 --- a/src/viam/sdk/CMakeLists.txt +++ b/src/viam/sdk/CMakeLists.txt @@ -142,6 +142,7 @@ target_sources(viamsdk ../../viam/sdk/common/exception.hpp ../../viam/sdk/common/linear_algebra.hpp ../../viam/sdk/common/pose.hpp + ../../viam/sdk/common/proto_convert.hpp ../../viam/sdk/common/proto_value.hpp ../../viam/sdk/common/service_helper.hpp ../../viam/sdk/common/utils.hpp diff --git a/src/viam/sdk/common/client_helper.hpp b/src/viam/sdk/common/client_helper.hpp index d8db1f338..95d1f8f09 100644 --- a/src/viam/sdk/common/client_helper.hpp +++ b/src/viam/sdk/common/client_helper.hpp @@ -59,7 +59,7 @@ class ClientHelper { ProtoValue value = key->second; debug_key_ = *value.get(); } - *request_.mutable_extra() = map_to_struct(extra); + *request_.mutable_extra() = v2::to_proto(extra); return with(std::forward(rsc)); } diff --git a/src/viam/sdk/common/linear_algebra.cpp b/src/viam/sdk/common/linear_algebra.cpp index afd7c5b8c..e464f0ee1 100644 --- a/src/viam/sdk/common/linear_algebra.cpp +++ b/src/viam/sdk/common/linear_algebra.cpp @@ -7,55 +7,45 @@ namespace viam { namespace sdk { -Vector3::Vector3(scalar_type x, scalar_type y, scalar_type z) : data_{x, y, z} {}; -Vector3::Vector3() : Vector3(0.0, 0.0, 0.0){}; - double Vector3::x() const { - return this->data_[0]; + return this->data[0]; } double Vector3::y() const { - return this->data_[1]; + return this->data[1]; } double Vector3::z() const { - return this->data_[2]; + return this->data[2]; } Vector3& Vector3::set_x(double x) { - this->data_[0] = x; + this->data[0] = x; return *this; } Vector3& Vector3::set_y(double y) { - this->data_[1] = y; + this->data[1] = y; return *this; } Vector3& Vector3::set_z(double z) { - this->data_[2] = z; + this->data[2] = z; return *this; } -const std::array& Vector3::data() const { - return this->data_; -} +namespace proto_convert_details { -std::array& Vector3::data() { - return this->data_; +void to_proto::operator()(const Vector3& self, common::v1::Vector3* proto) const { + proto->set_x(self.x()); + proto->set_y(self.y()); + proto->set_z(self.z()); } -viam::common::v1::Vector3 Vector3::to_proto() const { - viam::common::v1::Vector3 result; - result.set_x(x()); - result.set_y(y()); - result.set_z(z()); - return result; -}; - -Vector3 Vector3::from_proto(const viam::common::v1::Vector3& vec) { - return {vec.x(), vec.y(), vec.z()}; +Vector3 from_proto::operator()(const common::v1::Vector3* proto) const { + return {proto->x(), proto->y(), proto->z()}; } +} // namespace proto_convert_details } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/common/linear_algebra.hpp b/src/viam/sdk/common/linear_algebra.hpp index 6af24fe1b..e9045f38f 100644 --- a/src/viam/sdk/common/linear_algebra.hpp +++ b/src/viam/sdk/common/linear_algebra.hpp @@ -5,18 +5,25 @@ #include #include -#include +#include + +namespace viam { +namespace common { +namespace v1 { + +class Vector3; +} +} // namespace common +} // namespace viam + namespace viam { namespace sdk { // In the future, we may wish to inline this whole class // for performance reasons. -class Vector3 { - public: +struct Vector3 { using scalar_type = double; - Vector3(scalar_type x, scalar_type y, scalar_type z); - Vector3(); scalar_type x() const; scalar_type y() const; @@ -28,15 +35,22 @@ class Vector3 { /// Set the z value of the vector (can be chained) Vector3& set_z(scalar_type z); - const std::array& data() const; - std::array& data(); - viam::common::v1::Vector3 to_proto() const; - static Vector3 from_proto(const viam::common::v1::Vector3& vec); + std::array data; +}; - private: - std::array data_; +namespace proto_convert_details { + +template <> +struct to_proto { + void operator()(const Vector3&, common::v1::Vector3*) const; +}; + +template <> +struct from_proto { + Vector3 operator()(const common::v1::Vector3*) const; }; +} // namespace proto_convert_details } // namespace sdk } // namespace viam @@ -51,20 +65,20 @@ struct vec_traits { template static inline scalar_type& write_element(vec_type& v) { - return v.data()[I]; + return v.data[I]; } template static inline scalar_type read_element(vec_type const& v) { - return v.data()[I]; + return v.data[I]; } static inline scalar_type& write_element_idx(int i, vec_type& v) { - return v.data()[i]; + return v.data[i]; } static inline scalar_type read_element_idx(int i, vec_type const& v) { - return v.data()[i]; + return v.data[i]; } }; diff --git a/src/viam/sdk/common/pose.cpp b/src/viam/sdk/common/pose.cpp index 521d3127e..051f0712b 100644 --- a/src/viam/sdk/common/pose.cpp +++ b/src/viam/sdk/common/pose.cpp @@ -5,36 +5,6 @@ namespace viam { namespace sdk { -common::v1::PoseInFrame pose_in_frame::to_proto() const { - common::v1::PoseInFrame pif; - *pif.mutable_reference_frame() = reference_frame; - common::v1::Pose proto_pose; - proto_pose.set_x(pose.coordinates.x); - proto_pose.set_y(pose.coordinates.y); - proto_pose.set_z(pose.coordinates.z); - proto_pose.set_o_x(pose.orientation.o_x); - proto_pose.set_o_y(pose.orientation.o_y); - proto_pose.set_o_z(pose.orientation.o_z); - proto_pose.set_theta(pose.theta); - *pif.mutable_pose() = std::move(proto_pose); - return pif; -}; - -pose_in_frame pose_in_frame::from_proto(const common::v1::PoseInFrame& proto) { - pose_in_frame pif; - pif.reference_frame = proto.reference_frame(); - const auto& proto_pose = proto.pose(); - pif.pose.orientation.o_x = proto_pose.o_x(); - pif.pose.orientation.o_y = proto_pose.o_y(); - pif.pose.orientation.o_z = proto_pose.o_z(); - pif.pose.coordinates.x = proto_pose.x(); - pif.pose.coordinates.y = proto_pose.y(); - pif.pose.coordinates.z = proto_pose.z(); - pif.pose.theta = proto_pose.theta(); - - return pif; -} - bool operator==(const pose_in_frame& lhs, const pose_in_frame& rhs) { return lhs.pose == rhs.pose && lhs.reference_frame == rhs.reference_frame; } @@ -44,5 +14,48 @@ std::ostream& operator<<(std::ostream& os, const pose_in_frame& v) { return os; } +namespace proto_convert_details { + +void to_proto::operator()(const pose& self, common::v1::Pose* proto) const { + proto->set_x(self.coordinates.x); + proto->set_y(self.coordinates.y); + proto->set_z(self.coordinates.z); + proto->set_o_x(self.orientation.o_x); + proto->set_o_y(self.orientation.o_y); + proto->set_o_z(self.orientation.o_z); + proto->set_theta(self.theta); +} + +pose from_proto::operator()(const common::v1::Pose* proto) const { + pose pose; + pose.coordinates.x = proto->x(); + pose.coordinates.y = proto->y(); + pose.coordinates.z = proto->z(); + pose.orientation.o_x = proto->o_x(); + pose.orientation.o_y = proto->o_y(); + pose.orientation.o_z = proto->o_z(); + pose.theta = proto->theta(); + + return pose; +} + +void to_proto::operator()(const pose_in_frame& self, + common::v1::PoseInFrame* pif) const { + *(pif->mutable_reference_frame()) = self.reference_frame; + common::v1::Pose proto_pose; + to_proto{}(self.pose, &proto_pose); + *(pif->mutable_pose()) = std::move(proto_pose); +}; + +pose_in_frame from_proto::operator()( + const common::v1::PoseInFrame* proto) const { + pose_in_frame pif; + pif.reference_frame = proto->reference_frame(); + pif.pose = from_proto{}(&(proto->pose())); + + return pif; +} + +} // namespace proto_convert_details } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/common/pose.hpp b/src/viam/sdk/common/pose.hpp index b97353dc8..2eddd18ec 100644 --- a/src/viam/sdk/common/pose.hpp +++ b/src/viam/sdk/common/pose.hpp @@ -1,6 +1,18 @@ #pragma once -#include +#include + +#include + +namespace viam { +namespace common { +namespace v1 { + +class Pose; +class PoseInFrame; +} // namespace v1 +} // namespace common +} // namespace viam namespace viam { namespace sdk { @@ -20,16 +32,11 @@ struct pose { pose_orientation orientation; double theta; - static pose from_proto(const viam::common::v1::Pose& proto); - viam::common::v1::Pose to_proto() const; - friend bool operator==(const pose& lhs, const pose& rhs); friend std::ostream& operator<<(std::ostream& os, const pose& v); }; struct pose_in_frame { - viam::common::v1::PoseInFrame to_proto() const; - static pose_in_frame from_proto(const viam::common::v1::PoseInFrame& proto); pose_in_frame(std::string reference_frame_, struct pose pose_) : reference_frame(std::move(reference_frame_)), pose(std::move(pose_)) {} pose_in_frame() {} @@ -40,5 +47,29 @@ struct pose_in_frame { friend std::ostream& operator<<(std::ostream& os, const pose_in_frame& v); }; +namespace proto_convert_details { + +template <> +struct to_proto { + void operator()(const pose&, common::v1::Pose*) const; +}; + +template <> +struct from_proto { + pose operator()(const common::v1::Pose*) const; +}; + +template <> +struct to_proto { + void operator()(const pose_in_frame&, common::v1::PoseInFrame*) const; +}; + +template <> +struct from_proto { + pose_in_frame operator()(const common::v1::PoseInFrame*) const; +}; + +} // namespace proto_convert_details + } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/common/private/proto_utils.hpp b/src/viam/sdk/common/private/proto_utils.hpp deleted file mode 100644 index 1263587f6..000000000 --- a/src/viam/sdk/common/private/proto_utils.hpp +++ /dev/null @@ -1,63 +0,0 @@ -/// @file common/proto_utils.hpp -/// -/// @brief Utils that require generated proto includes. These should be #included -/// in cpp implementation files, but not in wrapper headers consumed by third party code. -#pragma once - -#include - -namespace viam { -namespace sdk { -namespace impl { - -/// @brief Copies elements from a protobuf repeated pointer array into a std::vector. Src type -/// must have a `to_proto` method. -template -void vecToRepeatedPtr(const std::vector& vec, google::protobuf::RepeatedPtrField& dest) { - dest.Clear(); - dest.Reserve(vec.size()); - for (auto& x : vec) { - *dest.Add() = x.to_proto(); - } -} - -/// @brief Non-member to_proto() version. (necessary for moving generated types out of wrapper -/// headers). Takes explicit `to_proto`. -template -void vecToRepeatedPtr(const std::vector& vec, - google::protobuf::RepeatedPtrField& dest, - Dst to_proto(const Src&)) { - dest.Clear(); - dest.Reserve(vec.size()); - for (auto& x : vec) { - *dest.Add() = to_proto(x); - } -} - -/// @brief Copies elements from a std::vector into a protobuf repeated pointer array. Dst type -/// must have a `from_proto` static method. -template -void repeatedPtrToVec(const google::protobuf::RepeatedPtrField& src, std::vector& vec) { - vec.clear(); - vec.reserve(src.size()); - for (auto& x : src) { - vec.push_back(Dst::from_proto(x)); - } -} - -/// @brief Non-member from_proto() version. (necessary for moving generated types out of wrapper -/// headers). Takes explicit `from_proto`. -template -void repeatedPtrToVec(const google::protobuf::RepeatedPtrField& src, - std::vector& vec, - Dst from_proto(const Src&)) { - vec.clear(); - vec.reserve(src.size()); - for (auto& x : src) { - vec.push_back(from_proto(x)); - } -} - -} // namespace impl -} // namespace sdk -} // namespace viam diff --git a/src/viam/sdk/common/private/repeated_ptr_convert.hpp b/src/viam/sdk/common/private/repeated_ptr_convert.hpp new file mode 100644 index 000000000..030eba8c5 --- /dev/null +++ b/src/viam/sdk/common/private/repeated_ptr_convert.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include + +#include + +#include + +namespace viam { +namespace sdk { +namespace impl { + +struct to_repeated_field_ { + template > + auto operator()(const std::vector& v) const { + ::google::protobuf::RepeatedPtrField> result; + result.Reserve(v.size()); + + for (const auto& elem : v) { + *(result.Add()) = v2::to_proto(elem); + } + + return result; + } +}; + +struct from_repeated_field_ { + template > + auto operator()(const ::google::protobuf::RepeatedPtrField& v) const { + std::vector> result; + result.reserve(v.size()); + + for (const auto& elem : v) { + result.push_back(v2::from_proto(elem)); + } + + return result; + } +}; + +namespace { + +constexpr auto& to_repeated_field = proto_convert_details::static_const::value; + +constexpr auto& from_repeated_field = + proto_convert_details::static_const::value; + +} // namespace + +} // namespace impl +} // namespace sdk +} // namespace viam diff --git a/src/viam/sdk/common/proto_convert.hpp b/src/viam/sdk/common/proto_convert.hpp new file mode 100644 index 000000000..1e2e82626 --- /dev/null +++ b/src/viam/sdk/common/proto_convert.hpp @@ -0,0 +1,99 @@ +#pragma once + +#include + +#include +#include +#include + +namespace viam { +namespace sdk { + +namespace proto_convert_details { + +// This is copied from range-v3 to allow the definition of callable object instances without +// ODR/linkage issues. It is obviated in C++17 and onwards by constexpr inline. +template +struct static_const { + static constexpr const T value{}; +}; + +template +constexpr const T static_const::value; + +// This struct should be explicitly specialized with a +// void operator()(const SdkType&, common::v1::ApiType*) const +// to provide API/ABI insulated proto conversion +template +struct to_proto; + +// This struct should be explicitly specialized with a +// SdkType operator()(const ProtoType*) const +// to provided API/ABI insulated construction from proto +template +struct from_proto; + +// This is a helper type trait for deducing corresponding API types from a to_proto specialization. +// We use boost::callable_traits to generate a tuple of the arguments to the to_proto call operator, +// of which the last entry (mp_back) is a pointer to the API type. +template +using ProtoArgType = std::remove_pointer_t< + boost::mp11::mp_back>>; + +// Implementation struct for the omni-to_proto callable defined below. +struct to_proto_impl { + template + auto operator()(const SdkType& t) const { + using ProtoReturnType = ProtoArgType>; + + ProtoReturnType ret; + to_proto{}(t, &ret); + + return ret; + } +}; + +// Implementation struct for the omni-from_proto callable defined below. +struct from_proto_impl { + template + auto operator()(const ProtoType& proto) const { // NOLINT(misc-no-recursion) + return from_proto{}(&proto); + } +}; + +} // namespace proto_convert_details + +namespace v2 { + +namespace { + +/// @brief Function object implementing conversion from an SDK type to an API type. +/// This callable works for any type with a proto_convert_details::to_proto specialization as +/// described above. +constexpr auto& to_proto = + proto_convert_details::static_const::value; + +/// @brief Function object implementing conversion from an API type to an SDK type. +/// This callable works for any type with a proto_convert_details::from_proto specialization as +/// described above. +constexpr auto& from_proto = + proto_convert_details::static_const::value; + +} // namespace + +} // namespace v2 + +/// @brief Type alias for the API type corresponding to a given SDK type. +/// This is the return type of calling to_proto on an instance of SdkType. +template +using EquivalentApiType = + proto_convert_details::ProtoArgType>; + +/// @brief Type alias for the SDK type corresponding to a given API type. +/// This is the return type of calling from_proto on an instance of ApiType. +template +using EquivalentSdkType = + boost::callable_traits::return_type_t>; + +} // namespace sdk +} // namespace viam diff --git a/src/viam/sdk/common/proto_value.cpp b/src/viam/sdk/common/proto_value.cpp index 0650856de..1521db29a 100644 --- a/src/viam/sdk/common/proto_value.cpp +++ b/src/viam/sdk/common/proto_value.cpp @@ -43,37 +43,6 @@ ProtoValue::ProtoValue(ProtoValue&& other) noexcept(proto_value_details::all_mov ProtoValue::ProtoValue(const ProtoValue& other) : vtable_(other.vtable_), self_(other.self_, other.vtable_) {} -ProtoValue::ProtoValue(const Value* value) // NOLINT(misc-no-recursion) - : ProtoValue([](const Value& v) { // NOLINT(misc-no-recursion) - switch (v.kind_case()) { - case Value::KindCase::kBoolValue: { - return ProtoValue(v.bool_value()); - } - case Value::KindCase::kStringValue: { - return ProtoValue(v.string_value()); - } - case Value::KindCase::kNumberValue: { - return ProtoValue(v.number_value()); - } - case Value::KindCase::kListValue: { - ProtoList vec; - vec.reserve(v.list_value().values_size()); - for (const Value& list_val : v.list_value().values()) { - vec.push_back(ProtoValue::from_proto(list_val)); - } - - return ProtoValue(std::move(vec)); - } - case Value::KindCase::kStructValue: { - return ProtoValue(struct_to_map(v.struct_value())); - } - case Value::KindCase::KIND_NOT_SET: - case Value::KindCase::kNullValue: - default: - return ProtoValue(nullptr); - } - }(*value)) {} - ProtoValue& ProtoValue::operator=(ProtoValue&& other) noexcept( proto_value_details::all_moves_noexcept{}) { ProtoValue(std::move(other)).swap(*this); @@ -98,13 +67,6 @@ void ProtoValue::swap(ProtoValue& other) noexcept(proto_value_details::all_moves std::swap(vtable_, other.vtable_); } -template -ProtoValue ProtoValue::from_proto(const Val& v) { // NOLINT(misc-no-recursion) - return ProtoValue(&v); -} - -template ProtoValue ProtoValue::from_proto(const Value&); - ProtoValue::Kind ProtoValue::kind() const { return vtable_.kind(); } @@ -182,8 +144,8 @@ void ProtoValue::model::move(void* self, void* dest) { } template -void ProtoValue::model::to_proto(void const* self, google::protobuf::Value* v) { - viam::sdk::to_proto(static_cast(self)->data, v); +void ProtoValue::model::to_value(void const* self, google::protobuf::Value* v) { + viam::sdk::proto_value_details::to_value(static_cast(self)->data, v); } template @@ -254,53 +216,94 @@ void ProtoValue::storage::destruct(const ProtoValue::vtable& vtab) noexcept { vtab.dtor(this->get()); } -void to_proto(std::nullptr_t, Value* v) { +namespace proto_value_details { + +void to_value(std::nullptr_t, Value* v) { v->set_null_value(::google::protobuf::NULL_VALUE); } -void to_proto(bool b, Value* v) { +void to_value(bool b, Value* v) { v->set_bool_value(b); } -void to_proto(double d, Value* v) { +void to_value(double d, Value* v) { v->set_number_value(d); } -void to_proto(std::string s, Value* v) { +void to_value(std::string s, Value* v) { v->set_string_value(std::move(s)); } -void to_proto(const ProtoList& vec, Value* v) { +void to_value(const ProtoList& vec, Value* v) { ::google::protobuf::ListValue l; for (const auto& val : vec) { - *l.add_values() = to_proto(val); + *l.add_values() = v2::to_proto(val); } *(v->mutable_list_value()) = std::move(l); } -void to_proto(const ProtoStruct& m, Value* v) { - Struct s; - map_to_struct(m, &s); - - *(v->mutable_struct_value()) = std::move(s); +void to_value(const ProtoStruct& m, Value* v) { + *(v->mutable_struct_value()) = v2::to_proto(m); +} + +} // namespace proto_value_details + +namespace proto_convert_details { + +void to_proto::operator()(const ProtoValue& self, google::protobuf::Value* v) const { + self.vtable_.to_value(self.self_.get(), v); +} + +ProtoValue from_proto::operator()( // NOLINT(misc-no-recursion) + const google::protobuf::Value* v) const { + switch (v->kind_case()) { + case Value::KindCase::kBoolValue: { + return ProtoValue(v->bool_value()); + } + case Value::KindCase::kStringValue: { + return ProtoValue(v->string_value()); + } + case Value::KindCase::kNumberValue: { + return ProtoValue(v->number_value()); + } + case Value::KindCase::kListValue: { + ProtoList vec; + vec.reserve(v->list_value().values_size()); + for (const Value& list_val : v->list_value().values()) { + vec.push_back(v2::from_proto(list_val)); + } + + return ProtoValue(std::move(vec)); + } + case Value::KindCase::kStructValue: { + return ProtoValue(v2::from_proto(v->struct_value())); + } + case Value::KindCase::KIND_NOT_SET: + case Value::KindCase::kNullValue: + default: + return ProtoValue(nullptr); + } } -void to_proto(const ProtoValue& t, Value* v) { - t.vtable_.to_proto(t.self_.get(), v); +void to_proto::operator()(const ProtoStruct& self, google::protobuf::Struct* s) const { + for (const auto& kv : self) { + s->mutable_fields()->insert( + google::protobuf::MapPair(kv.first, v2::to_proto(kv.second))); + } } -void struct_to_map(Struct const* s, ProtoStruct& map) { // NOLINT(misc-no-recursion) +ProtoStruct from_proto::operator()( // NOLINT(misc-no-recursion) + const google::protobuf::Struct* s) const { + ProtoStruct result; + for (const auto& val : s->fields()) { - map.emplace(val.first, ProtoValue::from_proto(val.second)); + result.emplace(val.first, v2::from_proto(val.second)); } -} -void map_to_struct(const ProtoStruct& m, Struct* s) { - for (const auto& kv : m) { - s->mutable_fields()->insert( - google::protobuf::MapPair(kv.first, to_proto(kv.second))); - } + return result; } +} // namespace proto_convert_details + } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/common/proto_value.hpp b/src/viam/sdk/common/proto_value.hpp index 61bd43ea1..cc5102e80 100644 --- a/src/viam/sdk/common/proto_value.hpp +++ b/src/viam/sdk/common/proto_value.hpp @@ -7,6 +7,8 @@ #include #include +#include + namespace google { namespace protobuf { @@ -52,6 +54,8 @@ struct all_moves_noexcept /// definition. class ProtoValue { public: + friend proto_convert_details::to_proto; + /// @brief Type discriminator constants for possible values stored in a ProtoValue. enum Kind { k_null = 0, k_bool = 1, k_double = 2, k_string = 3, k_list = 4, k_struct = 5 }; @@ -110,15 +114,6 @@ class ProtoValue { void swap(ProtoValue& other) noexcept(proto_value_details::all_moves_noexcept{}); - /// @brief Construct from proto value - /// @note This method is trivially templated to insulate google::protobuf::Value from our - /// API/ABI. It is meant to be called with no template parameters in a translation unit which - /// includes - template - static ProtoValue from_proto(const Value& v); // NOLINT(misc-no-recursion) - - friend void to_proto(const ProtoValue& t, google::protobuf::Value* v); - /// @name Value access API ///@{ @@ -178,7 +173,7 @@ class ProtoValue { void (*dtor)(void*); void (*copy)(void const*, void*); void (*move)(void*, void*); - void (*to_proto)(void const*, google::protobuf::Value*); + void (*to_value)(void const*, google::protobuf::Value*); Kind (*kind)(); bool (*equal_to)(void const*, void const*, const vtable&); }; @@ -199,13 +194,13 @@ class ProtoValue { // non-noexcept pointer anyway static void move(void* self, void* dest); - static void to_proto(void const* self, google::protobuf::Value* v); + static void to_value(void const* self, google::protobuf::Value* v); static Kind kind() noexcept; static bool equal_to(void const* self, void const* other, const vtable& other_vtable); - static constexpr vtable vtable_{dtor, copy, move, to_proto, kind, equal_to}; + static constexpr vtable vtable_{dtor, copy, move, to_value, kind, equal_to}; T data; }; @@ -320,55 +315,39 @@ extern template std::string&& ProtoValue::get_unchecked() &&; extern template ProtoList&& ProtoValue::get_unchecked() &&; extern template ProtoStruct&& ProtoValue::get_unchecked() &&; -void to_proto(std::nullptr_t, google::protobuf::Value* v); -void to_proto(bool b, google::protobuf::Value* v); -void to_proto(double d, google::protobuf::Value* v); -void to_proto(std::string s, google::protobuf::Value* v); -void to_proto(const ProtoList& vec, google::protobuf::Value* v); -void to_proto(const ProtoStruct& m, google::protobuf::Value* v); -void to_proto(const ProtoValue& t, google::protobuf::Value* v); - -void struct_to_map(google::protobuf::Struct const* s, ProtoStruct& m); -void map_to_struct(const ProtoStruct& m, google::protobuf::Struct* s); - -/// @brief Convert a type to a google::protobuf::Value. -/// @note This method is trivially templated to insulate google::protobuf::Value from our -/// API/ABI. It is meant to be called with no template parameters in a translation unit which -/// includes -template -Value to_proto(const ProtoValue& proto_value) { - Value v; - to_proto(proto_value, &v); - - return v; -} +namespace proto_convert_details { -/// @brief Convert a google::protobuf::Struct to a ProtoStruct. -/// @note This method is trivially templated to insulate google::protobuf::Struct from our -/// API/ABI. It is meant to be called with no template parameters in a translation unit which -/// includes -template -ProtoStruct struct_to_map(const Struct& s) { - ProtoStruct result; - struct_to_map(&s, result); +template <> +struct to_proto { + void operator()(const ProtoValue&, google::protobuf::Value*) const; +}; - return result; -} +template <> +struct to_proto { + void operator()(const ProtoStruct&, google::protobuf::Struct*) const; +}; -/// @brief Convert a ProtoStruct to a google::protobuf::Struct. -/// @note This method is trivially templated to insulate google::protobuf::Struct from our -/// API/ABI. It is meant to be called with no template parameters in a translation unit which -/// includes -template -Struct map_to_struct(const ProtoStruct& m) { - Struct s; - map_to_struct(m, &s); +template <> +struct from_proto { + ProtoValue operator()(const google::protobuf::Value*) const; +}; - return s; -} +template <> +struct from_proto { + ProtoStruct operator()(const google::protobuf::Struct*) const; +}; + +} // namespace proto_convert_details namespace proto_value_details { +void to_value(std::nullptr_t, google::protobuf::Value* v); +void to_value(bool b, google::protobuf::Value* v); +void to_value(double d, google::protobuf::Value* v); +void to_value(std::string s, google::protobuf::Value* v); +void to_value(const ProtoList& vec, google::protobuf::Value* v); +void to_value(const ProtoStruct& m, google::protobuf::Value* v); + template struct kind {}; diff --git a/src/viam/sdk/common/service_helper.hpp b/src/viam/sdk/common/service_helper.hpp index af65418c5..0dd193fcb 100644 --- a/src/viam/sdk/common/service_helper.hpp +++ b/src/viam/sdk/common/service_helper.hpp @@ -49,7 +49,7 @@ class ServiceHelper : public ServiceHelperBase { } auto getExtra() const { - return request_->has_extra() ? struct_to_map(request_->extra()) : ProtoStruct{}; + return request_->has_extra() ? v2::from_proto(request_->extra()) : ProtoStruct{}; } private: diff --git a/src/viam/sdk/common/world_state.cpp b/src/viam/sdk/common/world_state.cpp index bd1fa5ca6..8b4fb7503 100644 --- a/src/viam/sdk/common/world_state.cpp +++ b/src/viam/sdk/common/world_state.cpp @@ -3,96 +3,81 @@ #include #include +#include namespace viam { namespace sdk { -WorldState::geometries_in_frame WorldState::geometries_in_frame::from_proto( - const common::v1::GeometriesInFrame& proto) { - geometries_in_frame gif; - for (const auto& geo : proto.geometries()) { - gif.geometries.push_back(GeometryConfig::from_proto(geo)); - } - gif.reference_frame = proto.reference_frame(); +WorldState::WorldState(std::vector obstacles, + std::vector transforms) + : obstacles_(std::move(obstacles)), transforms_(std::move(transforms)) {} - return gif; +const std::vector& WorldState::obstacles() const { + return obstacles_; } -common::v1::GeometriesInFrame WorldState::geometries_in_frame::to_proto() const { - common::v1::GeometriesInFrame proto; +const std::vector& WorldState::transforms() const { + return transforms_; +} - *proto.mutable_reference_frame() = reference_frame; - for (const auto& geometry : geometries) { - *proto.mutable_geometries()->Add() = geometry.to_proto(); - } +bool operator==(const WorldState::geometries_in_frame& lhs, + const WorldState::geometries_in_frame& rhs) { + return lhs.reference_frame == rhs.reference_frame && lhs.geometries == rhs.geometries; +} - return proto; +bool operator==(const WorldState::transform& lhs, const WorldState::transform& rhs) { + return std::tie(lhs.reference_frame, lhs.pose_in_observer_frame, lhs.physical_object) == + std::tie(rhs.reference_frame, rhs.pose_in_observer_frame, rhs.physical_object); } -WorldState WorldState::from_proto(const common::v1::WorldState& ws) { - const auto& proto_obstacles = ws.obstacles(); - std::vector obstacles; - for (const auto& po : proto_obstacles) { - obstacles.push_back(geometries_in_frame::from_proto(po)); - } +bool operator==(const WorldState& lhs, const WorldState& rhs) { + return lhs.obstacles_ == rhs.obstacles_ && lhs.transforms_ == rhs.transforms_; +} - const auto& proto_transforms = ws.transforms(); - std::vector transforms; - for (const auto& pt : proto_transforms) { - transforms.push_back(WorldState::transform::from_proto(pt)); - } +namespace proto_convert_details { - return {obstacles, transforms}; +void to_proto::operator()( + const WorldState::geometries_in_frame& self, common::v1::GeometriesInFrame* proto) const { + *(proto->mutable_geometries()) = impl::to_repeated_field(self.geometries); + *(proto->mutable_reference_frame()) = self.reference_frame; } -common::v1::WorldState WorldState::to_proto() const { - common::v1::WorldState proto_ws; - for (const auto& obstacle : obstacles_) { - *proto_ws.mutable_obstacles()->Add() = obstacle.to_proto(); - } - for (const auto& transform : transforms_) { - *proto_ws.mutable_transforms()->Add() = transform.to_proto(); - } - - return proto_ws; +WorldState::geometries_in_frame from_proto::operator()( + const common::v1::GeometriesInFrame* proto) const { + return {impl::from_repeated_field(proto->geometries()), proto->reference_frame()}; } -WorldState::transform WorldState::transform::from_proto(const common::v1::Transform& proto) { - WorldState::transform transform; - transform.reference_frame = proto.reference_frame(); - transform.pose_in_observer_frame = pose_in_frame::from_proto(proto.pose_in_observer_frame()); - if (proto.has_physical_object()) { - transform.physical_object = - std::make_shared(GeometryConfig::from_proto(proto.physical_object())); +void to_proto::operator()(const WorldState::transform& self, + common::v1::Transform* proto) const { + *(proto->mutable_reference_frame()) = self.reference_frame; + *(proto->mutable_pose_in_observer_frame()) = v2::to_proto(self.pose_in_observer_frame); + if (self.physical_object) { + *(proto->mutable_physical_object()) = v2::to_proto(*self.physical_object); } - - return transform; } -common::v1::Transform WorldState::transform::to_proto() const { - common::v1::Transform proto; - *proto.mutable_reference_frame() = reference_frame; - *proto.mutable_pose_in_observer_frame() = pose_in_observer_frame.to_proto(); - if (physical_object) { - *proto.mutable_physical_object() = physical_object->to_proto(); +WorldState::transform from_proto::operator()( + const common::v1::Transform* proto) const { + WorldState::transform result{proto->reference_frame(), + v2::from_proto(proto->pose_in_observer_frame())}; + if (proto->has_physical_object()) { + result.physical_object = v2::from_proto(proto->physical_object()); } - return proto; -} -bool operator==(const WorldState::geometries_in_frame& lhs, - const WorldState::geometries_in_frame& rhs) { - return lhs.reference_frame == rhs.reference_frame && lhs.geometries == rhs.geometries; + return result; } -bool operator==(const WorldState::transform& lhs, const WorldState::transform& rhs) { - return lhs.reference_frame == rhs.reference_frame && - (lhs.physical_object == rhs.physical_object || - *lhs.physical_object == *rhs.physical_object) && - lhs.pose_in_observer_frame == rhs.pose_in_observer_frame; + +void to_proto::operator()(const WorldState& self, common::v1::WorldState* proto) const { + *(proto->mutable_obstacles()) = impl::to_repeated_field(self.obstacles()); + *(proto->mutable_transforms()) = impl::to_repeated_field(self.transforms()); } -bool operator==(const WorldState& lhs, const WorldState& rhs) { - return lhs.obstacles_ == rhs.obstacles_ && lhs.transforms_ == rhs.transforms_; +WorldState from_proto::operator()( + const common::v1::WorldState* proto) const { + return WorldState(impl::from_repeated_field(proto->obstacles()), + impl::from_repeated_field(proto->transforms())); } +} // namespace proto_convert_details } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/common/world_state.hpp b/src/viam/sdk/common/world_state.hpp index 3cc582a3c..aa9fe62b2 100644 --- a/src/viam/sdk/common/world_state.hpp +++ b/src/viam/sdk/common/world_state.hpp @@ -1,10 +1,23 @@ #pragma once -#include +#include #include +#include #include +namespace viam { +namespace common { +namespace v1 { + +class GeometriesInFrame; +class Transform; +class WorldState; + +} // namespace v1 +} // namespace common +} // namespace viam + namespace viam { namespace sdk { @@ -14,26 +27,20 @@ class WorldState { struct geometries_in_frame { std::vector geometries; std::string reference_frame; - common::v1::GeometriesInFrame to_proto() const; - static geometries_in_frame from_proto(const common::v1::GeometriesInFrame& proto); }; struct transform { std::string reference_frame; pose_in_frame pose_in_observer_frame; - std::shared_ptr physical_object; - - common::v1::Transform to_proto() const; - static transform from_proto(const common::v1::Transform& proto); - transform() {} + boost::optional physical_object; }; - common::v1::WorldState to_proto() const; - static WorldState from_proto(const common::v1::WorldState& ws); + WorldState() = default; + + WorldState(std::vector obstacles, std::vector transforms); - WorldState() {} - WorldState(std::vector obstacles, std::vector transforms) - : obstacles_(std::move(obstacles)), transforms_(std::move(transforms)) {} + const std::vector& obstacles() const; + const std::vector& transforms() const; friend bool operator==(const WorldState& lhs, const WorldState& rhs); friend bool operator==(const geometries_in_frame& lhs, const geometries_in_frame& rhs); @@ -44,5 +51,38 @@ class WorldState { std::vector transforms_; }; +namespace proto_convert_details { + +template <> +struct to_proto { + void operator()(const WorldState::geometries_in_frame&, common::v1::GeometriesInFrame*) const; +}; + +template <> +struct from_proto { + WorldState::geometries_in_frame operator()(const common::v1::GeometriesInFrame*) const; +}; + +template <> +struct to_proto { + void operator()(const WorldState::transform&, common::v1::Transform*) const; +}; + +template <> +struct from_proto { + WorldState::transform operator()(const common::v1::Transform*) const; +}; + +template <> +struct to_proto { + void operator()(const WorldState&, common::v1::WorldState*) const; +}; + +template <> +struct from_proto { + WorldState operator()(const common::v1::WorldState*) const; +}; + +} // namespace proto_convert_details } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/components/private/arm_client.cpp b/src/viam/sdk/components/private/arm_client.cpp index a78700168..ea889d1e4 100644 --- a/src/viam/sdk/components/private/arm_client.cpp +++ b/src/viam/sdk/components/private/arm_client.cpp @@ -17,12 +17,12 @@ ArmClient::ArmClient(std::string name, std::shared_ptr channel) pose ArmClient::get_end_position(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetEndPosition) .with(extra) - .invoke([&](auto& response) { return pose::from_proto(response.pose()); }); + .invoke([&](auto& response) { return v2::from_proto(response.pose()); }); } void ArmClient::move_to_position(const pose& pose, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::MoveToPosition) - .with(extra, [&](auto& request) { *request.mutable_to() = pose.to_proto(); }) + .with(extra, [&](auto& request) { *request.mutable_to() = v2::to_proto(pose); }) .invoke(); } @@ -83,8 +83,8 @@ void ArmClient::stop(const ProtoStruct& extra) { ProtoStruct ArmClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } Arm::KinematicsData ArmClient::get_kinematics(const ProtoStruct& extra) { @@ -109,7 +109,7 @@ Arm::KinematicsData ArmClient::get_kinematics(const ProtoStruct& extra) { std::vector ArmClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/arm_server.cpp b/src/viam/sdk/components/private/arm_server.cpp index fad2c0399..505c2122c 100644 --- a/src/viam/sdk/components/private/arm_server.cpp +++ b/src/viam/sdk/components/private/arm_server.cpp @@ -16,7 +16,7 @@ ::grpc::Status ArmServer::GetEndPosition( return make_service_helper( "ArmServer::GetEndPosition", this, request)([&](auto& helper, auto& arm) { const pose p = arm->get_end_position(helper.getExtra()); - *response->mutable_pose() = p.to_proto(); + *response->mutable_pose() = v2::to_proto(p); }); } @@ -26,7 +26,7 @@ ::grpc::Status ArmServer::MoveToPosition( ::viam::component::arm::v1::MoveToPositionResponse*) noexcept { return make_service_helper( "ArmServer::MoveToPosition", this, request)([&](auto& helper, auto& arm) { - arm->move_to_position(pose::from_proto(request->to()), helper.getExtra()); + arm->move_to_position(v2::from_proto(request->to()), helper.getExtra()); }); } @@ -98,8 +98,8 @@ ::grpc::Status ArmServer::DoCommand(::grpc::ServerContext*, const ::viam::common::v1::DoCommandRequest* request, ::viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper("ArmServer::DoCommand", this, request)([&](auto&, auto& arm) { - const ProtoStruct result = arm->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = arm->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } @@ -143,7 +143,7 @@ ::grpc::Status ArmServer::GetGeometries( "ArmServer::GetGeometries", this, request)([&](auto& helper, auto& arm) { const std::vector geometries = arm->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/base_client.cpp b/src/viam/sdk/components/private/base_client.cpp index 8a31e288e..93623d04c 100644 --- a/src/viam/sdk/components/private/base_client.cpp +++ b/src/viam/sdk/components/private/base_client.cpp @@ -53,8 +53,8 @@ void BaseClient::set_power(const Vector3& linear, return make_client_helper(this, *stub_, &StubType::SetPower) .with(extra, [&](auto& request) { - *request.mutable_linear() = linear.to_proto(); - *request.mutable_angular() = angular.to_proto(); + *request.mutable_linear() = v2::to_proto(linear); + *request.mutable_angular() = v2::to_proto(angular); }) .invoke(); } @@ -65,8 +65,8 @@ void BaseClient::set_velocity(const Vector3& linear, return make_client_helper(this, *stub_, &StubType::SetVelocity) .with(extra, [&](auto& request) { - *request.mutable_linear() = linear.to_proto(); - *request.mutable_angular() = angular.to_proto(); + *request.mutable_linear() = v2::to_proto(linear); + *request.mutable_angular() = v2::to_proto(angular); }) .invoke(); } @@ -84,7 +84,7 @@ bool BaseClient::is_moving() { std::vector BaseClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); } Base::properties BaseClient::get_properties(const ProtoStruct& extra) { @@ -99,8 +99,8 @@ Base::properties BaseClient::get_properties(const ProtoStruct& extra) { ProtoStruct BaseClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/base_server.cpp b/src/viam/sdk/components/private/base_server.cpp index 238542721..722a1f9ae 100644 --- a/src/viam/sdk/components/private/base_server.cpp +++ b/src/viam/sdk/components/private/base_server.cpp @@ -40,8 +40,8 @@ ::grpc::Status BaseServer::SetPower(::grpc::ServerContext*, ::viam::component::base::v1::SetPowerResponse*) noexcept { return make_service_helper( "BaseServer::SetPower", this, request)([&](auto& helper, auto& base) { - auto linear = Vector3::from_proto(request->linear()); - auto angular = Vector3::from_proto(request->angular()); + auto linear = v2::from_proto(request->linear()); + auto angular = v2::from_proto(request->angular()); base->set_power(linear, angular, helper.getExtra()); }); } @@ -52,8 +52,8 @@ ::grpc::Status BaseServer::SetVelocity( ::viam::component::base::v1::SetVelocityResponse*) noexcept { return make_service_helper( "BaseServer::SetVelocity", this, request)([&](auto& helper, auto& base) { - auto linear = Vector3::from_proto(request->linear()); - auto angular = Vector3::from_proto(request->angular()); + auto linear = v2::from_proto(request->linear()); + auto angular = v2::from_proto(request->angular()); base->set_velocity(linear, angular, helper.getExtra()); }); } @@ -83,7 +83,7 @@ ::grpc::Status BaseServer::GetGeometries( "BaseServer::GetGeometries", this, request)([&](auto& helper, auto& base) { const std::vector geometries = base->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } @@ -106,8 +106,8 @@ ::grpc::Status BaseServer::DoCommand(grpc::ServerContext*, viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "BaseServer::DoCommand", this, request)([&](auto&, auto& base) { - const ProtoStruct result = base->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = base->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } diff --git a/src/viam/sdk/components/private/board_client.cpp b/src/viam/sdk/components/private/board_client.cpp index 76ad230c1..b6c6a952f 100644 --- a/src/viam/sdk/components/private/board_client.cpp +++ b/src/viam/sdk/components/private/board_client.cpp @@ -105,8 +105,8 @@ void BoardClient::set_pwm_frequency(const std::string& pin, ProtoStruct BoardClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } // TODO(RSDK-6048) update `client_wrapper` to allow for requests without a `mutable_name()` method, @@ -119,7 +119,7 @@ Board::analog_response BoardClient::read_analog(const std::string& analog_reader request.set_board_name(this->name()); request.set_analog_reader_name(analog_reader_name); - *request.mutable_extra() = map_to_struct(extra); + *request.mutable_extra() = v2::to_proto(extra); const grpc::Status status = stub_->ReadAnalogReader(ctx, request, &response); if (!status.ok()) { @@ -149,7 +149,7 @@ Board::digital_value BoardClient::read_digital_interrupt(const std::string& digi request.set_board_name(this->name()); request.set_digital_interrupt_name(digital_interrupt_name); - *request.mutable_extra() = map_to_struct(extra); + *request.mutable_extra() = v2::to_proto(extra); const grpc::Status status = stub_->GetDigitalInterruptValue(ctx, request, &response); if (!status.ok()) { @@ -191,7 +191,7 @@ void BoardClient::set_power_mode(power_mode power_mode, std::vector BoardClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); }; } // namespace impl diff --git a/src/viam/sdk/components/private/board_server.cpp b/src/viam/sdk/components/private/board_server.cpp index 6f09e2011..b3064912a 100644 --- a/src/viam/sdk/components/private/board_server.cpp +++ b/src/viam/sdk/components/private/board_server.cpp @@ -105,8 +105,8 @@ ::grpc::Status BoardServer::DoCommand(grpc::ServerContext*, viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "BoardServer::DoCommand", this, request)([&](auto&, auto& board) { - const ProtoStruct result = board->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = board->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } @@ -128,7 +128,7 @@ ::grpc::Status BoardServer::ReadAnalogReader( ProtoStruct extra; if (request->has_extra()) { - extra = struct_to_map(request->extra()); + extra = v2::from_proto(request->extra()); } const Board::analog_response result = board->read_analog(request->analog_reader_name(), extra); @@ -158,7 +158,7 @@ ::grpc::Status BoardServer::WriteAnalog( ProtoStruct extra; if (request->has_extra()) { - extra = struct_to_map(request->extra()); + extra = v2::from_proto(request->extra()); } board->write_analog(request->pin(), request->value(), extra); @@ -183,7 +183,7 @@ ::grpc::Status BoardServer::GetDigitalInterruptValue( ProtoStruct extra; if (request->has_extra()) { - extra = struct_to_map(request->extra()); + extra = v2::from_proto(request->extra()); } const Board::digital_value result = @@ -242,7 +242,7 @@ ::grpc::Status BoardServer::GetGeometries( "BoardServer::GetGeometries", this, request)([&](auto& helper, auto& board) { const std::vector geometries = board->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/camera_client.cpp b/src/viam/sdk/components/private/camera_client.cpp index 5b164e31f..eb2e24ced 100644 --- a/src/viam/sdk/components/private/camera_client.cpp +++ b/src/viam/sdk/components/private/camera_client.cpp @@ -106,8 +106,8 @@ CameraClient::CameraClient(std::string name, std::shared_ptr chan ProtoStruct CameraClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); }; Camera::raw_image CameraClient::get_image(std::string mime_type, const ProtoStruct& extra) { @@ -134,7 +134,7 @@ Camera::point_cloud CameraClient::get_point_cloud(std::string mime_type, const P std::vector CameraClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); }; Camera::properties CameraClient::get_properties() { diff --git a/src/viam/sdk/components/private/camera_server.cpp b/src/viam/sdk/components/private/camera_server.cpp index ea1e89154..eb3532814 100644 --- a/src/viam/sdk/components/private/camera_server.cpp +++ b/src/viam/sdk/components/private/camera_server.cpp @@ -42,8 +42,8 @@ ::grpc::Status CameraServer::DoCommand(::grpc::ServerContext*, ::viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "CameraServer::DoCommand", this, request)([&](auto&, auto& camera) { - const ProtoStruct result = camera->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = camera->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } @@ -132,7 +132,7 @@ ::grpc::Status CameraServer::GetGeometries( "CameraServer::GetGeometries", this, request)([&](auto& helper, auto& camera) { const std::vector geometries = camera->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/encoder_client.cpp b/src/viam/sdk/components/private/encoder_client.cpp index 4468f25c6..320b095bd 100644 --- a/src/viam/sdk/components/private/encoder_client.cpp +++ b/src/viam/sdk/components/private/encoder_client.cpp @@ -67,13 +67,13 @@ Encoder::properties EncoderClient::get_properties(const ProtoStruct& extra) { std::vector EncoderClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); }; ProtoStruct EncoderClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/encoder_server.cpp b/src/viam/sdk/components/private/encoder_server.cpp index dfb6c0aa9..a85cbf157 100644 --- a/src/viam/sdk/components/private/encoder_server.cpp +++ b/src/viam/sdk/components/private/encoder_server.cpp @@ -56,7 +56,7 @@ ::grpc::Status EncoderServer::GetGeometries( "EncoderServer::GetGeometries", this, request)([&](auto& helper, auto& encoder) { const std::vector geometries = encoder->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } @@ -66,8 +66,8 @@ ::grpc::Status EncoderServer::DoCommand(grpc::ServerContext*, viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "EncoderServer::DoCommand", this, request)([&](auto&, auto& encoder) { - const ProtoStruct result = encoder->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = encoder->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } diff --git a/src/viam/sdk/components/private/gantry_client.cpp b/src/viam/sdk/components/private/gantry_client.cpp index 18ac97b62..028c1c55f 100644 --- a/src/viam/sdk/components/private/gantry_client.cpp +++ b/src/viam/sdk/components/private/gantry_client.cpp @@ -65,14 +65,14 @@ void GantryClient::stop(const ProtoStruct& extra) { ProtoStruct GantryClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } std::vector GantryClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/gantry_server.cpp b/src/viam/sdk/components/private/gantry_server.cpp index 5819cd524..124f6bcef 100644 --- a/src/viam/sdk/components/private/gantry_server.cpp +++ b/src/viam/sdk/components/private/gantry_server.cpp @@ -75,8 +75,8 @@ ::grpc::Status GantryServer::DoCommand(::grpc::ServerContext*, ::viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "GantryServer::DoCommand", this, request)([&](auto&, auto& gantry) { - const ProtoStruct result = gantry->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = gantry->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } @@ -88,7 +88,7 @@ ::grpc::Status GantryServer::GetGeometries( "GantryServer::GetGeometries", this, request)([&](auto& helper, auto& gantry) { const std::vector geometries = gantry->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/generic_client.cpp b/src/viam/sdk/components/private/generic_client.cpp index 007dd9e51..ec062a860 100644 --- a/src/viam/sdk/components/private/generic_client.cpp +++ b/src/viam/sdk/components/private/generic_client.cpp @@ -23,14 +23,14 @@ GenericComponentClient::GenericComponentClient(std::string name, ProtoStruct GenericComponentClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } std::vector GenericComponentClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/generic_server.cpp b/src/viam/sdk/components/private/generic_server.cpp index 34a63a0ba..75860c819 100644 --- a/src/viam/sdk/components/private/generic_server.cpp +++ b/src/viam/sdk/components/private/generic_server.cpp @@ -17,8 +17,8 @@ ::grpc::Status GenericComponentServer::DoCommand( ::viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "GenericComponentServer::DoCommand", this, request)([&](auto&, auto& generic) { - const ProtoStruct result = generic->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = generic->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } ::grpc::Status GenericComponentServer::GetGeometries( @@ -29,7 +29,7 @@ ::grpc::Status GenericComponentServer::GetGeometries( "GenericComponentServer::GetGeometries", this, request)([&](auto& helper, auto& generic) { const std::vector geometries = generic->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/gripper_client.cpp b/src/viam/sdk/components/private/gripper_client.cpp index 7da3cea58..6e77fd52a 100644 --- a/src/viam/sdk/components/private/gripper_client.cpp +++ b/src/viam/sdk/components/private/gripper_client.cpp @@ -38,14 +38,14 @@ void GripperClient::stop(const ProtoStruct& extra) { ProtoStruct GripperClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } std::vector GripperClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/gripper_server.cpp b/src/viam/sdk/components/private/gripper_server.cpp index 33d3e07b3..1675829af 100644 --- a/src/viam/sdk/components/private/gripper_server.cpp +++ b/src/viam/sdk/components/private/gripper_server.cpp @@ -46,8 +46,8 @@ ::grpc::Status GripperServer::DoCommand(::grpc::ServerContext*, ::viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "GripperServer::DoCommand", this, request)([&](auto&, auto& gripper) { - const ProtoStruct result = gripper->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = gripper->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } @@ -59,7 +59,7 @@ ::grpc::Status GripperServer::GetGeometries( "GripperServer::GetGeometries", this, request)([&](auto& helper, auto& gripper) { const std::vector geometries = gripper->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/motor_client.cpp b/src/viam/sdk/components/private/motor_client.cpp index a734502cc..bad14ccc6 100644 --- a/src/viam/sdk/components/private/motor_client.cpp +++ b/src/viam/sdk/components/private/motor_client.cpp @@ -102,7 +102,7 @@ Motor::power_status MotorClient::get_power_status(const ProtoStruct& extra) { std::vector MotorClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); } bool MotorClient::is_moving() { @@ -113,8 +113,8 @@ bool MotorClient::is_moving() { ProtoStruct MotorClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/motor_server.cpp b/src/viam/sdk/components/private/motor_server.cpp index 75cb6cc7c..66f366fe6 100644 --- a/src/viam/sdk/components/private/motor_server.cpp +++ b/src/viam/sdk/components/private/motor_server.cpp @@ -118,7 +118,7 @@ ::grpc::Status MotorServer::GetGeometries( "MotorServer::GetGeometries", this, request)([&](auto& helper, auto& motor) { const std::vector geometries = motor->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } @@ -128,8 +128,8 @@ ::grpc::Status MotorServer::DoCommand(grpc::ServerContext*, viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "MotorServer::GetGeometries", this, request)([&](auto&, auto& motor) { - const ProtoStruct result = motor->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = motor->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } diff --git a/src/viam/sdk/components/private/movement_sensor_client.cpp b/src/viam/sdk/components/private/movement_sensor_client.cpp index 7646cbe85..40eeddcde 100644 --- a/src/viam/sdk/components/private/movement_sensor_client.cpp +++ b/src/viam/sdk/components/private/movement_sensor_client.cpp @@ -27,7 +27,7 @@ MovementSensor::compassheading from_proto( MovementSensor::position from_proto( const viam::component::movementsensor::v1::GetPositionResponse& proto) { MovementSensor::position position; - position.coordinate = geo_point::from_proto(proto.coordinate()); + position.coordinate = v2::from_proto(proto.coordinate()); position.altitude_m = proto.altitude_m(); return position; } @@ -63,13 +63,13 @@ using namespace viam::component::movementsensor::v1; Vector3 MovementSensorClient::get_linear_velocity(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetLinearVelocity) .with(extra) - .invoke([](auto& response) { return Vector3::from_proto(response.linear_velocity()); }); + .invoke([](auto& response) { return v2::from_proto(response.linear_velocity()); }); } Vector3 MovementSensorClient::get_angular_velocity(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetAngularVelocity) .with(extra) - .invoke([](auto& response) { return Vector3::from_proto(response.angular_velocity()); }); + .invoke([](auto& response) { return v2::from_proto(response.angular_velocity()); }); } MovementSensor::compassheading MovementSensorClient::get_compass_heading(const ProtoStruct& extra) { @@ -112,19 +112,19 @@ std::unordered_map MovementSensorClient::get_accuracy( Vector3 MovementSensorClient::get_linear_acceleration(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetLinearAcceleration) .with(extra) - .invoke([](auto& response) { return Vector3::from_proto(response.linear_acceleration()); }); + .invoke([](auto& response) { return v2::from_proto(response.linear_acceleration()); }); } ProtoStruct MovementSensorClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } std::vector MovementSensorClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/movement_sensor_server.cpp b/src/viam/sdk/components/private/movement_sensor_server.cpp index 6cfb32dc0..7f48a61e5 100644 --- a/src/viam/sdk/components/private/movement_sensor_server.cpp +++ b/src/viam/sdk/components/private/movement_sensor_server.cpp @@ -33,7 +33,7 @@ ::grpc::Status MovementSensorServer::GetLinearVelocity( this, request)([&](auto& helper, auto& movementsensor) { const Vector3 result = movementsensor->get_linear_velocity(helper.getExtra()); - *response->mutable_linear_velocity() = result.to_proto(); + *response->mutable_linear_velocity() = v2::to_proto(result); }); } @@ -45,7 +45,7 @@ ::grpc::Status MovementSensorServer::GetAngularVelocity( this, request)([&](auto& helper, auto& movementsensor) { const Vector3 result = movementsensor->get_angular_velocity(helper.getExtra()); - *response->mutable_angular_velocity() = result.to_proto(); + *response->mutable_angular_velocity() = v2::to_proto(result); }); } @@ -80,7 +80,7 @@ ::grpc::Status MovementSensorServer::GetPosition(::grpc::ServerContext*, return make_service_helper("MovementSensorServer::GetPosition", this, request)( [&](auto& helper, auto& movementsensor) { const MovementSensor::position result = movementsensor->get_position(helper.getExtra()); - *response->mutable_coordinate() = result.coordinate.to_proto(); + *response->mutable_coordinate() = v2::to_proto(result.coordinate); response->set_altitude_m(result.altitude_m); }); } @@ -121,7 +121,7 @@ ::grpc::Status MovementSensorServer::GetLinearAcceleration( this, request)([&](auto& helper, auto& movementsensor) { const Vector3 result = movementsensor->get_linear_acceleration(helper.getExtra()); - *response->mutable_linear_acceleration() = result.to_proto(); + *response->mutable_linear_acceleration() = v2::to_proto(result); }); } @@ -131,8 +131,8 @@ ::grpc::Status MovementSensorServer::DoCommand( viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "MovementSensorServer::DoCommand", this, request)([&](auto&, auto& movementsensor) { - const ProtoStruct result = movementsensor->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = movementsensor->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } @@ -145,7 +145,7 @@ ::grpc::Status MovementSensorServer::GetGeometries( request)([&](auto& helper, auto& movementsensor) { const auto geometries = movementsensor->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/pose_tracker_client.cpp b/src/viam/sdk/components/private/pose_tracker_client.cpp index 6b2794540..25e1f3490 100644 --- a/src/viam/sdk/components/private/pose_tracker_client.cpp +++ b/src/viam/sdk/components/private/pose_tracker_client.cpp @@ -25,7 +25,7 @@ PoseTracker::pose_map PoseTrackerClient::get_poses(const std::vector PoseTrackerClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); } ProtoStruct PoseTrackerClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/pose_tracker_server.cpp b/src/viam/sdk/components/private/pose_tracker_server.cpp index ea26daaa3..028acb775 100644 --- a/src/viam/sdk/components/private/pose_tracker_server.cpp +++ b/src/viam/sdk/components/private/pose_tracker_server.cpp @@ -25,7 +25,7 @@ ::grpc::Status PoseTrackerServer::GetPoses( {request->body_names().begin(), request->body_names().end()}, helper.getExtra()); for (const auto& pair : result) { - response->mutable_body_poses()->insert({pair.first, pair.second.to_proto()}); + response->mutable_body_poses()->insert({pair.first, v2::to_proto(pair.second)}); } }); } @@ -36,8 +36,8 @@ ::grpc::Status PoseTrackerServer::DoCommand( viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "PoseTrackerServer::DoCommand", this, request)([&](auto&, auto& pose_tracker) { - const ProtoStruct result = pose_tracker->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = pose_tracker->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } @@ -50,7 +50,7 @@ ::grpc::Status PoseTrackerServer::GetGeometries( const std::vector geometries = pose_tracker->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/power_sensor_client.cpp b/src/viam/sdk/components/private/power_sensor_client.cpp index 63f2cc6b8..4c064b9ca 100644 --- a/src/viam/sdk/components/private/power_sensor_client.cpp +++ b/src/viam/sdk/components/private/power_sensor_client.cpp @@ -38,7 +38,7 @@ PowerSensor::current from_proto(const GetCurrentResponse& proto) { PowerSensorClient::PowerSensorClient(std::string name, std::shared_ptr channel) : PowerSensor(std::move(name)), stub_(PowerSensorService::NewStub(channel)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} PowerSensor::voltage PowerSensorClient::get_voltage(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetVoltage) @@ -64,7 +64,7 @@ ProtoStruct PowerSensorClient::get_readings(const ProtoStruct& extra) { .invoke([](auto& response) { ProtoStruct result; for (const auto& r : response.readings()) { - result.emplace(r.first, ProtoValue::from_proto(r.second)); + result.emplace(r.first, v2::from_proto(r.second)); } return result; }); @@ -72,8 +72,8 @@ ProtoStruct PowerSensorClient::get_readings(const ProtoStruct& extra) { ProtoStruct PowerSensorClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/power_sensor_server.cpp b/src/viam/sdk/components/private/power_sensor_server.cpp index 69461775f..4d81f013e 100644 --- a/src/viam/sdk/components/private/power_sensor_server.cpp +++ b/src/viam/sdk/components/private/power_sensor_server.cpp @@ -27,7 +27,7 @@ GetCurrentResponse to_proto(const PowerSensor::current& c) { } PowerSensorServer::PowerSensorServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status PowerSensorServer::GetVoltage(::grpc::ServerContext*, const GetVoltageRequest* request, @@ -65,10 +65,8 @@ ::grpc::Status PowerSensorServer::GetReadings( viam::common::v1::GetReadingsResponse* response) noexcept { return make_service_helper( "PowerSensorServer::GetReadings", this, request)([&](auto& helper, auto& powersensor) { - const ProtoStruct result = powersensor->get_readings(helper.getExtra()); - for (const auto& r : result) { - response->mutable_readings()->insert({r.first, to_proto(r.second)}); - } + *(response->mutable_readings()) = + v2::to_proto(powersensor->get_readings(helper.getExtra())).fields(); }); } @@ -78,8 +76,8 @@ ::grpc::Status PowerSensorServer::DoCommand( viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "PowerSensorServer::DoCommand", this, request)([&](auto&, auto& powersensor) { - const ProtoStruct result = powersensor->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = powersensor->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } diff --git a/src/viam/sdk/components/private/sensor_client.cpp b/src/viam/sdk/components/private/sensor_client.cpp index 04e0b3e1a..b28811dd1 100644 --- a/src/viam/sdk/components/private/sensor_client.cpp +++ b/src/viam/sdk/components/private/sensor_client.cpp @@ -29,7 +29,7 @@ ProtoStruct SensorClient::get_readings(const ProtoStruct& extra) { .invoke([](auto& response) { ProtoStruct result; for (const auto& r : response.readings()) { - result.emplace(r.first, ProtoValue::from_proto(r.second)); + result.emplace(r.first, v2::from_proto(r.second)); } return result; }); @@ -37,14 +37,14 @@ ProtoStruct SensorClient::get_readings(const ProtoStruct& extra) { ProtoStruct SensorClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } std::vector SensorClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/sensor_server.cpp b/src/viam/sdk/components/private/sensor_server.cpp index c7a623f63..47301b040 100644 --- a/src/viam/sdk/components/private/sensor_server.cpp +++ b/src/viam/sdk/components/private/sensor_server.cpp @@ -20,10 +20,8 @@ ::grpc::Status SensorServer::GetReadings(::grpc::ServerContext*, GetReadingsResponse* response) noexcept { return make_service_helper( "SensorServer::GetReadings", this, request)([&](auto& helper, auto& sensor) { - const ProtoStruct result = sensor->get_readings(helper.getExtra()); - for (const auto& r : result) { - response->mutable_readings()->insert({r.first, to_proto(r.second)}); - } + *(response->mutable_readings()) = + v2::to_proto(sensor->get_readings(helper.getExtra())).fields(); }); } @@ -32,8 +30,8 @@ ::grpc::Status SensorServer::DoCommand(grpc::ServerContext*, DoCommandResponse* response) noexcept { return make_service_helper( "SensorServer::DoCommand", this, request)([&](auto&, auto& sensor) { - const ProtoStruct result = sensor->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = sensor->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } @@ -44,7 +42,7 @@ ::grpc::Status SensorServer::GetGeometries(::grpc::ServerContext*, "SensorServer::GetGeometries", this, request)([&](auto& helper, auto& sensor) { const auto geometries = sensor->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/servo_client.cpp b/src/viam/sdk/components/private/servo_client.cpp index 1bd483687..eb5004e84 100644 --- a/src/viam/sdk/components/private/servo_client.cpp +++ b/src/viam/sdk/components/private/servo_client.cpp @@ -52,13 +52,13 @@ bool ServoClient::is_moving() { std::vector ServoClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return v2::from_proto(response); }); } ProtoStruct ServoClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/servo_server.cpp b/src/viam/sdk/components/private/servo_server.cpp index de594db63..831b57781 100644 --- a/src/viam/sdk/components/private/servo_server.cpp +++ b/src/viam/sdk/components/private/servo_server.cpp @@ -57,7 +57,7 @@ ::grpc::Status ServoServer::GetGeometries( "ServoServer::GetGeometries", this, request)([&](auto& helper, auto& servo) { const std::vector geometries = servo->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = v2::to_proto(geometry); } }); } @@ -67,8 +67,8 @@ ::grpc::Status ServoServer::DoCommand(grpc::ServerContext*, viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "ServoServer::GetGeometries", this, request)([&](auto&, auto& servo) { - const ProtoStruct result = servo->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = servo->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } diff --git a/src/viam/sdk/config/resource.cpp b/src/viam/sdk/config/resource.cpp index 758a52637..ed772a328 100644 --- a/src/viam/sdk/config/resource.cpp +++ b/src/viam/sdk/config/resource.cpp @@ -18,6 +18,8 @@ namespace viam { namespace sdk { +ResourceConfig::ResourceConfig(std::string type) : api_({kRDK, type, ""}), type_(std::move(type)) {} + Name ResourceConfig::resource_name() { this->fix_api(); std::vector remotes; @@ -94,7 +96,7 @@ ResourceConfig ResourceConfig::from_proto(const viam::app::v1::ComponentConfig& resource.name_ = proto_cfg.name(); resource.namespace__ = proto_cfg.namespace_(); resource.type_ = proto_cfg.type(); - resource.attributes_ = struct_to_map(proto_cfg.attributes()); + resource.attributes_ = v2::from_proto(proto_cfg.attributes()); const std::string& api = proto_cfg.api(); if (api.find(':') != std::string::npos) { resource.api_ = API::from_string(api); @@ -104,7 +106,7 @@ ResourceConfig ResourceConfig::from_proto(const viam::app::v1::ComponentConfig& resource.fix_api(); if (proto_cfg.has_frame()) { - resource.frame_ = LinkConfig::from_proto(proto_cfg.frame()); + resource.frame_ = v2::from_proto(proto_cfg.frame()); } return resource; @@ -112,14 +114,14 @@ ResourceConfig ResourceConfig::from_proto(const viam::app::v1::ComponentConfig& viam::app::v1::ComponentConfig ResourceConfig::to_proto() const { viam::app::v1::ComponentConfig proto_cfg; - const google::protobuf::Struct s = map_to_struct(attributes_); + const google::protobuf::Struct s = v2::to_proto(attributes_); const google::protobuf::RepeatedPtrField service_configs; for (const auto& svc_cfg : service_config_) { viam::app::v1::ResourceLevelServiceConfig cfg; *cfg.mutable_type() = svc_cfg.type; - *cfg.mutable_attributes() = map_to_struct(svc_cfg.attributes); + *cfg.mutable_attributes() = v2::to_proto(svc_cfg.attributes); *proto_cfg.mutable_service_configs()->Add() = cfg; } @@ -129,16 +131,14 @@ viam::app::v1::ComponentConfig ResourceConfig::to_proto() const { *proto_cfg.mutable_api() = api_.to_string(); const std::string mm = model_.to_string(); *proto_cfg.mutable_model() = mm; - *proto_cfg.mutable_attributes() = map_to_struct(attributes_); + *proto_cfg.mutable_attributes() = v2::to_proto(attributes_); for (const auto& dep : depends_on_) { *proto_cfg.mutable_depends_on()->Add() = dep; } - *proto_cfg.mutable_frame() = frame_.to_proto(); + *proto_cfg.mutable_frame() = v2::to_proto(frame_); return proto_cfg; } -ResourceConfig::ResourceConfig(std::string type) : api_({kRDK, type, ""}), type_(std::move(type)){}; - } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/referenceframe/frame.cpp b/src/viam/sdk/referenceframe/frame.cpp index 299942384..d93ec36b4 100644 --- a/src/viam/sdk/referenceframe/frame.cpp +++ b/src/viam/sdk/referenceframe/frame.cpp @@ -12,50 +12,45 @@ namespace viam { namespace sdk { -viam::app::v1::Frame LinkConfig::to_proto() const { - viam::app::v1::Frame frame; +LinkConfig::LinkConfig(translation t, Orientation o, GeometryConfig gcfg, std::string parent) + : translation_(std::move(t)), + orientation_(std::move(o)), + geometry_(std::move(gcfg)), + parent_(std::move(parent)) {} - *frame.mutable_parent() = parent_; - *frame.mutable_geometry() = geometry_.to_proto(); - *frame.mutable_orientation() = orientation_.to_proto(); - *frame.mutable_translation() = translation_.to_proto(); - return frame; -}; - -LinkConfig LinkConfig::from_proto(const viam::app::v1::Frame& proto) { - LinkConfig lc; - - lc.parent_ = proto.parent(); - lc.translation_.x = proto.translation().x(); - lc.translation_.y = proto.translation().y(); - lc.translation_.z = proto.translation().z(); - - if (proto.has_orientation()) { - lc.orientation_ = OrientationConfig::from_proto(proto.orientation()); - } - - if (proto.has_geometry()) { - lc.geometry_ = GeometryConfig::from_proto(proto.geometry()); - } - - return lc; -}; - -translation LinkConfig::get_translation() const { +const translation& LinkConfig::get_translation() const { return translation_; } -OrientationConfig LinkConfig::get_orientation_config() const { +const Orientation& LinkConfig::get_orientation() const { return orientation_; } -GeometryConfig LinkConfig::get_geometry_config() const { +const GeometryConfig& LinkConfig::get_geometry_config() const { return geometry_; } -std::string LinkConfig::get_parent() const { +const std::string& LinkConfig::get_parent() const { return parent_; } +namespace proto_convert_details { + +void to_proto::operator()(const LinkConfig& self, app::v1::Frame* proto) const { + *(proto->mutable_parent()) = self.get_parent(); + *(proto->mutable_geometry()) = v2::to_proto(self.get_geometry_config()); + *(proto->mutable_orientation()) = v2::to_proto(self.get_orientation()); + *(proto->mutable_translation()) = v2::to_proto(self.get_translation()); +} + +LinkConfig from_proto::operator()(const app::v1::Frame* proto) const { + return LinkConfig( + v2::from_proto(proto->translation()), + proto->has_orientation() ? v2::from_proto(proto->orientation()) : Orientation{}, + proto->has_geometry() ? v2::from_proto(proto->geometry()) : GeometryConfig{}, + proto->parent()); +} + +} // namespace proto_convert_details } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/referenceframe/frame.hpp b/src/viam/sdk/referenceframe/frame.hpp index 4d5f593e5..9d5dfd3ca 100644 --- a/src/viam/sdk/referenceframe/frame.hpp +++ b/src/viam/sdk/referenceframe/frame.hpp @@ -2,30 +2,51 @@ #include -#include - +#include #include #include +namespace viam { +namespace app { +namespace v1 { + +class Frame; +} +} // namespace app +} // namespace viam + namespace viam { namespace sdk { class LinkConfig { public: - viam::app::v1::Frame to_proto() const; - static LinkConfig from_proto(const viam::app::v1::Frame& proto); - translation get_translation() const; - OrientationConfig get_orientation_config() const; - GeometryConfig get_geometry_config() const; - std::string get_parent() const; + LinkConfig() = default; + LinkConfig(translation t, Orientation o, GeometryConfig gcfg, std::string parent); + + const translation& get_translation() const; + const Orientation& get_orientation() const; + const GeometryConfig& get_geometry_config() const; + const std::string& get_parent() const; private: - std::string id_; translation translation_; - OrientationConfig orientation_; + Orientation orientation_; GeometryConfig geometry_; std::string parent_; }; +namespace proto_convert_details { + +template <> +struct to_proto { + void operator()(const LinkConfig&, app::v1::Frame*) const; +}; + +template <> +struct from_proto { + LinkConfig operator()(const app::v1::Frame*) const; +}; + +} // namespace proto_convert_details } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 15cac8e17..290ea8210 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -66,15 +67,15 @@ RobotClient::discovery_query from_proto(const DiscoveryQuery& proto) { RobotClient::discovery from_proto(const Discovery& proto) { RobotClient::discovery discovery; discovery.query = from_proto(proto.query()); - discovery.results = struct_to_map(proto.results()); + discovery.results = v2::from_proto(proto.results()); return discovery; } RobotClient::frame_system_config from_proto(const FrameSystemConfig& proto) { RobotClient::frame_system_config fsconfig; - fsconfig.frame = WorldState::transform::from_proto(proto.frame()); + fsconfig.frame = v2::from_proto(proto.frame()); if (proto.has_kinematics()) { - fsconfig.kinematics = struct_to_map(proto.kinematics()); + fsconfig.kinematics = v2::from_proto(proto.kinematics()); } return fsconfig; } @@ -85,7 +86,7 @@ RobotClient::status from_proto(const Status& proto) { status.name = Name::from_proto(proto.name()); } if (proto.has_status()) { - status.status_map = struct_to_map(proto.status()); + status.status_map = v2::from_proto(proto.status()); } if (proto.has_last_reconfigured()) { status.last_reconfigured = timestamp_to_time_pt(proto.last_reconfigured()); @@ -101,7 +102,7 @@ RobotClient::operation from_proto(const Operation& proto) { op.session_id = proto.session_id(); } if (proto.has_arguments()) { - op.arguments = struct_to_map(proto.arguments()); + op.arguments = v2::from_proto(proto.arguments()); } if (proto.has_started()) { op.started = timestamp_to_time_pt(proto.started()); @@ -115,20 +116,20 @@ bool operator==(const RobotClient::discovery_query& lhs, const RobotClient::disc } bool operator==(const RobotClient::discovery& lhs, const RobotClient::discovery& rhs) { - return lhs.query == rhs.query && map_to_struct(lhs.results).SerializeAsString() == - map_to_struct(rhs.results).SerializeAsString(); + return lhs.query == rhs.query && v2::to_proto(lhs.results).SerializeAsString() == + v2::to_proto(rhs.results).SerializeAsString(); } bool operator==(const RobotClient::frame_system_config& lhs, const RobotClient::frame_system_config& rhs) { - return lhs.frame == rhs.frame && map_to_struct(lhs.kinematics).SerializeAsString() == - map_to_struct(rhs.kinematics).SerializeAsString(); + return lhs.frame == rhs.frame && v2::to_proto(lhs.kinematics).SerializeAsString() == + v2::to_proto(rhs.kinematics).SerializeAsString(); } bool operator==(const RobotClient::status& lhs, const RobotClient::status& rhs) { return lhs.name == rhs.name && - map_to_struct(lhs.status_map).SerializeAsString() == - map_to_struct(rhs.status_map).SerializeAsString() && + v2::to_proto(lhs.status_map).SerializeAsString() == + v2::to_proto(rhs.status_map).SerializeAsString() && lhs.last_reconfigured == rhs.last_reconfigured; } @@ -368,10 +369,7 @@ std::vector RobotClient::get_frame_system_conf viam::robot::v1::FrameSystemConfigResponse resp; ClientContext ctx; - RepeatedPtrField* req_transforms = req.mutable_supplemental_transforms(); - for (const WorldState::transform& transform : additional_transforms) { - *req_transforms->Add() = transform.to_proto(); - } + *(req.mutable_supplemental_transforms()) = sdk::impl::to_repeated_field(additional_transforms); const grpc::Status response = impl_->stub_->FrameSystemConfig(ctx, req, &resp); if (is_error_response(response)) { @@ -398,20 +396,16 @@ pose_in_frame RobotClient::transform_pose( viam::robot::v1::TransformPoseResponse resp; ClientContext ctx; - *req.mutable_source() = query.to_proto(); + *req.mutable_source() = v2::to_proto(query); *req.mutable_destination() = std::move(destination); - RepeatedPtrField* req_transforms = req.mutable_supplemental_transforms(); - - for (const WorldState::transform& transform : additional_transforms) { - *req_transforms->Add() = transform.to_proto(); - } + *req.mutable_supplemental_transforms() = sdk::impl::to_repeated_field(additional_transforms); const grpc::Status response = impl_->stub_->TransformPose(ctx, req, &resp); if (is_error_response(response)) { BOOST_LOG_TRIVIAL(error) << "Error getting PoseInFrame: " << response.error_message(); } - return pose_in_frame::from_proto(resp.pose()); + return v2::from_proto(resp.pose()); } std::vector RobotClient::discover_components( @@ -461,7 +455,7 @@ void RobotClient::stop_all(const std::unordered_map& extra) { for (const auto& xtra : extra) { const Name& name = xtra.first; const ProtoStruct& params = xtra.second; - const google::protobuf::Struct s = map_to_struct(params); + const google::protobuf::Struct s = v2::to_proto(params); viam::robot::v1::StopExtraParameters stop; *stop.mutable_name() = name.to_proto(); *stop.mutable_params() = s; diff --git a/src/viam/sdk/robot/service.cpp b/src/viam/sdk/robot/service.cpp index 20f5d62bb..cb8bdc40b 100644 --- a/src/viam/sdk/robot/service.cpp +++ b/src/viam/sdk/robot/service.cpp @@ -185,7 +185,7 @@ ::grpc::Status RobotService_::StopAll(::grpc::ServerContext*, std::unordered_map extra; for (const auto& ex : request->extra()) { const google::protobuf::Struct& struct_ = ex.params(); - const ProtoStruct value_map = struct_to_map(struct_); + const ProtoStruct value_map = v2::from_proto(struct_); const std::string name = ex.name().SerializeAsString(); extra.emplace(name, value_map); } diff --git a/src/viam/sdk/services/navigation.cpp b/src/viam/sdk/services/navigation.cpp index 016927564..e77d56628 100644 --- a/src/viam/sdk/services/navigation.cpp +++ b/src/viam/sdk/services/navigation.cpp @@ -1,6 +1,5 @@ #include -#include #include namespace viam { diff --git a/src/viam/sdk/services/private/generic_client.cpp b/src/viam/sdk/services/private/generic_client.cpp index de953d291..dabe414ec 100644 --- a/src/viam/sdk/services/private/generic_client.cpp +++ b/src/viam/sdk/services/private/generic_client.cpp @@ -22,8 +22,8 @@ GenericServiceClient::GenericServiceClient(std::string name, std::shared_ptr manager) - : ResourceServer(std::move(manager)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status GenericServiceServer::DoCommand( ::grpc::ServerContext*, @@ -17,8 +17,8 @@ ::grpc::Status GenericServiceServer::DoCommand( ::viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "GenericServiceServer::DoCommand", this, request)([&](auto&, auto& generic) { - const ProtoStruct result = generic->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = generic->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); } diff --git a/src/viam/sdk/services/private/mlmodel_client.cpp b/src/viam/sdk/services/private/mlmodel_client.cpp index d02405980..8b7fd6039 100644 --- a/src/viam/sdk/services/private/mlmodel_client.cpp +++ b/src/viam/sdk/services/private/mlmodel_client.cpp @@ -49,7 +49,7 @@ std::shared_ptr MLModelServiceClient::infer( auto* const req = pb::Arena::VIAM_SDK_PB_CREATE_MESSAGE(arena.get()); req->set_name(this->name()); - *req->mutable_extra() = map_to_struct(extra); + *req->mutable_extra() = v2::to_proto(extra); auto* const resp = pb::Arena::VIAM_SDK_PB_CREATE_MESSAGE(arena.get()); ClientContext ctx; @@ -93,7 +93,7 @@ struct MLModelService::metadata MLModelServiceClient::metadata(const ProtoStruct // Encode metadata args into a `MetadataRequest` viam::service::mlmodel::v1::MetadataRequest req; *req.mutable_name() = name(); - *req.mutable_extra() = map_to_struct(extra); + *req.mutable_extra() = v2::to_proto(extra); // Invoke the stub ClientContext ctx; @@ -150,7 +150,7 @@ struct MLModelService::metadata MLModelServiceClient::metadata(const ProtoStruct } } if (s.has_extra()) { - ti.extra = struct_to_map(s.extra()); + ti.extra = v2::from_proto(s.extra()); } } }; diff --git a/src/viam/sdk/services/private/mlmodel_server.cpp b/src/viam/sdk/services/private/mlmodel_server.cpp index da0ac4b91..2e3d3c2c2 100644 --- a/src/viam/sdk/services/private/mlmodel_server.cpp +++ b/src/viam/sdk/services/private/mlmodel_server.cpp @@ -142,7 +142,7 @@ ::grpc::Status MLModelServiceServer::Metadata( break; } } - *new_entry.mutable_extra() = map_to_struct(s.extra); + *new_entry.mutable_extra() = v2::to_proto(s.extra); } return ::grpc::Status(); }; diff --git a/src/viam/sdk/services/private/motion_client.cpp b/src/viam/sdk/services/private/motion_client.cpp index d2c6c12c3..115fd5280 100644 --- a/src/viam/sdk/services/private/motion_client.cpp +++ b/src/viam/sdk/services/private/motion_client.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -140,7 +141,7 @@ Motion::steps steps_from_proto( for (const auto& ps : proto) { step step; for (const auto& component : ps.step()) { - step.emplace(component.first, pose::from_proto(component.second.pose())); + step.emplace(component.first, v2::from_proto(component.second.pose())); } steps.push_back(std::move(step)); } @@ -189,12 +190,12 @@ bool MotionClient::move(const pose_in_frame& destination, .with(extra, [&](auto& request) { *request.mutable_component_name() = component_name.to_proto(); - *request.mutable_destination() = destination.to_proto(); + *request.mutable_destination() = v2::to_proto(destination); if (constraints) { *request.mutable_constraints() = to_proto(*constraints); } if (world_state) { - *request.mutable_world_state() = world_state->to_proto(); + *request.mutable_world_state() = v2::to_proto(*world_state); } }) .invoke([](auto& response) { return response.success(); }); @@ -210,12 +211,12 @@ std::string MotionClient::move_on_map( return make_client_helper(this, *stub_, &StubType::MoveOnMap) .with(extra, [&](auto& request) { - *request.mutable_destination() = destination.to_proto(); + *request.mutable_destination() = v2::to_proto(destination); *request.mutable_component_name() = component_name.to_proto(); *request.mutable_slam_service_name() = slam_name.to_proto(); for (const auto& obstacle : obstacles) { - *request.mutable_obstacles()->Add() = obstacle.to_proto(); + *request.mutable_obstacles()->Add() = v2::to_proto(obstacle); } if (motion_configuration) { @@ -237,7 +238,7 @@ std::string MotionClient::move_on_globe( return make_client_helper(this, *stub_, &StubType::MoveOnGlobe) .with(extra, [&](auto& request) { - *request.mutable_destination() = destination.to_proto(); + *request.mutable_destination() = v2::to_proto(destination); *request.mutable_component_name() = component_name.to_proto(); *request.mutable_movement_sensor_name() = movement_sensor_name.to_proto(); @@ -245,17 +246,13 @@ std::string MotionClient::move_on_globe( request.set_heading(*heading); } - for (const auto& obstacle : obstacles) { - *request.mutable_obstacles()->Add() = obstacle.to_proto(); - } + *request.mutable_obstacles() = impl::to_repeated_field(obstacles); if (motion_configuration) { *request.mutable_motion_configuration() = to_proto(*motion_configuration); } - for (const auto& bounding_region : bounding_regions) { - *request.mutable_bounding_regions()->Add() = bounding_region.to_proto(); - } + *request.mutable_bounding_regions() = impl::to_repeated_field(bounding_regions); }) .invoke([](auto& response) { return response.execution_id(); }); } @@ -270,11 +267,10 @@ pose_in_frame MotionClient::get_pose( [&](auto& request) { *request.mutable_component_name() = component_name.to_proto(); *request.mutable_destination_frame() = destination_frame; - for (const auto& transform : supplemental_transforms) { - *request.mutable_supplemental_transforms()->Add() = transform.to_proto(); - } + *request.mutable_supplemental_transforms() = + impl::to_repeated_field(supplemental_transforms); }) - .invoke([](auto& response) { return pose_in_frame::from_proto(response.pose()); }); + .invoke([](auto& response) { return v2::from_proto(response.pose()); }); } void MotionClient::stop_plan(const Name& name, const ProtoStruct& extra) { @@ -352,8 +348,8 @@ std::vector MotionClient::list_active_plan_statuses ProtoStruct MotionClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } } // namespace impl diff --git a/src/viam/sdk/services/private/motion_server.cpp b/src/viam/sdk/services/private/motion_server.cpp index be8c05191..a703ba587 100644 --- a/src/viam/sdk/services/private/motion_server.cpp +++ b/src/viam/sdk/services/private/motion_server.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -51,7 +52,7 @@ service::motion::v1::PlanStep to_proto(const Motion::steps::step& step) { service::motion::v1::PlanStep proto; for (const auto& kv : step) { service::motion::v1::ComponentState cs; - *cs.mutable_pose() = kv.second.to_proto(); + *cs.mutable_pose() = v2::to_proto(kv.second); proto.mutable_step()->insert({kv.first, cs}); } @@ -177,7 +178,7 @@ ::grpc::Status MotionServer::Move(::grpc::ServerContext*, "MotionServer::Move", this, request)([&](auto& helper, auto& motion) { std::shared_ptr ws; if (request->has_world_state()) { - ws = std::make_shared(WorldState::from_proto(request->world_state())); + ws = std::make_shared(v2::from_proto(request->world_state())); } std::shared_ptr constraints; @@ -185,7 +186,7 @@ ::grpc::Status MotionServer::Move(::grpc::ServerContext*, constraints = std::make_shared(from_proto(request->constraints())); } - const bool success = motion->move(pose_in_frame::from_proto(request->destination()), + const bool success = motion->move(v2::from_proto(request->destination()), Name::from_proto(request->component_name()), std::move(ws), std::move(constraints), @@ -200,7 +201,7 @@ ::grpc::Status MotionServer::MoveOnMap( ::viam::service::motion::v1::MoveOnMapResponse* response) noexcept { return make_service_helper( "MotionServer::MoveOnMap", this, request)([&](auto& helper, auto& motion) { - const auto destination = pose::from_proto(request->destination()); + const auto destination = v2::from_proto(request->destination()); const auto component_name = Name::from_proto(request->component_name()); const auto slam_name = Name::from_proto(request->slam_service_name()); @@ -212,7 +213,7 @@ ::grpc::Status MotionServer::MoveOnMap( std::vector obstacles; for (const auto& obstacle : request->obstacles()) { - obstacles.push_back(GeometryConfig::from_proto(obstacle)); + obstacles.push_back(v2::from_proto(obstacle)); } const std::string execution_id = motion->move_on_map( @@ -228,15 +229,12 @@ ::grpc::Status MotionServer::MoveOnGlobe( ::viam::service::motion::v1::MoveOnGlobeResponse* response) noexcept { return make_service_helper( "MotionServer::MoveOnGlobe", this, request)([&](auto& helper, auto& motion) { - const auto destination = geo_point::from_proto(request->destination()); + const auto destination = v2::from_proto(request->destination()); const auto component_name = Name::from_proto(request->component_name()); const auto movement_sensor_name = Name::from_proto(request->movement_sensor_name()); - std::vector obstacles; - std::vector bounding_regions; - - for (const auto& obstacle : request->obstacles()) { - obstacles.push_back(geo_geometry::from_proto(obstacle)); - } + const std::vector obstacles = impl::from_repeated_field(request->obstacles()); + const std::vector bounding_regions = + impl::from_repeated_field(request->bounding_regions()); boost::optional heading; if (request->has_heading()) { @@ -249,10 +247,6 @@ ::grpc::Status MotionServer::MoveOnGlobe( std::make_shared(from_proto(request->motion_configuration())); } - for (const auto& bounding_region : request->bounding_regions()) { - bounding_regions.push_back(geo_geometry::from_proto(bounding_region)); - } - const std::string execution_id = motion->move_on_globe(destination, heading, component_name, @@ -276,11 +270,11 @@ ::grpc::Status MotionServer::GetPose( const std::string& destination_frame = request->destination_frame(); std::vector supplemental_transforms; for (const auto& proto_transform : request->supplemental_transforms()) { - supplemental_transforms.push_back(WorldState::transform::from_proto(proto_transform)); + supplemental_transforms.push_back(v2::from_proto(proto_transform)); } const pose_in_frame pose = motion->get_pose( component_name, destination_frame, supplemental_transforms, helper.getExtra()); - *response->mutable_pose() = pose.to_proto(); + *response->mutable_pose() = v2::to_proto(pose); }); }; @@ -354,8 +348,8 @@ ::grpc::Status MotionServer::DoCommand(::grpc::ServerContext*, ::viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "MotionServer::DoCommand", this, request)([&](auto&, auto& motion) { - const ProtoStruct result = motion->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = motion->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); }; diff --git a/src/viam/sdk/services/private/navigation_client.cpp b/src/viam/sdk/services/private/navigation_client.cpp index 7834d5009..5b9bb73db 100644 --- a/src/viam/sdk/services/private/navigation_client.cpp +++ b/src/viam/sdk/services/private/navigation_client.cpp @@ -8,35 +8,44 @@ #include #include -#include +#include #include #include #include namespace viam { namespace sdk { -namespace impl { -using namespace viam::service::navigation::v1; +namespace proto_convert_details { -Navigation::Waypoint from_proto(const viam::service::navigation::v1::Waypoint& proto) { - return Navigation::Waypoint{proto.id(), geo_point::from_proto(proto.location())}; -} +template <> +struct from_proto { + Navigation::Path operator()(const service::navigation::v1::Path* proto) const { + return {proto->destination_waypoint_id(), impl::from_repeated_field(proto->geopoints())}; + } +}; -Navigation::Path from_proto(const viam::service::navigation::v1::Path& proto) { - Navigation::Path ret{proto.destination_waypoint_id()}; - repeatedPtrToVec(proto.geopoints(), ret.geopoints); - return ret; -} +template <> +struct from_proto { + Navigation::Waypoint operator()(const service::navigation::v1::Waypoint* proto) const { + return {proto->id(), v2::from_proto(proto->location())}; + } +}; + +} // namespace proto_convert_details + +namespace impl { + +using namespace viam::service::navigation::v1; NavigationClient::NavigationClient(std::string name, std::shared_ptr channel) : Navigation(std::move(name)), stub_(service::navigation::v1::NavigationService::NewStub(channel)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} Navigation::Mode NavigationClient::get_mode(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetMode) - .with([&](auto& request) { *request.mutable_extra() = map_to_struct(extra); }) + .with([&](auto& request) { *request.mutable_extra() = v2::to_proto(extra); }) .invoke([](auto& response) { return Navigation::Mode(response.mode()); }); } @@ -44,17 +53,17 @@ void NavigationClient::set_mode(const Navigation::Mode mode, const ProtoStruct& return make_client_helper(this, *stub_, &StubType::SetMode) .with([&](auto& request) { request.set_mode(viam::service::navigation::v1::Mode(mode)); - *request.mutable_extra() = map_to_struct(extra); + *request.mutable_extra() = v2::to_proto(extra); }) .invoke([](auto& response) {}); } Navigation::LocationResponse NavigationClient::get_location(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetLocation) - .with([&](auto& request) { *request.mutable_extra() = map_to_struct(extra); }) + .with([&](auto& request) { *request.mutable_extra() = v2::to_proto(extra); }) .invoke([](auto& response) { return Navigation::LocationResponse{ - geo_point::from_proto(response.location()), + v2::from_proto(response.location()), response.compass_heading(), }; }); @@ -62,19 +71,15 @@ Navigation::LocationResponse NavigationClient::get_location(const ProtoStruct& e std::vector NavigationClient::get_waypoints(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetWaypoints) - .with([&](auto& request) { *request.mutable_extra() = map_to_struct(extra); }) - .invoke([](auto& response) { - std::vector ret; - repeatedPtrToVec(response.waypoints(), ret, from_proto); - return ret; - }); + .with([&](auto& request) { *request.mutable_extra() = v2::to_proto(extra); }) + .invoke([](auto& response) { return impl::from_repeated_field(response.waypoints()); }); } void NavigationClient::add_waypoint(const geo_point& location, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::AddWaypoint) .with([&](auto& request) { - *request.mutable_location() = location.to_proto(); - *request.mutable_extra() = map_to_struct(extra); + *request.mutable_location() = v2::to_proto(location); + *request.mutable_extra() = v2::to_proto(extra); }) .invoke([](auto& response) {}); } @@ -83,29 +88,21 @@ void NavigationClient::remove_waypoint(const std::string id, const ProtoStruct& return make_client_helper(this, *stub_, &StubType::RemoveWaypoint) .with([&](auto& request) { *request.mutable_id() = id; - *request.mutable_extra() = map_to_struct(extra); + *request.mutable_extra() = v2::to_proto(extra); }) .invoke([](auto& response) {}); } std::vector NavigationClient::get_obstacles(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetObstacles) - .with([&](auto& request) { *request.mutable_extra() = map_to_struct(extra); }) - .invoke([](auto& response) { - std::vector ret; - repeatedPtrToVec(response.obstacles(), ret); - return ret; - }); + .with([&](auto& request) { *request.mutable_extra() = v2::to_proto(extra); }) + .invoke([](auto& response) { return impl::from_repeated_field(response.obstacles()); }); } std::vector NavigationClient::get_paths(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetPaths) - .with([&](auto& request) { *request.mutable_extra() = map_to_struct(extra); }) - .invoke([](auto& response) { - std::vector ret; - repeatedPtrToVec(response.paths(), ret, from_proto); - return ret; - }); + .with([&](auto& request) { *request.mutable_extra() = v2::to_proto(extra); }) + .invoke([](auto& response) { return impl::from_repeated_field(response.paths()); }); } NavigationClient::Properties NavigationClient::get_properties() { @@ -116,8 +113,8 @@ NavigationClient::Properties NavigationClient::get_properties() { ProtoStruct NavigationClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) - .with([&](auto& request) { *request.mutable_command() = map_to_struct(command); }) - .invoke([](auto& response) { return struct_to_map(response.result()); }); + .with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); }) + .invoke([](auto& response) { return v2::from_proto(response.result()); }); } } // namespace impl diff --git a/src/viam/sdk/services/private/navigation_server.cpp b/src/viam/sdk/services/private/navigation_server.cpp index 0bfbe7204..d3764a3a0 100644 --- a/src/viam/sdk/services/private/navigation_server.cpp +++ b/src/viam/sdk/services/private/navigation_server.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include @@ -13,23 +13,31 @@ namespace viam { namespace sdk { -namespace impl { -using namespace service::navigation::v1; +namespace proto_convert_details { -viam::service::navigation::v1::Waypoint to_proto(const Navigation::Waypoint& wp) { - viam::service::navigation::v1::Waypoint ret; - *ret.mutable_id() = wp.id; - *ret.mutable_location() = wp.location.to_proto(); - return ret; -} +template <> +struct to_proto { + void operator()(const Navigation::Path& self, service::navigation::v1::Path* proto) const { + *(proto->mutable_destination_waypoint_id()) = self.destination_waypoint_id; + *(proto->mutable_geopoints()) = impl::to_repeated_field(self.geopoints); + } +}; -viam::service::navigation::v1::Path to_proto(const Navigation::Path& p) { - viam::service::navigation::v1::Path ret; - *ret.mutable_destination_waypoint_id() = p.destination_waypoint_id; - vecToRepeatedPtr(p.geopoints, *ret.mutable_geopoints()); - return ret; -} +template <> +struct to_proto { + void operator()(const Navigation::Waypoint& self, + service::navigation::v1::Waypoint* proto) const { + *(proto->mutable_id()) = self.id; + *(proto->mutable_location()) = v2::to_proto(self.location); + } +}; + +} // namespace proto_convert_details + +namespace impl { + +using namespace service::navigation::v1; ::grpc::Status NavigationServer::GetMode(::grpc::ServerContext*, const GetModeRequest* request, @@ -55,7 +63,7 @@ ::grpc::Status NavigationServer::GetLocation(::grpc::ServerContext*, return make_service_helper( "NavigationServer::GetLocation", this, request)([&](auto& helper, auto& nav) { const auto& loc = nav->get_location(helper.getExtra()); - *response->mutable_location() = loc.location.to_proto(); + *response->mutable_location() = v2::to_proto(loc.location); response->set_compass_heading(loc.compass_heading); }); } @@ -65,8 +73,8 @@ ::grpc::Status NavigationServer::GetWaypoints(::grpc::ServerContext*, GetWaypointsResponse* response) noexcept { return make_service_helper( "NavigationServer::GetWaypoints", this, request)([&](auto& helper, auto& nav) { - const auto waypoints = nav->get_waypoints(helper.getExtra()); - vecToRepeatedPtr(waypoints, *response->mutable_waypoints(), to_proto); + *(response->mutable_waypoints()) = + impl::to_repeated_field(nav->get_waypoints(helper.getExtra())); }); } @@ -75,7 +83,7 @@ ::grpc::Status NavigationServer::AddWaypoint(::grpc::ServerContext*, AddWaypointResponse*) noexcept { return make_service_helper( "NavigationServer::AddWaypoint", this, request)([&](auto& helper, auto& nav) { - nav->add_waypoint(geo_point::from_proto(request->location()), helper.getExtra()); + nav->add_waypoint(v2::from_proto(request->location()), helper.getExtra()); }); } @@ -91,8 +99,8 @@ ::grpc::Status NavigationServer::GetObstacles(::grpc::ServerContext*, GetObstaclesResponse* response) noexcept { return make_service_helper( "NavigationServer::GetObstacles", this, request)([&](auto& helper, auto& nav) { - const auto obstacles = nav->get_obstacles(helper.getExtra()); - vecToRepeatedPtr(obstacles, *response->mutable_obstacles()); + *(response->mutable_obstacles()) = + impl::to_repeated_field(nav->get_obstacles(helper.getExtra())); }); } @@ -101,8 +109,7 @@ ::grpc::Status NavigationServer::GetPaths(::grpc::ServerContext*, GetPathsResponse* response) noexcept { return make_service_helper( "NavigationServer::GetPaths", this, request)([&](auto& helper, auto& nav) { - const auto paths = nav->get_paths(helper.getExtra()); - vecToRepeatedPtr(paths, *response->mutable_paths(), to_proto); + *response->mutable_paths() = impl::to_repeated_field(nav->get_paths(helper.getExtra())); }); } @@ -122,8 +129,8 @@ ::grpc::Status NavigationServer::DoCommand( ::viam::common::v1::DoCommandResponse* response) noexcept { return make_service_helper( "NavigationServer::DoCommand", this, request)([&](auto&, auto& motion) { - const ProtoStruct result = motion->do_command(struct_to_map(request->command())); - *response->mutable_result() = map_to_struct(result); + const ProtoStruct result = motion->do_command(v2::from_proto(request->command())); + *response->mutable_result() = v2::to_proto(result); }); }; diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 4fc999910..5f05b85f3 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -8,211 +8,51 @@ #include #include +#include +#include #include namespace viam { namespace sdk { -viam::common::v1::Sphere GeometryConfig::sphere_proto() const { - try { - viam::common::v1::Sphere sphere; - const auto sphere_specifics = boost::get(geometry_specifics_); - sphere.set_radius_mm(sphere_specifics.radius); - return sphere; - } catch (...) { - throw Exception( - "Couldn't convert geometry config to sphere proto; sphere specifics not found"); - } -} - -viam::common::v1::RectangularPrism GeometryConfig::box_proto() const { - try { - const auto box_specifics = boost::get(geometry_specifics_); - viam::common::v1::RectangularPrism box; - viam::common::v1::Vector3 vec3; - vec3.set_x(box_specifics.x); - vec3.set_y(box_specifics.y); - vec3.set_z(box_specifics.z); - *box.mutable_dims_mm() = vec3; - return box; - } catch (...) { - throw Exception("Couldn't convert geometry config to box proto; box specifics not found"); - } -} - -viam::common::v1::Capsule GeometryConfig::capsule_proto() const { - try { - const auto capsule_specifics = boost::get(geometry_specifics_); - viam::common::v1::Capsule capsule; - capsule.set_radius_mm(capsule_specifics.radius); - capsule.set_length_mm(capsule_specifics.length); - return capsule; - } catch (...) { - throw Exception( - "Couldn't convert geometry config to capsule proto; capsule specifics not found"); - } -} - -viam::common::v1::Pose GeometryConfig::pose_proto() const { - return pose_.to_proto(); -} +GeometryConfig::GeometryConfig(pose p, geometry_specifics gs, Orientation o, std::string label) + : pose_(std::move(p)), + geometry_specifics_(std::move(gs)), + orientation_(std::move(o)), + label_(std::move(label)) {} -pose pose::from_proto(const viam::common::v1::Pose& proto) { - struct pose pose; - pose.coordinates.x = proto.x(); - pose.coordinates.y = proto.y(); - pose.coordinates.z = proto.z(); - pose.orientation.o_x = proto.o_x(); - pose.orientation.o_y = proto.o_y(); - pose.orientation.o_z = proto.o_z(); - pose.theta = proto.theta(); - - return pose; -} +GeometryConfig::GeometryConfig(pose p, geometry_specifics gs, std::string label) + : GeometryConfig(std::move(p), std::move(gs), {}, std::move(label)) {} -viam::common::v1::Pose pose::to_proto() const { - viam::common::v1::Pose proto; - proto.set_x(coordinates.x); - proto.set_y(coordinates.y); - proto.set_z(coordinates.z); - proto.set_o_x(orientation.o_x); - proto.set_o_y(orientation.o_y); - proto.set_o_z(orientation.o_z); - proto.set_theta(theta); - - return proto; +const geometry_specifics& GeometryConfig::get_geometry_specifics() const { + return geometry_specifics_; } -GeometryConfig::GeometryConfig() : geometry_type_(GeometryType::box) {} - -GeometryConfig GeometryConfig::from_proto(const viam::common::v1::Geometry& proto) { - GeometryConfig cfg; - const auto& pose = proto.center(); - cfg.pose_ = pose::from_proto(pose); - cfg.label_ = proto.label(); - - switch (proto.geometry_type_case()) { - case viam::common::v1::Geometry::GeometryTypeCase::kBox: { - cfg.geometry_type_ = GeometryType::box; - struct box box; - box.x = proto.box().dims_mm().x(); - box.y = proto.box().dims_mm().y(); - box.z = proto.box().dims_mm().z(); - cfg.set_geometry_specifics(box); - return cfg; - } - case viam::common::v1::Geometry::GeometryTypeCase::kSphere: { - auto r = proto.sphere().radius_mm(); - if (r == 0) { - cfg.geometry_type_ = GeometryType::point; - } else { - cfg.geometry_type_ = GeometryType::sphere; - } - struct sphere sphere({r}); - cfg.set_geometry_specifics(sphere); - return cfg; +GeometryType GeometryConfig::get_geometry_type() const { + struct Visitor { + auto operator()(const box&) const { + return GeometryType::box; } - case viam::common::v1::Geometry::GeometryTypeCase::kCapsule: { - cfg.geometry_type_ = GeometryType::capsule; - struct capsule capsule; - capsule.radius = proto.capsule().radius_mm(); - capsule.length = proto.capsule().length_mm(); - cfg.set_geometry_specifics(capsule); - return cfg; + auto operator()(const sphere& s) const { + return s.radius == 0.0 ? GeometryType::point : GeometryType::sphere; } - case viam::common::v1::Geometry::GeometryTypeCase::GEOMETRY_TYPE_NOT_SET: - default: { - throw Exception(ErrorCondition::k_not_supported, "Geometry type is not supported"); + auto operator()(const capsule&) const { + return GeometryType::capsule; } - } -} -std::vector GeometryConfig::from_proto( - const viam::common::v1::GetGeometriesResponse& proto) { - std::vector response; - for (const auto& geometry : proto.geometries()) { - response.push_back(from_proto(geometry)); - } - return response; -} + }; -viam::common::v1::Geometry GeometryConfig::to_proto() const { - viam::common::v1::Geometry geometry_; - *geometry_.mutable_label() = label_; - *geometry_.mutable_center() = pose_proto(); - switch (geometry_type_) { - case GeometryType::box: { - *geometry_.mutable_box() = box_proto(); - return geometry_; - } - case GeometryType::sphere: { - *geometry_.mutable_sphere() = sphere_proto(); - return geometry_; - } - case point: { - viam::common::v1::Sphere sphere; - sphere.set_radius_mm(0); - *geometry_.mutable_sphere() = sphere; - return geometry_; - } - case capsule: { - *geometry_.mutable_capsule() = capsule_proto(); - return geometry_; - } - case unknown: - default: { - if (pose_.coordinates.x == 0 && pose_.coordinates.y == 0 && pose_.coordinates.z == 0) { - *geometry_.mutable_box() = box_proto(); - } else { - *geometry_.mutable_sphere() = sphere_proto(); - } - return geometry_; - } - } -} -void GeometryConfig::set_coordinates(coordinates coordinates) { - pose_.coordinates = std::move(coordinates); -} -void GeometryConfig::set_pose(pose pose) { - pose_ = std::move(pose); -} -void GeometryConfig::set_geometry_specifics(geometry_specifics gs) { - geometry_specifics_ = std::move(gs); -} -void GeometryConfig::set_pose_orientation(pose_orientation orientation) { - pose_.orientation = std::move(orientation); -} -void GeometryConfig::set_theta(double theta) { - pose_.theta = std::move(theta); -} -void GeometryConfig::set_geometry_type(GeometryType type) { - geometry_type_ = std::move(type); -} -void GeometryConfig::set_orientation_config(OrientationConfig config) { - orientation_config_ = std::move(config); -} -void GeometryConfig::set_label(std::string label) { - label_ = std::move(label); -} -geometry_specifics GeometryConfig::get_geometry_specifics() const { - return geometry_specifics_; + return boost::apply_visitor(Visitor{}, geometry_specifics_); } -double GeometryConfig::get_theta() const { - return pose_.theta; -} -GeometryType GeometryConfig::get_geometry_type() const { - return geometry_type_; -} -coordinates GeometryConfig::get_coordinates() const { - return pose_.coordinates; -} -pose GeometryConfig::get_pose() const { + +const pose& GeometryConfig::get_pose() const { return pose_; } -OrientationConfig GeometryConfig::get_orientation_config() const { - return orientation_config_; + +const Orientation& GeometryConfig::get_orientation() const { + return orientation_; } -std::string GeometryConfig::get_label() const { +const std::string& GeometryConfig::get_label() const { return label_; } @@ -256,64 +96,123 @@ bool operator==(const struct capsule& lhs, const struct capsule& rhs) { } bool operator==(const GeometryConfig& lhs, const GeometryConfig& rhs) { - const auto& lhs_coordinates = lhs.pose_.coordinates; - const auto& rhs_coordinates = rhs.pose_.coordinates; - const auto& lhs_orientation = lhs.pose_.orientation; - const auto& rhs_orientation = rhs.pose_.orientation; - return lhs_coordinates.x == rhs_coordinates.x && lhs_coordinates.y == rhs_coordinates.y && - lhs_coordinates.z == rhs_coordinates.z && lhs_orientation.o_x == rhs_orientation.o_x && - lhs_orientation.o_y == rhs_orientation.o_y && - lhs_orientation.o_z == rhs_orientation.o_z && lhs.label_ == rhs.label_ && - lhs.geometry_type_ == rhs.geometry_type_ && - lhs.geometry_specifics_ == rhs.geometry_specifics_; + return std::tie( + lhs.pose_.coordinates, lhs.pose_.orientation, lhs.label_, lhs.geometry_specifics_) == + std::tie( + rhs.pose_.coordinates, rhs.pose_.orientation, rhs.label_, rhs.geometry_specifics_); } bool operator==(const geo_point& lhs, const geo_point& rhs) { - return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude; + return std::tie(lhs.latitude, lhs.longitude) == std::tie(rhs.latitude, rhs.longitude); } bool operator==(const geo_geometry& lhs, const geo_geometry& rhs) { - return lhs.location == rhs.location && lhs.geometries == rhs.geometries; + return std::tie(lhs.location, lhs.geometries) == std::tie(rhs.location, rhs.geometries); } -common::v1::GeoPoint geo_point::to_proto() const { - common::v1::GeoPoint proto; - proto.set_latitude(latitude); - proto.set_longitude(longitude); +namespace proto_convert_details { - return proto; +void to_proto::operator()(const box& self, common::v1::RectangularPrism* proto) const { + *(proto->mutable_dims_mm()) = v2::to_proto(Vector3{self.x, self.y, self.z}); } -geo_point geo_point::from_proto(const common::v1::GeoPoint& proto) { - struct geo_point geo_point; - geo_point.latitude = proto.latitude(); - geo_point.longitude = proto.longitude(); +box from_proto::operator()( + const common::v1::RectangularPrism* proto) const { + const auto& dims = proto->dims_mm(); + return {dims.x(), dims.y(), dims.z()}; +} - return geo_point; +void to_proto::operator()(const sphere& self, common::v1::Sphere* proto) const { + proto->set_radius_mm(self.radius); } -common::v1::GeoGeometry geo_geometry::to_proto() const { - common::v1::GeoGeometry proto; - *proto.mutable_location() = location.to_proto(); +sphere from_proto::operator()(const common::v1::Sphere* proto) const { + return {proto->radius_mm()}; +} - for (const auto& geometry : geometries) { - *proto.mutable_geometries()->Add() = geometry.to_proto(); - } +void to_proto::operator()(const capsule& self, common::v1::Capsule* proto) const { + proto->set_radius_mm(self.radius); + proto->set_length_mm(self.length); +} - return proto; +capsule from_proto::operator()(const common::v1::Capsule* proto) const { + return {proto->radius_mm(), proto->length_mm()}; } -geo_geometry geo_geometry::from_proto(const common::v1::GeoGeometry& proto) { - struct geo_geometry geo_geometry; +void to_proto::operator()(const GeometryConfig& self, + common::v1::Geometry* proto) const { + struct Visitor { + common::v1::Geometry& geometry; - geo_geometry.location = geo_point::from_proto(proto.location()); - for (const auto& proto_geometry : proto.geometries()) { - auto geometry = GeometryConfig::from_proto(proto_geometry); - geo_geometry.geometries.push_back(std::move(geometry)); - } + void operator()(const box& b) { + *geometry.mutable_box() = v2::to_proto(b); + } - return geo_geometry; + void operator()(const sphere& s) { + *geometry.mutable_sphere() = v2::to_proto(s); + } + + void operator()(const capsule& c) { + *geometry.mutable_capsule() = v2::to_proto(c); + } + }; + + boost::apply_visitor(Visitor{*proto}, self.get_geometry_specifics()); + + *(proto->mutable_label()) = self.get_label(); + *(proto->mutable_center()) = v2::to_proto(self.get_pose()); +} + +GeometryConfig from_proto::operator()( + const common::v1::Geometry* proto) const { + auto get_specifics = [proto]() -> geometry_specifics { + switch (proto->geometry_type_case()) { + case viam::common::v1::Geometry::GeometryTypeCase::kBox: + return v2::from_proto(proto->box()); + case viam::common::v1::Geometry::GeometryTypeCase::kSphere: + return v2::from_proto(proto->sphere()); + case viam::common::v1::Geometry::GeometryTypeCase::kCapsule: + return v2::from_proto(proto->capsule()); + case viam::common::v1::Geometry::GeometryTypeCase:: + GEOMETRY_TYPE_NOT_SET: // fallthrough + default: + throw Exception(ErrorCondition::k_not_supported, "Geometry type is not supported"); + } + }; + + return GeometryConfig(v2::from_proto(proto->center()), get_specifics(), proto->label()); +} + +void to_proto::operator()(const geo_point& self, common::v1::GeoPoint* proto) const { + proto->set_latitude(self.latitude); + proto->set_longitude(self.longitude); } +geo_point from_proto::operator()(const common::v1::GeoPoint* proto) const { + geo_point result; + result.latitude = proto->latitude(); + result.longitude = proto->longitude(); + + return result; +} + +void to_proto::operator()(const geo_geometry& self, + common::v1::GeoGeometry* proto) const { + *(proto->mutable_location()) = v2::to_proto(self.location); + *(proto->mutable_geometries()) = impl::to_repeated_field(self.geometries); +} + +geo_geometry from_proto::operator()( + const common::v1::GeoGeometry* proto) const { + return {v2::from_proto(proto->location()), impl::from_repeated_field(proto->geometries())}; +} + +std::vector from_proto::operator()( + const common::v1::GetGeometriesResponse* proto) const { + return impl::from_repeated_field(proto->geometries()); +} + +} // namespace proto_convert_details + } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/spatialmath/geometry.hpp b/src/viam/sdk/spatialmath/geometry.hpp index 0d0dff78b..2935676d3 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -4,22 +4,34 @@ #include #include -#include - #include +#include #include +namespace viam { +namespace common { +namespace v1 { + +class RectangularPrism; +class Sphere; +class Capsule; + +class Geometry; +class GetGeometriesResponse; + +class GeoPoint; +class GeoGeometry; + +} // namespace v1 +} // namespace common +} // namespace viam + namespace viam { namespace sdk { // TODO(RSDK-4553): add thorough documentation to this whole file. -enum GeometryType { - box, - sphere, - capsule, - point, - unknown, -}; + +enum class GeometryType { box, sphere, capsule, point }; struct box { double x; @@ -37,51 +49,38 @@ struct capsule { friend bool operator==(const capsule& lhs, const capsule& rhs); }; -typedef boost::variant geometry_specifics; +typedef boost::variant geometry_specifics; class GeometryConfig { public: - viam::common::v1::Geometry to_proto() const; - viam::common::v1::RectangularPrism box_proto() const; - viam::common::v1::Sphere sphere_proto() const; - viam::common::v1::Capsule capsule_proto() const; - viam::common::v1::Pose pose_proto() const; - static GeometryConfig from_proto(const viam::common::v1::Geometry& proto); - static std::vector from_proto( - const viam::common::v1::GetGeometriesResponse& proto); - void set_coordinates(coordinates coordinates); - void set_pose(pose pose); - void set_pose_orientation(pose_orientation orientation); - void set_theta(double theta); - void set_geometry_specifics(geometry_specifics gs); - void set_geometry_type(GeometryType type); - void set_orientation_config(OrientationConfig config); - void set_label(std::string label); + GeometryConfig() = default; + + GeometryConfig(pose, geometry_specifics, Orientation, std::string label); + GeometryConfig(pose, geometry_specifics, std::string label); + double get_theta() const; - coordinates get_coordinates() const; - pose get_pose() const; - geometry_specifics get_geometry_specifics() const; + const pose& get_pose() const; + const geometry_specifics& get_geometry_specifics() const; GeometryType get_geometry_type() const; - OrientationConfig get_orientation_config() const; - std::string get_label() const; + const Orientation& get_orientation() const; + const std::string& get_label() const; + friend bool operator==(const GeometryConfig& lhs, const GeometryConfig& rhs); - GeometryConfig(); private: - GeometryType geometry_type_; pose pose_; geometry_specifics geometry_specifics_; // TODO: if and when RDK makes more explicit use of ox/oy/oz, we should // do the same here - OrientationConfig orientation_config_; + Orientation orientation_; std::string label_; }; struct geo_point { + // TODO it really bugs me that this is not in lat-long but this would break existing + // aggregate initializers double longitude, latitude; - common::v1::GeoPoint to_proto() const; - static geo_point from_proto(const common::v1::GeoPoint& proto); friend bool operator==(const geo_point& lhs, const geo_point& rhs); friend std::ostream& operator<<(std::ostream& os, const geo_point& v); }; @@ -90,10 +89,77 @@ struct geo_geometry { geo_point location; std::vector geometries; - common::v1::GeoGeometry to_proto() const; - static geo_geometry from_proto(const common::v1::GeoGeometry& proto); friend bool operator==(const geo_geometry& lhs, const geo_geometry& rhs); }; +namespace proto_convert_details { + +template <> +struct to_proto { + void operator()(const box&, common::v1::RectangularPrism*) const; +}; + +template <> +struct from_proto { + box operator()(const common::v1::RectangularPrism*) const; +}; + +template <> +struct to_proto { + void operator()(const sphere&, common::v1::Sphere*) const; +}; + +template <> +struct from_proto { + sphere operator()(const common::v1::Sphere*) const; +}; + +template <> +struct to_proto { + void operator()(const capsule&, common::v1::Capsule*) const; +}; + +template <> +struct from_proto { + capsule operator()(const common::v1::Capsule*) const; +}; + +template <> +struct to_proto { + void operator()(const GeometryConfig&, common::v1::Geometry*) const; +}; + +template <> +struct from_proto { + GeometryConfig operator()(const common::v1::Geometry*) const; +}; + +template <> +struct to_proto { + void operator()(const geo_point&, common::v1::GeoPoint*) const; +}; + +template <> +struct from_proto { + geo_point operator()(const common::v1::GeoPoint*) const; +}; + +template <> +struct to_proto { + void operator()(const geo_geometry&, common::v1::GeoGeometry*) const; +}; + +template <> +struct from_proto { + geo_geometry operator()(const common::v1::GeoGeometry*) const; +}; + +template <> +struct from_proto { + std::vector operator()(const common::v1::GetGeometriesResponse*) const; +}; + +} // namespace proto_convert_details + } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/spatialmath/orientation.cpp b/src/viam/sdk/spatialmath/orientation.cpp index 2de5a6802..12da26d35 100644 --- a/src/viam/sdk/spatialmath/orientation.cpp +++ b/src/viam/sdk/spatialmath/orientation.cpp @@ -3,8 +3,7 @@ #include #include -#include -#include +#include #include @@ -14,158 +13,144 @@ namespace viam { namespace sdk { -namespace proto = viam::app::v1; +OrientationType get_type(const Orientation& o) { + struct Visitor { + auto operator()(const quaternion&) const { + return OrientationType::Quaternion; + } -OrientationConfig::OrientationConfig() { - type_ = Quaternion; - quaternion quat; - quat.w = 1; - quat.x = 0; - quat.y = 0; - quat.z = 0; - orientation_ = quat; -} + auto operator()(const axis_angles&) const { + return OrientationType::AxisAngles; + } -OrientationConfig OrientationConfig::from_proto(const proto::Orientation& proto) { - OrientationConfig cfg; - switch (proto.type_case()) { - case proto::Orientation::TypeCase::kAxisAngles: { - cfg.type_ = AxisAngles; - axis_angles aa; - aa.x = proto.axis_angles().x(); - aa.y = proto.axis_angles().y(); - aa.z = proto.axis_angles().z(); - aa.theta = proto.axis_angles().theta(); - cfg.orientation_ = aa; - break; - } - case proto::Orientation::TypeCase::kEulerAngles: { - cfg.type_ = EulerAngles; - euler_angles ea; - ea.yaw = proto.euler_angles().yaw(); - ea.pitch = proto.euler_angles().pitch(); - ea.roll = proto.euler_angles().roll(); - cfg.orientation_ = ea; - break; - } - case proto::Orientation::TypeCase::kQuaternion: { - cfg.type_ = Quaternion; - quaternion quat; - quat.w = proto.quaternion().w(); - quat.x = proto.quaternion().x(); - quat.y = proto.quaternion().y(); - quat.z = proto.quaternion().z(); - cfg.orientation_ = quat; - break; - } - case proto::Orientation::TypeCase::kVectorDegrees: { - cfg.type_ = OrientationVectorDegrees; - orientation_vector_degrees ovd; - ovd.x = proto.vector_degrees().x(); - ovd.y = proto.vector_degrees().y(); - ovd.z = proto.vector_degrees().z(); - ovd.theta = proto.vector_degrees().theta(); - cfg.orientation_ = ovd; - break; - } - case proto::Orientation::TypeCase::kVectorRadians: { - cfg.type_ = OrientationVector; - orientation_vector ov; - ov.x = proto.vector_radians().x(); - ov.y = proto.vector_radians().y(); - ov.z = proto.vector_radians().z(); - ov.theta = proto.vector_radians().theta(); - cfg.orientation_ = ov; - break; - } - case proto::Orientation::TypeCase::kNoOrientation: { - // if type is NoOrientation, we put a sentinel - // orientation that indicates no rotation - cfg.type_ = Quaternion; - quaternion quat; - quat.w = 1; - quat.x = 0; - quat.y = 0; - quat.z = 0; - cfg.orientation_ = quat; - break; + auto operator()(const orientation_vector&) const { + return OrientationType::OrientationVector; } - case proto::Orientation::TypeCase::TYPE_NOT_SET: - default: { - throw Exception(ErrorCondition::k_not_supported, "orientation type not known"); + + auto operator()(const orientation_vector_degrees&) const { + return OrientationType::OrientationVectorDegrees; } - } - return cfg; + + auto operator()(const euler_angles&) const { + return OrientationType::EulerAngles; + } + }; + + return boost::apply_visitor(Visitor{}, o); } -proto::Orientation OrientationConfig::to_proto() const { - proto::Orientation orientation; - switch (type_) { - case AxisAngles: { - proto::Orientation_AxisAngles aa; - const axis_angles a = boost::get(orientation_); +namespace proto_convert_details { + +void to_proto::operator()(const Orientation& self, app::v1::Orientation* o) const { + struct Visitor { + void operator()(const axis_angles& a) { + app::v1::Orientation_AxisAngles aa; aa.set_x(a.x); aa.set_y(a.y); aa.set_z(a.z); aa.set_theta(a.theta); *orientation.mutable_axis_angles() = std::move(aa); - return orientation; - }; - case OrientationVector: { - proto::Orientation_OrientationVectorRadians ovec; - const orientation_vector ov = boost::get(orientation_); + } + + void operator()(const orientation_vector& ov) { + app::v1::Orientation_OrientationVectorRadians ovec; ovec.set_x(ov.x); ovec.set_y(ov.y); ovec.set_z(ov.z); ovec.set_theta(ov.theta); *orientation.mutable_vector_radians() = std::move(ovec); - return orientation; - }; - case OrientationVectorDegrees: { - proto::Orientation_OrientationVectorDegrees ovec; - const orientation_vector_degrees ovd = - boost::get(orientation_); + } + + void operator()(const orientation_vector_degrees& ovd) { + app::v1::Orientation_OrientationVectorDegrees ovec; ovec.set_x(ovd.x); ovec.set_y(ovd.y); ovec.set_z(ovd.z); ovec.set_theta(ovd.theta); *orientation.mutable_vector_degrees() = std::move(ovec); - return orientation; - }; - case EulerAngles: { - proto::Orientation_EulerAngles euler; - const euler_angles ea = boost::get(orientation_); + } + + void operator()(const euler_angles& ea) { + app::v1::Orientation_EulerAngles euler; euler.set_pitch(ea.pitch); euler.set_roll(ea.roll); euler.set_yaw(ea.yaw); *orientation.mutable_euler_angles() = std::move(euler); - return orientation; - }; - case Quaternion: { - proto::Orientation_Quaternion quat; - const quaternion q = boost::get(orientation_); + } + + void operator()(const quaternion& q) { + app::v1::Orientation_Quaternion quat; quat.set_w(q.w); quat.set_x(q.x); quat.set_y(q.y); quat.set_z(q.z); *orientation.mutable_quaternion() = std::move(quat); - return orientation; - }; + } + + app::v1::Orientation& orientation; + }; + + boost::apply_visitor(Visitor{*o}, self); +} + +Orientation from_proto::operator()(const app::v1::Orientation* proto) const { + switch (proto->type_case()) { + case app::v1::Orientation::TypeCase::kAxisAngles: { + axis_angles aa; + aa.x = proto->axis_angles().x(); + aa.y = proto->axis_angles().y(); + aa.z = proto->axis_angles().z(); + aa.theta = proto->axis_angles().theta(); + return aa; + } + case app::v1::Orientation::TypeCase::kEulerAngles: { + euler_angles ea; + ea.yaw = proto->euler_angles().yaw(); + ea.pitch = proto->euler_angles().pitch(); + ea.roll = proto->euler_angles().roll(); + return ea; + } + case app::v1::Orientation::TypeCase::kQuaternion: { + quaternion quat; + quat.w = proto->quaternion().w(); + quat.x = proto->quaternion().x(); + quat.y = proto->quaternion().y(); + quat.z = proto->quaternion().z(); + return quat; + } + case app::v1::Orientation::TypeCase::kVectorDegrees: { + orientation_vector_degrees ovd; + ovd.x = proto->vector_degrees().x(); + ovd.y = proto->vector_degrees().y(); + ovd.z = proto->vector_degrees().z(); + ovd.theta = proto->vector_degrees().theta(); + return ovd; + } + case app::v1::Orientation::TypeCase::kVectorRadians: { + orientation_vector ov; + ov.x = proto->vector_radians().x(); + ov.y = proto->vector_radians().y(); + ov.z = proto->vector_radians().z(); + ov.theta = proto->vector_radians().theta(); + return ov; + } + case app::v1::Orientation::TypeCase::kNoOrientation: { + // if type is NoOrientation, we put a sentinel + // orientation that indicates no rotation + quaternion quat; + quat.w = 1; + quat.x = 0; + quat.y = 0; + quat.z = 0; + return quat; + } + case app::v1::Orientation::TypeCase::TYPE_NOT_SET: default: { throw Exception(ErrorCondition::k_not_supported, "orientation type not known"); } } } -OrientationType OrientationConfig::get_type() const { - return type_; -}; -const std::vector& OrientationConfig::get_value() const { - return value_; -}; -const orientation& OrientationConfig::get_orientation() const { - return orientation_; -} - +} // namespace proto_convert_details } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/spatialmath/orientation.hpp b/src/viam/sdk/spatialmath/orientation.hpp index 6d77980e3..1ce7dca17 100644 --- a/src/viam/sdk/spatialmath/orientation.hpp +++ b/src/viam/sdk/spatialmath/orientation.hpp @@ -5,37 +5,38 @@ #include -#include - +#include #include +namespace viam { +namespace app { +namespace v1 { + +class Orientation; +} +} // namespace app +} // namespace viam + namespace viam { namespace sdk { -typedef boost:: - variant - orientation; - -class OrientationConfig { - public: - viam::app::v1::Orientation to_proto() const; - static OrientationConfig from_proto(const viam::app::v1::Orientation& proto); - OrientationConfig(OrientationType type_, - std::vector value, - orientation orientation) - : type_(std::move(type_)), value_(std::move(value)), orientation_(std::move(orientation)) {} - // Defaults to sentinel no rotation value - OrientationConfig(); - - OrientationType get_type() const; - const std::vector& get_value() const; - const orientation& get_orientation() const; - - private: - OrientationType type_; - std::vector value_; - orientation orientation_; +// Note that quaternion must be the first type because this is what is default constructed. +using Orientation = boost:: + variant; + +OrientationType get_type(const Orientation&); + +namespace proto_convert_details { + +template <> +struct to_proto { + void operator()(const Orientation&, app::v1::Orientation*) const; +}; +template <> +struct from_proto { + Orientation operator()(const app::v1::Orientation*) const; }; +} // namespace proto_convert_details } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/spatialmath/orientation_types.cpp b/src/viam/sdk/spatialmath/orientation_types.cpp index 454154ef3..1b981bc8c 100644 --- a/src/viam/sdk/spatialmath/orientation_types.cpp +++ b/src/viam/sdk/spatialmath/orientation_types.cpp @@ -5,13 +5,18 @@ namespace viam { namespace sdk { -viam::app::v1::Translation translation::to_proto() const { - viam::app::v1::Translation t; - t.set_x(x); - t.set_y(y); - t.set_z(z); - return t; +namespace proto_convert_details { + +void to_proto::operator()(const translation& self, app::v1::Translation* t) const { + t->set_x(self.x); + t->set_y(self.y); + t->set_z(self.z); +} + +translation from_proto::operator()(const app::v1::Translation* proto) const { + return {proto->x(), proto->y(), proto->z()}; } +} // namespace proto_convert_details } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/spatialmath/orientation_types.hpp b/src/viam/sdk/spatialmath/orientation_types.hpp index bcd077f6e..2cde64cfb 100644 --- a/src/viam/sdk/spatialmath/orientation_types.hpp +++ b/src/viam/sdk/spatialmath/orientation_types.hpp @@ -1,6 +1,15 @@ #pragma once -#include +#include + +namespace viam { +namespace app { +namespace v1 { + +class Translation; +} +} // namespace app +} // namespace viam namespace viam { namespace sdk { @@ -35,7 +44,20 @@ struct quaternion { struct translation { double x, y, z; - viam::app::v1::Translation to_proto() const; }; + +namespace proto_convert_details { + +template <> +struct to_proto { + void operator()(const translation&, app::v1::Translation*) const; +}; + +template <> +struct from_proto { + translation operator()(const app::v1::Translation*) const; +}; + +} // namespace proto_convert_details } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/tests/mocks/mock_motion.cpp b/src/viam/sdk/tests/mocks/mock_motion.cpp index 8279409ce..413d3cdb6 100644 --- a/src/viam/sdk/tests/mocks/mock_motion.cpp +++ b/src/viam/sdk/tests/mocks/mock_motion.cpp @@ -170,24 +170,13 @@ std::shared_ptr fake_motion_configuration() { } std::vector fake_obstacles() { - GeometryConfig gc; - gc.set_pose({{20, 25, 30}, {35, 40, 45}, 50}); - gc.set_geometry_type(sdk::sphere); - struct sphere sphere({1}); - gc.set_geometry_specifics(sphere); - gc.set_label("label"); - gc.set_orientation_config({AxisAngles, {}, axis_angles{1, 2, 3, 4}}); + GeometryConfig gc( + {{20, 25, 30}, {35, 40, 45}, 50}, sphere{1}, axis_angles{1, 2, 3, 4}, "label"); return {{fake_geo_point(), {std::move(gc)}}}; } std::vector fake_bounding_regions() { - GeometryConfig gc; - gc.set_pose({{1, 2, 3}, {0, 0, 0}, 90}); - gc.set_geometry_type(sdk::sphere); - struct sphere sphere({2}); - gc.set_geometry_specifics(sphere); - gc.set_label("label"); - gc.set_orientation_config({AxisAngles, {}, axis_angles{1, 2, 3, 4}}); + GeometryConfig gc({{1, 2, 3}, {0, 0, 0}, 90}, sphere{2}, axis_angles{1, 2, 3, 4}, "label"); return {{fake_geo_point(), {std::move(gc)}}}; } diff --git a/src/viam/sdk/tests/mocks/mock_robot.cpp b/src/viam/sdk/tests/mocks/mock_robot.cpp index 11eae813b..7f9e23a13 100644 --- a/src/viam/sdk/tests/mocks/mock_robot.cpp +++ b/src/viam/sdk/tests/mocks/mock_robot.cpp @@ -35,7 +35,7 @@ pose default_pose(int offset) { } Pose default_proto_pose(int offset = 0) { - return default_pose(offset).to_proto(); + return v2::to_proto(default_pose(offset)); } std::vector mock_operations_response() { @@ -95,7 +95,7 @@ std::vector mock_proto_discovery_response() { viam::robot::v1::Discovery discovery; *discovery.mutable_query() = query; - *discovery.mutable_results() = map_to_struct(d.results); + *discovery.mutable_results() = v2::to_proto(d.results); v.push_back(std::move(discovery)); } diff --git a/src/viam/sdk/tests/test_movement_sensor.cpp b/src/viam/sdk/tests/test_movement_sensor.cpp index 3de2a83dc..fda65ba8b 100644 --- a/src/viam/sdk/tests/test_movement_sensor.cpp +++ b/src/viam/sdk/tests/test_movement_sensor.cpp @@ -36,16 +36,16 @@ BOOST_AUTO_TEST_CASE(mock_get_api) { BOOST_AUTO_TEST_CASE(test_linear_vel) { std::shared_ptr mock = MockMovementSensor::get_mock_movementsensor(); client_to_mock_pipeline(mock, [&](MovementSensor& client) { - mock->peek_return_vec = Vector3(1, 2, 3); - BOOST_CHECK(client.get_linear_velocity().data() == mock->peek_return_vec.data()); + mock->peek_return_vec = Vector3{1, 2, 3}; + BOOST_CHECK(client.get_linear_velocity().data == mock->peek_return_vec.data); }); } BOOST_AUTO_TEST_CASE(test_angular_vel) { std::shared_ptr mock = MockMovementSensor::get_mock_movementsensor(); client_to_mock_pipeline(mock, [&](MovementSensor& client) { - mock->peek_return_vec = Vector3(1, 2, -3); - BOOST_CHECK(client.get_angular_velocity().data() == mock->peek_return_vec.data()); + mock->peek_return_vec = Vector3{1, 2, -3}; + BOOST_CHECK(client.get_angular_velocity().data == mock->peek_return_vec.data); }); } @@ -117,8 +117,8 @@ BOOST_AUTO_TEST_CASE(test_accuracy) { BOOST_AUTO_TEST_CASE(test_linear_accel) { std::shared_ptr mock = MockMovementSensor::get_mock_movementsensor(); client_to_mock_pipeline(mock, [&](MovementSensor& client) { - mock->peek_return_vec = Vector3(-1, 2.1, 3); - BOOST_CHECK(client.get_linear_acceleration().data() == mock->peek_return_vec.data()); + mock->peek_return_vec = Vector3{-1, 2.1, 3}; + BOOST_CHECK(client.get_linear_acceleration().data == mock->peek_return_vec.data); }); } diff --git a/src/viam/sdk/tests/test_proto_value.cpp b/src/viam/sdk/tests/test_proto_value.cpp index 169110e8c..320002854 100644 --- a/src/viam/sdk/tests/test_proto_value.cpp +++ b/src/viam/sdk/tests/test_proto_value.cpp @@ -91,9 +91,9 @@ BOOST_AUTO_TEST_CASE(test_object_equality) { BOOST_CHECK(v1_copy_to_change == v3); } - Value converted = to_proto(v3); - auto roundtrip = ProtoValue::from_proto(converted); - Value value_roundtrip = to_proto(roundtrip); + Value converted = v2::to_proto(v3); + auto roundtrip = v2::from_proto(converted); + Value value_roundtrip = v2::to_proto(roundtrip); BOOST_CHECK(v3.kind() == roundtrip.kind()); @@ -215,9 +215,9 @@ BOOST_AUTO_TEST_CASE(test_nested_objects) { ProtoValue map_proto(map); - Value val = to_proto(map_proto); - auto roundtrip = ProtoValue::from_proto(val); - Value val2 = to_proto(roundtrip); + Value val = v2::to_proto(map_proto); + auto roundtrip = v2::from_proto(val); + Value val2 = v2::to_proto(roundtrip); BOOST_CHECK(map_proto == roundtrip); @@ -243,13 +243,13 @@ BOOST_AUTO_TEST_CASE(test_manual_list_conversion) { Value protoval; set_proto_value(protoval, test_val); - auto from_value = ProtoValue::from_proto(protoval); + auto from_value = v2::from_proto(protoval); BOOST_CHECK(val == from_value); - Value converted_to_value = to_proto(val); + Value converted_to_value = v2::to_proto(val); BOOST_CHECK(protoval.ShortDebugString() == converted_to_value.ShortDebugString()); - auto roundtrip = ProtoValue::from_proto(converted_to_value); + auto roundtrip = v2::from_proto(converted_to_value); BOOST_CHECK(val == roundtrip); }); @@ -286,10 +286,10 @@ BOOST_AUTO_TEST_CASE(test_manual_list_conversion) { Value v; *v.mutable_list_value() = lv; - Value value_from_proto = to_proto(proto); + Value value_from_proto = v2::to_proto(proto); BOOST_CHECK(v.ShortDebugString() == value_from_proto.ShortDebugString()); - auto roundtrip = ProtoValue::from_proto(value_from_proto); + auto roundtrip = v2::from_proto(value_from_proto); BOOST_CHECK(proto == roundtrip); } @@ -314,7 +314,7 @@ BOOST_AUTO_TEST_CASE(test_manual_map_conversion) { Value v; *v.mutable_struct_value() = proto_struct; - auto from_proto = ProtoValue::from_proto(v); + auto from_proto = v2::from_proto(v); ProtoValue from_map(m); BOOST_CHECK(from_proto == from_map); diff --git a/src/viam/sdk/tests/test_resource.cpp b/src/viam/sdk/tests/test_resource.cpp index 39cdedb5e..db950355a 100644 --- a/src/viam/sdk/tests/test_resource.cpp +++ b/src/viam/sdk/tests/test_resource.cpp @@ -12,6 +12,8 @@ #include #include +BOOST_TEST_DONT_PRINT_LOG_VALUE(viam::sdk::GeometryType); + namespace viam { namespace sdktests { @@ -132,21 +134,24 @@ BOOST_AUTO_TEST_CASE(test_linkconfig) { *frame.mutable_orientation() = o; *frame.mutable_translation() = t; - LinkConfig lc = LinkConfig::from_proto(frame); + LinkConfig lc = v2::from_proto(frame); BOOST_CHECK_EQUAL(lc.get_parent(), "parent"); BOOST_CHECK_EQUAL(lc.get_translation().x, t.x()); BOOST_CHECK_EQUAL(lc.get_translation().y, t.y()); BOOST_CHECK_EQUAL(lc.get_translation().z, t.z()); GeometryConfig gcfg = lc.get_geometry_config(); BOOST_CHECK_EQUAL(gcfg.get_label(), "label"); - BOOST_CHECK_EQUAL(gcfg.get_pose(), pose::from_proto(pose)); + BOOST_CHECK_EQUAL(gcfg.get_pose(), v2::from_proto(pose)); BOOST_CHECK_EQUAL(gcfg.get_geometry_type(), GeometryType::box); - const auto gs = gcfg.box_proto(); - BOOST_CHECK_EQUAL(gs.dims_mm().x(), box.dims_mm().x()); - BOOST_CHECK_EQUAL(gs.dims_mm().y(), box.dims_mm().y()); - BOOST_CHECK_EQUAL(gs.dims_mm().z(), box.dims_mm().z()); - viam::app::v1::Frame proto_lc = lc.to_proto(); + common::v1::Geometry gs = v2::to_proto(gcfg); + BOOST_ASSERT(gs.has_box()); + + BOOST_CHECK_EQUAL(gs.box().dims_mm().x(), box.dims_mm().x()); + BOOST_CHECK_EQUAL(gs.box().dims_mm().y(), box.dims_mm().y()); + BOOST_CHECK_EQUAL(gs.box().dims_mm().z(), box.dims_mm().z()); + + viam::app::v1::Frame proto_lc = v2::to_proto(lc); BOOST_CHECK_EQUAL(proto_lc.parent(), "parent"); BOOST_CHECK_EQUAL(proto_lc.translation().x(), t.x()); BOOST_CHECK_EQUAL(proto_lc.translation().y(), t.y()); @@ -241,7 +246,7 @@ BOOST_AUTO_TEST_CASE(test_resource) { Value value; for (const auto& key_and_value : resource2.attributes()) { key = key_and_value.first; - value = to_proto(key_and_value.second); + value = v2::to_proto(key_and_value.second); } BOOST_CHECK_EQUAL(key, "a"); BOOST_CHECK_EQUAL(value.number_value(), 1); diff --git a/src/viam/sdk/tests/test_robot.cpp b/src/viam/sdk/tests/test_robot.cpp index a7658b3f5..605e5f5bf 100644 --- a/src/viam/sdk/tests/test_robot.cpp +++ b/src/viam/sdk/tests/test_robot.cpp @@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(test_frame_system_config) { proto1.frame().pose_in_observer_frame().pose().o_x()); BOOST_CHECK_EQUAL(config1.frame.pose_in_observer_frame.pose.theta, proto1.frame().pose_in_observer_frame().pose().theta()); - BOOST_CHECK_EQUAL(map_to_struct(config1.kinematics).SerializeAsString(), + BOOST_CHECK_EQUAL(v2::to_proto(config1.kinematics).SerializeAsString(), proto1.kinematics().SerializeAsString()); BOOST_CHECK_EQUAL(config2.frame.reference_frame, proto2.frame().reference_frame()); @@ -162,7 +162,7 @@ BOOST_AUTO_TEST_CASE(test_frame_system_config) { proto2.frame().pose_in_observer_frame().pose().o_x()); BOOST_CHECK_EQUAL(config2.frame.pose_in_observer_frame.pose.theta, proto2.frame().pose_in_observer_frame().pose().theta()); - BOOST_CHECK_EQUAL(map_to_struct(config2.kinematics).SerializeAsString(), + BOOST_CHECK_EQUAL(v2::to_proto(config2.kinematics).SerializeAsString(), proto2.kinematics().SerializeAsString()); }); } @@ -227,7 +227,7 @@ BOOST_AUTO_TEST_CASE(test_discovery) { // test `ProtoValue` conversions. Unfortunately the protobuf `ListValue` type doesn't // seem to have `==` defined, so we convert to a `DebugString` here to verify // comparison and to provide helpful printing of differences in case of an error. - BOOST_CHECK_EQUAL(to_proto(results->second).DebugString(), + BOOST_CHECK_EQUAL(v2::to_proto(results->second).DebugString(), proto_results->second.DebugString()); }); } diff --git a/src/viam/sdk/tests/test_utils.cpp b/src/viam/sdk/tests/test_utils.cpp index 4ffb969c3..703a7c52f 100644 --- a/src/viam/sdk/tests/test_utils.cpp +++ b/src/viam/sdk/tests/test_utils.cpp @@ -19,42 +19,11 @@ ProtoStruct fake_map() { } std::vector fake_geometries() { - GeometryConfig sphere_config; - sphere_config.set_geometry_type(GeometryType::sphere); - sphere_config.set_coordinates({1, 2, 3}); - sphere_config.set_pose({1, 2, 3}); - struct sphere sphere({1}); - sphere_config.set_geometry_specifics(std::move(sphere)); - sphere_config.set_label("sphere"); - - GeometryConfig box_config; - box_config.set_geometry_type(GeometryType::box); - box_config.set_coordinates({1, 2, 3}); - box_config.set_pose({1, 2, 3}); - struct box box({1, 2, 3}); - box_config.set_geometry_specifics(std::move(box)); - box_config.set_label("box"); - - GeometryConfig point_config; - point_config.set_geometry_type(GeometryType::point); - point_config.set_coordinates({1, 2, 3}); - point_config.set_pose({1, 2, 3}); - struct sphere point({0}); - point_config.set_geometry_specifics(std::move(point)); - point_config.set_label("point"); - - GeometryConfig capsule_config; - capsule_config.set_geometry_type(GeometryType::capsule); - capsule_config.set_coordinates({1, 2, 3}); - capsule_config.set_pose({1, 2, 3}); - struct capsule capsule({2, 4}); - capsule_config.set_geometry_specifics(std::move(capsule)); - capsule_config.set_label("capsule"); - - return {std::move(sphere_config), - std::move(box_config), - std::move(point_config), - std::move(capsule_config)}; + pose p{1, 2, 3}; + return {GeometryConfig(p, sphere{1}, "sphere"), + GeometryConfig(p, box{1, 2, 3}, "box"), + GeometryConfig(p, sphere{0}, "point"), + GeometryConfig(p, capsule{2, 4}, "capsule")}; } TestServer::TestServer(std::shared_ptr sdk_server) : sdk_server_(std::move(sdk_server)) {}