Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/viam/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ target_sources(viamsdk
common/exception.cpp
common/instance.cpp
common/linear_algebra.cpp
common/mesh.cpp
common/pose.cpp
common/proto_value.cpp
common/utils.cpp
Expand Down Expand Up @@ -177,6 +178,7 @@ target_sources(viamsdk
../../viam/sdk/common/exception.hpp
../../viam/sdk/common/instance.hpp
../../viam/sdk/common/linear_algebra.hpp
../../viam/sdk/common/mesh.hpp
../../viam/sdk/common/pose.hpp
../../viam/sdk/common/proto_convert.hpp
../../viam/sdk/common/proto_value.hpp
Expand Down
22 changes: 22 additions & 0 deletions src/viam/sdk/common/mesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <viam/sdk/common/mesh.hpp>

#include <viam/sdk/common/utils.hpp>

#include <common/v1/common.pb.h>

namespace viam {
namespace sdk {

namespace proto_convert_details {
void to_proto_impl<mesh>::operator()(const mesh& self, common::v1::Mesh* proto) const {
proto->set_content_type(self.content_type);
proto->set_mesh(bytes_to_string(self.data));
}

mesh from_proto_impl<common::v1::Mesh>::operator()(const common::v1::Mesh* proto) const {
return mesh{proto->content_type(), string_to_bytes(proto->mesh())};
}

} // namespace proto_convert_details
} // namespace sdk
} // namespace viam
41 changes: 41 additions & 0 deletions src/viam/sdk/common/mesh.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <viam/sdk/common/proto_convert.hpp>

#include <string>
#include <vector>

namespace viam {
namespace common {
namespace v1 {

class Mesh;

} // namespace v1
} // namespace common
} // namespace viam

namespace viam {
namespace sdk {

struct mesh {
std::string content_type;
std::vector<unsigned char> data;
};

namespace proto_convert_details {

template <>
struct to_proto_impl<mesh> {
void operator()(const mesh&, common::v1::Mesh*) const;
};

template <>
struct from_proto_impl<common::v1::Mesh> {
mesh operator()(const common::v1::Mesh*) const;
};

} // namespace proto_convert_details

} // namespace sdk
} // namespace viam
12 changes: 12 additions & 0 deletions src/viam/sdk/components/arm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// @brief Defines an `Arm` component
#pragma once

#include <common/v1/common.pb.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the include you'll want to delete, and add mesh.hpp to the include block below

#include <string>

#include <boost/optional/optional.hpp>
Expand Down Expand Up @@ -142,6 +143,17 @@ class Arm : public Component, public Stoppable {
return get_kinematics({});
}

/// @brief Returns `3DModel`s associated with the calling arm
/// @param extra Any additional arguments to the method
/// @return A map of `3DModel`s associated with the calling arm
virtual std::map<std::string, common::v1::Mesh> get_3d_models(const ProtoStruct& extra) = 0;

/// @brief Returns `3DModel`s associated with the calling arm
/// @return A map of `3DModel`s associated with the calling arm
inline std::map<std::string, common::v1::Mesh> get_3d_models() {
return get_3d_models({});
}

/// @brief Returns `GeometryConfig`s associated with the calling arm
inline std::vector<GeometryConfig> get_geometries() {
return get_geometries({});
Expand Down
9 changes: 9 additions & 0 deletions src/viam/sdk/components/private/arm_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ Arm::KinematicsData ArmClient::get_kinematics(const ProtoStruct& extra) {
});
}

std::map<std::string, common::v1::Mesh> ArmClient::get_3d_models(const ProtoStruct& extra) {
return make_client_helper(this, *stub_, &StubType::Get3DModels)
.with(extra)
.invoke([](auto& response) {
return std::map<std::string, common::v1::Mesh>(response.models().begin(),
response.models().end());
});
}

std::vector<GeometryConfig> ArmClient::get_geometries(const ProtoStruct& extra) {
return make_client_helper(this, *stub_, &StubType::GetGeometries)
.with(extra)
Expand Down
2 changes: 2 additions & 0 deletions src/viam/sdk/components/private/arm_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class ArmClient : public Arm {
void stop(const ProtoStruct& extra) override;
ProtoStruct do_command(const ProtoStruct& command) override;
Arm::KinematicsData get_kinematics(const ProtoStruct& extra) override;
std::map<std::string, common::v1::Mesh> get_3d_models(const ProtoStruct& extra) override;
std::vector<GeometryConfig> get_geometries(const ProtoStruct& extra) override;

// Using declarations to introduce convenience overloads of interface which do not need to be
// passed the ProtoStruct parameter.
using Arm::get_3d_models;
using Arm::get_end_position;
using Arm::get_geometries;
using Arm::get_joint_positions;
Expand Down
12 changes: 12 additions & 0 deletions src/viam/sdk/components/private/arm_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ ::grpc::Status ArmServer::GetKinematics(
});
}

::grpc::Status ArmServer::Get3DModels(::grpc::ServerContext*,
const ::viam::common::v1::Get3DModelsRequest* request,
::viam::common::v1::Get3DModelsResponse* response) noexcept {
return make_service_helper<Arm>(
"ArmServer::Get3DModels", this, request)([&](auto& helper, auto& arm) {
const std::map<std::string, common::v1::Mesh> models =
arm->get_3d_models(helper.getExtra());

response->mutable_models()->insert(models.begin(), models.end());
});
}

::grpc::Status ArmServer::GetGeometries(
::grpc::ServerContext*,
const ::viam::common::v1::GetGeometriesRequest* request,
Expand Down
4 changes: 4 additions & 0 deletions src/viam/sdk/components/private/arm_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class ArmServer : public ResourceServer, public viam::component::arm::v1::ArmSer
const ::viam::common::v1::GetKinematicsRequest* request,
::viam::common::v1::GetKinematicsResponse* response) noexcept override;

::grpc::Status Get3DModels(::grpc::ServerContext* context,
const ::viam::common::v1::Get3DModelsRequest* request,
::viam::common::v1::Get3DModelsResponse* response) noexcept override;

::grpc::Status GetGeometries(
::grpc::ServerContext* context,
const ::viam::common::v1::GetGeometriesRequest* request,
Expand Down
8 changes: 8 additions & 0 deletions src/viam/sdk/tests/mocks/mock_arm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace viam {
namespace sdktests {
namespace arm {

std::map<std::string, common::v1::Mesh> fake_3d_models() {
return {{"model1", common::v1::Mesh::default_instance()}};
}

sdk::Arm::KinematicsData fake_kinematics() {
return sdk::Arm::KinematicsDataSVA{{std::vector<unsigned char>{1, 2, 3, 4}}};
}
Expand Down Expand Up @@ -59,6 +63,10 @@ std::vector<sdk::GeometryConfig> MockArm::get_geometries(const sdk::ProtoStruct&
return fake_geometries();
}

std::map<std::string, common::v1::Mesh> MockArm::get_3d_models(const sdk::ProtoStruct&) {
return fake_3d_models();
}

} // namespace arm
} // namespace sdktests
} // namespace viam
3 changes: 2 additions & 1 deletion src/viam/sdk/tests/mocks/mock_arm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace sdktests {
namespace arm {

sdk::Arm::KinematicsData fake_kinematics();
std::map<std::string, common::v1::Mesh> fake_3d_models();

class MockArm : public sdk::Arm {
public:
Expand All @@ -29,7 +30,7 @@ class MockArm : public sdk::Arm {
sdk::ProtoStruct do_command(const sdk::ProtoStruct& command) override;
sdk::Arm::KinematicsData get_kinematics(const sdk::ProtoStruct&) override;
std::vector<sdk::GeometryConfig> get_geometries(const sdk::ProtoStruct&) override;

std::map<std::string, common::v1::Mesh> get_3d_models(const sdk::ProtoStruct&) override;
sdk::pose current_location;
std::vector<double> joint_positions;
std::vector<std::vector<double>> move_thru_positions;
Expand Down
20 changes: 20 additions & 0 deletions src/viam/sdk/tests/test_arm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ BOOST_AUTO_TEST_CASE(test_do_command) {
});
}

BOOST_AUTO_TEST_CASE(test_get_3d_models) {
std::shared_ptr<MockArm> mock = MockArm::get_mock_arm();
client_to_mock_pipeline<Arm>(mock, [](Arm& client) {
const auto& models = client.get_3d_models();
const auto& expected = fake_3d_models();

// Compare keysets directly
std::set<std::string> model_keys;
std::set<std::string> expected_keys;
for (const auto& [key, _] : models) {
model_keys.insert(key);
}
for (const auto& [key, _] : expected) {
expected_keys.insert(key);
}
BOOST_CHECK_EQUAL_COLLECTIONS(
model_keys.begin(), model_keys.end(), expected_keys.begin(), expected_keys.end());
});
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace sdktests
Expand Down