From 507ab5711f37bceb9720d5ff375517f16e26c949 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:16:46 -0500 Subject: [PATCH 01/29] first commit of generic proto conversion machinery --- src/viam/sdk/common/proto_convert.hpp | 80 +++++++++++++++++++ .../sdk/services/private/motion_server.cpp | 18 ++--- 2 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 src/viam/sdk/common/proto_convert.hpp diff --git a/src/viam/sdk/common/proto_convert.hpp b/src/viam/sdk/common/proto_convert.hpp new file mode 100644 index 000000000..08e571047 --- /dev/null +++ b/src/viam/sdk/common/proto_convert.hpp @@ -0,0 +1,80 @@ +#pragma once + +#include +#include +#include + +#include + +namespace viam { +namespace sdk { + +namespace proto_convert_details { + +// 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; + +template +using ProtoArgType = + std::remove_pointer_t>>; + +// 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 { + return from_proto{}(&proto); + } +}; + +} // namespace proto_convert_details + +namespace v2 { + +/// @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 proto_convert_details::to_proto_impl to_proto{}; + +/// @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 proto_convert_details::from_proto_impl from_proto{}; + +} // 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/services/private/motion_server.cpp b/src/viam/sdk/services/private/motion_server.cpp index 997bc1a67..79d2dbb9b 100644 --- a/src/viam/sdk/services/private/motion_server.cpp +++ b/src/viam/sdk/services/private/motion_server.cpp @@ -51,7 +51,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}); } @@ -107,23 +107,23 @@ motion_configuration from_proto(const service::motion::v1::MotionConfiguration& } if (proto.has_position_polling_frequency_hz()) { - *mc.position_polling_frequency_hz = proto.position_polling_frequency_hz(); + mc.position_polling_frequency_hz = proto.position_polling_frequency_hz(); } if (proto.has_obstacle_polling_frequency_hz()) { - *mc.obstacle_polling_frequency_hz = proto.obstacle_polling_frequency_hz(); + mc.obstacle_polling_frequency_hz = proto.obstacle_polling_frequency_hz(); } if (proto.has_plan_deviation_m()) { - *mc.plan_deviation_m = proto.plan_deviation_m(); + mc.plan_deviation_m = proto.plan_deviation_m(); } if (proto.has_linear_m_per_sec()) { - *mc.linear_m_per_sec = proto.linear_m_per_sec(); + mc.linear_m_per_sec = proto.linear_m_per_sec(); } if (proto.has_angular_degs_per_sec()) { - *mc.angular_degs_per_sec = proto.angular_degs_per_sec(); + mc.angular_degs_per_sec = proto.angular_degs_per_sec(); } return mc; @@ -185,7 +185,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 +200,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()); @@ -280,7 +280,7 @@ ::grpc::Status MotionServer::GetPose( } 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); }); }; From 9ff1696f117b7ecd9834c945b681b95acc2b4417 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:17:37 -0500 Subject: [PATCH 02/29] insulate pose and pose in frame --- src/viam/sdk/common/pose.cpp | 73 +++++++++++-------- src/viam/sdk/common/pose.hpp | 43 +++++++++-- src/viam/sdk/common/world_state.cpp | 4 +- .../sdk/components/private/arm_client.cpp | 4 +- .../sdk/components/private/arm_server.cpp | 4 +- .../private/pose_tracker_client.cpp | 2 +- .../private/pose_tracker_server.cpp | 2 +- src/viam/sdk/robot/client.cpp | 4 +- .../sdk/services/private/motion_client.cpp | 8 +- src/viam/sdk/spatialmath/geometry.cpp | 30 +------- src/viam/sdk/tests/mocks/mock_robot.cpp | 2 +- src/viam/sdk/tests/test_resource.cpp | 2 +- 12 files changed, 98 insertions(+), 80 deletions(-) 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..10ec0138a 100644 --- a/src/viam/sdk/common/pose.hpp +++ b/src/viam/sdk/common/pose.hpp @@ -1,8 +1,20 @@ #pragma once -#include +#include + +#include namespace viam { + +namespace common { +namespace v1 { + +class Pose; +class PoseInFrame; + +} // namespace v1 +} // namespace common + namespace sdk { struct coordinates { @@ -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/world_state.cpp b/src/viam/sdk/common/world_state.cpp index bd1fa5ca6..351964ff1 100644 --- a/src/viam/sdk/common/world_state.cpp +++ b/src/viam/sdk/common/world_state.cpp @@ -60,7 +60,7 @@ common::v1::WorldState WorldState::to_proto() const { 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()); + transform.pose_in_observer_frame = v2::from_proto(proto.pose_in_observer_frame()); if (proto.has_physical_object()) { transform.physical_object = std::make_shared(GeometryConfig::from_proto(proto.physical_object())); @@ -72,7 +72,7 @@ WorldState::transform WorldState::transform::from_proto(const common::v1::Transf 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(); + *proto.mutable_pose_in_observer_frame() = v2::to_proto(pose_in_observer_frame); if (physical_object) { *proto.mutable_physical_object() = physical_object->to_proto(); } diff --git a/src/viam/sdk/components/private/arm_client.cpp b/src/viam/sdk/components/private/arm_client.cpp index a78700168..491b5142b 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(); } diff --git a/src/viam/sdk/components/private/arm_server.cpp b/src/viam/sdk/components/private/arm_server.cpp index fad2c0399..0a0880bae 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()); }); } diff --git a/src/viam/sdk/components/private/pose_tracker_client.cpp b/src/viam/sdk/components/private/pose_tracker_client.cpp index 6b2794540..68bec4afe 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::vectorbody_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)}); } }); } diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 15cac8e17..6c81f3c9a 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -398,7 +398,7 @@ 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(); @@ -411,7 +411,7 @@ pose_in_frame RobotClient::transform_pose( 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( diff --git a/src/viam/sdk/services/private/motion_client.cpp b/src/viam/sdk/services/private/motion_client.cpp index d2c6c12c3..b0ba331c5 100644 --- a/src/viam/sdk/services/private/motion_client.cpp +++ b/src/viam/sdk/services/private/motion_client.cpp @@ -140,7 +140,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,7 +189,7 @@ 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); } @@ -210,7 +210,7 @@ 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(); @@ -274,7 +274,7 @@ pose_in_frame MotionClient::get_pose( *request.mutable_supplemental_transforms()->Add() = transform.to_proto(); } }) - .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) { diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 4fc999910..65b5713b1 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -54,33 +54,7 @@ viam::common::v1::Capsule GeometryConfig::capsule_proto() const { } viam::common::v1::Pose GeometryConfig::pose_proto() const { - return pose_.to_proto(); -} - -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; -} - -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; + return v2::to_proto(pose_); } GeometryConfig::GeometryConfig() : geometry_type_(GeometryType::box) {} @@ -88,7 +62,7 @@ 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.pose_ = v2::from_proto(pose); cfg.label_ = proto.label(); switch (proto.geometry_type_case()) { diff --git a/src/viam/sdk/tests/mocks/mock_robot.cpp b/src/viam/sdk/tests/mocks/mock_robot.cpp index 11eae813b..43b9b509a 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() { diff --git a/src/viam/sdk/tests/test_resource.cpp b/src/viam/sdk/tests/test_resource.cpp index 39cdedb5e..64837d0e7 100644 --- a/src/viam/sdk/tests/test_resource.cpp +++ b/src/viam/sdk/tests/test_resource.cpp @@ -139,7 +139,7 @@ BOOST_AUTO_TEST_CASE(test_linkconfig) { 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()); From 5128069aec2ec09a2f8c3cb6b9be40dab4915187 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Fri, 22 Nov 2024 10:10:24 -0500 Subject: [PATCH 03/29] add api fwd namespace macro --- src/viam/sdk/common/pose.hpp | 9 +++------ src/viam/sdk/common/proto_convert.hpp | 10 ++++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/viam/sdk/common/pose.hpp b/src/viam/sdk/common/pose.hpp index 10ec0138a..e6bc46e2a 100644 --- a/src/viam/sdk/common/pose.hpp +++ b/src/viam/sdk/common/pose.hpp @@ -4,17 +4,14 @@ #include -namespace viam { - -namespace common { -namespace v1 { +VIAM_SDK_API_FWD_NAMESPACE_BEGIN class Pose; class PoseInFrame; -} // namespace v1 -} // namespace common +VIAM_SDK_API_FWD_NAMESPACE_END +namespace viam { namespace sdk { struct coordinates { diff --git a/src/viam/sdk/common/proto_convert.hpp b/src/viam/sdk/common/proto_convert.hpp index 08e571047..c14257593 100644 --- a/src/viam/sdk/common/proto_convert.hpp +++ b/src/viam/sdk/common/proto_convert.hpp @@ -6,6 +6,16 @@ #include +#define VIAM_SDK_API_FWD_NAMESPACE_BEGIN \ + namespace viam { \ + namespace common { \ + namespace v1 { + +#define VIAM_SDK_API_FWD_NAMESPACE_END \ + } \ + } \ + } + namespace viam { namespace sdk { From cf35091699226a96965ee0617a2955b100c301bc Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:35:42 -0500 Subject: [PATCH 04/29] more general macro --- src/viam/sdk/common/pose.hpp | 2 +- src/viam/sdk/common/proto_convert.hpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/viam/sdk/common/pose.hpp b/src/viam/sdk/common/pose.hpp index e6bc46e2a..548e063eb 100644 --- a/src/viam/sdk/common/pose.hpp +++ b/src/viam/sdk/common/pose.hpp @@ -4,7 +4,7 @@ #include -VIAM_SDK_API_FWD_NAMESPACE_BEGIN +VIAM_SDK_API_FWD_NAMESPACE_BEGIN(common) class Pose; class PoseInFrame; diff --git a/src/viam/sdk/common/proto_convert.hpp b/src/viam/sdk/common/proto_convert.hpp index c14257593..b70d28f18 100644 --- a/src/viam/sdk/common/proto_convert.hpp +++ b/src/viam/sdk/common/proto_convert.hpp @@ -6,9 +6,9 @@ #include -#define VIAM_SDK_API_FWD_NAMESPACE_BEGIN \ - namespace viam { \ - namespace common { \ +#define VIAM_SDK_API_FWD_NAMESPACE_BEGIN(module_ns_name) \ + namespace viam { \ + namespace module_ns_name { \ namespace v1 { #define VIAM_SDK_API_FWD_NAMESPACE_END \ From 7a3376aac6cc643c23aa1f1f6c688ec76d6178ed Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:48:55 -0500 Subject: [PATCH 05/29] change translation --- src/viam/sdk/referenceframe/frame.cpp | 2 +- src/viam/sdk/spatialmath/orientation_types.cpp | 11 +++++------ src/viam/sdk/spatialmath/orientation_types.hpp | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/viam/sdk/referenceframe/frame.cpp b/src/viam/sdk/referenceframe/frame.cpp index 299942384..4bec25a0a 100644 --- a/src/viam/sdk/referenceframe/frame.cpp +++ b/src/viam/sdk/referenceframe/frame.cpp @@ -18,7 +18,7 @@ viam::app::v1::Frame LinkConfig::to_proto() const { *frame.mutable_parent() = parent_; *frame.mutable_geometry() = geometry_.to_proto(); *frame.mutable_orientation() = orientation_.to_proto(); - *frame.mutable_translation() = translation_.to_proto(); + *frame.mutable_translation() = v2::to_proto(translation_); return frame; }; diff --git a/src/viam/sdk/spatialmath/orientation_types.cpp b/src/viam/sdk/spatialmath/orientation_types.cpp index 454154ef3..177d3a398 100644 --- a/src/viam/sdk/spatialmath/orientation_types.cpp +++ b/src/viam/sdk/spatialmath/orientation_types.cpp @@ -5,12 +5,11 @@ 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; +void proto_convert_details::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); } } // namespace sdk diff --git a/src/viam/sdk/spatialmath/orientation_types.hpp b/src/viam/sdk/spatialmath/orientation_types.hpp index bcd077f6e..6b91fe83c 100644 --- a/src/viam/sdk/spatialmath/orientation_types.hpp +++ b/src/viam/sdk/spatialmath/orientation_types.hpp @@ -1,6 +1,12 @@ #pragma once -#include +#include + +VIAM_SDK_API_FWD_NAMESPACE_BEGIN(app) + +class Translation; + +VIAM_SDK_API_FWD_NAMESPACE_END namespace viam { namespace sdk { @@ -35,7 +41,15 @@ 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; +}; + +} // namespace proto_convert_details } // namespace sdk } // namespace viam From a4406142b915a3e6f698f4a071524133f2158bed Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 25 Nov 2024 08:25:49 -0500 Subject: [PATCH 06/29] refactor orientation and related code cleanup --- src/viam/sdk/referenceframe/frame.cpp | 6 +- src/viam/sdk/referenceframe/frame.hpp | 4 +- src/viam/sdk/spatialmath/geometry.cpp | 30 ++- src/viam/sdk/spatialmath/geometry.hpp | 19 +- src/viam/sdk/spatialmath/orientation.cpp | 223 +++++++++++------------ src/viam/sdk/spatialmath/orientation.hpp | 48 +++-- src/viam/sdk/tests/mocks/mock_motion.cpp | 4 +- 7 files changed, 168 insertions(+), 166 deletions(-) diff --git a/src/viam/sdk/referenceframe/frame.cpp b/src/viam/sdk/referenceframe/frame.cpp index 4bec25a0a..f9b7837cf 100644 --- a/src/viam/sdk/referenceframe/frame.cpp +++ b/src/viam/sdk/referenceframe/frame.cpp @@ -17,7 +17,7 @@ viam::app::v1::Frame LinkConfig::to_proto() const { *frame.mutable_parent() = parent_; *frame.mutable_geometry() = geometry_.to_proto(); - *frame.mutable_orientation() = orientation_.to_proto(); + *frame.mutable_orientation() = v2::to_proto(orientation_); *frame.mutable_translation() = v2::to_proto(translation_); return frame; }; @@ -31,7 +31,7 @@ LinkConfig LinkConfig::from_proto(const viam::app::v1::Frame& proto) { lc.translation_.z = proto.translation().z(); if (proto.has_orientation()) { - lc.orientation_ = OrientationConfig::from_proto(proto.orientation()); + lc.orientation_ = v2::from_proto(proto.orientation()); } if (proto.has_geometry()) { @@ -45,7 +45,7 @@ translation LinkConfig::get_translation() const { return translation_; } -OrientationConfig LinkConfig::get_orientation_config() const { +const Orientation& LinkConfig::get_orientation() const { return orientation_; } diff --git a/src/viam/sdk/referenceframe/frame.hpp b/src/viam/sdk/referenceframe/frame.hpp index 4d5f593e5..f09e3b8af 100644 --- a/src/viam/sdk/referenceframe/frame.hpp +++ b/src/viam/sdk/referenceframe/frame.hpp @@ -15,14 +15,14 @@ class LinkConfig { 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; + const Orientation& get_orientation() const; GeometryConfig get_geometry_config() const; std::string get_parent() const; private: std::string id_; translation translation_; - OrientationConfig orientation_; + Orientation orientation_; GeometryConfig geometry_; std::string parent_; }; diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 65b5713b1..ad4ab588c 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -143,50 +143,64 @@ viam::common::v1::Geometry GeometryConfig::to_proto() const { } } } + 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_orientation(Orientation o) { + orientation_ = std::move(o); } + void GeometryConfig::set_label(std::string label) { label_ = std::move(label); } -geometry_specifics GeometryConfig::get_geometry_specifics() const { + +const geometry_specifics& GeometryConfig::get_geometry_specifics() const { return geometry_specifics_; } + double GeometryConfig::get_theta() const { return pose_.theta; } + GeometryType GeometryConfig::get_geometry_type() const { return geometry_type_; } -coordinates GeometryConfig::get_coordinates() const { + +const 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_; } diff --git a/src/viam/sdk/spatialmath/geometry.hpp b/src/viam/sdk/spatialmath/geometry.hpp index 0d0dff78b..e37b0c3fe 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -49,22 +49,27 @@ class GeometryConfig { static GeometryConfig from_proto(const viam::common::v1::Geometry& proto); static std::vector from_proto( const viam::common::v1::GetGeometriesResponse& proto); + + // TODO replace trivial setters with constructor 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_orientation(Orientation); void set_label(std::string label); + double get_theta() const; - coordinates get_coordinates() const; - pose get_pose() const; - geometry_specifics get_geometry_specifics() const; + const coordinates& get_coordinates() 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: @@ -73,7 +78,7 @@ class GeometryConfig { 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_; }; 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..4089f5653 100644 --- a/src/viam/sdk/spatialmath/orientation.hpp +++ b/src/viam/sdk/spatialmath/orientation.hpp @@ -5,37 +5,35 @@ #include -#include - +#include #include +VIAM_SDK_API_FWD_NAMESPACE_BEGIN(app) + +class Orientation; + +VIAM_SDK_API_FWD_NAMESPACE_END + 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/tests/mocks/mock_motion.cpp b/src/viam/sdk/tests/mocks/mock_motion.cpp index 9d2eb9754..5f1ee1742 100644 --- a/src/viam/sdk/tests/mocks/mock_motion.cpp +++ b/src/viam/sdk/tests/mocks/mock_motion.cpp @@ -177,7 +177,7 @@ std::vector fake_obstacles() { struct sphere sphere({1}); gc.set_geometry_specifics(sphere); gc.set_label("label"); - gc.set_orientation_config({AxisAngles, {}, axis_angles{1, 2, 3, 4}}); + gc.set_orientation(axis_angles{1, 2, 3, 4}); return {{fake_geo_point(), {std::move(gc)}}}; } @@ -188,7 +188,7 @@ std::vector fake_bounding_regions() { struct sphere sphere({2}); gc.set_geometry_specifics(sphere); gc.set_label("label"); - gc.set_orientation_config({AxisAngles, {}, axis_angles{1, 2, 3, 4}}); + gc.set_orientation(axis_angles{1, 2, 3, 4}); return {{fake_geo_point(), {std::move(gc)}}}; } From 924b794b8156ab0d1ba069e2d066cb72c0c05e0e Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 25 Nov 2024 09:01:30 -0500 Subject: [PATCH 07/29] insult api vector3 and make sdk vector3 a struct --- src/viam/sdk/common/linear_algebra.cpp | 38 +++++++---------- src/viam/sdk/common/linear_algebra.hpp | 41 ++++++++++++------- .../sdk/components/private/base_client.cpp | 8 ++-- .../sdk/components/private/base_server.cpp | 8 ++-- .../private/movement_sensor_client.cpp | 6 +-- .../private/movement_sensor_server.cpp | 6 +-- src/viam/sdk/spatialmath/geometry.cpp | 4 -- src/viam/sdk/spatialmath/geometry.hpp | 1 - src/viam/sdk/tests/test_movement_sensor.cpp | 12 +++--- 9 files changed, 60 insertions(+), 64 deletions(-) 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..690595aa2 100644 --- a/src/viam/sdk/common/linear_algebra.hpp +++ b/src/viam/sdk/common/linear_algebra.hpp @@ -5,18 +5,22 @@ #include #include -#include +#include + +VIAM_SDK_API_FWD_NAMESPACE_BEGIN(common) + +class Vector3; + +VIAM_SDK_API_FWD_NAMESPACE_END + 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 +32,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; +}; + +namespace proto_convert_details { - private: - std::array data_; +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 +62,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/components/private/base_client.cpp b/src/viam/sdk/components/private/base_client.cpp index 8a31e288e..5a5e552b3 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(); } diff --git a/src/viam/sdk/components/private/base_server.cpp b/src/viam/sdk/components/private/base_server.cpp index 238542721..1675622c4 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()); }); } diff --git a/src/viam/sdk/components/private/movement_sensor_client.cpp b/src/viam/sdk/components/private/movement_sensor_client.cpp index 7646cbe85..8d8e5bf48 100644 --- a/src/viam/sdk/components/private/movement_sensor_client.cpp +++ b/src/viam/sdk/components/private/movement_sensor_client.cpp @@ -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,7 +112,7 @@ 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) { diff --git a/src/viam/sdk/components/private/movement_sensor_server.cpp b/src/viam/sdk/components/private/movement_sensor_server.cpp index 6cfb32dc0..0166549b0 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); }); } @@ -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); }); } diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index ad4ab588c..14863dd60 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -188,10 +188,6 @@ GeometryType GeometryConfig::get_geometry_type() const { return geometry_type_; } -const coordinates& GeometryConfig::get_coordinates() const { - return pose_.coordinates; -} - const pose& GeometryConfig::get_pose() const { return pose_; } diff --git a/src/viam/sdk/spatialmath/geometry.hpp b/src/viam/sdk/spatialmath/geometry.hpp index e37b0c3fe..b113097fe 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -61,7 +61,6 @@ class GeometryConfig { void set_label(std::string label); double get_theta() const; - const coordinates& get_coordinates() const; const pose& get_pose() const; const geometry_specifics& get_geometry_specifics() const; GeometryType get_geometry_type() const; 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); }); } From c4385807c1c7fafff7cfe9711674c23ae1ff3c8b Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 25 Nov 2024 09:27:50 -0500 Subject: [PATCH 08/29] remove duplicated pose interface --- src/viam/sdk/spatialmath/geometry.cpp | 16 ---------------- src/viam/sdk/spatialmath/geometry.hpp | 3 --- src/viam/sdk/tests/test_utils.cpp | 4 ---- 3 files changed, 23 deletions(-) diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 14863dd60..bde8350a5 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -144,10 +144,6 @@ viam::common::v1::Geometry GeometryConfig::to_proto() const { } } -void GeometryConfig::set_coordinates(coordinates coordinates) { - pose_.coordinates = std::move(coordinates); -} - void GeometryConfig::set_pose(pose pose) { pose_ = std::move(pose); } @@ -156,14 +152,6 @@ 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); } @@ -180,10 +168,6 @@ const geometry_specifics& GeometryConfig::get_geometry_specifics() const { return geometry_specifics_; } -double GeometryConfig::get_theta() const { - return pose_.theta; -} - GeometryType GeometryConfig::get_geometry_type() const { return geometry_type_; } diff --git a/src/viam/sdk/spatialmath/geometry.hpp b/src/viam/sdk/spatialmath/geometry.hpp index b113097fe..a451af88d 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -51,10 +51,7 @@ class GeometryConfig { const viam::common::v1::GetGeometriesResponse& proto); // TODO replace trivial setters with constructor - 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(Orientation); diff --git a/src/viam/sdk/tests/test_utils.cpp b/src/viam/sdk/tests/test_utils.cpp index 4ffb969c3..1c7d74afc 100644 --- a/src/viam/sdk/tests/test_utils.cpp +++ b/src/viam/sdk/tests/test_utils.cpp @@ -21,7 +21,6 @@ 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)); @@ -29,7 +28,6 @@ std::vector fake_geometries() { 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)); @@ -37,7 +35,6 @@ std::vector fake_geometries() { 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)); @@ -45,7 +42,6 @@ std::vector fake_geometries() { 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)); From 4fbd8bf41afa26768e26c30679390cc91d15b6e3 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 25 Nov 2024 09:33:36 -0500 Subject: [PATCH 09/29] use std::tie for equality operators --- src/viam/sdk/spatialmath/geometry.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index bde8350a5..29c7da187 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -224,24 +224,23 @@ 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_type_, + lhs.geometry_specifics_) == std::tie(rhs.pose_.coordinates, + rhs.pose_.orientation, + rhs.label_, + rhs.geometry_type_, + 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 { From 4c258ac2bf7dbe26b8d84cc8d052a044e1a9476f Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 25 Nov 2024 09:42:21 -0500 Subject: [PATCH 10/29] remove pose_proto --- src/viam/sdk/spatialmath/geometry.cpp | 6 +----- src/viam/sdk/spatialmath/geometry.hpp | 1 - 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 29c7da187..3e2143d43 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -53,10 +53,6 @@ viam::common::v1::Capsule GeometryConfig::capsule_proto() const { } } -viam::common::v1::Pose GeometryConfig::pose_proto() const { - return v2::to_proto(pose_); -} - GeometryConfig::GeometryConfig() : geometry_type_(GeometryType::box) {} GeometryConfig GeometryConfig::from_proto(const viam::common::v1::Geometry& proto) { @@ -112,7 +108,7 @@ std::vector GeometryConfig::from_proto( viam::common::v1::Geometry GeometryConfig::to_proto() const { viam::common::v1::Geometry geometry_; *geometry_.mutable_label() = label_; - *geometry_.mutable_center() = pose_proto(); + *geometry_.mutable_center() = v2::to_proto(pose_); switch (geometry_type_) { case GeometryType::box: { *geometry_.mutable_box() = box_proto(); diff --git a/src/viam/sdk/spatialmath/geometry.hpp b/src/viam/sdk/spatialmath/geometry.hpp index a451af88d..6db18597a 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -45,7 +45,6 @@ class GeometryConfig { 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); From d52071192fb3083202fbe80c791028c290251e51 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 26 Nov 2024 09:51:58 -0500 Subject: [PATCH 11/29] clean up geometry.hpp and add vector/ptr_field conversion --- src/viam/sdk/common/private/proto_utils.hpp | 4 +- src/viam/sdk/common/world_state.cpp | 8 +- .../sdk/components/private/arm_client.cpp | 2 +- .../sdk/components/private/arm_server.cpp | 2 +- .../sdk/components/private/base_client.cpp | 2 +- .../sdk/components/private/base_server.cpp | 2 +- .../sdk/components/private/board_client.cpp | 2 +- .../sdk/components/private/board_server.cpp | 2 +- .../sdk/components/private/camera_client.cpp | 2 +- .../sdk/components/private/camera_server.cpp | 2 +- .../sdk/components/private/encoder_client.cpp | 2 +- .../sdk/components/private/encoder_server.cpp | 2 +- .../sdk/components/private/gantry_client.cpp | 2 +- .../sdk/components/private/gantry_server.cpp | 2 +- .../sdk/components/private/generic_client.cpp | 2 +- .../sdk/components/private/generic_server.cpp | 2 +- .../sdk/components/private/gripper_client.cpp | 2 +- .../sdk/components/private/gripper_server.cpp | 2 +- .../sdk/components/private/motor_client.cpp | 2 +- .../sdk/components/private/motor_server.cpp | 2 +- .../private/movement_sensor_client.cpp | 4 +- .../private/movement_sensor_server.cpp | 4 +- .../private/pose_tracker_client.cpp | 2 +- .../private/pose_tracker_server.cpp | 2 +- .../sdk/components/private/sensor_client.cpp | 2 +- .../sdk/components/private/sensor_server.cpp | 2 +- .../sdk/components/private/servo_client.cpp | 2 +- .../sdk/components/private/servo_server.cpp | 2 +- src/viam/sdk/referenceframe/frame.cpp | 4 +- .../sdk/services/private/motion_client.cpp | 4 +- .../sdk/services/private/motion_server.cpp | 4 +- .../services/private/navigation_client.cpp | 8 +- .../services/private/navigation_server.cpp | 6 +- src/viam/sdk/spatialmath/geometry.cpp | 306 ++++++++---------- src/viam/sdk/spatialmath/geometry.hpp | 123 +++++-- src/viam/sdk/tests/mocks/mock_motion.cpp | 17 +- src/viam/sdk/tests/test_resource.cpp | 13 +- src/viam/sdk/tests/test_utils.cpp | 37 +-- 38 files changed, 290 insertions(+), 300 deletions(-) diff --git a/src/viam/sdk/common/private/proto_utils.hpp b/src/viam/sdk/common/private/proto_utils.hpp index 1263587f6..46e044764 100644 --- a/src/viam/sdk/common/private/proto_utils.hpp +++ b/src/viam/sdk/common/private/proto_utils.hpp @@ -17,7 +17,7 @@ void vecToRepeatedPtr(const std::vector& vec, google::protobuf::RepeatedPtr dest.Clear(); dest.Reserve(vec.size()); for (auto& x : vec) { - *dest.Add() = x.to_proto(); + *dest.Add() = v2::to_proto(x); } } @@ -41,7 +41,7 @@ void repeatedPtrToVec(const google::protobuf::RepeatedPtrField& src, std::v vec.clear(); vec.reserve(src.size()); for (auto& x : src) { - vec.push_back(Dst::from_proto(x)); + vec.push_back(v2::from_proto(x)); } } diff --git a/src/viam/sdk/common/world_state.cpp b/src/viam/sdk/common/world_state.cpp index 351964ff1..0763617b1 100644 --- a/src/viam/sdk/common/world_state.cpp +++ b/src/viam/sdk/common/world_state.cpp @@ -11,7 +11,7 @@ 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.geometries.push_back(v2::from_proto(geo)); } gif.reference_frame = proto.reference_frame(); @@ -23,7 +23,7 @@ common::v1::GeometriesInFrame WorldState::geometries_in_frame::to_proto() const *proto.mutable_reference_frame() = reference_frame; for (const auto& geometry : geometries) { - *proto.mutable_geometries()->Add() = geometry.to_proto(); + *proto.mutable_geometries()->Add() = v2::to_proto(geometry); } return proto; @@ -63,7 +63,7 @@ WorldState::transform WorldState::transform::from_proto(const common::v1::Transf transform.pose_in_observer_frame = v2::from_proto(proto.pose_in_observer_frame()); if (proto.has_physical_object()) { transform.physical_object = - std::make_shared(GeometryConfig::from_proto(proto.physical_object())); + std::make_shared(v2::from_proto(proto.physical_object())); } return transform; @@ -74,7 +74,7 @@ common::v1::Transform WorldState::transform::to_proto() const { *proto.mutable_reference_frame() = reference_frame; *proto.mutable_pose_in_observer_frame() = v2::to_proto(pose_in_observer_frame); if (physical_object) { - *proto.mutable_physical_object() = physical_object->to_proto(); + *proto.mutable_physical_object() = v2::to_proto(*physical_object); } return proto; diff --git a/src/viam/sdk/components/private/arm_client.cpp b/src/viam/sdk/components/private/arm_client.cpp index 491b5142b..f58115d3c 100644 --- a/src/viam/sdk/components/private/arm_client.cpp +++ b/src/viam/sdk/components/private/arm_client.cpp @@ -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 0a0880bae..6eef7dd6b 100644 --- a/src/viam/sdk/components/private/arm_server.cpp +++ b/src/viam/sdk/components/private/arm_server.cpp @@ -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 5a5e552b3..ca325444a 100644 --- a/src/viam/sdk/components/private/base_client.cpp +++ b/src/viam/sdk/components/private/base_client.cpp @@ -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) { diff --git a/src/viam/sdk/components/private/base_server.cpp b/src/viam/sdk/components/private/base_server.cpp index 1675622c4..7c19e623d 100644 --- a/src/viam/sdk/components/private/base_server.cpp +++ b/src/viam/sdk/components/private/base_server.cpp @@ -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); } }); } diff --git a/src/viam/sdk/components/private/board_client.cpp b/src/viam/sdk/components/private/board_client.cpp index 76ad230c1..072eb0638 100644 --- a/src/viam/sdk/components/private/board_client.cpp +++ b/src/viam/sdk/components/private/board_client.cpp @@ -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..acd4662ca 100644 --- a/src/viam/sdk/components/private/board_server.cpp +++ b/src/viam/sdk/components/private/board_server.cpp @@ -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..b86c62cd7 100644 --- a/src/viam/sdk/components/private/camera_client.cpp +++ b/src/viam/sdk/components/private/camera_client.cpp @@ -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..49ce978b9 100644 --- a/src/viam/sdk/components/private/camera_server.cpp +++ b/src/viam/sdk/components/private/camera_server.cpp @@ -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..e09535b8a 100644 --- a/src/viam/sdk/components/private/encoder_client.cpp +++ b/src/viam/sdk/components/private/encoder_client.cpp @@ -67,7 +67,7 @@ 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) { diff --git a/src/viam/sdk/components/private/encoder_server.cpp b/src/viam/sdk/components/private/encoder_server.cpp index dfb6c0aa9..798e70d14 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); } }); } diff --git a/src/viam/sdk/components/private/gantry_client.cpp b/src/viam/sdk/components/private/gantry_client.cpp index 18ac97b62..0fac69e89 100644 --- a/src/viam/sdk/components/private/gantry_client.cpp +++ b/src/viam/sdk/components/private/gantry_client.cpp @@ -72,7 +72,7 @@ ProtoStruct GantryClient::do_command(const ProtoStruct& command) { 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..7e8e6f247 100644 --- a/src/viam/sdk/components/private/gantry_server.cpp +++ b/src/viam/sdk/components/private/gantry_server.cpp @@ -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..96c8db854 100644 --- a/src/viam/sdk/components/private/generic_client.cpp +++ b/src/viam/sdk/components/private/generic_client.cpp @@ -30,7 +30,7 @@ ProtoStruct GenericComponentClient::do_command(const ProtoStruct& command) { 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..1e2b9423d 100644 --- a/src/viam/sdk/components/private/generic_server.cpp +++ b/src/viam/sdk/components/private/generic_server.cpp @@ -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..a6070c553 100644 --- a/src/viam/sdk/components/private/gripper_client.cpp +++ b/src/viam/sdk/components/private/gripper_client.cpp @@ -45,7 +45,7 @@ ProtoStruct GripperClient::do_command(const ProtoStruct& command) { 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..980988567 100644 --- a/src/viam/sdk/components/private/gripper_server.cpp +++ b/src/viam/sdk/components/private/gripper_server.cpp @@ -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..7d1f438d9 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() { diff --git a/src/viam/sdk/components/private/motor_server.cpp b/src/viam/sdk/components/private/motor_server.cpp index 75cb6cc7c..69739d65e 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); } }); } diff --git a/src/viam/sdk/components/private/movement_sensor_client.cpp b/src/viam/sdk/components/private/movement_sensor_client.cpp index 8d8e5bf48..a4466c622 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; } @@ -124,7 +124,7 @@ ProtoStruct MovementSensorClient::do_command(const ProtoStruct& command) { 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 0166549b0..dc3240265 100644 --- a/src/viam/sdk/components/private/movement_sensor_server.cpp +++ b/src/viam/sdk/components/private/movement_sensor_server.cpp @@ -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); }); } @@ -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 68bec4afe..7d9a7cb6c 100644 --- a/src/viam/sdk/components/private/pose_tracker_client.cpp +++ b/src/viam/sdk/components/private/pose_tracker_client.cpp @@ -35,7 +35,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) { diff --git a/src/viam/sdk/components/private/pose_tracker_server.cpp b/src/viam/sdk/components/private/pose_tracker_server.cpp index 48ef3478f..0009b9697 100644 --- a/src/viam/sdk/components/private/pose_tracker_server.cpp +++ b/src/viam/sdk/components/private/pose_tracker_server.cpp @@ -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/sensor_client.cpp b/src/viam/sdk/components/private/sensor_client.cpp index 04e0b3e1a..a083b0474 100644 --- a/src/viam/sdk/components/private/sensor_client.cpp +++ b/src/viam/sdk/components/private/sensor_client.cpp @@ -44,7 +44,7 @@ ProtoStruct SensorClient::do_command(const ProtoStruct& command) { 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..5432a0f33 100644 --- a/src/viam/sdk/components/private/sensor_server.cpp +++ b/src/viam/sdk/components/private/sensor_server.cpp @@ -44,7 +44,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..6b27bdaff 100644 --- a/src/viam/sdk/components/private/servo_client.cpp +++ b/src/viam/sdk/components/private/servo_client.cpp @@ -52,7 +52,7 @@ 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) { diff --git a/src/viam/sdk/components/private/servo_server.cpp b/src/viam/sdk/components/private/servo_server.cpp index de594db63..8392366a4 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); } }); } diff --git a/src/viam/sdk/referenceframe/frame.cpp b/src/viam/sdk/referenceframe/frame.cpp index f9b7837cf..4f655752c 100644 --- a/src/viam/sdk/referenceframe/frame.cpp +++ b/src/viam/sdk/referenceframe/frame.cpp @@ -16,7 +16,7 @@ viam::app::v1::Frame LinkConfig::to_proto() const { viam::app::v1::Frame frame; *frame.mutable_parent() = parent_; - *frame.mutable_geometry() = geometry_.to_proto(); + *frame.mutable_geometry() = v2::to_proto(geometry_); *frame.mutable_orientation() = v2::to_proto(orientation_); *frame.mutable_translation() = v2::to_proto(translation_); return frame; @@ -35,7 +35,7 @@ LinkConfig LinkConfig::from_proto(const viam::app::v1::Frame& proto) { } if (proto.has_geometry()) { - lc.geometry_ = GeometryConfig::from_proto(proto.geometry()); + lc.geometry_ = v2::from_proto(proto.geometry()); } return lc; diff --git a/src/viam/sdk/services/private/motion_client.cpp b/src/viam/sdk/services/private/motion_client.cpp index b0ba331c5..f054822e9 100644 --- a/src/viam/sdk/services/private/motion_client.cpp +++ b/src/viam/sdk/services/private/motion_client.cpp @@ -215,7 +215,7 @@ std::string MotionClient::move_on_map( *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 +237,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(); diff --git a/src/viam/sdk/services/private/motion_server.cpp b/src/viam/sdk/services/private/motion_server.cpp index 79d2dbb9b..86951d3e5 100644 --- a/src/viam/sdk/services/private/motion_server.cpp +++ b/src/viam/sdk/services/private/motion_server.cpp @@ -212,7 +212,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,7 +228,7 @@ ::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; diff --git a/src/viam/sdk/services/private/navigation_client.cpp b/src/viam/sdk/services/private/navigation_client.cpp index 7834d5009..d76339ae5 100644 --- a/src/viam/sdk/services/private/navigation_client.cpp +++ b/src/viam/sdk/services/private/navigation_client.cpp @@ -20,7 +20,7 @@ namespace impl { using namespace viam::service::navigation::v1; Navigation::Waypoint from_proto(const viam::service::navigation::v1::Waypoint& proto) { - return Navigation::Waypoint{proto.id(), geo_point::from_proto(proto.location())}; + return Navigation::Waypoint{proto.id(), v2::from_proto(proto.location())}; } Navigation::Path from_proto(const viam::service::navigation::v1::Path& proto) { @@ -32,7 +32,7 @@ Navigation::Path from_proto(const viam::service::navigation::v1::Path& proto) { 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) @@ -54,7 +54,7 @@ Navigation::LocationResponse NavigationClient::get_location(const ProtoStruct& e .with([&](auto& request) { *request.mutable_extra() = map_to_struct(extra); }) .invoke([](auto& response) { return Navigation::LocationResponse{ - geo_point::from_proto(response.location()), + v2::from_proto(response.location()), response.compass_heading(), }; }); @@ -73,7 +73,7 @@ std::vector NavigationClient::get_waypoints(const ProtoStr 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_location() = v2::to_proto(location); *request.mutable_extra() = map_to_struct(extra); }) .invoke([](auto& response) {}); diff --git a/src/viam/sdk/services/private/navigation_server.cpp b/src/viam/sdk/services/private/navigation_server.cpp index 0bfbe7204..5d8bdc2c2 100644 --- a/src/viam/sdk/services/private/navigation_server.cpp +++ b/src/viam/sdk/services/private/navigation_server.cpp @@ -20,7 +20,7 @@ using namespace service::navigation::v1; 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(); + *ret.mutable_location() = v2::to_proto(wp.location); return ret; } @@ -55,7 +55,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); }); } @@ -75,7 +75,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()); }); } diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 3e2143d43..54d6853bc 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -8,164 +8,39 @@ #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"); - } -} +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)) {} -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"); - } -} +GeometryConfig::GeometryConfig(pose p, geometry_specifics gs, std::string label) + : GeometryConfig(std::move(p), std::move(gs), {}, std::move(label)) {} -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"); - } +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_ = v2::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() = v2::to_proto(pose_); - 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_pose(pose pose) { - pose_ = std::move(pose); -} - -void GeometryConfig::set_geometry_specifics(geometry_specifics gs) { - geometry_specifics_ = std::move(gs); -} - -void GeometryConfig::set_geometry_type(GeometryType type) { - geometry_type_ = std::move(type); -} - -void GeometryConfig::set_orientation(Orientation o) { - orientation_ = std::move(o); -} - -void GeometryConfig::set_label(std::string label) { - label_ = std::move(label); -} - -const geometry_specifics& GeometryConfig::get_geometry_specifics() const { - return geometry_specifics_; -} - -GeometryType GeometryConfig::get_geometry_type() const { - return geometry_type_; + return boost::apply_visitor(Visitor{}, geometry_specifics_); } const pose& GeometryConfig::get_pose() const { @@ -220,15 +95,10 @@ bool operator==(const struct capsule& lhs, const struct capsule& rhs) { } bool operator==(const GeometryConfig& lhs, const GeometryConfig& rhs) { - return std::tie(lhs.pose_.coordinates, - lhs.pose_.orientation, - lhs.label_, - lhs.geometry_type_, - lhs.geometry_specifics_) == std::tie(rhs.pose_.coordinates, - rhs.pose_.orientation, - rhs.label_, - rhs.geometry_type_, - 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) { @@ -239,28 +109,12 @@ bool operator==(const geo_geometry& lhs, const geo_geometry& rhs) { 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); - - return proto; -} - -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(); - - return geo_point; -} - common::v1::GeoGeometry geo_geometry::to_proto() const { common::v1::GeoGeometry proto; - *proto.mutable_location() = location.to_proto(); + *proto.mutable_location() = v2::to_proto(location); for (const auto& geometry : geometries) { - *proto.mutable_geometries()->Add() = geometry.to_proto(); + *proto.mutable_geometries()->Add() = v2::to_proto(geometry); } return proto; @@ -269,14 +123,118 @@ common::v1::GeoGeometry geo_geometry::to_proto() const { geo_geometry geo_geometry::from_proto(const common::v1::GeoGeometry& proto) { struct geo_geometry geo_geometry; - geo_geometry.location = geo_point::from_proto(proto.location()); + geo_geometry.location = v2::from_proto(proto.location()); for (const auto& proto_geometry : proto.geometries()) { - auto geometry = GeometryConfig::from_proto(proto_geometry); + auto geometry = v2::from_proto(proto_geometry); geo_geometry.geometries.push_back(std::move(geometry)); } return geo_geometry; } +namespace proto_convert_details { + +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}); +} + +box from_proto::operator()( + const common::v1::RectangularPrism* proto) const { + const auto& dims = proto->dims_mm(); + return {dims.x(), dims.y(), dims.z()}; +} + +void to_proto::operator()(const sphere& self, common::v1::Sphere* proto) const { + proto->set_radius_mm(self.radius); +} + +sphere from_proto::operator()(const common::v1::Sphere* proto) const { + return {proto->radius_mm()}; +} + +void to_proto::operator()(const capsule& self, common::v1::Capsule* proto) const { + proto->set_radius_mm(self.radius); + proto->set_length_mm(self.length); +} + +capsule from_proto::operator()(const common::v1::Capsule* proto) const { + return {proto->radius_mm(), proto->length_mm()}; +} + +void to_proto::operator()(const GeometryConfig& self, + common::v1::Geometry* proto) const { + struct Visitor { + common::v1::Geometry& geometry; + + void operator()(const box& b) { + *geometry.mutable_box() = v2::to_proto(b); + } + + 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()) = v2::to_proto(self.geometries); +} + +geo_geometry from_proto::operator()( + const common::v1::GeoGeometry* proto) const { + return {v2::from_proto(proto->location()), v2::from_proto(proto->geometries())}; +} + +std::vector from_proto::operator()( + const common::v1::GetGeometriesResponse* proto) const { + return v2::from_proto(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 6db18597a..5e5e89a23 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -4,22 +4,31 @@ #include #include -#include - #include +#include +#include #include +VIAM_SDK_API_FWD_NAMESPACE_BEGIN(common) + +class RectangularPrism; +class Sphere; +class Capsule; + +class Geometry; +class GetGeometriesResponse; + +class GeoPoint; +class GeoGeometry; + +VIAM_SDK_API_FWD_NAMESPACE_END + 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,24 +46,14 @@ 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; - static GeometryConfig from_proto(const viam::common::v1::Geometry& proto); - static std::vector from_proto( - const viam::common::v1::GetGeometriesResponse& proto); - - // TODO replace trivial setters with constructor - void set_pose(pose pose); - void set_geometry_specifics(geometry_specifics gs); - void set_geometry_type(GeometryType type); - void set_orientation(Orientation); - 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; const pose& get_pose() const; @@ -65,10 +64,7 @@ class GeometryConfig { 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 @@ -78,10 +74,10 @@ class GeometryConfig { }; 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); }; @@ -95,5 +91,74 @@ struct geo_geometry { 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/tests/mocks/mock_motion.cpp b/src/viam/sdk/tests/mocks/mock_motion.cpp index 5f1ee1742..93237325e 100644 --- a/src/viam/sdk/tests/mocks/mock_motion.cpp +++ b/src/viam/sdk/tests/mocks/mock_motion.cpp @@ -171,24 +171,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(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(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/test_resource.cpp b/src/viam/sdk/tests/test_resource.cpp index 64837d0e7..a71fd1161 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 { @@ -141,10 +143,13 @@ BOOST_AUTO_TEST_CASE(test_linkconfig) { BOOST_CHECK_EQUAL(gcfg.get_label(), "label"); 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()); + + 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 = lc.to_proto(); BOOST_CHECK_EQUAL(proto_lc.parent(), "parent"); diff --git a/src/viam/sdk/tests/test_utils.cpp b/src/viam/sdk/tests/test_utils.cpp index 1c7d74afc..703a7c52f 100644 --- a/src/viam/sdk/tests/test_utils.cpp +++ b/src/viam/sdk/tests/test_utils.cpp @@ -19,38 +19,11 @@ ProtoStruct fake_map() { } std::vector fake_geometries() { - GeometryConfig sphere_config; - sphere_config.set_geometry_type(GeometryType::sphere); - 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_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_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_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)) {} From c55948ec8e7ddde4f323b19d694730bb76935a47 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:32:06 -0500 Subject: [PATCH 12/29] linter --- src/viam/sdk/services/private/navigation_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/viam/sdk/services/private/navigation_client.cpp b/src/viam/sdk/services/private/navigation_client.cpp index d76339ae5..da4e1bf65 100644 --- a/src/viam/sdk/services/private/navigation_client.cpp +++ b/src/viam/sdk/services/private/navigation_client.cpp @@ -32,7 +32,7 @@ Navigation::Path from_proto(const viam::service::navigation::v1::Path& proto) { 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) From e2a5a3c8428c098eafd5529409a3068dd5a79f97 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:32:34 -0500 Subject: [PATCH 13/29] forgot to commit new file --- src/viam/sdk/common/proto_convert_vector.hpp | 51 ++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/viam/sdk/common/proto_convert_vector.hpp diff --git a/src/viam/sdk/common/proto_convert_vector.hpp b/src/viam/sdk/common/proto_convert_vector.hpp new file mode 100644 index 000000000..da5cf5f2b --- /dev/null +++ b/src/viam/sdk/common/proto_convert_vector.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include + +#include + +#include + +namespace google { +namespace protobuf { + +template +class RepeatedPtrField; + +} +} // namespace google + +namespace viam { +namespace sdk { +namespace proto_convert_details { + +template +struct to_proto> { + void operator()( + const std::vector& vec, + ::google::protobuf::RepeatedPtrField>* ptr_field) const { + ptr_field->Reserve(vec.size()); + for (const auto& elem : vec) { + to_proto{}(elem, ptr_field->Add()); + } + } +}; + +template +struct from_proto<::google::protobuf::RepeatedPtrField> { + auto operator()(const ::google::protobuf::RepeatedPtrField* ptr_field) const { + std::vector> result; + result.reserve(ptr_field->size()); + + for (const auto* elem : + boost::make_iterator_range(ptr_field->pointer_begin(), ptr_field->pointer_end())) { + result.push_back(from_proto{}(elem)); + } + + return result; + } +}; + +} // namespace proto_convert_details +} // namespace sdk +} // namespace viam From 8479d34025f617dd6665608b312cab4bf1e44d3c Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 26 Nov 2024 11:14:27 -0500 Subject: [PATCH 14/29] add headers to file list --- src/viam/sdk/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/viam/sdk/CMakeLists.txt b/src/viam/sdk/CMakeLists.txt index 626254823..49f89c66f 100644 --- a/src/viam/sdk/CMakeLists.txt +++ b/src/viam/sdk/CMakeLists.txt @@ -142,6 +142,8 @@ 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_convert_vector.hpp ../../viam/sdk/common/proto_value.hpp ../../viam/sdk/common/service_helper.hpp ../../viam/sdk/common/utils.hpp From 27f147c536a6123f4bd984637685bad65d3dc560 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 26 Nov 2024 12:08:26 -0500 Subject: [PATCH 15/29] also do translation and linkconfig --- src/viam/sdk/config/resource.cpp | 7 ++- src/viam/sdk/referenceframe/frame.cpp | 57 +++++++++---------- src/viam/sdk/referenceframe/frame.hpp | 34 ++++++++--- .../sdk/spatialmath/orientation_types.cpp | 10 +++- .../sdk/spatialmath/orientation_types.hpp | 5 ++ src/viam/sdk/tests/test_resource.cpp | 4 +- 6 files changed, 71 insertions(+), 46 deletions(-) diff --git a/src/viam/sdk/config/resource.cpp b/src/viam/sdk/config/resource.cpp index 758a52637..695ffb709 100644 --- a/src/viam/sdk/config/resource.cpp +++ b/src/viam/sdk/config/resource.cpp @@ -104,7 +104,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; @@ -133,12 +133,13 @@ viam::app::v1::ComponentConfig ResourceConfig::to_proto() const { 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)){}; +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 4f655752c..d93ec36b4 100644 --- a/src/viam/sdk/referenceframe/frame.cpp +++ b/src/viam/sdk/referenceframe/frame.cpp @@ -12,36 +12,13 @@ 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() = v2::to_proto(geometry_); - *frame.mutable_orientation() = v2::to_proto(orientation_); - *frame.mutable_translation() = v2::to_proto(translation_); - 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_ = v2::from_proto(proto.orientation()); - } - - if (proto.has_geometry()) { - lc.geometry_ = v2::from_proto(proto.geometry()); - } - - return lc; -}; - -translation LinkConfig::get_translation() const { +const translation& LinkConfig::get_translation() const { return translation_; } @@ -49,13 +26,31 @@ 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 f09e3b8af..0aaed7540 100644 --- a/src/viam/sdk/referenceframe/frame.hpp +++ b/src/viam/sdk/referenceframe/frame.hpp @@ -2,30 +2,48 @@ #include -#include - +#include #include #include +VIAM_SDK_API_FWD_NAMESPACE_BEGIN(app) + +class Frame; + +VIAM_SDK_API_FWD_NAMESPACE_END + 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; + LinkConfig() = default; + LinkConfig(translation t, Orientation o, GeometryConfig gcfg, std::string parent); + + const translation& get_translation() const; const Orientation& get_orientation() const; - GeometryConfig get_geometry_config() const; - std::string get_parent() const; + const GeometryConfig& get_geometry_config() const; + const std::string& get_parent() const; private: - std::string id_; translation translation_; 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/spatialmath/orientation_types.cpp b/src/viam/sdk/spatialmath/orientation_types.cpp index 177d3a398..1b981bc8c 100644 --- a/src/viam/sdk/spatialmath/orientation_types.cpp +++ b/src/viam/sdk/spatialmath/orientation_types.cpp @@ -5,12 +5,18 @@ namespace viam { namespace sdk { -void proto_convert_details::to_proto::operator()(const translation& self, - app::v1::Translation* t) const { +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 6b91fe83c..e1d9df6b7 100644 --- a/src/viam/sdk/spatialmath/orientation_types.hpp +++ b/src/viam/sdk/spatialmath/orientation_types.hpp @@ -50,6 +50,11 @@ 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/test_resource.cpp b/src/viam/sdk/tests/test_resource.cpp index a71fd1161..a14bfa917 100644 --- a/src/viam/sdk/tests/test_resource.cpp +++ b/src/viam/sdk/tests/test_resource.cpp @@ -134,7 +134,7 @@ 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()); @@ -151,7 +151,7 @@ BOOST_AUTO_TEST_CASE(test_linkconfig) { 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 = lc.to_proto(); + 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()); From f69d7971684da7eae659377fb4e75883dc946212 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 26 Nov 2024 12:11:11 -0500 Subject: [PATCH 16/29] linter semicolon --- src/viam/sdk/config/resource.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/viam/sdk/config/resource.cpp b/src/viam/sdk/config/resource.cpp index 695ffb709..67d7aed00 100644 --- a/src/viam/sdk/config/resource.cpp +++ b/src/viam/sdk/config/resource.cpp @@ -138,8 +138,7 @@ viam::app::v1::ComponentConfig ResourceConfig::to_proto() const { return proto_cfg; } -ResourceConfig::ResourceConfig(std::string type) - : api_({kRDK, type, ""}), type_(std::move(type)) {}; +ResourceConfig::ResourceConfig(std::string type) : api_({kRDK, type, ""}), type_(std::move(type)) {} } // namespace sdk } // namespace viam From 42552ff593e60130d7832697817b6c8a0a1b7731 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 26 Nov 2024 14:54:19 -0500 Subject: [PATCH 17/29] do worldstate --- src/viam/sdk/common/world_state.cpp | 112 ++++++++---------- src/viam/sdk/common/world_state.hpp | 62 ++++++++-- src/viam/sdk/config/resource.cpp | 4 +- src/viam/sdk/robot/client.cpp | 13 +- .../sdk/services/private/motion_client.cpp | 7 +- .../sdk/services/private/motion_server.cpp | 4 +- 6 files changed, 107 insertions(+), 95 deletions(-) diff --git a/src/viam/sdk/common/world_state.cpp b/src/viam/sdk/common/world_state.cpp index 0763617b1..c5ea3f463 100644 --- a/src/viam/sdk/common/world_state.cpp +++ b/src/viam/sdk/common/world_state.cpp @@ -3,96 +3,80 @@ #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(v2::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() = v2::to_proto(geometry); - } +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()) = v2::to_proto(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 {v2::from_proto(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 = v2::from_proto(proto.pose_in_observer_frame()); - if (proto.has_physical_object()) { - transform.physical_object = - std::make_shared(v2::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() = v2::to_proto(pose_in_observer_frame); - if (physical_object) { - *proto.mutable_physical_object() = v2::to_proto(*physical_object); +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()) = v2::to_proto(self.obstacles()); + *(proto->mutable_transforms()) = v2::to_proto(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(v2::from_proto(proto->obstacles()), v2::from_proto(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..a76275d16 100644 --- a/src/viam/sdk/common/world_state.hpp +++ b/src/viam/sdk/common/world_state.hpp @@ -1,10 +1,19 @@ #pragma once -#include +#include #include +#include #include +VIAM_SDK_API_FWD_NAMESPACE_BEGIN(common) + +class GeometriesInFrame; +class Transform; +class WorldState; + +VIAM_SDK_API_FWD_NAMESPACE_END + namespace viam { namespace sdk { @@ -14,26 +23,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 +47,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/config/resource.cpp b/src/viam/sdk/config/resource.cpp index 67d7aed00..5831428ba 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; @@ -138,7 +140,5 @@ viam::app::v1::ComponentConfig ResourceConfig::to_proto() const { 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/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 6c81f3c9a..068f9bc32 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -72,7 +72,7 @@ RobotClient::discovery from_proto(const Discovery& proto) { 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()); } @@ -368,10 +368,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()) = v2::to_proto(additional_transforms); const grpc::Status response = impl_->stub_->FrameSystemConfig(ctx, req, &resp); if (is_error_response(response)) { @@ -400,11 +397,7 @@ pose_in_frame RobotClient::transform_pose( *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() = v2::to_proto(additional_transforms); const grpc::Status response = impl_->stub_->TransformPose(ctx, req, &resp); if (is_error_response(response)) { diff --git a/src/viam/sdk/services/private/motion_client.cpp b/src/viam/sdk/services/private/motion_client.cpp index f054822e9..fa87ae56e 100644 --- a/src/viam/sdk/services/private/motion_client.cpp +++ b/src/viam/sdk/services/private/motion_client.cpp @@ -194,7 +194,7 @@ bool MotionClient::move(const pose_in_frame& destination, *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(); }); @@ -270,9 +270,8 @@ 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() = + v2::to_proto(supplemental_transforms); }) .invoke([](auto& response) { return v2::from_proto(response.pose()); }); } diff --git a/src/viam/sdk/services/private/motion_server.cpp b/src/viam/sdk/services/private/motion_server.cpp index 86951d3e5..fcc28dbcf 100644 --- a/src/viam/sdk/services/private/motion_server.cpp +++ b/src/viam/sdk/services/private/motion_server.cpp @@ -177,7 +177,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; @@ -276,7 +276,7 @@ ::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()); From 838262918efc49a2b8c91af322430c9ad7c23aa6 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 2 Dec 2024 15:24:25 -0500 Subject: [PATCH 18/29] remove proto_utils.hpp --- src/viam/sdk/common/private/proto_utils.hpp | 63 ------------------- src/viam/sdk/services/navigation.cpp | 1 - .../services/private/navigation_client.cpp | 49 +++++++-------- .../services/private/navigation_server.cpp | 46 +++++++------- 4 files changed, 48 insertions(+), 111 deletions(-) delete mode 100644 src/viam/sdk/common/private/proto_utils.hpp 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 46e044764..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() = v2::to_proto(x); - } -} - -/// @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(v2::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/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/navigation_client.cpp b/src/viam/sdk/services/private/navigation_client.cpp index da4e1bf65..b8f45a046 100644 --- a/src/viam/sdk/services/private/navigation_client.cpp +++ b/src/viam/sdk/services/private/navigation_client.cpp @@ -8,26 +8,35 @@ #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(), v2::from_proto(proto.location())}; -} +template <> +struct from_proto { + Navigation::Path operator()(const service::navigation::v1::Path* proto) const { + return {proto->destination_waypoint_id(), v2::from_proto(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)), @@ -63,11 +72,7 @@ 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; - }); + .invoke([](auto& response) { return v2::from_proto(response.waypoints()); }); } void NavigationClient::add_waypoint(const geo_point& location, const ProtoStruct& extra) { @@ -91,21 +96,13 @@ void NavigationClient::remove_waypoint(const std::string id, const ProtoStruct& 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; - }); + .invoke([](auto& response) { return v2::from_proto(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; - }); + .invoke([](auto& response) { return v2::from_proto(response.paths()); }); } NavigationClient::Properties NavigationClient::get_properties() { diff --git a/src/viam/sdk/services/private/navigation_server.cpp b/src/viam/sdk/services/private/navigation_server.cpp index 5d8bdc2c2..030a36670 100644 --- a/src/viam/sdk/services/private/navigation_server.cpp +++ b/src/viam/sdk/services/private/navigation_server.cpp @@ -3,7 +3,6 @@ #include #include -#include #include #include #include @@ -13,23 +12,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() = v2::to_proto(wp.location); - 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()) = v2::to_proto(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, @@ -65,8 +72,7 @@ ::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()) = v2::to_proto(nav->get_waypoints(helper.getExtra())); }); } @@ -91,8 +97,7 @@ ::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()) = v2::to_proto(nav->get_obstacles(helper.getExtra())); }); } @@ -101,8 +106,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() = v2::to_proto(nav->get_paths(helper.getExtra())); }); } From 141487f6909353740af9e57dab2a9106121250d9 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 3 Dec 2024 10:09:41 -0500 Subject: [PATCH 19/29] bring ProtoValue into the to_proto/from_proto fold --- src/viam/sdk/common/client_helper.hpp | 2 +- src/viam/sdk/common/proto_value.cpp | 125 +++++++++--------- src/viam/sdk/common/proto_value.hpp | 87 +++++------- src/viam/sdk/common/service_helper.hpp | 2 +- .../sdk/components/private/arm_client.cpp | 4 +- .../sdk/components/private/arm_server.cpp | 4 +- .../sdk/components/private/base_client.cpp | 4 +- .../sdk/components/private/base_server.cpp | 4 +- .../sdk/components/private/board_client.cpp | 8 +- .../sdk/components/private/board_server.cpp | 10 +- .../sdk/components/private/camera_client.cpp | 4 +- .../sdk/components/private/camera_server.cpp | 4 +- .../sdk/components/private/encoder_client.cpp | 4 +- .../sdk/components/private/encoder_server.cpp | 4 +- .../sdk/components/private/gantry_client.cpp | 4 +- .../sdk/components/private/gantry_server.cpp | 4 +- .../sdk/components/private/generic_client.cpp | 4 +- .../sdk/components/private/generic_server.cpp | 4 +- .../sdk/components/private/gripper_client.cpp | 4 +- .../sdk/components/private/gripper_server.cpp | 4 +- .../sdk/components/private/motor_client.cpp | 4 +- .../sdk/components/private/motor_server.cpp | 4 +- .../private/movement_sensor_client.cpp | 4 +- .../private/movement_sensor_server.cpp | 4 +- .../private/pose_tracker_client.cpp | 4 +- .../private/pose_tracker_server.cpp | 4 +- .../private/power_sensor_client.cpp | 8 +- .../private/power_sensor_server.cpp | 12 +- .../sdk/components/private/sensor_client.cpp | 6 +- .../sdk/components/private/sensor_server.cpp | 10 +- .../sdk/components/private/servo_client.cpp | 4 +- .../sdk/components/private/servo_server.cpp | 4 +- src/viam/sdk/config/resource.cpp | 8 +- src/viam/sdk/robot/client.cpp | 22 +-- src/viam/sdk/robot/service.cpp | 2 +- .../sdk/services/private/generic_client.cpp | 4 +- .../sdk/services/private/generic_server.cpp | 6 +- .../sdk/services/private/mlmodel_client.cpp | 6 +- .../sdk/services/private/mlmodel_server.cpp | 2 +- .../sdk/services/private/motion_client.cpp | 4 +- .../sdk/services/private/motion_server.cpp | 4 +- .../services/private/navigation_client.cpp | 20 +-- .../services/private/navigation_server.cpp | 4 +- src/viam/sdk/tests/mocks/mock_robot.cpp | 2 +- src/viam/sdk/tests/test_proto_value.cpp | 24 ++-- src/viam/sdk/tests/test_resource.cpp | 2 +- src/viam/sdk/tests/test_robot.cpp | 6 +- 47 files changed, 226 insertions(+), 248 deletions(-) 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/proto_value.cpp b/src/viam/sdk/common/proto_value.cpp index 0650856de..580941c2e 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()( + const google::protobuf::Value* v) const { // 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(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()( + const google::protobuf::Struct* s) const { // NOLINT(misc-no-recursion) + 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/components/private/arm_client.cpp b/src/viam/sdk/components/private/arm_client.cpp index f58115d3c..ea889d1e4 100644 --- a/src/viam/sdk/components/private/arm_client.cpp +++ b/src/viam/sdk/components/private/arm_client.cpp @@ -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) { diff --git a/src/viam/sdk/components/private/arm_server.cpp b/src/viam/sdk/components/private/arm_server.cpp index 6eef7dd6b..505c2122c 100644 --- a/src/viam/sdk/components/private/arm_server.cpp +++ b/src/viam/sdk/components/private/arm_server.cpp @@ -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); }); } diff --git a/src/viam/sdk/components/private/base_client.cpp b/src/viam/sdk/components/private/base_client.cpp index ca325444a..93623d04c 100644 --- a/src/viam/sdk/components/private/base_client.cpp +++ b/src/viam/sdk/components/private/base_client.cpp @@ -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 7c19e623d..722a1f9ae 100644 --- a/src/viam/sdk/components/private/base_server.cpp +++ b/src/viam/sdk/components/private/base_server.cpp @@ -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 072eb0638..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()) { diff --git a/src/viam/sdk/components/private/board_server.cpp b/src/viam/sdk/components/private/board_server.cpp index acd4662ca..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 = diff --git a/src/viam/sdk/components/private/camera_client.cpp b/src/viam/sdk/components/private/camera_client.cpp index b86c62cd7..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) { diff --git a/src/viam/sdk/components/private/camera_server.cpp b/src/viam/sdk/components/private/camera_server.cpp index 49ce978b9..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); }); } diff --git a/src/viam/sdk/components/private/encoder_client.cpp b/src/viam/sdk/components/private/encoder_client.cpp index e09535b8a..320b095bd 100644 --- a/src/viam/sdk/components/private/encoder_client.cpp +++ b/src/viam/sdk/components/private/encoder_client.cpp @@ -72,8 +72,8 @@ std::vector EncoderClient::get_geometries(const ProtoStruct& ext 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 798e70d14..a85cbf157 100644 --- a/src/viam/sdk/components/private/encoder_server.cpp +++ b/src/viam/sdk/components/private/encoder_server.cpp @@ -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 0fac69e89..028c1c55f 100644 --- a/src/viam/sdk/components/private/gantry_client.cpp +++ b/src/viam/sdk/components/private/gantry_client.cpp @@ -65,8 +65,8 @@ 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) { diff --git a/src/viam/sdk/components/private/gantry_server.cpp b/src/viam/sdk/components/private/gantry_server.cpp index 7e8e6f247..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); }); } diff --git a/src/viam/sdk/components/private/generic_client.cpp b/src/viam/sdk/components/private/generic_client.cpp index 96c8db854..ec062a860 100644 --- a/src/viam/sdk/components/private/generic_client.cpp +++ b/src/viam/sdk/components/private/generic_client.cpp @@ -23,8 +23,8 @@ 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) { diff --git a/src/viam/sdk/components/private/generic_server.cpp b/src/viam/sdk/components/private/generic_server.cpp index 1e2b9423d..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( diff --git a/src/viam/sdk/components/private/gripper_client.cpp b/src/viam/sdk/components/private/gripper_client.cpp index a6070c553..6e77fd52a 100644 --- a/src/viam/sdk/components/private/gripper_client.cpp +++ b/src/viam/sdk/components/private/gripper_client.cpp @@ -38,8 +38,8 @@ 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) { diff --git a/src/viam/sdk/components/private/gripper_server.cpp b/src/viam/sdk/components/private/gripper_server.cpp index 980988567..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); }); } diff --git a/src/viam/sdk/components/private/motor_client.cpp b/src/viam/sdk/components/private/motor_client.cpp index 7d1f438d9..bad14ccc6 100644 --- a/src/viam/sdk/components/private/motor_client.cpp +++ b/src/viam/sdk/components/private/motor_client.cpp @@ -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 69739d65e..66f366fe6 100644 --- a/src/viam/sdk/components/private/motor_server.cpp +++ b/src/viam/sdk/components/private/motor_server.cpp @@ -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 a4466c622..40eeddcde 100644 --- a/src/viam/sdk/components/private/movement_sensor_client.cpp +++ b/src/viam/sdk/components/private/movement_sensor_client.cpp @@ -117,8 +117,8 @@ Vector3 MovementSensorClient::get_linear_acceleration(const ProtoStruct& extra) 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) { diff --git a/src/viam/sdk/components/private/movement_sensor_server.cpp b/src/viam/sdk/components/private/movement_sensor_server.cpp index dc3240265..7f48a61e5 100644 --- a/src/viam/sdk/components/private/movement_sensor_server.cpp +++ b/src/viam/sdk/components/private/movement_sensor_server.cpp @@ -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); }); } diff --git a/src/viam/sdk/components/private/pose_tracker_client.cpp b/src/viam/sdk/components/private/pose_tracker_client.cpp index 7d9a7cb6c..25e1f3490 100644 --- a/src/viam/sdk/components/private/pose_tracker_client.cpp +++ b/src/viam/sdk/components/private/pose_tracker_client.cpp @@ -40,8 +40,8 @@ std::vector PoseTrackerClient::get_geometries(const ProtoStruct& 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 0009b9697..028acb775 100644 --- a/src/viam/sdk/components/private/pose_tracker_server.cpp +++ b/src/viam/sdk/components/private/pose_tracker_server.cpp @@ -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); }); } diff --git a/src/viam/sdk/components/private/power_sensor_client.cpp b/src/viam/sdk/components/private/power_sensor_client.cpp index 63f2cc6b8..8f08aee69 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..2b5cecac3 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 a083b0474..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,8 +37,8 @@ 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) { diff --git a/src/viam/sdk/components/private/sensor_server.cpp b/src/viam/sdk/components/private/sensor_server.cpp index 5432a0f33..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); }); } diff --git a/src/viam/sdk/components/private/servo_client.cpp b/src/viam/sdk/components/private/servo_client.cpp index 6b27bdaff..eb5004e84 100644 --- a/src/viam/sdk/components/private/servo_client.cpp +++ b/src/viam/sdk/components/private/servo_client.cpp @@ -57,8 +57,8 @@ std::vector ServoClient::get_geometries(const ProtoStruct& extra 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 8392366a4..831b57781 100644 --- a/src/viam/sdk/components/private/servo_server.cpp +++ b/src/viam/sdk/components/private/servo_server.cpp @@ -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 5831428ba..ed772a328 100644 --- a/src/viam/sdk/config/resource.cpp +++ b/src/viam/sdk/config/resource.cpp @@ -96,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); @@ -114,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; } @@ -131,7 +131,7 @@ 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; } diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 068f9bc32..24f3fdee8 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -66,7 +66,7 @@ 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; } @@ -74,7 +74,7 @@ RobotClient::frame_system_config from_proto(const FrameSystemConfig& proto) { RobotClient::frame_system_config fsconfig; 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 +85,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 +101,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 +115,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; } @@ -454,7 +454,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/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 fa87ae56e..fe5aa59fe 100644 --- a/src/viam/sdk/services/private/motion_client.cpp +++ b/src/viam/sdk/services/private/motion_client.cpp @@ -351,8 +351,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 fcc28dbcf..0f81af575 100644 --- a/src/viam/sdk/services/private/motion_server.cpp +++ b/src/viam/sdk/services/private/motion_server.cpp @@ -354,8 +354,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 da4e1bf65..a165eda58 100644 --- a/src/viam/sdk/services/private/navigation_client.cpp +++ b/src/viam/sdk/services/private/navigation_client.cpp @@ -36,7 +36,7 @@ NavigationClient::NavigationClient(std::string name, std::shared_ptr NavigationClient::get_waypoints(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetWaypoints) - .with([&](auto& request) { *request.mutable_extra() = map_to_struct(extra); }) + .with([&](auto& request) { *request.mutable_extra() = v2::to_proto(extra); }) .invoke([](auto& response) { std::vector ret; repeatedPtrToVec(response.waypoints(), ret, from_proto); @@ -74,7 +74,7 @@ void NavigationClient::add_waypoint(const geo_point& location, const ProtoStruct return make_client_helper(this, *stub_, &StubType::AddWaypoint) .with([&](auto& request) { *request.mutable_location() = v2::to_proto(location); - *request.mutable_extra() = map_to_struct(extra); + *request.mutable_extra() = v2::to_proto(extra); }) .invoke([](auto& response) {}); } @@ -83,14 +83,14 @@ 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); }) + .with([&](auto& request) { *request.mutable_extra() = v2::to_proto(extra); }) .invoke([](auto& response) { std::vector ret; repeatedPtrToVec(response.obstacles(), ret); @@ -100,7 +100,7 @@ std::vector NavigationClient::get_obstacles(const ProtoStruct& ext 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); }) + .with([&](auto& request) { *request.mutable_extra() = v2::to_proto(extra); }) .invoke([](auto& response) { std::vector ret; repeatedPtrToVec(response.paths(), ret, from_proto); @@ -116,8 +116,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 5d8bdc2c2..f803918fa 100644 --- a/src/viam/sdk/services/private/navigation_server.cpp +++ b/src/viam/sdk/services/private/navigation_server.cpp @@ -122,8 +122,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/tests/mocks/mock_robot.cpp b/src/viam/sdk/tests/mocks/mock_robot.cpp index 43b9b509a..7f9e23a13 100644 --- a/src/viam/sdk/tests/mocks/mock_robot.cpp +++ b/src/viam/sdk/tests/mocks/mock_robot.cpp @@ -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_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 a14bfa917..db950355a 100644 --- a/src/viam/sdk/tests/test_resource.cpp +++ b/src/viam/sdk/tests/test_resource.cpp @@ -246,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()); }); } From 7b7da5a99b00ee3cf135f0aef6f36a5f8d471565 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 3 Dec 2024 10:26:58 -0500 Subject: [PATCH 20/29] remove ctor semicolon --- src/viam/sdk/components/private/power_sensor_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/viam/sdk/components/private/power_sensor_client.cpp b/src/viam/sdk/components/private/power_sensor_client.cpp index 8f08aee69..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) From cf7f60b2622be3c7324479255cc2481cbddad3fb Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 3 Dec 2024 10:27:38 -0500 Subject: [PATCH 21/29] remove ctor semicolon --- src/viam/sdk/components/private/power_sensor_server.cpp | 2 +- src/viam/sdk/services/private/generic_server.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/viam/sdk/components/private/power_sensor_server.cpp b/src/viam/sdk/components/private/power_sensor_server.cpp index 2b5cecac3..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, diff --git a/src/viam/sdk/services/private/generic_server.cpp b/src/viam/sdk/services/private/generic_server.cpp index 15e4e7649..efecfcbfd 100644 --- a/src/viam/sdk/services/private/generic_server.cpp +++ b/src/viam/sdk/services/private/generic_server.cpp @@ -9,7 +9,7 @@ namespace sdk { namespace impl { GenericServiceServer::GenericServiceServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)) {}; + : ResourceServer(std::move(manager)) {} ::grpc::Status GenericServiceServer::DoCommand( ::grpc::ServerContext*, From 02ce15f5d4fc5a513682fc359dc2d5c0a95ab1ac Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Wed, 4 Dec 2024 14:56:13 -0500 Subject: [PATCH 22/29] remove geo geometry stuff i forgot to delete --- .../sdk/services/private/motion_client.cpp | 9 +++----- .../sdk/services/private/motion_server.cpp | 13 +++-------- src/viam/sdk/spatialmath/geometry.cpp | 23 ------------------- src/viam/sdk/spatialmath/geometry.hpp | 2 -- 4 files changed, 6 insertions(+), 41 deletions(-) diff --git a/src/viam/sdk/services/private/motion_client.cpp b/src/viam/sdk/services/private/motion_client.cpp index fa87ae56e..7c223d966 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 @@ -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() = v2::to_proto(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() = v2::to_proto(bounding_regions); }) .invoke([](auto& response) { return response.execution_id(); }); } diff --git a/src/viam/sdk/services/private/motion_server.cpp b/src/viam/sdk/services/private/motion_server.cpp index fcc28dbcf..27a74a66c 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 @@ -231,12 +232,8 @@ ::grpc::Status MotionServer::MoveOnGlobe( 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)); - } + std::vector obstacles = v2::from_proto(request->obstacles()); + std::vector bounding_regions = v2::from_proto(request->bounding_regions()); boost::optional heading; if (request->has_heading()) { @@ -249,10 +246,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, diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 54d6853bc..319d94b49 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -109,29 +109,6 @@ bool operator==(const geo_geometry& lhs, const geo_geometry& rhs) { return std::tie(lhs.location, lhs.geometries) == std::tie(rhs.location, rhs.geometries); } -common::v1::GeoGeometry geo_geometry::to_proto() const { - common::v1::GeoGeometry proto; - *proto.mutable_location() = v2::to_proto(location); - - for (const auto& geometry : geometries) { - *proto.mutable_geometries()->Add() = v2::to_proto(geometry); - } - - return proto; -} - -geo_geometry geo_geometry::from_proto(const common::v1::GeoGeometry& proto) { - struct geo_geometry geo_geometry; - - geo_geometry.location = v2::from_proto(proto.location()); - for (const auto& proto_geometry : proto.geometries()) { - auto geometry = v2::from_proto(proto_geometry); - geo_geometry.geometries.push_back(std::move(geometry)); - } - - return geo_geometry; -} - namespace proto_convert_details { void to_proto::operator()(const box& self, common::v1::RectangularPrism* proto) const { diff --git a/src/viam/sdk/spatialmath/geometry.hpp b/src/viam/sdk/spatialmath/geometry.hpp index 5e5e89a23..9895427ea 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -86,8 +86,6 @@ 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); }; From 2ec0610f9568592fe02eb8f38dad9a7a3be1e167 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 5 Dec 2024 09:21:53 -0500 Subject: [PATCH 23/29] set const --- src/viam/sdk/services/private/motion_server.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/viam/sdk/services/private/motion_server.cpp b/src/viam/sdk/services/private/motion_server.cpp index 27a74a66c..28e7f4c93 100644 --- a/src/viam/sdk/services/private/motion_server.cpp +++ b/src/viam/sdk/services/private/motion_server.cpp @@ -232,8 +232,9 @@ ::grpc::Status MotionServer::MoveOnGlobe( 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 = v2::from_proto(request->obstacles()); - std::vector bounding_regions = v2::from_proto(request->bounding_regions()); + const std::vector obstacles = v2::from_proto(request->obstacles()); + const std::vector bounding_regions = + v2::from_proto(request->bounding_regions()); boost::optional heading; if (request->has_heading()) { From 369e321a3b477cc924b7866959e08efeac6966ac Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:00:34 -0500 Subject: [PATCH 24/29] add or relocate nolints --- src/viam/sdk/common/proto_convert.hpp | 2 +- src/viam/sdk/common/proto_value.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/viam/sdk/common/proto_convert.hpp b/src/viam/sdk/common/proto_convert.hpp index b70d28f18..998fc6bb4 100644 --- a/src/viam/sdk/common/proto_convert.hpp +++ b/src/viam/sdk/common/proto_convert.hpp @@ -53,7 +53,7 @@ struct to_proto_impl { // Implementation struct for the omni-from_proto callable defined below. struct from_proto_impl { template - auto operator()(const ProtoType& proto) const { + auto operator()(const ProtoType& proto) const { // NOLINT(misc-no-recursion) return from_proto{}(&proto); } }; diff --git a/src/viam/sdk/common/proto_value.cpp b/src/viam/sdk/common/proto_value.cpp index 580941c2e..1521db29a 100644 --- a/src/viam/sdk/common/proto_value.cpp +++ b/src/viam/sdk/common/proto_value.cpp @@ -254,8 +254,8 @@ void to_proto::operator()(const ProtoValue& self, google::protobuf:: self.vtable_.to_value(self.self_.get(), v); } -ProtoValue from_proto::operator()( - const google::protobuf::Value* v) const { // NOLINT(misc-no-recursion) +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()); @@ -292,8 +292,8 @@ void to_proto::operator()(const ProtoStruct& self, google::protobuf } } -ProtoStruct from_proto::operator()( - const google::protobuf::Struct* s) const { // 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()) { From 97841a583132bb2bf76967a1740b30603e2817f7 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:15:27 -0500 Subject: [PATCH 25/29] change include order and replace macro with explicit namespaces --- src/viam/sdk/common/linear_algebra.hpp | 9 ++++++--- src/viam/sdk/common/pose.hpp | 9 ++++++--- src/viam/sdk/common/proto_convert.hpp | 14 ++------------ src/viam/sdk/common/world_state.hpp | 8 ++++++-- src/viam/sdk/referenceframe/frame.hpp | 9 ++++++--- src/viam/sdk/spatialmath/geometry.hpp | 9 ++++++--- src/viam/sdk/spatialmath/orientation.hpp | 9 ++++++--- src/viam/sdk/spatialmath/orientation_types.hpp | 9 ++++++--- 8 files changed, 44 insertions(+), 32 deletions(-) diff --git a/src/viam/sdk/common/linear_algebra.hpp b/src/viam/sdk/common/linear_algebra.hpp index 690595aa2..e9045f38f 100644 --- a/src/viam/sdk/common/linear_algebra.hpp +++ b/src/viam/sdk/common/linear_algebra.hpp @@ -7,11 +7,14 @@ #include -VIAM_SDK_API_FWD_NAMESPACE_BEGIN(common) +namespace viam { +namespace common { +namespace v1 { class Vector3; - -VIAM_SDK_API_FWD_NAMESPACE_END +} +} // namespace common +} // namespace viam namespace viam { namespace sdk { diff --git a/src/viam/sdk/common/pose.hpp b/src/viam/sdk/common/pose.hpp index 548e063eb..2eddd18ec 100644 --- a/src/viam/sdk/common/pose.hpp +++ b/src/viam/sdk/common/pose.hpp @@ -4,12 +4,15 @@ #include -VIAM_SDK_API_FWD_NAMESPACE_BEGIN(common) +namespace viam { +namespace common { +namespace v1 { class Pose; class PoseInFrame; - -VIAM_SDK_API_FWD_NAMESPACE_END +} // namespace v1 +} // namespace common +} // namespace viam namespace viam { namespace sdk { diff --git a/src/viam/sdk/common/proto_convert.hpp b/src/viam/sdk/common/proto_convert.hpp index 998fc6bb4..b5d16f8bd 100644 --- a/src/viam/sdk/common/proto_convert.hpp +++ b/src/viam/sdk/common/proto_convert.hpp @@ -1,21 +1,11 @@ #pragma once +#include + #include #include #include -#include - -#define VIAM_SDK_API_FWD_NAMESPACE_BEGIN(module_ns_name) \ - namespace viam { \ - namespace module_ns_name { \ - namespace v1 { - -#define VIAM_SDK_API_FWD_NAMESPACE_END \ - } \ - } \ - } - namespace viam { namespace sdk { diff --git a/src/viam/sdk/common/world_state.hpp b/src/viam/sdk/common/world_state.hpp index a76275d16..aa9fe62b2 100644 --- a/src/viam/sdk/common/world_state.hpp +++ b/src/viam/sdk/common/world_state.hpp @@ -6,13 +6,17 @@ #include #include -VIAM_SDK_API_FWD_NAMESPACE_BEGIN(common) +namespace viam { +namespace common { +namespace v1 { class GeometriesInFrame; class Transform; class WorldState; -VIAM_SDK_API_FWD_NAMESPACE_END +} // namespace v1 +} // namespace common +} // namespace viam namespace viam { namespace sdk { diff --git a/src/viam/sdk/referenceframe/frame.hpp b/src/viam/sdk/referenceframe/frame.hpp index 0aaed7540..9d5dfd3ca 100644 --- a/src/viam/sdk/referenceframe/frame.hpp +++ b/src/viam/sdk/referenceframe/frame.hpp @@ -6,11 +6,14 @@ #include #include -VIAM_SDK_API_FWD_NAMESPACE_BEGIN(app) +namespace viam { +namespace app { +namespace v1 { class Frame; - -VIAM_SDK_API_FWD_NAMESPACE_END +} +} // namespace app +} // namespace viam namespace viam { namespace sdk { diff --git a/src/viam/sdk/spatialmath/geometry.hpp b/src/viam/sdk/spatialmath/geometry.hpp index 9895427ea..127da0e67 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -9,7 +9,9 @@ #include #include -VIAM_SDK_API_FWD_NAMESPACE_BEGIN(common) +namespace viam { +namespace common { +namespace v1 { class RectangularPrism; class Sphere; @@ -20,8 +22,9 @@ class GetGeometriesResponse; class GeoPoint; class GeoGeometry; - -VIAM_SDK_API_FWD_NAMESPACE_END +} // namespace v1 +} // namespace common +} // namespace viam namespace viam { namespace sdk { diff --git a/src/viam/sdk/spatialmath/orientation.hpp b/src/viam/sdk/spatialmath/orientation.hpp index 4089f5653..1ce7dca17 100644 --- a/src/viam/sdk/spatialmath/orientation.hpp +++ b/src/viam/sdk/spatialmath/orientation.hpp @@ -8,11 +8,14 @@ #include #include -VIAM_SDK_API_FWD_NAMESPACE_BEGIN(app) +namespace viam { +namespace app { +namespace v1 { class Orientation; - -VIAM_SDK_API_FWD_NAMESPACE_END +} +} // namespace app +} // namespace viam namespace viam { namespace sdk { diff --git a/src/viam/sdk/spatialmath/orientation_types.hpp b/src/viam/sdk/spatialmath/orientation_types.hpp index e1d9df6b7..2cde64cfb 100644 --- a/src/viam/sdk/spatialmath/orientation_types.hpp +++ b/src/viam/sdk/spatialmath/orientation_types.hpp @@ -2,11 +2,14 @@ #include -VIAM_SDK_API_FWD_NAMESPACE_BEGIN(app) +namespace viam { +namespace app { +namespace v1 { class Translation; - -VIAM_SDK_API_FWD_NAMESPACE_END +} +} // namespace app +} // namespace viam namespace viam { namespace sdk { From fbe2ea9e16c81402e629c3ae1bf06f6daadd0b08 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:29:23 -0500 Subject: [PATCH 26/29] document args_t trick and set container to mp_list --- src/viam/sdk/common/proto_convert.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/viam/sdk/common/proto_convert.hpp b/src/viam/sdk/common/proto_convert.hpp index b5d16f8bd..1c3148785 100644 --- a/src/viam/sdk/common/proto_convert.hpp +++ b/src/viam/sdk/common/proto_convert.hpp @@ -23,9 +23,12 @@ struct to_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>>; +using ProtoArgType = std::remove_pointer_t< + boost::mp11::mp_back>>; // Implementation struct for the omni-to_proto callable defined below. struct to_proto_impl { From 670d2ef5271d1436bafcc3117263044ad48006b9 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:34:13 -0500 Subject: [PATCH 27/29] make to/from repeated field its own function object and a private detail --- src/viam/sdk/CMakeLists.txt | 1 - .../common/private/repeated_ptr_convert.hpp | 46 +++++++++++++++++ src/viam/sdk/common/proto_convert_vector.hpp | 51 ------------------- src/viam/sdk/common/world_state.cpp | 13 ++--- src/viam/sdk/robot/client.cpp | 4 +- .../sdk/services/private/motion_client.cpp | 8 +-- .../sdk/services/private/motion_server.cpp | 6 +-- .../services/private/navigation_client.cpp | 10 ++-- .../services/private/navigation_server.cpp | 10 ++-- src/viam/sdk/spatialmath/geometry.cpp | 6 +-- src/viam/sdk/spatialmath/geometry.hpp | 2 +- 11 files changed, 77 insertions(+), 80 deletions(-) create mode 100644 src/viam/sdk/common/private/repeated_ptr_convert.hpp delete mode 100644 src/viam/sdk/common/proto_convert_vector.hpp diff --git a/src/viam/sdk/CMakeLists.txt b/src/viam/sdk/CMakeLists.txt index 49f89c66f..fafeb316b 100644 --- a/src/viam/sdk/CMakeLists.txt +++ b/src/viam/sdk/CMakeLists.txt @@ -143,7 +143,6 @@ target_sources(viamsdk ../../viam/sdk/common/linear_algebra.hpp ../../viam/sdk/common/pose.hpp ../../viam/sdk/common/proto_convert.hpp - ../../viam/sdk/common/proto_convert_vector.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/private/repeated_ptr_convert.hpp b/src/viam/sdk/common/private/repeated_ptr_convert.hpp new file mode 100644 index 000000000..25e3eea0a --- /dev/null +++ b/src/viam/sdk/common/private/repeated_ptr_convert.hpp @@ -0,0 +1,46 @@ +#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; + } +}; + +constexpr to_repeated_field_ to_repeated_field; +constexpr from_repeated_field_ from_repeated_field; + +} // namespace impl +} // namespace sdk +} // namespace viam diff --git a/src/viam/sdk/common/proto_convert_vector.hpp b/src/viam/sdk/common/proto_convert_vector.hpp deleted file mode 100644 index da5cf5f2b..000000000 --- a/src/viam/sdk/common/proto_convert_vector.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include - -#include - -#include - -namespace google { -namespace protobuf { - -template -class RepeatedPtrField; - -} -} // namespace google - -namespace viam { -namespace sdk { -namespace proto_convert_details { - -template -struct to_proto> { - void operator()( - const std::vector& vec, - ::google::protobuf::RepeatedPtrField>* ptr_field) const { - ptr_field->Reserve(vec.size()); - for (const auto& elem : vec) { - to_proto{}(elem, ptr_field->Add()); - } - } -}; - -template -struct from_proto<::google::protobuf::RepeatedPtrField> { - auto operator()(const ::google::protobuf::RepeatedPtrField* ptr_field) const { - std::vector> result; - result.reserve(ptr_field->size()); - - for (const auto* elem : - boost::make_iterator_range(ptr_field->pointer_begin(), ptr_field->pointer_end())) { - result.push_back(from_proto{}(elem)); - } - - return result; - } -}; - -} // namespace proto_convert_details -} // namespace sdk -} // namespace viam diff --git a/src/viam/sdk/common/world_state.cpp b/src/viam/sdk/common/world_state.cpp index c5ea3f463..8b4fb7503 100644 --- a/src/viam/sdk/common/world_state.cpp +++ b/src/viam/sdk/common/world_state.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include namespace viam { namespace sdk { @@ -38,13 +38,13 @@ namespace proto_convert_details { void to_proto::operator()( const WorldState::geometries_in_frame& self, common::v1::GeometriesInFrame* proto) const { - *(proto->mutable_geometries()) = v2::to_proto(self.geometries); + *(proto->mutable_geometries()) = impl::to_repeated_field(self.geometries); *(proto->mutable_reference_frame()) = self.reference_frame; } WorldState::geometries_in_frame from_proto::operator()( const common::v1::GeometriesInFrame* proto) const { - return {v2::from_proto(proto->geometries()), proto->reference_frame()}; + return {impl::from_repeated_field(proto->geometries()), proto->reference_frame()}; } void to_proto::operator()(const WorldState::transform& self, @@ -68,13 +68,14 @@ WorldState::transform from_proto::operator()( } void to_proto::operator()(const WorldState& self, common::v1::WorldState* proto) const { - *(proto->mutable_obstacles()) = v2::to_proto(self.obstacles()); - *(proto->mutable_transforms()) = v2::to_proto(self.transforms()); + *(proto->mutable_obstacles()) = impl::to_repeated_field(self.obstacles()); + *(proto->mutable_transforms()) = impl::to_repeated_field(self.transforms()); } WorldState from_proto::operator()( const common::v1::WorldState* proto) const { - return WorldState(v2::from_proto(proto->obstacles()), v2::from_proto(proto->transforms())); + return WorldState(impl::from_repeated_field(proto->obstacles()), + impl::from_repeated_field(proto->transforms())); } } // namespace proto_convert_details diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 24f3fdee8..3674fd133 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -368,7 +368,7 @@ std::vector RobotClient::get_frame_system_conf viam::robot::v1::FrameSystemConfigResponse resp; ClientContext ctx; - *(req.mutable_supplemental_transforms()) = v2::to_proto(additional_transforms); + *(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)) { @@ -397,7 +397,7 @@ pose_in_frame RobotClient::transform_pose( *req.mutable_source() = v2::to_proto(query); *req.mutable_destination() = std::move(destination); - *req.mutable_supplemental_transforms() = v2::to_proto(additional_transforms); + *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)) { diff --git a/src/viam/sdk/services/private/motion_client.cpp b/src/viam/sdk/services/private/motion_client.cpp index a14302f3d..115fd5280 100644 --- a/src/viam/sdk/services/private/motion_client.cpp +++ b/src/viam/sdk/services/private/motion_client.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include @@ -246,13 +246,13 @@ std::string MotionClient::move_on_globe( request.set_heading(*heading); } - *request.mutable_obstacles() = v2::to_proto(obstacles); + *request.mutable_obstacles() = impl::to_repeated_field(obstacles); if (motion_configuration) { *request.mutable_motion_configuration() = to_proto(*motion_configuration); } - *request.mutable_bounding_regions() = v2::to_proto(bounding_regions); + *request.mutable_bounding_regions() = impl::to_repeated_field(bounding_regions); }) .invoke([](auto& response) { return response.execution_id(); }); } @@ -268,7 +268,7 @@ pose_in_frame MotionClient::get_pose( *request.mutable_component_name() = component_name.to_proto(); *request.mutable_destination_frame() = destination_frame; *request.mutable_supplemental_transforms() = - v2::to_proto(supplemental_transforms); + impl::to_repeated_field(supplemental_transforms); }) .invoke([](auto& response) { return v2::from_proto(response.pose()); }); } diff --git a/src/viam/sdk/services/private/motion_server.cpp b/src/viam/sdk/services/private/motion_server.cpp index 9110f52cc..a703ba587 100644 --- a/src/viam/sdk/services/private/motion_server.cpp +++ b/src/viam/sdk/services/private/motion_server.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include #include @@ -232,9 +232,9 @@ ::grpc::Status MotionServer::MoveOnGlobe( 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()); - const std::vector obstacles = v2::from_proto(request->obstacles()); + const std::vector obstacles = impl::from_repeated_field(request->obstacles()); const std::vector bounding_regions = - v2::from_proto(request->bounding_regions()); + impl::from_repeated_field(request->bounding_regions()); boost::optional heading; if (request->has_heading()) { diff --git a/src/viam/sdk/services/private/navigation_client.cpp b/src/viam/sdk/services/private/navigation_client.cpp index aaf0a272e..5b9bb73db 100644 --- a/src/viam/sdk/services/private/navigation_client.cpp +++ b/src/viam/sdk/services/private/navigation_client.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include @@ -21,7 +21,7 @@ namespace proto_convert_details { template <> struct from_proto { Navigation::Path operator()(const service::navigation::v1::Path* proto) const { - return {proto->destination_waypoint_id(), v2::from_proto(proto->geopoints())}; + return {proto->destination_waypoint_id(), impl::from_repeated_field(proto->geopoints())}; } }; @@ -72,7 +72,7 @@ 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() = v2::to_proto(extra); }) - .invoke([](auto& response) { return v2::from_proto(response.waypoints()); }); + .invoke([](auto& response) { return impl::from_repeated_field(response.waypoints()); }); } void NavigationClient::add_waypoint(const geo_point& location, const ProtoStruct& extra) { @@ -96,13 +96,13 @@ void NavigationClient::remove_waypoint(const std::string id, const ProtoStruct& std::vector NavigationClient::get_obstacles(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetObstacles) .with([&](auto& request) { *request.mutable_extra() = v2::to_proto(extra); }) - .invoke([](auto& response) { return v2::from_proto(response.obstacles()); }); + .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() = v2::to_proto(extra); }) - .invoke([](auto& response) { return v2::from_proto(response.paths()); }); + .invoke([](auto& response) { return impl::from_repeated_field(response.paths()); }); } NavigationClient::Properties NavigationClient::get_properties() { diff --git a/src/viam/sdk/services/private/navigation_server.cpp b/src/viam/sdk/services/private/navigation_server.cpp index 27ea9c3e6..d031cfcd6 100644 --- a/src/viam/sdk/services/private/navigation_server.cpp +++ b/src/viam/sdk/services/private/navigation_server.cpp @@ -19,7 +19,7 @@ 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()) = v2::to_proto(self.geopoints); + *(proto->mutable_geopoints()) = impl::to_repeated_field(self.geopoints); } }; @@ -72,7 +72,8 @@ ::grpc::Status NavigationServer::GetWaypoints(::grpc::ServerContext*, GetWaypointsResponse* response) noexcept { return make_service_helper( "NavigationServer::GetWaypoints", this, request)([&](auto& helper, auto& nav) { - *(response->mutable_waypoints()) = v2::to_proto(nav->get_waypoints(helper.getExtra())); + *(response->mutable_waypoints()) = + impl::to_repeated_field(nav->get_waypoints(helper.getExtra())); }); } @@ -97,7 +98,8 @@ ::grpc::Status NavigationServer::GetObstacles(::grpc::ServerContext*, GetObstaclesResponse* response) noexcept { return make_service_helper( "NavigationServer::GetObstacles", this, request)([&](auto& helper, auto& nav) { - *(response->mutable_obstacles()) = v2::to_proto(nav->get_obstacles(helper.getExtra())); + *(response->mutable_obstacles()) = + impl::to_repeated_field(nav->get_obstacles(helper.getExtra())); }); } @@ -106,7 +108,7 @@ ::grpc::Status NavigationServer::GetPaths(::grpc::ServerContext*, GetPathsResponse* response) noexcept { return make_service_helper( "NavigationServer::GetPaths", this, request)([&](auto& helper, auto& nav) { - *response->mutable_paths() = v2::to_proto(nav->get_paths(helper.getExtra())); + *response->mutable_paths() = impl::to_repeated_field(nav->get_paths(helper.getExtra())); }); } diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 319d94b49..6a8aa925d 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -198,17 +198,17 @@ geo_point from_proto::operator()(const common::v1::GeoPoin void to_proto::operator()(const geo_geometry& self, common::v1::GeoGeometry* proto) const { *(proto->mutable_location()) = v2::to_proto(self.location); - *(proto->mutable_geometries()) = v2::to_proto(self.geometries); + *(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()), v2::from_proto(proto->geometries())}; + return {v2::from_proto(proto->location()), impl::from_repeated_field(proto->geometries())}; } std::vector from_proto::operator()( const common::v1::GetGeometriesResponse* proto) const { - return v2::from_proto(proto->geometries()); + return impl::from_repeated_field(proto->geometries()); } } // namespace proto_convert_details diff --git a/src/viam/sdk/spatialmath/geometry.hpp b/src/viam/sdk/spatialmath/geometry.hpp index 127da0e67..152ac41f5 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -5,8 +5,8 @@ #include #include +#include #include -#include #include namespace viam { From 96f2742905171893da0e81bf23d006f04b784a44 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 5 Dec 2024 14:05:40 -0500 Subject: [PATCH 28/29] use static_const for callable objects --- .../common/private/repeated_ptr_convert.hpp | 10 ++++++++-- src/viam/sdk/common/proto_convert.hpp | 20 +++++++++++++++++-- src/viam/sdk/spatialmath/geometry.hpp | 2 +- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/viam/sdk/common/private/repeated_ptr_convert.hpp b/src/viam/sdk/common/private/repeated_ptr_convert.hpp index 25e3eea0a..030eba8c5 100644 --- a/src/viam/sdk/common/private/repeated_ptr_convert.hpp +++ b/src/viam/sdk/common/private/repeated_ptr_convert.hpp @@ -38,8 +38,14 @@ struct from_repeated_field_ { } }; -constexpr to_repeated_field_ to_repeated_field; -constexpr from_repeated_field_ from_repeated_field; +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 diff --git a/src/viam/sdk/common/proto_convert.hpp b/src/viam/sdk/common/proto_convert.hpp index 1c3148785..1e2e82626 100644 --- a/src/viam/sdk/common/proto_convert.hpp +++ b/src/viam/sdk/common/proto_convert.hpp @@ -11,6 +11,16 @@ 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 @@ -55,15 +65,21 @@ struct from_proto_impl { 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 proto_convert_details::to_proto_impl to_proto{}; +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 proto_convert_details::from_proto_impl from_proto{}; +constexpr auto& from_proto = + proto_convert_details::static_const::value; + +} // namespace } // namespace v2 diff --git a/src/viam/sdk/spatialmath/geometry.hpp b/src/viam/sdk/spatialmath/geometry.hpp index 152ac41f5..2935676d3 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -5,7 +5,6 @@ #include #include -#include #include #include @@ -22,6 +21,7 @@ class GetGeometriesResponse; class GeoPoint; class GeoGeometry; + } // namespace v1 } // namespace common } // namespace viam From 40cf73bb7d7fb46a791296e9c63375874ecd8eb8 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 5 Dec 2024 15:38:59 -0500 Subject: [PATCH 29/29] include repeated ptr conversion --- src/viam/sdk/robot/client.cpp | 1 + src/viam/sdk/services/private/navigation_server.cpp | 1 + src/viam/sdk/spatialmath/geometry.cpp | 1 + 3 files changed, 3 insertions(+) diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 3674fd133..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 diff --git a/src/viam/sdk/services/private/navigation_server.cpp b/src/viam/sdk/services/private/navigation_server.cpp index d031cfcd6..d3764a3a0 100644 --- a/src/viam/sdk/services/private/navigation_server.cpp +++ b/src/viam/sdk/services/private/navigation_server.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 6a8aa925d..5f05b85f3 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -9,6 +9,7 @@ #include #include +#include #include namespace viam {